MCPcopy
hub / github.com/TheAlgorithms/Go / New

Function New

math/matrix/matrix.go:17–40  ·  view source on GitHub ↗

NewMatrix creates a new Matrix based on the provided arguments.

(rows, columns int, initial T)

Source from the content-addressed store, hash-verified

15
16// NewMatrix creates a new Matrix based on the provided arguments.
17func 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.
43func NewFromElements[T constraints.Integer](elements [][]T) (Matrix[T], error) {

Callers 15

TestAddFunction · 0.92
BenchmarkAddSmallMatrixFunction · 0.92
TestNullMatrixStringFunction · 0.92
BenchmarkStringFunction · 0.92
BenchmarkMatchDimensionsFunction · 0.92
BenchmarkSubMatrixFunction · 0.92
TestMultiplyMatrixFunction · 0.92
BenchmarkMatrixMultiplyFunction · 0.92
TestCheckEqualFunction · 0.92

Calls 1

AddMethod · 0.65

Tested by 15

TestAddFunction · 0.74
BenchmarkAddSmallMatrixFunction · 0.74
TestNullMatrixStringFunction · 0.74
BenchmarkStringFunction · 0.74
BenchmarkMatchDimensionsFunction · 0.74
BenchmarkSubMatrixFunction · 0.74
TestMultiplyMatrixFunction · 0.74
BenchmarkMatrixMultiplyFunction · 0.74
TestCheckEqualFunction · 0.74