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

Simple implementation in test #94

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
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ const MP = MultivariatePolynomials

include("utils.jl")

include("simple.jl")
Mod = SimplePolynomials
include("commutativetests.jl")

# Taken from JuMP/test/solvers.jl
function try_import(name::Symbol)
try
Expand Down
55 changes: 55 additions & 0 deletions test/simple.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
module SimplePolynomials

using MultivariatePolynomials
const MP = MultivariatePolynomials

# Taken from DynamicPolynomials/src/var.jl
function polyvecvar(prefix, idxset)
[Variable("$(prefix * string(i))") for i in idxset]
end

function buildpolyvar(var)
if isa(var, Symbol)
:($(esc(var)) = Variable($"$var"))
else
isa(var, Expr) || error("Expected $var to be a variable name")
Base.Meta.isexpr(var, :ref) || error("Expected $var to be of the form varname[idxset]")
length(var.args) == 2 || error("Expected $var to have one index set")
varname = var.args[1]
prefix = string(var.args[1])
idxset = esc(var.args[2])
:($(esc(varname)) = polyvecvar($prefix, $idxset))
end
end

macro polyvar(args...)
reduce((x,y) -> :($x; $y), :(), buildpolyvar.(args))
end


struct Variable <: AbstractVariable
name::String
end
MP.name(v::Variable) = v.name
Base.:(==)(v1::Variable, v2::Variable) = v1.name == v2.name
struct Monomial <: AbstractMonomial
vars::Vector{Variable}
exps::Vector{Int}
end
const MonomialLike = Union{Variable, Monomial}
MP.variables(m::Monomial) = m.vars
MP.exponents(m::Monomial) = m.exps
MP.monomial(v::Variable) = Monomial([v], [1])
MP.termtype(::Union{MonomialLike, Type{<:MonomialLike}}) = Term{Int}
struct Term{T} <: AbstractTerm{T}
α::T
m::Monomial
end
MP.coefficient(t::Term) = t.α
MP.monomial(t::Term) = t.m
struct Polynomial{T} <: AbstractPolynomial{T}
terms::Vector{Term{T}}
end
MP.terms(p::Polynomial) = p.terms

end