Skip to content

Commit

Permalink
fixup! feat: support array variables in linear_expansion
Browse files Browse the repository at this point in the history
  • Loading branch information
AayushSabharwal committed Oct 23, 2024
1 parent 2164cef commit a1bdba1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/linear_algebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -338,13 +338,34 @@ function _linear_expansion(t, 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
3 changes: 3 additions & 0 deletions test/linear_solver.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,7 @@ a, b, islinear = Symbolics.linear_expansion(D(x) - x, x)
@test islin && isequal(a, 2) && isequal(b, 0)
a, b, islin = linear_expansion(y[1], x[1])
@test islin && isequal(a, 0) && isequal(b, y[1])
@test !linear_expansion(z([x...]), x[1])[3]
@test !linear_expansion(z(collect(unwrap(x))), x[1])[3]
@test !linear_expansion(z([x, 2x]), x[1])[3]
end

0 comments on commit a1bdba1

Please sign in to comment.