Compact way of performing array math with specified result type?
I often find myself doing simple math on sequences of numbers (which might or might not be numpy arrays) where I want the result (and thus the inputs) coerced to a particular data type. I'd like to be able to say: numpy.divide(seq1, seq2, dtype=float) but ufuncs don't allow on to specify a result type. So I do this instead: numpy.array(seq1, dtype=float) / numpy.array(seq2, dtype=float) Is there a more compact solution (without having to create the result array first and supply it as an argument)? -- Russell
Russell E. Owen wrote:
I often find myself doing simple math on sequences of numbers (which might or might not be numpy arrays) where I want the result (and thus the inputs) coerced to a particular data type.
I'd like to be able to say:
numpy.divide(seq1, seq2, dtype=float)
but ufuncs don't allow on to specify a result type. So I do this instead:
numpy.array(seq1, dtype=float) / numpy.array(seq2, dtype=float)
Is there a more compact solution (without having to create the result array first and supply it as an argument)?
def fasarray(seq): return numpy.asarray(seq, dtype=float) fasarray(seq1) / fasarray(seq2) -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
Russell E. Owen wrote:
I often find myself doing simple math on sequences of numbers (which might or might not be numpy arrays) where I want the result (and thus the inputs) coerced to a particular data type.
I'd like to be able to say:
numpy.divide(seq1, seq2, dtype=float)
but ufuncs don't allow on to specify a result type. So I do this instead:
numpy.array(seq1, dtype=float) / numpy.array(seq2, dtype=float)
Is there a more compact solution (without having to create the result array first and supply it as an argument)?
Every ufunc has a little-documented keyword "sig" for (signature) which allows you to specify the signature of the inner loop. Thus, numpy.divide(seq1, seq1, sig=('d',)*3) will do what you want. -Travis
-- Russell
_______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
On Sat, Apr 28, 2007 at 10:04 PM, Travis Oliphant <oliphant.travis@ieee.org> wrote:
Russell E. Owen wrote:
I often find myself doing simple math on sequences of numbers (which might or might not be numpy arrays) where I want the result (and thus the inputs) coerced to a particular data type.
I'd like to be able to say:
numpy.divide(seq1, seq2, dtype=float)
but ufuncs don't allow on to specify a result type. So I do this instead:
numpy.array(seq1, dtype=float) / numpy.array(seq2, dtype=float)
Is there a more compact solution (without having to create the result array first and supply it as an argument)?
Every ufunc has a little-documented keyword "sig" for (signature) which allows you to specify the signature of the inner loop.
Thus,
numpy.divide(seq1, seq1, sig=('d',)*3)
will do what you want.
-Travis
Hi, going through my very old emails - I was wondering if this has gotten better documented by now !? (and where ?) -Sebastian Haase
participants (4)
-
Robert Kern
-
Russell E. Owen
-
Sebastian Haase
-
Travis Oliphant