Inherit from array

2006-04-26 Thread TG
Hi there. I'm trying to create a simple class called Vector which inherit from array. class Vector(array): def __init__(self,length): """initialize a vector of random floats of size length. floats are in interval [0;1]""" array.__init__(self,'f') for _ in xrange(length

Re: Inherit from array

2006-04-26 Thread TG
from array import array class Vector(array): def __init__(self,size): print "pouet" array.__init__('f') print "pouet" v = Vector('c') print repr(v) will output : pouet pouet array('c') -- http://mail.python.org/mailman/listinfo/python-list

Re: Inherit from array

2006-04-26 Thread TG
Obviously, there is something I didn't catch in python's inheritance. from array import array class Vector(array): def __init__(self,size): print self.typecode array.__init__(self,'f') >>> v = Vector('c') c Here, it says the typecode is 'c' - I thought such an information was

Re: Inherit from array

2006-04-27 Thread TG
Hmm ... I'm definitely not a python wizard, but it seems to be quite a special case that breaks the rules ... unpythonic, isn't it ? Has anyone seen a PEP on this subject ? Just in case a troll reads this message : i'm not saying python sucks or has huge design flaws here ... -- http://mail.pyt

Subclassing array

2006-05-04 Thread TG
Hi. i've already something about inheriting from array a few weeks ago and had my answer. But again, there is something that I don't understand. Here is my vector class, which works quite well : class Vector(array): def __new__(cls,length,data=None): return super(Vector,cls).__new__(c

Re: Subclassing array

2006-05-05 Thread TG
That's great, thanks ! To put it short, when I create a Stimulus object, it first seek __new__() method. But if I don't define it, it looks for the one defined in Vector. This raises a problem because the parameters passed to Stimulus(params) aren't fitting with Vector parameters, raising an excep

numpy performance and list comprehension

2007-04-03 Thread TG
Hi there. Reading the page on python performance ( http://scipy.org/PerformancePython ) made me realize that I can achieve tremendous code acceleration with numpy just by using "u[:,:]" kind of syntax the clever way. Here is a little problem (Oja's rule of synaptic plasticity) * W is a matrix co

Boost python : get the shape of a numpy ndarray in C++ code.

2007-05-09 Thread TG
Hi there. I'm strugling here with some boost python code (damn I hate C++) : All I want to do is to initialize the content of an array with a numpy ndarray parameter. I have this, which actually works. But I want to add some kind of data check such as : * is array two dimensional ? * are the dim

Re: Boost python : get the shape of a numpy ndarray in C++ code.

2007-05-10 Thread TG
What I'm trying to say here : a numpy array is supposed to have it's shape stored as a tuple. What I want to do is to access this information from my C++ code, in order to do some validity check. So, by looking around in the doc of boost/python/numeric.hpp I was able to do this : void Layer::set_

Numeric N-dimensional array initialization

2006-06-22 Thread TG
Hi there ! I'm just starting to use Numeric here, and I'm wondering : how can I efficiently initialize every values of a N-dimensional array, given I don't know the number of dimensions ? I'm looking for something like a map function, or a way to conveniently iterate through the whole N-array, bu

Re: Numeric N-dimensional array initialization

2006-06-22 Thread TG
I tried to use Numeric.fromfunction, but there seems to be a problem : the function called must have the right number of args (hint : the number of dimensions, which I don't know). So i tried to use a function like : def myfunc(*args, **kw): return 0 and then i get : >> Numeric.fromfunctio

Re: Numeric N-dimensional array initialization

2006-06-22 Thread TG
Thanks for your precious advices. The flat iterator is definitely what i need. -- http://mail.python.org/mailman/listinfo/python-list

numpy : argmin in multidimensional arrays

2006-07-06 Thread TG
Hi there. I am working with multi-dimensional arrays and I need to get coordinates of the min value in it. using myarray.argmin() returns the index in the flatten array, which is a first step, but I wonder if it is possible to get the coordinates directly as an array, rather than calculating them

Re: numpy : argmin in multidimensional arrays

2006-07-06 Thread TG
thanks. unravel_index do the trick. Travis E. Oliphant wrote: > TG wrote: > > Hi there. > > > > I am working with multi-dimensional arrays and I need to get > > coordinates of the min value in it. > > > > using myarray.argmin() returns the index in the f

multinormal distribution

2006-07-07 Thread TG
hi there. I'm struggling with a function of numpy. Here it is : import numpy as NP mean = NP.array([0,0]) cov = NP.array([[1,0.25],[0.25,1]]) v = NP.random.multivariate_normal(mean,cov) Quite simple code : it is supposed to generate an array of two random values taken from a multinormal distrib

solving equation system

2006-07-17 Thread TG
Hi there. Anyone knows how to use numpy / scipy in order to solve this ? * A is an array of shape (n,) * X is a positive float number * B is an array of shape (n,) * O is an array of shape (n,) containing only zeros. A.X - B = O min(X) thanks. -- http://mail.python.org/mailman/listinfo/python

Re: solving equation system

2006-07-17 Thread TG
Ben C wrote: > On 2006-07-17, TG <[EMAIL PROTECTED]> wrote: > > Hi there. > > > > Anyone knows how to use numpy / scipy in order to solve this ? > > > > * A is an array of shape (n,) > > * X is a positive float number > > * B is an array of shap

access to submodules

2006-07-19 Thread TG
hi. This is my first try on modules. I've got : tom/ __init__.py core.py ui.py data.py then, when I'm in my ipython shell : ?> from tom import * this works, it loads core, ui and data but when I do this : ?> import tom ?> tom.core AttributeError: 'module' object has no att

Re: access to submodules

2006-07-19 Thread TG
I've just found this : If I add : "import core, data, ui" inside my "tom/__init__.py" file, it will work. But this line does not seems to exist in other files (after having a look at several files inside /usr/lib/python2.4). -- http://mail.python.org/mailman/listinfo/python-list

Re: access to submodules

2006-07-19 Thread TG
I know this is a bad habit ... I was just doing it to show what is disturbing me. Obviously the "star" syntax finds the submodules because they are loaded, but when I properly load the module alone with "import tom", the "dot" syntax does not find "tom.core". BartlebyScrivener wrote: > >> > from

Re: access to submodules

2006-07-19 Thread TG
BartlebyScrivener wrote: > then you no longer need tom, you imported all of his FUNCTIONS (never > heard of submodule). my mistake, I was using the wrong name tom/ <-- package __init__.py core.py < data.py < these are modules contained in tom/ ui.py < if I import t

Re: access to submodules

2006-07-19 Thread TG
okay, so only when I have inside __init__.py __all__ = ["core"] this works ?> from tom import * ?> help(core) but (in a brand new interpretor) ?> import tom ?> help(tom.core) AttributeError: 'module' object has no attribute 'core' got it. But ... ?> import numpy ?> help(numpy.core) this w

Re: access to submodules

2006-07-20 Thread TG
okay, thanks everyone. this is much clearer now. -- http://mail.python.org/mailman/listinfo/python-list

pasting numpy array into bigger array

2006-07-26 Thread TG
hi. let's say I have : from numpy import * x = identity(5) y = zeros((7,7)) I want to paste x into y, starting at coordinates (1,1) in order to change y to something like this : 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 how would you do t

Re: pasting numpy array into bigger array

2006-07-26 Thread TG
Thanks, that's exactly what I needed. Tim Heaney wrote: > You can use Python slice notation for each dimension > > y[1:6,1:6] = x > -- http://mail.python.org/mailman/listinfo/python-list