Add Fill method to the Matrix implementation

This commit is contained in:
2025-05-19 14:52:18 +02:00
parent cf17a6fb72
commit 35e848ec43
2 changed files with 28 additions and 0 deletions

View File

@ -283,3 +283,13 @@ func (m *Matrix[T]) HadamardMultiply(matrices ...*Matrix[T]) *Matrix[T] {
m.values = values
return m
}
// Fill sets all components of this matrix to the given value. Returns the receiver.
func (m *Matrix[T]) Fill(value T) *Matrix[T] {
for i := range m.rows {
for j := range m.cols {
m.values[i][j] = value
}
}
return m
}

View File

@ -475,3 +475,21 @@ func TestHadamardProduct(t *testing.T) {
matrix.HadamardProduct(a, d)
})
}
func TestMatrix_Fill(t *testing.T) {
a := matrix.Create[int](2, 3)
assert.Equal(t, 0, a.Get(0, 0))
assert.Equal(t, 0, a.Get(0, 1))
assert.Equal(t, 0, a.Get(0, 2))
assert.Equal(t, 0, a.Get(1, 0))
assert.Equal(t, 0, a.Get(1, 1))
assert.Equal(t, 0, a.Get(1, 2))
a.Fill(3)
assert.Equal(t, 3, a.Get(0, 0))
assert.Equal(t, 3, a.Get(0, 1))
assert.Equal(t, 3, a.Get(0, 2))
assert.Equal(t, 3, a.Get(1, 0))
assert.Equal(t, 3, a.Get(1, 1))
assert.Equal(t, 3, a.Get(1, 2))
}