# Silly SymPy question

**URL:** <https://discourse.julialang.org/t/silly-sympy-question/52936>\
**Category:** New to Julia\
**Created:** [January 6, 2021, 1:35pm UTC](https://discourse.julialang.org/t/silly-sympy-question/52936 "2021-01-06T13:35:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [January 6, 2021, 1:35pm UTC](https://discourse.julialang.org/t/silly-sympy-question/52936/1 "2021-01-06T13:35:47Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ranocha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ranocha/32/35588_2.png) [@ranocha](https://discourse.julialang.org/u/ranocha)\
**Post date:** [January 6, 2021, 2:18pm UTC](https://discourse.julialang.org/t/silly-sympy-question/52936/2 "2021-01-06T14:18:46Z")

</div>

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

```julia
julia> using SymPy

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

```

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [January 6, 2021, 7:50pm UTC](https://discourse.julialang.org/t/silly-sympy-question/52936/3 "2021-01-06T19:50:03Z")

</div>

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

---

<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:** [January 7, 2021, 1:28am UTC](https://discourse.julialang.org/t/silly-sympy-question/52936/4 "2021-01-07T01:28:58Z")

</div>

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

```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
julia> sympify("aᵢⱼ")
aᵢⱼ

```

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

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [January 7, 2021, 10:17am UTC](https://discourse.julialang.org/t/silly-sympy-question/52936/5 "2021-01-07T10:17:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [January 7, 2021, 10:51am UTC](https://discourse.julialang.org/t/silly-sympy-question/52936/6 "2021-01-07T10:51:28Z")

</div>

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](https://discourse.julialang.org/t/how-is-a-state-of-subscript-notation-in-julia-1-x/17101/4)

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [January 7, 2021, 11:06am UTC](https://discourse.julialang.org/t/silly-sympy-question/52936/7 "2021-01-07T11:06:58Z")

</div>

A final question for now – SymPy and Latexify…

In the Latexify tutorial, there is the following example:

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

```

which leads to:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/e/de9fa90440772474b0adde2f35723f2b64b6f09d.png)  
or:

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

```julia
# 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](https://global.discourse-cdn.com/julialang/original/3X/5/6/56e759af76239c3ec95b2ca19f3dfc5fb84bc26e.png)  
while `latexify` gives a rather “ugly” result:

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

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [January 8, 2021, 8:34am UTC](https://discourse.julialang.org/t/silly-sympy-question/52936/8 "2021-01-08T08:34:29Z")

</div>

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.
