Skip to content

Commit 6b32f8e

Browse files
committed
Make BandMatrixRow callable
1 parent ca08c77 commit 6b32f8e

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

src/MatrixFields/band_matrix_row.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,11 @@ inv(::BandMatrixRow{ld, bw}) where {ld, bw} = error(
157157
"The inverse of a matrix with $bw diagonals is generally a dense matrix, \
158158
so it cannot be represented using BandMatrixRows",
159159
)
160+
161+
@generated function (row::BandMatrixRow{ld, bw, T})(args::Vararg{Any, N}) where {ld, bw, T, N}
162+
N == bw || error(
163+
"BandMatrixRow with bandwidth $bw expected $bw arguments, but got $N"
164+
)
165+
terms = [:(row.entries[$i] * args[$i]) for i in 1:bw]
166+
return Expr(:call, :+, terms...)
167+
end

test/MatrixFields/band_matrix_row.jl

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

0 commit comments

Comments
 (0)