Question about __str()__ for custom class using sympy expressions

29 views
Skip to first unread message

Alan Bromborsky

unread,
Feb 23, 2025, 1:09:27 PM2/23/25
to sympy
I am coding a str() function for a custom object.  The object is a liner
differential operator that consists of a linear combination of sympy
expressions and partial derivatives (defined in a class I have defined
and have their own str() function).  The problem is to know when to
represent a term in the linear differential operator str() with

str(sympy expression)+str(partial derivative)

or

'('+str(sympy expression)+')'+str(partial derivative)

that is does str(sympy expression) have low level instances of '+' or
'-' in it as opposed to '+' or '-' being used in functions within the
expression such as sin(x+y) or exp(-x)?

Any suggestions would be appreciated before I resort to using grep on 
str(sympy expression).

Sangyub Lee

unread,
Feb 23, 2025, 1:25:12 PM2/23/25
to sympy

I think that you should take a look at the StrPrinter interface and consider implementing a custom StrPrinter. You may need to implement a method like _print_LinearDifferentialOperator.

While simple string concatenation or interpolation might work for basic expressions, these approaches have significant limitations when dealing with expressions that require proper handling of operator precedence and parenthesizing.

Reply all
Reply to author
Forward
0 new messages