You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
1.5 KiB
Go
67 lines
1.5 KiB
Go
package maths
|
|
|
|
type Matrix[T comparable] [][]T
|
|
|
|
// Transpose returns the transpose of the matrix.
|
|
func (m Matrix[T]) Transpose() Matrix[T] {
|
|
xl := len(m[0])
|
|
yl := len(m)
|
|
result := make([][]T, xl)
|
|
for i := range result {
|
|
result[i] = make([]T, yl)
|
|
}
|
|
for i := 0; i < xl; i++ {
|
|
for j := 0; j < yl; j++ {
|
|
result[i][j] = m[j][i]
|
|
}
|
|
}
|
|
return result
|
|
}
|
|
|
|
// RotateClockwise rotates the matrix clockwise by 90 degrees.
|
|
// It returns a new matrix with the rotated values.
|
|
func (m Matrix[T]) RotateClockwise() Matrix[T] {
|
|
xl := len(m[0])
|
|
yl := len(m)
|
|
result := make([][]T, xl)
|
|
for i := range result {
|
|
result[i] = make([]T, yl)
|
|
}
|
|
|
|
for row := 0; row < yl; row++ {
|
|
for col := 0; col < xl; col++ {
|
|
result[col][yl-1-row] = m[row][col]
|
|
}
|
|
}
|
|
return result
|
|
}
|
|
|
|
// Duplicate creates a duplicate of the matrix.
|
|
// It returns a new matrix with the same values as the original matrix.
|
|
func (m Matrix[T]) Duplicate() Matrix[T] {
|
|
duplicate := make([][]T, len(m))
|
|
for i := range m {
|
|
duplicate[i] = make([]T, len(m[i]))
|
|
copy(duplicate[i], m[i])
|
|
}
|
|
return duplicate
|
|
}
|
|
|
|
// Equal checks if the current matrix is equal to the given matrix.
|
|
// It returns true if the matrices are equal, and false otherwise.
|
|
func (matrix1 Matrix[T]) Equal(matrix2 Matrix[T]) bool {
|
|
if len(matrix1) != len(matrix2) || len(matrix1[0]) != len(matrix2[0]) {
|
|
return false
|
|
}
|
|
|
|
for i := 0; i < len(matrix1); i++ {
|
|
for j := 0; j < len(matrix1[0]); j++ {
|
|
if matrix1[i][j] != matrix2[i][j] {
|
|
return false
|
|
}
|
|
}
|
|
}
|
|
|
|
return true
|
|
}
|