How can one suppress evaluation while still being able to index?
<p>I have two small but very complicated matrices that I want to multiply. I've done this using sympy:</p>

<pre><code>C=sympy.MatMul(A,B,hold=True)
</code></pre>

<p>This gives me a MatMul object which saves a huge amount of time, and I'm not interested in a symbolic expression anyway, rather I want to evaluate at specific points later on.</p>

<p>If this was the end of my calculation it would be fine, but I then need to use elements of C in order to define a new equation, however, I cannot index it. I get the following,</p>

<pre><code>In [286]: C[0]
Traceback (most recent call last):

  File "&lt;ipython-input-286-829e2440bf19&gt;", line 1, in &lt;module&gt;
    C[0]

  File "C:\Anaconda3\lib\site-packages\sympy\matrices\expressions\matexpr.py", line 242, in __getitem__
    raise IndexError("Single index only supported for "

IndexError: Single index only supported for non-symbolic matrix shapes.
</code></pre>

<p>Is it possible to somehow index such an object? For example, in maple I can use a semi-colon which suppresses the output, while retaining information about the structure of the resulting object so I can index it.</p>

<p>To explain the error, normally, a single index indexes a matrix row-by-row:</p>

<pre><code>In [8]: M = Matrix([[1, 2], [3, 4]])

In [9]: M
Out[9]:
â¡1  2â¤
â¢    â¥
â£3  4â¦

In [10]: M[0]
Out[10]: 1

In [11]: M[1]
Out[11]: 2

In [12]: M[2]
Out[12]: 3

In [14]: M[3]
Out[14]: 4
</code></pre>

<p>For a symbolic matrix symbol, this is computed as the row,column index. For instance, </p>

<pre><code>In [16]: MatrixSymbol('A', 3, 4)[0]
Out[16]: Aââ

In [17]: MatrixSymbol('A', 3, 4)[10]
Out[17]: Aââ
</code></pre>

<p>The element <code>A[10]</code> is automatically converted to <code>A[2, 2]</code>, because A has 4 columns, so 10 is the third column of the third row (remember that everything is 0-indexed). </p>

<p>However, if your shape is symbolic, particularly the number of columns, say <code>A</code> is m x n, there is no way to know which row,column <code>A[i]</code> refers to (<code>n % i</code> is symbolic). Probably, SymPy could be changed to make <code>A[i]</code> return <code>A[i//n,i%n]</code> symbolically, but you generally want to explicitly reference matrix elements by row,column anyway, so if you really want that, you could just do it manually. Also, this formula has no bounds checking (if <code>i &gt;= n*m</code> the element is out of bounds).</p>

<p>Strictly speaking, <code>A[0]</code> could probably work, since that will always be <code>A[0, 0]</code> regardless of the shape of A. However, this would be a single special-case, and SymPy has chosen to disallow it, since it's possible to just write the explicit <code>A[0, 0]</code> anyway. </p>

python numpy math indexing sympy