Skip to content

Commit e0f733d

Browse files
committed
Add Fixed basis
1 parent 37d9cf3 commit e0f733d

File tree

6 files changed

+73
-12
lines changed

6 files changed

+73
-12
lines changed

src/MultivariateBases.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,6 @@ function MA.promote_operation(
8282
end
8383

8484
include("arithmetic.jl")
85+
include("fixed.jl")
8586

8687
end # module

src/arithmetic.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,18 @@ for op in [:+, :-]
7676
end
7777
end
7878

79+
function term_element(α, p::Polynomial{B,M}) where {B,M}
80+
return algebra_element(
81+
sparse_coefficients(MP.term(α, p.monomial)),
82+
FullBasis{B,M}(),
83+
)
84+
end
85+
86+
# Needed by `SymbolicWedderburn` which multiplies elements of the basis by `Int`
87+
# We'll see if `::Number` is too restrictive
88+
# Should be able to remove once https://github.com/kalmarek/SymbolicWedderburn.jl/issues/88 is closed
89+
Base.:*::Number, p::Polynomial) = term_element(α, p)
90+
7991
function MA.operate!(op::Union{typeof(+),typeof(-),typeof(*)}, p::_APL, q::_AE)
8092
return MA.operate!(op, p, MP.polynomial(q))
8193
end

src/fixed.jl

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
struct FixedPolynomialBasis{B,M,T,V} <: SA.ExplicitBasis{
2+
SA.AlgebraElement{Algebra{FullBasis{B,M},B,M},T,V},
3+
Int,
4+
}
5+
elements::Vector{SA.AlgebraElement{Algebra{FullBasis{B,M},B,M},T,V}}
6+
end
7+
8+
Base.length(b::FixedPolynomialBasis) = length(b.elements)
9+
Base.getindex(b::FixedPolynomialBasis, i::Integer) = b.elements[i]
10+
11+
function Base.show(io::IO, b::FixedPolynomialBasis)
12+
print(io, "FixedPolynomialBasis(")
13+
_show_vector(io, MIME"text/plain"(), b.elements)
14+
print(io, ")")
15+
return
16+
end
17+
18+
struct SemisimpleBasis{T,I,B<:SA.ExplicitBasis{T,I}} <: SA.ExplicitBasis{T,I}
19+
bases::Vector{B}
20+
end
21+
22+
Base.length(b::SemisimpleBasis) = length(first(b.bases))
23+
24+
struct MultiPoly{P}
25+
polynomials::Vector{P}
26+
end
27+
SA.star(p::MultiPoly) = MultiPoly(SA.star.(p.polynomials))
28+
29+
Base.getindex(b::SemisimpleBasis, i::Integer) = MultiPoly(getindex.(b.bases, i))
30+
31+
function Base.show(io::IO, b::SemisimpleBasis)
32+
if length(b.bases) == 1
33+
print(io, "Simple basis:")
34+
else
35+
print(io, "Semisimple basis with $(length(b.bases)) simple sub-bases:")
36+
end
37+
for basis in b.bases
38+
println(io)
39+
print(io, " ")
40+
print(io, basis)
41+
end
42+
end

src/monomial.jl

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -258,20 +258,26 @@ function constant_algebra_element(::Type{<:SubBasis{B,M}}, α) where {B,M}
258258
)
259259
end
260260

261-
function _show(io::IO, mime::MIME, basis::SubBasis{B}) where {B}
262-
print(io, "SubBasis{$(nameof(B))}")
263-
print(io, "([")
261+
# TODO use Base.show_vector here, maybe by wrapping the `generator` vector
262+
# into something that spits objects wrapped with the `mime` type
263+
function _show_vector(io::IO, mime::MIME, v)
264+
print(io, '[')
264265
first = true
265-
# TODO use Base.show_vector here, maybe by wrapping the `generator` vector
266-
# into something that spits objects wrapped with the `mime` type
267-
for mono in basis.monomials
266+
for el in v
268267
if !first
269268
print(io, ", ")
270269
end
271270
first = false
272-
show(io, mime, mono)
271+
show(io, mime, el)
273272
end
274-
return print(io, "])")
273+
print(io, ']')
274+
end
275+
276+
function _show(io::IO, mime::MIME, basis::SubBasis{B}) where {B}
277+
print(io, "SubBasis{$(nameof(B))}(")
278+
_show_vector(io, mime, basis.monomials)
279+
print(io, ')')
280+
return
275281
end
276282

277283
function Base.show(io::IO, mime::MIME"text/plain", basis::SubBasis)
@@ -449,7 +455,7 @@ function SA.coeffs(
449455
return SA.SparseCoefficients(_vec(source.monomials), _vec(cfs))
450456
else
451457
res = SA.zero_coeffs(
452-
_promote_coef(_promote_coef(valtype(cfs), B1), B2),
458+
_promote_coef(_promote_coef(SA.value_type(cfs), B1), B2),
453459
target,
454460
)
455461
return SA.coeffs!(res, cfs, source, target)

src/polynomial.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# TODO Add to MultivariatePolynomials
22
MP.variables(p::SA.AlgebraElement) = MP.variables(explicit_basis(p))
33
Base.keytype(p::MP.AbstractPolynomialLike) = MP.monomial_type(p)
4-
Base.valtype(p::MP.AbstractPolynomialLike) = MP.coefficient_type(p)
4+
SA.value_type(p::MP.AbstractPolynomialLike) = MP.coefficient_type(p)
55
#Base.keys(p::MP.AbstractPolynomial) = MP.monomials(p)
66
SA.nonzero_pairs(p::MP.AbstractPolynomialLike) = MP.terms(p)
77
function Base.similar(p::PT, ::Type{T}) where {PT<:MP.AbstractPolynomial,T}

test/monomial.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ using DynamicPolynomials
1414
@test MB.Polynomial{MB.Monomial}(x^3) != MB.Polynomial{MB.Monomial}(x^2)
1515
end
1616

17-
@testset "Linear" begin
17+
#@testset "Linear" begin
1818
basis = SubBasis{MB.Monomial}([x, y])
1919
@test basis == SubBasis{MB.Monomial}([y, x])
2020
@test basis != SubBasis{MB.Monomial}([y, y^2, x])
@@ -33,7 +33,7 @@ end
3333
@test sprint(show, MIME"text/plain"(), basis) ==
3434
"SubBasis{Monomial}([y, x])"
3535
@test sprint(print, basis) == "SubBasis{Monomial}([y, x])"
36-
end
36+
#end
3737
@testset "Affine" begin
3838
# It will be sorted and 1 will be moved at the end
3939
basis = SubBasis{MB.Monomial}([1, x, y])

0 commit comments

Comments
 (0)