From: <vi...@id...> - 2001-02-22 21:47:03
|
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. -- Victor S. Miller | " ... Meanwhile, those of us who can compute can hardly vi...@id... | be expected to keep writing papers saying 'I can do the CCR, Princeton, NJ | following useless calculation in 2 seconds', and indeed 08540 USA | what editor would publish them?" -- Oliver Atkin |
From: John J. L. <ph...@cs...> - 2001-02-23 12:38:04
|
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 |
From: John J. L. <ph...@cs...> - 2001-02-23 15:09:17
|
I must be missing something obvious: how does one check if two variables refer to the same array object? John |
From: Robert K. <ke...@it...> - 2001-02-23 15:34:54
|
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 ke...@ca... "In the fields of hell where the grass grows high Are the graves of dreams allowed to die." -- Richard Harter |
From: Paul F. D. <pa...@pf...> - 2001-02-23 15:40:16
|
if a is b: ... -----Original Message----- From: num...@li... [mailto:num...@li...]On Behalf Of John J. Lee Sent: Friday, February 23, 2001 7:10 AM To: num...@li... 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 Num...@li... http://lists.sourceforge.net/lists/listinfo/numpy-discussion |
From: Aureli S. F. <Aur...@ip...> - 2001-03-19 19:59:36
|
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:au...@ip... fon: +49 30 39 00 61 50 fax: +49 30 39 17 517 ################################# |
From: Jon S. <js...@wm...> - 2001-03-19 21:23:32
|
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:au...@ip... > fon: +49 30 39 00 61 50 > fax: +49 30 39 17 517 > ################################# > > > > _______________________________________________ > Numpy-discussion mailing list > Num...@li... > http://lists.sourceforge.net/lists/listinfo/numpy-discussion > |
From: Warren F. <fo...@sl...> - 2001-03-19 21:38:39
|
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 |
From: John J. L. <ph...@cs...> - 2001-03-19 22:56:47
|
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 |
From: Aureli S. F. <Aur...@ip...> - 2001-03-22 10:58:47
|
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:au...@ip... fon: +49 30 39 00 61 50 fax: +49 30 39 17 517 ################################# |
From: Konrad H. <hi...@cn...> - 2001-03-20 14:17:42
|
> 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 <hi...@cn...> # 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. -- ------------------------------------------------------------------------------- Konrad Hinsen | E-Mail: hi...@cn... Centre de Biophysique Moleculaire (CNRS) | Tel.: +33-2.38.25.56.24 Rue Charles Sadron | Fax: +33-2.38.63.15.17 45071 Orleans Cedex 2 | Deutsch/Esperanto/English/ France | Nederlands/Francais ------------------------------------------------------------------------------- |