[Numpy-discussion] Vector stacks

Nathaniel Smith njs at pobox.com
Sat Jul 1 18:53:07 EDT 2017

On Sat, Jul 1, 2017 at 3:31 PM, Charles R Harris
<charlesr.harris at gmail.com> wrote:
> Hi All,
> The  '@' operator works well with stacks of matrices, but not with stacks of
> vectors. Given the recent addition of '__array_ufunc__',  and the intent to
> make `__matmul__` use a ufunc, I've been wondering is it would make sense to
> add ndarray subclasses 'rvec' and 'cvec' that would override that operator
> so as to behave like stacks of row/column vectors. Any other ideas for the
> solution to stacked vectors are welcome.

I feel like the lesson of np.matrix is that subclassing ndarray to
change the meaning of basic operators creates more problems than it

Some alternatives include:
- if you specifically want a stack of row vectors or column vectors,
insert a new axis at position -1 or -2
- if you want a stack of 1d vectors that automatically act as rows on
the left of @ and columns on the right, then we could have vecvec,
matvec, vecmat gufuncs that do that -- which isn't quite as terse as
@, but not everything can be and at least it'd be explicit what was
going on.


Nathaniel J. Smith -- https://vorpus.org

More information about the NumPy-Discussion mailing list