Skip to content

Commit d28bab9

Browse files
committed
Add effective_variables
1 parent c23eac6 commit d28bab9

File tree

6 files changed

+52
-2
lines changed

6 files changed

+52
-2
lines changed

src/polynomial.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export polynomial, polynomial!, polynomialtype, terms, nterms, coefficients, monomials
22
export coefficienttype, monomialtype
3-
export mindegree, maxdegree, extdegree
3+
export mindegree, maxdegree, extdegree, effective_variables
44
export leadingterm, leadingcoefficient, leadingmonomial
55
export removeleadingterm, removemonomials, monic
66

@@ -258,6 +258,11 @@ function extdegree(p::Union{AbstractPolynomialLike, AbstractVector{<:AbstractTer
258258
(mindegree(p, args...), maxdegree(p, args...))
259259
end
260260

261+
function effective_variables(p::AbstractPolynomialLike, args...)
262+
VT = variable_union_type(p)
263+
return VT[v for v in variables(p) if !iszero(maxdegree(p, v))]
264+
end
265+
261266
"""
262267
leadingterm(p::AbstractPolynomialLike)
263268

src/variable.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export name, name_base_indices, similarvariable, @similarvariable
1+
export name, name_base_indices, similarvariable, @similarvariable, variable_union_type
22

33
Base.copy(x::AbstractVariable) = x
44

test/monomial.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,19 @@ const MP = MultivariatePolynomials
5151
@test adjoint(x) == x
5252
@test transpose(x^2) == x^2
5353
@test adjoint(x^2) == x^2
54+
55+
@testset "Effective variables" begin
56+
T = variable_union_type(x)
57+
@test x isa T
58+
@test y[2] isa T
59+
@test T[x, y[2]] == @inferred effective_variables(x * y[2])
60+
@test T[x, y[2]] == @inferred effective_variables(y[2] * x)
61+
@test T[x] == @inferred effective_variables(x * y[2]^0)
62+
@test T[x] == @inferred effective_variables(y[2]^0 * x)
63+
@test T[y[2]] == @inferred effective_variables(x^0 * y[2])
64+
@test T[y[2]] == @inferred effective_variables(y[2] * x^0)
65+
@test T[x, y[2]] == @inferred effective_variables(y[3]^0 * x * y[2])
66+
@test T[x, y[2]] == @inferred effective_variables(y[2] * y[3]^0 * x)
67+
@test T[x, y[2]] == @inferred effective_variables(y[2] * x * y[3]^0)
68+
end
5469
end

test/polynomial.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,16 @@ const MP = MultivariatePolynomials
154154
@test variables(v)[1] == x
155155
@test variables(v)[2] == y
156156
end
157+
158+
@testset "Effective variables" begin
159+
Mod.@polyvar x y z
160+
T = variable_union_type(x)
161+
@test x isa T
162+
@test y isa T
163+
@test z isa T
164+
@test T[x] == @inferred effective_variables(x + y - y)
165+
@test T[x, y] == @inferred effective_variables(z + x + y - z)
166+
@test T[y, z] == @inferred effective_variables(z + 0 * x + y)
167+
@test T[z] == @inferred effective_variables(z + 0 * x + y^0)
168+
end
157169
end

test/term.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,17 @@
4848

4949
@test_throws InexactError push!([1], 2x)
5050
@test_throws ErrorException push!([x^2], 2x)
51+
52+
53+
@testset "Effective variables" begin
54+
T = variable_union_type(x)
55+
@test x isa T
56+
@test y isa T
57+
@test T[x, y] == @inferred effective_variables(x * y)
58+
@test T[x, y] == @inferred effective_variables(y * x)
59+
@test T[x] == @inferred effective_variables(x * y^0)
60+
@test T[x] == @inferred effective_variables(y^0 * x)
61+
@test T[y] == @inferred effective_variables(x^0 * y)
62+
@test T[y] == @inferred effective_variables(y * x^0)
63+
end
5164
end

test/variable.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ import MultivariatePolynomials: AbstractVariable, similarvariable, @similarvaria
4949
end
5050
end
5151
end
52+
53+
@testset "Effective variables" begin
54+
@test [x] == @inferred effective_variables(x)
55+
@test [y] == @inferred effective_variables(y)
56+
end
5257
end
5358
@testset "Create similar variable" begin
5459
Mod.@polyvar x y

0 commit comments

Comments
 (0)