Skip to content

Commit

Permalink
Merge pull request #14 from JuliaAlgebra/bl/convintmono
Browse files Browse the repository at this point in the history
Add conversion from Int to Monomial
  • Loading branch information
blegat authored Jun 12, 2018
2 parents f1f114c + a76150c commit f583ba5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/mono.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ Monomial{C}() where C = Monomial{C}(PolyVar{C}[], Int[])
Monomial(vars::TupOrVec{PolyVar{C}}, z::Vector{Int}) where C = Monomial{C}(vars, z)
Monomial{C}(x::PolyVar{C}) where C = Monomial{C}([x], [1])
Monomial(x::PolyVar{C}) where C = Monomial{C}(x)
function Monomial{C}(α) where C
α == 1 || error("Cannot convert to a Monomial{$C} as it is not one")
Monomial{C}(PolyVar{C}[], Int[])
end
# defaults to commutative so that `Monomial(1)` is consistent with TypedPolynomials
Monomial(α) = Monomial{true}(α)

Base.copy(m::M) where {M<:Monomial} = M(m.vars, copy(m.z))

Expand Down
9 changes: 9 additions & 0 deletions test/mono.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@
@test zeroterm(Monomial{false}) == 0
@test zero(Monomial{false}) == 0
@test one(Monomial{true}) == 1
@test_throws ErrorException Monomial(2)
@test (@inferred Monomial(1)) isa Monomial{true}
@test Monomial(1) == 1
@test_throws ErrorException Monomial{true}(2)
@test (@inferred Monomial{true}(1)) isa Monomial{true}
@test Monomial{true}(1) == 1
@test_throws ErrorException Monomial{false}(2)
@test (@inferred Monomial{false}(1)) isa Monomial{false}
@test Monomial{false}(1) == 1
@polyvar x
@test_throws ArgumentError Monomial{true}([x], [1,0])
@test zeroterm(x^2) isa Term{true, Int}
Expand Down

0 comments on commit f583ba5

Please sign in to comment.