-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add orthonormal basis * Fix format
- Loading branch information
Showing
8 changed files
with
161 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
""" | ||
struct OrthonormalCoefficientsBasis{PT<:MP.AbstractPolynomialLike, PV<:AbstractVector{PT}} <: AbstractPolynomialBasis | ||
polynomials::PV | ||
end | ||
Polynomial basis with the polynomials of the vector `polynomials` that are | ||
orthonormal with respect to the inner produce derived from the inner product | ||
of their coefficients. | ||
For instance, `FixedPolynomialBasis([1, x, 2x^2-1, 4x^3-3x])` is the Chebyshev | ||
polynomial basis for cubic polynomials in the variable `x`. | ||
""" | ||
struct OrthonormalCoefficientsBasis{ | ||
PT<:MP.AbstractPolynomialLike, | ||
PV<:AbstractVector{PT}, | ||
} <: AbstractPolynomialVectorBasis{PT,PV} | ||
polynomials::PV | ||
end | ||
|
||
function LinearAlgebra.dot( | ||
p::MP.AbstractPolynomialLike{S}, | ||
q::MP.AbstractPolynomialLike{T}, | ||
::Type{<:OrthonormalCoefficientsBasis}, | ||
) where {S,T} | ||
s = zero(MA.promote_operation(*, S, T)) | ||
terms_p = MP.terms(p) | ||
terms_q = MP.terms(q) | ||
tsp = iterate(terms_p) | ||
tsq = iterate(terms_q) | ||
while !isnothing(tsp) && !isnothing(tsq) | ||
tp, sp = tsp | ||
tq, sq = tsq | ||
cmp = MP.compare(MP.monomial(tp), MP.monomial(tq)) | ||
if iszero(cmp) | ||
s += conj(MP.coefficient(tp)) * MP.coefficient(tq) | ||
tsp = iterate(terms_p, sp) | ||
tsq = iterate(terms_q, sq) | ||
elseif cmp < 0 | ||
tsp = iterate(terms_p, sp) | ||
else | ||
tsq = iterate(terms_q, sq) | ||
end | ||
end | ||
return s | ||
end | ||
|
||
function MP.coefficients(p, basis::OrthonormalCoefficientsBasis) | ||
return [LinearAlgebra.dot(q, p, typeof(basis)) for q in generators(basis)] | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using Test | ||
using MultivariateBases | ||
using DynamicPolynomials | ||
|
||
@polyvar x y | ||
|
||
@testset "Polynomials" begin | ||
gens = [1 + x + y + x * y, 1 - x + y - x * y] / 2 | ||
basis = OrthonormalCoefficientsBasis(gens) | ||
@test iszero(dot(gens[1], gens[2], OrthonormalCoefficientsBasis)) | ||
coefficient_test(basis, [2, -3]) | ||
coefficient_test(basis, [-2im, 1 + 5im]) | ||
coefficient_test(basis, [1im, 2im]) | ||
@test polynomial_type(basis, Int) == polynomial_type(x, Float64) | ||
@test polynomial(one, basis) == 1 + y | ||
@test basis[1] == gens[1] | ||
@test basis[2] == gens[2] | ||
@test collect(basis) == gens | ||
@test generators(basis) == gens | ||
@test length(basis) == 2 | ||
@test mindegree(basis) == 0 | ||
@test mindegree(basis, x) == 0 | ||
@test mindegree(basis, y) == 0 | ||
@test maxdegree(basis) == 2 | ||
@test maxdegree(basis, x) == 1 | ||
@test maxdegree(basis, y) == 1 | ||
@test extdegree(basis) == (0, 2) | ||
@test extdegree(basis, x) == (0, 1) | ||
@test extdegree(basis, y) == (0, 1) | ||
@test variables(basis) == [x, y] | ||
@test nvariables(basis) == 2 | ||
@test sprint(show, basis) == | ||
"OrthonormalCoefficientsBasis([0.5 + 0.5y + 0.5x + 0.5xy, 0.5 + 0.5y - 0.5x - 0.5xy])" | ||
@test sprint(print, basis) == | ||
"OrthonormalCoefficientsBasis([0.5 + 0.5*y + 0.5*x + 0.5*x*y, 0.5 + 0.5*y - 0.5*x - 0.5*x*y])" | ||
b2 = basis[2:2] | ||
@test length(b2) == 1 | ||
@test b2[1] == gens[2] | ||
b3 = basis[2:1] | ||
@test isempty(b3) | ||
end | ||
@testset "Linear" begin | ||
basis = OrthonormalCoefficientsBasis([x, y]) | ||
@test polynomial_type(basis, Int) == polynomial_type(x, Int) | ||
@test polynomial(identity, basis) == x + 2y | ||
end | ||
@testset "One variable" begin | ||
basis = OrthonormalCoefficientsBasis([x]) | ||
@test polynomial_type(basis, Int) == polynomial_type(x, Int) | ||
@test polynomial(i -> 5, basis) == 5x | ||
@test typeof(polynomial(i -> 5, basis)) == polynomial_type(basis, Int) | ||
@test typeof(polynomial(ones(Int, 1, 1), basis, Int)) <: | ||
AbstractPolynomial{Int} | ||
@test typeof(polynomial(ones(Int, 1, 1), basis, Float64)) <: | ||
AbstractPolynomial{Float64} | ||
end | ||
@testset "Complex" begin | ||
for a in [1, -1, im, -im] | ||
basis = OrthonormalCoefficientsBasis([a * x]) | ||
@test 5x^2 == | ||
@inferred polynomial(5ones(Int, 1, 1), basis, Complex{Int}) | ||
@test 5x^2 == @inferred polynomial(5ones(Int, 1, 1), basis, Int) | ||
coefficient_test(basis, [2]) | ||
coefficient_test(basis, [-2im]) | ||
coefficient_test(basis, [1 + 5im]) | ||
end | ||
end | ||
@testset "Empty" begin | ||
basis = OrthonormalCoefficientsBasis(typeof(x + 1)[]) | ||
@test isempty(basis) | ||
@test isempty(eachindex(basis)) | ||
p = @inferred polynomial(zeros(Int, 0, 0), basis, Int) | ||
@test iszero(p) | ||
end | ||
|
||
@testset "Enumerate" begin | ||
monos = [1, x, y^2] | ||
basis = OrthonormalCoefficientsBasis(monos) | ||
for (i, e) in enumerate(basis) | ||
@test e == monos[i] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters