Skip to content

Commit 05daa2b

Browse files
sjavisMikolaj-A-Kowalski
authored andcommitted
Make BandMatrixRow callable
1 parent c39d382 commit 05daa2b

2 files changed

Lines changed: 16 additions & 0 deletions

File tree

src/MatrixFields/band_matrix_row.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,12 @@ is_auto_broadcastable(::Type{BMR}) where {BMR <: BandMatrixRow} =
162162
is_auto_broadcastable(eltype(BMR))
163163
add_auto_broadcasters(row::BandMatrixRow) = map(add_auto_broadcasters, row)
164164
drop_auto_broadcasters(row::BandMatrixRow) = map(drop_auto_broadcasters, row)
165+
166+
167+
@generated function (row::BandMatrixRow{ld, bw, T})(args::Vararg{Any, N}) where {ld, bw, T, N}
168+
N == bw || error(
169+
"BandMatrixRow with bandwidth $bw expected $bw arguments, but got $N"
170+
)
171+
terms = [:(row.entries[$i] * args[$i]) for i in 1:bw]
172+
return Expr(:call, :+, terms...)
173+
end

test/MatrixFields/band_matrix_row.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,11 @@ include("matrix_field_test_utils.jl")
5959
@test zero(typeof(DiagonalMatrixRow(1))) == DiagonalMatrixRow(0)
6060
@test eltype(typeof(DiagonalMatrixRow(1))) == Int
6161
@test inv(DiagonalMatrixRow(1)) == DiagonalMatrixRow(float(1))
62+
63+
# Test callable behaviour
64+
@test DiagonalMatrixRow(2.0)(10.0) == 20.0
65+
@test BidiagonalMatrixRow(1.0, 2.0)(10.0, 5.0) == 20.0
66+
@test TridiagonalMatrixRow(-1.0, 2.0, -1.0)(10.0, 25.0, 12.0) == 28.0
67+
@test_throws ErrorException TridiagonalMatrixRow(1, 2, 3)(10.0, 5.0)
68+
@test_throws ErrorException TridiagonalMatrixRow(1, 2, 3)(10.0, 5.0, 1.0, 2.0)
6269
end

0 commit comments

Comments
 (0)