Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support array variables in linear_expansion #1320

Closed
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
39 changes: 39 additions & 0 deletions src/linear_algebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,13 @@ function _linear_expansion(t, x)
op, args = operation(t), arguments(t)
expansion_check(op)

if iscall(x) && operation(x) == getindex
arrx, idxsx... = arguments(x)
else
arrx = nothing
idxsx = nothing
end

if op === (+)
a₁ = b₁ = 0
islinear = true
Expand Down Expand Up @@ -318,15 +325,47 @@ function _linear_expansion(t, x)
a₁, b₁, islinear = linear_expansion(args[1], x)
# (a₁ x + b₁)/b₂
return islinear ? (a₁ / b₂, b₁ / b₂, islinear) : (0, 0, false)
elseif op === getindex
arrt, idxst... = arguments(t)
isequal(arrt, arrx) && return (0, t, true)

indexed_t = Symbolics.scalarize(arrt)[idxst...]
# when indexing a registered function/callable symbolic
# scalarizing and indexing leads to the same symbolic variable
# which causes a StackOverflowError without this
isequal(t, indexed_t) && return (0, t, true)
return linear_expansion(Symbolics.scalarize(arrt)[idxst...], x)
else
for (i, arg) in enumerate(args)
isequal(arg, arrx) && return (0, 0, false)
if symbolic_type(arg) == NotSymbolic()
arg isa AbstractArray || continue
_occursin_array(x, arrx, arg) && return (0, 0, false)
continue
end
a, b, islinear = linear_expansion(arg, x)
(_iszero(a) && islinear) || return (0, 0, false)
end
return (0, t, true)
end
end

"""
_occursin_array(sym, arrsym, arr)

Check if `sym` (or, if `sym` is an element of an array symbolic, the array symbolic
`arrsym`) occursin in the non-symbolic array `arr`.
"""
function _occursin_array(sym, arrsym, arr)
for el in arr
if symbolic_type(el) == NotSymbolic()
return el isa AbstractArray && _occursin_array(sym, arrsym, el)
else
return occursin(sym, el) || occursin(arrsym, el)
end
end
end

###
### Utilities
###
Expand Down
15 changes: 15 additions & 0 deletions test/linear_solver.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,18 @@ a, b, islinear = Symbolics.linear_expansion(D(x) - x, x)
@test islinear
@test isequal(a, -1)
@test isequal(b, D(x))

@testset "linear_expansion with array variables" begin
@variables x[1:2] y[1:2] z(..)
@test !Symbolics.linear_expansion(z(x) + x[1], x[1])[3]
@test !Symbolics.linear_expansion(z(x[1]) + x[1], x[1])[3]
a, b, islin = Symbolics.linear_expansion(z(x[2]) + x[1], x[1])
@test islin && isequal(a, 1) && isequal(b, z(x[2]))
a, b, islin = Symbolics.linear_expansion((x + x)[1], x[1])
@test islin && isequal(a, 2) && isequal(b, 0)
a, b, islin = Symbolics.linear_expansion(y[1], x[1])
@test islin && isequal(a, 0) && isequal(b, y[1])
@test !Symbolics.linear_expansion(z([x...]), x[1])[3]
@test !Symbolics.linear_expansion(z(collect(Symbolics.unwrap(x))), x[1])[3]
@test !Symbolics.linear_expansion(z([x, 2x]), x[1])[3]
end
Loading