[Numpy-discussion] Vectorizing a function

2008-01-30 Thread LB
   Hi,

I've got some questions on the numpy.vectorize  function.
Currently, i'm doing this kind of work :

[ code]
def calc_0d(x, y):
 make complexe calculation using two scalars x and y 
[ ... ]
return res1, res2, res 3

# vectorize the function
calc = vectorize(calc_0d)

res1, res_2, res_3 = calc(array_x, array_y)
[/code]

This works fine. Really impressive. Good work for this !

My problem is that the complexe calculations made in calc_0d use some
parameters, which are currently defined at the head of my python file.
This is not very nice and I can't define a module containing theses
two functions and call them with different parameters.

I would like to make this cleaner and pass theses parameter as
keyword  argument, but this don't seems to be possible with vectorize.
Indeed, some of theses parameters are array parameters and only the x
and y arguments should be interpreted with the broadcasting rules

What is the good way for doing this ?

Regards,

--
LB
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Vectorizing a function

2008-01-30 Thread Gael Varoquaux
On Wed, Jan 30, 2008 at 12:49:44AM -0800, LB wrote:
 My problem is that the complexe calculations made in calc_0d use some
 parameters, which are currently defined at the head of my python file.
 This is not very nice and I can't define a module containing theses
 two functions and call them with different parameters.

 I would like to make this cleaner and pass theses parameter as
 keyword  argument, but this don't seems to be possible with vectorize.
 Indeed, some of theses parameters are array parameters and only the x
 and y arguments should be interpreted with the broadcasting rules

 What is the good way for doing this ?

I don't know what the good way is, but you can always use functional
programming style (Oh, no, CaML is getting on me !):

def calc_0d_params(param1, param2, param3):
def calc_0d(x, y):
# Here your code making use of param1, param2, param3)
...

return calc_0d(x, y)

you call the function like this:

calc_0d_params(param1, param2, param3)(x, y)

To vectorize it you can do:

calc_0d_vect = lambda *params: vectorize(calc_0d_params(*params))

This is untested code, but I hope you get the idea. It all about partial
evaluation of arguments. By the way, the parameters can now be keyword
arguments.

HTH,

Gaël
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] argsort memory problem?

2008-01-30 Thread Oriol Vendrell
* Robin [EMAIL PROTECTED] [2008-01-29 19:23:11 +]:

 On Jan 29, 2008 7:16 PM, Lou Pecora [EMAIL PROTECTED] wrote:
  Hmmm... Interesting.  I am using Python 2.4.4.  It
  would be nice to have other Mac people with same/other
  Python and numpy versions try the argsort bug code.
 
 I don't see any memory leak with the test code.
 Mac OS X 10.5.1
 Python 2.5.1 (not apple one)
 Numpy 1.0.5.dev4722

I have run the test1 code again, this time on my laptop PC (no MAC-user,
sorry) using the last stable numpy release. The memory problem does
_not_ show up now.  I'm running with:
 - Ubuntu Feisty (kernel 2.6.17-12-generic i686)
 - python 2.5.1 (Feisty package)
 - numpy 1.0.4 (compiled with gcc version 4.1.2)

However, the memory leak appears on my laptop if I use python 2.5.1 with
numpy 1.0.1. At least here, this seems to be an issue dependent only on
the numpy version, and a solved one.

-- oriol
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Vectorizing a function

2008-01-30 Thread YW
Try use a closure.

On Jan 30, 12:49 am, LB [EMAIL PROTECTED] wrote:
    Hi,

 I've got some questions on the numpy.vectorize  function.
 Currently, i'm doing this kind of work :

 [ code]
 def calc_0d(x, y):
      make complexe calculation using two scalars x and y 
     [ ... ]
     return res1, res2, res 3

 # vectorize the function
 calc = vectorize(calc_0d)

 res1, res_2, res_3 = calc(array_x, array_y)
 [/code]

 This works fine. Really impressive. Good work for this !

 My problem is that the complexe calculations made in calc_0d use some
 parameters, which are currently defined at the head of my python file.
 This is not very nice and I can't define a module containing theses
 two functions and call them with different parameters.

 I would like to make this cleaner and pass theses parameter as
 keyword  argument, but this don't seems to be possible with vectorize.
 Indeed, some of theses parameters are array parameters and only the x
 and y arguments should be interpreted with the broadcasting rules

 What is the good way for doing this ?

 Regards,

 --
 LB
 ___
 Numpy-discussion mailing list
 [EMAIL PROTECTED]://projects.scipy.org/mailman/listinfo/numpy-discussion
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Vectorizing a function

2008-01-30 Thread Scott Ransom
On a side note, given that I've seen quite a few posts about
vectorize() over the past several months...

I've written hundreds or thousands of functions that are intended
to work with numeric/numpy arrays and/or scalars and I've _never_
(not once!) found a need for the vectorize function.  Python's
duck-typing has almost always allowed things to work without any
(or sometimes with only minor) changes to the function.  For
example:

In [12]: def foo(x, y):
   : return 2.0*x + y

In [13]: foo(3.0, 5.0)
Out[13]: 11.0
  
In [14]: foo(arange(4), ones(4)+3.0)
Out[14]: array([  4.,   6.,   8.,  10.])
  
In [15]: foo(arange(4), 3.0)
Out[15]: array([ 3.,  5.,  7.,  9.])
  
That works fine with arrays, scalars, or array/scalar mixes in the
calling.  I do understand that more complicated functions might
require vectorize(), however, I wonder if sometimes it is used
when it doesn't need to be?

Scott


On Wed, Jan 30, 2008 at 10:22:15AM +0100, Gael Varoquaux wrote:
 On Wed, Jan 30, 2008 at 12:49:44AM -0800, LB wrote:
  My problem is that the complexe calculations made in calc_0d use some
  parameters, which are currently defined at the head of my python file.
  This is not very nice and I can't define a module containing theses
  two functions and call them with different parameters.
 
  I would like to make this cleaner and pass theses parameter as
  keyword  argument, but this don't seems to be possible with vectorize.
  Indeed, some of theses parameters are array parameters and only the x
  and y arguments should be interpreted with the broadcasting rules
 
  What is the good way for doing this ?
 
 I don't know what the good way is, but you can always use functional
 programming style (Oh, no, CaML is getting on me !):
 
 def calc_0d_params(param1, param2, param3):
 def calc_0d(x, y):
   # Here your code making use of param1, param2, param3)
   ...
 
 return calc_0d(x, y)
 
 you call the function like this:
 
 calc_0d_params(param1, param2, param3)(x, y)
 
 To vectorize it you can do:
 
 calc_0d_vect = lambda *params: vectorize(calc_0d_params(*params))
 
 This is untested code, but I hope you get the idea. It all about partial
 evaluation of arguments. By the way, the parameters can now be keyword
 arguments.
 
 HTH,
 
 Gaël
 ___
 Numpy-discussion mailing list
 Numpy-discussion@scipy.org
 http://projects.scipy.org/mailman/listinfo/numpy-discussion

-- 
-- 
Scott M. RansomAddress:  NRAO
Phone:  (434) 296-0320   520 Edgemont Rd.
email:  [EMAIL PROTECTED] Charlottesville, VA 22903 USA
GPG Fingerprint: 06A9 9553 78BE 16DB 407B  FFCA 9BFA B6FF FFD3 2989
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] load movie frames in python?

2008-01-30 Thread Brian Blais

On Jan 29, 2008, at Jan 29:9:25 PM, Andrew Straw wrote:


I'm pretty sure there's code floating around the pyglet mailing list.
I'd be happy to add it to
http://code.astraw.com/projects/motmot/wiki/pygarrayimage if it seems
reasonable. (pygarrayimage goes from numpy array to pyglet texture).




I checked the pyglet users group, and the only mention I see is from:

http://groups.google.com/group/pyglet-users/browse_thread/thread/ 
5981f764902c7df/888328e19653be1a?lnk=gstq=numpy#888328e19653be1a



there, he has the solution:

a = numpy.frombuffer(surf_clip.get_data(), numpy.uint8)
a.shape = (sw, sh, 4)
a = a[:,:,0:3] #drop alpha channel

but when I do it (see code below) the image is clearly munged, so I  
think the decoding is not quite right.


any ideas?


thanks,


Brian Blais
--
Brian Blais
[EMAIL PROTECTED]
http://web.bryant.edu/~bblais


import pylab
import numpy
from pyglet import media,window

win = window.Window(resizable=True)
player = media.Player()
filename='ica_mvl.avi' # from http://hlab.phys.rug.nl/demos/ica/ 
jiminy.html

source = media.load(filename)
player.queue(source)

player.play()

if True:
player.dispatch_events()

imdata=player.texture.get_image_data()

a = numpy.frombuffer(player.texture.get_image_data().data,  
numpy.uint8)


a.shape = (imdata.width, imdata.height, 4)
a = a[:,:,0:3] #drop alpha channel

# make gray
im=a.sum(axis=1)/3

pylab.imshow(im,cmap=pylab.cm.gray)
pylab.show()
pylab.draw()


 
   ___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


[Numpy-discussion] Can not update a submatrix

2008-01-30 Thread Nadav Horesh
In the following piece of code:

 import numpy as N
 R = N.arange(9).reshape(3,3)
 ax = [1,2]
 R
array([[0, 1, 2],
   [3, 4, 5],
   [6, 7, 8]])
 R[ax,:][:,ax] = 100
 R
array([[0, 1, 2],
   [3, 4, 5],
   [6, 7, 8]])

Why R is not updated?

I was expecting:

 R
array([[0, 1, 2],
   [3, 100, 100],
   [6, 100, 100]])

I am using numpy 1.04 on gentoo-linux/amd64

  Nadav

___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Can not update a submatrix

2008-01-30 Thread Francesc Altet
A Wednesday 30 January 2008, Nadav Horesh escrigué:
 In the following piece of code:
  import numpy as N
  R = N.arange(9).reshape(3,3)
  ax = [1,2]
  R

 array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])

  R[ax,:][:,ax] = 100
  R

 array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])

 Why R is not updated?

Because R[ax] is not a view of R, but another copy of the original 
object (fancy indexing does return references to different objects).  
In order to get views, you must specify only a slice of the original 
array.  For example:

In [50]: S = R[::2]
In [51]: S[:] = 2
In [52]: R
Out[52]:
array([[2, 2, 2],
   [3, 4, 5],
   [2, 2, 2]])

So, what you need is something like:

In [68]: R = N.arange(9).reshape(3,3)
In [69]: S = R[1:3,:][:,1:3]
In [70]: S[:] = 2
In [71]: R
Out[71]:
array([[0, 1, 2],
   [3, 2, 2],
   [6, 2, 2]])

Cheers,

-- 
0,0   Francesc Altet     http://www.carabos.com/
V   V   Cárabos Coop. V.   Enjoy Data
 -
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Can not update a submatrix

2008-01-30 Thread Nadav Horesh
But:


 R[ax,:] = 100
 R
array([[  0,   1,   2],
   [100, 100, 100],
   [100, 100, 100]])
 R[:,ax] = 200
 R
array([[  0, 200, 200],
   [100, 200, 200],
   [100, 200, 200]])

Do I get an array view only if the array is contiguous?

  Nadav.

On Wed, 2008-01-30 at 16:08 +0100, Francesc Altet wrote:

 A Wednesday 30 January 2008, Nadav Horesh escrigué:
  In the following piece of code:
   import numpy as N
   R = N.arange(9).reshape(3,3)
   ax = [1,2]
   R
 
  array([[0, 1, 2],
 [3, 4, 5],
 [6, 7, 8]])
 
   R[ax,:][:,ax] = 100
   R
 
  array([[0, 1, 2],
 [3, 4, 5],
 [6, 7, 8]])
 
  Why R is not updated?
 
 Because R[ax] is not a view of R, but another copy of the original 
 object (fancy indexing does return references to different objects).  
 In order to get views, you must specify only a slice of the original 
 array.  For example:
 
 In [50]: S = R[::2]
 In [51]: S[:] = 2
 In [52]: R
 Out[52]:
 array([[2, 2, 2],
[3, 4, 5],
[2, 2, 2]])
 
 So, what you need is something like:
 
 In [68]: R = N.arange(9).reshape(3,3)
 In [69]: S = R[1:3,:][:,1:3]
 In [70]: S[:] = 2
 In [71]: R
 Out[71]:
 array([[0, 1, 2],
[3, 2, 2],
[6, 2, 2]])
 
 Cheers,
 
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Can not update a submatrix

2008-01-30 Thread Hans Meine
Am Mittwoch, 30. Januar 2008 16:21:40 schrieb Nadav Horesh:
 But:
  R[ax,:] = 100

This is calling __setitem__, i.e. does not create either a view or a copy.

Non-contiguous views (e.g. using [::2]) are also possible AFAIK, but fancy 
indexing is something different.

-- 
Ciao, /  /
 /--/
/  / ANS
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Can not update a submatrix

2008-01-30 Thread lorenzo bolla
you simply need to change the definition of ax:
ax = slice(1,3)

and all works fine.
L.

On 1/30/08, Francesc Altet [EMAIL PROTECTED] wrote:

 A Wednesday 30 January 2008, Nadav Horesh escrigué:
  In the following piece of code:
   import numpy as N
   R = N.arange(9).reshape(3,3)
   ax = [1,2]
   R
 
  array([[0, 1, 2],
 [3, 4, 5],
 [6, 7, 8]])
 
   R[ax,:][:,ax] = 100
   R
 
  array([[0, 1, 2],
 [3, 4, 5],
 [6, 7, 8]])
 
  Why R is not updated?

 Because R[ax] is not a view of R, but another copy of the original
 object (fancy indexing does return references to different objects).
 In order to get views, you must specify only a slice of the original
 array.  For example:

 In [50]: S = R[::2]
 In [51]: S[:] = 2
 In [52]: R
 Out[52]:
 array([[2, 2, 2],
   [3, 4, 5],
   [2, 2, 2]])

 So, what you need is something like:

 In [68]: R = N.arange(9).reshape(3,3)
 In [69]: S = R[1:3,:][:,1:3]
 In [70]: S[:] = 2
 In [71]: R
 Out[71]:
 array([[0, 1, 2],
   [3, 2, 2],
   [6, 2, 2]])

 Cheers,

 --
 0,0   Francesc Altet http://www.carabos.com/
 V   V   Cárabos Coop. V.   Enjoy Data
 -
 ___
 Numpy-discussion mailing list
 Numpy-discussion@scipy.org
 http://projects.scipy.org/mailman/listinfo/numpy-discussion




-- 
Lorenzo Bolla
[EMAIL PROTECTED]
http://lorenzobolla.emurse.com/
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Can not update a submatrix

2008-01-30 Thread Charles R Harris
On Jan 30, 2008 8:21 AM, Nadav Horesh [EMAIL PROTECTED] wrote:

  But:


  R[ax,:] = 100
  R
 array([[  0,   1,   2],
[100, 100, 100],
[100, 100, 100]])
  R[:,ax] = 200
  R
 array([[  0, 200, 200],
[100, 200, 200],
[100, 200, 200]])

 Do I get an array view only if the array is contiguous?


You get an array view if the elements can be addressed with offsets,
strides, and counts. Think nested for loops.

In [1]: a = zeros((4,4))

In [2]: a[::2,::2] = 1

In [3]: a
Out[3]:
array([[ 1.,  0.,  1.,  0.],
   [ 0.,  0.,  0.,  0.],
   [ 1.,  0.,  1.,  0.],
   [ 0.,  0.,  0.,  0.]])

Chuck
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Can not update a submatrix

2008-01-30 Thread lorenzo bolla
or you can maybe use numpy.ix_:
ax = [1,2]
R[numpy.ix_(ax,ax)] = 100

hth,
L.


On 1/30/08, lorenzo bolla [EMAIL PROTECTED] wrote:

 you simply need to change the definition of ax:
 ax = slice(1,3)

 and all works fine.
 L.

  On 1/30/08, Francesc Altet [EMAIL PROTECTED] wrote:
 
  A Wednesday 30 January 2008, Nadav Horesh escrigué:
   In the following piece of code:
import numpy as N
R = N.arange(9).reshape(3,3)
ax = [1,2]
R
  
   array([[0, 1, 2],
  [3, 4, 5],
  [6, 7, 8]])
  
R[ax,:][:,ax] = 100
R
  
   array([[0, 1, 2],
  [3, 4, 5],
  [6, 7, 8]])
  
   Why R is not updated?
 
  Because R[ax] is not a view of R, but another copy of the original
  object (fancy indexing does return references to different objects).
  In order to get views, you must specify only a slice of the original
  array.  For example:
 
  In [50]: S = R[::2]
  In [51]: S[:] = 2
  In [52]: R
  Out[52]:
  array([[2, 2, 2],
[3, 4, 5],
[2, 2, 2]])
 
  So, what you need is something like:
 
  In [68]: R = N.arange(9).reshape(3,3)
  In [69]: S = R[1:3,:][:,1:3]
  In [70]: S[:] = 2
  In [71]: R
  Out[71]:
  array([[0, 1, 2],
[3, 2, 2],
[6, 2, 2]])
 
  Cheers,
 
  --
  0,0   Francesc Altet http://www.carabos.com/
  V   V   Cárabos Coop. V.   Enjoy Data
  -
  ___
  Numpy-discussion mailing list
  Numpy-discussion@scipy.org
  http://projects.scipy.org/mailman/listinfo/numpy-discussion
 



 --
 Lorenzo Bolla
 [EMAIL PROTECTED]
 http://lorenzobolla.emurse.com/




-- 
Lorenzo Bolla
[EMAIL PROTECTED]
http://lorenzobolla.emurse.com/
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Vectorizing a function

2008-01-30 Thread Timothy Hochberg
On Jan 30, 2008 10:10 AM, Charles R Harris [EMAIL PROTECTED]
wrote:

[SNIP]



 IIRC, the way to do closures in Python is something like

 In [5]: def factory(x) :
...: def f() :
...: print x
...: f.x = x
...: return f
...:

 In [6]: f = factory(Hello world.)

 In [7]: f()
 Hello world.

 There is a reason to do it that way, but I don't recall what it is.


You don't need the f.x = x line. It's possible it has some beneficial side
effect that I don't recall, but basic closures work fine without out.

Nor do I know if the result can be vectorized, I've never used vectorize.


I expect they could be, but I don't use vectorize either.


-- 
.  __
.   |-\
.
.  [EMAIL PROTECTED]
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Vectorizing a function

2008-01-30 Thread Charles R Harris
On Jan 30, 2008 10:10 AM, Charles R Harris [EMAIL PROTECTED]
wrote:



 On Jan 30, 2008 2:22 AM, Gael Varoquaux [EMAIL PROTECTED]
 wrote:

  On Wed, Jan 30, 2008 at 12:49:44AM -0800, LB wrote:
   My problem is that the complexe calculations made in calc_0d use some
   parameters, which are currently defined at the head of my python file.
   This is not very nice and I can't define a module containing theses
   two functions and call them with different parameters.
 
   I would like to make this cleaner and pass theses parameter as
   keyword  argument, but this don't seems to be possible with vectorize.
   Indeed, some of theses parameters are array parameters and only the x
   and y arguments should be interpreted with the broadcasting rules
 
   What is the good way for doing this ?
 
  I don't know what the good way is, but you can always use functional
  programming style (Oh, no, CaML is getting on me !):
 
  def calc_0d_params(param1, param2, param3):
 def calc_0d(x, y):
 # Here your code making use of param1, param2, param3)
 ...
 
 return calc_0d(x, y)
 
  you call the function like this:
 
  calc_0d_params(param1, param2, param3)(x, y)
 
  To vectorize it you can do:
 
  calc_0d_vect = lambda *params: vectorize(calc_0d_params(*params))
 
  This is untested code, but I hope you get the idea. It all about partial
  evaluation of arguments. By the way, the parameters can now be keyword
  arguments.
 

 IIRC, the way to do closures in Python is something like

 In [5]: def factory(x) :
...: def f() :
...: print x
...: f.x = x
...: return f
...:


Oops, looks like that needs to be:

In [5]: def factory(x) :
   ...: def f() :
   ...: print f.x
   ...: f.x = x
   ...: return f
   ...:

You can also do something simpler:

In [51]: def f() : print f.x
   :

In [52]: f.x = Hello World!

In [53]: f()
Hello World!

Chuck
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Vectorizing a function

2008-01-30 Thread Charles R Harris
On Jan 30, 2008 10:18 AM, Timothy Hochberg [EMAIL PROTECTED] wrote:



 On Jan 30, 2008 10:10 AM, Charles R Harris [EMAIL PROTECTED]
 wrote:

 [SNIP]


 
  IIRC, the way to do closures in Python is something like
 
  In [5]: def factory(x) :
 ...: def f() :
 ...: print x
 ...: f.x = x
 ...: return f
 ...:
 
  In [6]: f = factory(Hello world.)
 
  In [7]: f()
  Hello world.
 
  There is a reason to do it that way, but I don't recall what it is.
 

 You don't need the f.x = x line. It's possible it has some beneficial
 side effect that I don't recall, but basic closures work fine without out.


You're right. It looks like the context gets stored in f.func_closure.

In [14]: f.func_closure[0].cell_contents
Out[14]: 'Hello world.'

Chuck
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Can not update a submatrix

2008-01-30 Thread Nadav Horesh

Thank you very much,
that what I was looking for.
Charles made a good point about offsets, counts and strides --- I really should 
go and reread the documentation.

  Nadav.

-Original Message-
From: [EMAIL PROTECTED] on behalf of lorenzo bolla
Sent: Wed 30-Jan-08 17:31
To: Discussion of Numerical Python
Subject: Re: [Numpy-discussion] Can not update a submatrix
 
or you can maybe use numpy.ix_:
ax = [1,2]
R[numpy.ix_(ax,ax)] = 100

hth,
L.


On 1/30/08, lorenzo bolla [EMAIL PROTECTED] wrote:

 you simply need to change the definition of ax:
 ax = slice(1,3)

 and all works fine.
 L.

  On 1/30/08, Francesc Altet [EMAIL PROTECTED] wrote:
 
  A Wednesday 30 January 2008, Nadav Horesh escrigué:
   In the following piece of code:
import numpy as N
R = N.arange(9).reshape(3,3)
ax = [1,2]
R
  
   array([[0, 1, 2],
  [3, 4, 5],
  [6, 7, 8]])
  
R[ax,:][:,ax] = 100
R
  
   array([[0, 1, 2],
  [3, 4, 5],
  [6, 7, 8]])
  
   Why R is not updated?
 
  Because R[ax] is not a view of R, but another copy of the original
  object (fancy indexing does return references to different objects).
  In order to get views, you must specify only a slice of the original
  array.  For example:
 
  In [50]: S = R[::2]
  In [51]: S[:] = 2
  In [52]: R
  Out[52]:
  array([[2, 2, 2],
[3, 4, 5],
[2, 2, 2]])
 
  So, what you need is something like:
 
  In [68]: R = N.arange(9).reshape(3,3)
  In [69]: S = R[1:3,:][:,1:3]
  In [70]: S[:] = 2
  In [71]: R
  Out[71]:
  array([[0, 1, 2],
[3, 2, 2],
[6, 2, 2]])
 
  Cheers,
 
  --
  0,0   Francesc Altet http://www.carabos.com/
  V   V   Cárabos Coop. V.   Enjoy Data
  -
  ___
  Numpy-discussion mailing list
  Numpy-discussion@scipy.org
  http://projects.scipy.org/mailman/listinfo/numpy-discussion
 



 --
 Lorenzo Bolla
 [EMAIL PROTECTED]
 http://lorenzobolla.emurse.com/




-- 
Lorenzo Bolla
[EMAIL PROTECTED]
http://lorenzobolla.emurse.com/

winmail.dat___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Median again

2008-01-30 Thread Travis E. Oliphant
Timothy Hochberg wrote:


 On Jan 29, 2008 5:48 PM, Travis E. Oliphant [EMAIL PROTECTED] 
 mailto:[EMAIL PROTECTED] wrote:

 Joris De Ridder wrote:
  On 30 Jan 2008, at 00:32, Travis E. Oliphant wrote:
 
 
  Matthew Brett wrote:
 
  Hi,
 
 
  median moved mediandim0
  implementation of medianwithaxis or similar, with same call
  signature as mean.
 
 
  But - for the median function change - do we agree that this
 should
  be
  changed?  I think it is a significant wart in the numpy API,
 and has
  caught quite a few people...
 
 
  I'm fine with a median API change for 1.1.
 
  We can add the axis keyword for 1.0.5 as long as the default
 stays the
  same.  We can also add the other keywords as well if appropriate
  defaults can be determined.
 
 
  Do you mean creating a median(a, axis=0) for 1.0.5, and changing
 it to
  median(a,axis=None) for 1.1? (Modulo other keywords).
 

 Yes.   That is the approach I prefer.  


 I'm all for fixing this, but the prospect of going straight from  one 
 default to another makes me nervous. Is there is any prospect we could 
 spit out a warning when an axis is not specified for median starting 
 in 1.05 up till 1.1. It could even be a PendingDeprecationWarning, 
 which by default doesn't print anything I believe, but would allow 
 people to check there code for potential failure points.
Yes, we could start to do that (spit out a warning in 1.0.5).  This 
should definitely be done in 1.0.6

Perhaps we use axis=None to start with and then check for that and spit 
out the warning (and change axis to 0). Thanks for the pointer to 
PendingDeprecationWarning.

We could also have an APIChangeWarning that we define in NumPy which 
would allow SciPy to use it as well.

-Travis


___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Vectorizing a function

2008-01-30 Thread LB
Thank you Gael, I think this could work for my case.

It will be a bit tricky, since calc_0d is already a closure in which
I've defined a function : the parameters x and y are to main
parameters of an ODE.
So calc_0d define a function, integrate it sing scipy.integrate.odeint
and returns some characteristics of the solution. Numpy.vectorize
allows me to draw very easily map and contours of theses
characteristics.

So I will have a function, defining a function, defining another
one...

___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Can not update a submatrix

2008-01-30 Thread Anne Archibald
On 30/01/2008, Francesc Altet [EMAIL PROTECTED] wrote:
 A Wednesday 30 January 2008, Nadav Horesh escrigué:
  In the following piece of code:
   import numpy as N
   R = N.arange(9).reshape(3,3)
   ax = [1,2]
   R
 
  array([[0, 1, 2],
 [3, 4, 5],
 [6, 7, 8]])
 
   R[ax,:][:,ax] = 100
   R
 
  array([[0, 1, 2],
 [3, 4, 5],
 [6, 7, 8]])
 
  Why R is not updated?

 Because R[ax] is not a view of R, but another copy of the original
 object (fancy indexing does return references to different objects).
 In order to get views, you must specify only a slice of the original
 array.  For example:

This is not exactly correct.

There are two kinds of fancy indexing in numpy, which behave
similarly. There is normal fancy indexing, which produces a new array,
not sharing data with the old array:

a = N.random.normal(size=10)
b = a[a0]

There is also fancy indexing of lvalues (to use a C term):

a = N.random.normal(size=10)
a[a0] *= -1

Here no copy is made; instead, the data is modified in-place. This
requires some clever hacks under the hood, since a[a0] *can't* be a
view of a.

The problem the OP had is that they are going beyond what the clever
hacks can deal with. That's why

R[ax,:] = 100

works but

R[ax,:][:,ax] = 100

doesn't. The problem is that you have two indexing operations in the
second situation, and the inplace operators can't deal with that.

Solutions include working on a flattened version of the array (for
which a single indexing operation suffices), using the (not in-place)
where() construct, or using the ix_ function:

R[N.ix_(ax,ax)] = 100

N.ix_ is necessary because R[ax,ax] doesn't do what I, for one, would
have expected: R[[1,2],[3,4]] yields [R[1,3],R[2,4]], not
[[R[1,3],R[1,4]],[R[2,3],R[2,4]]]. But in any case, once you reduce it
to a single fancy-indexing operation, you can successfully use it as
an lvalue. Striding and views are not really the issue.

Anne
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Can not update a submatrix

2008-01-30 Thread Timothy Hochberg
On Jan 30, 2008 12:43 PM, Anne Archibald [EMAIL PROTECTED] wrote:

 On 30/01/2008, Francesc Altet [EMAIL PROTECTED] wrote:
  A Wednesday 30 January 2008, Nadav Horesh escrigué:
   In the following piece of code:
import numpy as N
R = N.arange(9).reshape(3,3)
ax = [1,2]
R
  
   array([[0, 1, 2],
  [3, 4, 5],
  [6, 7, 8]])
  
R[ax,:][:,ax] = 100
R
  
   array([[0, 1, 2],
  [3, 4, 5],
  [6, 7, 8]])
  
   Why R is not updated?
 
  Because R[ax] is not a view of R, but another copy of the original
  object (fancy indexing does return references to different objects).
  In order to get views, you must specify only a slice of the original
  array.  For example:

 This is not exactly correct.


[...a fine explanation by Anne...]

Another way to look at this is note that:

R[ax,:][:,ax] = 100

is translated to:

R.__getitem__((ax, :)).__setitem__((:,ax), 100)

'__setitem__' is capable of setting values using fancy indexing, but as has
been noted, '__getitem__' makes a copy of R when using fancy indexing, and
therefore the original does not get modified.

[Technically, ':' gets translated to slice(None,None,None), but I didn't
want to write that out].

-- 
.  __
.   |-\
.
.  [EMAIL PROTECTED]
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Median again

2008-01-30 Thread Jarrod Millman
On Jan 30, 2008 10:41 AM, Travis E. Oliphant [EMAIL PROTECTED] wrote:
 Yes, we could start to do that (spit out a warning in 1.0.5).  This
 should definitely be done in 1.0.6

 Perhaps we use axis=None to start with and then check for that and spit
 out the warning (and change axis to 0). Thanks for the pointer to
 PendingDeprecationWarning.

 We could also have an APIChangeWarning that we define in NumPy which
 would allow SciPy to use it as well.

+1
I think this sounds very reasonable and will fix a major NumPy wart.

-- 
Jarrod Millman
Computational Infrastructure for Research Labs
10 Giannini Hall, UC Berkeley
phone: 510.643.4014
http://cirl.berkeley.edu/
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


[Numpy-discussion] Visual studio with mingw g77 built libraries

2008-01-30 Thread David Cournapeau
Hi,

I would like to ask some details about the build process of numpy
when visual studio is used for C compilation, and g77 for blas/lapack.
As I understand it, the current situation consists in using libraries
built the Unix way (e.g. libblas.a, static library built with ar +
ranlib), taking the gcc runtimes on the fly (libgcc and g2c), and put
everything together to feed VS compiler. I wonder if we should not use
another way, that is use libraries built the windows way (dll +
lib). I think it is more natural (and would also make my life easier
for numscons):

 - it would means that on windows, all compilers combination would
follow a similar process.
 - dll and lib are more natural for windows developers (who are the
ones using VS in the first place).

The only difference for users would be in the way blas/lapack should
be built. Is there a reason why this was not the chosen path ? Is it
because it would have been too difficult to do it with distutils
(using dlltool and co) ? Or other reasons I am not aware of ?

cheers,

David
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion