[Numpy-discussion] New DTypes: Are scalars a central concept in NumPy or not?

Chris Barker chris.barker at noaa.gov
Wed Apr 8 18:45:58 EDT 2020


On Wed, Apr 8, 2020 at 1:17 PM Sebastian Berg <sebastian at sipsolutions.net>
wrote:

> > > > But, backward compatibility aside, could we have ONLY Scalars?
> > > Well, it is hard to write functions that work on N-Dimensions
> > > (where N
> > > can be 0), if the 0-D array does not exist.
>


> So as a (silly) example, the following does not generalize to 0d, even
> though it should:
>
> def weird_normalize_by_trace_inplace(stacked_matrices)
>     """Devides matrices by their trace but retains sign
>     (works in-place, and thus e.g. not for integer arrays)
>
>     Parameters
>     ----------
>     stacked_matrices : (..., N, M) ndarray
>     """
>     assert stacked_matrices.shape[-1] == stacked_matrices.shape[-2]
>
>     trace = np.trace(stacked_matrices, axis1=-2, axis2=-1)
>     trace[trace < 0] *= -1
>     stacked_matrices /= trace
>
> Sure that function does not make sense and you could rewrite it, but
> the fact is that in that function you want to conditionally modify
> trace in-place, but trace can be 0d and the "conditional" modification
> breaks down.
>

I guess that's what I'm getting at -- there is always an endpoint to
reducing the rank. a function that's designed to work on a "stack" of
something doesn't have to work on a single something, when it can, instead,
work on a "stack" of hight one.

Isn't the trace of a matrix always a scalar? and thus the trace(s) of a
stack of matrixes would always  be 1-D?

So that function should do something like:

stacked_matrixes.shape = (-1, M, M)

yes?

and then it would always work.

Again, backwards compatibility, but there is a reason the np.atleast_*()
functions exist -- you often need to make sure your inputs have the
dimensionality expected.

-CHB

-- 

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20200408/64c1959c/attachment.html>


More information about the NumPy-Discussion mailing list