Module with functions operating on IndexedBase, Indexed and Idx objects
etc.
Methods in this module could be implemented by calling methods on Expr objects instead. When things stabilize this could be a useful refactoring.
Determine dummy indices of expr and describe it’s structure
By dummy we mean indices that are summation indices.
The stucture of the expression is determined and described as follows:
A conforming summation of Indexed objects is described with a dict where the keys are summation indices and the corresponding values are sets containing all terms for which the summation applies. All Add objects in the SymPy expression tree are described like this.
For all nodes in the SymPy expression tree that are not of type Add, the following applies:
If a node discovers contractions in one of it’s arguments, the node itself will be stored as a key in the dict. For that key, the corresponding value is a list of dicts, each of which is the result of a recursive call to get_contraction_structure(). The list contains only dicts for the non-trivial deeper contractions, ommitting dicts with None as the one and only key.
Note
The presence of expressions among the dictinary keys indicates multiple levels of index contractions. A nested dict displays nested contractions and may itself contain dicts from a deeper level. In practical calculations the summation in the deepest nested level must be calculated first so that the outer expression can access the resulting indexed object.
Examples
>>> from sympy.tensor.index_methods import get_contraction_structure
>>> from sympy import symbols, default_sort_key
>>> from sympy.tensor import IndexedBase, Idx
>>> x, y, A = list(map(IndexedBase, ['x', 'y', 'A']))
>>> i, j, k, l = list(map(Idx, ['i', 'j', 'k', 'l']))
>>> get_contraction_structure(x[i]*y[i] + A[j, j])
{(i,): set([x[i]*y[i]]), (j,): set([A[j, j]])}
>>> get_contraction_structure(x[i]*y[j])
{None: set([x[i]*y[j]])}
A multiplication of contracted factors results in nested dicts representing the internal contractions.
>>> d = get_contraction_structure(x[i, i]*y[j, j])
>>> sorted(list(d.keys()), key=default_sort_key)
[None, x[i, i]*y[j, j]]
>>> d[None] # Note that the product has no contractions
set([x[i, i]*y[j, j]])
>>> sorted(d[x[i, i]*y[j, j]], key=default_sort_key) # factors are contracted ''first''
[{(i,): set([x[i, i]])}, {(j,): set([y[j, j]])}]
A parenthesized Add object is also returned as a nested dictionary. The term containing the parenthesis is a Mul with a contraction among the arguments, so it will be found as a key in the result. It stores the dictionary resulting from a recursive call on the Add expression.
>>> d = get_contraction_structure(x[i]*(y[i] + A[i, j]*x[j]))
>>> sorted(list(d.keys()), key=default_sort_key)
[x[i]*(y[i] + A[i, j]*x[j]), (i,)]
>>> d[(i,)]
set([x[i]*(y[i] + A[i, j]*x[j])])
>>> d[x[i]*(A[i, j]*x[j] + y[i])]
[{None: set([y[i]]), (j,): set([A[i, j]*x[j]])}]
Powers with contractions in either base or exponent will also be found as keys in the dictionary, mapping to a list of results from recursive calls:
>>> d = get_contraction_structure(A[j, j]**A[i, i])
>>> d[None]
set([A[j, j]**A[i, i]])
>>> nested_contractions = d[A[j, j]**A[i, i]]
>>> nested_contractions[0]
{(j,): set([A[j, j]])}
>>> nested_contractions[1]
{(i,): set([A[i, i]])}
The description of the contraction structure may appear complicated when represented with a string in the above examples, but it is easy to iterate over:
>>> from sympy import Expr
>>> for key in d:
... if isinstance(key, Expr):
... continue
... for term in d[key]:
... if term in d:
... # treat deepest contraction first
... pass
... # treat outermost contactions here
Determine the outer indices of expression expr
By outer we mean indices that are not summation indices. Returns a set and a dict. The set contains outer indices and the dict contains information about index symmetries.
Examples
>>> from sympy.tensor.index_methods import get_indices
>>> from sympy import symbols
>>> from sympy.tensor import IndexedBase, Idx
>>> x, y, A = list(map(IndexedBase, ['x', 'y', 'A']))
>>> i, j, a, z = symbols('i j a z', integer=True)
The indices of the total expression is determined, Repeated indices imply a summation, for instance the trace of a matrix A:
>>> get_indices(A[i, i])
(set(), {})
In the case of many terms, the terms are required to have identical outer indices. Else an IndexConformanceException is raised.
>>> get_indices(x[i] + A[i, j]*y[j])
(set([i]), {})
Exceptions : |
---|
An IndexConformanceException means that the terms ar not compatible, e.g.
>>> get_indices(x[i] + y[j])
(...)
IndexConformanceException: Indices are not consistent: x(i) + y(j)
Warning
The concept of outer indices applies recursively, starting on the deepest level. This implies that dummies inside parenthesis are assumed to be summed first, so that the following expression is handled gracefully:
>>> get_indices((x[i] + A[i, j]*y[j])*x[j])
(set([i, j]), {})
This is correct and may appear convenient, but you need to be careful with this as SymPy will happily .expand() the product, if requested. The resulting expression would mix the outer j with the dummies inside the parenthesis, which makes it a different expression. To be on the safe side, it is best to avoid such ambiguities by using unique indices for all contractions that should be held separate.