線性代數:矩陣基本運算 矩陣怎么進行加減

本文將介紹矩陣的大部分基本運算,其次是矩陣的加減運算、矩陣的標量乘法、矩陣與矩陣的乘法、求轉置矩陣,并深入了解矩陣的行列式運算 。本文不涉及逆矩陣和矩陣秩的概念,以后再討論 。
矩陣的加法和減法的矩陣加減運算將接收兩個矩陣作為輸入,并輸出一個新的矩陣 。矩陣的加和減都是在組件級進行的,所以要加和減的矩陣必須有相同的維數 。
為了避免重復加減法的代碼,我們首先創建了一個可以接收運算函數的方法,它將對兩個矩陣的分量分別執行一些傳入運算 。然后在加法、減法或其他運算中直接調用它:
class Matrix {// ...componentWiseOperation(func, { rows }) {const newRows = rows.map((row, i) =>row.map((element, j) => func(this.rows[i][j], element)))return new Matrix(...newRows)}add(other) {return this.componentWiseOperation((a, b) => a + b, other)}subtract(other) {return this.componentWiseOperation((a, b) => a - b, other)}}const one = new Matrix([1, 2],[3, 4])const other = new Matrix([5, 6],[7, 8])console.log(one.add(other))// Matrix { rows: [ [ 6, 8 ], [ 10, 12 ] ] }console.log(other.subtract(one))// Matrix { rows: [ [ 4, 4 ], [ 4, 4 ] ]&n創業網bsp;}復制代碼矩陣的標量乘法矩陣的標量乘法類似于向量的縮放,即將矩陣中的每個元素乘以一個標量:
class Matrix {// ...scaleBy(number) {const newRows = this.rows.map(row =>row.map(element => element * number))return new Matrix(...newRows)}}const matrix = new Matrix([2, 3],[4, 5])console.log(matrix.scaleBy(2))// Matrix { rows: [ [ 4, 6 ], [ 8, 10 ] ] }復制代碼矩陣乘法當矩陣A和矩陣B的維數相容時,可以對這兩個矩陣進行矩陣乘法 。維數相容是指A的列數與B的行數相同,矩陣的乘積AB是通過計算A的每行與矩陣B的每列的點積得到的:

線性代數:矩陣基本運算  矩陣怎么進行加減

文章插圖

class Matrix {// ...multiply(other) {if (this.rows[0].length !== other.rows.length) {throw new Error('The number of columns of this matrix is not equal to the number of rows of the given matrix.')}const columns = other.columns()const newRows = this.rows.map(row =>columns.map(column => sum(row.map((element, i) => element * column[i])))&n創業網bsp;)return new Matrix(...newRows)}}const one = new Matrix([3, -4],[0, -3],[6, -2],[-1, 1])const other = new Matrix([3,2, -4],&nb創業網sp; [4, -3,5])console.log(one.multiply(other))// Matrix {//rows://[ [ -7, 18, -32 ],//[ -12, 9, -15 ],//[ 10, 18, -34 ],//[ 1, -5, 9 ] ]}復制代碼我們可以把矩陣乘法AB看作是連續應用兩個線性變換矩陣A和B 。為了更好地理解這個概念,看看我們的線性代數演示 。
下圖中的黃色部分是對紅色方塊應用線性變換C的結果 。線性變換C是矩陣乘法AB的結果,其中A是相對于Y軸反射的變換矩陣,B是執行剪切變換的矩陣 。
線性代數:矩陣基本運算  矩陣怎么進行加減

文章插圖

如果切換矩陣乘法中A和B的順序,會得到不同的結果,因為這相當于先應用B的剪切變換,再應用A的反射變換:
線性代數:矩陣基本運算  矩陣怎么進行加減

文章插圖

調換轉置矩陣由公式定義 。換句話說,我們通過翻轉矩陣的對角線得到轉置矩陣 。請注意,矩陣對角線上的元素不受轉置操作的影響 。
【線性代數:矩陣基本運算矩陣怎么進行加減】

    推薦閱讀