Skip to content

Commit 5498282

Browse files
committed
Add var_indices for mpolys
1 parent 6257b04 commit 5498282

File tree

5 files changed

+31
-13
lines changed

5 files changed

+31
-13
lines changed

docs/src/mpolynomial.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,10 @@ The following functionality is also provided for all multivariate polynomials.
321321
is_univariate(::MPolyRing{T}) where T <: RingElement
322322
```
323323

324+
```@docs
325+
var_indices(p::MPolyRingElem{T}) where T <: RingElement
326+
```
327+
324328
```@docs
325329
vars(p::MPolyRingElem{T}) where T <: RingElement
326330
```

src/MPoly.jl

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,19 +89,27 @@ Return the number of variables in `R`.
8989
"""
9090
number_of_generators(R::MPolyRing) = number_of_variables(R)
9191

92+
@doc raw"""
93+
var_indices(p::MPolyRingElem{T}) where {T <: RingElement}
94+
95+
Return the indices of the variables actually occurring in $p$.
96+
"""
97+
function var_indices(p::MPolyRingElem{T}) where {T <: RingElement}
98+
isused = zeros(Int, nvars(parent(p)))
99+
for v in exponent_vectors(p)
100+
isused .|= v # accumulate by OR-ing the exponent vectors
101+
end
102+
return findall(!iszero, isused)
103+
end
104+
92105
@doc raw"""
93106
vars(p::MPolyRingElem{T}) where {T <: RingElement}
94107
95108
Return the variables actually occurring in $p$.
96109
"""
97110
function vars(p::MPolyRingElem{T}) where {T <: RingElement}
98111
R = parent(p)
99-
n = nvars(R)
100-
isused = zeros(Int, n)
101-
for v in exponent_vectors(p)
102-
isused .|= v # accumulate by OR-ing the exponent vectors
103-
end
104-
return [R[i] for i in 1:n if isused[i] != 0]
112+
return [gen(R, i) for i in var_indices(p)]
105113
end
106114

107115
@doc raw"""

src/exports.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,7 @@ export use_karamul
598598
export valuation
599599
export var
600600
export var_index
601+
export var_indices
601602
export vars
602603
export vector_space
603604
export vector_space_dim

src/generic/MPoly.jl

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,17 @@ function gen(a::MPolyRing{T}, i::Int) where {T <: RingElement}
7777
return a([one(base_ring(a))], exps)
7878
end
7979

80-
function vars(p::MPoly{T}) where {T <: RingElement}
81-
vars_in_p = Vector{MPoly{T}}(undef, 0)
82-
n = nvars(p.parent)
80+
function var_indices(p::MPoly)
81+
vars_in_p = Int[]
82+
n = nvars(parent(p))
8383
exps = p.exps
84-
gen_list = gens(p.parent)
8584
for j = 1:n
8685
for i = 1:length(p)
8786
if exps[j, i] > 0
8887
if p.parent.ord == :degrevlex
89-
push!(vars_in_p, gen_list[j])
88+
push!(vars_in_p, j)
9089
else
91-
push!(vars_in_p, gen_list[n - j + 1])
90+
push!(vars_in_p, n - j + 1)
9291
end
9392
break
9493
end
@@ -97,7 +96,12 @@ function vars(p::MPoly{T}) where {T <: RingElement}
9796
if p.parent.ord != :degrevlex
9897
vars_in_p = reverse(vars_in_p)
9998
end
100-
return(vars_in_p)
99+
return vars_in_p
100+
end
101+
102+
function vars(p::MPoly{T}) where {T <: RingElement}
103+
R = parent(p)
104+
return MPoly{T}[gen(R, i) for i in var_indices(p)]
101105
end
102106

103107
@doc raw"""

src/generic/imports.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ import ..AbstractAlgebra: use_karamul
230230
import ..AbstractAlgebra: valuation
231231
import ..AbstractAlgebra: var
232232
import ..AbstractAlgebra: var_index
233+
import ..AbstractAlgebra: var_indices
233234
import ..AbstractAlgebra: vars
234235
import ..AbstractAlgebra: vector_space_dim
235236
import ..AbstractAlgebra: zero!

0 commit comments

Comments
 (0)