NewFromElements creates a new Matrix from the given elements.
(elements [][]T)
| 41 | |
| 42 | // NewFromElements creates a new Matrix from the given elements. |
| 43 | func NewFromElements[T constraints.Integer](elements [][]T) (Matrix[T], error) { |
| 44 | if !IsValid(elements) { |
| 45 | return Matrix[T]{}, errors.New("rows have different numbers of columns") |
| 46 | } |
| 47 | rows := len(elements) |
| 48 | if rows == 0 { |
| 49 | return Matrix[T]{}, nil // Empty matrix |
| 50 | } |
| 51 | |
| 52 | columns := len(elements[0]) |
| 53 | matrix := Matrix[T]{ |
| 54 | elements: make([][]T, rows), |
| 55 | rows: rows, // Set the rows field |
| 56 | columns: columns, // Set the columns field |
| 57 | } |
| 58 | for i := range matrix.elements { |
| 59 | matrix.elements[i] = make([]T, columns) |
| 60 | copy(matrix.elements[i], elements[i]) |
| 61 | } |
| 62 | |
| 63 | return matrix, nil |
| 64 | } |
| 65 | |
| 66 | func (m Matrix[T]) Get(row, col int) (T, error) { |
| 67 | if row < 0 || row >= m.rows || col < 0 || col >= m.columns { |