Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix an inexact error #207

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 18 additions & 51 deletions src/classical/jacobi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
orthogonalityweight(P::Jacobi) = JacobiWeight(P.a, P.b)

const WeightedJacobi{T} = WeightedBasis{T,<:JacobiWeight,<:Jacobi}
WeightedJacobi(P::Jacobi{T}) where T = JacobiWeight(zero(T),zero(T)) .* P

"""
HalfWeighted{lr}(Jacobi(a,b))
Expand Down Expand Up @@ -292,80 +293,46 @@
end
end

# TODO: implement ApplyArray(*, A) in LazyArrays.jl, then these can be simplified
# the type specification is also because LazyArrays.jl is slow otherwise
# getindex and print could be very slow. This needs to be fixed by LazyArrays.jl
function _jacobi_convert_a(a, b, k, T) # Jacobi(a+k, b) \ Jacobi(a, b)
if iszero(k)
Eye{T}(∞)
elseif isone(k)
_jacobi_convert_a(a, b)
else
list = tuple([_jacobi_convert_a(a+j, b) for j in k-1:-1:0]...)
ApplyArray{T,2,typeof(*),typeof(list)}(*, list)
j = round(k)
if j ≉ k
throw(ArgumentError("non-integer conversions not supported"))
end
k = Integer(j)
reduce(*, [_jacobi_convert_a(a+j, b) for j in k-1:-1:0], init=Eye{T}(∞))
end
function _jacobi_convert_b(a, b, k, T) # Jacobi(a, b+k) \ Jacobi(a, b)
if iszero(k)
Eye{T}(∞)
elseif isone(k)
_jacobi_convert_b(a, b)
else
list = tuple([_jacobi_convert_b(a, b+j) for j in k-1:-1:0]...)
ApplyArray{T,2,typeof(*),typeof(list)}(*, list)
j = round(k)
if j ≉ k
throw(ArgumentError("non-integer conversions not supported"))

Check warning on line 307 in src/classical/jacobi.jl

View check run for this annotation

Codecov / codecov/patch

src/classical/jacobi.jl#L307

Added line #L307 was not covered by tests
end
k = Integer(j)
reduce(*, [_jacobi_convert_b(a, b+j) for j in k-1:-1:0], init=Eye{T}(∞))
end

isapproxinteger(x) = isinteger(x) || isapprox(x,round(Int,x)) || isapprox(x+1,round(Int,x+1))


function \(A::Jacobi, B::Jacobi)
T = promote_type(eltype(A), eltype(B))
aa, ab = A.a, A.b
ba, bb = B.a, B.b
if !isapproxinteger(aa-ba) || !isapproxinteger(ab-bb)
throw(ArgumentError("non-integer conversions not supported"))
end
ka = round(Integer, aa-ba)
kb = round(Integer, ab-bb)
ka = aa - ba
kb = ab - bb
if ka >= 0
C1 = _jacobi_convert_a(ba, ab, ka, T)
if kb >= 0
C2 = _jacobi_convert_b(ba, bb, kb, T)
else
C2 = inv(_jacobi_convert_b(ba, ab, -kb, T))
end
if iszero(ka)
C2
elseif iszero(kb)
C1
else
list = (C1, C2)
ApplyArray{T,2,typeof(*),typeof(list)}(*, list)
end
C1 * C2
else
inv(B \ A)
end
end

function \(A::Jacobi, w_B::WeightedJacobi)
a,b = A.a,A.b
(JacobiWeight(zero(a),zero(b)) .* A) \ w_B
end

function \(w_A::WeightedJacobi, B::Jacobi)
a,b = B.a,B.b
w_A \ (JacobiWeight(zero(a),zero(b)) .* B)
end

function \(A::AbstractJacobi, w_B::WeightedJacobi)
à = Jacobi(A)
(A \ Ã) * (Ã \ w_B)
end
function \(w_A::WeightedJacobi, B::AbstractJacobi)
B̃ = Jacobi(B)
(w_A \ B̃) * (B̃ \ B)
end
\(A::Jacobi, w_B::WeightedJacobi) = WeightedJacobi(A) \ w_B
\(w_A::WeightedJacobi, B::Jacobi) = w_A \ WeightedJacobi(B)
\(A::AbstractJacobi, w_B::WeightedJacobi) = Jacobi(A) \ w_B
\(w_A::WeightedJacobi, B::AbstractJacobi) = w_A \ Jacobi(B)


function broadcastbasis(::typeof(+), w_A::WeightedJacobi, w_B::WeightedJacobi)
Expand Down
3 changes: 3 additions & 0 deletions test/test_jacobi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ import ClassicalOrthogonalPolynomials: recurrencecoefficients, basis, MulQuasiMa
# special weighted conversions
W = (JacobiWeight(-1,-1) .* Jacobi(0,0)) \ Jacobi(0,0)
@test ((JacobiWeight(-1,-1) .* Jacobi(0,0)) * W)[0.1,1:10] ≈ Jacobi(0,0)[0.1,1:10]

# potential inexact errors
Jacobi(1.9,1.9) \ Jacobi(0.9,0.9) # isinteger(1.9-0.9) = false
end

@testset "Derivative" begin
Expand Down