Re: [Numpy-discussion] Iterate over all 1-dim views

2007-10-08 Thread Stefan van der Walt
On Sun, Oct 07, 2007 at 06:52:11AM -0400, Neal Becker wrote:
 Suppose I have a function F(), which is defined for 1-dim arguments.  If the
 user passes an n1 dim array, I want to apply F to each 1-dim view.
 
 For example, for a 2-d array, apply F to each row and return a 2-d result.
 
 For a 3-d array, select each 2-d subarray and see above.  Return 3-d result.
 
 Any suggestions on how to code something like this in numpy?

Not the most efficient way, but easy to read and understand:

import numpy as N

def func(a):
return a.shape

z = N.zeros((2,2,2,2))
print N.array([func(sub) for sub in z])

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


[Numpy-discussion] dot product not behaving as expected

2007-10-08 Thread Robin
Hi,

I have a problem using numpy.dot, see below:

In [151]: m=5

In [152]: n=5

In [153]: x=(m*ones((1,5)))**arange(0,n)

In [154]: y=test.order_length[::-1]

In [155]: x
Out[155]: array([[   1.,5.,   25.,  125.,  625.]])

In [156]: y
Out[156]:
array([[ 1024.],
   [ 1280.],
   [  640.],
   [  160.],
   [   20.]])

In [157]: numpy.dot(x,y)
Out[157]: array([[ 64.]])

In [158]: sum(x*y.T)
Out[158]: 55924.0

Am I missing something?

Thanks,

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


Re: [Numpy-discussion] dot product not behaving as expected

2007-10-08 Thread Robert Kern
Robin wrote:
 Hi,
 
 I have a problem using numpy.dot, see below:
 
 In [151]: m=5
 
 In [152]: n=5
 
 In [153]: x=(m*ones((1,5)))**arange(0,n)
 
 In [154]: y=test.order_length[::-1]
 
 In [155]: x
 Out[155]: array([[   1.,5.,   25.,  125.,  625.]])
 
 In [156]: y
 Out[156]:
 array([[ 1024.],
[ 1280.],
[  640.],
[  160.],
[   20.]])
 
 In [157]: numpy.dot(x,y)
 Out[157]: array([[ 64.]])
 
 In [158]: sum(x* y.T)
 Out[158]: 55924.0
 
 Am I missing something?

In [1]: from numpy import *

In [2]: x = array([[   1.,5.,   25.,  125.,  625.]])

In [3]: y = array([[ 1024.],
   ...:[ 1280.],
   ...:[  640.],
   ...:[  160.],
   ...:[   20.]])

In [4]: dot(x, y)
Out[4]: array([[ 55924.]])

In [5]: sum(x * y.T)
Out[5]: 55924.0


It works for me with a recent SVN numpy on OS X. What version of numpy are you
using? What platform are you on? Did you build with ATLAS or other optimized
linear algebra library?

-- 
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
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] dot product not behaving as expected

2007-10-08 Thread Robin

 It works for me with a recent SVN numpy on OS X. What version of numpy are
 you
 using? What platform are you on? Did you build with ATLAS or other
 optimized
 linear algebra library?


I am on Ubuntu 7.04, gcc 4.1.2

In [181]: numpy.__version__
Out[181]: '1.0.4.dev4155'

Built with ATLAS 3.7.37, Lapack 3.1.1

It also occurs for me on Windows XP SP2 with 1.0.4dev4154 built with cygwin
gcc 3.4.4 and atlas 3.7.37 and lapack 3.1.1.

Actually I found that for me it also doesn't occur if I type them in like
that. The problem comes when y is reverse with the [::-1] index. Is this the
correct way to reverse a vector?

Anyway try this:

In [36]: x=array([[1.,5.,25.,125.,625.]])

In [37]: y=array([[20.],[160.],[640.],[1280.],[1024.]])

In [38]: y
Out[38]:
array([[   20.],
   [  160.],
   [  640.],
   [ 1280.],
   [ 1024.]])

In [39]: y2=y[::-1]

In [40]: y2
Out[40]:
array([[ 1024.],
   [ 1280.],
   [  640.],
   [  160.],
   [   20.]])

In [41]: dot(x,y)
Out[41]: array([[ 816820.]])

In [42]: dot(x,y2)
Out[42]: array([[ 64.]])

Thanks,

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


Re: [Numpy-discussion] dot product not behaving as expected

2007-10-08 Thread Robert Kern
Robin wrote:
 It works for me with a recent SVN numpy on OS X. What version of
 numpy are you
 using? What platform are you on? Did you build with ATLAS or other
 optimized
 linear algebra library?
 
 
 I am on Ubuntu 7.04, gcc 4.1.2
 
 In [181]: numpy.__version__
 Out[181]: '1.0.4.dev4155'
 
 Built with ATLAS 3.7.37, Lapack 3.1.1
 
 It also occurs for me on Windows XP SP2 with 1.0.4dev4154 built with
 cygwin gcc 3.4.4 and atlas 3.7.37 and lapack 3.1.1.
 
 Actually I found that for me it also doesn't occur if I type them in
 like that. The problem comes when y is reverse with the [::-1] index. Is
 this the correct way to reverse a vector?

Aha. Yes, that is the correct way to do it. However, it appears that there is a
bug in how dot() is interpreting the non-contiguous array. In the meantime, you
can make a contiguous array with array(y2).

-- 
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
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Convert array type

2007-10-08 Thread Ryan May
Gary Ruben wrote:
 Try using astype. This works:
 
 values = array(wavearray.split()).astype(float)
 

Why not use numpy.fromstring?

fromstring(string, dtype=float, count=-1, sep='')

Return a new 1d array initialized from the raw binary data in string.

If count is positive, the new array will have count elements,
otherwise its
size is determined by the size of string.  If sep is not empty then the
string is interpreted in ASCII mode and converted to the desired
number type
using sep as the separator between elements (extra whitespace is
ignored).

Ryan

-- 
Ryan May
Graduate Research Assistant
School of Meteorology
University of Oklahoma
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


[Numpy-discussion] numpy/Windows shared arrays between processes?

2007-10-08 Thread Ray S
Is anyone sharing arrays between processes on Windows?
I tried compiling the posh sources (once, so far) with the new MS 
toolkit and failed...
What other solutions are in use?

Have a second process create an array view from an address would 
suffice for this particular purpose. I could pass the address as a 
parameter of the second process's argv.

I've also tried things like
pb=pythonapi.PyBuffer_FromReadWriteMemory(9508824, 9*sizeof(c_int))
N.frombuffer(pb, N.int32)
which fails since pb is and int. What are my options?

Ray Schumacher

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


Re: [Numpy-discussion] Convert array type

2007-10-08 Thread Adam Mercer
On 08/10/2007, Ryan May [EMAIL PROTECTED] wrote:

 Why not use numpy.fromstring?

because that results in the array being filled with gibberish

values = numpy.fromstring(wavearray, dtype=float, count=-1, sep='')
print values

gives:

[  1.39804329e-076   1.30354290e-076   1.18295070e-076 ...,
   5.45168074e-067   2.11101912e-052   6.58519056e-260]

where using

values = array(wavearray.split()).astype(float)
print values

results in the correct

[  0.e+00   0.e+00   0.e+00 ...,
   4.22233200e-23   3.86799900e-23   3.48452000e-23]

Cheers

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


Re: [Numpy-discussion] Convert array type

2007-10-08 Thread Robert Kern
Adam Mercer wrote:
 On 08/10/2007, Ryan May [EMAIL PROTECTED] wrote:
 
 Why not use numpy.fromstring?
 
 because that results in the array being filled with gibberish
 
 values = numpy.fromstring(wavearray, dtype=float, count=-1, sep='')

Use sep=' '. As the docstring says, if sep is empty, then the string is
interpreted as binary data. If it is not empty, then the string is interpreted
as ASCII.

-- 
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
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Convert array type

2007-10-08 Thread Charles R Harris
On 10/8/07, Adam Mercer [EMAIL PROTECTED] wrote:

 On 08/10/2007, Ryan May [EMAIL PROTECTED] wrote:

  Why not use numpy.fromstring?

 because that results in the array being filled with gibberish

 values = numpy.fromstring(wavearray, dtype=float, count=-1, sep='')
 print values


You need to use

values = numpy.fromstring(wavearray, dtype=float, count=-1, sep=' ')

Note that sep is a blank (whitespace). This will convert ascii strings.

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


Re: [Numpy-discussion] beginner question: rank-1 arrays

2007-10-08 Thread Gael Varoquaux
On Mon, Oct 08, 2007 at 11:00:39PM +0100, Robin wrote:
Coming from matlab and being use to 0:10 for row or (0:10)' for column
this seems a bit messy. Is there a better way of constructing row/column
2d arrays from a slice type range?

r_[0:10] and c_[0:10].

Does that suit you ? The first one is indeed only 1D, but I don't see the
problem with that. If you really want 2D you can use c_[0:10].T .

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


Re: [Numpy-discussion] beginner question: rank-1 arrays

2007-10-08 Thread Stefan van der Walt
On Mon, Oct 08, 2007 at 11:00:39PM +0100, Robin wrote:
 Hi,
 
 I am trying to implement a project in scipy. I think I am getting somewhere
 finally.
 
 However in my code (I am converting from MATLAB) it is important to maintain 
 2d
 arrays, and keep the difference between row and column vectors. After working
 through some initial problems I think I am getting more of a picture of how
 things work in numpy.
 
 However I am finding my code littered with things like:
 np.array(np.r_[0:nterms],ndmin=2)(for a row vector)
 or
 np.array(np.r_[0:nterms],ndmin=2).T (for a column vector)

You can use

N.arange(10).reshape(-1,1)

or

N.c_[:10,]

But if you use this sort of thing often, just write your own factory
method:

def col(n):
return N.arange(n).reshape(-1,1)

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


Re: [Numpy-discussion] beginner question: rank-1 arrays

2007-10-08 Thread Gael Varoquaux
On Mon, Oct 08, 2007 at 11:12:07PM +0100, Robin wrote:
On 10/8/07, Gael Varoquaux [EMAIL PROTECTED] wrote:

  r_[0:10] and c_[0:10].

  Does that suit you ? The first one is indeed only 1D, but I don't see
  the
  problem with that. If you really want 2D you can use c_[0:10].T .

Thanks, but not really :)

Firstly - for me I don' see any difference between the two, they both give
a numpy rank-1 array which doesn't have a row/column characteristic.
x.shape for both of the above is (10,) I need (10,1) or (1,10) for my
code.

Damn it. Shame on me. I meant c_[0:10,]. If you really need a shape of
(1,10) (I have never had such a need) you can use c_[0:10,].T.

HTH,

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


Re: [Numpy-discussion] beginner question: rank-1 arrays

2007-10-08 Thread Alan G Isaac
On Mon, 8 Oct 2007, Robin apparently wrote:
 However in my code (I am converting from MATLAB) it is 
 important to maintain 2d arrays, and keep the difference 
 between row and column vectors.

How about using matrices?
help(numpy.mat)

hth,
Alan Isaac



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


Re: [Numpy-discussion] beginner question: rank-1 arrays

2007-10-08 Thread Robin
On 10/8/07, Gael Varoquaux [EMAIL PROTECTED] wrote:

 Damn it. Shame on me. I meant c_[0:10,]. If you really need a shape of
 (1,10) (I have never had such a need) you can use c_[0:10,].T.


Thanks! - the trick with the , is just the sort of thing I was looking for -
I knew there must be an easy way...

Cheers

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


Re: [Numpy-discussion] Convert array type

2007-10-08 Thread Adam Mercer
On 08/10/2007, Robert Kern [EMAIL PROTECTED] wrote:

 Use sep=' '. As the docstring says, if sep is empty, then the string is
 interpreted as binary data. If it is not empty, then the string is interpreted
 as ASCII.

Thanks, got it the wrong way round. That works now.

Cheers

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