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>You can index elements of <code>C</code>, e.g. <code>C[0,0]</code>.  So this will give you the first row as a list:</p>

<pre><code>row0 = [C[0, k] for k in range(C.shape[1])]
</code></pre>

<p>Here's an example.  <code>x</code> and <code>y</code> are sympy symbols.</p>

<pre><code>In [40]: A
Out[40]: 
Matrix([
[2*x + 1, x + 3],
[     -2,     3]])

In [41]: B
Out[41]: 
Matrix([
[-3,   3],
[ y, 2*y]])

In [42]: C = sympy.MatMul(A, B, hold=True)

In [43]: C[0,0]
Out[43]: -6*x + y*(x + 3) - 3

In [44]: [C[0,k] for k in range(C.shape[1])]
Out[44]: [-6*x + y*(x + 3) - 3, 6*x + 2*y*(x + 3) + 3]
</code></pre>

python numpy math indexing sympy