The overhead of the np.matrix class is quite high for small matrices. See for example the following code:
import time import math import numpy as np
def rot2D(phi): c=math.cos(phi); return np.matrix(c)
_b=np.matrix(np.zeros( (1,))) def rot2Dx(phi): global _b r=_b.copy() c=math.cos(phi); r.itemset(0, c) return r
phi=.023
%timeit rot2D(phi) %timeit rot2Dx(phi)
The second implementation performs much better by using a copy instead of a constructor. Is there a way to efficiency create a new np.matrix object? For other functions in my code I do not have the option to copy an existing matrix, but I need to construct a new object or perform a cast from np.array to np.matrix.
I am already aware of two alternatives:
 Using the new multiplication operator ( https://www.python.org/dev/peps/pep0465/). This is a good solution, but only python 3.5  Using the .dot functions from np.array. This works, but personally I like the notation using np.matrix much better.
I also created an issue on github: https://github.com/numpy/numpy/issues/6186
With kind regards, Pieter Eendebak
participants (1)

Pieter Eendebak