MCPcopy Index your code
hub / github.com/TheAlgorithms/Go / TestStrassenMatrixMultiply

Function TestStrassenMatrixMultiply

math/matrix/strassenmatrixmultiply_test.go:12–67  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

10)
11
12func TestStrassenMatrixMultiply(t *testing.T) {
13 // Create two sample matrices
14 dataA := [][]int{{1, 2}, {4, 5}}
15 dataB := [][]int{{9, 8}, {6, 5}}
16 matrixA, err := matrix.NewFromElements(dataA)
17 if err != nil {
18 t.Error("copyMatrix.Set error: " + err.Error())
19 }
20 matrixB, err := matrix.NewFromElements(dataB)
21 if err != nil {
22 t.Error("copyMatrix.Set error: " + err.Error())
23 }
24
25 // Perform matrix multiplication using Strassen's algorithm
26 resultMatrix, err := matrixA.StrassenMatrixMultiply(matrixB)
27 if err != nil {
28 t.Error("copyMatrix.Set error: " + err.Error())
29 }
30
31 // Expected result
32 expectedData, err := matrixA.Multiply(matrixB)
33 if err != nil {
34 t.Error("copyMatrix.Set error: " + err.Error())
35 }
36
37 // Check the dimensions of the result matrix
38 expectedRows := expectedData.Rows()
39 expectedColumns := expectedData.Columns()
40 rows := resultMatrix.Rows()
41 columns := resultMatrix.Columns()
42
43 if rows != expectedRows {
44 t.Errorf("Expected %d rows in result matrix, but got %d", expectedRows, rows)
45 }
46
47 if columns != expectedColumns {
48 t.Errorf("Expected %d columns in result matrix, but got %d", expectedColumns, columns)
49 }
50
51 // Check the values in the result matrix
52 for i := 0; i < expectedRows; i++ {
53 for j := 0; j < expectedColumns; j++ {
54 val, err := resultMatrix.Get(i, j)
55 if err != nil {
56 t.Fatalf("Failed to copy matrix: %v", err)
57 }
58 expVal, err := expectedData.Get(i, j)
59 if err != nil {
60 t.Fatalf("Failed to copy matrix: %v", err)
61 }
62 if val != expVal {
63 t.Errorf("Expected value %d at (%d, %d) in result matrix, but got %d", expVal, i, j, val)
64 }
65 }
66 }
67}
68func TestMatrixMultiplication(t *testing.T) {
69 rand.New(rand.NewSource(time.Now().UnixNano()))

Callers

nothing calls this directly

Calls 6

NewFromElementsFunction · 0.92
MultiplyMethod · 0.80
RowsMethod · 0.80
ColumnsMethod · 0.80
GetMethod · 0.65

Tested by

no test coverage detected