python-3.xsympysymbolstrigonometrypprint

Get sympy to pprint sin(a_n) with s(a_n)


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.


Solution

  • 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.