Thanks for the help.

What I am actually doing is computing a gradient to a least squares objective. That is,

     X.T.dot(X.dot(beta) - Y)

If X is such that X.dot(beta) is fast (i.e. matvec is fast) then am I missing a "simple" optimization here at the cost of a copy? Alternatively, if X is such that vecmat is fast, then what is the best way to do this? A copy seems easiest, and possibly applying the previous "simple" optimization.
Based on my understanding of the other replies, I would guess that if X2=X.copy(), then the fastest way would be 

    (X2.dot(beta) - Y).dot(X)

This doesn't pan out in my example, the winner is

    X2.T.dot(X2.dot(beta) - Y)

which is about the same as

    (X2.dot(beta)-Y).dot(X2)

I made a small gist: https://gist.github.com/da7b2ef6ef109511af06a9cebbfc8ed1

One difference I see between a numpy array with the same strides and the array loaded from a MAT file is the ALIGNED flag.

On Sat, Aug 26, 2017 at 10:10 AM, Stephan Hoyer <shoyer@gmail.com> wrote:
On Sat, Aug 26, 2017 at 12:09 AM, Jonathan Taylor <jonathan.taylor@stanford.edu> wrote:
So, matvec is just slower because of strides and where numpy retrieves data? Is there a simple way to do this besides a copy? I can easily afford the copy, just wondering.

No, the only way to change the strides of an array with the same data is to make a copy.

Array operations will always be fastest when the smallest strides are along the axis iterated over in the inner-most (summed) loop. So this existing strides of your matrix are not sub-optimal in general, just for this specific operation. They would be suitable, for example, in a vector-matrix multiply.

_______________________________________________
SciPy-Dev mailing list
SciPy-Dev@python.org
https://mail.python.org/mailman/listinfo/scipy-dev




--
Jonathan Taylor                         
Dept. of Statistics                     
Sequoia Hall, 137                         
390 Serra Mall
Stanford, CA 94305
Tel:   650.723.9230
Fax:   650.725.8977
Web: http://www-stat.stanford.edu/~jtaylo