Skip to content

3 Matrix Operations

Süleyman Özarslan edited this page Sep 17, 2022 · 2 revisions

Mathematical Operations Between Matrices

Adding Matrices

func (matrix Matrix) Add(mx Matrix) Matrix

The Add function is used to add the two matrices. and it returns the result matrix.For adding, the two matrices must be the same size. Otherwise, the program is terminated with panic.

mx1 := matrix.Matrix{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
mx2 := matrix.Matrix{{9, 8, 7}, {6, 5, 4}, {3, 2, 1}}
resultMatrix := mx1.Add(mx2)

Subtracting Matrices

func (matrix Matrix) Subtract(mx Matrix) Matrix

Subtract function is used to extract the matrices and returns the result matrix. For subtraction, the two matrices must be the same size. Otherwise, the program is terminated with panic.

mx1 := matrix.Matrix{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
mx2 := matrix.Matrix{{9, 8, 7}, {6, 5, 4}, {3, 2, 1}}
resultMatrix := mx1.Subtract(mx2)

Matrix Multiplication

func (matrix Matrix) Dot(mx Matrix) Matrix

The Dot function is used to multiply the matrices and returns the result matrix. The number of columns of the first matrix and the number of rows of the second matrix must be the same for the operation to be performed. If not, the program is terminated with panic.

mx1 := matrix.Matrix{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
mx2 := matrix.Matrix{{9, 8}, {6, 5}, {3, 2}}
resultMatrix := mx1.Dot(mx2)

Mathematical Operations Between Matrices and Numbers

Summing a Number with Matrix Values (Plus)

func (matrix Matrix) Plus(value Col) Matrix

The plus function sums a given number with all the values in the matrix and returns the result matrix.

mx := matrix.Matrix{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
resultMatrix := mx.Plus(10)

Subtracting a Number from the Values in the Matrix (Minus)

func (matrix Matrix) Minus(value Col) Matrix

The minus function subtracts a given number from all values in the matrix and returns the result matrix.

mx := matrix.Matrix{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
resultMatrix := mx.Minus(10)

Multiply a Number with a Matrix

func (matrix Matrix) Multiply(value float64) Matrix

The Multiply function multiplies a given number with the values in the matrix and returns the resulting matrix.

mx := matrix.Matrix{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
resultMatrix := mx.Multiply(10)

Dividing the Matrix by a Number

func (matrix Matrix) Divide(value float64) Matrix

The Divide function divides all values in the matrix and the value given as a parameter and returns the result matrix.

mx := matrix.Matrix{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
resultMatrix := mx.Divide(10)

Transpose

func (matrix Matrix) T() Matrix

T function returns the transpose of a matrix

mx := matrix.Matrix{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
resultMatrix := mx.T()

Inverse

func (matrix Matrix) Inv() Matrix

Inv function returns the inverse of a matrix.

mx := matrix.Matrix{{20, 30, 40}, {7, 5, 6}, {1, 8, 9}}
resultMatrix := mx.Inv()

If the determinant of a matrix is 0, it cannot be inverted. If an inverse of a matrix with a determinant of 0 is attempted, the program will end with a panic. The Det() function can be used to check the determinant.

mx := matrix.Matrix{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
resultMatrix := mx.Inv() // Because the determinant is 0, the program is terminated with panic.

Conversion to Lower and Upper Triangle Matrix

Upper Triangle Matrix

func (matrix Matrix) UpperTriangle() Matrix

UpperTriangle function creates the upper triangle matrix and returns the result matrix

mx := matrix.Matrix{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
resultMatrix := mx.UpperTriangle()
1 2 3
0 -3 -6
0 0 0

Lower Triangle Matrix

func (matrix Matrix) LowerTriangle() Matrix

LowerTriangle function creates the lower triangle matrix and returns the result matrix

mx := matrix.Matrix{{9, 8, 7}, {6, 5, 4}, {3, 2, 1}}
resultMatrix := mx.LowerTriangle()
0 0 0
-6 -3 0
3 2 1