Add matrix.CreateFromSlice function

This commit is contained in:
2025-05-21 23:04:00 +02:00
parent 5f450e206f
commit 9729fe6dcb
2 changed files with 62 additions and 0 deletions

View File

@ -54,6 +54,33 @@ func Create[T Number](rows, cols int) *Matrix[T] {
}
}
// Creates a new matrix from a given 2D slice of values. The first index of the
// slice denotes the rows and the second index denotes the columns. Returns the
// newly created matrix.
//
// Panics with ErrIncompatibleDataDimensions if any of the lengths are 0 or if
// not all rows have the same length.
func CreateFromSlice[T Number](values [][]T) *Matrix[T] {
rows := len(values)
if rows == 0 {
panic(ErrInvalidDimensions)
}
cols := len(values[0])
if cols == 0 {
panic(ErrInvalidDimensions)
}
m := Create[T](rows, cols)
for i := range rows {
if len(values[i]) != cols {
panic(ErrIncompatibleDataDimensions)
}
copy(m.values[i], values[i])
}
return m
}
// Creates a new matrix of a given size and sets the values from the given
// slice. The values are used to fill the matrix left to right and top to
// bottom. Returns the newly created matrix.

View File

@ -55,6 +55,41 @@ func TestCreate(t *testing.T) {
})
}
func TestCreateFromSlice(t *testing.T) {
m := matrix.CreateFromSlice([][]int{
{1, 2, 3},
{4, 5, 6},
})
assert.NotNil(t, m)
assert.Equal(t, 2, m.Rows())
assert.Equal(t, 3, m.Cols())
assert.Equal(t, 1, m.Get(0, 0))
assert.Equal(t, 2, m.Get(0, 1))
assert.Equal(t, 3, m.Get(0, 2))
assert.Equal(t, 4, m.Get(1, 0))
assert.Equal(t, 5, m.Get(1, 1))
assert.Equal(t, 6, m.Get(1, 2))
assert.PanicsWithValue(t, matrix.ErrInvalidDimensions, func() {
matrix.CreateFromSlice([][]int{})
})
assert.PanicsWithValue(t, matrix.ErrInvalidDimensions, func() {
matrix.CreateFromSlice([][]int{
{},
})
})
assert.PanicsWithValue(t, matrix.ErrIncompatibleDataDimensions, func() {
matrix.CreateFromSlice([][]int{
{1, 2, 3},
{4, 5},
})
})
}
func TestCreateFromFlatSlice(t *testing.T) {
m := matrix.CreateFromFlatSlice(2, 3, []int{1, 2, 3, 4, 5, 6})
assert.NotNil(t, m)