Get left hand side of SymPy equations

I have a vector of equations:

using SymPy
θ,ω,dθ,dω,b,c = symbols("θ,ω,\\dot{θ},\\dot{ω},b,c")
f1 = Eq(dθ,ω)
f2 = Eq(dω,-b*ω - c* sin(θ))
eqns = [f1;f2]

out:

\left[ \begin{array}{c} \dot{θ} = ω \\ \dot{ω} = -b*ω - c*sin(θ) \\ \end{array} \right]

I want to return only the first side of the equality.
The code I use for this is as follows:

sys_vec = function (sys,col)
vec = zeros(Sym, (size(sys, 1),1)) #vetor variaveis
    for i in 1:size(sys, 1)
        vec[i,1] = [var for var in sys[i].args][col]
    end
    return vec
end

sys_vec(eqns,1)

out:

\left[ \begin{array}{c} \dot{θ} \\ \dot{ω} \\ \end{array} \right]

Would there be a way to simplify it? Without using the loop directly?

Did you try:

[eqns[1].lhs; eqns[2].lhs]

or better:
[x.lhs for x in eqns]
2 Likes

@rafael.guerra has exactly the right answer in general, as this just calls the always available SymPy method. In this case, there are also exports for rhs and lhs (SymPy.jl/mathfuns.jl at master · JuliaPy/SymPy.jl · GitHub) that replicate the same, so lhs.(eqns) should work as desired.

2 Likes

Thank you very much @rafael.guerra .
I’m learning the Julia language, and I think the interaction between Julia and Sympy is confusing me a little.

Yes, it can be confusing. Basically, (using “sympy” for the Python library, and “SymPy” for the Julia package):

  • For as many generic functions from Julia as possible, methods are defined for symbolic values and called as fn(obj, ...)
  • sympy methods and properties for SymPy objects are called as in sympy, through obj.fn(...) or obj.prop
  • Some, not so many, sympy methods and properties, such as lhs and rhs, solve,…, have Julia counterparts and are called as fn(obj,...)
  • Constructors and other calls from sympy may be called from the exported sympy object as sympy.Constructor(...), conversions from Julia objects follow rules set up within PyCall.
2 Likes

Very enlightening.