日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

python矩阵运算与线形代数_[译] 线性代数:矩阵基本运算

發(fā)布時間:2024/8/23 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python矩阵运算与线形代数_[译] 线性代数:矩阵基本运算 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

線性代數(shù):矩陣基本運算

在本文中,我們將介紹矩陣的大部分基本運算,依次是矩陣的加減法、矩陣的標量乘法、矩陣與矩陣的乘法、求轉(zhuǎn)置矩陣,以及深入了解矩陣的行列式運算。本文將不會涉及逆矩陣、矩陣的秩等概念,將來再探討它們。

矩陣的加減法

矩陣的加法與減法運算將接收兩個矩陣作為輸入,并輸出一個新的矩陣。矩陣的加法和減法都是在分量級別上進行的,因此要進行加減的矩陣必須有著相同的維數(shù)。

為了避免重復(fù)編寫加減法的代碼,我們先創(chuàng)建一個可以接收運算函數(shù)的方法,這個方法將對兩個矩陣的分量分別執(zhí)行傳入的某種運算。然后在加法、減法或者其它運算中直接調(diào)用它就行了: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 ] ] }

矩陣的標量乘法

矩陣的標量乘法與向量的縮放類似,就是將矩陣中的每個元素都乘上標量: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 ] ] }

矩陣乘法

當(dāng) A、B 兩個矩陣的維數(shù)是兼容的時候,就能對這兩個矩陣進行矩陣乘法。所謂維數(shù)兼容,指的是 A 的列數(shù)與 B 的行數(shù)相同。矩陣的乘積 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])))

)

return new Matrix(...newRows)

}

}

const one = new Matrix(

[3, -4],

[0, -3],

[6, -2],

[-1, 1]

)

const other = new Matrix(

[3, 2, -4],

[4, -3, 5]

)

console.log(one.multiply(other))

// Matrix {

// rows:

// [ [ -7, 18, -32 ],

// [ -12, 9, -15 ],

// [ 10, 18, -34 ],

// [ 1, -5, 9 ] ]}

我們可以把矩陣乘法 AB 視為先后應(yīng)用 A 和 B 兩個線性變換矩陣。為了更好地理解這種概念,可以看一看我們的 linear-algebra-demo。

下圖中黃色的部分就是對紅色方塊應(yīng)用線性變換 C 的結(jié)果。而線性變換 C 就是矩陣乘法 AB 的結(jié)果,其中 A 是做相對于 y 軸進行反射的變換矩陣,B 是做剪切變換的矩陣。

如果在矩陣乘法中調(diào)換 A 和 B 的順序,我們會得到一個不同的結(jié)果,因為相當(dāng)于先應(yīng)用了 B 的剪切變換,再應(yīng)用 A 的反射變換:

轉(zhuǎn)置

轉(zhuǎn)置矩陣

由公式

定義。換句話說,我們通過關(guān)于矩陣的對角線對其進行翻轉(zhuǎn)來得到轉(zhuǎn)置矩陣。需要注意的是,矩陣對角線上的元素不受轉(zhuǎn)置運算影響。class Matrix{

// ...

transpose() {

return new Matrix(...this.columns())

}

}

const matrix = new Matrix(

[0, 1, 2],

[3, 4, 5],

[6, 7, 8],

[9, 10, 11]

)

console.log(matrix.transpose())

// Matrix {

// rows: [

// [ 0, 3, 6, 9 ],

// [ 1, 4, 7, 10 ],

// [ 2, 5, 8, 11 ]

// ]

// }

行列式運算

矩陣的行列式運算將計算矩陣中的所有系數(shù),最后輸出一個數(shù)字。準確地說,行列式可以描述一個由矩陣行構(gòu)成的向量的相對幾何指標(比如在歐式空間中的有向面積、體積等空間概念)。更準確地說,矩陣 A 的行列式相當(dāng)于告訴你由 A 的行定義的方塊的體積。

矩陣的行列式運算如下所示:

矩陣的行列式運算如下所示:

我們的方法可以計算任意大小矩陣(只要其行列的數(shù)量相同)的行列式:class Matrix{

// ...

determinant() {

if (this.rows.length !== this.rows[0].length) {

throw new Error('Only matrices with the same number of rows and columns are supported.')

}

if (this.rows.length === 2) {

return this.rows[0][0] * this.rows[1][1] - this.rows[0][1] * this.rows[1][0]

}

const parts = this.rows[0].map((coef, index) => {

const matrixRows = this.rows.slice(1).map(row => [ ...row.slice(0, index), ...row.slice(index + 1)])

const matrix = new Matrix(...matrixRows)

const result = coef * matrix.determinant()

return index % 2 === 0 ? result : -result

})

return sum(parts)

}

}

const matrix2 = new Matrix(

[ 0, 3],

[-2, 1]

)

console.log(matrix2.determinant())

// 6

const matrix3 = new Matrix(

[2, -3, 1],

[2, 0, -1],

[1, 4, 5]

)

console.log(matrix3.determinant())

// 49

const matrix4 = new Matrix(

[3, 0, 2, -1],

[1, 2, 0, -2],

[4, 0, 6, -3],

[5, 0, 2, 0]

)

console.log(matrix4.determinant())

// 20

行列式可以告訴我們變換時對象被拉伸的程度。因此我們可以將其視為線性變換改變面積的因子。為了更好地理解這個概念,請參考 linear-algebra-demo:

在下圖中,我們可以看到對紅色的 1×1 方形進行線性變換后得到了一個 3×2 的長方形,面積從 1 變?yōu)榱?6,這個數(shù)字與線性變換矩陣的行列式值相同。

如果我們應(yīng)用一個剪切變換,可以看到方形會變成一個面積不變的平行四邊形。因此,剪切變換矩陣的行列式值等于 1:

如果行列式的值是負數(shù),則說明應(yīng)用線性變換后,空間被反轉(zhuǎn)了。比如在下圖中,我們可以看到變換前

的左邊,而變換后

的右邊。

如果變換的行列式為 0,則表示它會將所有空間都壓縮到一條線或一個點上。也就是說,計算一個給定矩陣的行列式是否為 0,可以判斷這個矩陣對應(yīng)的線性變換是否會將對象壓縮到更小的維度去。

在三維空間里,行列式可以告訴你體積縮放了多少:

變換行列式等于 0,意味著原來的空間會被完全壓縮成體積為 0 的空間。如前文所說,如果在 2 維空間中變換的行列式為 0,則意味著變換的結(jié)果將空間壓縮成了一條線或一個點;而在 3 維空間中變換的行列式為 0 意味著一個物體會被壓扁成一個平面,如下圖所示:

出處:https://juejin.im/post/5d107b00f265da1b67211a21

總結(jié)

以上是生活随笔為你收集整理的python矩阵运算与线形代数_[译] 线性代数:矩阵基本运算的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。