MCPcopy Index your code
hub / github.com/TheAlgorithms/Go / NewFromElements

Function NewFromElements

math/matrix/matrix.go:43–64  ·  view source on GitHub ↗

NewFromElements creates a new Matrix from the given elements.

(elements [][]T)

Source from the content-addressed store, hash-verified

41
42// NewFromElements creates a new Matrix from the given elements.
43func 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
66func (m Matrix[T]) Get(row, col int) (T, error) {
67 if row < 0 || row >= m.rows || col < 0 || col >= m.columns {

Callers 8

MakeRandomMatrixFunction · 0.92
TestMatrixStringFunction · 0.92
TestMultiplyMatrixFunction · 0.92
TestNewFromElementsFunction · 0.92
BenchmarkNewFromElementsFunction · 0.92
TestMatrixDeterminantFunction · 0.92

Calls 1

IsValidFunction · 0.85

Tested by 8

MakeRandomMatrixFunction · 0.74
TestMatrixStringFunction · 0.74
TestMultiplyMatrixFunction · 0.74
TestNewFromElementsFunction · 0.74
BenchmarkNewFromElementsFunction · 0.74
TestMatrixDeterminantFunction · 0.74