
Is there some way of having calculations which cause underflow automatically set their result to 0.0? For example when I take exp(a), where a is a floating point array.

On 22 Feb 2001, Victor S. Miller wrote:
Is there some way of having calculations which cause underflow automatically set their result to 0.0? For example when I take exp(a), where a is a floating point array.
Not 'automatically', but:
a = whatever() choose(greater(a, MAX), (a, MAX)) answer = exp(-a)
any good? This is with a 1D array -- I haven't used higher dimensions much.
John

I must be missing something obvious: how does one check if two variables refer to the same array object?
John

if a is b: ...
-----Original Message----- From: numpy-discussion-admin@lists.sourceforge.net [mailto:numpy-discussion-admin@lists.sourceforge.net]On Behalf Of John J. Lee Sent: Friday, February 23, 2001 7:10 AM To: numpy-discussion@lists.sourceforge.net Subject: [Numpy-discussion] checking identity of arrays?
I must be missing something obvious: how does one check if two variables refer to the same array object?
John
_______________________________________________ Numpy-discussion mailing list Numpy-discussion@lists.sourceforge.net http://lists.sourceforge.net/lists/listinfo/numpy-discussion

On Fri, 23 Feb 2001, John J. Lee wrote:
I must be missing something obvious: how does one check if two variables refer to the same array object?
Python's id() builtin function?
John
-- Robert Kern kern@caltech.edu
"In the fields of hell where the grass grows high Are the graves of dreams allowed to die." -- Richard Harter

Hello!
I have a N-dimensional array A and I want to operate in one of the axis (k) with a 1 dimensional array (for instance, subtracting an array B of length k). I have looked for some solutions in the manual and did not found any.
So after testing a lot I found following solution:
A=Numeric.array(a)
A.shape=(a1,a2,...,ak,...,aN) #where N is any int value
B=Numeric.array(b) B.shape=(ak,)
A_modified=Numeric.reshape(A, (a1*a2*...*aN,ak)) #ak is not included in the product
result=[] for i in A_modified: result.append(i - B)
and finally reshaping the result appropriately. But it does not seem neither elegant nor simple.
Is there any more elegant solution? The key point is: how to operate a N-D array with a 1D in one determined axis?
Thanks in advance. Regards, Aureli
################################# Aureli Soria Frisch Fraunhofer IPK Dept. Pattern Recognition
post: Pascalstr. 8-9, 10587 Berlin, Germany e-mail:aureli@ipk.fhg.de fon: +49 30 39 00 61 50 fax: +49 30 39 17 517 #################################

Probably this can help you.
Look in the manual about the ... operator, too.
bash-2.02$ python Python 1.6b1 (#3, Aug 30 2000, 08:32:49) [GCC 2.7.2.1] on freebsd3 Copyright (c) Corporation for National Research Initiatives. Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam.
from Numeric import * a=array([[2,3],[4,5],[6,7]]) b=array([9,10,11]) a.shape
(3, 2)
b.shape
(3,)
print a+b[:,NewAxis]
[[11 12] [14 15] [17 18]]
Jon Saenz. | Tfno: +34 946012470 Depto. Fisica Aplicada II | Fax: +34 944648500 Facultad de Ciencias. \ Universidad del Pais Vasco \ Apdo. 644 \ 48080 - Bilbao \ SPAIN
On Mon, 19 Mar 2001, Aureli Soria Frisch wrote:
Hello!
I have a N-dimensional array A and I want to operate in one of the axis (k) with a 1 dimensional array (for instance, subtracting an array B of length k). I have looked for some solutions in the manual and did not found any.
So after testing a lot I found following solution:
A=Numeric.array(a)
A.shape=(a1,a2,...,ak,...,aN) #where N is any int value
B=Numeric.array(b) B.shape=(ak,)
A_modified=Numeric.reshape(A, (a1*a2*...*aN,ak)) #ak is not included in the product
result=[] for i in A_modified: result.append(i - B)
and finally reshaping the result appropriately. But it does not seem neither elegant nor simple.
Is there any more elegant solution? The key point is: how to operate a N-D array with a 1D in one determined axis?
Thanks in advance. Regards, Aureli
################################# Aureli Soria Frisch Fraunhofer IPK Dept. Pattern Recognition
post: Pascalstr. 8-9, 10587 Berlin, Germany e-mail:aureli@ipk.fhg.de fon: +49 30 39 00 61 50 fax: +49 30 39 17 517 #################################
Numpy-discussion mailing list Numpy-discussion@lists.sourceforge.net http://lists.sourceforge.net/lists/listinfo/numpy-discussion

On Mon, 19 Mar 2001, Aureli Soria Frisch wrote:
Hello!
I have a N-dimensional array A and I want to operate in one of the axis (k) with a 1 dimensional array (for instance, subtracting an array B of length k). I have looked for some solutions in the manual and did not found any.
So after testing a lot I found following solution:
A=Numeric.array(a)
A.shape=(a1,a2,...,ak,...,aN) #where N is any int value
B=Numeric.array(b) B.shape=(ak,)
newshape = [1] * len(A.shape) newshape[k] = A.shape[k] B.shape = newshape result = A - B
wbf

On Mon, 19 Mar 2001, Aureli Soria Frisch wrote: [...]
I have a N-dimensional array A and I want to operate in one of the axis (k) with a 1 dimensional array (for instance, subtracting an array B of length k). I have looked for some solutions in the manual and did not found any.
[...]
Is there any more elegant solution? The key point is: how to operate a N-D array with a 1D in one determined axis?
I'm not sure I understand exactly what you want. Are you sure you don't want to do A[n1,n2,...,:,...,nN] instead? (the '...'s are just an indication of where the extra numbers would go, not the literal Numeric syntax. If so, you can use
s = slice(None) # n is your list [n1, n2, n3, ..., n(k-1), n(k+1), ...,nN] n.insert(k, s) print "answer is", A[n] - B print slice.__doc__
(possibly there is an off-by-one error in the above, but you get the idea)
John

Thanks to all answers in spite of the not so understandable question.
I have solved my problem NewAxis seems tome the simplest and most elegant way to solve the problem. Maybe a name like 'VirtualAxis' would clarify more the question.
Regards
On Mon, 19 Mar 2001, Aureli Soria Frisch wrote: [...]
I have a N-dimensional array A and I want to operate in one of the axis (k) with a 1 dimensional array (for instance, subtracting an array B of length k). I have looked for some solutions in the manual and did not found any.
[...]
Is there any more elegant solution? The key point is: how to operate a N-D array with a 1D in one determined axis?
################################# Aureli Soria Frisch Fraunhofer IPK Dept. Pattern Recognition
post: Pascalstr. 8-9, 10587 Berlin, Germany e-mail:aureli@ipk.fhg.de fon: +49 30 39 00 61 50 fax: +49 30 39 17 517 #################################

I have a N-dimensional array A and I want to operate in one of the axis (k) with a 1 dimensional array (for instance, subtracting an array B of length k). I have looked for some solutions in the manual and did not found any.
I am not sure that I understand your problem exactly, but here's the solution to what I think your problem is ;-)
from Scientific.indexing import index_expression import Numeric
indices = index_expression[::] + \ (len(A.shape)-k-1)*index_expression[Numeric.NewAxis] result = A-B[indices]
This uses a module from ScientificPython which provides syntactic sugar for indexing. In fact, the module is so simple that I include it here:
--------------------------------------------------------------------------- # A nicer way to build up index tuples for arrays. # # You can do all this with slice() plus a few special objects, # but there's a lot to remember. This version is simpler because # it uses the standard array indexing syntax. # # Written by Konrad Hinsen hinsen@cnrs-orleans.fr # last revision: 1999-7-23 #
"""This module provides a convenient method for constructing array indices algorithmically. It provides one importable object, 'index_expression'.
For any index combination, including slicing and axis insertion, 'a[indices]' is the same as 'a[index_expression[indices]]' for any array 'a'. However, 'index_expression[indices]' can be used anywhere in Python code and returns a tuple of indexing objects that can be used in the construction of complex index expressions.
Sole restriction: Slices must be specified in the double-colon form, i.e. a[::] is allowed, whereas a[:] is not. """
class _index_expression_class:
import sys maxint = sys.maxint
def __getitem__(self, item): if type(item) != type(()): return (item,) else: return item
def __len__(self): return self.maxint
def __getslice__(self, start, stop): if stop == self.maxint: stop = None return self[start:stop:None]
index_expression = _index_expression_class() ---------------------------------------------------------------------------
Konrad.
participants (8)
-
Aureli Soria Frisch
-
John J. Lee
-
Jon Saenz
-
Konrad Hinsen
-
Paul F. Dubois
-
Robert Kern
-
victor@idaccr.org
-
Warren Focke