@Warren points out that square
'delegates' to multiply. I verified this by making an object array that includes a list:
In [524]: arr = np.array([np.arange(3), 3, [3,4]])
In [525]: np.square(arr)
TypeError: can't multiply sequence by non-int of type 'list'
square
works on the rest of the array:
In [526]: np.square(arr[:2])
Out[526]: array([array([0, 1, 4]), 9], dtype=object)
sqrt
doesn't work on any of these:
In [527]: np.sqrt(arr)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-527-b58949107b3d> in <module>()
----> 1 np.sqrt(arr)
AttributeError: 'numpy.ndarray' object has no attribute 'sqrt'
I can make sqrt
work with a custom class:
class Foo(float):
def sqrt(self):
return self**0.5
In [539]: arr = np.array([Foo(3), Foo(2)], object)
In [540]: np.square(arr)
Out[540]: array([9.0, 4.0], dtype=object)
In [541]: np.sqrt(arr)
Out[541]: array([1.7320508075688772, 1.4142135623730951], dtype=object)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…