| 7 | ) |
| 8 | |
| 9 | func TestMatrixSubMatrix(t *testing.T) { |
| 10 | // Create a sample matrix |
| 11 | data := [][]int{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} |
| 12 | matrix, err := matrix.NewFromElements(data) |
| 13 | if err != nil { |
| 14 | t.Fatalf("Failed to copy matrix: %v", err) |
| 15 | } |
| 16 | // Extract a submatrix |
| 17 | subMatrix, err := matrix.SubMatrix(1, 1, 2, 2) |
| 18 | if err != nil { |
| 19 | t.Errorf("Error extracting submatrix: %v", err) |
| 20 | } |
| 21 | |
| 22 | // Check the dimensions of the submatrix |
| 23 | expectedRows := 2 |
| 24 | expectedColumns := 2 |
| 25 | rows := subMatrix.Rows() |
| 26 | columns := subMatrix.Columns() |
| 27 | |
| 28 | if rows != expectedRows { |
| 29 | t.Errorf("Expected %d rows in submatrix, but got %d", expectedRows, rows) |
| 30 | } |
| 31 | |
| 32 | if columns != expectedColumns { |
| 33 | t.Errorf("Expected %d columns in submatrix, but got %d", expectedColumns, columns) |
| 34 | } |
| 35 | |
| 36 | // Check the values in the submatrix |
| 37 | expectedData := [][]int{{5, 6}, {8, 9}} |
| 38 | for i := 0; i < expectedRows; i++ { |
| 39 | for j := 0; j < expectedColumns; j++ { |
| 40 | val, err := subMatrix.Get(i, j) |
| 41 | if err != nil { |
| 42 | t.Fatalf("Failed to copy matrix: %v", err) |
| 43 | } |
| 44 | if val != expectedData[i][j] { |
| 45 | t.Errorf("Expected value %d at (%d, %d) in submatrix, but got %d", expectedData[i][j], i, j, val) |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | func TestMatrixInvalidSubMatrix(t *testing.T) { |
| 52 | // Create a sample matrix |