[Numpy-discussion] Who uses matrix?
Tim Hochberg
tim.hochberg at cox.net
Tue May 9 22:37:13 EDT 2006
Bill Baxter wrote:
> [MATH elided because I'm too tired to try to follow it]
>
>
> Anyway, back to sum -- if .sum() is to return a scalar, then what
> about .sum(axis=0)? Should that be a 1-D array of scalars rather
> than a matrix? If you answer no, then what about
> .sum(axis=0).sum(axis=1)? (Unrelated issue, but it seems that
> .sum(axis=0) and .sum(axis=1) both return row vectors, whereas I would
> expect the axis=1 variety to be a column vector.)
For matrices, sum(0) returns a 1xN matrix, while sum(1) returns a Nx1
vector as you expect. For arrays, it just returns a 1-D array, which
isn't row or column it's just 1-D.
> Anyway, seems to be like Tim (I think) said. This is just
> introducing new inconsistencies in place of old ones, so what's the
> point.
Well as much as possible the end result should be (a) useful and (b)
easy to explain. I suspect that the problem with the matrix class is
that not enough people have experience with it, so we're left with
either blindly following the lead of other matrix packages, some of
which I know do stupid things, or taking our best guess. I suspect
things will need to be tweaked as more experience with it piles up.
> In numpy, however, algebraic differences between 1x1 matrices and
> scalars are not as important as the fact that matrices are mutable
> while scalars are not.
>
>
> Seems like for most code the lack of methods and attributes on the
> scalar would be the bigger deal than the mutability difference.
Scalars in numpy *do* have methods and attributes, which may be why
Sasha doesn't think that difference is a big deal ;-).
> But either way, I'm not sure what point you're trying to make
> there. Scalars should go away because 1x1 matrices are more flexible?
Actually I thought that Sasha's position was that both scalars and
*rank-0* [aka shape=()] arrays were useful in different circumstances
and that we shouldn't completely anihilate rank-0 arrays in favor of
scalars. I'm not quite sure what that has to do with 1x1 arrays which
are a different kettle of fish, although also weird because of
broadcasting. I admit to never taking the time to fully deciphre Sasha's
position on rank-0 arrays though.
Speaking of rank-0 arrays, here's a wacky idea that Sasha will probably
appreciate even if it (probably) turns out to be impracticle: what if
matrix reduce returned 0xN and Nx0 array instead of 1xN and Nx1 arrays.
This preserves the column/rowness of the result, however a rank-0 array
falls out of the axis=None case naturally. Rank-0 arrays are nearly
identical to scalars (except for the mutability bit, which I suspect is
a pretty minor issue). I generated a Nx0 array by hand to try this out;
some stuff works, but at least moderate tweaking would be required to
make this work correctly.
-tim
More information about the NumPy-Discussion
mailing list