sympy/sympy — pull request #7883 by jcrist (2014-08-17 18:16:05+00:00)
Title: Support for single indexes in MatrixExpr

This allows MatrixSymbols to be iterated over, and gives a similar
interface to DenseMatrix. MatExpr are treated as row-major order arrays.
This only works if both the matrix shape and index are Integer/int.

Demo:
```
>>> qs = symbols('q0:5')
>>> q = MatrixSymbol('q', 5, 1)
>>> expr = sum(qs); expr
q0 + q1 + q2 + q3 + q4

# Can be used to create a sub_dict, with all symbols in a vector representation.
# This is useful for code-generation or numerical evaluation
>>> sub_dict = dict(zip(qs, q)); sub_dict
{q4: q[4, 0], q3: q[3, 0], q0: q[0, 0], q1: q[1, 0], q2: q[2, 0]}
>>> expr.subs(sub_dict)
q[0, 0] + q[1, 0] + q[2, 0] + q[3, 0] + q[4, 0]

# Can also iterate over slices. This will sum just the fourth column of matrix A
>>> A = MatrixSymbol('A', 5, 5)
>>> sum(A[3, :])
A[3, 0] + A[3, 1] + A[3, 2] + A[3, 3] + A[3, 4]
```