How to create an equation with a symbolic matrix using SymPy.jl?

Hello, good morning.

How to build an equation using a symbolic matrix with SymPy.jl?
Using python, I would just call sympy.Matrix class. In julia, we have Matrix in SymPy package, but it seems not create a symbol, and when a try to create an equation I got an error.

import sympy as sp
G = sp.Matrix(matrix)

julia> expression = g*v ~ i
ERROR: PyError (ccall(#= C:\Users\jcbri\.julia\packages\PyCall\1gn3u\src\conversions.jl:43 =# @pysym(:PyLong_AsLongLongAndOverflow), Clonglong, (PyPtr, Ref{Cint}), po, overflow)) <class 'TypeError'>
TypeError("'Symbol' object cannot be interpreted as an integer")

Stacktrace:
  [1] pyerr_check
    @ C:\Users\jcbri\.julia\packages\PyCall\1gn3u\src\exception.jl:75 [inlined]
  [2] convert(::Type{Int64}, po::PyCall.PyObject)
    @ PyCall C:\Users\jcbri\.julia\packages\PyCall\1gn3u\src\conversions.jl:43
  [3] py2array(T::Type, A::Matrix{Int64}, o::PyCall.PyObject, dim::Int64, i::Int64)
    @ PyCall C:\Users\jcbri\.julia\packages\PyCall\1gn3u\src\conversions.jl:350
  [4] py2array(T::Type, A::Matrix{Int64}, o::PyCall.PyObject, dim::Int64, i::Int64)
    @ PyCall C:\Users\jcbri\.julia\packages\PyCall\1gn3u\src\conversions.jl:361
  [5] py2array(T::Type, o::PyCall.PyObject)
    @ PyCall C:\Users\jcbri\.julia\packages\PyCall\1gn3u\src\conversions.jl:407
  [6] convert(::Type{Matrix{Int64}}, o::PyCall.PyObject)
    @ PyCall C:\Users\jcbri\.julia\packages\PyCall\1gn3u\src\pyarray.jl:287
  [7] Sym
    @ C:\Users\jcbri\.julia\packages\SymPyCore\Dg45i\src\types.jl:30 [inlined]
  [8] *(x::Sym{Matrix{Int64}}, y::Sym{PyCall.PyObject})
    @ SymPyCore C:\Users\jcbri\.julia\packages\SymPyCore\Dg45i\src\mathops.jl:16
  [9] _broadcast_getindex_evalf
    @ .\broadcast.jl:678 [inlined]
 [10] _broadcast_getindex
    @ .\broadcast.jl:651 [inlined]
 [11] getindex
    @ .\broadcast.jl:610 [inlined]
 [12] copy
    @ .\broadcast.jl:911 [inlined]
 [13] materialize
    @ .\broadcast.jl:872 [inlined]
 [14] broadcast_preserving_zero_d
    @ .\broadcast.jl:861 [inlined]
 [15] *(A::Sym{Matrix{Int64}}, B::Vector{Sym})
    @ Base .\arraymath.jl:21
 [16] top-level scope
    @ REPL[22]:1

caused by: PyError (ccall(#= C:\Users\jcbri\.julia\packages\PyCall\1gn3u\src\conversions.jl:43 =# @pysym(:PyLong_AsLongLongAndOverflow), Clonglong, (PyPtr, Ref{Cint}), po, overflow)) <class 'TypeError'>
TypeError("'Symbol' object cannot be interpreted as an integer")

There are a few ways, though maybe you have suggestions to make this better. Here is one:

m = Sym[2 4 1; 1 1 3; 1 3 2]
@syms x[1:3]
i = Sym[1,2,3]
eqs = m * x .~ i
solve(eqs, x)

You might get a deprecation error if you don’t pass eqs as a tuple.

To get a matrix on the python side, this might be of use

mm = sympy.ImmutableMatrix(m)
ii = sympy.ImmutableMatrix(i)
mm.solve(ii)

This answer will be a symbolic vector on the python side, not a vector of symbolic objects on the Julia side.

1 Like

Hi, thats is exactly what I was searching. Thanks very much, it will help a lot to solve electric circuits.