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

Add substitute promotion #11

Open
wants to merge 10 commits into
base: myb/mp
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
5 changes: 5 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ jobs:
${{ runner.os }}-test-${{ env.cache-name }}-
${{ runner.os }}-test-
${{ runner.os }}-
- name: MP#master
shell: julia --project=@. {0}
run: |
using Pkg
Pkg.add(PackageSpec(name="MultivariatePolynomials", rev="master"))
- uses: julia-actions/julia-buildpkg@v1
- uses: julia-actions/julia-runtest@v1
- uses: julia-actions/julia-processcoverage@v1
Expand Down
39 changes: 33 additions & 6 deletions Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@

[[ArgTools]]
uuid = "0dad84c5-d112-42e6-8d28-ef12dabb789f"
version = "1.1.1"

[[Artifacts]]
uuid = "56f22d72-fd6d-98f1-02f0-08ddc0907c33"

[[Base64]]
uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"

[[ChainRulesCore]]
deps = ["Compat", "LinearAlgebra", "SparseArrays"]
git-tree-sha1 = "e7ff6cadf743c098e08fca25c91103ee4303c9bb"
uuid = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
version = "1.15.6"

[[Compat]]
deps = ["Base64", "Dates", "DelimitedFiles", "Distributed", "InteractiveUtils", "LibGit2", "Libdl", "LinearAlgebra", "Markdown", "Mmap", "Pkg", "Printf", "REPL", "Random", "SHA", "Serialization", "SharedArrays", "Sockets", "SparseArrays", "Statistics", "Test", "UUIDs", "Unicode"]
git-tree-sha1 = "96b0bc6c52df76506efc8a441c6cf1adcb1babc4"
Expand All @@ -18,6 +25,7 @@ version = "3.42.0"
[[CompilerSupportLibraries_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "e66e0078-7015-5450-92f7-15fbd957f2ae"
version = "1.0.1+0"

[[DataStructures]]
deps = ["Compat", "InteractiveUtils", "OrderedCollections"]
Expand All @@ -38,8 +46,12 @@ deps = ["Random", "Serialization", "Sockets"]
uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b"

[[Downloads]]
deps = ["ArgTools", "LibCURL", "NetworkOptions"]
deps = ["ArgTools", "FileWatching", "LibCURL", "NetworkOptions"]
uuid = "f43a241f-c20a-4ad4-852c-f6b1247861c6"
version = "1.6.0"

[[FileWatching]]
uuid = "7b1f6079-737a-58dc-b8bc-7a2ca5c1b5ee"

[[InteractiveUtils]]
deps = ["Markdown"]
Expand All @@ -48,10 +60,12 @@ uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
[[LibCURL]]
deps = ["LibCURL_jll", "MozillaCACerts_jll"]
uuid = "b27032c2-a3e7-50c8-80cd-2d36dbcbfd21"
version = "0.6.3"

[[LibCURL_jll]]
deps = ["Artifacts", "LibSSH2_jll", "Libdl", "MbedTLS_jll", "Zlib_jll", "nghttp2_jll"]
uuid = "deac9b47-8bc7-5906-a0fe-35ac56dc84c0"
version = "7.84.0+0"

[[LibGit2]]
deps = ["Base64", "NetworkOptions", "Printf", "SHA"]
Expand All @@ -60,6 +74,7 @@ uuid = "76f85450-5226-5b5a-8eaa-529ad045b433"
[[LibSSH2_jll]]
deps = ["Artifacts", "Libdl", "MbedTLS_jll"]
uuid = "29816b5a-b9ab-546f-933c-edad1886dfa8"
version = "1.10.2+0"

[[Libdl]]
uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
Expand All @@ -78,31 +93,35 @@ uuid = "d6f4376e-aef5-505a-96c1-9c027394607a"
[[MbedTLS_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "c8ffd9c3-330d-5841-b78e-0817d7145fa1"
version = "2.28.0+0"

[[Mmap]]
uuid = "a63ad114-7e13-5084-954f-fe012c677804"

[[MozillaCACerts_jll]]
uuid = "14a3606d-f60d-562e-9121-12d972cd8159"
version = "2022.2.1"

[[MultivariatePolynomials]]
deps = ["DataStructures", "LinearAlgebra", "MutableArithmetics"]
path = "/Users/scheme/src/julia/MultivariatePolynomials"
deps = ["ChainRulesCore", "DataStructures", "LinearAlgebra", "MutableArithmetics"]
git-tree-sha1 = "393fc4d82a73c6fe0e2963dd7c882b09257be537"
uuid = "102ac46a-7ee4-5c85-9060-abc95bfdeaa3"
version = "0.4.4"
version = "0.4.6"

[[MutableArithmetics]]
deps = ["LinearAlgebra", "SparseArrays", "Test"]
git-tree-sha1 = "ba8c0f8732a24facba709388c74ba99dcbfdda1e"
git-tree-sha1 = "aa532179d4a643d4bd9f328589ca01fa20a0d197"
uuid = "d8a4904e-b15c-11e9-3269-09a3773c0cb0"
version = "1.0.0"
version = "1.1.0"

[[NetworkOptions]]
uuid = "ca575930-c2e3-43a9-ace4-1e988b2c1908"
version = "1.2.0"

[[OpenBLAS_jll]]
deps = ["Artifacts", "CompilerSupportLibraries_jll", "Libdl"]
uuid = "4536629a-c528-5b80-bd46-f80d51c5b363"
version = "0.3.20+0"

[[OrderedCollections]]
git-tree-sha1 = "85f8e6578bf1f9ee0d11e7bb1b1456435479d47c"
Expand All @@ -112,6 +131,7 @@ version = "1.4.1"
[[Pkg]]
deps = ["Artifacts", "Dates", "Downloads", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "Serialization", "TOML", "Tar", "UUIDs", "p7zip_jll"]
uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
version = "1.8.0"

[[Printf]]
deps = ["Unicode"]
Expand All @@ -127,6 +147,7 @@ uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

[[SHA]]
uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce"
version = "0.7.0"

[[SIMD]]
git-tree-sha1 = "39e3df417a0dd0c4e1f89891a281f82f5373ea3b"
Expand Down Expand Up @@ -154,10 +175,12 @@ uuid = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
[[TOML]]
deps = ["Dates"]
uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
version = "1.0.0"

[[Tar]]
deps = ["ArgTools", "SHA"]
uuid = "a4e569a6-e804-4fa4-b0f3-eef7a1d5b13e"
version = "1.10.1"

[[Test]]
deps = ["InteractiveUtils", "Logging", "Random", "Serialization"]
Expand All @@ -173,15 +196,19 @@ uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5"
[[Zlib_jll]]
deps = ["Libdl"]
uuid = "83775a58-1f1d-513f-b197-d71354ab007a"
version = "1.2.12+3"

[[libblastrampoline_jll]]
deps = ["Artifacts", "Libdl", "OpenBLAS_jll"]
uuid = "8e850b90-86db-534c-a0d3-1478176c7d93"
version = "5.1.1+0"

[[nghttp2_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "8e850ede-7688-5339-a07c-302acd2aaf8d"
version = "1.48.0+0"

[[p7zip_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "3f19e933-33d8-53b3-aaab-bd5110c3b7a0"
version = "17.4.0+0"
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ version = "0.1.0"

[deps]
MultivariatePolynomials = "102ac46a-7ee4-5c85-9060-abc95bfdeaa3"
MutableArithmetics = "d8a4904e-b15c-11e9-3269-09a3773c0cb0"
SIMD = "fdea26ae-647d-5447-a871-4b548cad5224"

[compat]
MultivariatePolynomials = "0.4"
MutableArithmetics = "1"
SIMD = "3"
julia = "1"

Expand Down
7 changes: 4 additions & 3 deletions src/SIMDPolynomials.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
module SIMDPolynomials

using MultivariatePolynomials
const Term = MultivariatePolynomials.Term
const Polynomial = MultivariatePolynomials.Polynomial
import MutableArithmetics as MA
import MultivariatePolynomials as MP
const Term = MP.Term
const Polynomial = MP.Polynomial
export Uninomial, Uniterm, Poly
export PackedMonomial, Monomial, Term, MPoly
export terms, coeffs, content, contprim
Expand Down
6 changes: 3 additions & 3 deletions src/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Base.:+(y::T, x::T) where {T <: AbstractMonomial} = Term(y) + Term(x)
Base.:*(c::CoeffType, m::AbstractMonomial) = Term(c, m)
Base.:*(m::AbstractMonomial, c::CoeffType) = c * m
Base.:^(y::T, n::Integer) where {T <: AbstractMonomial} = iszero(n) ? T() : Base.power_by_squaring(y, n)
monomialtype(x::AbstractMonomial) = typeof(x)
monomial_type(x::AbstractMonomial) = typeof(x)
nvariables(x::AbstractMonomial) = error("nvariables not implemented for $(typeof(x))")

###
Expand All @@ -41,7 +41,7 @@ Base.zero(t::T) where {T<:AbstractTerm} = parameterless_type(T)(zero(coeff(t)),
Base.one(t::T) where {T<:AbstractTerm} = parameterless_type(T)(one(coeff(t)), one(monomial(t)))
Base.isinteger(x::AbstractTerm) = isinteger(coeff(x))

monomialtype(x::AbstractTerm) = monomialtype(monomial(x))
monomial_type(x::AbstractTerm) = monomial_type(monomial(x))
exps(x::AbstractTerm) = exps(monomial(x))

Base.:*(x::T, y::T) where {T<:AbstractTerm} = T(coeff(x) * coeff(y), monomial(x) * monomial(y))
Expand Down Expand Up @@ -141,7 +141,7 @@ Base.promote_rule(p::Type{<:AbstractPolynomial}, ::Type{<:CoeffType}) = p
Base.iszero(x::AbstractPolynomial) = isempty(terms(x))
Base.isone(x::AbstractPolynomial) = (ts = terms(x); length(ts) == 1 && isone(only(ts)))
Base.:(==)(x::T, y::T) where {T<:AbstractPolynomial} = x === y || (terms(x) == terms(y))
monomialtype(p::AbstractPolynomial) = monomialtype(lt(p))
monomial_type(p::AbstractPolynomial) = monomial_type(lt(p))
monomials(p::AbstractPolynomial) = (monomial(t) for t in terms(p))

function Base.show(io::IO, p::AbstractPolynomial)
Expand Down
2 changes: 1 addition & 1 deletion src/monomial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const IDType = UInt32
const NOT_A_VAR = typemax(IDType)
const EMPTY_IDS = IDType[]

struct Monomial <: AbstractMonomial
struct Monomial <: MP.AbstractMonomial
ids::Vector{IDType}
end
Monomial() = Monomial(EMPTY_IDS)
Expand Down
2 changes: 1 addition & 1 deletion src/mpoly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ MPoly{T}(x::M) where {T,M<:AbstractMonomial} = MPoly(Term(x))
MPoly{T}(x::CoeffType) where {T} = MPoly(eltype(T)(x))
terms(x::MPoly) = x.terms
function nvariables(p::MPoly)
if monomialtype(p) <: PackedMonomial
if monomial_type(p) <: PackedMonomial
nvariables(monomial(lt(p)))
else
error()
Expand Down
62 changes: 38 additions & 24 deletions src/packedmonomial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ new_E(::Val{E}) where {E} = max(Base._nextpow2(1+E)-1, 7)

Bit packed monomial with maximum of L variables and E bits of exponents.
"""
struct PackedMonomial{L,E,K} <: AbstractMonomial
struct PackedMonomial{L,E,K} <: MP.AbstractMonomial
bits::NTuple{K,UInt64}
function PackedMonomial{L,E}() where {L,E}
EN = new_E(Val(E))
Expand All @@ -50,8 +50,8 @@ struct PackedMonomial{L,E,K} <: AbstractMonomial
PackedMonomial{L,E,K}(bits::NTuple{K,UInt64}) where {L,E,K} = new{L,new_E(Val(E)),K}(bits)
end
Base.copy(m::PackedMonomial) = m
nvariables(x::PackedMonomial{L}) where L = L
function MultivariatePolynomials.exponents(m::PackedMonomial{L,E,K}) where {L,E,K}
nvariables(::PackedMonomial{L}) where L = L
function MP.exponents(m::PackedMonomial{L,E,K}) where {L,E,K}
tup = m.bits
k = 0
vpu = var_per_UInt64(Val(E))
Expand Down Expand Up @@ -149,7 +149,7 @@ end

# the first chunk is the total degree
Base.isone(x::PackedMonomial) = iszero(first(x.bits))
function MultivariatePolynomials.grlex(x::T, y::T) where {T<:PackedMonomial}
function MP.compare(x::T, y::T) where {T<:PackedMonomial}
x.bits < y.bits ? -1 : (x == y ? 0 : 1)
end
Base.isless(x::T, y::T) where {T<:PackedMonomial} = x.bits < y.bits
Expand Down Expand Up @@ -184,7 +184,7 @@ function Base.:*(x::T, y::T) where {L,E,T<:PackedMonomial{L,E}}
return T(xys)
end

function MultivariatePolynomials.divides(y::T, x::T) where {L,E,T<:PackedMonomial{L,E}}
function MP.divides(y::T, x::T) where {L,E,T<:PackedMonomial{L,E}}
xys = _fmap(-, x.bits, y.bits)
o = reduce_tup(|, _fmap(Base.Fix2(zero_bits, Val(E)), xys))

Expand All @@ -193,23 +193,23 @@ function MultivariatePolynomials.divides(y::T, x::T) where {L,E,T<:PackedMonomia
return o == zero(UInt64)
end

MultivariatePolynomials.constantmonomial(::M) where {M<:PackedMonomial} = constantmonomial(M)
function MultivariatePolynomials.constantmonomial(M::Type{<:PackedMonomial})
MP.constant_monomial(::M) where {M<:PackedMonomial} = MP.constant_monomial(M)
function MP.constant_monomial(M::Type{<:PackedMonomial})
M()
end

# TODO: make it fast
function MultivariatePolynomials.mapexponents!(f::F, m1::M, m2::M) where {F<:Function, L, E, M<:PackedMonomial{L,E}}
MultivariatePolynomials.mapexponents(f, m1, m2)
function MP.map_exponents!(f::F, m1::M, m2::M) where {F<:Function, L, E, M<:PackedMonomial{L,E}}
MP.map_exponents(f, m1, m2)
end
function MultivariatePolynomials.mapexponents(f::F, m1::M, m2::M) where {F<:Function, L, E, M<:PackedMonomial{L,E}}
e1 = exponents(m1)
e2 = exponents(m2)
function MP.map_exponents(f::F, m1::M, m2::M) where {F<:Function, L, E, M<:PackedMonomial{L,E}}
e1 = MP.exponents(m1)
e2 = MP.exponents(m2)
ne = map(f, e1, e2)
prod(((i, e),)->PackedMonomial{L,E}(i-1)^e, enumerate(ne))
end

function MultivariatePolynomials._div(x::T, y::T) where {L,E,T<:PackedMonomial{L,E}}
function MP.div_multiple(x::T, y::T) where {L,E,T<:PackedMonomial{L,E}}
xys = _fmap(-, x.bits, y.bits)
o = reduce_tup(|, _fmap(Base.Fix2(zero_bits, Val(E)), xys))

Expand Down Expand Up @@ -295,7 +295,7 @@ function Base.show(io::IO, m::PackedMonomial{L,E}) where {L,E}
end
end

struct Variable{L,E} <: AbstractVariable
struct Variable{L,E} <: MP.AbstractVariable
id::UInt
end

Expand All @@ -305,25 +305,39 @@ end
function Base.isless(v1::V, v2::V) where {V<:Variable}
isless(v1.id, v2.id)
end
function MultivariatePolynomials.name_base_indices(v::Variable)
function MP.name_base_indices(v::Variable)
"x", (v.id,)
end
MultivariatePolynomials.name(v::Variable) = string("x", int2lowerscript(v.id))
MultivariatePolynomials.variable_union_type(::Type{<:PackedMonomial{L,E}}) where {L,E} = Variable{L,E}
MP.name(v::Variable) = string("x", int2lowerscript(v.id))
MP.variable_union_type(::Type{<:PackedMonomial{L,E}}) where {L,E} = Variable{L,E}

MultivariatePolynomials.variables(::Type{<:PackedMonomial{L,E}}) where {L, E} = ntuple(i->Variable{L,E}(i-1), Val(L))
MultivariatePolynomials.variables(::M) where {M<:PackedMonomial} = variables(M)
MP.variables(::Type{<:PackedMonomial{L,E}}) where {L, E} = ntuple(i->Variable{L,E}(i-1), Val(L))
MP.variables(::M) where {M<:PackedMonomial} = MP.variables(M)

MultivariatePolynomials.termtype(M::Type{<:PackedMonomial}, ::Type{T}) where {T} = Term{T, M}
MultivariatePolynomials.polynomialtype(::Type{Term{T, M}}) where {T, M<:PackedMonomial} = Polynomial{T, Term{T, M}, Vector{Term{T, M}}}
MP.term_type(M::Type{<:PackedMonomial}, ::Type{T}) where {T} = Term{T, M}
MP.polynomial_type(::Type{Term{T, M}}) where {T, M<:PackedMonomial} = Polynomial{T, Term{T, M}, Vector{Term{T, M}}}

MultivariatePolynomials.nvariables(p::Polynomial{T, Term{T, M}}) where {T, L, M<:PackedMonomial{L}} = L
MultivariatePolynomials.variables(p::Polynomial{T, Term{T, M}}) where {T, M<:PackedMonomial} = variables(M)
MP.nvariables(p::Polynomial{T, Term{T, M}}) where {T, L, M<:PackedMonomial{L}} = L
MP.variables(p::Polynomial{T, Term{T, M}}) where {T, M<:PackedMonomial} = MP.variables(M)
Base.:(^)(x::Variable{L,E}, p::Integer) where {L,E} = PackedMonomial{L,E}(x.id) ^ p
function Base.:(==)(m1::T, m2::T) where {T<:PackedMonomial}
m1 === m2
end

function MultivariatePolynomials.substitute(::MultivariatePolynomials.Subs, v::V, p::Pair{V, Int64}) where {L,E,V<:Variable{L, E}}
function MP.substitute(::MP.Subs, v::V, p::Pair{V, Int64}) where {L,E,V<:Variable{L, E}}
v == p[1] ? p[2] : v
end
function MP.monomial_type(::Type{Variable{L,E}}) where {L,E}
EN = new_E(Val(E))
K = calc_K(Val(L),Val(EN))
return PackedMonomial{L,EN,K}
end
function MA.promote_operation(
::typeof(MP.substitute),
::Type{MP.Subs},
::Type{PackedMonomial{L,E,K}},
::Type{Pair{Variable{L,E},T}},
) where {L,E,K,T}
U = MA.promote_operation(^, T, Int)
return Term{U,PackedMonomial{L,E,K}}
end
2 changes: 1 addition & 1 deletion src/poly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ function SparsePoly(p::MPoly, v::IDType)
return poly
end

univariate_to_multivariate(g::SparsePoly{<:AbstractPolynomial}) = univariate_to_multivariate(g, monomialtype(lc(g)))
univariate_to_multivariate(g::SparsePoly{<:AbstractPolynomial}) = univariate_to_multivariate(g, monomial_type(lc(g)))
function univariate_to_multivariate(g, ::Type{<:Monomial})
cfs = coeffs(g)
eps = g.exps
Expand Down
Loading