Say I have defined the following expression:
from sympy import *
N, D, i, j, d = symbols("N D i j d", integer=True)
beta, gamma = symbols(r'\beta \gamma')
X = IndexedBase("X", shape=(N, D))
# r(i, j) = euclidian distance between X[i] and X[j]
r = lambda i, j: sqrt(Sum((X[i, d] - X[j, d])**2, (d, 1, D)))
expr = r(i, j)**2 + r(i, j)
The expr
variable now displays like this:
While this is fine for this minimal example, it gets quite messy in larger expressions. This really hinders my ability to see what happens later on when I compute sums over all r(i,j)
, derivatives etc.
My question: Is there a way to tell SymPy about r(i, j)
, such that it can be displayed as something like this:
while still behaving as before in subsequent expressions?
I know I could make r
a Function
, which would display as wanted, but then it would not work properly in a subsequent calculation (e.g. derivatives would be abstract and not evaluated).
Any help would be much appreciated!