One-line explanation: they represent a symbolic array of undetermined, possibly infinite, size.
Suppose you want to work with n symbols, how would you do that? Easy enough if n is a given number, like 10. But it's just n, an unspecified integer number. Formulas like that appear in mathematics all the time: "add or multiply (something) over the indices i=1, ..., n".
For example, suppose I have a function in n-dimensional space Rn, such as f(x) = 1/distance(x, 0). The distance is, of course, the square root of the sum of squares of coordinates. And maybe I want to find some partial derivative of f. How to express all of this in SymPy? Like this:
from sympy import *
x = IndexedBase('x')
j, k, n = symbols('j k n', cls=Idx)
f = 1/sqrt(Sum(x[k]**2, (k, 1, n)))
print(f.diff(x[j]))
This computes the derivative of f with respect to the coordinate x[j]
. The answer is
-Sum(2*KroneckerDelta(j, k)*x[k], (k, 1, n))/(2*Sum(x[k]**2, (k, 1, n))**(3/2))
which is correct (although perhaps the numerator could be simplified if we assume that j is in the range 1..n).
In the above example, x[j]
is the coordinate with index j. In your example, M[i, j]
could be the entry of some matrix at position i, j.
- M is the name of symbolic array, its class is IndexedBase
- i and j are indices of that array, their class is Idx
The above are the classes that you would instantiate yourself. The class of M[i, j] is Indexed but you don't create those objects by using class name, M[i, j]
simply does that.
Two recent questions with examples of working with indexed objects: