How to assign variables to numpy matrix
<p>I want to make a function to generate rotational matrix R. Here is my code:</p>

<pre><code>import numpy as np
def R(theta):
    a = np.cos(theta)
    b = -1*np.sin(theta)
    c = np.sin(theta)
    d = np.cos(theta)
    return np.matrix('a b; c d')
</code></pre>

<p>But it has error like this</p>

<pre><code>raise TypeError("Invalid data string supplied: " + astr)
TypeError: Invalid data string supplied: a
</code></pre>

<p>Any suggestions?</p>

<p>That matrix notation:</p>

<pre><code>a = np.matrix('1 2; 3 4')
</code></pre>

<p>only works for literals, not variables; with scalar variables you'd use the bracket notation</p>

<pre><code>np.matrix([[1, 2], [3, 4]])
np.matrix([[a, b], [c, d]])
</code></pre>

<p>This part, <code>[[a, b], [c, d]]</code> is an ordinary Python list (of lists).  <code>np.matrix(...)</code> then takes this list and turns it into a <code>numpy</code> matrix.</p>

<p>But really, you should be using</p>

<pre><code>np.array(...)
</code></pre>

<p><code>np.matrix</code> was written to make things familiar to MATLAB visitors.  Most <code>numpy</code> work is the <code>array</code>, not <code>matrix</code>.</p>

<p>Another point - <code>a = np.cos(theta)</code> works with <code>theta</code> is scalar or an array.  But if an array, than <code>a</code> itself will be an array, not a scalar.  You should be aware of that when constructing this rotation matrix.</p>

python numpy matrix