From ddrake at brontes3d.com Tue Sep 4 14:26:32 2007
From: ddrake at brontes3d.com (Daniel Drake)
Date: Tue, 04 Sep 2007 14:26:32 -0400
Subject: [Numpy-discussion] Numeric 64 bit issues with python 2.5 -- fixed
Message-ID: <1188930392.10589.3.camel@localhost>
I realise Numeric is a dead project, but in case it is useful for anyone
else:
I wrote a patch to solve the issue reported here:
http://projects.scipy.org/pipermail/numpy-discussion/2007-May/027616.html
The patch can be found here:
http://bugs.gentoo.org/attachment.cgi?id=129716&action=view
S?bastien Fabbro performed a more comprehensive review and fixed up some
other potential issues alongside my fix. His patch is here:
http://sources.gentoo.org/viewcvs.py/*checkout*/gentoo-x86/dev-python/numeric/files/numeric-24.2-python25.patch
This patch has been included in Gentoo's package tree.
--
Daniel Drake
Brontes Technologies, A 3M Company
http://www.brontes3d.com/opensource
From David.L.Goldsmith at noaa.gov Tue Sep 4 20:24:20 2007
From: David.L.Goldsmith at noaa.gov (David Goldsmith)
Date: Tue, 04 Sep 2007 17:24:20 -0700
Subject: [Numpy-discussion] Anyone have a well-tested SWIG-based C++ STL
valarray <=> numpy.array typemap to share?
Message-ID: <46DDF734.7070708@noaa.gov>
Anyone have a well-tested SWIG-based C++ STL valarray <=> numpy.array
typemap to share? Thanks!
DG
--
ERD/ORR/NOS/NOAA
From broman at spawar.navy.mil Tue Sep 4 20:52:07 2007
From: broman at spawar.navy.mil (Vincent Broman)
Date: Tue, 4 Sep 2007 17:52:07 -0700
Subject: [Numpy-discussion] numpy build fails on powerpc ydl
In-Reply-To:
References:
Message-ID: <200709041752.08069@b00d61a8cecf8b2266f81358fd170621.navy.mil>
Oliphant:
> Would you be willing to help get the config.h file set up correctly?
Yes. I thought it was automatic, tho.
What to do?
me:
> Its kernel is 2.4.19-Asmp tailored by the vendor.
Harris:
> Which vendor?
Curtiss-Wright Controls, back when they were called Synergy.
Harris:
> Ancient.... Curtiss-Wright now supports Linux and kernel 2.6.16 on
> some of their newer hardware
The military ends up supporting ancient systems for a long time.
I don't think I have the option of upgrading the OS on these beasts,
especially when it involves a new binary format.
That would be nice, tho, especially if it made it possible to run subversion.
Harris:
> Any more detail on these? What causes the conflict. I've got to wonder about
> the the libc/libm versions also. Does the include file math.h say anything
> about the prototypes for these functions? I expect cosl et.al. to be
> potential problems on the PPC anyway due to the way long doubles were
> implemented.
"grep -r sinl /usr/include" finds nothing.
math.h defines sin and sinf with various underscores attached,
using token pasting, but no mention of sinl.
Similarly for fabs and cos which I checked.
Harris:
> What is the PPC model number?
The CWC VSS4 contains four powerpc G4's, the 7400.
Vincent Broman
From charlesr.harris at gmail.com Tue Sep 4 23:41:37 2007
From: charlesr.harris at gmail.com (Charles R Harris)
Date: Tue, 4 Sep 2007 21:41:37 -0600
Subject: [Numpy-discussion] numpy build fails on powerpc ydl
In-Reply-To: <200709041752.08069@b00d61a8cecf8b2266f81358fd170621.navy.mil>
References:
<200709041752.08069@b00d61a8cecf8b2266f81358fd170621.navy.mil>
Message-ID:
Hi Vincent,
On 9/4/07, Vincent Broman wrote:
>
> Oliphant:
> > Would you be willing to help get the config.h file set up correctly?
>
> Yes. I thought it was automatic, tho.
> What to do?
>
> me:
> > Its kernel is 2.4.19-Asmp tailored by the vendor.
>
> Harris:
> > Which vendor?
>
> Curtiss-Wright Controls, back when they were called Synergy.
>
> Harris:
> > Ancient.... Curtiss-Wright now supports Linux and kernel 2.6.16 on
> > some of their newer hardware
>
> The military ends up supporting ancient systems for a long time.
> I don't think I have the option of upgrading the OS on these beasts,
> especially when it involves a new binary format.
> That would be nice, tho, especially if it made it possible to run
> subversion.
Too bad. It looks to me like gcc2.95.3 should work just fine. Could you run
$ cat ./build/src.linux-i686-2.5/numpy/core/config.h
in your numpy source directory, with the appropriate changes of course, and
send the result? I'm curious about the value of SIZEOF_LONG_DOUBLE.
Then could you also compile and run
#include
int main(int argc, char** argv)
{
printf("size of long double: %d\n", sizeof(long double));
return 1;
}
And see what it prints? The two should be the same.
Thanks,
Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
From dannoritzer at web.de Wed Sep 5 03:48:45 2007
From: dannoritzer at web.de (=?ISO-8859-1?Q?G=FCnter_Dannoritzer?=)
Date: Wed, 05 Sep 2007 09:48:45 +0200
Subject: [Numpy-discussion] Use my own data type with NumPy
Message-ID: <46DE5F5D.8060204@web.de>
Hi,
I am trying to use my own data type with NumPy, but get some funny
result when creating a NumPy array with it.
My data type is indexable and sliceable and what happens now is when I
create an array, NumPy is adding the instance as a list of the indexed
values. How can I force NumPy to handle my data type as an 'Object' and
use the value of __repr__ to display in the array?
Maybe I am handling that too simple, as I had another data type before,
-- not indexable though --, that just works fine with NumPy. Do I have
to worry about a dtype object with my new data type or how can I use my
new data type with NumPy?
Thanks for your help.
Cheers,
Guenter
From Marc.Poinot at onera.fr Wed Sep 5 05:05:00 2007
From: Marc.Poinot at onera.fr (Marc POINOT)
Date: Wed, 05 Sep 2007 11:05:00 +0200
Subject: [Numpy-discussion] Change array memory ownership status
Message-ID: <46DE713C.1080805@onera.fr>
I want to change the "status" of a numpy array.
I mean this array was created by a server application using PyArray_FromDimsAndData that sets the NPY_OWNDATA flag to False.
The server application believes the client would free the memory.
But there are more than one client application and none knows who is in charge of freeing this memory.
Then I want to set the flag NPY_OWNDATA to True to tell the server to do the job when it finishes the script.
How can I do that, I mean at the Python interface level, not the C API.
>>> print a.flags.owndata
True
>>>
>>> a.flags.owndata=False
Traceback (most recent call last):
File "", line 1, in ?
TypeError: attribute 'owndata' of 'numpy.flagsobj' objects is not writable
>>>
Or should I have to write my own function set/get for OWNDATA ?
The get is there but I can't get the set...
-MP-
-----------------------------------------------------------------------
Marc POINOT [ONERA/DSNA] Tel:+33.1.46.73.42.84 Fax:+33.1.46.73.41.66
Avertissement/disclaimer http://www.onera.fr/onera-en/emails-terms
From david at ar.media.kyoto-u.ac.jp Wed Sep 5 07:01:11 2007
From: david at ar.media.kyoto-u.ac.jp (David Cournapeau)
Date: Wed, 05 Sep 2007 20:01:11 +0900
Subject: [Numpy-discussion] Improving bug triage in trac ?
Message-ID: <46DE8C77.10808@ar.media.kyoto-u.ac.jp>
Hi there,
I am personnally a bit annoyed by the way trac handle bug reports,
and would like to know if there is space for improvement. I do not know
much about bug tracking systems, so maybe I just don't know how to use
it, though. The main thing I dislike is the status of tickets and
reports. In particular:
- I don't know how other feel, but I am rather confused by the meta
data of a ticket, and do not find them really useful from a developer
point of view. It would be quite helpful to have information whether the
bug is confirmed or not, another one which says wether a patch is
available or not. This would make bug triage much easier, IMHO. This
should be possible, since I have seen some trac installation with such
features (wordpad trac, for example).
- This one maybe a bit more difficult to implement I guess (I don't
know anything about trac internals): I find the general view of bugs for
a given repository really helpful in launchpad, in perticular, you can
easily view the percentage of bugs wrt their status (eg 30 % bugs
unconfirmed, etc...); see for example https://bugs.launchpad.net/+about.
This gives a pretty good idea of what needs to be done for a particular
release.
How do other people feel about those suggestions ?
cheers,
David
From Joris.DeRidder at ster.kuleuven.be Wed Sep 5 07:36:13 2007
From: Joris.DeRidder at ster.kuleuven.be (Joris De Ridder)
Date: Wed, 5 Sep 2007 13:36:13 +0200
Subject: [Numpy-discussion] Anyone have a well-tested SWIG-based C++ STL
valarray <=> numpy.array typemap to share?
In-Reply-To: <46DDF734.7070708@noaa.gov>
References: <46DDF734.7070708@noaa.gov>
Message-ID: <32632B72-2B1A-4AC2-AA04-288696E37A46@ster.kuleuven.be>
A related question, just out of curiosity: is there a technical
reason why Numpy has been coded in C rather than C++?
Joris
On 05 Sep 2007, at 02:24, David Goldsmith wrote:
> Anyone have a well-tested SWIG-based C++ STL valarray <=> numpy.array
> typemap to share? Thanks!
>
> DG
> --
> ERD/ORR/NOS/NOAA emergencyresponse/>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
From gnata at obs.univ-lyon1.fr Wed Sep 5 08:08:16 2007
From: gnata at obs.univ-lyon1.fr (Xavier Gnata)
Date: Wed, 05 Sep 2007 14:08:16 +0200
Subject: [Numpy-discussion] Anyone have a well-tested SWIG-based C++ STL
valarray <=> numpy.array typemap to share?
In-Reply-To: <32632B72-2B1A-4AC2-AA04-288696E37A46@ster.kuleuven.be>
References: <46DDF734.7070708@noaa.gov>
<32632B72-2B1A-4AC2-AA04-288696E37A46@ster.kuleuven.be>
Message-ID: <46DE9C30.8020300@obs.univ-lyon1.fr>
I'm using the numpy C API (PyArray_SimpleNewFromData) to perform the
conversion but my code is written by hands. I would like to simplify it
using SWIG but I also would like to see a good typemap valarray <=>
numpy.array :)
Joris : Historical ones? Maybe also the fact that distutils has some small pb with C++ module. To sum up : You have to compile you module with the same compiler options you used to compile Python. Python is coded in C so some options does not make sense in C++.
As a result, you get warnings at compile time (see pylab compiled with gcc for instance).
Xavier
> A related question, just out of curiosity: is there a technical
> reason why Numpy has been coded in C rather than C++?
>
> Joris
>
>
>
> On 05 Sep 2007, at 02:24, David Goldsmith wrote:
>
>
>> Anyone have a well-tested SWIG-based C++ STL valarray <=> numpy.array
>> typemap to share? Thanks!
>>
>> DG
>> --
>> ERD/ORR/NOS/NOAA > emergencyresponse/>
>> _______________________________________________
>> Numpy-discussion mailing list
>> Numpy-discussion at scipy.org
>> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>>
>
>
> Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>
>
--
############################################
Xavier Gnata
CRAL - Observatoire de Lyon
9, avenue Charles Andr?
69561 Saint Genis Laval cedex
Phone: +33 4 78 86 85 28
Fax: +33 4 78 86 83 86
E-mail: gnata at obs.univ-lyon1.fr
############################################
From travis at enthought.com Wed Sep 5 10:07:14 2007
From: travis at enthought.com (Travis Vaught)
Date: Wed, 5 Sep 2007 09:07:14 -0500
Subject: [Numpy-discussion] Anyone have a well-tested SWIG-based C++ STL
valarray <=> numpy.array typemap to share?
In-Reply-To: <46DE9C30.8020300@obs.univ-lyon1.fr>
References: <46DDF734.7070708@noaa.gov>
<32632B72-2B1A-4AC2-AA04-288696E37A46@ster.kuleuven.be>
<46DE9C30.8020300@obs.univ-lyon1.fr>
Message-ID:
Have you seen this?
http://www.scipy.org/Cookbook/SWIG_and_NumPy
Also, the numpy/doc/swig directory has the simple typemaps.
Travis
On Sep 5, 2007, at 7:08 AM, Xavier Gnata wrote:
> I'm using the numpy C API (PyArray_SimpleNewFromData) to perform the
> conversion but my code is written by hands. I would like to
> simplify it
> using SWIG but I also would like to see a good typemap valarray <=>
> numpy.array :)
>
> Joris : Historical ones? Maybe also the fact that distutils has
> some small pb with C++ module. To sum up : You have to compile you
> module with the same compiler options you used to compile Python.
> Python is coded in C so some options does not make sense in C++.
> As a result, you get warnings at compile time (see pylab compiled
> with gcc for instance).
>
>
> Xavier
>
>
>
>> A related question, just out of curiosity: is there a technical
>> reason why Numpy has been coded in C rather than C++?
>>
>> Joris
>>
>>
>>
>> On 05 Sep 2007, at 02:24, David Goldsmith wrote:
>>
>>
>>> Anyone have a well-tested SWIG-based C++ STL valarray <=>
>>> numpy.array
>>> typemap to share? Thanks!
>>>
>>> DG
>>> --
>>> ERD/ORR/NOS/NOAA >> emergencyresponse/>
>>> _______________________________________________
>>> Numpy-discussion mailing list
>>> Numpy-discussion at scipy.org
>>> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>>>
>>
>>
>> Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
>>
>> _______________________________________________
>> Numpy-discussion mailing list
>> Numpy-discussion at scipy.org
>> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>>
>>
>
>
> --
> ############################################
> Xavier Gnata
> CRAL - Observatoire de Lyon
> 9, avenue Charles Andr?
> 69561 Saint Genis Laval cedex
> Phone: +33 4 78 86 85 28
> Fax: +33 4 78 86 83 86
> E-mail: gnata at obs.univ-lyon1.fr
> ############################################
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>
From george.sakkis at gmail.com Wed Sep 5 10:22:46 2007
From: george.sakkis at gmail.com (George Sakkis)
Date: Wed, 5 Sep 2007 10:22:46 -0400
Subject: [Numpy-discussion] 2-d in-place operation performance vs 1-d non
in-place
Message-ID: <91ad5bf80709050722n67ef53b3l6902aa03eff95a66@mail.gmail.com>
I was surprised to see that an in-place modification of a 2-d array
turns out to be slower from the respective non-mutating operation on 1-
d arrays, although the latter creates new array objects. Here is the
benchmarking code:
import timeit
for n in 10,100,1000,10000:
setup = 'from numpy.random import random;' \
'm=random((%d,2));' \
'u1=random(%d);' \
'u2=u1.reshape((u1.size,1))' % (n,n)
timers = [timeit.Timer(stmt,setup) for stmt in
# 1-d operations; create new arrays
'a0 = m[:,0]-u1; a1 = m[:,1]-u1',
# 2-d in place operation
'm -= u2'
]
print n, [min(timer.repeat(3,1000)) for timer in timers]
And some results (Python 2.5, WinXP):
10 [0.010832382327921563, 0.0045706926438974782]
100 [0.010882668048592767, 0.021704993232380093]
1000 [0.018272154701226007, 0.19477587235249172]
10000 [0.073787590322233698, 1.9234369172618306]
So the 2-d in-place modification time grows linearly with the array
size but the 1-d operations are much more efficient, despite
allocating new arrays while doing so. What gives ?
George
From wfspotz at sandia.gov Wed Sep 5 10:45:40 2007
From: wfspotz at sandia.gov (Bill Spotz)
Date: Wed, 5 Sep 2007 08:45:40 -0600
Subject: [Numpy-discussion] Anyone have a well-tested SWIG-based C++ STL
valarray <=> numpy.array typemap to share?
In-Reply-To: <46DDF734.7070708@noaa.gov>
References: <46DDF734.7070708@noaa.gov>
Message-ID: <4CED078D-B8C7-4917-B0DE-4ED60BDFE015@sandia.gov>
I have been considering adding some C++ STL support to numpy/doc/swig/
numpy.i. Probably std::vector <=> PyArrayObject (and some
std::complex support as well). Is this what you had in mind?
On Sep 4, 2007, at 6:24 PM, David Goldsmith wrote:
> Anyone have a well-tested SWIG-based C++ STL valarray <=> numpy.array
> typemap to share? Thanks!
>
> DG
> --
> ERD/ORR/NOS/NOAA emergencyresponse/>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
** Bill Spotz **
** Sandia National Laboratories Voice: (505)845-0170 **
** P.O. Box 5800 Fax: (505)284-5451 **
** Albuquerque, NM 87185-0370 Email: wfspotz at sandia.gov **
From David.L.Goldsmith at noaa.gov Wed Sep 5 12:01:50 2007
From: David.L.Goldsmith at noaa.gov (David Goldsmith)
Date: Wed, 05 Sep 2007 09:01:50 -0700
Subject: [Numpy-discussion] Anyone have a well-tested SWIG-based C++ STL
valarray <=> numpy.array typemap to share?
In-Reply-To: <46DDF734.7070708@noaa.gov>
References: <46DDF734.7070708@noaa.gov>
Message-ID: <46DED2EE.6070400@noaa.gov>
Point of clarification: below "well-tested" = "well-use-tested," not
(necessarily) "well-unit-tested".
DG
David Goldsmith wrote:
> Anyone have a well-tested SWIG-based C++ STL valarray <=> numpy.array
> typemap to share? Thanks!
>
> DG
>
--
ERD/ORR/NOS/NOAA
From David.L.Goldsmith at noaa.gov Wed Sep 5 12:16:24 2007
From: David.L.Goldsmith at noaa.gov (David Goldsmith)
Date: Wed, 05 Sep 2007 09:16:24 -0700
Subject: [Numpy-discussion] Anyone have a well-tested SWIG-based C++ STL
valarray <=> numpy.array typemap to share?
In-Reply-To:
References: <46DDF734.7070708@noaa.gov>
<32632B72-2B1A-4AC2-AA04-288696E37A46@ster.kuleuven.be>
<46DE9C30.8020300@obs.univ-lyon1.fr>
Message-ID: <46DED658.9070206@noaa.gov>
No I hadn't - thanks! (Probably should have Google-d first, huh. :-[ )
DG
Travis Vaught wrote:
> Have you seen this?
>
> http://www.scipy.org/Cookbook/SWIG_and_NumPy
>
> Also, the numpy/doc/swig directory has the simple typemaps.
>
> Travis
>
> On Sep 5, 2007, at 7:08 AM, Xavier Gnata wrote:
>
>
>> I'm using the numpy C API (PyArray_SimpleNewFromData) to perform the
>> conversion but my code is written by hands. I would like to
>> simplify it
>> using SWIG but I also would like to see a good typemap valarray <=>
>> numpy.array :)
>>
>> Joris : Historical ones? Maybe also the fact that distutils has
>> some small pb with C++ module. To sum up : You have to compile you
>> module with the same compiler options you used to compile Python.
>> Python is coded in C so some options does not make sense in C++.
>> As a result, you get warnings at compile time (see pylab compiled
>> with gcc for instance).
>>
>>
>> Xavier
>>
>>
>>
>>
>>> A related question, just out of curiosity: is there a technical
>>> reason why Numpy has been coded in C rather than C++?
>>>
>>> Joris
>>>
>>>
>>>
>>> On 05 Sep 2007, at 02:24, David Goldsmith wrote:
>>>
>>>
>>>
>>>> Anyone have a well-tested SWIG-based C++ STL valarray <=>
>>>> numpy.array
>>>> typemap to share? Thanks!
>>>>
>>>> DG
>>>> --
>>>> ERD/ORR/NOS/NOAA >>> emergencyresponse/>
>>>> _______________________________________________
>>>> Numpy-discussion mailing list
>>>> Numpy-discussion at scipy.org
>>>> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>>>>
>>>>
>>> Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
>>>
>>> _______________________________________________
>>> Numpy-discussion mailing list
>>> Numpy-discussion at scipy.org
>>> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>>>
>>>
>>>
>> --
>> ############################################
>> Xavier Gnata
>> CRAL - Observatoire de Lyon
>> 9, avenue Charles Andr?
>> 69561 Saint Genis Laval cedex
>> Phone: +33 4 78 86 85 28
>> Fax: +33 4 78 86 83 86
>> E-mail: gnata at obs.univ-lyon1.fr
>> ############################################
>>
>> _______________________________________________
>> Numpy-discussion mailing list
>> Numpy-discussion at scipy.org
>> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>>
>>
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>
--
ERD/ORR/NOS/NOAA
From David.L.Goldsmith at noaa.gov Wed Sep 5 12:20:30 2007
From: David.L.Goldsmith at noaa.gov (David Goldsmith)
Date: Wed, 05 Sep 2007 09:20:30 -0700
Subject: [Numpy-discussion] Anyone have a well-tested SWIG-based C++ STL
valarray <=> numpy.array typemap to share?
In-Reply-To: <4CED078D-B8C7-4917-B0DE-4ED60BDFE015@sandia.gov>
References: <46DDF734.7070708@noaa.gov>
<4CED078D-B8C7-4917-B0DE-4ED60BDFE015@sandia.gov>
Message-ID: <46DED74E.4090506@noaa.gov>
Not presently, as the C++ code I need to wrap now is using the valarray
class template (largely at my behest), though I (and I imagine others)
might find this useful in the future.
DG
Bill Spotz wrote:
> I have been considering adding some C++ STL support to
> numpy/doc/swig/numpy.i. Probably std::vector <=> PyArrayObject
> (and some std::complex support as well). Is this what you had
> in mind?
>
> On Sep 4, 2007, at 6:24 PM, David Goldsmith wrote:
>
>> Anyone have a well-tested SWIG-based C++ STL valarray <=> numpy.array
>> typemap to share? Thanks!
>>
>> DG
>> --
>> ERD/ORR/NOS/NOAA
>>
>> _______________________________________________
>> Numpy-discussion mailing list
>> Numpy-discussion at scipy.org
>> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>
> ** Bill Spotz **
> ** Sandia National Laboratories Voice: (505)845-0170 **
> ** P.O. Box 5800 Fax: (505)284-5451 **
> ** Albuquerque, NM 87185-0370 Email: wfspotz at sandia.gov **
>
>
>
--
ERD/ORR/NOS/NOAA
From faltet at carabos.com Wed Sep 5 12:29:24 2007
From: faltet at carabos.com (Francesc Altet)
Date: Wed, 5 Sep 2007 18:29:24 +0200
Subject: [Numpy-discussion] 2-d in-place operation performance vs 1-d
non in-place
In-Reply-To: <91ad5bf80709050722n67ef53b3l6902aa03eff95a66@mail.gmail.com>
References: <91ad5bf80709050722n67ef53b3l6902aa03eff95a66@mail.gmail.com>
Message-ID: <200709051829.24448.faltet@carabos.com>
A Wednesday 05 September 2007, George Sakkis escrigu?:
> I was surprised to see that an in-place modification of a 2-d array
> turns out to be slower from the respective non-mutating operation on
> 1- d arrays, although the latter creates new array objects. Here is
> the benchmarking code:
>
> import timeit
>
> for n in 10,100,1000,10000:
> ? ?setup = 'from numpy.random import random;' \
> ? ? ? ? ? ?'m=random((%d,2));' \
> ? ? ? ? ? ?'u1=random(%d);' \
> ? ? ? ? ? ?'u2=u1.reshape((u1.size,1))' % (n,n)
> ? ?timers = [timeit.Timer(stmt,setup) for stmt in
> ? ? ? ?# 1-d operations; create new arrays
> ? ? ? ?'a0 = m[:,0]-u1; a1 = m[:,1]-u1',
> ? ? ? ?# 2-d in place operation
> ? ? ? ?'m -= u2'
> ? ?]
> ? ?print n, [min(timer.repeat(3,1000)) for timer in timers]
>
>
> And some results (Python 2.5, WinXP):
>
> 10 [0.010832382327921563, 0.0045706926438974782]
> 100 [0.010882668048592767, 0.021704993232380093]
> 1000 [0.018272154701226007, 0.19477587235249172]
> 10000 [0.073787590322233698, 1.9234369172618306]
>
> So the 2-d in-place modification time grows linearly with the array
> size but the 1-d operations are much more efficient, despite
> allocating new arrays while doing so. What gives ?
This seems the effect of broadcasting u2. If you were to use a
pre-computed broadcasted, you would get rid of such bottleneck:
for n in 10,100,1000,10000:
setup = 'import numpy;' \
'm=numpy.random.random((%d,2));' \
'u1=numpy.random.random(%d);' \
'u2=u1[:, numpy.newaxis];' \
'u3=numpy.array([u1,u1]).transpose()' % (n,n)
timers = [timeit.Timer(stmt,setup) for stmt in
# 1-d operations; create new arrays
'a0 = m[:,0]-u1; a1 = m[:,1]-u1',
# 2-d in place operation (using broadcasting)
'm -= u2',
# 2-d in-place operation (not forcing broadcasting)
'm -= u3'
]
print n, [min(timer.repeat(3,1000)) for timer in timers]
gives in my machine:
10 [0.03213191032409668, 0.012019872665405273, 0.0068600177764892578]
100 [0.033048152923583984, 0.06542205810546875, 0.0076580047607421875]
1000 [0.040294170379638672, 0.59892702102661133, 0.014600992202758789]
10000 [0.32667303085327148, 5.9721651077270508, 0.10261106491088867]
HTH,
--
>0,0< Francesc Altet ? ? http://www.carabos.com/
V V C?rabos Coop. V. ??Enjoy Data
"-"
From rcdailey at gmail.com Wed Sep 5 12:55:36 2007
From: rcdailey at gmail.com (Robert Dailey)
Date: Wed, 5 Sep 2007 11:55:36 -0500
Subject: [Numpy-discussion] Vector magnitude?
Message-ID: <496954360709050955t64ad9edclc18b0fa5d71acb9b@mail.gmail.com>
Hi,
I have two questions:
1) Is there any way in numpy to represent vectors? Currently I'm using
'array' for vectors.
2) Is there a way to calculate the magnitude (length) of a vector in numpy?
Thanks.
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
From broman at spawar.navy.mil Wed Sep 5 13:13:50 2007
From: broman at spawar.navy.mil (Vincent Broman)
Date: Wed, 5 Sep 2007 10:13:50 -0700
Subject: [Numpy-discussion] numpy build fails on powerpc ydl
In-Reply-To:
References:
Message-ID: <200709051013.50786@b00d61a8cecf8b2266f81358fd170621.navy.mil>
Harris asked about long doubles.
On my YDL, SIZEOF_LONG_DOUBLE and sizeof( long double) were both 8.
Vincent Broman
From Chris.Barker at noaa.gov Wed Sep 5 13:19:43 2007
From: Chris.Barker at noaa.gov (Christopher Barker)
Date: Wed, 05 Sep 2007 10:19:43 -0700
Subject: [Numpy-discussion] Anyone have a well-tested SWIG-based C++ STL
valarray <=> numpy.array typemap to share?
In-Reply-To: <4CED078D-B8C7-4917-B0DE-4ED60BDFE015@sandia.gov>
References: <46DDF734.7070708@noaa.gov>
<4CED078D-B8C7-4917-B0DE-4ED60BDFE015@sandia.gov>
Message-ID: <46DEE52F.8050000@noaa.gov>
Bill Spotz wrote:
> I have been considering adding some C++ STL support to numpy/doc/swig/
> numpy.i. Probably std::vector <=> PyArrayObject (and some
> std::complex support as well). Is this what you had in mind?
well, std::valarray is not the same as std::vector, though there are
similarities, so the example would be helpful.
Of greatest concern to me is the memory management issue -- it would be
nice to be able to share the data black between the valarray and the
numpy array (rather than copying back and forth), but as the both
valarrays and vectors are re-sizeable, that might get tricky. I'm
assuming that you can get the pointer to the data block from both, but
it might change on you.
If you solve that for std::vector, the solution is likely to be similar
for std:valarray. (I hope).
David Goldsmith wrote:
> Point of clarification: below "well-tested" = "well-use-tested," not
> (necessarily) "well-unit-tested".
Of course, the better tested the better, but anything is probably better
than starting from scratch!
Travis Vaught wrote:
> Have you seen this?
>
> http://www.scipy.org/Cookbook/SWIG_and_NumPy
>
> Also, the numpy/doc/swig directory has the simple typemaps.
Looked at both, and they are a great starting point, but only deal with
plain old C arrays, as far as I've seen.
Unless someone speaks up, it sounds like it's not been done, but there
are at least a few of us that are interested, so maybe we can start a
collaboration.
-Chris
By the way, David G. and I are working on the same project, so we're
kind of like one person....
--
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
From robert.kern at gmail.com Wed Sep 5 13:18:57 2007
From: robert.kern at gmail.com (Robert Kern)
Date: Wed, 05 Sep 2007 12:18:57 -0500
Subject: [Numpy-discussion] Use my own data type with NumPy
In-Reply-To: <46DE5F5D.8060204@web.de>
References: <46DE5F5D.8060204@web.de>
Message-ID: <46DEE501.7080609@gmail.com>
G?nter Dannoritzer wrote:
> Hi,
>
> I am trying to use my own data type with NumPy, but get some funny
> result when creating a NumPy array with it.
>
> My data type is indexable and sliceable and what happens now is when I
> create an array, NumPy is adding the instance as a list of the indexed
> values. How can I force NumPy to handle my data type as an 'Object' and
> use the value of __repr__ to display in the array?
>
> Maybe I am handling that too simple, as I had another data type before,
> -- not indexable though --, that just works fine with NumPy. Do I have
> to worry about a dtype object with my new data type or how can I use my
> new data type with NumPy?
I presume that by "new data type" you mean some Python class that you've made,
not some C data type.
Creating an object array can be a bit tricky. array() has to make some guesses
as to which objects are containers and which are elements. Sometimes, it guesses
wrong (at least measured against what the user actually desired). The most
robust way to construct an object array is to create an empty() object array of
the shape you want, and then assign the contents.
In [1]: from numpy import *
In [2]: a = empty((3,), dtype=object)
In [3]: a
Out[3]: array([None, None, None], dtype=object)
In [4]: a[:] = [[1], [2], [3]]
In [5]: a
Out[5]: array([[1], [2], [3]], dtype=object)
In [6]: a.shape
Out[6]: (3,)
--
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
From gael.varoquaux at normalesup.org Wed Sep 5 13:20:45 2007
From: gael.varoquaux at normalesup.org (Gael Varoquaux)
Date: Wed, 5 Sep 2007 19:20:45 +0200
Subject: [Numpy-discussion] Vector magnitude?
In-Reply-To: <496954360709050955t64ad9edclc18b0fa5d71acb9b@mail.gmail.com>
References: <496954360709050955t64ad9edclc18b0fa5d71acb9b@mail.gmail.com>
Message-ID: <20070905172044.GI15889@clipper.ens.fr>
On Wed, Sep 05, 2007 at 11:55:36AM -0500, Robert Dailey wrote:
> 1) Is there any way in numpy to represent vectors? Currently I'm using
> 'array' for vectors.
What do you call a vector ? For me a vector is an element of an linear
space. In numerical methods what is comonly called a vector is a 1D array
of arbitrary length. I suspect you mean something different, given your
question
> 2) Is there a way to calculate the magnitude (length) of a vector in
> numpy?
I am being dumb. What do you mean by magnitude (or length) ? Maybe it is
just because I am not a native English speaker. If you are talking about
the euclidien norm, I don't know a built in way of doing it, but it is
very easy to define a norm function:
import numpy as N
a = N.arange(3)
norm = lambda x: N.sqrt(N.square(x).sum())
norm(a)
-> 2.2360679775
From matthieu.brucher at gmail.com Wed Sep 5 13:21:05 2007
From: matthieu.brucher at gmail.com (Matthieu Brucher)
Date: Wed, 5 Sep 2007 19:21:05 +0200
Subject: [Numpy-discussion] Vector magnitude?
In-Reply-To: <496954360709050955t64ad9edclc18b0fa5d71acb9b@mail.gmail.com>
References: <496954360709050955t64ad9edclc18b0fa5d71acb9b@mail.gmail.com>
Message-ID:
2007/9/5, Robert Dailey :
>
> Hi,
>
> I have two questions:
>
> 1) Is there any way in numpy to represent vectors? Currently I'm using
> 'array' for vectors.
A vector is an array with one dimension, it's OK. You could use a matrix of
dimension 1xn or nx1 as well.
2) Is there a way to calculate the magnitude (length) of a vector in numpy?
Yes, len(a)
Matthieu
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
From Chris.Barker at noaa.gov Wed Sep 5 13:24:27 2007
From: Chris.Barker at noaa.gov (Christopher Barker)
Date: Wed, 05 Sep 2007 10:24:27 -0700
Subject: [Numpy-discussion] Use my own data type with NumPy
In-Reply-To: <46DE5F5D.8060204@web.de>
References: <46DE5F5D.8060204@web.de>
Message-ID: <46DEE64B.4010100@noaa.gov>
G?nter Dannoritzer wrote:
> My data type is indexable and sliceable and what happens now is when I
> create an array, NumPy is adding the instance as a list of the indexed
> values. How can I force NumPy to handle my data type as an 'Object'
Object arrays are tricky, 'cause it's hard for numpy to know how you
want to unpack arbitrary objects.
The solution is to make an empty object array first, then populate it.
For example:
>>> import numpy as N
>>> MyData = [[1,2,3],
... [4,5,6],
... [7,8,9]]
This is a list or lists, so numpy.array would unpack it into a 2-d array:
>>> N.array(MyData)
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
However, let's say what I want is a 1-d object array. I create the
object array empty:
>>> OA = N.empty((3,), dtype=N.object)
>>> OA
array([None, None, None], dtype=object)
Then populate it:
>>> OA[:] = MyData
>>> OA
array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=object)
Does that help?
-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
From rowen at cesmail.net Wed Sep 5 13:31:46 2007
From: rowen at cesmail.net (Russell E. Owen)
Date: Wed, 05 Sep 2007 10:31:46 -0700
Subject: [Numpy-discussion] Anyone have a well-tested SWIG-based C++ STL
valarray <=> numpy.array typemap to share?
References: <46DDF734.7070708@noaa.gov>
<4CED078D-B8C7-4917-B0DE-4ED60BDFE015@sandia.gov>
Message-ID:
In article <4CED078D-B8C7-4917-B0DE-4ED60BDFE015 at sandia.gov>,
"Bill Spotz" wrote:
> I have been considering adding some C++ STL support to numpy/doc/swig/
> numpy.i. Probably std::vector <=> PyArrayObject (and some
> std::complex support as well). Is this what you had in mind?
That sounds very useful, but how did you get it to work? std::vectors
are resizable and numpy arrays are not. However, much of the time I want
std::vectors of a particular size -- in which case numpy would be a
great match.
(Speaking of which, do you happen to know of any good std::vector
variant that has fixed length?)
-- Russell
From Chris.Barker at noaa.gov Wed Sep 5 13:38:04 2007
From: Chris.Barker at noaa.gov (Christopher Barker)
Date: Wed, 05 Sep 2007 10:38:04 -0700
Subject: [Numpy-discussion] Anyone have a well-tested SWIG-based C++ STL
valarray <=> numpy.array typemap to share?
In-Reply-To: <32632B72-2B1A-4AC2-AA04-288696E37A46@ster.kuleuven.be>
References: <46DDF734.7070708@noaa.gov>
<32632B72-2B1A-4AC2-AA04-288696E37A46@ster.kuleuven.be>
Message-ID: <46DEE97C.5070008@noaa.gov>
Joris De Ridder wrote:
> A related question, just out of curiosity: is there a technical
> reason why Numpy has been coded in C rather than C++?
There was a fair bit of discussion about this back when the numarray
project started, which was a re-implementation of the original Numeric.
IIRC, one of the drivers was that C++ support was still pretty
inconsistent across compilers and OSs, particularly if you wanted to
really get the advantages of C++, by using templates and the like.
It was considered very important that the numpy code base be very portable.
C++ compilers have gotten better an more standards compliant, but as a
recent thread shows, folks still want to build numpy with older
compilers and libs, so the reasoning still holds.
Too bad, in a way, I suspect a well-designed C++ numpy could make it
easier to write compiled extensions, which would be pretty nice.
Of course, it should be possible to write C++ wrappers around the core
ND-array object, if anyone wants to take that on!
-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
From charlesr.harris at gmail.com Wed Sep 5 13:50:14 2007
From: charlesr.harris at gmail.com (Charles R Harris)
Date: Wed, 5 Sep 2007 11:50:14 -0600
Subject: [Numpy-discussion] numpy build fails on powerpc ydl
In-Reply-To: <200709051013.50786@b00d61a8cecf8b2266f81358fd170621.navy.mil>
References:
<200709051013.50786@b00d61a8cecf8b2266f81358fd170621.navy.mil>
Message-ID:
On 9/5/07, Vincent Broman wrote:
>
> Harris asked about long doubles.
> On my YDL, SIZEOF_LONG_DOUBLE and sizeof( long double) were both 8.
Hmm, so long doubles are just doubles, I kinda suspected that. I'm not
really familiar with this code, but what happens if you go to
numpy/numpy/core/include/numpy/ndarrayobject.h and change the double in line
84 to long double?
Chuck.
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
From robert.kern at gmail.com Wed Sep 5 13:53:26 2007
From: robert.kern at gmail.com (Robert Kern)
Date: Wed, 05 Sep 2007 12:53:26 -0500
Subject: [Numpy-discussion] Improving bug triage in trac ?
In-Reply-To: <46DE8C77.10808@ar.media.kyoto-u.ac.jp>
References: <46DE8C77.10808@ar.media.kyoto-u.ac.jp>
Message-ID: <46DEED16.9010802@gmail.com>
David Cournapeau wrote:
> Hi there,
>
> I am personnally a bit annoyed by the way trac handle bug reports,
> and would like to know if there is space for improvement. I do not know
> much about bug tracking systems, so maybe I just don't know how to use
> it, though. The main thing I dislike is the status of tickets and
> reports. In particular:
> - I don't know how other feel, but I am rather confused by the meta
> data of a ticket, and do not find them really useful from a developer
> point of view. It would be quite helpful to have information whether the
> bug is confirmed or not, another one which says wether a patch is
> available or not. This would make bug triage much easier, IMHO. This
> should be possible, since I have seen some trac installation with such
> features (wordpad trac, for example).
Did you mean "WordPress Trac"? (luckily, the first Google hit for "wordpad trac"
happens to be the WordPress Trac)
They seem to do this with a standard lexicon of keywords and custom queries.
http://codex.wordpress.org/Reporting_Bugs#Trac_Keywords
> - This one maybe a bit more difficult to implement I guess (I don't
> know anything about trac internals): I find the general view of bugs for
> a given repository really helpful in launchpad, in perticular, you can
> easily view the percentage of bugs wrt their status (eg 30 % bugs
> unconfirmed, etc...); see for example https://bugs.launchpad.net/+about.
> This gives a pretty good idea of what needs to be done for a particular
> release.
> How do other people feel about those suggestions ?
We can certainly add custom fields or start using keywords like WordPress. We
can't change the status field (new, assigned, closed, reopened), though. That
workflow is hardcoded in Trac 0.10, which we are currently using. Getting the
summary (30% unconfirmed) may be a bit more difficult. For more reading:
http://scipy.org/scipy/scipy/wiki/TracReports
http://scipy.org/scipy/scipy/wiki/TracQuery
--
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
From wfspotz at sandia.gov Wed Sep 5 14:04:37 2007
From: wfspotz at sandia.gov (Bill Spotz)
Date: Wed, 5 Sep 2007 12:04:37 -0600
Subject: [Numpy-discussion] Anyone have a well-tested SWIG-based C++ STL
valarray <=> numpy.array typemap to share?
In-Reply-To: <46DEE52F.8050000@noaa.gov>
References: <46DDF734.7070708@noaa.gov>
<4CED078D-B8C7-4917-B0DE-4ED60BDFE015@sandia.gov>
<46DEE52F.8050000@noaa.gov>
Message-ID: <609FEB05-86EB-4FE2-867E-4A1C84105951@sandia.gov>
On Sep 5, 2007, at 11:19 AM, Christopher Barker wrote:
> Bill Spotz wrote:
>> I have been considering adding some C++ STL support to numpy/doc/
>> swig/
>> numpy.i. Probably std::vector <=> PyArrayObject (and some
>> std::complex support as well). Is this what you had in mind?
>
> well, std::valarray is not the same as std::vector, though there are
> similarities, so the example would be helpful.
Ah, silly me. Back when I first learned C++, valarray wasn't around
yet (or at least it wasn't taught to me), and it is not in the
(clearly outdated) references I use. But it is a more logical choice
than vector.
> Of greatest concern to me is the memory management issue -- it
> would be
> nice to be able to share the data black between the valarray and the
> numpy array (rather than copying back and forth), but as the both
> valarrays and vectors are re-sizeable, that might get tricky. I'm
> assuming that you can get the pointer to the data block from both, but
> it might change on you. If you solve that for std::vector, the
> solution
> is likely to be similar for std:valarray. (I hope).
Yes, this resizing memory management issue is the main reason I
haven't tried to implement it in numpy.i yet.
A possibly better solution would be to develop a class that inherits
from std::valarray but also implements the array interface
attributes (these attributes would have to be dynamic, in that they
check the std::valarray attributes when accessed rather than storing
copies). We could then write typemaps that utilize the array
interface. So pure input arguments could be numpy.ndarrays (or any
reasonable sequence, really), but output arrays would be a wrapped
version of this new class. (Which would behave both like
std::valarrays and like numpy.ndarrays. I think...)
** Bill Spotz **
** Sandia National Laboratories Voice: (505)845-0170 **
** P.O. Box 5800 Fax: (505)284-5451 **
** Albuquerque, NM 87185-0370 Email: wfspotz at sandia.gov **
From wfspotz at sandia.gov Wed Sep 5 14:07:31 2007
From: wfspotz at sandia.gov (Bill Spotz)
Date: Wed, 5 Sep 2007 12:07:31 -0600
Subject: [Numpy-discussion] Anyone have a well-tested SWIG-based C++ STL
valarray <=> numpy.array typemap to share?
In-Reply-To: <46DEE97C.5070008@noaa.gov>
References: <46DDF734.7070708@noaa.gov>
<32632B72-2B1A-4AC2-AA04-288696E37A46@ster.kuleuven.be>
<46DEE97C.5070008@noaa.gov>
Message-ID:
On Sep 5, 2007, at 11:38 AM, Christopher Barker wrote:
> Of course, it should be possible to write C++ wrappers around the core
> ND-array object, if anyone wants to take that on!
boost::python has done this for Numeric, but last I checked, they
have not upgraded to numpy.
** Bill Spotz **
** Sandia National Laboratories Voice: (505)845-0170 **
** P.O. Box 5800 Fax: (505)284-5451 **
** Albuquerque, NM 87185-0370 Email: wfspotz at sandia.gov **
From rcdailey at gmail.com Wed Sep 5 14:40:19 2007
From: rcdailey at gmail.com (Robert Dailey)
Date: Wed, 5 Sep 2007 13:40:19 -0500
Subject: [Numpy-discussion] Vector magnitude?
In-Reply-To:
References: <496954360709050955t64ad9edclc18b0fa5d71acb9b@mail.gmail.com>
Message-ID: <496954360709051140k40f1b5cen7cdfafd9a9913873@mail.gmail.com>
Thanks for your response.
I was not able to find len() in the numpy documentation at the following
link:
http://www.scipy.org/doc/numpy_api_docs/namespace_index.html
Perhaps I'm looking in the wrong location?
On 9/5/07, Matthieu Brucher wrote:
>
>
>
> 2007/9/5, Robert Dailey :
> >
> > Hi,
> >
> > I have two questions:
> >
> > 1) Is there any way in numpy to represent vectors? Currently I'm using
> > 'array' for vectors.
>
>
>
> A vector is an array with one dimension, it's OK. You could use a matrix
> of dimension 1xn or nx1 as well.
>
>
> 2) Is there a way to calculate the magnitude (length) of a vector in
> > numpy?
>
>
> Yes, len(a)
>
> Matthieu
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
From Chris.Barker at noaa.gov Wed Sep 5 14:45:57 2007
From: Chris.Barker at noaa.gov (Christopher Barker)
Date: Wed, 05 Sep 2007 11:45:57 -0700
Subject: [Numpy-discussion] Anyone have a well-tested SWIG-based C++ STL
valarray <=> numpy.array typemap to share?
In-Reply-To: <609FEB05-86EB-4FE2-867E-4A1C84105951@sandia.gov>
References: <46DDF734.7070708@noaa.gov>
<4CED078D-B8C7-4917-B0DE-4ED60BDFE015@sandia.gov>
<46DEE52F.8050000@noaa.gov>
<609FEB05-86EB-4FE2-867E-4A1C84105951@sandia.gov>
Message-ID: <46DEF965.1000308@noaa.gov>
Bill Spotz wrote:
> Yes, this resizing memory management issue is the main reason I haven't
> tried to implement it in numpy.i yet.
>
> A possibly better solution would be to develop a class that inherits
> from std::valarray but also implements the array interface
> attributes (these attributes would have to be dynamic, in that they
> check the std::valarray attributes when accessed rather than storing
> copies).
I like that, though it's way over my head to implement.
However, I'm beginning to have my doubts about valarrays. I'm reading:
Josuttis, Nicolai M. 1999. "The C+= Standard Library: A Tutorial and
Reference"
It's 8 years old now, but he writes:
"The valarray classes were not designed very well. In fact, nobody tried
to determine if the specification worked"
He goes on to suggest that Blitz++ might have more of a future. (though
it looks like he's involved with the Boost project now)
He also points out some major flaws in the text. In reading, I also see
that while valarrays can be used for multidimensional arrays, the
semantics are pretty ugly.
However, he also says: "In principle...you can change their size.
However, changing the size of a valarray is provided only to make a
two-step initialization (creating and setting the size)"
So maybe the memory re-allocation is such an issue.
Does anyone know the status of support for valarrays now?
Is there another alternative? At the moment, all we need is a one-d
fixed size array. There is Boost::array (and MultiArray), but Boost has
always seemed like a big, ugly, hard to build dependency. Can you just
grab the code for some of these small pieces by themselves?
-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
From zpincus at stanford.edu Wed Sep 5 14:49:45 2007
From: zpincus at stanford.edu (Zachary Pincus)
Date: Wed, 5 Sep 2007 14:49:45 -0400
Subject: [Numpy-discussion] Vector magnitude?
In-Reply-To: <496954360709051140k40f1b5cen7cdfafd9a9913873@mail.gmail.com>
References: <496954360709050955t64ad9edclc18b0fa5d71acb9b@mail.gmail.com>
<496954360709051140k40f1b5cen7cdfafd9a9913873@mail.gmail.com>
Message-ID: <445A7F45-C4A7-41C4-8FDC-A7FBD8AA2D53@stanford.edu>
Hello,
'len' is a (pretty basic) python builtin function for getting the
length of anything with a list-like interface. (Or more generally,
getting the size of anything that is sized, e.g. a set or dictionary.)
Numpy arrays offer a list-like interface allowing you to iterate
along their first dimension, etc. (*) Thus, len(numpy_array) is
equivalent to numpy_array.shape[0], which is the number of elements
along the first dimension of the array.
Zach
(*) For example, this is useful if you've got numerous data vectors
packed into an array along the first dimension, and want to iterate
across the different vectors.
a = numpy.ones((number_of_data_elements, size_of_data_element))
for element in a:
# element is a 1-D array with a length of 'size_of_data_element'
Note further that this works even if your data elements are multi-
dimensional; i.e. the above works the same if:
element_shape = (x,y,z)
a = numpy.ones((number_of_data_elements,)+element_shape)
for element in a:
# element is a 3-D array with a shape of (x,y,z)
On Sep 5, 2007, at 2:40 PM, Robert Dailey wrote:
> Thanks for your response.
>
> I was not able to find len() in the numpy documentation at the
> following link:
> http://www.scipy.org/doc/numpy_api_docs/namespace_index.html
>
> Perhaps I'm looking in the wrong location?
>
> On 9/5/07, Matthieu Brucher wrote:
>
> 2007/9/5, Robert Dailey < rcdailey at gmail.com>: Hi,
>
> I have two questions:
>
> 1) Is there any way in numpy to represent vectors? Currently I'm
> using 'array' for vectors.
>
>
> A vector is an array with one dimension, it's OK. You could use a
> matrix of dimension 1xn or nx1 as well.
>
>
> 2) Is there a way to calculate the magnitude (length) of a vector
> in numpy?
>
> Yes, len(a)
>
> Matthieu
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
From matthieu.brucher at gmail.com Wed Sep 5 14:50:13 2007
From: matthieu.brucher at gmail.com (Matthieu Brucher)
Date: Wed, 5 Sep 2007 20:50:13 +0200
Subject: [Numpy-discussion] Vector magnitude?
In-Reply-To: <496954360709051140k40f1b5cen7cdfafd9a9913873@mail.gmail.com>
References: <496954360709050955t64ad9edclc18b0fa5d71acb9b@mail.gmail.com>
<496954360709051140k40f1b5cen7cdfafd9a9913873@mail.gmail.com>
Message-ID:
2007/9/5, Robert Dailey :
>
> Thanks for your response.
>
> I was not able to find len() in the numpy documentation at the following
> link:
> http://www.scipy.org/doc/numpy_api_docs/namespace_index.html
>
> Perhaps I'm looking in the wrong location?
Yes, it's a Python function ;)
Matthieu
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
From rcdailey at gmail.com Wed Sep 5 14:52:53 2007
From: rcdailey at gmail.com (Robert Dailey)
Date: Wed, 5 Sep 2007 13:52:53 -0500
Subject: [Numpy-discussion] Vector magnitude?
In-Reply-To: <496954360709051140k40f1b5cen7cdfafd9a9913873@mail.gmail.com>
References: <496954360709050955t64ad9edclc18b0fa5d71acb9b@mail.gmail.com>
<496954360709051140k40f1b5cen7cdfafd9a9913873@mail.gmail.com>
Message-ID: <496954360709051152v30ed8ff6td831ba5922ed469e@mail.gmail.com>
Oh I think I get it.
You mean the built-in len() function? This isn't what I am looking for.
len() returns the number of components in the vector (e.g. whether it is a
2D, 3D, etc vector). I found that magnitude can be calculated using hypot()
in the math module that comes with python. However, this method only appears
to work with 2D vectors. And yes, by magnitude I mean euclidean norm:
sqrt( x*x + y*y ) = magnitude (length) of a vector
On 9/5/07, Robert Dailey wrote:
>
> Thanks for your response.
>
> I was not able to find len() in the numpy documentation at the following
> link:
> http://www.scipy.org/doc/numpy_api_docs/namespace_index.html
>
> Perhaps I'm looking in the wrong location?
>
> On 9/5/07, Matthieu Brucher wrote:
>
> >
> >
> > 2007/9/5, Robert Dailey < rcdailey at gmail.com>:
> > >
> > > Hi,
> > >
> > > I have two questions:
> > >
> > > 1) Is there any way in numpy to represent vectors? Currently I'm using
> > > 'array' for vectors.
> >
> >
> >
> > A vector is an array with one dimension, it's OK. You could use a matrix
> > of dimension 1xn or nx1 as well.
> >
> >
> > 2) Is there a way to calculate the magnitude (length) of a vector in
> > > numpy?
> >
> >
> > Yes, len(a)
> >
> > Matthieu
> >
> > _______________________________________________
> > Numpy-discussion mailing list
> > Numpy-discussion at scipy.org
> > http://projects.scipy.org/mailman/listinfo/numpy-discussion
> >
> >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
From matthieu.brucher at gmail.com Wed Sep 5 14:53:38 2007
From: matthieu.brucher at gmail.com (Matthieu Brucher)
Date: Wed, 5 Sep 2007 20:53:38 +0200
Subject: [Numpy-discussion] Anyone have a well-tested SWIG-based C++ STL
valarray <=> numpy.array typemap to share?
In-Reply-To: <46DEF965.1000308@noaa.gov>
References: <46DDF734.7070708@noaa.gov>
<4CED078D-B8C7-4917-B0DE-4ED60BDFE015@sandia.gov>
<46DEE52F.8050000@noaa.gov>
<609FEB05-86EB-4FE2-867E-4A1C84105951@sandia.gov>
<46DEF965.1000308@noaa.gov>
Message-ID:
>
> He goes on to suggest that Blitz++ might have more of a future. (though
> it looks like he's involved with the Boost project now)
Blitz++ is more or less avandoned. It uses indexes than can be not-portable
between 32bits platforms and 64bits ones.
Is there another alternative? At the moment, all we need is a one-d
> fixed size array. There is Boost::array (and MultiArray), but Boost has
> always seemed like a big, ugly, hard to build dependency. Can you just
> grab the code for some of these small pieces by themselves?
>
The Boost.Array is a fixed-size array, determined at compile-time, not
interesting there, I suppose. Multiarrays are what you're looking for.
Besides, it is not needed to build Boost to use them (Boost needs
compilation for some libraries like Regex, program_options or Python).
Matthieu
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
From wbaxter at gmail.com Wed Sep 5 14:55:12 2007
From: wbaxter at gmail.com (Bill Baxter)
Date: Thu, 6 Sep 2007 03:55:12 +0900
Subject: [Numpy-discussion] Anyone have a well-tested SWIG-based C++ STL
valarray <=> numpy.array typemap to share?
In-Reply-To: <46DEF965.1000308@noaa.gov>
References: <46DDF734.7070708@noaa.gov>
<4CED078D-B8C7-4917-B0DE-4ED60BDFE015@sandia.gov>
<46DEE52F.8050000@noaa.gov>
<609FEB05-86EB-4FE2-867E-4A1C84105951@sandia.gov>
<46DEF965.1000308@noaa.gov>
Message-ID:
On 9/6/07, Christopher Barker wrote:
> Bill Spotz wrote:
> However, I'm beginning to have my doubts about valarrays. I'm reading:
>
> Josuttis, Nicolai M. 1999. "The C+= Standard Library: A Tutorial and
> Reference"
>
> It's 8 years old now, but he writes:
>
> "The valarray classes were not designed very well. In fact, nobody tried
> to determine if the specification worked"
I've never read that particular book, but I've also read somewhere
that valarray is pretty useless for serious work. The timeframe is
probably about the same, though -- I think the last time I used
valarray was about 7-8 years ago.
--bb
From robert.kern at gmail.com Wed Sep 5 14:57:17 2007
From: robert.kern at gmail.com (Robert Kern)
Date: Wed, 05 Sep 2007 13:57:17 -0500
Subject: [Numpy-discussion] Vector magnitude?
In-Reply-To: <496954360709051140k40f1b5cen7cdfafd9a9913873@mail.gmail.com>
References: <496954360709050955t64ad9edclc18b0fa5d71acb9b@mail.gmail.com>
<496954360709051140k40f1b5cen7cdfafd9a9913873@mail.gmail.com>
Message-ID: <46DEFC0D.8040207@gmail.com>
Robert Dailey wrote:
> Thanks for your response.
>
> I was not able to find len() in the numpy documentation at the following
> link:
> http://www.scipy.org/doc/numpy_api_docs/namespace_index.html
>
>
> Perhaps I'm looking in the wrong location?
It's a Python builtin function, but it doesn't do what you want. It returns the
number of elements in a sequence (any sequence, not just arrays) not the
magnitude of the vector.
Besides constructing the Euclidean norm itself (as shown by others here), you
can also use numpy.linalg.norm() to calculate any of several different norms of
a vector or a matrix:
In [7]: numpy.linalg.norm?
Type: function
Base Class:
Namespace: Interactive
File:
/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/numpy-1.0.4.dev4025-py2.5-macosx-10.3-fat.egg/numpy/linalg/linalg.py
Definition: numpy.linalg.norm(x, ord=None)
Docstring:
norm(x, ord=None) -> n
Matrix or vector norm.
Inputs:
x -- a rank-1 (vector) or rank-2 (matrix) array
ord -- the order of the norm.
Comments:
For arrays of any rank, if ord is None:
calculate the square norm (Euclidean norm for vectors,
Frobenius norm for matrices)
For vectors ord can be any real number including Inf or -Inf.
ord = Inf, computes the maximum of the magnitudes
ord = -Inf, computes minimum of the magnitudes
ord is finite, computes sum(abs(x)**ord,axis=0)**(1.0/ord)
For matrices ord can only be one of the following values:
ord = 2 computes the largest singular value
ord = -2 computes the smallest singular value
ord = 1 computes the largest column sum of absolute values
ord = -1 computes the smallest column sum of absolute values
ord = Inf computes the largest row sum of absolute values
ord = -Inf computes the smallest row sum of absolute values
ord = 'fro' computes the frobenius norm sqrt(sum(diag(X.H * X),axis=0))
For values ord < 0, the result is, strictly speaking, not a
mathematical 'norm', but it may still be useful for numerical purposes.
--
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
From lbolla at gmail.com Wed Sep 5 14:59:17 2007
From: lbolla at gmail.com (lorenzo bolla)
Date: Wed, 5 Sep 2007 20:59:17 +0200
Subject: [Numpy-discussion] Vector magnitude?
In-Reply-To: <496954360709051152v30ed8ff6td831ba5922ed469e@mail.gmail.com>
References: <496954360709050955t64ad9edclc18b0fa5d71acb9b@mail.gmail.com>
<496954360709051140k40f1b5cen7cdfafd9a9913873@mail.gmail.com>
<496954360709051152v30ed8ff6td831ba5922ed469e@mail.gmail.com>
Message-ID: <80c99e790709051159ie84cc7wdf79205119e933e6@mail.gmail.com>
maybe numpy.vdot is good for you.
In [3]: x = numpy.random.rand(4)
In [4]: x
Out[4]: array([ 0.45426898, 0.22369238, 0.98731244, 0.7758774 ])
In [5]: numpy.sqrt(numpy.vdot(x,x))
Out[5]: 1.35394615117
hth,
lorenzo
On 9/5/07, Robert Dailey wrote:
>
> Oh I think I get it.
>
> You mean the built-in len() function? This isn't what I am looking for.
> len() returns the number of components in the vector (e.g. whether it is a
> 2D, 3D, etc vector). I found that magnitude can be calculated using hypot()
> in the math module that comes with python. However, this method only appears
> to work with 2D vectors. And yes, by magnitude I mean euclidean norm:
>
> sqrt( x*x + y*y ) = magnitude (length) of a vector
>
> On 9/5/07, Robert Dailey wrote:
> >
> > Thanks for your response.
> >
> > I was not able to find len() in the numpy documentation at the following
> > link:
> > http://www.scipy.org/doc/numpy_api_docs/namespace_index.html
> >
> > Perhaps I'm looking in the wrong location?
> >
> > On 9/5/07, Matthieu Brucher < matthieu.brucher at gmail.com > wrote:
> >
> > >
> > >
> > > 2007/9/5, Robert Dailey < rcdailey at gmail.com>:
> > > >
> > > > Hi,
> > > >
> > > > I have two questions:
> > > >
> > > > 1) Is there any way in numpy to represent vectors? Currently I'm
> > > > using 'array' for vectors.
> > >
> > >
> > >
> > > A vector is an array with one dimension, it's OK. You could use a
> > > matrix of dimension 1xn or nx1 as well.
> > >
> > >
> > > 2) Is there a way to calculate the magnitude (length) of a vector in
> > > > numpy?
> > >
> > >
> > > Yes, len(a)
> > >
> > > Matthieu
> > >
> > > _______________________________________________
> > > Numpy-discussion mailing list
> > > Numpy-discussion at scipy.org
> > > http://projects.scipy.org/mailman/listinfo/numpy-discussion
> > >
> > >
> >
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
From lou_boog2000 at yahoo.com Wed Sep 5 15:02:58 2007
From: lou_boog2000 at yahoo.com (Lou Pecora)
Date: Wed, 5 Sep 2007 12:02:58 -0700 (PDT)
Subject: [Numpy-discussion] Vector magnitude?
In-Reply-To: <46DEFC0D.8040207@gmail.com>
Message-ID: <870179.54459.qm@web34409.mail.mud.yahoo.com>
--- Robert Kern wrote:
>
> Besides constructing the Euclidean norm itself (as
> shown by others here), you
> can also use numpy.linalg.norm() to calculate any of
> several different norms of
> a vector or a matrix:
Right. linalg.norm also gives the proper magnitude of
complex vectors
-- Lou Pecora, my views are my own.
---------------
Great spirits have always encountered violent opposition from mediocre minds.
-Albert Einstein
____________________________________________________________________________________
Moody friends. Drama queens. Your life? Nope! - their life, your story. Play Sims Stories at Yahoo! Games.
http://sims.yahoo.com/
From rcdailey at gmail.com Wed Sep 5 15:05:15 2007
From: rcdailey at gmail.com (Robert Dailey)
Date: Wed, 5 Sep 2007 14:05:15 -0500
Subject: [Numpy-discussion] How-to: Uniform vector scale
Message-ID: <496954360709051205g60433bfcndb5144c7bb1a3867@mail.gmail.com>
Hi,
I have a scalar value S. I want to perform the following math on vectors A
and B (both of type array):
A + B * S
By order of operations, B * S should be done first. This is a vector
multiplied by a real number and should be valid. However, the interpreter
outputs:
"ValueError: shape mismatch: objects cannot be broadcast to a single
shape"
I am not sure how I am supposed to multiply a vector with a scalar value.
For example:
array([5,2]) * 2 = [10,4]
The above should happen. However, I get the error message instead. Any
ideas? Thanks.
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
From robert.kern at gmail.com Wed Sep 5 15:18:24 2007
From: robert.kern at gmail.com (Robert Kern)
Date: Wed, 05 Sep 2007 14:18:24 -0500
Subject: [Numpy-discussion] How-to: Uniform vector scale
In-Reply-To: <496954360709051205g60433bfcndb5144c7bb1a3867@mail.gmail.com>
References: <496954360709051205g60433bfcndb5144c7bb1a3867@mail.gmail.com>
Message-ID: <46DF0100.7080905@gmail.com>
Robert Dailey wrote:
> Hi,
>
> I have a scalar value S. I want to perform the following math on vectors
> A and B (both of type array):
>
> A + B * S
>
> By order of operations, B * S should be done first. This is a vector
> multiplied by a real number and should be valid. However, the
> interpreter outputs:
> "ValueError: shape mismatch: objects cannot be broadcast to a single
> shape"
Most likely, the + is failing, not the multiplication. However, I can't tell
from your description. It is much more helpful for us to help you if you give us
a self-contained, small example that demonstrates the problem along with the
output (copy-and-paste, no summaries) and the output that you expected.
> I am not sure how I am supposed to multiply a vector with a scalar
> value. For example:
>
> array([5,2]) * 2 = [10,4]
>
> The above should happen. However, I get the error message instead. Any
> ideas? Thanks.
In [3]: from numpy import *
In [4]: array([5,2]) * 2
Out[4]: array([10, 4])
--
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
From dannoritzer at web.de Wed Sep 5 15:19:58 2007
From: dannoritzer at web.de (=?ISO-8859-1?Q?G=FCnter_Dannoritzer?=)
Date: Wed, 05 Sep 2007 21:19:58 +0200
Subject: [Numpy-discussion] Use my own data type with NumPy
In-Reply-To: <46DEE64B.4010100@noaa.gov>
References: <46DE5F5D.8060204@web.de> <46DEE64B.4010100@noaa.gov>
Message-ID: <46DF015E.3020502@web.de>
Christopher Barker wrote:
>
[...]
> The solution is to make an empty object array first, then populate it.
[...]
>
> Does that help?
Robert, Chris, thanks for that explanation. I understand that now.
The purpose of my (Python) class is to model a fixed point data type. So
I can specify how many bits are used for integer and how many bits are
used for fractional representation. Then it should be possible to assign
a value and do basic arithmetic with an instance of that class. The idea
is that based on fixed point arithmetic rules, each operation tracks
changes of bit width.
I would now like to use that class in connection with numpy and my
question is, whether there is a way to make its use as intuitive as
possible for the user. Meaning that it would be possible to create a
list of my FixedPoint instances and then assign that list to a numpy array.
I created some minimal code that shows the behavior:
import numpy
class FixPoint(object):
def __repr__(self):
return "Hello"
def __len__(self):
return 3
def __getitem__(self, key):
return 7
if __name__ == '__main__':
a = numpy.array([FixPoint(), FixPoint()])
print "a: ", a
b = [FixPoint(), FixPoint()]
print "b: ", b
When running that code, the output is:
a: [[7 7 7]
[7 7 7]]
b: [Hello, Hello]
What is interesting, the list uses the representation of the class,
whereas array changes it to a list of the indexed values.
Note that when changing the __len__ function to something else, the
array also uses the __repr__ output.
Would creating my own dType solve that problem?
Cheers,
Guenter
From robert.kern at gmail.com Wed Sep 5 15:28:49 2007
From: robert.kern at gmail.com (Robert Kern)
Date: Wed, 05 Sep 2007 14:28:49 -0500
Subject: [Numpy-discussion] Use my own data type with NumPy
In-Reply-To: <46DF015E.3020502@web.de>
References: <46DE5F5D.8060204@web.de> <46DEE64B.4010100@noaa.gov>
<46DF015E.3020502@web.de>
Message-ID: <46DF0371.1050108@gmail.com>
G?nter Dannoritzer wrote:
> Christopher Barker wrote:
> [...]
>> The solution is to make an empty object array first, then populate it.
> [...]
>> Does that help?
>
> Robert, Chris, thanks for that explanation. I understand that now.
>
> The purpose of my (Python) class is to model a fixed point data type. So
> I can specify how many bits are used for integer and how many bits are
> used for fractional representation. Then it should be possible to assign
> a value and do basic arithmetic with an instance of that class. The idea
> is that based on fixed point arithmetic rules, each operation tracks
> changes of bit width.
>
> I would now like to use that class in connection with numpy and my
> question is, whether there is a way to make its use as intuitive as
> possible for the user. Meaning that it would be possible to create a
> list of my FixedPoint instances and then assign that list to a numpy array.
>
> I created some minimal code that shows the behavior:
>
> import numpy
>
> class FixPoint(object):
> def __repr__(self):
> return "Hello"
>
> def __len__(self):
> return 3
>
> def __getitem__(self, key):
> return 7
>
>
>
> if __name__ == '__main__':
> a = numpy.array([FixPoint(), FixPoint()])
> print "a: ", a
>
> b = [FixPoint(), FixPoint()]
> print "b: ", b
>
>
> When running that code, the output is:
>
> a: [[7 7 7]
> [7 7 7]]
> b: [Hello, Hello]
>
> What is interesting, the list uses the representation of the class,
> whereas array changes it to a list of the indexed values.
>
> Note that when changing the __len__ function to something else, the
> array also uses the __repr__ output.
Yes, I believe we've explained why this is the case and how to work around it.
You can encapsulate that workaround into a function specifically for making
arrays of FixedPoint objects, if you like.
> Would creating my own dType solve that problem?
No. That's only useful for C data types, not Python instances. You're pretty
much stuck with object arrays.
--
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
From bryanv at enthought.com Wed Sep 5 15:35:05 2007
From: bryanv at enthought.com (Bryan Van de Ven)
Date: Wed, 05 Sep 2007 14:35:05 -0500
Subject: [Numpy-discussion] Anyone have a well-tested SWIG-based C++ STL
valarray <=> numpy.array typemap to share?
In-Reply-To: <46DEF965.1000308@noaa.gov>
References: <46DDF734.7070708@noaa.gov> <4CED078D-B8C7-4917-B0DE-4ED60BDFE015@sandia.gov> <46DEE52F.8050000@noaa.gov> <609FEB05-86EB-4FE2-867E-4A1C84105951@sandia.gov>
<46DEF965.1000308@noaa.gov>
Message-ID: <46DF04E9.2020501@enthought.com>
Christopher Barker wrote:
> Does anyone know the status of support for valarrays now?
I used std::valarray to implement a variant of the example Matrix class in
Stroustrup's book (2D only) about two years ago. I was aware that is in disuse,
by and large, but it worked well enough for my purposes and I was happy with it.
I'm sure it could have been done differently/better.
Bryan
From zpincus at stanford.edu Wed Sep 5 16:10:38 2007
From: zpincus at stanford.edu (Zachary Pincus)
Date: Wed, 5 Sep 2007 16:10:38 -0400
Subject: [Numpy-discussion] numpy.rot90 bug with >2D arrays
Message-ID:
Hello,
numpy.rot90 (from twodim_base.by) says it works only on the first two
axes of an array, but due to its use of the transpose method (which
reverses the shape tuple), can affect other axes.
For example:
a = numpy.ones((50,40,3))
b = numpy.rot90(a)
assert(b.shape == (3,40,50))
# desired result is b.shape == (40,50,3)
I believe that the fix is to replace the two calls to 'transpose()'
in the function with 'swapaxes(0,1)'. This would allow rotating an
array along just the first two axes.
Does this fix look right? Should I file a bug or can someone just
check that in?
Zach
From Chris.Barker at noaa.gov Wed Sep 5 16:22:29 2007
From: Chris.Barker at noaa.gov (Christopher Barker)
Date: Wed, 05 Sep 2007 13:22:29 -0700
Subject: [Numpy-discussion] How-to: Uniform vector scale
In-Reply-To: <496954360709051205g60433bfcndb5144c7bb1a3867@mail.gmail.com>
References: <496954360709051205g60433bfcndb5144c7bb1a3867@mail.gmail.com>
Message-ID: <46DF1005.6090808@noaa.gov>
Robert Dailey wrote:
> The
> interpreter outputs:
> "ValueError: shape mismatch: objects cannot be broadcast to a single
> shape"
You need to post your actually input and output. The above works fine
for me, just as you'd expect:
>>> A = N.array([2,3,4])
>>> B = N.array([5,6,7])
>>> S = 5
>>> A+B*S
array([27, 33, 39])
# I like to be explicit about order of operations, though:
>>> A+(B*S)
array([27, 33, 39])
-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
From Chris.Barker at noaa.gov Wed Sep 5 16:28:28 2007
From: Chris.Barker at noaa.gov (Christopher Barker)
Date: Wed, 05 Sep 2007 13:28:28 -0700
Subject: [Numpy-discussion] Use my own data type with NumPy
In-Reply-To: <46DF015E.3020502@web.de>
References: <46DE5F5D.8060204@web.de> <46DEE64B.4010100@noaa.gov>
<46DF015E.3020502@web.de>
Message-ID: <46DF116C.5040604@noaa.gov>
G?nter Dannoritzer wrote:
> The purpose of my (Python) class is to model a fixed point data type. So
> I can specify how many bits are used for integer and how many bits are
> used for fractional representation.
> it would be possible to create a
> list of my FixedPoint instances and then assign that list to a numpy array.
If your class looks like a sequence, it's going to confuse numpy.array()
> I created some minimal code that shows the behavior:
>
> import numpy
>
> class FixPoint(object):
> def __repr__(self):
> return "Hello"
>
> def __len__(self):
> return 3
>
> def __getitem__(self, key):
> return 7
Why would a FixPoint object have to look like a sequence, with a length
and a _getitem_? That's where the confusion is coming from.
If I understand your needs, a FixPoint object is a number -- you'll want
to override __add__ __mult__, and all those sorts of things, but there
should be no need to make it look like a sequence.
What does the "length" of a fixed point number mean? What does it meant
to get the third element of a fixed point object?
I'm guessing that maybe you're using __len__ to mean bit-width, but I'd
just have a property for that, and call it BitWidth or something.
-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
From Chris.Barker at noaa.gov Wed Sep 5 17:08:07 2007
From: Chris.Barker at noaa.gov (Christopher Barker)
Date: Wed, 05 Sep 2007 14:08:07 -0700
Subject: [Numpy-discussion] Anyone have a well-tested SWIG-based C++ STL
valarray <=> numpy.array typemap to share?
In-Reply-To:
References: <46DDF734.7070708@noaa.gov>
<4CED078D-B8C7-4917-B0DE-4ED60BDFE015@sandia.gov>
<46DEE52F.8050000@noaa.gov>
<609FEB05-86EB-4FE2-867E-4A1C84105951@sandia.gov>
<46DEF965.1000308@noaa.gov>
Message-ID: <46DF1AB7.5030900@noaa.gov>
Matthieu Brucher wrote:
> Blitz++ is more or less avandoned. It uses indexes than can be
> not-portable between 32bits platforms and 64bits ones.
Oh well -- that seems remarkably short sited, but would I have done better?
> The Boost.Array is a fixed-size array, determined at compile-time,
Ah, I had gotten the wrong impression -- I thought it was fixed at
construction time, not compile time.
> not interesting there, I suppose.
I agree, I kind of wonder what the point is.
> Multiarrays are what you're looking for.
Even if I just want 1-d? though I guess a 1-d multiarray is pretty simple.
> Besides, it is not needed to build Boost to use them
I've seen that -- it does look like all we'd need is the header.
So, can one:
- create a Multiarray from an existing data pointer?
- get the data pointer for an existing Multiarray?
I think that's what I'd need to make the numpy array <-> Multiarray
transition without any copying.
( I know those are really questions that are best asked on the boost
list, and should be in the docs, but you folks are so helpful...)
Maybe this is the class to wrap with the array interface, though maybe
that's exactly what Boost::python::array does (though, AFAICT, still not
for numpy).
-CHB
--
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
From paustin at eos.ubc.ca Wed Sep 5 17:32:13 2007
From: paustin at eos.ubc.ca (Philip Austin)
Date: Wed, 5 Sep 2007 14:32:13 -0700
Subject: [Numpy-discussion] Anyone have a well-tested SWIG-based C++ STL
valarray <=> numpy.array typemap to share?
In-Reply-To: <46DF1AB7.5030900@noaa.gov>
References: <46DDF734.7070708@noaa.gov>
<4CED078D-B8C7-4917-B0DE-4ED60BDFE015@sandia.gov>
<46DEE52F.8050000@noaa.gov>
<609FEB05-86EB-4FE2-867E-4A1C84105951@sandia.gov>
<46DEF965.1000308@noaa.gov>
<46DF1AB7.5030900@noaa.gov>
Message-ID: <18143.8285.670787.980433@owl.eos.ubc.ca>
Christopher Barker writes:
>
> I've seen that -- it does look like all we'd need is the header.
>
> So, can one:
>
> - create a Multiarray from an existing data pointer?
>
> - get the data pointer for an existing Multiarray?
>
> I think that's what I'd need to make the numpy array <-> Multiarray
> transition without any copying.
Albert Strasheim has done some work on this:
http://thread.gmane.org/gmane.comp.python.c++/11559/focus=11560
>
> ( I know those are really questions that are best asked on the boost
> list, and should be in the docs, but you folks are so helpful...)
>
> Maybe this is the class to wrap with the array interface, though maybe
> that's exactly what Boost::python::array does (though, AFAICT, still not
> for numpy).
>
From Chris.Barker at noaa.gov Wed Sep 5 17:57:01 2007
From: Chris.Barker at noaa.gov (Christopher Barker)
Date: Wed, 05 Sep 2007 14:57:01 -0700
Subject: [Numpy-discussion] Anyone have a well-tested SWIG-based C++ STL
valarray <=> numpy.array typemap to share?
In-Reply-To: <18143.8285.670787.980433@owl.eos.ubc.ca>
References: <46DDF734.7070708@noaa.gov>
<4CED078D-B8C7-4917-B0DE-4ED60BDFE015@sandia.gov>
<46DEE52F.8050000@noaa.gov>
<609FEB05-86EB-4FE2-867E-4A1C84105951@sandia.gov>
<46DEF965.1000308@noaa.gov>
<46DF1AB7.5030900@noaa.gov> <18143.8285.670787.980433@owl.eos.ubc.ca>
Message-ID: <46DF262D.5080001@noaa.gov>
Philip Austin wrote:
> Albert Strasheim has done some work on this:
> http://thread.gmane.org/gmane.comp.python.c++/11559/focus=11560
Thanks for the pointer. Not a lot of docs, and it looks like he's using
boost::python, and I want to use SWIG, but I'm sure there's something
useful in there.
-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
From David.L.Goldsmith at noaa.gov Wed Sep 5 17:57:42 2007
From: David.L.Goldsmith at noaa.gov (David Goldsmith)
Date: Wed, 05 Sep 2007 14:57:42 -0700
Subject: [Numpy-discussion] Anyone have a well-tested SWIG-based C++ STL
valarray <=> numpy.array typemap to share?
In-Reply-To:
References: <46DDF734.7070708@noaa.gov>
<32632B72-2B1A-4AC2-AA04-288696E37A46@ster.kuleuven.be>
<46DE9C30.8020300@obs.univ-lyon1.fr>
Message-ID: <46DF2656.9010605@noaa.gov>
Travis Vaught wrote:
> Have you seen this?
>
> http://www.scipy.org/Cookbook/SWIG_and_NumPy
>
Unclear (to me): precisely what does one get from running python
numpy/docs/swig/setup.py install, and is the product necessary, and if
so, which other components rely on the product? I ask 'cause I'm
getting the following error trying to do so:
[Parallels emulating] Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\Python25\Lib\site-packages\numpy\doc\swig>python setup.py install
running install
running build
running build_py
file Vector.py (for module Vector) not found
file Matrix.py (for module Matrix) not found
file Tensor.py (for module Tensor) not found
file Vector.py (for module Vector) not found
file Matrix.py (for module Matrix) not found
file Tensor.py (for module Tensor) not found
running build_ext
building '_Vector' extension
creating build
creating build\temp.win32-2.5
creating build\temp.win32-2.5\Release
C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\bin\cl.exe /c
/nologo /Ox
/MD /W3 /GX /DNDEBUG -IC:\Python25\lib\site-packages\numpy\core\include
-IC:\Python25\include -IC:\Python25\PC /TpVector_wrap.cxx
/Fobuild\temp.win32-2.5\Release\Vector_wrap.obj
cl : Command line warning D4029 : optimization is not available in the
standard edition compiler
Vector_wrap.cxx
c1xx : fatal error C1083: Cannot open source file: 'Vector_wrap.cxx': No
such file or directory
error: command '"C:\Program Files\Microsoft Visual Studio .NET
2003\Vc7\bin\cl.exe"' failed with exit status 2
I have Python 2.5.1 installed:
C:\Python25\Lib\site-packages\numpy\doc\swig>python
Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit
(Intel)] on win32
and numpy 1.0.3:
>>> import numpy
>>> numpy.__version__
'1.0.3'
I have the swig-1.3.31 exe:
Directory of C:\SWIG\swigwin-1.3.31
11/21/2006 12:07 AM 1,190,652 swig.exe
and it runs (take my word for it)
and the VC++ compiler via Visual Studio .NET 2003 (this I know 'cause I
use it frequently).
So, if I don't need the product of python numpy/doc/swig/setup.py
install, please explain why I don't, but if I do need it, please help me
figure out why I can't build it. Thanks!
DG
> Also, the numpy/doc/swig directory has the simple typemaps.
>
> Travis
>
> On Sep 5, 2007, at 7:08 AM, Xavier Gnata wrote:
>
>
>> I'm using the numpy C API (PyArray_SimpleNewFromData) to perform the
>> conversion but my code is written by hands. I would like to
>> simplify it
>> using SWIG but I also would like to see a good typemap valarray <=>
>> numpy.array :)
>>
>> Joris : Historical ones? Maybe also the fact that distutils has
>> some small pb with C++ module. To sum up : You have to compile you
>> module with the same compiler options you used to compile Python.
>> Python is coded in C so some options does not make sense in C++.
>> As a result, you get warnings at compile time (see pylab compiled
>> with gcc for instance).
>>
>>
>> Xavier
>>
>>
>>
>>
>>> A related question, just out of curiosity: is there a technical
>>> reason why Numpy has been coded in C rather than C++?
>>>
>>> Joris
>>>
>>>
>>>
>>> On 05 Sep 2007, at 02:24, David Goldsmith wrote:
>>>
>>>
>>>
>>>> Anyone have a well-tested SWIG-based C++ STL valarray <=>
>>>> numpy.array
>>>> typemap to share? Thanks!
>>>>
>>>> DG
>>>> --
>>>> ERD/ORR/NOS/NOAA >>> emergencyresponse/>
>>>> _______________________________________________
>>>> Numpy-discussion mailing list
>>>> Numpy-discussion at scipy.org
>>>> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>>>>
>>>>
>>> Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
>>>
>>> _______________________________________________
>>> Numpy-discussion mailing list
>>> Numpy-discussion at scipy.org
>>> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>>>
>>>
>>>
>> --
>> ############################################
>> Xavier Gnata
>> CRAL - Observatoire de Lyon
>> 9, avenue Charles Andr?
>> 69561 Saint Genis Laval cedex
>> Phone: +33 4 78 86 85 28
>> Fax: +33 4 78 86 83 86
>> E-mail: gnata at obs.univ-lyon1.fr
>> ############################################
>>
>> _______________________________________________
>> Numpy-discussion mailing list
>> Numpy-discussion at scipy.org
>> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>>
>>
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>
From dannoritzer at web.de Wed Sep 5 17:59:23 2007
From: dannoritzer at web.de (=?ISO-8859-1?Q?G=FCnter_Dannoritzer?=)
Date: Wed, 05 Sep 2007 23:59:23 +0200
Subject: [Numpy-discussion] Use my own data type with NumPy
In-Reply-To: <46DF116C.5040604@noaa.gov>
References: <46DE5F5D.8060204@web.de>
<46DEE64B.4010100@noaa.gov> <46DF015E.3020502@web.de>
<46DF116C.5040604@noaa.gov>
Message-ID: <46DF26BB.8090107@web.de>
Christopher Barker wrote:
[...]
>
> Why would a FixPoint object have to look like a sequence, with a length
> and a _getitem_? That's where the confusion is coming from.
>
That allows me to slice bits.
> If I understand your needs, a FixPoint object is a number -- you'll want
> to override __add__ __mult__, and all those sorts of things, but there
> should be no need to make it look like a sequence.
>
> What does the "length" of a fixed point number mean? What does it meant
> to get the third element of a fixed point object?
>
Yes it returns the bit width. When indexing you can get individual bits
or a bit range. That is good for modeling hardware behavior.
> I'm guessing that maybe you're using __len__ to mean bit-width, but I'd
> just have a property for that, and call it BitWidth or something.
That solved it. I took out __len__ and added a width property. Now array
is happy.
Guenter
From wfspotz at sandia.gov Wed Sep 5 18:13:18 2007
From: wfspotz at sandia.gov (Bill Spotz)
Date: Wed, 5 Sep 2007 16:13:18 -0600
Subject: [Numpy-discussion] Anyone have a well-tested SWIG-based C++ STL
valarray <=> numpy.array typemap to share?
In-Reply-To: <46DF2656.9010605@noaa.gov>
References: <46DDF734.7070708@noaa.gov>
<32632B72-2B1A-4AC2-AA04-288696E37A46@ster.kuleuven.be>
<46DE9C30.8020300@obs.univ-lyon1.fr>
<46DF2656.9010605@noaa.gov>
Message-ID:
The setup.py script in numpy/doc/swig is for compiling test code for
numpy.i. It is properly invoked by the Makefile, which will first
run swig to generate the wrapper code for the test classes. All a
developer, who is using swig to interface some code with numpy in
python, needs is numpy.i. The setup.py script could possibly work as
a template for whatever they are wrapping, I guess.
On Sep 5, 2007, at 3:57 PM, David Goldsmith wrote:
> Travis Vaught wrote:
>> Have you seen this?
>>
>> http://www.scipy.org/Cookbook/SWIG_and_NumPy
>>
> Unclear (to me): precisely what does one get from running python
> numpy/docs/swig/setup.py install, and is the product necessary, and if
> so, which other components rely on the product? I ask 'cause I'm
> getting the following error trying to do so:
>
> [Parallels emulating] Microsoft Windows XP [Version 5.1.2600]
> (C) Copyright 1985-2001 Microsoft Corp.
>
> C:\Python25\Lib\site-packages\numpy\doc\swig>python setup.py install
> running install
> running build
> running build_py
> file Vector.py (for module Vector) not found
> file Matrix.py (for module Matrix) not found
> file Tensor.py (for module Tensor) not found
> file Vector.py (for module Vector) not found
> file Matrix.py (for module Matrix) not found
> file Tensor.py (for module Tensor) not found
> running build_ext
> building '_Vector' extension
> creating build
> creating build\temp.win32-2.5
> creating build\temp.win32-2.5\Release
> C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\bin\cl.exe /c
> /nologo /Ox
> /MD /W3 /GX /DNDEBUG -IC:\Python25\lib\site-packages\numpy\core
> \include
> -IC:\Python25\include -IC:\Python25\PC /TpVector_wrap.cxx
> /Fobuild\temp.win32-2.5\Release\Vector_wrap.obj
> cl : Command line warning D4029 : optimization is not available in the
> standard edition compiler
> Vector_wrap.cxx
> c1xx : fatal error C1083: Cannot open source file:
> 'Vector_wrap.cxx': No
> such file or directory
> error: command '"C:\Program Files\Microsoft Visual Studio .NET
> 2003\Vc7\bin\cl.exe"' failed with exit status 2
>
> I have Python 2.5.1 installed:
>
> C:\Python25\Lib\site-packages\numpy\doc\swig>python
> Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit
> (Intel)] on win32
>
> and numpy 1.0.3:
>>>> import numpy
>>>> numpy.__version__
> '1.0.3'
>
> I have the swig-1.3.31 exe:
>
> Directory of C:\SWIG\swigwin-1.3.31
>
> 11/21/2006 12:07 AM 1,190,652 swig.exe
>
> and it runs (take my word for it)
>
> and the VC++ compiler via Visual Studio .NET 2003 (this I know
> 'cause I
> use it frequently).
>
> So, if I don't need the product of python numpy/doc/swig/setup.py
> install, please explain why I don't, but if I do need it, please
> help me
> figure out why I can't build it. Thanks!
>
> DG
>
>
>> Also, the numpy/doc/swig directory has the simple typemaps.
>>
>> Travis
>>
>> On Sep 5, 2007, at 7:08 AM, Xavier Gnata wrote:
>>
>>
>>> I'm using the numpy C API (PyArray_SimpleNewFromData) to perform the
>>> conversion but my code is written by hands. I would like to
>>> simplify it
>>> using SWIG but I also would like to see a good typemap valarray <=>
>>> numpy.array :)
>>>
>>> Joris : Historical ones? Maybe also the fact that distutils has
>>> some small pb with C++ module. To sum up : You have to compile you
>>> module with the same compiler options you used to compile Python.
>>> Python is coded in C so some options does not make sense in C++.
>>> As a result, you get warnings at compile time (see pylab compiled
>>> with gcc for instance).
>>>
>>>
>>> Xavier
>>>
>>>
>>>
>>>
>>>> A related question, just out of curiosity: is there a technical
>>>> reason why Numpy has been coded in C rather than C++?
>>>>
>>>> Joris
>>>>
>>>>
>>>>
>>>> On 05 Sep 2007, at 02:24, David Goldsmith wrote:
>>>>
>>>>
>>>>
>>>>> Anyone have a well-tested SWIG-based C++ STL valarray <=>
>>>>> numpy.array
>>>>> typemap to share? Thanks!
>>>>>
>>>>> DG
>>>>> --
>>>>> ERD/ORR/NOS/NOAA >>>> emergencyresponse/>
>>>>> _______________________________________________
>>>>> Numpy-discussion mailing list
>>>>> Numpy-discussion at scipy.org
>>>>> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>>>>>
>>>>>
>>>> Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
>>>>
>>>> _______________________________________________
>>>> Numpy-discussion mailing list
>>>> Numpy-discussion at scipy.org
>>>> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>>>>
>>>>
>>>>
>>> --
>>> ############################################
>>> Xavier Gnata
>>> CRAL - Observatoire de Lyon
>>> 9, avenue Charles Andr?
>>> 69561 Saint Genis Laval cedex
>>> Phone: +33 4 78 86 85 28
>>> Fax: +33 4 78 86 83 86
>>> E-mail: gnata at obs.univ-lyon1.fr
>>> ############################################
>>>
>>> _______________________________________________
>>> Numpy-discussion mailing list
>>> Numpy-discussion at scipy.org
>>> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>>>
>>>
>>
>> _______________________________________________
>> Numpy-discussion mailing list
>> Numpy-discussion at scipy.org
>> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>>
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>
** Bill Spotz **
** Sandia National Laboratories Voice: (505)845-0170 **
** P.O. Box 5800 Fax: (505)284-5451 **
** Albuquerque, NM 87185-0370 Email: wfspotz at sandia.gov **
From jbednar at inf.ed.ac.uk Wed Sep 5 18:37:06 2007
From: jbednar at inf.ed.ac.uk (James A. Bednar)
Date: Wed, 5 Sep 2007 23:37:06 +0100
Subject: [Numpy-discussion] Use my own data type with NumPy
In-Reply-To:
References:
Message-ID: <18143.12178.421607.971471@lodestar.inf.ed.ac.uk>
| Date: Wed, 05 Sep 2007 21:19:58 +0200
| From: G?nter Dannoritzer
| Subject: Re: [Numpy-discussion] Use my own data type with NumPy
|
| The purpose of my (Python) class is to model a fixed point data
| type. So I can specify how many bits are used for integer and how
| many bits are used for fractional representation. Then it should be
| possible to assign a value and do basic arithmetic with an instance
| of that class. The idea is that based on fixed point arithmetic
| rules, each operation tracks changes of bit width.
You may already be aware of this, but there is a package available
that does exactly what you describe:
http://fixedpoint.sourceforge.net
It is not currently actively aintained, and it's quite slow, but it
does work reliably, and we use it daily at my site. It might be best
for all concerned if you simply took over that project, making it
faster and well supported, rather than creating a competing one...
Jim
From matthieu.brucher at gmail.com Thu Sep 6 04:06:35 2007
From: matthieu.brucher at gmail.com (Matthieu Brucher)
Date: Thu, 6 Sep 2007 10:06:35 +0200
Subject: [Numpy-discussion] Anyone have a well-tested SWIG-based C++ STL
valarray <=> numpy.array typemap to share?
In-Reply-To: <46DF1AB7.5030900@noaa.gov>
References: <46DDF734.7070708@noaa.gov>
<4CED078D-B8C7-4917-B0DE-4ED60BDFE015@sandia.gov>
<46DEE52F.8050000@noaa.gov>
<609FEB05-86EB-4FE2-867E-4A1C84105951@sandia.gov>
<46DEF965.1000308@noaa.gov>
<46DF1AB7.5030900@noaa.gov>
Message-ID:
2007/9/5, Christopher Barker :
>
> Matthieu Brucher wrote:
> > Blitz++ is more or less avandoned. It uses indexes than can be
> > not-portable between 32bits platforms and 64bits ones.
>
> Oh well -- that seems remarkably short sited, but would I have done
> better?
Well, it's too bad the mainteners used int instead of long or somthing like
that, but at the time, 64bits platforms did not exist.
> The Boost.Array is a fixed-size array, determined at compile-time,
>
> Ah, I had gotten the wrong impression -- I thought it was fixed at
> construction time, not compile time.
According to the doc, it's fixed at compile-time.
> not interesting there, I suppose.
>
> I agree, I kind of wonder what the point is.
In some case you might want them, but not very often, only to speed up
computation.
> Multiarrays are what you're looking for.
>
> Even if I just want 1-d? though I guess a 1-d multiarray is pretty simple.
>
> > Besides, it is not needed to build Boost to use them
>
> I've seen that -- it does look like all we'd need is the header.
>
> So, can one:
>
> - create a Multiarray from an existing data pointer?
>
> - get the data pointer for an existing Multiarray?
>
> I think that's what I'd need to make the numpy array <-> Multiarray
> transition without any copying.
>
I have the same problem at my job, but I don't think SWIG will suit me,
although I use it for simpler wrappers.
Like Philip said, there are some trials, I hope someone (or I) will come up
with a Python array <-> C++ array wrapper without copy. What you really need
is that the multiarray must be able to use the data pointer, it's a special
policy, and then you must be able to register a shared pointer to Python,
that is if the original container use shared pointer. If the last part is
not possible, you will need to create first a Python array and then make a
view of it in C++, even for a result array.
Matthieu
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
From george.sakkis at gmail.com Thu Sep 6 07:30:43 2007
From: george.sakkis at gmail.com (George Sakkis)
Date: Thu, 06 Sep 2007 11:30:43 -0000
Subject: [Numpy-discussion] 2-d in-place operation performance vs 1-d
non in-place
In-Reply-To: <200709051829.24448.faltet@carabos.com>
References: <91ad5bf80709050722n67ef53b3l6902aa03eff95a66@mail.gmail.com>
<200709051829.24448.faltet@carabos.com>
Message-ID: <1189078243.699888.8790@k79g2000hse.googlegroups.com>
On Sep 5, 12:29 pm, Francesc Altet wrote:
> A Wednesday 05 September 2007, George Sakkis escrigu?:
>
>
>
> > I was surprised to see that an in-place modification of a 2-d array
> > turns out to be slower from the respective non-mutating operation on
> > 1- d arrays, although the latter creates new array objects. Here is
> > the benchmarking code:
>
> > import timeit
>
> > for n in 10,100,1000,10000:
> > setup = 'from numpy.random import random;' \
> > 'm=random((%d,2));' \
> > 'u1=random(%d);' \
> > 'u2=u1.reshape((u1.size,1))' % (n,n)
> > timers = [timeit.Timer(stmt,setup) for stmt in
> > # 1-d operations; create new arrays
> > 'a0 = m[:,0]-u1; a1 = m[:,1]-u1',
> > # 2-d in place operation
> > 'm -= u2'
> > ]
> > print n, [min(timer.repeat(3,1000)) for timer in timers]
>
> > And some results (Python 2.5, WinXP):
>
> > 10 [0.010832382327921563, 0.0045706926438974782]
> > 100 [0.010882668048592767, 0.021704993232380093]
> > 1000 [0.018272154701226007, 0.19477587235249172]
> > 10000 [0.073787590322233698, 1.9234369172618306]
>
> > So the 2-d in-place modification time grows linearly with the array
> > size but the 1-d operations are much more efficient, despite
> > allocating new arrays while doing so. What gives ?
>
> This seems the effect of broadcasting u2. If you were to use a
> pre-computed broadcasted, you would get rid of such bottleneck:
>
> for n in 10,100,1000,10000:
> setup = 'import numpy;' \
> 'm=numpy.random.random((%d,2));' \
> 'u1=numpy.random.random(%d);' \
> 'u2=u1[:, numpy.newaxis];' \
> 'u3=numpy.array([u1,u1]).transpose()' % (n,n)
> timers = [timeit.Timer(stmt,setup) for stmt in
> # 1-d operations; create new arrays
> 'a0 = m[:,0]-u1; a1 = m[:,1]-u1',
> # 2-d in place operation (using broadcasting)
> 'm -= u2',
> # 2-d in-place operation (not forcing broadcasting)
> 'm -= u3'
> ]
> print n, [min(timer.repeat(3,1000)) for timer in timers]
>
> gives in my machine:
>
> 10 [0.03213191032409668, 0.012019872665405273, 0.0068600177764892578]
> 100 [0.033048152923583984, 0.06542205810546875, 0.0076580047607421875]
> 1000 [0.040294170379638672, 0.59892702102661133, 0.014600992202758789]
> 10000 [0.32667303085327148, 5.9721651077270508, 0.10261106491088867]
Thank you, indeed broadcasting is the bottleneck here. I had the
impression that broadcasting was a fast operation, i.e. it doesn't
require allocating physically the target array of the broadcast but it
seems this is not the case.
George
From tim.hochberg at ieee.org Thu Sep 6 11:07:19 2007
From: tim.hochberg at ieee.org (Timothy Hochberg)
Date: Thu, 6 Sep 2007 08:07:19 -0700
Subject: [Numpy-discussion] 2-d in-place operation performance vs 1-d
non in-place
In-Reply-To: <1189078243.699888.8790@k79g2000hse.googlegroups.com>
References: <91ad5bf80709050722n67ef53b3l6902aa03eff95a66@mail.gmail.com>
<200709051829.24448.faltet@carabos.com>
<1189078243.699888.8790@k79g2000hse.googlegroups.com>
Message-ID:
On 9/6/07, George Sakkis wrote:
>
> On Sep 5, 12:29 pm, Francesc Altet wrote:
> > A Wednesday 05 September 2007, George Sakkis escrigu?:
> >
> >
> >
> > > I was surprised to see that an in-place modification of a 2-d array
> > > turns out to be slower from the respective non-mutating operation on
> > > 1- d arrays, although the latter creates new array objects. Here is
> > > the benchmarking code:
> >
> > > import timeit
> >
> > > for n in 10,100,1000,10000:
> > > setup = 'from numpy.random import random;' \
> > > 'm=random((%d,2));' \
> > > 'u1=random(%d);' \
> > > 'u2=u1.reshape((u1.size,1))' % (n,n)
> > > timers = [timeit.Timer(stmt,setup) for stmt in
> > > # 1-d operations; create new arrays
> > > 'a0 = m[:,0]-u1; a1 = m[:,1]-u1',
> > > # 2-d in place operation
> > > 'm -= u2'
> > > ]
> > > print n, [min(timer.repeat(3,1000)) for timer in timers]
> >
> > > And some results (Python 2.5, WinXP):
> >
> > > 10 [0.010832382327921563, 0.0045706926438974782]
> > > 100 [0.010882668048592767, 0.021704993232380093]
> > > 1000 [0.018272154701226007, 0.19477587235249172]
> > > 10000 [0.073787590322233698, 1.9234369172618306]
> >
> > > So the 2-d in-place modification time grows linearly with the array
> > > size but the 1-d operations are much more efficient, despite
> > > allocating new arrays while doing so. What gives ?
> >
> > This seems the effect of broadcasting u2. If you were to use a
> > pre-computed broadcasted, you would get rid of such bottleneck:
> >
> > for n in 10,100,1000,10000:
> > setup = 'import numpy;' \
> > 'm=numpy.random.random((%d,2));' \
> > 'u1=numpy.random.random(%d);' \
> > 'u2=u1[:, numpy.newaxis];' \
> > 'u3=numpy.array([u1,u1]).transpose()' % (n,n)
> > timers = [timeit.Timer(stmt,setup) for stmt in
> > # 1-d operations; create new arrays
> > 'a0 = m[:,0]-u1; a1 = m[:,1]-u1',
> > # 2-d in place operation (using broadcasting)
> > 'm -= u2',
> > # 2-d in-place operation (not forcing broadcasting)
> > 'm -= u3'
> > ]
> > print n, [min(timer.repeat(3,1000)) for timer in timers]
> >
> > gives in my machine:
> >
> > 10 [0.03213191032409668, 0.012019872665405273, 0.0068600177764892578]
> > 100 [0.033048152923583984, 0.06542205810546875, 0.0076580047607421875]
> > 1000 [0.040294170379638672, 0.59892702102661133, 0.014600992202758789]
> > 10000 [0.32667303085327148, 5.9721651077270508, 0.10261106491088867]
>
> Thank you, indeed broadcasting is the bottleneck here. I had the
> impression that broadcasting was a fast operation,
I'm fairly certain that this is correct.
> i.e. it doesn't
> require allocating physically the target array of the broadcast but it
> seems this is not the case.
I don't think it does. However it does change the way the ufuncs stride over
the array when they do the operation, which I suspect is the root of the
problem. First note that all of these operations are (and should be) linear
in n. I played around this a little bit and found a couple of things; first
the axis along which you broadcast matters a lot. That's not surprising;
when you broadcast along the short axis I believe that the inner loop ends
up very short and thus grows a lot of overhead. The other is that in-place
versus not in place makes a big difference. I'm not sure why that is. Here's
the amended benchmark; I use larger values of N so that the linear behavior
of all the examples is clearer.
import timeit
for n in 10000,20000,30000,40000,50000:
setup = """
import numpy
m = numpy.random.random((%d,2))
u1 = numpy.random.random(%d)
u2 = u1[:, numpy.newaxis]
u3 = numpy.array([u1,u1]).transpose()
m2 = numpy.array(m.transpose())
u4 = u1[numpy.newaxis, :]
""" % (n,n)
timers = [timeit.Timer(stmt,setup) for stmt in
# 1-d operations; create new arrays
'a0 = m[:,0]-u1; a1 = m[:,1]-u1',
# 2-d in place operation (using broadcasting)
'x = m - u2',
# 2-d in-place operation (not forcing broadcasting)
'x = m - u3',
# transposed using broadcasting
'x = m2 - u4'
]
print n, [min(timer.repeat(3,100)) for timer in timers]
and the results:
10000 [0.0061748071333088406, 0.091492354475219639, 0.0063329277883082957,
0.0055317086389471415]
20000 [0.01121259824921883, 0.18455949010279238, 0.013805665245163912,
0.010877918841640355]
30000 [0.018362668998434195, 0.27472122853602898, 0.026029285844988426,
0.01910075163184155]
40000 [0.029564092643059592, 0.4027084447883742, 0.12735166061608361,
0.10206883835794844]
50000 [0.059520972637583824, 0.50685380404492797, 0.17739796538386798,
0.13967277964098823]
--
. __
. |-\
.
. tim.hochberg at ieee.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
From jdh2358 at gmail.com Thu Sep 6 14:06:50 2007
From: jdh2358 at gmail.com (John Hunter)
Date: Thu, 6 Sep 2007 13:06:50 -0500
Subject: [Numpy-discussion] corrcoef
Message-ID: <88e473830709061106w634e997s7dc027da040ba260@mail.gmail.com>
Is it desirable that numpy.corrcoef for two arrays returns a 2x2 array
rather than a scalar
In [10]: npy.corrcoef(npy.random.rand(10), npy.random.rand(10))
Out[10]:
array([[ 1. , -0.16088728],
[-0.16088728, 1. ]])
I always end up extracting the 0,1 element anyway. What is the
advantage, aside from backwards compatibility, for returning a 2x2?
JDH
From svetosch at gmx.net Thu Sep 6 13:13:24 2007
From: svetosch at gmx.net (Sven Schreiber)
Date: Thu, 06 Sep 2007 18:13:24 +0100
Subject: [Numpy-discussion] corrcoef
In-Reply-To: <88e473830709061106w634e997s7dc027da040ba260@mail.gmail.com>
References: <88e473830709061106w634e997s7dc027da040ba260@mail.gmail.com>
Message-ID: <46E03534.9000309@gmx.net>
John Hunter schrieb:
> Is it desirable that numpy.corrcoef for two arrays returns a 2x2 array
> rather than a scalar
>
> In [10]: npy.corrcoef(npy.random.rand(10), npy.random.rand(10))
> Out[10]:
> array([[ 1. , -0.16088728],
> [-0.16088728, 1. ]])
>
>
> I always end up extracting the 0,1 element anyway. What is the
> advantage, aside from backwards compatibility, for returning a 2x2?
> JDH
Forgive me if my answer is trivial and misses your point, but I guess
because for more than two data series one also gets the entire
correlation matrix and that is desirable. However, I would agree that
the name "corrcoef" is unfortunate, suggesting a scalar value (at least
to me).
-sven
From Chris.Barker at noaa.gov Thu Sep 6 18:10:15 2007
From: Chris.Barker at noaa.gov (Christopher Barker)
Date: Thu, 06 Sep 2007 15:10:15 -0700
Subject: [Numpy-discussion] Anyone have a well-tested SWIG-based C++ STL
valarray <=> numpy.array typemap to share?
In-Reply-To: <46DE9C30.8020300@obs.univ-lyon1.fr>
References: <46DDF734.7070708@noaa.gov>
<32632B72-2B1A-4AC2-AA04-288696E37A46@ster.kuleuven.be>
<46DE9C30.8020300@obs.univ-lyon1.fr>
Message-ID: <46E07AC7.2010001@noaa.gov>
Xavier Gnata wrote:
> I'm using the numpy C API (PyArray_SimpleNewFromData) to perform the
> conversion but my code is written by hands.
I'd like to see that. How are you getting the pointer to pass in to
PyArray_SimpleNewFromData? It looks like you can do something like:
(VA is a valarray)
npy_intp *dims
dims[0] = VA.size()
NPA = PyArray_SimpleNewFromData(1, dims, typenum, &VA[0]);
Is that what you're doing? Is there any guarantee that &VA[0] won't
change? In any case, I assume that you have to make sure that VA doesn't
get deleted while the array is still around.
> I would like to simplify it using SWIG but I also would like to see a good typemap valarray <=>
> numpy.array :)
In principle, if you know how to write the code by hand, you know how to
write the typemap.
--
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
From david at ar.media.kyoto-u.ac.jp Fri Sep 7 05:36:50 2007
From: david at ar.media.kyoto-u.ac.jp (David Cournapeau)
Date: Fri, 07 Sep 2007 18:36:50 +0900
Subject: [Numpy-discussion] why system_info.check_libs does not look for dll
on windows ?
Message-ID: <46E11BB2.4060203@ar.media.kyoto-u.ac.jp>
Hi,
I would like to know if there is a reason why system_info does not
look for dll on windows ? I think it would make sense to look for dll
when you want to use an external lib through ctypes, for example.
cheers,
David
From nardei at infinito.it Fri Sep 7 06:11:10 2007
From: nardei at infinito.it (ale)
Date: Fri, 07 Sep 2007 12:11:10 +0200
Subject: [Numpy-discussion] Importing data from html tables
Message-ID:
Hi,
I'm trying to import into array the data contained in a html table.
I use BeautifulSoup as html parser
html = open('T0015.html','r')
bs = BeautifulSoup(html)
for tr in bs.findAll('tr')[1:]:
table.append([td.p.string for td in tr.findAll('td')])
and I get this:
print table
[[u'1925', u'--', u'--', u'--', u'--', u'--', u'--', u'--', u'--', u'105.0']
[u'1926', u'--', u'--', u'--', u'--', u'--', u'--', u'--', u'--', u'136.0']
[u'1927', u'--', u'--', u'--', u'--', u'--', u'--', u'--', u'--', u'51.0']
[u'1928', u'--', u'--', u'--', u'nn', u'--', u'--', u'--', u'--', u'104.0']
,.......and so on]
How to put this list of list of strings in a numpy array, and set '--'
and 'nn' as NaN?
Thank you
Alessio
From cookedm at physics.mcmaster.ca Fri Sep 7 09:59:50 2007
From: cookedm at physics.mcmaster.ca (David M. Cooke)
Date: Fri, 07 Sep 2007 09:59:50 -0400
Subject: [Numpy-discussion] Anyone have a well-tested SWIG-based C++ STL
valarray <=> numpy.array typemap to share?
In-Reply-To: (Bill Spotz's
message of "Wed\, 5 Sep 2007 12\:07\:31 -0600")
References: <46DDF734.7070708@noaa.gov>
<32632B72-2B1A-4AC2-AA04-288696E37A46@ster.kuleuven.be>
<46DEE97C.5070008@noaa.gov>
Message-ID:
"Bill Spotz" writes:
> On Sep 5, 2007, at 11:38 AM, Christopher Barker wrote:
>
>> Of course, it should be possible to write C++ wrappers around the core
>> ND-array object, if anyone wants to take that on!
>
> boost::python has done this for Numeric, but last I checked, they
> have not upgraded to numpy.
Even then, their wrappers went through the Python interface, not the C
API. So, it's no faster than using Python straight.
--
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke http://arbutus.physics.mcmaster.ca/dmc/
|cookedm at physics.mcmaster.ca
From cookedm at physics.mcmaster.ca Fri Sep 7 10:03:47 2007
From: cookedm at physics.mcmaster.ca (David M. Cooke)
Date: Fri, 07 Sep 2007 10:03:47 -0400
Subject: [Numpy-discussion] Anyone have a well-tested SWIG-based C++ STL
valarray <=> numpy.array typemap to share?
In-Reply-To: <46DEE97C.5070008@noaa.gov> (Christopher Barker's message of
"Wed\, 05 Sep 2007 10\:38\:04 -0700")
References: <46DDF734.7070708@noaa.gov>
<32632B72-2B1A-4AC2-AA04-288696E37A46@ster.kuleuven.be>
<46DEE97C.5070008@noaa.gov>
Message-ID:
Christopher Barker writes:
> Joris De Ridder wrote:
>> A related question, just out of curiosity: is there a technical
>> reason why Numpy has been coded in C rather than C++?
>
> There was a fair bit of discussion about this back when the numarray
> project started, which was a re-implementation of the original Numeric.
>
> IIRC, one of the drivers was that C++ support was still pretty
> inconsistent across compilers and OSs, particularly if you wanted to
> really get the advantages of C++, by using templates and the like.
>
> It was considered very important that the numpy code base be very portable.
One of the big problems has always been that the C++ application
binary interface (ABI) has historically not been all that stable: all
the C++ libraries your program used would have to be compiled by the
same version of the compiler. That includes Python. You couldn't
import an extension module written in C++ compiled with g++ 3.3, say,
at the same time as one compiled with g++ 4.0, and your Python would
have to been linked with the same version.
While the ABI issues (at least on Linux with GCC) are better now, it's
still something of a quagmire.
--
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke http://arbutus.physics.mcmaster.ca/dmc/
|cookedm at physics.mcmaster.ca
From robert.kern at gmail.com Fri Sep 7 14:16:05 2007
From: robert.kern at gmail.com (Robert Kern)
Date: Fri, 07 Sep 2007 13:16:05 -0500
Subject: [Numpy-discussion] why system_info.check_libs does not look for
dll on windows ?
In-Reply-To: <46E11BB2.4060203@ar.media.kyoto-u.ac.jp>
References: <46E11BB2.4060203@ar.media.kyoto-u.ac.jp>
Message-ID: <46E19565.1060407@gmail.com>
David Cournapeau wrote:
> Hi,
>
> I would like to know if there is a reason why system_info does not
> look for dll on windows ? I think it would make sense to look for dll
> when you want to use an external lib through ctypes, for example.
Because it was designed to find libraries that the compiler can link against.
Most Windows compilers require a .lib or a .a "import library" in order to link
with the DLL. Making system_info find .dlls would give false positives for its
intended use.
--
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
From robert.kern at gmail.com Fri Sep 7 14:17:44 2007
From: robert.kern at gmail.com (Robert Kern)
Date: Fri, 07 Sep 2007 13:17:44 -0500
Subject: [Numpy-discussion] Importing data from html tables
In-Reply-To:
References:
Message-ID: <46E195C8.3030507@gmail.com>
ale wrote:
> Hi,
> I'm trying to import into array the data contained in a html table.
> I use BeautifulSoup as html parser
>
> html = open('T0015.html','r')
> bs = BeautifulSoup(html)
> for tr in bs.findAll('tr')[1:]:
> table.append([td.p.string for td in tr.findAll('td')])
>
> and I get this:
>
> print table
>
> [[u'1925', u'--', u'--', u'--', u'--', u'--', u'--', u'--', u'--', u'105.0']
> [u'1926', u'--', u'--', u'--', u'--', u'--', u'--', u'--', u'--', u'136.0']
> [u'1927', u'--', u'--', u'--', u'--', u'--', u'--', u'--', u'--', u'51.0']
> [u'1928', u'--', u'--', u'--', u'nn', u'--', u'--', u'--', u'--', u'104.0']
> ,.......and so on]
>
> How to put this list of list of strings in a numpy array, and set '--'
> and 'nn' as NaN?
from numpy import array, nan
def myfloat(x):
if x == '--':
return nan
else:
return float(x)
arr = array([map(myfloat, row) for row in table])
--
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
From dalcinl at gmail.com Fri Sep 7 20:39:16 2007
From: dalcinl at gmail.com (Lisandro Dalcin)
Date: Fri, 7 Sep 2007 21:39:16 -0300
Subject: [Numpy-discussion] Anyone have a well-tested SWIG-based C++ STL
valarray <=> numpy.array typemap to share?
In-Reply-To: <46DDF734.7070708@noaa.gov>
References: <46DDF734.7070708@noaa.gov>
Message-ID:
David, I'll try to show you what I do for a custom C++ class, of
course this does not solve the issue resizing (my class does not
actually support resizing, so this is fine for me):
My custom class is a templatized one called DTable (is like a 2d
contiguous array), but currently I only instantiate it for 'int' and
'double'. This class have two relevant methods: getShape(), returning
a std::pair, and getArray(), returning a reference to an underliing
std::vector. So I am able to automatically support array interface by
using this:
First, I define a templatized utility function 'array_interface'
%header %{
namespace numpy {
template static char typechar() { return '\0'; }
template<> static char typechar() { return 'i'; }
template<> static char typechar() { return 'f'; }
template
static PyObject*
array_interface(const DTable* self)
{
const std::pair& shape = self->getShape();
const std::vector& data = self->getArray();
void* array = const_cast(&data[0]);
char endian = PyArray_NATIVE;
char kind = typechar();
int elsize = sizeof(T);
return Py_BuildValue("{sNsNsNsN}",
"shape", Py_BuildValue("ii", shape.first, shape.second),
"typestr", PyString_FromFormat("%c%c%d", endian, kind, elsize),
"data", Py_BuildValue("NO", PyLong_FromVoidPtr(array), Py_False),
"version", PyInt_FromLong(3));
}
}
%}
Now define a SWIG macro to apply it to instantiations of my class
%define %array_interface(Class)
%extend Class { PyObject* __array_interface__; }
%{
#define %mangle(Class) ##_## __array_interface__ ## _get(_t) \
numpy::array_interface(_t)
#define %mangle(Class) ##_## __array_interface__ ## _set(_t, _val) \
SWIG_exception_fail(SWIG_AttributeError, "read-only attribute")
%}
%enddef
and finally instantiate my class with different names for 'int' and
'double' in SWIG and finally apply previous macro
%template(DTableI) DTable;
%template(DTableS) DTable;
%array_interface( DTable );
%array_interface( DTable );
I think you can implement someting similar for std::vector,
std::valarray, or whatever... For use other data types, the only you
need is to specialize the 'typecode' function.
Hope this help you.
On 9/4/07, David Goldsmith wrote:
> Anyone have a well-tested SWIG-based C++ STL valarray <=> numpy.array
> typemap to share? Thanks!
>
> DG
> --
> ERD/ORR/NOS/NOAA
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>
--
Lisandro Dalc?n
---------------
Centro Internacional de M?todos Computacionales en Ingenier?a (CIMEC)
Instituto de Desarrollo Tecnol?gico para la Industria Qu?mica (INTEC)
Consejo Nacional de Investigaciones Cient?ficas y T?cnicas (CONICET)
PTLC - G?emes 3450, (3000) Santa Fe, Argentina
Tel/Fax: +54-(0)342-451.1594
From David.L.Goldsmith at noaa.gov Fri Sep 7 21:23:26 2007
From: David.L.Goldsmith at noaa.gov (David Goldsmith)
Date: Fri, 07 Sep 2007 18:23:26 -0700
Subject: [Numpy-discussion] Anyone have a well-tested SWIG-based C++ STL
valarray <=> numpy.array typemap to share?
In-Reply-To:
References: <46DDF734.7070708@noaa.gov>
Message-ID: <46E1F98E.6070408@noaa.gov>
Thanks!
DG
Lisandro Dalcin wrote:
> David, I'll try to show you what I do for a custom C++ class, of
> course this does not solve the issue resizing (my class does not
> actually support resizing, so this is fine for me):
>
> My custom class is a templatized one called DTable (is like a 2d
> contiguous array), but currently I only instantiate it for 'int' and
> 'double'. This class have two relevant methods: getShape(), returning
> a std::pair, and getArray(), returning a reference to an underliing
> std::vector. So I am able to automatically support array interface by
> using this:
>
> First, I define a templatized utility function 'array_interface'
>
> %header %{
> namespace numpy {
>
> template static char typechar() { return '\0'; }
> template<> static char typechar() { return 'i'; }
> template<> static char typechar() { return 'f'; }
>
> template
> static PyObject*
> array_interface(const DTable* self)
> {
> const std::pair& shape = self->getShape();
> const std::vector& data = self->getArray();
> void* array = const_cast(&data[0]);
> char endian = PyArray_NATIVE;
> char kind = typechar();
> int elsize = sizeof(T);
> return Py_BuildValue("{sNsNsNsN}",
> "shape", Py_BuildValue("ii", shape.first, shape.second),
> "typestr", PyString_FromFormat("%c%c%d", endian, kind, elsize),
> "data", Py_BuildValue("NO", PyLong_FromVoidPtr(array), Py_False),
> "version", PyInt_FromLong(3));
> }
> }
> %}
>
> Now define a SWIG macro to apply it to instantiations of my class
>
> %define %array_interface(Class)
> %extend Class { PyObject* __array_interface__; }
> %{
> #define %mangle(Class) ##_## __array_interface__ ## _get(_t) \
> numpy::array_interface(_t)
> #define %mangle(Class) ##_## __array_interface__ ## _set(_t, _val) \
> SWIG_exception_fail(SWIG_AttributeError, "read-only attribute")
> %}
> %enddef
>
> and finally instantiate my class with different names for 'int' and
> 'double' in SWIG and finally apply previous macro
>
>
> %template(DTableI) DTable;
> %template(DTableS) DTable;
>
> %array_interface( DTable );
> %array_interface( DTable );
>
> I think you can implement someting similar for std::vector,
> std::valarray, or whatever... For use other data types, the only you
> need is to specialize the 'typecode' function.
>
> Hope this help you.
>
>
> On 9/4/07, David Goldsmith wrote:
>
>> Anyone have a well-tested SWIG-based C++ STL valarray <=> numpy.array
>> typemap to share? Thanks!
>>
>> DG
>> --
>> ERD/ORR/NOS/NOAA
>> _______________________________________________
>> Numpy-discussion mailing list
>> Numpy-discussion at scipy.org
>> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>>
>>
>
>
>
From david at ar.media.kyoto-u.ac.jp Sat Sep 8 07:59:02 2007
From: david at ar.media.kyoto-u.ac.jp (David Cournapeau)
Date: Sat, 08 Sep 2007 20:59:02 +0900
Subject: [Numpy-discussion] why system_info.check_libs does not look for
dll on windows ?
In-Reply-To: <46E19565.1060407@gmail.com>
References: <46E11BB2.4060203@ar.media.kyoto-u.ac.jp>
<46E19565.1060407@gmail.com>
Message-ID: <46E28E86.1010904@ar.media.kyoto-u.ac.jp>
Robert Kern wrote:
> David Cournapeau wrote:
>
>> Hi,
>>
>> I would like to know if there is a reason why system_info does not
>> look for dll on windows ? I think it would make sense to look for dll
>> when you want to use an external lib through ctypes, for example.
>>
>
> Because it was designed to find libraries that the compiler can link against.
> Most Windows compilers require a .lib or a .a "import library" in order to link
> with the DLL. Making system_info find .dlls would give false positives for its
> intended use.
>
I see, thanks for the explanation.
cheers,
David
From gnata at obs.univ-lyon1.fr Sat Sep 8 15:20:37 2007
From: gnata at obs.univ-lyon1.fr (Xavier Gnata)
Date: Sat, 08 Sep 2007 21:20:37 +0200
Subject: [Numpy-discussion] Anyone have a well-tested SWIG-based C++ STL
valarray <=> numpy.array typemap to share?
In-Reply-To: <46E07AC7.2010001@noaa.gov>
References: <46DDF734.7070708@noaa.gov> <32632B72-2B1A-4AC2-AA04-288696E37A46@ster.kuleuven.be> <46DE9C30.8020300@obs.univ-lyon1.fr>
<46E07AC7.2010001@noaa.gov>
Message-ID: <46E2F605.1020103@obs.univ-lyon1.fr>
Christopher Barker wrote:
> Xavier Gnata wrote:
>
>> I'm using the numpy C API (PyArray_SimpleNewFromData) to perform the
>> conversion but my code is written by hands.
>>
>
> I'd like to see that. How are you getting the pointer to pass in to
> PyArray_SimpleNewFromData? It looks like you can do something like:
>
> (VA is a valarray)
> npy_intp *dims
> dims[0] = VA.size()
>
> NPA = PyArray_SimpleNewFromData(1, dims, typenum, &VA[0]);
>
> Is that what you're doing? Is there any guarantee that &VA[0] won't
> change? In any case, I assume that you have to make sure that VA doesn't
> get deleted while the array is still around.
>
>
>> I would like to simplify it using SWIG but I also would like to see a good typemap valarray <=>
>> numpy.array :)
>>
>
> In principle, if you know how to write the code by hand, you know how to
> write the typemap.
>
>
>
Here it is :) OK it is only a smal code but I often use something like
that to debug my C++ code (the goal is just to have a quick look to
larrge array to unnderstand what is going wrong).
#include
#include
#include
using namespace std;
int
main (int argc, char *argv[])
{
PyObject *pylab_module, *pylab_module_dict, *func_imshow, *func_show,
*args,
*result_imshow, *result_show, *array;
int NbDims = 2;
int *Dims;
long NbData;
Dims = new int[NbDims];
Dims[0] = 10;
Dims[1] = 10;
NbData = Dims[0] * Dims[1];
valarray < double >Data (NbData);
for (long i = 0; i < NbData; i++)
{
Data[i] = (double) i / (NbData - 1);
}
Py_Initialize ();
// Needed before any call to PyArray_foo function.
import_array1 (-1);
// New reference to a numpy array
array = PyArray_SimpleNewFromData (NbDims, Dims, PyArray_DOUBLE,
&Data[0]);
pylab_module = PyImport_Import (PyString_FromString ("pylab"));
if (pylab_module)
{
pylab_module_dict = PyModule_GetDict (pylab_module);
if (pylab_module_dict)
{
func_imshow = PyDict_GetItemString (pylab_module_dict, "imshow");
if (func_imshow)
{
func_show = PyDict_GetItemString (pylab_module_dict, "show");
if (func_show)
{
args = PyTuple_New (1);
PyTuple_SetItem (args, 0, array);
result_imshow = PyObject_CallObject (func_imshow, args);
Py_XDECREF (result_imshow); // We dont use the result...
Py_XDECREF (args);
result_show = PyObject_CallObject (func_show, NULL);
Py_XDECREF (result_show); // We dont use the result...
Py_XDECREF (array);
}
}
}
Py_XDECREF (pylab_module);
}
Py_Finalize ();
return 0;
}
"In principle, if you know how to write the code by hand, you know how to
write the typemap."
Yes but I had to spend a bit more time on that. Hand written code I have posted fit my (debug) needs so I decided not to use SWIG.
"Is that what you're doing? Is there any guarantee that &VA[0] won't
change? In any case, I assume that you have to make sure that VA doesn't
get deleted while the array is still around.
"
Yes I have but it is not a problem in my simple use cases.
Xavier
--
############################################
Xavier Gnata
CRAL - Observatoire de Lyon
9, avenue Charles Andr?
69561 Saint Genis Laval cedex
Phone: +33 4 78 86 85 28
Fax: +33 4 78 86 83 86
E-mail: gnata at obs.univ-lyon1.fr
############################################
From gnata at obs.univ-lyon1.fr Sat Sep 8 15:33:58 2007
From: gnata at obs.univ-lyon1.fr (Xavier Gnata)
Date: Sat, 08 Sep 2007 21:33:58 +0200
Subject: [Numpy-discussion] Anyone have a well-tested SWIG-based C++ STL
valarray <=> numpy.array typemap to share?
In-Reply-To: <46DF04E9.2020501@enthought.com>
References: <46DDF734.7070708@noaa.gov> <4CED078D-B8C7-4917-B0DE-4ED60BDFE015@sandia.gov> <46DEE52F.8050000@noaa.gov> <609FEB05-86EB-4FE2-867E-4A1C84105951@sandia.gov> <46DEF965.1000308@noaa.gov>
<46DF04E9.2020501@enthought.com>
Message-ID: <46E2F926.1000400@obs.univ-lyon1.fr>
Bryan Van de Ven wrote:
> Christopher Barker wrote:
>
>
>> Does anyone know the status of support for valarrays now?
>>
>
> I used std::valarray to implement a variant of the example Matrix class in
> Stroustrup's book (2D only) about two years ago. I was aware that is in disuse,
> by and large, but it worked well enough for my purposes and I was happy with it.
> I'm sure it could have been done differently/better.
>
> Bryan
>
Hi,
std:valarray are quite strange containers because they are not well
integrated in the STL.
For instance, you cannot play with maps of valarrays :
http://gcc.gnu.org/ml/gcc-bugs/2006-05/msg02397.html
Note that it is *not* a gcc bug but a design choice. As a result and
beacuse I'm an STL happy user, I always use vector when I have to deal
with arrays (of course matrix lib are great as log as you have to do
some more complicated maths or as long as you have matrix and not only
arrays).
Hum...we are on a python list ;) so I would be very happy to see some
support of std:vector <--> numpy.
Xavier
ps : There are no real performance differences between vector and
valarray (in my use cases...)
--
############################################
Xavier Gnata
CRAL - Observatoire de Lyon
9, avenue Charles Andr?
69561 Saint Genis Laval cedex
Phone: +33 4 78 86 85 28
Fax: +33 4 78 86 83 86
E-mail: gnata at obs.univ-lyon1.fr
############################################
From koepsell at gmail.com Sat Sep 8 18:39:23 2007
From: koepsell at gmail.com (killian koepsell)
Date: Sat, 8 Sep 2007 15:39:23 -0700
Subject: [Numpy-discussion] von mises distribution in numpy.random biased
Message-ID:
hi,
the von mises distribution in numpy.random seems to be biased towards
a higher concentration (kappa). given a concentration of 2, it
produces data that has a concentration of 2.36. i compared the
distribution to the one produced by the CircStats[1] package of R[2]
using RPy [3] and created a figure here:
http://redwood.berkeley.edu/kilian/vonmises.png
the script i used is attached to this email. i don't know what
algorithm NumPy uses, so i can't tell if it is a real bug or some sort
of rounding error. the CircStats package uses the algorithm by Best
and Fisher [4].
kilian
[1] http://cran.r-project.org/src/contrib/Descriptions/CircStats.html
[2] http://www.r-project.org/
[3] http://rpy.sf.net/
[4] Best, D. and Fisher, N. (1979). Efficient simulation of the von
Mises distribution. Applied Statistics, 24, 152-157.
----------------------
vonmises.py
----------------------
from numpy import array,pi,exp,mod,random
from pylab import hist,linspace,figure,clf,plot,subplot,xlim,title,legend
from rpy import r as R
R.library("CircStats")
# compute von Mises distribuion fo two different values of kappa
size = 1e5
kappa1 = 1
x1 = random.vonmises(0,kappa1,size=size)
y1 = mod(R.rvm(size,0,kappa1),2*pi)
kappa2 = 2
x2 = random.vonmises(0,kappa2,size=size)
y2 = mod(R.rvm(size,0,kappa2),2*pi)
def phasedist(x,kappa):
ph = linspace(-pi,pi,50)
vM = R.dvm(ph,0,kappa)
kest = R.A1inv(abs(exp(1j*array(x)).mean()))
vMest = R.dvm(ph,0,kest)
plot(ph,vM,'r',linewidth=2)
plot(ph,vMest,'k:',linewidth=2)
legend(['$\\kappa=%d$'%kappa,'$\\hat{\\kappa}=%1.2f$'%kest])
hist(x,ph,normed=1)
xlim((-pi,pi))
# plot figure
fig=figure(1)
clf()
ax1=subplot(2,2,1)
title('NumPy.random')
phasedist(x1,kappa1)
ax2=subplot(2,2,3)
phasedist(x2,kappa2)
subplot(2,2,2,sharey=ax1)
title('RPy.CircStats')
phasedist(y1,kappa1)
subplot(2,2,4,sharey=ax2)
phasedist(y2,kappa2)
From oliphant at enthought.com Sun Sep 9 00:44:36 2007
From: oliphant at enthought.com (Travis E. Oliphant)
Date: Sat, 08 Sep 2007 23:44:36 -0500
Subject: [Numpy-discussion] von mises distribution in numpy.random biased
In-Reply-To:
References:
Message-ID: <46E37A34.9080103@enthought.com>
killian koepsell wrote:
> hi,
>
> the von mises distribution in numpy.random seems to be biased towards
> a higher concentration (kappa). given a concentration of 2, it
> produces data that has a concentration of 2.36. i compared the
> distribution to the one produced by the CircStats[1] package of R[2]
> using RPy [3] and created a figure here:
>
> http://redwood.berkeley.edu/kilian/vonmises.png
>
> the script i used is attached to this email. i don't know what
> algorithm NumPy uses, so i can't tell if it is a real bug or some sort
> of rounding error. the CircStats package uses the algorithm by Best
> and Fisher [4].
>
Interesting.
The algorithm in NumPy is basically the same algorithm. However, two
things are different about it and the one used in R (and in Python
itself by the way).
1) The two random variates used in the rejection algorithm are drawn
from a uniform on [-1,1]
2) The testing for the sign is done with the same random variable
instead of a new one.
I've updated the source to change both of these behaviors. I suspect
the bias is coming from 1)
Perhaps you could re-run your tests.
-Travis
From orest.kozyar at gmail.com Sun Sep 9 22:36:11 2007
From: orest.kozyar at gmail.com (Orest Kozyar)
Date: Sun, 9 Sep 2007 22:36:11 -0400
Subject: [Numpy-discussion] odd behavior for numpy.ndarray index?
Message-ID: <000401c7f353$5a2a3860$4e07fa12@issphoenix>
In the following output (see below), why would x[1,None] work, but
x[1,None,2] or even x[1,2,None] not work?
Incidentally, I would be very interested in a solution that allows me to
index numpy arrays using a list/iterable that might contain None values. Is
there a straightforward way to do so, or should I just use list
comprehensions (i.e. [x[a] for a in indices if a])?
Thanks!
Orest
In [122]: x = arange(5)
In [123]: x[1]
Out[123]: 1
In [124]: x[None]
Out[124]: array([[0, 1, 2, 3, 4]])
In [125]: x[1,None]
Out[125]: array([1])
In [126]: x[1,None,2]
---------------------------------------------------------------------------
Traceback (most recent call last)
c:\documents\research\programs\python\epl\src\ in
()
: invalid inde
From koepsell at gmail.com Sun Sep 9 22:47:03 2007
From: koepsell at gmail.com (killian koepsell)
Date: Sun, 9 Sep 2007 19:47:03 -0700
Subject: [Numpy-discussion] von mises distribution in numpy.random biased
In-Reply-To: <46E37A34.9080103@enthought.com>
References:
<46E37A34.9080103@enthought.com>
Message-ID:
On 9/8/07, Travis E. Oliphant wrote:
> killian koepsell wrote:
> > hi,
> >
> > the von mises distribution in numpy.random seems to be biased towards
> > a higher concentration (kappa). given a concentration of 2, it
> > produces data that has a concentration of 2.36. i compared the
> > distribution to the one produced by the CircStats[1] package of R[2]
> > using RPy [3] and created a figure here:
> >
> > http://redwood.berkeley.edu/kilian/vonmises.png
> >
>
> Interesting.
>
> The algorithm in NumPy is basically the same algorithm. However, two
> things are different about it and the one used in R (and in Python
> itself by the way).
>
> 1) The two random variates used in the rejection algorithm are drawn
> from a uniform on [-1,1]
> 2) The testing for the sign is done with the same random variable
> instead of a new one.
>
> I've updated the source to change both of these behaviors. I suspect
> the bias is coming from 1)
>
> Perhaps you could re-run your tests.
>
> -Travis
hi travis,
thanks for fixing this so fast. it looks very good now:
http://redwood.berkeley.edu/kilian/vonmises_new.png
kilian
From tim.hochberg at ieee.org Sun Sep 9 22:47:49 2007
From: tim.hochberg at ieee.org (Timothy Hochberg)
Date: Sun, 9 Sep 2007 19:47:49 -0700
Subject: [Numpy-discussion] odd behavior for numpy.ndarray index?
In-Reply-To: <000401c7f353$5a2a3860$4e07fa12@issphoenix>
References: <000401c7f353$5a2a3860$4e07fa12@issphoenix>
Message-ID:
On 9/9/07, Orest Kozyar wrote:
>
> In the following output (see below), why would x[1,None] work, but
> x[1,None,2] or even x[1,2,None] not work?
None is the same thing as newaxis (newaxis is just an alias for None). Armed
with that tidbit, a little perusing of the docs should quickly explain the
behaviour you are seeing.
Incidentally, I would be very interested in a solution that allows me to
> index numpy arrays using a list/iterable that might contain None
> values. Is
> there a straightforward way to do so, or should I just use list
> comprehensions (i.e. [x[a] for a in indices if a])?
You could try something using compress x[compress(indices, indices)] for
example. Whether that's superior to a iterator solution would probably
depend on the problem. I'd probably use fromiter instead of a list
comprehension if I went that route though. There are probably other ways
too. It very much depends on the details and performance requirements of
what you are trying to do.
--
. __
. |-\
.
. tim.hochberg at ieee.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
From Chris.Barker at noaa.gov Mon Sep 10 12:30:05 2007
From: Chris.Barker at noaa.gov (Christopher Barker)
Date: Mon, 10 Sep 2007 09:30:05 -0700
Subject: [Numpy-discussion] Anyone have a well-tested SWIG-based C++ STL
valarray <=> numpy.array typemap to share?
In-Reply-To: <46E2F926.1000400@obs.univ-lyon1.fr>
References: <46DDF734.7070708@noaa.gov>
<4CED078D-B8C7-4917-B0DE-4ED60BDFE015@sandia.gov>
<46DEE52F.8050000@noaa.gov>
<609FEB05-86EB-4FE2-867E-4A1C84105951@sandia.gov>
<46DEF965.1000308@noaa.gov>
<46DF04E9.2020501@enthought.com> <46E2F926.1000400@obs.univ-lyon1.fr>
Message-ID: <46E5710D.4010208@noaa.gov>
Thanks for you input Xavier.
Xavier Gnata wrote:
> std:valarray are quite strange containers because they are not well
> integrated in the STL.
> I always use vector when I have to deal with arrays.
> ps : There are no real performance differences between vector and
> valarray (in my use cases...)
Probably not for our use cases either. However, we're not doing a lot of
STL either, so I'm not sure there is any downside to valarray. It looks
like neither one supports any kind of "view" semantics, so for the
purposes of numpy array wrapping, they really aren't any different.
On the other hand, I don't know if any of the other common array
implementations do either -- boost::multiarray, blitz++, etc.
So I guess we just need to copy data s we move back and forth (which may
not even be a problem -- we haven't gotten it working yet, so have no
idea if there are any performance issues for us)
A nice numpy-array compatible array for C++ would be nice -- but I know
I'm not going to write it!
-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
From Chris.Barker at noaa.gov Mon Sep 10 12:33:44 2007
From: Chris.Barker at noaa.gov (Christopher Barker)
Date: Mon, 10 Sep 2007 09:33:44 -0700
Subject: [Numpy-discussion] Anyone have a well-tested SWIG-based C++ STL
valarray <=> numpy.array typemap to share?
In-Reply-To: <46E2F605.1020103@obs.univ-lyon1.fr>
References: <46DDF734.7070708@noaa.gov>
<32632B72-2B1A-4AC2-AA04-288696E37A46@ster.kuleuven.be>
<46DE9C30.8020300@obs.univ-lyon1.fr> <46E07AC7.2010001@noaa.gov>
<46E2F605.1020103@obs.univ-lyon1.fr>
Message-ID: <46E571E8.1070908@noaa.gov>
Xavier Gnata wrote:
> Here it is :)
Thanks, that's helpful. Am I reading it right? Are you running the
python process embedded in your C++ app? (rather than extending?)
> valarray < double >Data (NbData);
> array = PyArray_SimpleNewFromData (NbDims, Dims, PyArray_DOUBLE,
> &Data[0]);
OK, so you've now got a view of the data from the valarray. Nice to know
this works, but, of course, fragile if the valarray is re-sized or
anything, so it probably won't work for us.
-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
From matthieu.brucher at gmail.com Mon Sep 10 14:59:30 2007
From: matthieu.brucher at gmail.com (Matthieu Brucher)
Date: Mon, 10 Sep 2007 20:59:30 +0200
Subject: [Numpy-discussion] Anyone have a well-tested SWIG-based C++ STL
valarray <=> numpy.array typemap to share?
In-Reply-To: <46E571E8.1070908@noaa.gov>
References: <46DDF734.7070708@noaa.gov>
<32632B72-2B1A-4AC2-AA04-288696E37A46@ster.kuleuven.be>
<46DE9C30.8020300@obs.univ-lyon1.fr> <46E07AC7.2010001@noaa.gov>
<46E2F605.1020103@obs.univ-lyon1.fr> <46E571E8.1070908@noaa.gov>
Message-ID:
>
> OK, so you've now got a view of the data from the valarray. Nice to know
> this works, but, of course, fragile if the valarray is re-sized or
> anything, so it probably won't work for us.
>
Unless you use a special allocator/desallocator (I don't know if the latter
is possible), I don't know how you could first correctly share the pointer
(as you said, if the valarray is resized or deleted, you lose your memory).
If you want to have a correct implementation (robust to array deletion,
resizing in Python and C++), you should use shared pointers, and I don't
know if you can build a vector with a shared pointer (perhaps with the
special allocator/desallocator, but then it does not work with std::set,
std::list or std::map).
Matthieu
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
From gnata at obs.univ-lyon1.fr Mon Sep 10 16:11:28 2007
From: gnata at obs.univ-lyon1.fr (Xavier Gnata)
Date: Mon, 10 Sep 2007 22:11:28 +0200
Subject: [Numpy-discussion] Anyone have a well-tested SWIG-based C++ STL
valarray <=> numpy.array typemap to share?
In-Reply-To: <46E571E8.1070908@noaa.gov>
References: <46DDF734.7070708@noaa.gov> <32632B72-2B1A-4AC2-AA04-288696E37A46@ster.kuleuven.be> <46DE9C30.8020300@obs.univ-lyon1.fr>
<46E07AC7.2010001@noaa.gov> <46E2F605.1020103@obs.univ-lyon1.fr>
<46E571E8.1070908@noaa.gov>
Message-ID: <46E5A4F0.1040605@obs.univ-lyon1.fr>
Christopher Barker wrote:
> Xavier Gnata wrote:
>
>> Here it is :)
>>
>
> Thanks, that's helpful. Am I reading it right? Are you running the
> python process embedded in your C++ app? (rather than extending?)
>
>
Yes! The point is this way I'm able to debug my C++ code plotting the
array using matplotlib :)
That is cool ;).
>> valarray < double >Data (NbData);
>>
>
>
>> array = PyArray_SimpleNewFromData (NbDims, Dims, PyArray_DOUBLE,
>> &Data[0]);
>>
>
> OK, so you've now got a view of the data from the valarray. Nice to know
> this works, but, of course, fragile if the valarray is re-sized or
> anything, so it probably won't work for us.
>
>
Yep it is not robust at all because the valarray can be modify.
However, it is a a quite great way to plot an array in a C++ code.
Nothing more.
If you want to try with shared pointers, maybe you should have a look at
the boost lib.
Xavier
--
############################################
Xavier Gnata
CRAL - Observatoire de Lyon
9, avenue Charles Andr?
69561 Saint Genis Laval cedex
Phone: +33 4 78 86 85 28
Fax: +33 4 78 86 83 86
E-mail: gnata at obs.univ-lyon1.fr
############################################
From gnata at obs.univ-lyon1.fr Mon Sep 10 16:20:06 2007
From: gnata at obs.univ-lyon1.fr (Xavier Gnata)
Date: Mon, 10 Sep 2007 22:20:06 +0200
Subject: [Numpy-discussion] Anyone have a well-tested SWIG-based C++ STL
valarray <=> numpy.array typemap to share?
In-Reply-To: <46E1F98E.6070408@noaa.gov>
References: <46DDF734.7070708@noaa.gov>
<46E1F98E.6070408@noaa.gov>
Message-ID: <46E5A6F6.9090706@obs.univ-lyon1.fr>
yeah! Looks good!
Thanks a lot.
Xavier
> Thanks!
>
> DG
>
> Lisandro Dalcin wrote:
>
>> David, I'll try to show you what I do for a custom C++ class, of
>> course this does not solve the issue resizing (my class does not
>> actually support resizing, so this is fine for me):
>>
>> My custom class is a templatized one called DTable (is like a 2d
>> contiguous array), but currently I only instantiate it for 'int' and
>> 'double'. This class have two relevant methods: getShape(), returning
>> a std::pair, and getArray(), returning a reference to an underliing
>> std::vector. So I am able to automatically support array interface by
>> using this:
>>
>> First, I define a templatized utility function 'array_interface'
>>
>> %header %{
>> namespace numpy {
>>
>> template static char typechar() { return '\0'; }
>> template<> static char typechar() { return 'i'; }
>> template<> static char typechar() { return 'f'; }
>>
>> template
>> static PyObject*
>> array_interface(const DTable* self)
>> {
>> const std::pair& shape = self->getShape();
>> const std::vector& data = self->getArray();
>> void* array = const_cast(&data[0]);
>> char endian = PyArray_NATIVE;
>> char kind = typechar();
>> int elsize = sizeof(T);
>> return Py_BuildValue("{sNsNsNsN}",
>> "shape", Py_BuildValue("ii", shape.first, shape.second),
>> "typestr", PyString_FromFormat("%c%c%d", endian, kind, elsize),
>> "data", Py_BuildValue("NO", PyLong_FromVoidPtr(array), Py_False),
>> "version", PyInt_FromLong(3));
>> }
>> }
>> %}
>>
>> Now define a SWIG macro to apply it to instantiations of my class
>>
>> %define %array_interface(Class)
>> %extend Class { PyObject* __array_interface__; }
>> %{
>> #define %mangle(Class) ##_## __array_interface__ ## _get(_t) \
>> numpy::array_interface(_t)
>> #define %mangle(Class) ##_## __array_interface__ ## _set(_t, _val) \
>> SWIG_exception_fail(SWIG_AttributeError, "read-only attribute")
>> %}
>> %enddef
>>
>> and finally instantiate my class with different names for 'int' and
>> 'double' in SWIG and finally apply previous macro
>>
>>
>> %template(DTableI) DTable;
>> %template(DTableS) DTable;
>>
>> %array_interface( DTable );
>> %array_interface( DTable );
>>
>> I think you can implement someting similar for std::vector,
>> std::valarray, or whatever... For use other data types, the only you
>> need is to specialize the 'typecode' function.
>>
>> Hope this help you.
>>
>>
>> On 9/4/07, David Goldsmith wrote:
>>
>>
>>> Anyone have a well-tested SWIG-based C++ STL valarray <=> numpy.array
>>> typemap to share? Thanks!
>>>
>>> DG
>>> --
>>> ERD/ORR/NOS/NOAA
>>> _______________________________________________
>>> Numpy-discussion mailing list
>>> Numpy-discussion at scipy.org
>>> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>>>
>>>
>>>
>>
>>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>
>
--
############################################
Xavier Gnata
CRAL - Observatoire de Lyon
9, avenue Charles Andr?
69561 Saint Genis Laval cedex
Phone: +33 4 78 86 85 28
Fax: +33 4 78 86 83 86
E-mail: gnata at obs.univ-lyon1.fr
############################################
From Chris.Barker at noaa.gov Mon Sep 10 19:56:28 2007
From: Chris.Barker at noaa.gov (Christopher Barker)
Date: Mon, 10 Sep 2007 16:56:28 -0700
Subject: [Numpy-discussion] how to include numpy headers when building an
extension?
Message-ID: <46E5D9AC.8040206@noaa.gov>
Hi all,
I'm porting an extension from Numeric. At the top, I've changed:
#include
to
#include
But distutils can't find numpy/arrayobject.h -- how do I tell distutils
where to look for it? I've gotten it to work by hard-coding the entire
path, but that's not very portable.
It looks like Numeric used to put its headers in a standard location,
but that numpy buries them deeper.
thanks,
-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
From robert.kern at gmail.com Mon Sep 10 20:03:26 2007
From: robert.kern at gmail.com (Robert Kern)
Date: Mon, 10 Sep 2007 19:03:26 -0500
Subject: [Numpy-discussion] how to include numpy headers when building
an extension?
In-Reply-To: <46E5D9AC.8040206@noaa.gov>
References: <46E5D9AC.8040206@noaa.gov>
Message-ID: <46E5DB4E.3090008@gmail.com>
Christopher Barker wrote:
> Hi all,
>
> I'm porting an extension from Numeric. At the top, I've changed:
>
> #include
>
> to
>
> #include
>
> But distutils can't find numpy/arrayobject.h -- how do I tell distutils
> where to look for it? I've gotten it to work by hard-coding the entire
> path, but that's not very portable.
numpy.get_include()
--
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
From Chris.Barker at noaa.gov Tue Sep 11 01:57:22 2007
From: Chris.Barker at noaa.gov (Christopher Barker)
Date: Mon, 10 Sep 2007 22:57:22 -0700
Subject: [Numpy-discussion] how to include numpy headers when building
an extension?
In-Reply-To: <46E5DB4E.3090008@gmail.com>
References: <46E5D9AC.8040206@noaa.gov> <46E5DB4E.3090008@gmail.com>
Message-ID: <46E62E42.7030308@noaa.gov>
Robert Kern wrote:
> Christopher Barker wrote:
>> how do I tell distutils
>> where to look for it?
> numpy.get_include()
Ah, got it. thanks.
I know this has been discussed, but why doesn't numpy put its includes
somewhere that distutils would know where to find it?
-Chris
--
Christopher Barker, Ph.D.
Oceanographer
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
From oliphant at enthought.com Tue Sep 11 02:35:49 2007
From: oliphant at enthought.com (Travis E. Oliphant)
Date: Tue, 11 Sep 2007 01:35:49 -0500
Subject: [Numpy-discussion] how to include numpy headers when building
an extension?
In-Reply-To: <46E62E42.7030308@noaa.gov>
References: <46E5D9AC.8040206@noaa.gov> <46E5DB4E.3090008@gmail.com>
<46E62E42.7030308@noaa.gov>
Message-ID: <46E63745.1060306@enthought.com>
Christopher Barker wrote:
>
> I know this has been discussed, but why doesn't numpy put its includes
> somewhere that distutils would know where to find it?
>
I think one answer is because distutils doesn't have defaults that play
well with eggs. NumPy provides very nice extensions to distutils which
will correctly add the include directories you need.
Look at any of the setup.py files for scipy for examples of how to use
numpy.distutils. Once you convert your setup.py files to use
numpy.distutils there really isn't any problem anymore about where numpy
puts its include files and the benefit gained by being able to use
versioned eggs is worth it.
Yes, the transition is a little messy (especially if the build is not
using distutils at all). But, numpy.get_include() really helps
mitigate that pain.
Thanks for the comments,
-Travis
>
>
From david at ar.media.kyoto-u.ac.jp Tue Sep 11 03:13:00 2007
From: david at ar.media.kyoto-u.ac.jp (David Cournapeau)
Date: Tue, 11 Sep 2007 16:13:00 +0900
Subject: [Numpy-discussion] how to include numpy headers when building
an extension?
In-Reply-To: <46E63745.1060306@enthought.com>
References: <46E5D9AC.8040206@noaa.gov>
<46E5DB4E.3090008@gmail.com> <46E62E42.7030308@noaa.gov>
<46E63745.1060306@enthought.com>
Message-ID: <46E63FFC.2020900@ar.media.kyoto-u.ac.jp>
Travis E. Oliphant wrote:
> Christopher Barker wrote:
>
>> I know this has been discussed, but why doesn't numpy put its includes
>> somewhere that distutils would know where to find it?
>>
>>
> I think one answer is because distutils doesn't have defaults that play
> well with eggs. NumPy provides very nice extensions to distutils which
> will correctly add the include directories you need.
>
Concerning numpy.distutils, is anyone working on improving it ? There
was some work started, but I have not seen any news on this front: am I
missing something ? I would really like to have the possibility to
compile custom extensions to be used through ctypes, and didn't go very
far just by myself, unfortunately (understanding distutils is not
trivial, to say the least).
David
From Chris.Barker at noaa.gov Tue Sep 11 11:53:36 2007
From: Chris.Barker at noaa.gov (Christopher Barker)
Date: Tue, 11 Sep 2007 08:53:36 -0700
Subject: [Numpy-discussion] how to include numpy headers when building
an extension?
In-Reply-To: <46E63745.1060306@enthought.com>
References: <46E5D9AC.8040206@noaa.gov> <46E5DB4E.3090008@gmail.com>
<46E62E42.7030308@noaa.gov> <46E63745.1060306@enthought.com>
Message-ID: <46E6BA00.4000603@noaa.gov>
Travis E. Oliphant wrote:
> I think one answer is because distutils doesn't have defaults that play
> well with eggs.
Fair enough -- anyone know about the progress of integrating setuptool
into the standard library?
> Look at any of the setup.py files for scipy for examples of how to use
> numpy.distutils.
I'll do that. I guess I was under the impression that numpy.distutils
was fro building numpy itself. I don't know why it didn't dawn on my to
give it a try.
> Yes, the transition is a little messy (especially if the build is not
> using distutils at all).
Well, yes, but I learned that lesson a long time ago.
[OT: why the heck do none of the SWIG docs give examples (or even
suggest) using distutils for building SWIG extensions???]
-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
From a.schmolck at gmx.net Tue Sep 11 12:41:17 2007
From: a.schmolck at gmx.net (Alexander Schmolck)
Date: Tue, 11 Sep 2007 17:41:17 +0100
Subject: [Numpy-discussion] numpy.ndarrays as C++ arrays (wrapped with boost)
Message-ID:
Hi,
I've sent pretty much the same email to c++sig, but I thought I'd also try my
luck here, especially since I just saw a closely related question posted one
week ago here (albeit mostly from a swig context).
I'm working working on an existing scientific code base that's mostly C++ and
I'm currently interfacing it to python with boost.python with a view to doing
non-performance critical things in python. The code currently mostly just uses
plain C double arrays passed around by pointers and I'd like to encapsulate
this at least with something like stl::vector (or maybe valarray), but I've
been wondering whether it might not make sense to use (slightly wrapped) numpy
ndarrays -- since I eventually plan to make fairly heavy use of existing
python infrastructure like matplotlib and scipy where possible. Also, ndarrays
provide fairly rich functionality even at the C-API-level and I'm pretty
familiar with numpy. Furthermore I can't find something equivalent to numpy
for C++ -- there's ublas as well as several other matrix libs and a couple of
array ones (like blitz++), but there doesn't seem to be one obvious choice, as
there is for python. I think I will mostly use double arrays of fairly large
size, so having really low overhead operations on small arrays with more or
less exotic types is not important to me.
Things that would eventually come in handy, although they're not needed yet,
are basic linear algebra and maybe two or three LAPACK-level functions (I can
think of cholesky decomposition and SVD right now) as well as possibly
wavelets (DWT). I think I could get all these things (and more) from scipy
(and kin) with too much fuzz (although I haven't tried wavelet support yet)
and it seems like picking together the same functionality from different C++
libs would require considerably more work.
So my question is: might it make sense to use (a slightly wrapped)
numpy.ndarray, and if so is some code already floating around for that (on
first glance it seems like there's a bit of support for the obsolete Numeric
package in boost, but none for the newer numpy that supercedes it); if not is
my impression correct that making the existing code numpy compatible shouldn't
be too much work.
Provided this route doesn't make much sense, I'd also be curious what people
would recommend doing instead.
In last week's thread mentioned above I found the following link which looks
pretty relevant, albeit essentially undocumented and possibly pre-alpha -- has
anyone here tried it out?
many thanks,
'as
From paustin at eos.ubc.ca Tue Sep 11 13:46:31 2007
From: paustin at eos.ubc.ca (Philip Austin)
Date: Tue, 11 Sep 2007 10:46:31 -0700
Subject: [Numpy-discussion] numpy.ndarrays as C++ arrays (wrapped with
boost)
In-Reply-To:
References:
Message-ID: <18150.54391.274768.497392@owl.eos.ubc.ca>
Alexander Schmolck writes:
> So my question is: might it make sense to use (a slightly wrapped)
> numpy.ndarray, and if so is some code already floating around for that (on
> first glance it seems like there's a bit of support for the obsolete Numeric
> package in boost, but none for the newer numpy that supercedes it); if not is
> my impression correct that making the existing code numpy compatible shouldn't
> be too much work.
Right, it should work more or less as is if you just do:
set_module_and_type("numpy", "ArrayType");
in the examples. Some tests will fail because of numpy changes to
function signatures, etc.
The current library doesn't wrap numpy.zeros, numpy.ones or
numpy.empty constructors, so the only way to construct an empty
is to pass the constructor a tuple and then resize. Because
it (by design) doesn't include arrayobject.h, there's also no clean
way to get at the underlying data pointer to share the memory. You
can use helper functions like this, though:
//Create a one-dimensional numpy array of length n and type t
boost::python::numeric::array makeNum(intp n, PyArray_TYPES t=PyArray_DOUBLE){
object obj(handle<>(PyArray_FromDims(1, &n, t)));
return extract(obj);
}
http://www.eos.ubc.ca/research/clouds/software/pythonlibs/num_util/num_util_release2/Readme.html
has more examples -- Phil
From oliphant at enthought.com Tue Sep 11 14:43:36 2007
From: oliphant at enthought.com (Travis E. Oliphant)
Date: Tue, 11 Sep 2007 13:43:36 -0500
Subject: [Numpy-discussion] how to include numpy headers when building
an extension?
In-Reply-To: <46E6BA00.4000603@noaa.gov>
References: <46E5D9AC.8040206@noaa.gov>
<46E5DB4E.3090008@gmail.com> <46E62E42.7030308@noaa.gov>
<46E63745.1060306@enthought.com> <46E6BA00.4000603@noaa.gov>
Message-ID: <46E6E1D8.3040707@enthought.com>
Christopher Barker wrote:
> [OT: why the heck do none of the SWIG docs give examples (or even
> suggest) using distutils for building SWIG extensions???]
>
I'm pretty sure this is because the people who contributed to the SWIG
docs the most are not currently using numpy.distutils (but probably
should be) --- I suspect they were in your same boat of not realizing
that numpy.distutils makes it easier to build packages that depend on
numpy in general.
-Travis
From Chris.Barker at noaa.gov Tue Sep 11 15:02:14 2007
From: Chris.Barker at noaa.gov (Christopher Barker)
Date: Tue, 11 Sep 2007 12:02:14 -0700
Subject: [Numpy-discussion] how to include numpy headers when building
an extension?
In-Reply-To: <46E6E1D8.3040707@enthought.com>
References: <46E5D9AC.8040206@noaa.gov> <46E5DB4E.3090008@gmail.com>
<46E62E42.7030308@noaa.gov> <46E63745.1060306@enthought.com>
<46E6BA00.4000603@noaa.gov> <46E6E1D8.3040707@enthought.com>
Message-ID: <46E6E636.8060300@noaa.gov>
Travis E. Oliphant wrote:
> Christopher Barker wrote:
>> [OT: why the heck do none of the SWIG docs give examples (or even
>> suggest) using distutils for building SWIG extensions???]
>
> I'm pretty sure this is because the people who contributed to the SWIG
> docs the most are not currently using numpy.distutils
Actually, I'm talking about the general SWIG docs -- that have nothing
to do with numpy at all. They've got you calling gcc on the command
line, which I haven't done for years!
But while we're on the topic, I started this be looking at the swig
examples that come with numpy. It would be really great if there was a
complete, very simple, sample or two complete with c code, *.i files,
and setup.py. There are the tests, which built and ran fine for me, but
that's a bit of a complicated mess.
Anyway, I'm working on some simple samples -- I'll contribute them when
I get them working!
OT:
The struggle at the moment is building a C++ extension -- if I use
straight C, a trivial sample works, if I call the file *.cpp, swig it
with -c++, I get a *_wrap.cxx file that won't build. Do I have to do
something different in the setup.py?
-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
From wfspotz at sandia.gov Tue Sep 11 15:53:15 2007
From: wfspotz at sandia.gov (Bill Spotz)
Date: Tue, 11 Sep 2007 13:53:15 -0600
Subject: [Numpy-discussion] how to include numpy headers when building
an extension?
In-Reply-To: <46E6E636.8060300@noaa.gov>
References: <46E5D9AC.8040206@noaa.gov> <46E5DB4E.3090008@gmail.com>
<46E62E42.7030308@noaa.gov> <46E63745.1060306@enthought.com>
<46E6BA00.4000603@noaa.gov> <46E6E1D8.3040707@enthought.com>
<46E6E636.8060300@noaa.gov>
Message-ID:
On Sep 11, 2007, at 1:02 PM, Christopher Barker wrote:
> There are the tests, which built and ran fine for me, but
> that's a bit of a complicated mess.
There is a combinatorics problem there that make the tests difficult
to follow. Lots of nested macros and such.
> Anyway, I'm working on some simple samples -- I'll contribute them
> when
> I get them working!
Great. I have been considering changing the directory structure of
numpy/doc/swig to include subdirectories: doc/, test/, and now
example/, if there are some. Some people were confused by its
current structure, which just lumps everything together. Let me know
when you have a working example.
> OT:
> The struggle at the moment is building a C++ extension -- if I use
> straight C, a trivial sample works, if I call the file *.cpp, swig it
> with -c++, I get a *_wrap.cxx file that won't build. Do I have to do
> something different in the setup.py?
I use numpy.i with C++ all the time. Let me know the specific nature
of your problem and I'll look into it.
** Bill Spotz **
** Sandia National Laboratories Voice: (505)845-0170 **
** P.O. Box 5800 Fax: (505)284-5451 **
** Albuquerque, NM 87185-0370 Email: wfspotz at sandia.gov **
From cookedm at physics.mcmaster.ca Tue Sep 11 16:49:59 2007
From: cookedm at physics.mcmaster.ca (David M. Cooke)
Date: Tue, 11 Sep 2007 16:49:59 -0400
Subject: [Numpy-discussion] how to include numpy headers when building
an extension?
In-Reply-To: <46E63FFC.2020900@ar.media.kyoto-u.ac.jp> (David Cournapeau's
message of "Tue\, 11 Sep 2007 16\:13\:00 +0900")
References: <46E5D9AC.8040206@noaa.gov> <46E5DB4E.3090008@gmail.com>
<46E62E42.7030308@noaa.gov> <46E63745.1060306@enthought.com>
<46E63FFC.2020900@ar.media.kyoto-u.ac.jp>
Message-ID:
David Cournapeau writes:
> Travis E. Oliphant wrote:
>> Christopher Barker wrote:
>>
>>> I know this has been discussed, but why doesn't numpy put its includes
>>> somewhere that distutils would know where to find it?
>>>
>>>
>> I think one answer is because distutils doesn't have defaults that play
>> well with eggs. NumPy provides very nice extensions to distutils which
>> will correctly add the include directories you need.
>>
> Concerning numpy.distutils, is anyone working on improving it ? There
> was some work started, but I have not seen any news on this front: am I
> missing something ? I would really like to have the possibility to
> compile custom extensions to be used through ctypes, and didn't go very
> far just by myself, unfortunately (understanding distutils is not
> trivial, to say the least).
I work on it off and on. As you say, it's not trivial :-) It also has
a tendency to be fragile, so large changes are harder. Something will work
for me, then I merge it into the trunk, and it breaks on half-a-dozen
platforms that I can't test on :-) So, it's slow going.
I've got a list of my current goals at
http://scipy.org/scipy/numpy/wiki/DistutilsRevamp.
--
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke http://arbutus.physics.mcmaster.ca/dmc/
|cookedm at physics.mcmaster.ca
From mike.ressler at alum.mit.edu Tue Sep 11 18:11:53 2007
From: mike.ressler at alum.mit.edu (Mike Ressler)
Date: Tue, 11 Sep 2007 15:11:53 -0700
Subject: [Numpy-discussion] Slicing/selection in multiple dimensions
simultaneously
Message-ID: <268febdf0709111511n3ca15d42o85d31831178d96a@mail.gmail.com>
The following seems to be a wart: is it expected?
Set up a 10x10 array and some indexing arrays:
a=arange(100)
a.shape=(10,10)
q=array([0,2,4,6,8])
r=array([0,5])
Suppose I want to extract only the "even" numbered rows from a - then
print a[q,:]
Every fifth column:
print a[:,r]
Only the even rows of every fifth column:
print a[q,r]
---------------------------------------------------------------------------
Traceback (most recent call last)
/.../.../.../ in ()
: shape mismatch: objects cannot be
broadcast to a single shape
But, this works:
print a[q,:][:,r]
[[ 0 5]
[20 25]
[40 45]
[60 65]
[80 85]]
So why does the a[q,r] form have problems? Thanks for your insights.
Mike
--
mike.ressler at alum.mit.edu
From robert.kern at gmail.com Tue Sep 11 18:24:17 2007
From: robert.kern at gmail.com (Robert Kern)
Date: Tue, 11 Sep 2007 17:24:17 -0500
Subject: [Numpy-discussion] Slicing/selection in multiple
dimensions simultaneously
In-Reply-To: <268febdf0709111511n3ca15d42o85d31831178d96a@mail.gmail.com>
References: <268febdf0709111511n3ca15d42o85d31831178d96a@mail.gmail.com>
Message-ID: <46E71591.20802@gmail.com>
Mike Ressler wrote:
> The following seems to be a wart: is it expected?
>
> Set up a 10x10 array and some indexing arrays:
>
> a=arange(100)
> a.shape=(10,10)
> q=array([0,2,4,6,8])
> r=array([0,5])
>
> Suppose I want to extract only the "even" numbered rows from a - then
>
> print a[q,:]
>
>
>
> Every fifth column:
>
> print a[:,r]
>
>
>
> Only the even rows of every fifth column:
>
> print a[q,r]
>
> ---------------------------------------------------------------------------
> Traceback (most recent call last)
>
> /.../.../.../ in ()
>
> : shape mismatch: objects cannot be
> broadcast to a single shape
>
> But, this works:
>
> print a[q,:][:,r]
>
> [[ 0 5]
> [20 25]
> [40 45]
> [60 65]
> [80 85]]
>
> So why does the a[q,r] form have problems? Thanks for your insights.
It is intended that the form a[q,r] be the general case: q and r are broadcasted
against each other to a single shape. The result of the indexing is an array of
that broadcasted shape with elements found by using each pair of elements in the
broadcasted q and r arrays as indices.
There are operations you can express with this form that you couldn't if the
behavior that you expected were the case whereas you can get the result you want
relatively straightforwardly.
In [6]: a[q[:,newaxis], r]
Out[6]:
array([[ 0, 5],
[20, 25],
[40, 45],
[60, 65],
[80, 85]])
--
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
From a.schmolck at gmx.net Tue Sep 11 18:25:45 2007
From: a.schmolck at gmx.net (Alexander Schmolck)
Date: Tue, 11 Sep 2007 23:25:45 +0100
Subject: [Numpy-discussion] numpy.ndarrays as C++ arrays (wrapped with
boost)
In-Reply-To: <18150.54391.274768.497392@owl.eos.ubc.ca> (Philip Austin's
message of "Tue\, 11 Sep 2007 10\:46\:31 -0700")
References:
<18150.54391.274768.497392@owl.eos.ubc.ca>
Message-ID:
Philip Austin writes:
> Alexander Schmolck writes:
>
> > So my question is: might it make sense to use (a slightly wrapped)
> > numpy.ndarray, and if so is some code already floating around for that (on
> > first glance it seems like there's a bit of support for the obsolete Numeric
> > package in boost, but none for the newer numpy that supercedes it); if not is
> > my impression correct that making the existing code numpy compatible shouldn't
> > be too much work.
>
> Right, it should work more or less as is if you just do:
>
> set_module_and_type("numpy", "ArrayType");
Ah, I guess that's the advantage of going via python, rather than calling the
C-api directly (although I assume it must be rather costly).
>
> in the examples. Some tests will fail because of numpy changes to
> function signatures, etc.
>
> The current library doesn't wrap numpy.zeros, numpy.ones or
> numpy.empty constructors, so the only way to construct an empty
> is to pass the constructor a tuple and then resize. Because
> it (by design) doesn't include arrayobject.h, there's also no clean
> way to get at the underlying data pointer to share the memory.
Not being able to get at the data-pointer sounds like a show-stopper for this
purpose -- I will almost certainly need to interface to exisitng C and C++
code, and I do not intend to copy hundres of MB around unnecessarily.
I think it is a real shame that boost currently doesn't properly support numpy
out of the box, although numpy has long obsoleted both numarray and Numeric
(which is both buggy and completely unsupported). All the more so since
writing multimedial or scientific extensions (in which numpy's array interface
is very natural to figure prominently) would seem such an ideal use for
boost.python, as soon as complex classes or compound structures that need to
efficiently support several (primitive) datatypes are involved, boost.python
could really play its strenghts compared to Fortran/C based extensions.
I've since stumbled (in the numpy-list) upon
which seems like it could offer a great fit for my needs, but unfortunately
it's not really documented and there's also no indication how ready for use it
is -- I'd be interested to hear if anyone has an experience-report to offer;
if not I guess I might just end up settling for std::vector for the time
being, I need something workable soon, and it doesn't look like it'd be able
to figure it out and verify that it works for me without a substantial time
investment.
> You can use helper functions like this, though:
>
> //Create a one-dimensional numpy array of length n and type t
> boost::python::numeric::array makeNum(intp n, PyArray_TYPES t=PyArray_DOUBLE){
> object obj(handle<>(PyArray_FromDims(1, &n, t)));
> return extract(obj);
> }
>
> http://www.eos.ubc.ca/research/clouds/software/pythonlibs/num_util/num_util_release2/Readme.html
Thanks! This looks rather useful, I will try it on some data I need to convert
to pass to python tomorrow.
alex
From tim.hochberg at ieee.org Tue Sep 11 18:42:07 2007
From: tim.hochberg at ieee.org (Timothy Hochberg)
Date: Tue, 11 Sep 2007 15:42:07 -0700
Subject: [Numpy-discussion] Slicing/selection in multiple dimensions
simultaneously
In-Reply-To: <46E71591.20802@gmail.com>
References: <268febdf0709111511n3ca15d42o85d31831178d96a@mail.gmail.com>
<46E71591.20802@gmail.com>
Message-ID:
On 9/11/07, Robert Kern wrote:
>
> Mike Ressler wrote:
> > The following seems to be a wart: is it expected?
> >
> > Set up a 10x10 array and some indexing arrays:
> >
> > a=arange(100)
> > a.shape=(10,10)
> > q=array([0,2,4,6,8])
> > r=array([0,5])
> >
> > Suppose I want to extract only the "even" numbered rows from a - then
> >
> > print a[q,:]
> >
> >
> >
> > Every fifth column:
> >
> > print a[:,r]
> >
> >
> >
> > Only the even rows of every fifth column:
> >
> > print a[q,r]
> >
> >
> ---------------------------------------------------------------------------
> > Traceback (most recent call
> last)
> >
> > /.../.../.../ in ()
> >
> > : shape mismatch: objects cannot be
> > broadcast to a single shape
> >
> > But, this works:
> >
> > print a[q,:][:,r]
> >
> > [[ 0 5]
> > [20 25]
> > [40 45]
> > [60 65]
> > [80 85]]
> >
> > So why does the a[q,r] form have problems? Thanks for your insights.
>
> It is intended that the form a[q,r] be the general case: q and r are
> broadcasted
> against each other to a single shape. The result of the indexing is an
> array of
> that broadcasted shape with elements found by using each pair of elements
> in the
> broadcasted q and r arrays as indices.
>
> There are operations you can express with this form that you couldn't if
> the
> behavior that you expected were the case whereas you can get the result
> you want
> relatively straightforwardly.
>
> In [6]: a[q[:,newaxis], r]
> Out[6]:
> array([[ 0, 5],
> [20, 25],
> [40, 45],
> [60, 65],
> [80, 85]])
At the risk of making Robert grumpy: while it is true the form we ended up
with is more general I've come to the conclusion that it was a bit of a
mistake. In the spirit of making simple things simple and complex things
possible, I suspect that having fancy-indexing do the obvious thing here[1]
and delegating the more powerful but also more difficult to understand case
to a function or method would have been overall more useful. Cases where the
multidimensional features of fancy-indexing get used are messy enough that
they don't benefit much from the conciseness of the indexing notation, at
least in my experience.
--
. __
. |-\
.
. tim.hochberg at ieee.org
[1] Just in case the 'obvious' thing isn't all that obvious: I mean restrict
index-arrays to one dimension and have them simply select the given values
along the axis. Hmmm. Without giving examples, which I have not time for
right now, that's probably not any clearer than saying nothing. Ah well.
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
From mike.ressler at alum.mit.edu Tue Sep 11 18:49:41 2007
From: mike.ressler at alum.mit.edu (Mike Ressler)
Date: Tue, 11 Sep 2007 15:49:41 -0700
Subject: [Numpy-discussion] Slicing/selection in multiple dimensions
simultaneously
In-Reply-To: <46E71591.20802@gmail.com>
References: <268febdf0709111511n3ca15d42o85d31831178d96a@mail.gmail.com>
<46E71591.20802@gmail.com>
Message-ID: <268febdf0709111549gf3996b4re10109c6d0a233d1@mail.gmail.com>
Thanks, Robert, for the quick response.
On 9/11/07, Robert Kern wrote:
> There are operations you can express with this form that you couldn't if the
> behavior that you expected were the case whereas you can get the result you want
> relatively straightforwardly.
>
> In [6]: a[q[:,newaxis], r]
Ah, yes, of course. I forgot my broadcasting rules. However, I also
thank Timothy Hochberg for the moral support of agreeing that the
obvious way (a[q,r]) should be reasonably expected to work.
Thanks again for your help - the routine is now working nicely.
Mike
--
mike.ressler at alum.mit.edu
From oliphant at enthought.com Tue Sep 11 19:10:27 2007
From: oliphant at enthought.com (Travis E. Oliphant)
Date: Tue, 11 Sep 2007 18:10:27 -0500
Subject: [Numpy-discussion] numpy.ndarrays as C++ arrays (wrapped with
boost)
In-Reply-To:
References: <18150.54391.274768.497392@owl.eos.ubc.ca>
Message-ID: <46E72063.1060408@enthought.com>
> nd to copy hundres of MB around unnecessarily.
>
> I think it is a real shame that boost currently doesn't properly support numpy
> out of the box, although numpy has long obsoleted both numarray and Numeric
> (which is both buggy and completely unsupported). All the more so since
> writing multimedial or scientific extensions (in which numpy's array interface
> is very natural to figure prominently) would seem such an ideal use for
> boost.python, as soon as complex classes or compound structures that need to
> efficiently support several (primitive) datatypes are involved, boost.python
> could really play its strenghts compared to Fortran/C based extensions.
>
>
I think it could be that boost.python is waiting for the extended buffer
interface which is coming in Python 3.0 and Python 2.6. This would
really be ideal for wrapping external code in a form that plays well
with other libraries.
-Travis O.
From oliphant at enthought.com Tue Sep 11 19:13:26 2007
From: oliphant at enthought.com (Travis E. Oliphant)
Date: Tue, 11 Sep 2007 18:13:26 -0500
Subject: [Numpy-discussion] Slicing/selection in multiple
dimensions simultaneously
In-Reply-To:
References: <268febdf0709111511n3ca15d42o85d31831178d96a@mail.gmail.com> <46E71591.20802@gmail.com>
Message-ID: <46E72116.8040408@enthought.com>
Timothy Hochberg wrote:
>
>
> On 9/11/07, *Robert Kern* > wrote:
>
> Mike Ressler wrote:
> > The following seems to be a wart: is it expected?
> >
> > Set up a 10x10 array and some indexing arrays:
> >
> > a=arange(100)
> > a.shape=(10,10)
> > q=array([0,2,4,6,8])
> > r=array([0,5])
> >
> > Suppose I want to extract only the "even" numbered rows from a -
> then
> >
> > print a[q,:]
> >
> >
> >
> > Every fifth column:
> >
> > print a[:,r]
> >
> >
> >
> > Only the even rows of every fifth column:
> >
> > print a[q,r]
> >
> >
> ---------------------------------------------------------------------------
>
> > Traceback (most recent
> call last)
> >
> > /.../.../.../ in ()
> >
> > : shape mismatch: objects cannot be
> > broadcast to a single shape
> >
> > But, this works:
> >
> > print a[q,:][:,r]
> >
> > [[ 0 5]
> > [20 25]
> > [40 45]
> > [60 65]
> > [80 85]]
> >
> > So why does the a[q,r] form have problems? Thanks for your insights.
>
> It is intended that the form a[q,r] be the general case: q and r
> are broadcasted
> against each other to a single shape. The result of the indexing
> is an array of
> that broadcasted shape with elements found by using each pair of
> elements in the
> broadcasted q and r arrays as indices.
>
> There are operations you can express with this form that you
> couldn't if the
> behavior that you expected were the case whereas you can get the
> result you want
> relatively straightforwardly.
>
> In [6]: a[q[:,newaxis], r]
> Out[6]:
> array([[ 0, 5],
> [20, 25],
> [40, 45],
> [60, 65],
> [80, 85]])
>
>
>
> At the risk of making Robert grumpy: while it is true the form we
> ended up with is more general I've come to the conclusion that it was
> a bit of a mistake. In the spirit of making simple things simple and
> complex things possible, I suspect that having fancy-indexing do the
> obvious thing here[1] and delegating the more powerful but also more
> difficult to understand case to a function or method would have been
> overall more useful. Cases where the multidimensional features of
> fancy-indexing get used are messy enough that they don't benefit much
> from the conciseness of the indexing notation, at least in my experience.
This is a reasonable argument. It is reasonable enough that I
intentionally made an ix_ function to do what you want.
a[ix_(q,r)]
does as originally expected if a bit more line-noise.
-Travis
From david at ar.media.kyoto-u.ac.jp Wed Sep 12 01:55:09 2007
From: david at ar.media.kyoto-u.ac.jp (David Cournapeau)
Date: Wed, 12 Sep 2007 14:55:09 +0900
Subject: [Numpy-discussion] how to include numpy headers when building
an extension?
In-Reply-To:
References: <46E5D9AC.8040206@noaa.gov>
<46E5DB4E.3090008@gmail.com> <46E62E42.7030308@noaa.gov>
<46E63745.1060306@enthought.com> <46E63FFC.2020900@ar.media.kyoto-u.ac.jp>
Message-ID: <46E77F3D.1010409@ar.media.kyoto-u.ac.jp>
David M. Cooke wrote:
>
> I work on it off and on. As you say, it's not trivial :-) It also has
> a tendency to be fragile, so large changes are harder. Something will work
> for me, then I merge it into the trunk, and it breaks on half-a-dozen
> platforms that I can't test on :-) So, it's slow going.
>
> I've got a list of my current goals at
> http://scipy.org/scipy/numpy/wiki/DistutilsRevamp.
>
Would some contribution help ? Or is distutils such a beast that working
together on it would be counter productive ? The things I had in mind were:
- add the possibility to build shared libraries usable by ctypes (I
just have a problem to start: I do not know how to add a new command to
numpy.distutils)
- provides an interface to be able to know how numpy/scipy was
configured (for example: is numpy compiled with ATLAS, Apple perf
libraries, GOTO, fftw, etc...)
- add the possibility to build several object files differently (one
of your goal if I remember correctly)
cheers,
David
From Chris.Barker at noaa.gov Wed Sep 12 02:03:46 2007
From: Chris.Barker at noaa.gov (Christopher Barker)
Date: Tue, 11 Sep 2007 23:03:46 -0700
Subject: [Numpy-discussion] numpy.ndarrays as C++ arrays (wrapped with
boost)
In-Reply-To:
References:
Message-ID: <46E78142.7080500@noaa.gov>
Alexander Schmolck wrote:
> I just saw a closely related question posted one
> week ago here (albeit mostly from a swig context).
SWIG, Boost, whatever, the issues are similar. I guess what I'd love to
find is an array implementation that plays well with modern C++, and
also numpy.
> The code currently mostly just uses
> plain C double arrays passed around by pointers and I'd like to encapsulate
> this at least with something like stl::vector (or maybe valarray), but I've
> been wondering whether it might not make sense to use (slightly wrapped) numpy
> ndarrays --
Well, you can go back and forth between pointers to data blacks and
numpy arrays pretty easily. Where you thinking of doing this at the
python-C++ interface, or where you looking for something you could use
throughout your code. If the later, then I expect you don't want to use
a Python Object (unless you're using your code only from Python).
Our case is such: We want to have a nice array-like container that we
can use in C++ code that makes sense both for pure C++, and interacts
well with numpy arrays, as the code may be used in pure C++ app, but
also want to test it, script it, etc from Python.
> Also, ndarrays
> provide fairly rich functionality even at the C-API-level
Yes, the more I look into this, the more I'm impressed with numpy's design.
> but there doesn't seem to be one obvious choice, as
> there is for python.
Though there may be more than one good choice -- did you check out
boost::multiarray ? I didn't see that on your list.
> Things that would eventually come in handy, although they're not needed yet,
> are basic linear algebra and maybe two or three LAPACK-level functions (I can
> think of cholesky decomposition and SVD right now)
It would be nice to just have that (is MTL viable?), but writing
connection code to LAPACK for a few functions is not too bad.
> I think I could get all these things (and more) from scipy
> (and kin) with too much fuzz (although I haven't tried wavelet support yet)
> and it seems like picking together the same functionality from different C++
> libs would require considerably more work.
True -- do-able, but you'd have to do it!
> So my question is: might it make sense to use (a slightly wrapped)
> numpy.ndarray,
I guess what I'd like is a C++ array that was essentially an ndarray
without the pyobject stuff -- it could then be useful for C++, but also
easy to go back and forth between numpy and C++.
Ideally, there'd be something that already fits that bill. I see a
couple design issues that are key:
"View" semantics: numpy arrays have the idea of "views" of data built in
to them -- a given array can have it's own data block, or a be a view
onto another. This is quite powerful and flexible, and can save a lot a
data copying. The STL containers don't seem to have that concept at all.
std::valarray has utility classes that are views of a valarray, but they
really only useful as temporaries - they are not full-blown valarrays.
It looks like boost::multiarrays have a similar concept though
"""
The MultiArray concept defines an interface to hierarchically nested
containers. It specifies operations for accessing elements, traversing
containers, and creating views of array data.
"""
Another issue is dynamic typing. Templates provide a way to do generic
programming, but it's only generic at the code level. At compile time,
types are fixed, so you have a valarray, for instance. numpy
arrays, on the other hand are of only one type - with the data type
specified as meta-data essentially. I don't know what mismatch this may
cause, but it's a pretty different way to structure things. (Side note:
I used this feature once to re-type an array in place, using the same
data block -- it was a nifty hack used to unpack an odd binary format).
Would it make sense to use this approach in C++? I suspect not -- all
your computational code would have to deal with it.
There is also the re-sizing issue. It's pretty handy to be able to
re-size arrays -- but then the data pointer can change, making it pretty
impossible to share the data. Maybe it would be helpful to have a
pointer-to-a-pointer instead, so that the shared pointer wouldn't
change. However, there could be uglyness with the pointer changing while
some other view is working with it.
>
That does look promising -- and it used boost::multiarrays
The more I look at boost::multiarray, the better I like it (and the more
it looks like numpy) -- does anyone here have experience (good or bad)
with it?
-Chris
--
Christopher Barker, Ph.D.
Oceanographer
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
From david at ar.media.kyoto-u.ac.jp Wed Sep 12 06:19:59 2007
From: david at ar.media.kyoto-u.ac.jp (David Cournapeau)
Date: Wed, 12 Sep 2007 19:19:59 +0900
Subject: [Numpy-discussion] numpy.ndarrays as C++ arrays (wrapped with
boost)
In-Reply-To: <46E78142.7080500@noaa.gov>
References: <46E78142.7080500@noaa.gov>
Message-ID: <46E7BD4F.90001@ar.media.kyoto-u.ac.jp>
Christopher Barker wrote:
> Alexander Schmolck wrote:
>> I just saw a closely related question posted one
>> week ago here (albeit mostly from a swig context).
>
> SWIG, Boost, whatever, the issues are similar. I guess what I'd love to
> find is an array implementation that plays well with modern C++, and
> also numpy.
>
Maybe I am naive, but I think a worthy goal would be a minimal C++
library which wraps ndarray, without thinking about SWIG, boost and co
first. I don't know what other people are looking for, but for me, the
interesting things with using C++ for ndarrays would be (in this order
of importance):
1 much less error prone memory management
2 a bit more high level than plain C ndarrays (syntactic sugar
mostly: keyword args, overloaded methods and so on)
3 more high level things for views
4 actual computation (linear algebra, SVD, etc...)
1 and 2 can "easily" be implemented without anything but plain C++. 3
would be a pain to do right without e.g boost::multiarray; 4 obviously
needs external libraries anyway. My guess is that most people need
mostly those 4 points, but not in the same order. Am I right ?
One huge advantage of being independant of external libraries would be
that the wrapper could then be included in numpy, and you could expect
it everywhere.
>
> I guess what I'd like is a C++ array that was essentially an ndarray
> without the pyobject stuff -- it could then be useful for C++, but also
> easy to go back and forth between numpy and C++.
>
> Ideally, there'd be something that already fits that bill. I see a
> couple design issues that are key:
>
> "View" semantics: numpy arrays have the idea of "views" of data built in
> to them -- a given array can have it's own data block, or a be a view
> onto another. This is quite powerful and flexible, and can save a lot a
> data copying. The STL containers don't seem to have that concept at all.
> std::valarray has utility classes that are views of a valarray, but they
> really only useful as temporaries - they are not full-blown valarrays.
What do you mean by STL does not have the concept of view ? Do you mean
vector ?
>
> It looks like boost::multiarrays have a similar concept though
> """
> The MultiArray concept defines an interface to hierarchically nested
> containers. It specifies operations for accessing elements, traversing
> containers, and creating views of array data.
> """
>
> Another issue is dynamic typing. Templates provide a way to do generic
> programming, but it's only generic at the code level. At compile time,
> types are fixed, so you have a valarray, for instance. numpy
> arrays, on the other hand are of only one type - with the data type
> specified as meta-data essentially. I don't know what mismatch this may
> cause, but it's a pretty different way to structure things. (Side note:
> I used this feature once to re-type an array in place, using the same
> data block -- it was a nifty hack used to unpack an odd binary format).
> Would it make sense to use this approach in C++? I suspect not -- all
> your computational code would have to deal with it.
Why not making one non template class, and having all the work done
inside the class instead ?
class ndarray {
private:
ndarray_imp a;
};
>
> There is also the re-sizing issue. It's pretty handy to be able to
> re-size arrays -- but then the data pointer can change, making it pretty
> impossible to share the data. Maybe it would be helpful to have a
> pointer-to-a-pointer instead, so that the shared pointer wouldn't
> change. However, there could be uglyness with the pointer changing while
> some other view is working with it.
If you have an array with several views on it, why not just enforcing
that the block data address cannot change as long as you have a view ?
This should not be too complicated, right ? I don't use views that much
myself in numpy (other than implicetely, of course), so I may missing
something important here
cheers,
David
From a.schmolck at gmx.net Wed Sep 12 06:31:39 2007
From: a.schmolck at gmx.net (Alexander Schmolck)
Date: Wed, 12 Sep 2007 11:31:39 +0100
Subject: [Numpy-discussion] numpy.ndarrays as C++ arrays (wrapped with
boost)
In-Reply-To: <46E78142.7080500@noaa.gov> (Christopher Barker's message of
"Tue\, 11 Sep 2007 23\:03\:46 -0700")
References: <46E78142.7080500@noaa.gov>
Message-ID:
Christopher Barker writes:
> Alexander Schmolck wrote:
>> I just saw a closely related question posted one
>> week ago here (albeit mostly from a swig context).
>
> SWIG, Boost, whatever, the issues are similar. I guess what I'd love to
> find is an array implementation that plays well with modern C++, and
> also numpy.
>
>
>> The code currently mostly just uses plain C double arrays passed around by
>> pointers and I'd like to encapsulate this at least with something like
>> stl::vector (or maybe valarray), but I've been wondering whether it might
>> not make sense to use (slightly wrapped) numpy ndarrays --
>
> Well, you can go back and forth between pointers to data blacks and
> numpy arrays pretty easily. Where you thinking of doing this at the
> python-C++ interface, or where you looking for something you could use
> throughout your code.
The latter -- I'd ideally like something that I can more or less transparently
pass and return data between python and C++ and I want to use numpy arrays on
the python side. It'd also be nice to have reference semantics and reference
counting working fairly painlessly between both sides.
> If the later, then I expect you don't want to use a Python Object (unless
> you're using your code only from Python).
Yup; that would be somewhat perverse -- although as I said I expect that most
data I deal with will be pretty large, so overheads from creating python
objects aren't likely to matter that much.
> Our case is such: We want to have a nice array-like container that we
> can use in C++ code that makes sense both for pure C++, and interacts
> well with numpy arrays, as the code may be used in pure C++ app, but
> also want to test it, script it, etc from Python.
Yes, that's exactly what I'm after. What's your current solution for this?
>> Also, ndarrays
>> provide fairly rich functionality even at the C-API-level
>
> Yes, the more I look into this, the more I'm impressed with numpy's design.
>
>
>> but there doesn't seem to be one obvious choice, as
>> there is for python.
>
> Though there may be more than one good choice -- did you check out
> boost::multiarray ? I didn't see that on your list.
No, I hadn't looked at that -- thanks. It looks like a raw, stripped down
version of a multidimensional array -- no . Since I'm mostly going to use
matrices (and vectors, here and there), maybe ublas, which does provide useful
numeric functionality is a better choice. I must say I find it fairly painful
to figure out how to do things I consider quite basic with the matrix/array
classes I come accross in C++ (I'm not exactly a C++ expert, but still); I
also can't seem to find a way to construct an ublas matrix or vector from
existing C-array data.
>> Things that would eventually come in handy, although they're not needed yet,
>> are basic linear algebra and maybe two or three LAPACK-level functions (I can
>> think of cholesky decomposition and SVD right now)
>
> It would be nice to just have that (is MTL viable?)
No idea -- as far as I can tell the webpage is broken, so I can't look at the
examples (http://osl.iu.edu/research/mtl/examples.php3). It doesn't seem to
provide SVD out of th box either though -- and since I've already got a boost
dependency my first instinct would be to use something from there. What's the
advantage of MTL over ublas?
> but writing connection code to LAPACK for a few functions is not too bad.
>
>> I think I could get all these things (and more) from scipy
>> (and kin) with too much fuzz (although I haven't tried wavelet support yet)
>> and it seems like picking together the same functionality from different C++
>> libs would require considerably more work.
>
> True -- do-able, but you'd have to do it!
>
>> So my question is: might it make sense to use (a slightly wrapped)
>> numpy.ndarray,
>
> I guess what I'd like is a C++ array that was essentially an ndarray
> without the pyobject stuff -- it could then be useful for C++, but also
> easy to go back and forth between numpy and C++.
Indeed.
> Ideally, there'd be something that already fits that bill. I see a
> couple design issues that are key:
>
> "View" semantics: numpy arrays have the idea of "views" of data built in
> to them -- a given array can have it's own data block, or a be a view
> onto another. This is quite powerful and flexible, and can save a lot a
> data copying. The STL containers don't seem to have that concept at all.
Yes. C++ copying semantics seem completely braindamaged to me.
> std::valarray has utility classes that are views of a valarray, but they
> really only useful as temporaries - they are not full-blown valarrays.
>
> It looks like boost::multiarrays have a similar concept though
> """
> The MultiArray concept defines an interface to hierarchically nested
> containers. It specifies operations for accessing elements, traversing
> containers, and creating views of array data.
> """
>
> Another issue is dynamic typing. Templates provide a way to do generic
> programming, but it's only generic at the code level. At compile time,
> types are fixed, so you have a valarray, for instance.
> numpy arrays, on the other hand are of only one type - with the data type
> specified as meta-data essentially. I don't know what mismatch this may
> cause, but it's a pretty different way to structure things. (Side note: I
> used this feature once to re-type an array in place, using the same data
> block -- it was a nifty hack used to unpack an odd binary format). Would it
> make sense to use this approach in C++? I suspect not -- all your
> computational code would have to deal with it.
Any solution that just works fine for doubles as element type would perfectly
suffice for me, but yes, I'm sure compile time vs. run-time
element-type-specification causes impedance mismatch.
>
> There is also the re-sizing issue. It's pretty handy to be able to
> re-size arrays -- but then the data pointer can change, making it pretty
> impossible to share the data. Maybe it would be helpful to have a
> pointer-to-a-pointer instead, so that the shared pointer wouldn't
> change. However, there could be uglyness with the pointer changing while
> some other view is working with it.
>
>>
>
> That does look promising -- and it used boost::multiarrays
Yes (and also ublas vectors and matrices). Unfortunately, the author just
wrote in the c++-sig noted that he's unlikely to work on the code again -- but
it might still make a good starting point for someone looking into creating
nice-seamless integration between numpy and a decent C++ matrix/array type.
Unfortunately I haven't time for this; I might start out just using multiarray
or ublas matrices/vectors and use some primitive explicit hack to convert.
> The more I look at boost::multiarray, the better I like it (and the more
> it looks like numpy) -- does anyone here have experience (good or bad)
> with it
I'd be interested to hear about that too.
cheers,
'as
From kurdt.bane at gmail.com Wed Sep 12 06:43:56 2007
From: kurdt.bane at gmail.com (Kurdt Bane)
Date: Wed, 12 Sep 2007 12:43:56 +0200
Subject: [Numpy-discussion] Optimizing similarity matrix algorithm
Message-ID:
Hi to all!
For reverse engineering purposes, I need to find where every possible chunk
of bytes in file A is contained in file B. Obviously, if a chunk of length n
is contained in B, I dont' want my script to recognize also all the
subchunks of size < n contained in the chunk.
I coded a naive implementation of a similarity algorithm: scan the
similarity algorithm and find all the diagonals. Here's the code:
file_a = open(argv[1])
file_b = open(argv[2])
a = numpy.fromstring(file_a.read(),'c')
b = numpy.fromstring(file_b.read(),'c')
tolerance = int(argv[3])
chunks = []
valid = True
count_xcent = 0
for x in xrange(len(a)):
for y in xrange(len(b)):
count = 0
if (a[x] == b[y]):
x_cnt, y_cnt = x,y
if (a[x_cnt] == b[y_cnt]):
try:
while (a[x_cnt+1] == b[y_cnt+1]):
count += 1
x_cnt += 1
y_cnt += 1
except IndexError:
pass
if ((count > tolerance) or (count == tolerance)):
for tuple in chunks:
if (((x >= tuple[0]) and (x_cnt <= tuple[1])) and
((y >= tuple[2]) and (y_cnt <= tuple[3]))):
valid = False
if __debug__:
print "Found an already processed subchunk"
break
if (valid):
chunks.append ((x, x_cnt, y, y_cnt))
print "Corresponding chunk found. List 1: from " +
str(x) + " to " + str(x_cnt) +". List 2: from " + str(y) + " to " +
str(y_cnt)
print "with length of " + str (x_cnt + 1 - x)
else:
valid = True
It simply scans the similarity matrix, finds the diagonals in wich a[x] ==
a[y] and interprets the diagonal as a chunk. Then, it stores the chunk in a
list and determines if it's a subchunk of another greater chunk already
found.
The problem is: this implementation is very slow, imho because of three
factors:
1. I use a nested for loop to scan the matrix
2. When the start of a diagonal is found, the program scans the diagonal
with another additional loop. Maybe it would be faster to use a function
such as diagonal() (but I can't actually _create_ the boolean similarity
matrix, as it gets way too big for files big enough (we're talking about ~
100 Kb files) - I am forced to compute it "on the way".
3. When I find a chunk, I compute all its subchunks with this approach, and
compare them with the "big" chunk I stored in the list. Maybe there's a
better algorithmical way.
What do you think about these issues? Is there a way to optimize them? And
are there other issues I didn't take in account?
Thanks in advance,
regards,
Chris.
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
From kurdt.bane at gmail.com Wed Sep 12 06:48:24 2007
From: kurdt.bane at gmail.com (Kurdt Bane)
Date: Wed, 12 Sep 2007 12:48:24 +0200
Subject: [Numpy-discussion] Optimizing similarity matrix algorithm
In-Reply-To:
References:
Message-ID:
Er.. obviousl, when i wrote : "scan the similarity algorithm and find all
the diagonals", I meant scan the "similarity matrix and find all the
diagonals".
"Similarity matrix" should really be called "Equality matrix", as I imagine
it as a matrix with dimensions len(a) x len(b) where M[x][y] = (a[x] ==
b[y])
On 9/12/07, Kurdt Bane wrote:
>
> Hi to all!
> For reverse engineering purposes, I need to find where every possible
> chunk of bytes in file A is contained in file B. Obviously, if a chunk of
> length n is contained in B, I dont' want my script to recognize also all the
> subchunks of size < n contained in the chunk.
>
> I coded a naive implementation of a similarity algorithm: scan the
> similarity algorithm and find all the diagonals. Here's the code:
>
> file_a = open(argv[1])
> file_b = open(argv[2])
>
> a = numpy.fromstring (file_a.read(),'c')
> b = numpy.fromstring(file_b.read(),'c')
>
> tolerance = int(argv[3])
>
> chunks = []
> valid = True
>
> count_xcent = 0
> for x in xrange(len(a)):
> for y in xrange(len(b)):
> count = 0
> if (a[x] == b[y]):
> x_cnt, y_cnt = x,y
> if (a[x_cnt] == b[y_cnt]):
> try:
> while (a[x_cnt+1] == b[y_cnt+1]):
> count += 1
> x_cnt += 1
> y_cnt += 1
> except IndexError:
> pass
> if ((count > tolerance) or (count == tolerance)):
> for tuple in chunks:
> if (((x >= tuple[0]) and (x_cnt <= tuple[1])) and
> ((y >= tuple[2]) and (y_cnt <= tuple[3]))):
> valid = False
> if __debug__:
> print "Found an already processed
> subchunk"
> break
> if (valid):
> chunks.append ((x, x_cnt, y, y_cnt))
> print "Corresponding chunk found. List 1: from " +
> str(x) + " to " + str(x_cnt) +". List 2: from " + str(y) + " to " +
> str(y_cnt)
> print "with length of " + str (x_cnt + 1 - x)
> else:
> valid = True
>
>
> It simply scans the similarity matrix, finds the diagonals in wich a[x] ==
> a[y] and interprets the diagonal as a chunk. Then, it stores the chunk in a
> list and determines if it's a subchunk of another greater chunk already
> found.
>
> The problem is: this implementation is very slow, imho because of three
> factors:
>
> 1. I use a nested for loop to scan the matrix
>
> 2. When the start of a diagonal is found, the program scans the diagonal
> with another additional loop. Maybe it would be faster to use a function
> such as diagonal() (but I can't actually _create_ the boolean similarity
> matrix, as it gets way too big for files big enough (we're talking about ~
> 100 Kb files) - I am forced to compute it "on the way".
>
> 3. When I find a chunk, I compute all its subchunks with this approach,
> and compare them with the "big" chunk I stored in the list. Maybe there's a
> better algorithmical way.
>
> What do you think about these issues? Is there a way to optimize them? And
> are there other issues I didn't take in account?
>
> Thanks in advance,
>
> regards,
>
> Chris.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
From Chris.Barker at noaa.gov Wed Sep 12 12:01:38 2007
From: Chris.Barker at noaa.gov (Christopher Barker)
Date: Wed, 12 Sep 2007 09:01:38 -0700
Subject: [Numpy-discussion] numpy.ndarrays as C++ arrays (wrapped with
boost)
In-Reply-To: <46E7BD4F.90001@ar.media.kyoto-u.ac.jp>
References: <46E78142.7080500@noaa.gov>
<46E7BD4F.90001@ar.media.kyoto-u.ac.jp>
Message-ID: <46E80D62.6000003@noaa.gov>
David Cournapeau wrote:
> Maybe I am naive, but I think a worthy goal would be a minimal C++
> library which wraps ndarray, without thinking about SWIG, boost and co
> first.
That's exactly what I had in mind. If you have something that works well
with ndarray -- then SWIG et al. can work with it. In principle, if you
can do the transition nicely with hand-written wrappers, then you can do
it with the automated tools too.
> I don't know what other people are looking for, but for me, the
> interesting things with using C++ for ndarrays would be (in this order
> of importance):
> 1 much less error prone memory management
less than what? std:valarray, etc. all help with this.
> 2 a bit more high level than plain C ndarrays (syntactic sugar
> mostly: keyword args, overloaded methods and so on)
Yes.
> 3 more high level things for views
I think views are key.
> 4 actual computation (linear algebra, SVD, etc...)
This is last on my list -- key is the core data type. I may be an
unusual user, but what I expect is that in a given pile of code, we need
one or two linear algebra routines, so I don't mind hand-wrapping
LAPACK. Not that it wouldn't be nice to have it built in, but it's not a
deal breaker. In any case, it should be separate: a core set of array
objects, an a linear algebra (or whatever else) package built on top of it.
> 3
> would be a pain to do right without e.g boost::multiarray;
Yes, it sure would be nice to build it on an existing code base, and
boost::multiarray seems to fit.
> One huge advantage of being independant of external libraries would be
> that the wrapper could then be included in numpy, and you could expect
> it everywhere.
That would be nice, but may be too much work.
I"m really a C++ newbie, but it seems like the key here is the view
semantics -- and perhaps the core solution is to have a "data block"
class -- all it would have is a pointer to a block of data, and a
reference counter. Then each array object would have a view of one of
those -- each new array object that used a given instance would increase
the ref count, and decrease it on deletion. The view would destroy
itself when its refcount went to zero. (is this how numpy works now?)
Even if this makes sense, I have no idea how compatible it would be with
numpy and/or python.
boost:multiarray does not seem to take this approach. Rather it has two
classes: a multi_array: responsible for its own data block, and a
multi_array_ref: which uses a view on another multiarray's data block.
This is getting close, but it means that when you create a
multi_array_ref, the original multi_array needs to stay around. I'd
rather have much more flexible system,where you could create an array,
create a view of that array, then destroy the original, then have the
data block go away when you destroy the view. This could cause little
complications if you started with a huge array, made a view into a tiny
piece of it, then the whole data block would stick around -- but that
would be up to the user to think about.
>> Would it make sense to use this approach in C++? I suspect not -- all
>> your computational code would have to deal with it.
> Why not making one non template class, and having all the work done
> inside the class instead ?
>
> class ndarray {
> private:
> ndarray_imp a;
> };
hm. that could work (as far as my limited C++ knowledge tells me),b ut
it's still static at run time -- which may be OK -- and is C++-is anyway.
> If you have an array with several views on it, why not just enforcing
> that the block data address cannot change as long as you have a view ?
Maybe I"m missing what you're suggesting but this would lock in the
original array once any views were on it -- that would greatly restrict
flexibility. My suggestion above may help, but I think maybe I could
just live without re-sizing.
> This should not be too complicated, right ? I don't use views that much
> myself in numpy (other than implicitly, of course), so I may missing
> something important here
Implicitly, we're all using them all the time -- which is why I think
views are key.
Alexander Schmolck wrote:
> I'd ideally like something that I can more or less transparently
> pass and return data between python and C++ and I want to use numpy arrays on
> the python side. It'd also be nice to have reference semantics and reference
> counting working fairly painlessly between both sides.
Can the python gurus here comment on how possible that is?
> as I said I expect that most
> data I deal with will be pretty large, so overheads from creating python
> objects aren't likely to matter that much.
I"m not so much worried about the overhead as the dependency -- to use
your words, it would feel perverse to by including python.h for a
program that wasn't using python at all.
>> Our case is such: We want to have a nice array-like container that we
>> can use in C++ code that makes sense both for pure C++, and interacts
>> well with numpy arrays, as the code may be used in pure C++ app, but
>> also want to test it, script it, etc from Python.
>
> Yes, that's exactly what I'm after. What's your current solution for this?
We're trying to build it now. The old code used Mac-OS Handles, those
have been converted to std::valarrays, and we're working on wrapping
those for with numpy arrays -- which, at the moment looks like copying
the data back and forth -- fine for testing code, but maybe not OK for
production work.
>> did you check out
>> boost::multiarray ? I didn't see that on your list.
> Since I'm mostly going to use
> matrices (and vectors, here and there), maybe ublas, which does provide useful
> numeric functionality is a better choice.
Well, one of the lesson's I learned from numpy is that I'm much happier
with a general purpose n-d array than with a "matrix" and "vector". the
latter can be built on top of the former if you want (like it is in
numpy). How compatible are multiarray and ublas matrices? It kind of
looks like boost isn't really a single project, so things that could be
related may not be.
Hmmm -- if my concept above works, then all you need is for your n-d
arrays and your matrices and vectors to all share the data "data block"
class.
> I must say I find it fairly painful
> to figure out how to do things I consider quite basic with the matrix/array
> classes I come accross in C++ (I'm not exactly a C++ expert, but still);
neither am I -- but I think it's the nature of C++!
> I
> also can't seem to find a way to construct an ublas matrix or vector from
> existing C-array data.
This functionality seems to be missing from many (moat) of these C++
containers. I suspect that it's the memory management issue. One of the
points of these containers it to take care of memory management for you
-- if you pass in a pointer to an existing data block -- it's not
managing your memory any more.
>> It would be nice to just have that (is MTL viable?)
>
> No idea -- as far as I can tell the webpage is broken, so I can't look at the
> examples (http://osl.iu.edu/research/mtl/examples.php3).
Too many dead or sleeping projects....
> Yes. C++ copying semantics seem completely braindamaged to me.
It's the memory management issue again -- C++ doesn't have it built in
-- so it's built in to each class instead.
>>>
>> That does look promising -- and it used boost::multiarrays
>
> Yes (and also ublas vectors and matrices). Unfortunately, the author just
> wrote in the c++-sig noted that he's unlikely to work on the code again --
Darn
but
> it might still make a good starting point for someone
The advantage of open source!
Full Disclosure: I have neither the skills nor the time to actually
implement any of these ideas. If no one else does, then I guess we're
just blabbing -- not that there is anything wrong with blabbing!
-Chris
From travis at enthought.com Wed Sep 12 12:05:15 2007
From: travis at enthought.com (Travis Vaught)
Date: Wed, 12 Sep 2007 11:05:15 -0500
Subject: [Numpy-discussion] ANN: Reminder - Texas Python Regional
Unconference
Message-ID:
Greetings,
Just a reminder for those in the area...
http://pycamp.python.org/Texas/HomePage
The Unconference is to be held this weekend (Saturday and Sunday,
September 15, 16) at the Texas Learning & Computing Center at the
University of Houston main campus.
It's free. Sign up by adding your name to the wiki page.
Travis
From myeates at jpl.nasa.gov Wed Sep 12 14:03:17 2007
From: myeates at jpl.nasa.gov (Mathew Yeates)
Date: Wed, 12 Sep 2007 11:03:17 -0700
Subject: [Numpy-discussion] pycdf probs
Message-ID: <46E829E5.7080500@jpl.nasa.gov>
Anybody know how to contact the pycdf author? His name is Gosselin I
think. There are hardcoded values that cause pycdf to segfault when
using large strings.
Mathew
From matthieu.brucher at gmail.com Wed Sep 12 14:10:39 2007
From: matthieu.brucher at gmail.com (Matthieu Brucher)
Date: Wed, 12 Sep 2007 20:10:39 +0200
Subject: [Numpy-discussion] numpy.ndarrays as C++ arrays (wrapped with
boost)
In-Reply-To: <46E80D62.6000003@noaa.gov>
References: <46E78142.7080500@noaa.gov>
<46E7BD4F.90001@ar.media.kyoto-u.ac.jp> <46E80D62.6000003@noaa.gov>
Message-ID:
>
> less than what? std:valarray, etc. all help with this.
I do not agree with this statement. A correct memory managed array would
increment and decrement a reference counter somewhere.
Yes, it sure would be nice to build it on an existing code base, and
> boost::multiarray seems to fit.
The problem with multiarray is that the dimension of the array are fixed at
the compilation. Although one could use 1 for the size in the remaning
dimension, I don't think it's the best choice, given that a real
dynamic-dimension array is not much complicated than a static-dimension one,
perhaps a little more slower.
boost:multiarray does not seem to take this approach. Rather it has two
> classes: a multi_array: responsible for its own data block, and a
> multi_array_ref: which uses a view on another multiarray's data block.
> This is getting close, but it means that when you create a
> multi_array_ref, the original multi_array needs to stay around. I'd
> rather have much more flexible system,where you could create an array,
> create a view of that array, then destroy the original, then have the
> data block go away when you destroy the view. This could cause little
> complications if you started with a huge array, made a view into a tiny
> piece of it, then the whole data block would stick around -- but that
> would be up to the user to think about.
I don't know ho numpy does it either, but a view on an view of an array may
be a view on an array, so in C++, an view should only reference the data,
not the real view, so when the array is destroyed, the view is still
correct, as it has a reference on the data and not the original array.
hm. that could work (as far as my limited C++ knowledge tells me),b ut
> it's still static at run time -- which may be OK -- and is C++-is anyway.
I've done this before, with type traits and a multi-dispatch method, you can
instantiate several functions with the correct type. It's a classic approach
that is used in plugins, and it does not use RTTI, and it is compatible
across C++ compilers.
Can the python gurus here comment on how possible that is?
Once you have the Python object, increment the reference counter when you
wrap the data in C++ for a real array or for a view, and decrement it in the
destructor of your C++ object, is that what you mean ?
If the C++ object can directly use a PyObject, it's very simple to use. It
perhaps could be done by a policy class, so that temporary C++ object would
use a default policy that does not rely on a Python object.
I"m not so much worried about the overhead as the dependency -- to use
> your words, it would feel perverse to by including python.h for a
> program that wasn't using python at all.
This is solved if one can use policy classes.
This functionality seems to be missing from many (moat) of these C++
> containers. I suspect that it's the memory management issue. One of the
> points of these containers it to take care of memory management for you
> -- if you pass in a pointer to an existing data block -- it's not
> managing your memory any more.
What Albert did for hs wrapper is this : provide an adaptator that can use
the data pointer. It's only a policy (but not the default one).
Full Disclosure: I have neither the skills nor the time to actually
> implement any of these ideas. If no one else does, then I guess we're
> just blabbing -- not that there is anything wrong with blabbing!
>
I know that in my lab, we intend to wrap numpy arrays in a C++ multi array,
but not the boost one. It will be for array that have more than 3
dimensions, and for less than 2 dimensions, we will use our own matrix
library, as it is "simple" to wrap array with it. The most complicated thing
will be the automatic conversion. It will most likely be Open Source (GPL),
but I don't know when we will be able to have time to do it and then when we
will make it available...
Matthieu
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
From lawtrevor at gmail.com Wed Sep 12 20:24:59 2007
From: lawtrevor at gmail.com (Trevor Law)
Date: Wed, 12 Sep 2007 17:24:59 -0700
Subject: [Numpy-discussion] pycdf probs
In-Reply-To: <46E829E5.7080500@jpl.nasa.gov>
References: <46E829E5.7080500@jpl.nasa.gov>
Message-ID: <61e67ee50709121724o14693eb3je4d505e1696f0a04@mail.gmail.com>
I believe I have contacted them before at this address:
gosselina at dfo-mpo.gc.ca
Trevor Law
UC Irvine Undergraduate Student
On 9/12/07, Mathew Yeates wrote:
> Anybody know how to contact the pycdf author? His name is Gosselin I
> think. There are hardcoded values that cause pycdf to segfault when
> using large strings.
>
> Mathew
>
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>
From fperez.net at gmail.com Wed Sep 12 21:14:07 2007
From: fperez.net at gmail.com (Fernando Perez)
Date: Wed, 12 Sep 2007 19:14:07 -0600
Subject: [Numpy-discussion] Docstring improvements for numpy.where?
Message-ID:
Hi all,
A couple of times I've been confused by numpy.where(), and I think
part of it comes from the docstring. Searching my gmail archive seems
to indicate I'm not the only one bitten by this.
Compare:
In [14]: pdoc numpy.where
Class Docstring:
where(condition, | x, y)
The result is shaped like condition and has elements of x and y where
condition is respectively true or false. If x or y are not given,
then it is equivalent to condition.nonzero().
To group the indices by element, rather than dimension, use
transpose(where(condition, | x, y))
instead. This always results in a 2d array, with a row of indices for
each element that satisfies the condition.
with (b is just any array):
In [17]: pdoc b.nonzero
Class Docstring:
a.nonzero() returns a tuple of arrays
Returns a tuple of arrays, one for each dimension of a,
containing the indices of the non-zero elements in that
dimension. The corresponding non-zero values can be obtained
with
a[a.nonzero()].
To group the indices by element, rather than dimension, use
transpose(a.nonzero())
instead. The result of this is always a 2d array, with a row for
each non-zero element.;
The sentence "The result is shaped like condition" in the where()
docstring is misleading, since the behavior is really that of
nonzero(). Where() *always* returns a tuple, not an array shaped like
condition. If this were more clearly explained, along with a simple
example for the usual case that seems to trip everyone:
In [21]: a=arange(10)
In [22]: N.where(a>5)
Out[22]: (array([6, 7, 8, 9]),)
In [23]: N.where(a>5)[0]
Out[23]: array([6, 7, 8, 9])
I think we'd get a lot less confusion.
Or am I missing something, or just being dense (quite likely)?
Cheers,
f
From robert.kern at gmail.com Wed Sep 12 22:16:09 2007
From: robert.kern at gmail.com (Robert Kern)
Date: Wed, 12 Sep 2007 21:16:09 -0500
Subject: [Numpy-discussion] Docstring improvements for numpy.where?
In-Reply-To:
References:
Message-ID: <46E89D69.5080804@gmail.com>
Fernando Perez wrote:
> Hi all,
>
> A couple of times I've been confused by numpy.where(), and I think
> part of it comes from the docstring. Searching my gmail archive seems
> to indicate I'm not the only one bitten by this.
>
> Compare:
>
> In [14]: pdoc numpy.where
> Class Docstring:
> where(condition, | x, y)
>
> The result is shaped like condition and has elements of x and y where
> condition is respectively true or false. If x or y are not given,
> then it is equivalent to condition.nonzero().
>
> To group the indices by element, rather than dimension, use
>
> transpose(where(condition, | x, y))
>
> instead. This always results in a 2d array, with a row of indices for
> each element that satisfies the condition.
>
> with (b is just any array):
>
> In [17]: pdoc b.nonzero
> Class Docstring:
> a.nonzero() returns a tuple of arrays
>
> Returns a tuple of arrays, one for each dimension of a,
> containing the indices of the non-zero elements in that
> dimension. The corresponding non-zero values can be obtained
> with
> a[a.nonzero()].
>
> To group the indices by element, rather than dimension, use
> transpose(a.nonzero())
> instead. The result of this is always a 2d array, with a row for
> each non-zero element.;
>
>
> The sentence "The result is shaped like condition" in the where()
> docstring is misleading, since the behavior is really that of
> nonzero(). Where() *always* returns a tuple, not an array shaped like
> condition. If this were more clearly explained, along with a simple
> example for the usual case that seems to trip everyone:
>
> In [21]: a=arange(10)
>
> In [22]: N.where(a>5)
> Out[22]: (array([6, 7, 8, 9]),)
>
> In [23]: N.where(a>5)[0]
> Out[23]: array([6, 7, 8, 9])
>
> I think we'd get a lot less confusion.
>
> Or am I missing something, or just being dense (quite likely)?
That sentence applies to the 3-argument form, which has nothing to do with
nonzero() and does not yield a tuple. But in general, yes, the docstring leaves
much to be desired.
--
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
From ndbecker2 at gmail.com Wed Sep 12 22:30:11 2007
From: ndbecker2 at gmail.com (Neal Becker)
Date: Wed, 12 Sep 2007 22:30:11 -0400
Subject: [Numpy-discussion] numpy.ndarrays as C++ arrays (wrapped with
boost)
References: <18150.54391.274768.497392@owl.eos.ubc.ca>
<46E72063.1060408@enthought.com>
Message-ID:
Travis E. Oliphant wrote:
>
>> nd to copy hundres of MB around unnecessarily.
>>
>> I think it is a real shame that boost currently doesn't properly support
>> numpy out of the box, although numpy has long obsoleted both numarray and
>> Numeric (which is both buggy and completely unsupported). All the more so
>> since writing multimedial or scientific extensions (in which numpy's
>> array interface is very natural to figure prominently) would seem such an
>> ideal use for boost.python, as soon as complex classes or compound
>> structures that need to efficiently support several (primitive) datatypes
>> are involved, boost.python could really play its strenghts compared to
>> Fortran/C based extensions.
>>
>>
> I think it could be that boost.python is waiting for the extended buffer
> interface which is coming in Python 3.0 and Python 2.6. This would
> really be ideal for wrapping external code in a form that plays well
> with other libraries.
>
>
> -Travis O.
I've spent a lot of time on this issue as well. There have been a few
efforts, and at present I've followed my own path.
My interest is in exposing algorithms that are written in generic c++ style
to python.
The requirement here is to find containers (vectors, n-dim arrays) that are
friendly to the generic c++ side and can be used from python. My opinion
is that Numeric and all it's descendants aren't what I want on the c++
interface side. Also, I've stumbled over trying to grok ndarrayobject.h,
and I haven't had much success finding docs.
What I've done instead is to basically write all that I need from numpy
myself. I've usually used ublas vector and matrix to do this. I've also
used boost::multi_array at times (and found it quite good), and fixed_array
from stlsoft. I implemented all the arithmetic I need and many functions
that operate on vectors (mostly I'm interested in vectors to represent
signals - not so much higher dimen arrays).
As far as views of arrays, ref counting, etc. I have not worried much about
it. I thought it would be a very elegant idea, but in practice I don't
really need it. The most common thing I'd do with a view is to operate on
a slice. Python supports this via __setitem__. For example:
u[4:10:3] += 2
works. There is no need for python to hold a reference to a vector slice to
do this.
Probably the biggest problem I've encountered is that there is not any
perfect c++ array container. For 1-dimen, std::vector is pretty good - and
the interface is reasonable. For more dimen, there doesn't seem to be any
perfect solution or general agreement on interface (or semantics).
One of my favorite ideas related to this. I've gotten a lot of mileage out
of moving from the pair-of-iterator interface featured by stl to the
boost::range. I believe it would be useful to consider a multi-dimen
extension of this idea. Perhaps this could present some unifying interface
to different underlying array libraries. For example, maybe something
like:
template