Skip to content

Commit c041a60

Browse files
authored
More arithmetic (#35)
* More arithmetic * Fix format * Fixes * Fix format * up
1 parent 4ba5138 commit c041a60

File tree

7 files changed

+61
-30
lines changed

7 files changed

+61
-30
lines changed

src/MultivariateBases.jl

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ end
2525
SA.basis(a::Algebra) = a.basis
2626

2727
#Base.:(==)(::Algebra{BT1,B1,M}, ::Algebra{BT2,B2,M}) where {BT1,B1,BT2,B2,M} = true
28-
#Base.:(==)(::Algebra, ::Algebra) = false
28+
function Base.:(==)(a::Algebra, b::Algebra)
29+
# `===` is a shortcut for speedup
30+
return a.basis === b.basis || a.basis == b.basis
31+
end
2932

3033
function Base.show(io::IO, ::Algebra{BT,B}) where {BT,B}
3134
ioc = IOContext(io, :limit => true, :compact => true)
@@ -72,13 +75,6 @@ function MA.promote_operation(
7275
return Algebra{BT,B,M}
7376
end
7477

75-
const _APL = MP.AbstractPolynomialLike
76-
# We don't define it for all `AlgebraElement` as this would be type piracy
77-
const _AE = SA.AlgebraElement{<:Algebra}
78-
79-
Base.:(+)(p::_APL, q::_AE) = +(p, MP.polynomial(q))
80-
Base.:(+)(p::_AE, q::_APL) = +(MP.polynomial(p), q)
81-
Base.:(-)(p::_APL, q::_AE) = -(p, MP.polynomial(q))
82-
Base.:(-)(p::_AE, q::_APL) = -(MP.polynomial(p), q)
78+
include("arithmetic.jl")
8379

8480
end # module

src/arithmetic.jl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const _APL = MP.AbstractPolynomialLike
2+
# We don't define it for all `AlgebraElement` as this would be type piracy
3+
const _AE = SA.AlgebraElement{<:Algebra}
4+
5+
Base.:(+)(p::_APL, q::_AE) = +(p, MP.polynomial(q))
6+
Base.:(+)(p::_AE, q::_APL) = +(MP.polynomial(p), q)
7+
Base.:(-)(p::_APL, q::_AE) = -(p, MP.polynomial(q))
8+
Base.:(-)(p::_AE, q::_APL) = -(MP.polynomial(p), q)
9+
10+
Base.:(+)(p, q::_AE) = +(constant_algebra_element(typeof(SA.basis(q)), p), q)
11+
function Base.:(+)(p::_AE, q)
12+
return +(MP.polynomial(p), constant_algebra_element(typeof(SA.basis(p)), q))
13+
end
14+
function Base.:(-)(p, q::_AE)
15+
return -(constant_algebra_element(typeof(SA.basis(q)), p), MP.polynomial(q))
16+
end
17+
function Base.:(-)(p::_AE, q)
18+
return -(MP.polynomial(p), constant_algebra_element(typeof(SA.basis(p)), q))
19+
end
20+
21+
function Base.:(+)(p::_AE, q::_AE)
22+
return MA.operate_to!(SA._preallocate_output(+, p, q), +, p, q)
23+
end
24+
25+
function Base.:(-)(p::_AE, q::_AE)
26+
return MA.operate_to!(SA._preallocate_output(-, p, q), -, p, q)
27+
end

src/chebyshev.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ struct ChebyshevFirstKind <: AbstractChebyshev end
2323
const Chebyshev = ChebyshevFirstKind
2424

2525
# https://en.wikipedia.org/wiki/Chebyshev_polynomials#Properties
26-
# T_n * T_m = T_{n + m} / 2 + T_{|n - m|} / 2
26+
# `T_n * T_m = T_{n + m} / 2 + T_{|n - m|} / 2`
2727
function (::Mul{Chebyshev})(a::MP.AbstractMonomial, b::MP.AbstractMonomial)
2828
terms = [MP.term(1 // 1, MP.constant_monomial(a * b))]
2929
vars_a = MP.variables(a)

src/monomial.jl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -198,23 +198,23 @@ function algebra_element(f::Function, basis::SubBasis)
198198
return algebra_element(map(f, eachindex(basis)), basis)
199199
end
200200

201-
function constant_algebra_element(
202-
::Type{FullBasis{B,M}},
203-
::Type{T},
204-
) where {B,M,T}
201+
_one_if_type(α) = α
202+
_one_if_type(::Type{T}) where {T} = one(T)
203+
204+
function constant_algebra_element(::Type{FullBasis{B,M}}, α) where {B,M}
205205
return algebra_element(
206206
sparse_coefficients(
207-
MP.polynomial(MP.term(one(T), MP.constant_monomial(M))),
207+
MP.polynomial(MP.term(_one_if_type), MP.constant_monomial(M))),
208208
),
209209
FullBasis{B,M}(),
210210
)
211211
end
212212

213-
function constant_algebra_element(
214-
::Type{<:SubBasis{B,M}},
215-
::Type{T},
216-
) where {B,M,T}
217-
return algebra_element([one(T)], SubBasis{B}([MP.constant_monomial(M)]))
213+
function constant_algebra_element(::Type{<:SubBasis{B,M}}, α) where {B,M}
214+
return algebra_element(
215+
[_one_if_type(α)],
216+
SubBasis{B}([MP.constant_monomial(M)]),
217+
)
218218
end
219219

220220
function _show(io::IO, mime::MIME, basis::SubBasis{B}) where {B}

src/orthogonal.jl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,15 @@ function SA.coeffs(
190190
end
191191

192192
function SA.coeffs(
193-
p::Polynomial{B},
193+
p::Polynomial{B,M},
194194
::FullBasis{Monomial},
195-
) where {B<:AbstractMultipleOrthogonal}
195+
) where {B<:AbstractMultipleOrthogonal,M}
196196
return sparse_coefficients(
197197
prod(
198-
univariate_orthogonal_basis(B, var, deg)[deg+1] for
199-
(var, deg) in MP.powers(p.monomial)
200-
),
198+
MP.powers(p.monomial);
199+
init = MP.constant_monomial(M),
200+
) do (var, deg)
201+
return univariate_orthogonal_basis(B, var, deg)[deg+1]
202+
end,
201203
)
202204
end

src/scaled.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ function Base.promote_rule(
7070
end
7171

7272
function scaling(m::MP.AbstractMonomial)
73-
return (factorial(MP.degree(m)) / prod(factorial, MP.exponents(m)))
73+
return (
74+
factorial(MP.degree(m)) / prod(factorial, MP.exponents(m); init = 1),
75+
)
7476
end
7577
unscale_coef(t::MP.AbstractTerm) = MP.coefficient(t) / scaling(MP.monomial(t))
7678
function SA.coeffs(

test/runtests.jl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,14 @@ function api_test(B::Type{<:MB.AbstractMonomialIndexed}, degree)
7474
_wrap(MB.SA.trim_LaTeX(mime, sprint(show, mime, p.monomial))) *
7575
" \$\$"
7676
const_mono = constant_monomial(prod(x))
77-
@test const_mono + MB.algebra_element(MB.Polynomial{B}(const_mono)) == 2
78-
@test MB.algebra_element(MB.Polynomial{B}(const_mono)) + const_mono == 2
79-
@test iszero(const_mono - MB.algebra_element(MB.Polynomial{B}(const_mono)))
80-
@test iszero(MB.algebra_element(MB.Polynomial{B}(const_mono)) - const_mono)
77+
const_poly = MB.Polynomial{B}(const_mono)
78+
const_alg_el = MB.algebra_element(const_poly)
79+
for other in (const_mono, 1, const_alg_el)
80+
@test other + const_alg_el 2 * other
81+
@test const_alg_el + other 2 * other
82+
@test iszero(other - const_alg_el)
83+
@test iszero(const_alg_el - other)
84+
end
8185
@test typeof(MB.sparse_coefficients(sum(x))) ==
8286
MA.promote_operation(MB.sparse_coefficients, typeof(sum(x)))
8387
@test typeof(MB.algebra_element(sum(x))) ==

0 commit comments

Comments
 (0)