Skip to content

Commit

Permalink
make linear_combination work with SVector of coefficients
Browse files Browse the repository at this point in the history
  • Loading branch information
tpapp committed May 23, 2024
1 parent a88c2a6 commit 7d25c48
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/derivatives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ export 𝑑, ∂

_one(::Type{T}) where {T<:Real} = one(T)

_add(x::Real, y::Real) = x + y
_add(x::T, y::T) where {T <: Union{Real,SVector}} = x + y

_sub(x::Real, y::Real) = x - y

_mul(x::Real, y::Real) = x * y
_mul(x::Union{Real,SVector}, y::Real) = x * y

_mul(x, y, z) = _mul(_mul(x, y), z)

Expand Down
4 changes: 4 additions & 0 deletions src/generic_api.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ $(SIGNATURES)
Helper function for linear combinations of basis elements at `x`. Always checks that `θ`
and `basis` have compatible dimensions.
!!! NOTE
`x` and `θ` can be anything that supports `_mul(θ[i], b[i])` and `_add` on the
result of this.
"""
@inline function _linear_combination(basis, θ, x)
# an implementation of mapreduce, to work around
Expand Down
9 changes: 9 additions & 0 deletions test/test_generic_api.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ end
@test transform_from(basis, t, x) == transform_from(domain(basis), t, x)
end

@testset "linear combination SVector passthrough" begin
N = 10
M = 3
basis = Chebyshev(InteriorGrid(), N) SemiInfRational(0.0, 1.0)
θ = rand(SVector{M,Float64}, N)
x = 2.0
@test linear_combination(basis, θ, x) SVector{M}(linear_combination(basis, map(x -> x[i], θ), x) for i in 1:M)
end

@testset "subset fallback" begin
@test !is_subset_basis(Chebyshev(InteriorGrid(), 4), # just test the fallback method
smolyak_basis(Chebyshev, InteriorGrid(), SmolyakParameters(2, 2), 2))
Expand Down

0 comments on commit 7d25c48

Please sign in to comment.