-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
34 lines (26 loc) · 705 Bytes
/
main.go
File metadata and controls
34 lines (26 loc) · 705 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package matrixcalc
import (
"errors"
)
func MultiplyMatrices(matrixA, matrixB [][]int) ([][]int, error) {
rowsAColumnBA, colsA := len(matrixA), len(matrixA[0])
rowsAColumnBB, colsB := len(matrixB), len(matrixB[0])
// Check if matrices can be multiplied
if colsA != rowsAColumnBB {
return nil, errors.New("matrices cannot be multiplied")
}
// Create the result matrix
result := make([][]int, rowsAColumnBA)
for i := range result {
result[i] = make([]int, colsB)
}
// Perform matrix multiplication
for i := 0; i < rowsAColumnBA; i++ {
for j := 0; j < colsB; j++ {
for k := 0; k < colsA; k++ {
result[i][j] += matrixA[i][k] * matrixB[k][j]
}
}
}
return result, nil
}