Skip to content

Commit

Permalink
v0.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
blegat committed Oct 23, 2021
1 parent 603d61c commit 7c5153d
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name = "MultivariatePolynomials"
uuid = "102ac46a-7ee4-5c85-9060-abc95bfdeaa3"
license = "MIT"
repo = "https://github.com/JuliaAlgebra/MultivariatePolynomials.jl"
version = "0.3.18"
version = "0.4.0"

[deps]
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
Expand Down
2 changes: 1 addition & 1 deletion docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ makedocs(
# See https://github.com/JuliaDocs/Documenter.jl/issues/868
prettyurls = get(ENV, "CI", nothing) == "true"
),
# See https://github.com/JuliaOpt/JuMP.jl/issues/1576
# See https://github.com/jump-dev/JuMP.jl/issues/1576
strict = true,

pages = [
Expand Down
19 changes: 12 additions & 7 deletions src/conversion.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export variable
export variable, convert_to_constant

function convertconstant end
Base.convert(::Type{P}, α) where P<:APL = convertconstant(P, α)
Expand Down Expand Up @@ -60,18 +60,23 @@ function Base.convert(::Type{T}, p::AbstractPolynomial) where T <: AbstractTermL
end

MA.scaling(p::AbstractPolynomialLike{T}) where {T} = convert(T, p)
# Conversion polynomial -> scalar
function scalarize(::Type{S}, p::APL) where S
# Conversion polynomial -> constant
# We don't define a method for `Base.convert` to reduce invalidations;
# see https://github.com/JuliaAlgebra/MultivariatePolynomials.jl/pull/172
function convert_to_constant(::Type{S}, p::APL) where S
s = zero(S)
for t in terms(p)
if !isconstant(t)
# The polynomial is not constant
throw(InexactError(:convert, S, p))
throw(InexactError(:convert_to_constant, S, p))
end
s += S(coefficient(t))
s = MA.add!!(s, convert(S, coefficient(t)))
end
s
return s
end
Base.convert(::Type{T}, p::APL) where T<:Number = convert_to_constant(T, p)
function convert_to_constant(p::APL{S}) where {S}
return convert_to_constant(S, p)
end
Base.convert(::Type{T}, p::APL) where T<:Number = scalarize(T, p)

Base.convert(::Type{PT}, p::PT) where {PT<:APL} = p
24 changes: 23 additions & 1 deletion src/monovec.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,34 @@ function monovec(X::AbstractVector{MT}) where {MT<:AbstractMonomial}
end
monovec(X::AbstractVector{TT}) where {TT<:AbstractTermLike} = monovec(AbstractVector{monomialtype(TT)}(X))

"""
monovec(a, X::AbstractVector{MT}) where {MT<:AbstractMonomialLike}
Returns `b, Y` where `Y` is the vector of monomials of `X` in decreasing order
and without any duplicates and `b` is the vector of corresponding coefficients
in `a`, where coefficients of duplicate entries are summed together.
### Examples
Calling `monovec` on ``[2, 1, 4, 3, -1], [xy, x, xy, x^2y, x]`` should return
``[3, 6, 0], [x^2y, xy, x]``.
"""
function monovec(a, x)
if length(a) != length(x)
throw(ArgumentError("There should be as many coefficient than monomials"))
end
σ, X = sortmonovec(x)
(a[σ], X)
b = a[σ]
if length(x) > length(X)
rev = Dict(X[j] => j for j in eachindex(σ))
for i in eachindex(x)
j = rev[x[i]]
if i != σ[j]
b[j] += a[i]
end
end
end
return b, X
end

"""
Expand Down
5 changes: 4 additions & 1 deletion src/polynomial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ end

polynomial(f::Function, mv::AbstractVector{<:AbstractMonomialLike}) = polynomial!([term(f(i), mv[i]) for i in 1:length(mv)])

polynomial(a::AbstractVector, x::AbstractVector, s::ListState=MessyState()) = polynomial([term(α, m) for (α, m) in zip(a, x)], s)
function polynomial(a::AbstractVector, x::AbstractVector, s::ListState=MessyState())
# If `x` is e.g. `[v, 1]` then it will contains terms that are convertible to monomials.
return polynomial([term(α, convert(monomialtype(m), m)) for (α, m) in zip(a, x)], s)
end

polynomial(ts::AbstractVector, s::ListState=MessyState()) = sum(ts)
polynomial!(ts::AbstractVector, s::ListState=MessyState()) = sum(ts)
Expand Down
11 changes: 9 additions & 2 deletions test/polynomial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,9 @@ const MP = MultivariatePolynomials

@inferred polynomial(i -> float(i), [x, x*x])
@inferred polynomial(i -> 3 - float(i), monovec([x*x, x]))
for p in (polynomial(i -> float(i), [x, x*x]),
polynomial(i -> 3 - float(i), monovec([x*x, x])))
for p in [polynomial(i -> float(i), [x, x*x]),
polynomial(i -> 1.0, [x*x, x, x*x]),
polynomial(i -> 3 - float(i), monovec([x*x, x]))]
@test coefficients(p) == [2.0, 1.0]
@test monomials(p) == monovec([x^2, x])
end
Expand Down Expand Up @@ -143,10 +144,16 @@ const MP = MultivariatePolynomials
end

@testset "Convertion" begin
Mod.@polyvar x y z
p = 2.5x + 1 - 2.5x
@test convert(Int, p) == 1
@test convert(typeof(p), p) === p
@test convert(Union{Nothing, typeof(p)}, p) === p
a = 2y
q = polynomial([a, z, -a], [x, 1, x])
@test convert_to_constant(q) == z
q = polynomial([a, z], [x, 1])
@test_throws InexactError convert_to_constant(q)
end

@testset "Vector" begin
Expand Down

2 comments on commit 7c5153d

@blegat
Copy link
Member Author

@blegat blegat commented on 7c5153d Oct 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/47382

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.4.0 -m "<description of version>" 7c5153dfad948c894f5dc7636abf365c1088855d
git push origin v0.4.0

Please sign in to comment.