[Numpy-discussion] Faster way to generate a rotation matrix?
josef.pktd@gmai...
josef.pktd@gmai...
Tue Mar 3 23:11:08 CST 2009
On Tue, Mar 3, 2009 at 11:41 PM, Jonathan Taylor
<jonathan.taylor@utoronto.ca> wrote:
> Thanks, All these things make sense and I should have known to
> calculate the sins and cosines up front. I managed a few more
> "tricks" and knocked off 40% of the computation time:
>
> def rotation(theta, R = np.zeros((3,3))):
> cx,cy,cz = np.cos(theta)
> sx,sy,sz = np.sin(theta)
> R.flat = (cx*cz - sx*cy*sz, cx*sz + sx*cy*cz, sx*sy,
> -sx*cz - cx*cy*sz, -sx*sz + cx*cy*cz,
> cx*sy, sy*sz, -sy*cz, cy)
> return R
>
> Pretty evil looking ;) but still wouldn't mind somehow getting it faster.
One of the usual recommendation on the python list is also to load
functions into the local scope to avoid the lookup in the module.
e.g. npcos = np.cos
or I think the usual: `from numpy import cos, sin, zeros` should be
better for speed
also you still have a few duplicate multiplications, e.g. cx*cz, cx*sz, ..?
but this looks already like micro optimization.
Josef
More information about the Numpy-discussion
mailing list