Skip to content

Commit b3de256

Browse files
authored
Improve type-inference in TimesOperator * Fun (#481)
* improve type-inference in TimesOpertor * Fun * Add test
1 parent 69dd4a2 commit b3de256

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/Operators/general/algebra.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -700,8 +700,10 @@ for mulcoeff in [:mul_coefficients, :mul_coefficients!]
700700

701701
function $mulcoeff(A::TimesOperator, b, args...)
702702
ret = b
703+
# we call mulcoeff_maybeconvert to improve type-inference
704+
# in case b is a Vector
703705
for k = length(A.ops):-1:1
704-
ret = $mulcoeff(A.ops[k], ret, args...)
706+
ret = mulcoeff_maybeconvert($mulcoeff, A.ops[k], ret, args...)
705707
end
706708

707709
ret
@@ -719,7 +721,7 @@ mulcoeff_maybeconvert(args...) = _mulcoeff_maybeconvert(args...)
719721
function mulcoeff_maybeconvert(f::typeof(mul_coefficients), A, b::Vector)
720722
v = _mulcoeff_maybeconvert(f, A, b)
721723
T = promote_type(eltype(A), eltype(b))
722-
convert(Vector{T}, v)
724+
strictconvert(Vector{T}, v)
723725
end
724726

725727
function *(A::Operator, b)

test/runtests.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,13 @@ end
475475
S = ApproxFunBase.SpaceOperator(M, sp, sp)
476476
@test size(S) == (3,3)
477477
end
478+
@testset "mul_coefficients" begin
479+
C = Conversion(PointSpace(1:4), PointSpace(1:4))
480+
M = Multiplication(Fun(PointSpace(1:4)), PointSpace(1:4))
481+
T = C * M * C
482+
v = @inferred mul_coefficients(T, Float64[1:4;])
483+
@test v == Float64[1:4;].^2
484+
end
478485
end
479486

480487
@testset "RowVector" begin

0 commit comments

Comments
 (0)