[Numpy-discussion] wrong casting of augmented assignment statements
Christopher Barker
Chris.Barker at noaa.gov
Tue Jan 12 14:34:11 EST 2010
Sebastian Walter wrote:
>>> However, this particular problem occurs when you try to automatically
>>> differentiate an algorithm by using an Algorithmic Differentiation
>>> (AD) tool.
>>> E.g. given a function
>>>
>>> x = numpy.ones(2)
>>> def f(x):
>>> a = numpy.ones(2)
>>> a *= x
>>> return numpy.sum(a)
I don't know anything about AD, but in general, when I write a function
that requires a given numpy array type as input, I'll do something like:
def f(x):
x = np.asarray(a, dtype=np.float)
a = np.ones(2)
a *= x
return np.sum(a)
That makes the casting explicit, and forces it to happen at the top of
the function, where the error will be more obvious. asarray will just
pass through a conforming array, so little performance penalty when you
do give it the right type.
-Chris
--
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
More information about the NumPy-Discussion
mailing list