# Problem computing Jacobian with Symbolics.jl 3.3

I’m using Julia v.1.6.2 and the package Symbolic v.3.3. I have a simple script to compute that Jacobian that gives an error:

``````typeusing Symbolics
using LinearAlgebra

renormI(x)    = x ./ x[[end],:]
homogenize(x) = [x; ones(1,size(x,2))]
cross_product_matrix(v) = [0  -v[3]  v[2];  v[3]  0  -v[1];  -v[2]  v[1]  0]

@variables H[1:3,1:3]
@variables x[1:3]

xp = H*x
J = Symbolics.jacobian(xp,x)

``````

which gives the error

``````MethodError: no method matching similar(::Type{Matrix{Num}}, ::Tuple{UnitRange{Int64}, UnitRange{Int64}})
Closest candidates are:
similar(!Matched::AbstractArray{T, N} where N, ::Tuple) where T at abstractarray.jl:740
similar(::Type{A}, !Matched::Type{T}, !Matched::StaticArrays.Size{S}) where {A<:Array, T, S} at /home/jbpritts/.julia/packages/StaticArrays/vxjOO/src/abstractarray.jl:163
similar(::Type{T}, !Matched::Union{Integer, AbstractUnitRange}...) where T<:AbstractArray at abstractarray.jl:783
...

_array_for(#unused#::Type{Num}, itr::Base.Iterators.ProductIterator{Tuple{Symbolics.Arr{Num, 1}, Symbolics.Arr{Num, 1}}}, #unused#::Base.HasShape{2}) at array.jl:670
jacobian(ops::Symbolics.Arr{Num, 1}, vars::Symbolics.Arr{Num, 1}; simplify::Bool) at diff.jl:356
jacobian(ops::Symbolics.Arr{Num, 1}, vars::Symbolics.Arr{Num, 1}) at diff.jl:356
top-level scope at make_cov_expressions.jl:12
eval at boot.jl:360 [inlined]
eval_in_module(fully_qualified_module_name::Vector{Symbol}, expr::Expr) at JuliaSnail.jl:160
eval_tmpfile(tmpfile::String, modpath::Vector{Symbol}, realfile::String, linenum::Int64) at JuliaSnail.jl:190
top-level scope at none:1
eval at boot.jl:360 [inlined]
eval_in_module(fully_qualified_module_name::Vector{Symbol}, expr::Expr) at JuliaSnail.jl:160
macro expansion at JuliaSnail.jl:698 [inlined]
``````

`collect(x)` first?

Can you explain why this is needed or point to some documentation? I don’t understand what is expected.

Thanks.

https://symbolics.juliasymbolics.org/dev/manual/arrays/#Scalar-indexing-and-scalarization-1

It’s just a quick way to scalarize.

1 Like

Ok, thanks, I thought it would be able to parse the symbolic vector. But now I try to do some computation and compute the Jacobian and again I get a failure. I suspect because v is some expression.

``````using Symbolics
using LinearAlgebra

renormI(x)    = x ./ x[3,:]
homogenize(x) = [x; ones(1,size(x,2))]
cross_product_matrix(v) = [0  -v[3]  v[2];  v[3]  0  -v[1];  -v[2]  v[1]  0]

@variables H[1:3,1:3]
@variables u[1:3]

up = H*u
v = renormI(up)
dx = collect(x)
J = Symbolics.jacobian(v,dx)

``````

Afterward I would like to do common subexpression elimination and then replace the subexpressions and generate code. I’m just starting with Symbolics.jl to see if I can get off of MATLAB

Make an issue for this one. I haven’t run it but I’m busy and don’t want it to get lost. If you make an issue it can get checked and added to the tests.

1 Like