Skip to content
Merged
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
7 changes: 7 additions & 0 deletions src/variable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,16 @@ Returns whether variables of the given type are commutative, i.e., whether
`x * y == y * x`.
"""
function is_commutative end
function is_commutative(
::Type{P},
) where {P<:Union{AbstractTerm,AbstractPolynomial}}
return is_commutative(monomial_type(P))
end
is_commutative(p::_APL) = is_commutative(typeof(p))
is_commutative(v::AbstractVector) = is_commutative(eltype(v))
is_commutative(::Type{V}) where {V<:AbstractVector} = is_commutative(eltype(V))
is_commutative(v::Tuple) = all(is_commutative, v)
is_commutative(::Type{T}) where {T<:Tuple} = all(is_commutative, fieldtypes(T))

"""
name(v::AbstractVariable)::AbstractString
Expand Down
12 changes: 12 additions & 0 deletions test/commutative/variable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ import MultivariatePolynomials:
AbstractVariable, similar_variable, @similar_variable

@testset "Variable" begin
@testset "is_commutative" begin
function _is_comm_test(p)
@test MP.is_commutative(p)
@test MP.is_commutative(typeof(p))
end
Mod.@polyvar x y[1:2]
_is_comm_test(x)
_is_comm_test(y)
_is_comm_test(x^2)
_is_comm_test(2x^2)
_is_comm_test(sum(y))
end
@testset "polyvar macro index set" begin
Mod.@polyvar x y z
Mod.@polyvar x[1:3] y z[1:2]
Expand Down
12 changes: 12 additions & 0 deletions test/noncommutative/monomial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,15 @@ end
@test collect(exponents(X1[i])) == collect(exponents(X2[i])) == Z[i]
end
end
@testset "is_commutative" begin
function _is_comm_test(p)
@test !MP.is_commutative(p)
@test !MP.is_commutative(typeof(p))
end
Mod.@ncpolyvar x y[1:2]
_is_comm_test(x)
_is_comm_test(y)
_is_comm_test(x^2)
_is_comm_test(2x^2)
_is_comm_test(sum(y))
end
Loading