Skip to content

Commit 72842fe

Browse files
committed
Make variables return a sorted list
1 parent b24ddde commit 72842fe

File tree

3 files changed

+8
-10
lines changed

3 files changed

+8
-10
lines changed

docs/src/submodules/Nonlinear/SymbolicAD.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ existing function.
153153

154154
## `variables`
155155

156-
Use [`Nonlinear.SymbolicAD.variables`](@ref) to return a list of the variables
157-
that appear in the function:
156+
Use [`Nonlinear.SymbolicAD.variables`](@ref) to return a sorted list of the
157+
variables that appear in the function:
158158

159159
```jldoctest
160160
julia> x = MOI.VariableIndex.(1:3)
@@ -168,13 +168,10 @@ atan(MOI.VariableIndex(3), 0.0 + 2.0 MOI.VariableIndex(1))
168168
169169
julia> MOI.Nonlinear.SymbolicAD.variables(f)
170170
2-element Vector{MathOptInterface.VariableIndex}:
171-
MOI.VariableIndex(3)
172171
MOI.VariableIndex(1)
172+
MOI.VariableIndex(3)
173173
```
174174

175-
Note that this list is not sorted, but it is guaranteed to contain only unique
176-
items.
177-
178175
## `derivative`
179176

180177
Use [`Nonlinear.SymbolicAD.derivative`](@ref) to compute the symbolic derivative

src/Nonlinear/SymbolicAD/SymbolicAD.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,12 +310,12 @@ end
310310
"""
311311
variables(f::Union{Real,MOI.AbstractScalarFunction})
312312
313-
Return a list of the `MOI.VariableIndex` present in the function `f`.
313+
Return a sorted list of the `MOI.VariableIndex` present in the function `f`.
314314
"""
315315
function variables(f::MOI.AbstractScalarFunction)
316316
ret = MOI.VariableIndex[]
317317
_variables(ret, f)
318-
return ret
318+
return sort!(ret; by = x -> x.value)
319319
end
320320

321321
variables(::Real) = MOI.VariableIndex[]

test/Nonlinear/SymbolicAD.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,9 @@ function test_variable()
163163
)=>[x, y],
164164
1.0*x*x=>[x],
165165
1.0*x*x+x=>[x],
166-
1.0*x*x+y=>[y, x],
166+
1.0*x*x+y=>[x, y],
167167
1.0*x*y=>[x, y],
168-
1.0*y*x=>[y, x],
168+
1.0*y*x=>[x, y],
169169
# ::NonlinearExpr
170170
MOI.ScalarNonlinearFunction(:sin, Any[x])=>[x],
171171
MOI.ScalarNonlinearFunction(:sin, Any[1.0*x+y])=>[x, y],
@@ -704,6 +704,7 @@ function test_gradient_and_hessian()
704704
sin_x = op(:sin, x[1])
705705
for (f, ret) in Any[
706706
(2.0*x[1]*x[2])=>(x, Any[2.0*x[2], 2.0*x[1]], [(1, 2)], [2.0]),
707+
(2.0*x[2]*x[1])=>(x, Any[2.0*x[2], 2.0*x[1]], [(1, 2)], [2.0]),
707708
op(:*, x[1], x[2])=>(x, Any[x[2], x[1]], [(1, 2)], [true]),
708709
sin_x=>([x[1]], Any[op(:cos, x[1])], [(1, 1)], Any[op(:-, sin_x)]),
709710
]

0 commit comments

Comments
 (0)