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

Not specific to Float64 #41

Open
wants to merge 2 commits into
base: master
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
10 changes: 10 additions & 0 deletions src/arithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ function (p::_AE)(args::MP.AbstractSubstitution...)
return MP.substitute(MP.Eval(), p, args...)
end

function (p::_AE)(x::NTuple{N,<:Number}) where {N}
return (MP.polynomial(p))(x)
end

function (p::_AE)(x::AbstractVector{<:Number})
return (MP.polynomial(p))(x)
end

(p::_AE)(x::Number...) = (MP.polynomial(p))(x...)

function MP.differentiate(p::_AE, args...)
return MP.differentiate(MP.polynomial(p), args...)
end
22 changes: 11 additions & 11 deletions src/scaled.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ end
_float(::Type{T}) where {T<:Number} = float(T)
# Could be for instance `MathOptInterface.ScalarAffineFunction{Float64}`
# which does not implement `float`
_float(::Type{F}) where {F} = F
_float(::Type{F}) where {F} = MA.promote_operation(+, F, F)

_promote_coef(::Type{T}, ::Type{ScaledMonomial}) where {T} = _float(T)

function MP.polynomial(f::Function, basis::SubBasis{ScaledMonomial})
function MP.polynomial(f::Function, basis::SubBasis{ScaledMonomial}, ::Type{T}) where {T}
return MP.polynomial(
i -> scaling(basis.monomials[i]) * f(i),
i -> scaling(T, basis.monomials[i]) * f(i),
basis.monomials,
)
end
Expand All @@ -69,12 +69,12 @@ function Base.promote_rule(
return SubBasis{Monomial,M,V}
end

function scaling(m::MP.AbstractMonomial)
return √(
function scaling(::Type{T}, m::MP.AbstractMonomial) where {T}
return √(T(
factorial(MP.degree(m)) / prod(factorial, MP.exponents(m); init = 1),
)
))
end
unscale_coef(t::MP.AbstractTerm) = MP.coefficient(t) / scaling(MP.monomial(t))
unscale_coef(t::MP.AbstractTerm) = MP.coefficient(t) / scaling(MP.coefficient_type(t), MP.monomial(t))
function SA.coeffs(
t::MP.AbstractTermLike,
::FullBasis{ScaledMonomial},
Expand All @@ -84,10 +84,10 @@ function SA.coeffs(
return MP.term(mono * MP.coefficient(t), mono)
end
function MP.coefficients(p, ::FullBasis{ScaledMonomial})
return unscale_coef.(MP.terms(p))
return unscale_coef.(MP.coefficient_type(p), MP.terms(p))
end
function MP.coefficients(p, basis::SubBasis{ScaledMonomial})
return MP.coefficients(p, basis.monomials) ./ scaling.(MP.monomials(p))
return MP.coefficients(p, basis.monomials) ./ scaling.(MP.coefficient_type(p), MP.monomials(p))
end
function SA.coeffs(
p::MP.AbstractPolynomialLike,
Expand All @@ -109,7 +109,7 @@ function SA.coeffs!(
SA.unsafe_push!(
res,
target[Polynomial{Monomial}(mono)],
v * scaling(mono),
v * scaling(eltype(res), mono),
)
end
MA.operate!(SA.canonical, res)
Expand All @@ -128,7 +128,7 @@ function SA.coeffs!(
SA.unsafe_push!(
res,
target[Polynomial{ScaledMonomial}(mono)],
v / scaling(mono),
v / scaling(eltype(T), mono),
)
end
MA.operate!(SA.canonical, res)
Expand Down
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ function api_test(B::Type{<:MB.AbstractMonomialIndexed}, degree)
@test typeof(MB.algebra_element(sum(x))) ==
MA.promote_operation(MB.algebra_element, typeof(sum(x)))
@test const_alg_el(x => ones(length(x))) == const_mono
@test const_alg_el(ones(length(x))) == const_mono
@test const_alg_el(ones(length(x))...) == const_mono
@test const_alg_el(tuple(ones(length(x))...)) == const_mono
@test subs(const_alg_el, x => ones(length(x))) == const_mono
@test differentiate(const_alg_el, x) == differentiate(const_mono, x)
end
Expand Down
Loading