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

**URL:** https://discourse.julialang.org/t/how-to-create-an-equation-with-a-symbolic-matrix-using-sympy-jl/125937
**Category:** General Usage
**Tags:** question, sympy
**Created:** [February 15, 2025, 11:39am UTC](https://discourse.julialang.org/t/how-to-create-an-equation-with-a-symbolic-matrix-using-sympy-jl/125937 "2025-02-15T11:39:30Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)
#### Post date: [February 15, 2025, 11:39am UTC](https://discourse.julialang.org/t/how-to-create-an-equation-with-a-symbolic-matrix-using-sympy-jl/125937/1 "2025-02-15T11:39:30Z")

</div>

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.

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

```

```julia

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

```

---

<div class="post-metadata">

### Author: ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)
#### Post date: [February 15, 2025, 3:32pm UTC](https://discourse.julialang.org/t/how-to-create-an-equation-with-a-symbolic-matrix-using-sympy-jl/125937/2 "2025-02-15T15:32:48Z")

</div>

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

```julia
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

```julia
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.

---

<div class="post-metadata">

### Author: ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)
#### Post date: [February 15, 2025, 7:07pm UTC](https://discourse.julialang.org/t/how-to-create-an-equation-with-a-symbolic-matrix-using-sympy-jl/125937/3 "2025-02-15T19:07:06Z")

</div>

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