NewMatrix creates a new Matrix based on the provided arguments.
(rows, columns int, initial T)
| 15 | |
| 16 | // NewMatrix creates a new Matrix based on the provided arguments. |
| 17 | func New[T constraints.Integer](rows, columns int, initial T) Matrix[T] { |
| 18 | if rows < 0 || columns < 0 { |
| 19 | return Matrix[T]{} // Invalid dimensions, return an empty matrix |
| 20 | } |
| 21 | |
| 22 | // Initialize the matrix with the specified dimensions and fill it with the initial value. |
| 23 | elements := make([][]T, rows) |
| 24 | var wg sync.WaitGroup |
| 25 | wg.Add(rows) |
| 26 | |
| 27 | for i := range elements { |
| 28 | go func(i int) { |
| 29 | defer wg.Done() |
| 30 | elements[i] = make([]T, columns) |
| 31 | for j := range elements[i] { |
| 32 | elements[i][j] = initial |
| 33 | } |
| 34 | }(i) |
| 35 | } |
| 36 | |
| 37 | wg.Wait() |
| 38 | |
| 39 | return Matrix[T]{elements, rows, columns} |
| 40 | } |
| 41 | |
| 42 | // NewFromElements creates a new Matrix from the given elements. |
| 43 | func NewFromElements[T constraints.Integer](elements [][]T) (Matrix[T], error) { |