SubMatrix extracts a submatrix from the current matrix.
(rowStart, colStart, numRows, numCols int)
| 8 | |
| 9 | // SubMatrix extracts a submatrix from the current matrix. |
| 10 | func (m Matrix[T]) SubMatrix(rowStart, colStart, numRows, numCols int) (Matrix[T], error) { |
| 11 | if rowStart < 0 || colStart < 0 || numRows < 0 || numCols < 0 { |
| 12 | return Matrix[T]{}, errors.New("negative dimensions are not allowed") |
| 13 | } |
| 14 | |
| 15 | if rowStart+numRows > m.rows || colStart+numCols > m.columns { |
| 16 | return Matrix[T]{}, errors.New("submatrix dimensions exceed matrix bounds") |
| 17 | } |
| 18 | |
| 19 | var zeroVal T |
| 20 | if numRows == 0 || numCols == 0 { |
| 21 | return New(numRows, numCols, zeroVal), nil // Return an empty matrix |
| 22 | } |
| 23 | |
| 24 | subMatrix := New(numRows, numCols, zeroVal) |
| 25 | |
| 26 | ctx, cancel := context.WithCancel(context.Background()) |
| 27 | defer cancel() // Make sure it's called to release resources even if no errors |
| 28 | |
| 29 | var wg sync.WaitGroup |
| 30 | errCh := make(chan error, 1) |
| 31 | |
| 32 | for i := 0; i < numRows; i++ { |
| 33 | i := i // Capture the loop variable for the goroutine |
| 34 | wg.Add(1) |
| 35 | go func() { |
| 36 | defer wg.Done() |
| 37 | for j := 0; j < numCols; j++ { |
| 38 | select { |
| 39 | case <-ctx.Done(): |
| 40 | return // Context canceled; return without an error |
| 41 | default: |
| 42 | } |
| 43 | |
| 44 | val, err := m.Get(rowStart+i, colStart+j) |
| 45 | if err != nil { |
| 46 | cancel() |
| 47 | select { |
| 48 | case errCh <- err: |
| 49 | default: |
| 50 | } |
| 51 | return |
| 52 | } |
| 53 | |
| 54 | err = subMatrix.Set(i, j, val) |
| 55 | if err != nil { |
| 56 | cancel() |
| 57 | select { |
| 58 | case errCh <- err: |
| 59 | default: |
| 60 | } |
| 61 | return |
| 62 | } |
| 63 | } |
| 64 | }() |
| 65 | } |
| 66 | |
| 67 | // Wait for all goroutines to finish |