-
Notifications
You must be signed in to change notification settings - Fork 1
3 Matrix Operations
func (matrix Matrix) Add(mx Matrix) MatrixThe 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)func (matrix Matrix) Subtract(mx Matrix) MatrixSubtract 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)func (matrix Matrix) Dot(mx Matrix) MatrixThe 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)func (matrix Matrix) Plus(value Col) MatrixThe 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)func (matrix Matrix) Minus(value Col) MatrixThe 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)func (matrix Matrix) Multiply(value float64) MatrixThe 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)func (matrix Matrix) Divide(value float64) MatrixThe 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)func (matrix Matrix) T() MatrixT function returns the transpose of a matrix
mx := matrix.Matrix{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
resultMatrix := mx.T()func (matrix Matrix) Inv() MatrixInv 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.func (matrix Matrix) UpperTriangle() MatrixUpperTriangle 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 |
func (matrix Matrix) LowerTriangle() MatrixLowerTriangle 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 |