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

jcbritobr
Sat, 15 Feb 2025 11:39:30 +0000
General Usage

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")

3 posts - 2 participants