Add Fill method to the Matrix implementation
This commit is contained in:
@ -283,3 +283,13 @@ func (m *Matrix[T]) HadamardMultiply(matrices ...*Matrix[T]) *Matrix[T] {
|
|||||||
m.values = values
|
m.values = values
|
||||||
return m
|
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
|
||||||
|
}
|
||||||
|
@ -475,3 +475,21 @@ func TestHadamardProduct(t *testing.T) {
|
|||||||
matrix.HadamardProduct(a, d)
|
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))
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user