Skip to content

Commit 42d1cd0

Browse files
authored
Add WeightedFactorization (#81)
* Add WeightedFactorization * Update runtests.jl * to_indices for BlockIndex arrays * increase coverage * Update runtests.jl
1 parent 6580943 commit 42d1cd0

File tree

5 files changed

+33
-6
lines changed

5 files changed

+33
-6
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ContinuumArrays"
22
uuid = "7ae1f121-cc2c-504b-ac30-9b923412ae5c"
3-
version = "0.6.1"
3+
version = "0.6.2"
44

55
[deps]
66
ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"

src/ContinuumArrays.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ end
7474
@inline to_indices(A::AbstractQuasiArray, inds, I::Tuple{BlockIndex{1}, Vararg{Any}}) =
7575
(inds[1][I[1]], to_indices(A, _maybetail(inds), tail(I))...)
7676

77+
@inline to_indices(A::AbstractQuasiArray, inds, I::Tuple{AbstractArray{<:BlockIndex{1}}, Vararg{Any}}) =
78+
(inds[1][I[1]], to_indices(A, _maybetail(inds), tail(I))...)
79+
7780
checkpoints(d::AbstractInterval{T}) where T = width(d) .* SVector{3,float(T)}(0.823972,0.01,0.3273484) .+ leftendpoint(d)
7881
checkpoints(x::Inclusion) = checkpoints(x.domain)
7982
checkpoints(A::AbstractQuasiMatrix) = checkpoints(axes(A,1))

src/bases/bases.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ sublayout(::AbstractBasisLayout, ::Type{<:Tuple{Map,AbstractUnitRange}}) = Mappe
4040
## Weighted basis interface
4141
unweightedbasis(P::BroadcastQuasiMatrix{<:Any,typeof(*),<:Tuple{AbstractQuasiVector,AbstractQuasiMatrix}}) = last(P.args)
4242
unweightedbasis(V::SubQuasiArray) = view(unweightedbasis(parent(V)), parentindices(V)...)
43+
weight(P::BroadcastQuasiMatrix{<:Any,typeof(*),<:Tuple{AbstractQuasiVector,AbstractQuasiMatrix}}) = first(P.args)
44+
weight(V::SubQuasiArray) = weight(parent(V))[parentindices(V)[1]]
4345

4446

4547

@@ -181,6 +183,15 @@ copy(L::Ldiv{<:AbstractBasisLayout,<:Any,<:Any,<:AbstractQuasiVector}) =
181183
copy(L::Ldiv{<:AbstractBasisLayout,ApplyLayout{typeof(*)},<:Any,<:AbstractQuasiVector}) =
182184
transform_ldiv(L.A, L.B)
183185

186+
struct WeightedFactorization{T, WW, FAC<:Factorization{T}} <: Factorization{T}
187+
w::WW
188+
F::FAC
189+
end
190+
191+
_factorize(::WeightedBasisLayouts, wS) = WeightedFactorization(weight(wS), factorize(unweightedbasis(wS)))
192+
193+
194+
\(F::WeightedFactorization, b::AbstractQuasiVector) = F.F \ (b ./ F.w)
184195

185196
##
186197
# Algebra

test/runtests.jl

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using ContinuumArrays, QuasiArrays, LazyArrays, IntervalSets, FillArrays, LinearAlgebra, BandedMatrices, FastTransforms, InfiniteArrays, Test, Base64
22
import ContinuumArrays: ℵ₁, materialize, AffineQuasiVector, BasisLayout, AdjointBasisLayout, SubBasisLayout, ℵ₁,
33
MappedBasisLayout, AdjointMappedBasisLayout, MappedWeightedBasisLayout, TransformFactorization, Weight, WeightedBasisLayout, SubWeightedBasisLayout, WeightLayout,
4-
Expansion, basis, invmap, Map
4+
Expansion, basis, invmap, Map, checkpoints
55
import QuasiArrays: SubQuasiArray, MulQuasiMatrix, Vec, Inclusion, QuasiDiagonal, LazyQuasiArrayApplyStyle, LazyQuasiArrayStyle
66
import LazyArrays: MemoryLayout, ApplyStyle, Applied, colsupport, arguments, ApplyLayout, LdivStyle, MulStyle
77

@@ -425,6 +425,7 @@ Base.axes(T::ChebyshevWeight) = (Inclusion(-1..1),)
425425

426426
Base.getindex(::Chebyshev, x::Float64, n::Int) = cos((n-1)*acos(x))
427427
Base.getindex(::ChebyshevWeight, x::Float64) = 1/sqrt(1-x^2)
428+
Base.getindex(w::ChebyshevWeight, ::Inclusion) = w # TODO: make automatic
428429

429430
LinearAlgebra.factorize(L::Chebyshev) =
430431
TransformFactorization(grid(L), plan_chebyshevtransform(Array{Float64}(undef, size(L,2))))
@@ -451,9 +452,13 @@ ContinuumArrays.invmap(::InvQuadraticMap{T}) where T = QuadraticMap{T}()
451452
w = ChebyshevWeight()
452453
wT = w .* T
453454
x = axes(T,1)
454-
F = factorize(T)
455-
g = grid(F)
456-
@test T \ exp.(x) == F \ exp.(x) == F \ exp.(g) == chebyshevtransform(exp.(g), Val(1))
455+
456+
@testset "basics" begin
457+
F = factorize(T)
458+
g = grid(F)
459+
@test T \ exp.(x) == F \ exp.(x) == F \ exp.(g) == chebyshevtransform(exp.(g), Val(1))
460+
@test all(checkpoints(T) .∈ Ref(axes(T,1)))
461+
end
457462

458463
@testset "Weighted" begin
459464
@test MemoryLayout(w) isa WeightLayout
@@ -469,6 +474,10 @@ ContinuumArrays.invmap(::InvQuadraticMap{T}) where T = QuadraticMap{T}()
469474
@test ContinuumArrays.unweightedbasis(wT) T
470475
@test ContinuumArrays.unweightedbasis(wT2) T[:,2:4]
471476
@test ContinuumArrays.unweightedbasis(wT3) T[:,2:4]
477+
478+
@test ContinuumArrays.weight(wT) ContinuumArrays.weight(wT2) ContinuumArrays.weight(wT3) w
479+
480+
@test wT \ @.(exp(x) / sqrt(1-x^2)) T \ exp.(x)
472481
end
473482
@testset "Mapped" begin
474483
y = affine(0..1, x)
@@ -508,6 +517,7 @@ ContinuumArrays.invmap(::InvQuadraticMap{T}) where T = QuadraticMap{T}()
508517

509518
@testset "Broadcasted" begin
510519
T = Chebyshev(5)
520+
F = factorize(T)
511521
x = axes(T,1)
512522
a = 1 .+ x .+ x.^2
513523
# The following are wrong, just testing dispatch

test/test_basisconcat.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,15 @@ import ContinuumArrays: PiecewiseBasis, VcatBasis, HvcatBasis
6161
S1 = LinearSpline(0:1)
6262
S2 = LinearSpline(0:0.5:1)
6363
S = HvcatBasis(2, S1, S2, S2, S1)
64+
D = Derivative(axes(S,1))
6465

6566
@test S == S
6667

6768
@test S[0.1, 1] == [S1[0.1,1] 0; 0 0]
6869
@test S[0.1,Block(1)] == [[S1[0.1,1] 0; 0 0], [S1[0.1,2] 0; 0 0]]
70+
@test S[0.1,Block(1)[1]] == [S1[0.1,1] 0; 0 0]
71+
@test S[0.1,getindex.(Block(1),1:2)] == [[S1[0.1,1] 0; 0 0], [S1[0.1,2] 0; 0 0]]
6972
D = Derivative(axes(S,1))
70-
@test_broken (D*S)[0.1,1]
73+
@test_broken (D*S)[0.1,1] # throws error
7174
end
7275
end

0 commit comments

Comments
 (0)