Skip to content

Commit 2544fcd

Browse files
fixup tests, allow X'W-W*X by better colsupport
add some moment-based tests
1 parent 57e5602 commit 2544fcd

File tree

3 files changed

+55
-43
lines changed

3 files changed

+55
-43
lines changed

src/FastTransforms.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ import AbstractFFTs: Plan, ScaledPlan,
1919
fftshift, ifftshift, rfft_output_size, brfft_output_size,
2020
normalization
2121

22-
import ArrayLayouts: colsupport, LayoutMatrix, MemoryLayout, AbstractBandedLayout
22+
import ArrayLayouts: rowsupport, colsupport, LayoutMatrix, MemoryLayout, AbstractBandedLayout
2323

24-
import BandedMatrices: bandwidths
24+
import BandedMatrices: bandwidths, BandedLayout
2525

2626
import FFTW: dct, dct!, idct, idct!, plan_dct!, plan_idct!,
2727
plan_dct, plan_idct, fftwNumber

src/GramMatrix.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ abstract type AbstractGramMatrix{T} <: LayoutMatrix{T} end
22

33
@inline issymmetric(G::AbstractGramMatrix) = true
44
@inline isposdef(G::AbstractGramMatrix) = true
5-
@inline colsupport(G::AbstractGramMatrix, j) = colrange(G, j)
65

76
struct GramMatrix{T, WT <: AbstractMatrix{T}, XT <: AbstractMatrix{T}} <: AbstractGramMatrix{T}
87
W::WT
@@ -53,6 +52,8 @@ GramMatrix(W::WT, X::XT) where {T, WT <: AbstractMatrix{T}, XT <: AbstractMatrix
5352
@inline getindex(G::GramMatrix, i::Integer, j::Integer) = G.W[i, j]
5453
@inline bandwidths(G::GramMatrix) = bandwidths(G.W)
5554
@inline MemoryLayout(G::GramMatrix) = MemoryLayout(G.W)
55+
@inline rowsupport(G::GramMatrix, j) = rowsupport(MemoryLayout(G), G.W, j)
56+
@inline colsupport(G::GramMatrix, j) = colsupport(MemoryLayout(G), G.W, j)
5657

5758
"""
5859
GramMatrix(μ::AbstractVector, X::AbstractMatrix)
@@ -282,6 +283,7 @@ end
282283
@inline size(G::ChebyshevGramMatrix) = (G.n, G.n)
283284
@inline getindex(G::ChebyshevGramMatrix, i::Integer, j::Integer) = (G.μ[abs(i-j)+1] + G.μ[i+j-1])/2
284285
@inline bandwidths(G::ChebyshevGramMatrix{T, <: PaddedVector{T}}) where T = (length(G.μ.args[2])-1, length(G.μ.args[2])-1)
286+
@inline MemoryLayout(G::ChebyshevGramMatrix{T, <: PaddedVector{T}}) where T = BandedLayout()
285287

286288
#
287289
# 2X'W-W*2X = G*J*G'

test/grammatrixtests.jl

Lines changed: 50 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,43 @@ using FastTransforms, BandedMatrices, LazyArrays, LinearAlgebra, Test
55
for T in (Float32, Float64, BigFloat)
66
R = plan_leg2cheb(T, n; normcheb=true)*I
77
X = Tridiagonal([T(n)/(2n-1) for n in 1:n-1], zeros(T, n), [T(n)/(2n+1) for n in 1:n-1]) # Legendre X
8-
W = Symmetric(R'R)
9-
G = GramMatrix(W, X)
10-
F = cholesky(G)
11-
@test F.L*F.L' W
8+
W = GramMatrix(Symmetric(R'R), X)
9+
F = cholesky(W)
10+
@test F.L*F.L' Symmetric(R'R)
1211
@test F.U R
1312

1413
R = plan_leg2cheb(T, n; normcheb=true, normleg=true)*I
1514
X = SymTridiagonal(zeros(T, n), [sqrt(T(n)^2/(4*n^2-1)) for n in 1:n-1]) # normalized Legendre X
16-
W = Symmetric(R'R)
17-
G = GramMatrix(W, X)
18-
F = cholesky(G)
19-
@test F.L*F.L' W
15+
W = GramMatrix(Symmetric(R'R), X)
16+
F = cholesky(W)
17+
@test F.L*F.L' Symmetric(R'R)
2018
@test F.U R
2119

2220
b = 4
2321
X = BandedMatrix(SymTridiagonal(zeros(T, n+b), [sqrt(T(n)^2/(4*n^2-1)) for n in 1:n+b-1])) # normalized Legendre X
24-
W = I+X^2+X^4
25-
W = Symmetric(W[1:n, 1:n])
26-
X = BandedMatrix(SymTridiagonal(zeros(T, n), [sqrt(T(n)^2/(4*n^2-1)) for n in 1:n-1])) # normalized Legendre X
27-
G = GramMatrix(W, X)
28-
@test bandwidths(G) == (b, b)
29-
F = cholesky(G)
30-
@test F.L*F.L' W
22+
M = Symmetric((I+X^2+X^4)[1:n, 1:n])
23+
W = GramMatrix(M, X[1:n, 1:n])
24+
@test bandwidths(W) == (b, b)
25+
F = cholesky(W)
26+
@test F.L*F.L' M
3127

3228
X = BandedMatrix(SymTridiagonal(T[2n-1 for n in 1:n+b], T[-n for n in 1:n+b-1])) # Laguerre X, tests nonzero diagonal
33-
W = I+X^2+X^4
34-
W = Symmetric(W[1:n, 1:n])
35-
X = BandedMatrix(SymTridiagonal(T[2n-1 for n in 1:n], T[-n for n in 1:n-1])) # Laguerre X
36-
G = GramMatrix(W, X)
37-
@test bandwidths(G) == (b, b)
38-
F = cholesky(G)
39-
@test F.L*F.L' W
29+
M = Symmetric((I+X^2+X^4)[1:n, 1:n])
30+
W = GramMatrix(M, X[1:n, 1:n])
31+
@test bandwidths(W) == (b, b)
32+
F = cholesky(W)
33+
@test F.L*F.L' M
34+
35+
for μ in (PaddedVector([T(4)/3;0;-T(4)/15], 2n-1), # w(x) = 1-x^2
36+
PaddedVector([T(26)/15;0;-T(4)/105;0;T(16)/315], 2n-1), # w(x) = 1-x^2+x^4
37+
T(1) ./ (1:2n-1)) # Related to a log weight
38+
X = Tridiagonal([T(n)/(2n-1) for n in 1:2n-2], zeros(T, 2n-1), [T(n)/(2n+1) for n in 1:2n-2]) # Legendre X
39+
W = GramMatrix(μ, X)
40+
X = Tridiagonal(X[1:n, 1:n])
41+
G = FastTransforms.compute_skew_generators(W)
42+
J = T[0 1; -1 0]
43+
@test X'W-W*X G*J*G'
44+
end
4045
end
4146
W = reshape([i for i in 1.0:n^2], n, n)
4247
X = reshape([i for i in 1.0:4n^2], 2n, 2n)
@@ -50,38 +55,43 @@ end
5055
n = 128
5156
for T in (Float32, Float64, BigFloat)
5257
μ = FastTransforms.chebyshevmoments1(T, 2n-1)
53-
G = ChebyshevGramMatrix(μ)
54-
F = cholesky(G)
55-
@test F.L*F.L' G
58+
W = ChebyshevGramMatrix(μ)
59+
F = cholesky(W)
60+
@test F.L*F.L' W
5661
R = plan_cheb2leg(T, n; normleg=true)*I
5762
@test F.U R
5863

5964
α, β = (T(0.123), T(0.456))
6065
μ = FastTransforms.chebyshevjacobimoments1(T, 2n-1, α, β)
61-
G = ChebyshevGramMatrix(μ)
62-
F = cholesky(G)
63-
@test F.L*F.L' G
66+
W = ChebyshevGramMatrix(μ)
67+
F = cholesky(W)
68+
@test F.L*F.L' W
6469
R = plan_cheb2jac(T, n, α, β; normjac=true)*I
6570
@test F.U R
6671

6772
μ = FastTransforms.chebyshevlogmoments1(T, 2n-1)
68-
G = ChebyshevGramMatrix(μ)
69-
F = cholesky(G)
70-
@test F.L*F.L' G
73+
W = ChebyshevGramMatrix(μ)
74+
F = cholesky(W)
75+
@test F.L*F.L' W
7176

7277
μ = FastTransforms.chebyshevabsmoments1(T, 2n-1)
73-
G = ChebyshevGramMatrix(μ)
74-
F = cholesky(G)
75-
@test F.L*F.L' G
78+
W = ChebyshevGramMatrix(μ)
79+
F = cholesky(W)
80+
@test F.L*F.L' W
7681

7782
μ = PaddedVector(T(1) ./ [1,2,3,4,5], 2n-1)
78-
G = ChebyshevGramMatrix(μ)
79-
@test bandwidths(G) == (4, 4)
80-
F = cholesky(G)
81-
@test F.L*F.L' G
83+
W = ChebyshevGramMatrix(μ)
84+
@test bandwidths(W) == (4, 4)
85+
F = cholesky(W)
86+
@test F.L*F.L' W
8287
μd = Vector{T}(μ)
83-
Gd = ChebyshevGramMatrix(μd)
84-
Fd = cholesky(Gd)
88+
Wd = ChebyshevGramMatrix(μd)
89+
Fd = cholesky(Wd)
8590
@test F.L Fd.L
91+
92+
X = Tridiagonal([T(1); ones(T, n-2)/2], zeros(T, n), ones(T, n-1)/2)
93+
G = FastTransforms.compute_skew_generators(W)
94+
J = T[0 1; -1 0]
95+
@test 2*(X'W-W*X) G*J*G'
8696
end
8797
end

0 commit comments

Comments
 (0)