Silly SymPy question

I want to convert some symbolic computations from Maple to Julia/SymPy, specifically regarding the Park transformation.

Question: how do I use coefficients like sqrt(3/2) and make sure that these are preserved as exact numbers, instead of being converted to a Floting Point number?

You can use Sym to convert coefficients to symbolic expressions in SymPy:

julia> using SymPy

julia> sqrt(Sym(3//2))
√6
──
2 
4 Likes

Beautiful… I got the same results as in Maple, and with a simpler set-up :-).

Questions on subscripts with Julia/SymPy:
A final question… I’d like to use subscripts with variable names, if possible. I am able to produce \theta_a by \theta followed by TAB followed by \_a and then TAB.

But if I try with subscripts b and c, \_b doesn’t produce b as subscript, but instead expands to \_beta and then produces \theta_\beta. Likewise, \_c expands to \_chi and then produces \theta_\chi.

Is it possible to have multiple symbols in subscript? I’d like to have something like R_\mathrm{dq0}… or should I instead use Rdq0 or R_dq0?

This is more of a Julia question than SymPy, as the unicode subscripts are limited to what they are (on my version of Julia):

\_(     \_-      \_2      \_5      \_8      \_a      \_e      \_i      \_l      \_o      \_r      \_schwa  \_v
\_)     \_0      \_3      \_6      \_9      \_beta   \_gamma  \_j      \_m      \_p      \_rho    \_t      \_x
\_+     \_1      \_4      \_7      \_=      \_chi    \_h      \_k      \_n      \_phi    \_s      \_u

You can certainly create symbolic variables with these unicode strings:

julia> sympify("aᵢⱼ")
aᵢⱼ

(Where the part in the string is entered a\_i[tab]\_j[tab].)

4 Likes

Thanks. Yes, I realize it is a Julia question.

For electric machines, the phases are normally referred to as a, b, c, so I would need to use subscripts with a, b and c to make it elegant – as in \theta_a, \theta_b, \theta_c. Anyway, I can live with the less elegant \theta a, \theta b, \theta c.

It actually isn’t a Julia question, but a question of what Unicode supports, see e.g. How is a state of subscript notation in Julia 1.x? - #4 by mbauman

1 Like

A final question for now – SymPy and Latexify…

In the Latexify tutorial, there is the following example:

m = [2//3 "e^(-c*t)" 1+3im; :(x/(x+k_1)) "gamma(n)" :(log10(x))]
latexify(m)

which leads to:
image
or:

julia> show(latexify(m))
L"\begin{equation}
\left[
\begin{array}{ccc}
\frac{2}{3} & e^{\left(  - c \right) \cdot t} & 1+3\textit{i} \\
\frac{x}{x + k_{1}} & \Gamma\left( n \right) & \log_{10}\left( x \right) \\
\end{array}
\right]
\end{equation}
"

This looks good. HOWEVER, when I apply latexify to a SymPy result, I get the following, e.g.:

# Symmetric 3-phase
θb = θa - 2PI/3
θc = θb - 2PI/3
# Park-Clark transformation matrix
P = [cos(θa) sin(θa) 1/sqrt(Sym(2));
    cos(θb) sin(θb) 1/sqrt(Sym(2));
    cos(θc) sin(θc) 1/sqrt(Sym(2))]*sqrt(Sym(2//3))

leading to:
image
while latexify gives a rather “ugly” result:

julia> show(latexify(Ldq0)
L"\begin{equation}
\left[
\begin{array}{ccc}
sqrt(6)*cos(θa)/3 & sqrt(6)*sin(θa)/3 & sqrt(3)/3 \\
-sqrt(6)*cos(θa + pi/3)/3 & -sqrt(6)*sin(θa + pi/3)/3 & sqrt(3)/3 \\
-sqrt(6)*sin(θa + pi/6)/3 & sqrt(6)*cos(θa + pi/6)/3 & sqrt(3)/3 \\
\end{array}
\right]
\end{equation}
"

The "ugliness* lies in:

  • Use of * for multiplication instead of \cdot
  • Use of simple parentheses (, ) instead of expanding parentheses \left( and \right)
  • use of / instead of \frac{}{}

Questions:

  • Is this due to a “SymPy recipe”?
  • Is there a way to modify choice of multiplication symbol and parentheses type?

Aha. OK, I’ll have to wait for an update of Unicode.

I did find that choosing label names like R_a etc. in SymPy expressions actually is shown as R_a in the Out-cell of Jupyter notebooks, so this partially solves my problem. Currently my word processor choice (LyX) doesn’t support Unicode in program listing anyway — I’ll experiment more on that in the next version of the word processor.