I’m trying to use this unofficial CasADi interface in Julia since the official interface seems to be a long way from working (couldn’t get it installed in Julia 1.10, as stated in Project.toml). I’m having a hard time handling the fact that CasADi’s main number type, SX, is supposed to behave both as a scalar and as an array, since this leads to vector expressions mixing Array{SX} and SX. As an example, the following code errors:
using CasADi
v = SX("v", 2)
[v[2]; v[1]] + v
because it actually tries to add a Vector{SX} to a SX, thought to be a scalar (since SX <: Real):
ERROR: MethodError: no method matching +(::Vector{SX}, ::SX)
For element-wise addition, use broadcasting with dot syntax: array .+ scalar
The function+exists, but no method is defined for this combination of argument types.Closest candidates are:
+(::Any, ::Any, ::Any, ::Any…)
@ Base operators.jl:642
+(::PyCall.PyObject, ::Any)
@ PyCall ~/.julia/packages/PyCall/1gn3u/src/pyoperators.jl:13
+(::Complex{Bool}, ::Real)
@ Base complex.jl:323
…Stacktrace:
[1] top-level scope
@ REPL
So I wonder:
- can I hijack the
Array{SX}constructor to always return anSX? - how can I make this work with expressions like
scalar_sx * I(3), where the result is aDiagonal <: AbstractMatrix? - can I use
promote_rulefor this? - how could I overload the array building operator
[...]to make it return anSXinstead of anArray?
Currently, I’m handling this through a converter function inserted manually as-needed in my code:
convertSX(x::AbstractArray{SX}) = SX(x)
convertSX(x::AbstractArray) = x
but this is cumbersome, unreliable, and I’m sure there’s a better way of doing this.