1

I'm trying to get sympy to pprint matrices with trig functions with the first letter instead of the shortened term in order to save space. So sin(a_1) will look like sa_1. Right now I'm printing to text then running a find and replace. (I'm new to programming.) So far, this is what does not work:

from sympy import sin as s
from sympy import cos as c

# declaring symbolic variables:
sin, cos, = sym.symbols('s, c')

  #An example Matrix
  T = Matrix([[c(theta), -s(theta), 0, a],
                [s(theta) * c(alpha), c(theta) * c(alpha), -s(alpha), -s(alpha) * d],
                [s(theta) * s(alpha), c(theta) * s(alpha), c(alpha), c(alpha) * d],
                [0, 0, 0, 1]])
#T04 was a sympy symbol matrix solution 
T_000 = str(print_latex(T04))
T_000 = str(T04)
T_000 = T_000.replace('sin', 's')
T_000 = T_000.replace('cos', 'c')
print('T000\n')
pprint(T_000)
T_001 = Matrix([[(-s(theta_1) * s(theta_2) + c(theta_1) * c(theta_2)) * c(theta_3) + (
            -s(theta_1) * c(theta_2) - s(theta_2) * c(theta_1)) * s(theta_3),
                 -(-s(theta_1) * s(theta_2) + c(theta_1) * c(theta_2)) * s(theta_3) + (
                             -s(theta_1) * c(theta_2) - s(theta_2) * c(theta_1)) * c(theta_3), 0,
                 L_1 * c(theta_1) + L_2 * (-s(theta_1) * s(theta_2) + c(theta_1) * c(theta_2))], [
                    (-s(theta_1) * s(theta_2) + c(theta_1) * c(theta_2)) * s(theta_3) + (
                                s(theta_1) * c(theta_2) + s(theta_2) * c(theta_1)) * c(theta_3),
                    (-s(theta_1) * s(theta_2) + c(theta_1) * c(theta_2)) * c(theta_3) - (
                                s(theta_1) * c(theta_2) + s(theta_2) * c(theta_1)) * s(theta_3), 0,
                    L_1 * s(theta_1) + L_2 * (s(theta_1) * c(theta_2)
                                              + s(theta_2) * c(theta_1))], [0, 0, 1, d_4], [0, 0, 0, 1]])
print('\nT001\n')
pprint(T_001)

T_000.replace(sin, s)
print('T000\n', T_000)

It's always printing with the full "sin" and "cos" names.

Manju
  • 15
  • 3

1 Answers1

0

There are two common ways to replace functions in an expression(in this case the expression is the matrix T). One is to use the replace function, the other is to use the subs function. Any of the two can be used to achieve the same goal of replacing sin/cos with other functions.

In the code below we're replacing the function s (which is an alias to sin) with the undefined function s1 (whose symbolic name is s). The same happens with c and c1.

from sympy import *

# s,c act as aliases to sin,cos
s = sin
c = cos

# the intended short-hand functions used to replace sin/cos later on
s1 = Function('s')
c1 = Function('c')

a,d,theta,alpha = symbols('a d \\theta \\alpha')

T = Matrix([
    [c(theta), -s(theta), 0, a],
    [s(theta) * c(alpha), c(theta) * c(alpha), -s(alpha), -s(alpha) * d],
    [s(theta) * s(alpha), c(theta) * s(alpha), c(alpha), c(alpha) * d],
    [0, 0, 0, 1]
])

T_1 = T.replace(s,s1).replace(c,c1)
T_2 = T.subs({s:s1,c:c1})

display(T_1)
display(T_2)

OUTPUT:

enter image description here

The code used in this post is also available here.

wsdookadr
  • 2,584
  • 1
  • 21
  • 44
  • Sorry for the late answer! Thank you! I was substituting incorrectly and not using an extra variable to describe the function. YAY! – Manju Mar 25 '21 at 08:36
  • I also didn't realize you could import everything with *. That's helpful. Thank you! But how did you use the "display" function? I changed to from sympy import * and it's giving me a return error of "'display' not defined – Manju Mar 25 '21 at 10:14
  • @Manju the `display` function was available because I ran all of this in a [Jupyter](https://jupyter.org/) notebook. Jupyter can be advantageous to iterate fast when using SymPy. About the call to `display`, it can also be replaced with `print`. There's a more detailed discussion of [printing in SymPy here](https://docs.sympy.org/latest/tutorial/printing.html). – wsdookadr Mar 25 '21 at 11:14
  • Ahh, ok, I'm using pycharm. Thank you!!! **Just as a note for those who may do this later: substituting the function also removes the solving capability, for example sin(x) will no longer solve for sin(x) if x is substituted for, for example pi, later. So for printing purposes only, substitute the s1 function in a separate "for printing only" matrix. – Manju Apr 08 '21 at 11:06