Skip to content

Commit

Permalink
Expand functions in P'(x .^2 .* P) (#191)
Browse files Browse the repository at this point in the history
* Expand functions in P'(x .^2 .* P)

* Update ci.yml

* add tests and use expand

* Delete Project.toml

* Update bases.jl

* add coverage

* test adj broadcast

* Add tests

* Update test_chebyshev.jl

* Update test_chebyshev.jl

* add one mroe test
  • Loading branch information
dlfivefifty authored Nov 12, 2024
1 parent 09105e5 commit 6e7d8fd
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ jobs:
fail-fast: false
matrix:
version:
- '1.10'
- 'lts'
- '1'
os:
- ubuntu-latest
- macOS-latest
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Infinities = "0.1"
IntervalSets = "0.7"
LazyArrays = "2"
Makie = "0.20, 0.21"
QuasiArrays = "0.11.5"
QuasiArrays = "0.11.8"
RecipesBase = "1.0"
StaticArrays = "1.0"
julia = "1.10"
Expand Down
44 changes: 42 additions & 2 deletions src/bases/bases.jl
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,54 @@ function _broadcast_mul_ldiv(::Tuple{ScalarLayout,ApplyLayout{typeof(*)}}, A, B)
a * (A \ b)
end

_broadcast_mul_ldiv(::Tuple{ScalarLayout,AbstractBasisLayout}, A, B) =
_broadcast_mul_ldiv((ScalarLayout(),UnknownLayout()), A, B)
_broadcast_mul_ldiv(::Tuple{ScalarLayout,AbstractBasisLayout}, A, B) = _broadcast_mul_ldiv((ScalarLayout(),UnknownLayout()), A, B)
_broadcast_mul_ldiv(_, A, B) = copy(Ldiv{typeof(MemoryLayout(A)),UnknownLayout}(A,B))

copy(L::Ldiv{<:AbstractBasisLayout,BroadcastLayout{typeof(*)}}) = _broadcast_mul_ldiv(map(MemoryLayout,arguments(L.B)), L.A, L.B)
copy(L::Ldiv{<:MappedBasisLayouts,BroadcastLayout{typeof(*)}}) = _broadcast_mul_ldiv(map(MemoryLayout,arguments(L.B)), L.A, L.B)



# multiplication operators, reexpand in basis A
@inline function _broadcast_mul_adj(::Tuple{Any,AbstractBasisLayout}, Ac, B)
a,b = arguments(B)
@assert a isa AbstractQuasiVector # Only works for vec .* mat
A = Ac'
ab = (A * (A \ a)) .* b # broadcasted should be overloaded
MemoryLayout(ab) isa BroadcastLayout && return Ac*transform_ldiv(A, ab)
Ac*ab
end

@inline function _broadcast_mul_adj(::Tuple{Any,ApplyLayout{typeof(*)}}, Ac, B)
a,b = arguments(B)
@assert a isa AbstractQuasiVector # Only works for vec .* mat
args = arguments(*, b)
*(Ac*(a .* first(args)), tail(args)...)
end


function _broadcast_mul_adj(::Tuple{ScalarLayout,Any}, Ac, B)
a,b = arguments(B)
a * (Ac*b)
end

function _broadcast_mul_adj(::Tuple{ScalarLayout,ApplyLayout{typeof(*)}}, Ac, B)
a,b = arguments(B)
a * (Ac*b)
end

_broadcast_mul_adj(::Tuple{ScalarLayout,AbstractBasisLayout}, A, B) = _broadcast_mul_adj((ScalarLayout(),UnknownLayout()), A, B)
_broadcast_mul_adj(_, A, B) = copy(Mul{typeof(MemoryLayout(A)),UnknownLayout}(A,B))

_broadcast_mul_adj_simplifiable(_, ::AbstractBasisLayout) = Val(true)
_broadcast_mul_adj_simplifiable(_, ::ApplyLayout{typeof(*)}) = Val(true)
_broadcast_mul_adj_simplifiable(::ScalarLayout, _) = Val(true)
_broadcast_mul_adj_simplifiable(::ScalarLayout, ::ApplyLayout{typeof(*)}) = Val(true)
_broadcast_mul_adj_simplifiable(::ScalarLayout, ::AbstractBasisLayout) = Val(true)
_broadcast_mul_adj_simplifiable(_, _) = Val(false)

simplifiable(L::Mul{<:AdjointBasisLayout,BroadcastLayout{typeof(*)}}) = _broadcast_mul_adj_simplifiable(map(MemoryLayout,arguments(L.B))...)
copy(L::Mul{<:AdjointBasisLayout,BroadcastLayout{typeof(*)}}) = _broadcast_mul_adj(map(MemoryLayout,arguments(L.B)), L.A, L.B)


"""
Expand Down Expand Up @@ -651,6 +690,7 @@ diff_layout(::ExpansionLayout, A, dims...) = diff_layout(ApplyLayout{typeof(*)}(
####

simplifiable(::Mul{<:AdjointBasisLayout, <:AbstractBasisLayout}) = Val(true)
@inline simplifiable(L::Mul{<:AdjointBasisLayout,ApplyLayout{typeof(*)}}) = simplifiable(*, L.A, first(arguments(*, L.B)))
function copy(M::Mul{<:AdjointBasisLayout, <:AbstractBasisLayout})
A = (M.A)'
A == M.B && return grammatrix(A)
Expand Down
22 changes: 21 additions & 1 deletion test/test_chebyshev.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ContinuumArrays, LinearAlgebra, FastTransforms, QuasiArrays, ArrayLayouts, Base64, LazyArrays, Test
using ContinuumArrays, LinearAlgebra, QuasiArrays, ArrayLayouts, Base64, LazyArrays, Test
using FastTransforms
import ContinuumArrays: Basis, Weight, Map, LazyQuasiArrayStyle, TransformFactorization,
ExpansionLayout, checkpoints, MappedBasisLayout, MappedWeightedBasisLayout,
SubWeightedBasisLayout, WeightedBasisLayout, WeightLayout, basis, grammatrix
Expand Down Expand Up @@ -159,6 +160,25 @@ Base.:(==)(::FooBasis, ::FooBasis) = true

= T * (T \ a)
@test T \ (ã .* ã) [1.5,1,0.5,0,0]

@test T'*(a .* T) isa Matrix
@test T'*(a .* (T * (T \ a))) isa Vector
@test_broken T'f isa Vector
@test T'isa Vector
@test T'*(ã .* ã) isa Vector
@test (2T)'*(a .* T) isa Matrix
@test T'*(2T) isa Matrix
@test T'*(2T*randn(5)) isa Vector
@test (2T)'*(T*(1:5)) T'*(2T*(1:5)) T'BroadcastQuasiMatrix(*, 2, T*(1:5))
@test T' * (a .* (T * (1:5))) T' * ((a .* T) * (1:5))
@test T'BroadcastQuasiMatrix(*, 2, 2T) == 4*(T'T)

@test LazyArrays.simplifiable(*, T', T*(1:5)) == Val(true)
@test LazyArrays.simplifiable(*, T', (a .* (T * (1:5)))) == Val(true)
@test LazyArrays.simplifiable(*, T', a .* T) == Val(true)
@test LazyArrays.simplifiable(*, T', 2T) == Val(true)
@test LazyArrays.simplifiable(*, T', BroadcastQuasiMatrix(*, 2, T*(1:5))) == Val(true)
@test LazyArrays.simplifiable(*, T', BroadcastQuasiMatrix(*, 2, 2T)) == Val(true)
end

@testset "sum/dot/diff" begin
Expand Down
2 changes: 1 addition & 1 deletion test/test_splines.jl
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ import ContinuumArrays: basis, AdjointBasisLayout, ExpansionLayout, BasisLayout,
@test Δ == -(*(B',D',D,B))
@test Δ == -(B'D'D*B)
@test Δ == -((B'D')*(D*B))
@test_broken Δ == -B'*(D'D)*B
@test Δ == -B'*(D'D)*B
@test Δ == -(B'*(D'D)*B)

f = L*exp.(L.points) # project exp(x)
Expand Down

2 comments on commit 6e7d8fd

@dlfivefifty
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/119234

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.18.5 -m "<description of version>" 6e7d8fde8bf66885c78bb201ffce7ee21fc1c214
git push origin v0.18.5

Please sign in to comment.