Am Donnerstag, 28. Juli 2011, 17:42:38 schrieb Matthew Brett:
> If I understand you correctly, the problem is that, for 1.5.1:
> >>> class Test(np.ndarray): pass
> >>> type(np.min(Test((1,))))
> 
> <type 'numpy.float64'>
> 
> and for 1.6.0 (and current trunk):
> >>> class Test(np.ndarray): pass
> >>> type(np.min(Test((1,))))
> 
> <class '__main__.Test'>

Correct.  This is the behavior change we're talking (/complaining) about.

> So, 1.6.0 is returning a zero-dimensional scalar of the given type,
> and 1.5.1 returns a python scalar.

I am not sure about the terminology; I would have said that 1.5.1 returns a 
"numpy array scalar" (as opposed to a built-in python scalar), but at least we 
agree on the /scalar/ part here.

OTOH, 1.6.0 does not return a zero-dimensional /scalar/, but a zero-rank 
/array/, which may be used to represent scalars in some contexts.  But IMO 
this makes array subclassing much harder, since you need to special-case zero-
dimensional arrays in all your member functions.

IOW, it is hard to make a zero-dimensional instance of your array subclass 
"quack" like a scalar.  And I do not see any need / reason for the above 
change.

Right now, I found http://projects.scipy.org/numpy/wiki/ZeroRankArray which is 
an interesting read in this context, but does not seem to mention this change 
(age of that page is 5-6 years).

> Zero dimensional scalars are designed to behave in a similar way to
> python scalars, so the change should be all but invisible in practice.

Now I am not sure if you're confusing something.  I do not complain about min 
returning a numpy.float64 instead of a float, but about numpy wrapping the 
scalar in an instance of my *array* subclass.

This is also inconsistent with ndarray, and the matrix class contains an 
explicit workaround, returning self[0,0] in cases where a scalar is expected:

class matrix(N.ndarray):
    [...]

    def min(self, axis=None, out=None):
        """..."""
        return N.ndarray.min(self, axis, out)._align(axis)

    def _align(self, axis):
        """A convenience function for operations that need to preserve axis
        orientation.
        """
        if axis is None:
            return self[0,0] <=== HERE
        elif axis==0:
            return self
        elif axis==1:
            return self.transpose()
        else:
            raise ValueError, "unsupported axis"

> Was there a particular case you ran into where this was a problem?

Yes, but it is not easy to reproduce for me right now (changed versions of 
numpy, our software, etc.).

Basically, the problem arose because our ndarray subclass does not support 
zero-rank-instances fully.  (And previously, there was no need for that.)

Have a nice day,
  Hans
_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion

Reply via email to