get the shape of a numpy ndarray in C++ code [boost.python]

2007-11-09 Thread Marc Oldenhof
[sorry is half a post appeared earlier. Bloody Google groups...]

Hello,

I'm trying to use a numpy array in C++ (win2000) using boost.python.

Test code:
void test( numeric::array& nsP)
{
   object shape = nsP.getshape();
   int rows = extract(shape[0]);
   int cols = extract(shape[1]);
}

At first, running it in Python got me this message:

   ArgumentError: Python argument types in
   d3d.wr_conn(numpy.ndarray)
   did not match C++ signature:
   wr_conn(class boost::python::numeric::array {lvalue})

I fixed this using this line:

   numeric::array::set_module_and_type( "numpy", "ndarray");

[was that right?]


At least it got me one step further; the array is accepted. Now the
message is this:

   AttributeError: 'numpy.ndarray' object has no attribute 'getshape'

Now I'm stumped. The only thing I can find is
   http://www.thescripts.com/forum/showthread.php?t=644270

which regrettably ends with the same question. What's wrong here?

greets,
Marc

-- 
http://mail.python.org/mailman/listinfo/python-list


error using all()/numpy [TypeError: cannot perform reduce with flexible type]

2008-05-23 Thread Marc Oldenhof
Hello all,

I'm pretty new to Python, but use it a lot lately. I'm getting a crazy
error trying to do operations on a string list after importing numpy.
Minimal example:

[start Python]

Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.

>>> a=['1','2','3']
>>> all(a)
True
>>> from numpy import *
>>> all(a)
Traceback (most recent call last):
  File "", line 1, in 
  File "C:\Python25\lib\site-packages\numpy\core\fromnumeric.py", line
982, in all
return _wrapit(a, 'all', axis, out)
  File "C:\Python25\lib\site-packages\numpy\core\fromnumeric.py", line
37, in _wrapit
result = getattr(asarray(obj),method)(*args, **kwds)
TypeError: cannot perform reduce with flexible type

[end of example]

It seems that Python calls numpy's "all" instead of the standard one,
is that right? If so, how can I call the standard "all" after the
numpy import? ["import numpy" is not a desirable option, I use a lot
of math in my progs]

Is there another way around this error?

Marc
--
http://mail.python.org/mailman/listinfo/python-list


Re: error using all()/numpy [TypeError: cannot perform reduce with flexible type]

2008-05-23 Thread Marc Oldenhof
On 23 mei, 09:12, Marc Oldenhof <[EMAIL PROTECTED]> wrote:
> Hello all,
>
> I'm pretty new to Python, but use it a lot lately. I'm getting a crazy
> error trying to do operations on a string list after importing numpy.
> Minimal example:
>
> [start Python]
>
> Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit
> (Intel)] on win32
> Type "help", "copyright", "credits" or "license" for more information.
>
> >>> a=['1','2','3']
> >>> all(a)
> True
> >>> from numpy import *
> >>> all(a)
>
> Traceback (most recent call last):
>   File "", line 1, in 
>   File "C:\Python25\lib\site-packages\numpy\core\fromnumeric.py", line
> 982, in all
>     return _wrapit(a, 'all', axis, out)
>   File "C:\Python25\lib\site-packages\numpy\core\fromnumeric.py", line
> 37, in _wrapit
>     result = getattr(asarray(obj),method)(*args, **kwds)
> TypeError: cannot perform reduce with flexible type
>
> [end of example]
>
> It seems that Python calls numpy's "all" instead of the standard one,
> is that right? If so, how can I call the standard "all" after the
> numpy import? ["import numpy" is not a desirable option, I use a lot
> of math in my progs]
>
> Is there another way around this error?
>
> Marc

Never mind, I found a way. For reference:

>>> import __builtin__
>>> __builtin__.all(a)
True

Works!

Marc
--
http://mail.python.org/mailman/listinfo/python-list


Re: error using all()/numpy [TypeError: cannot perform reduce with flexible type]

2008-05-23 Thread Marc Oldenhof
On 23 mei, 09:12, Marc Oldenhof <[EMAIL PROTECTED]> wrote:


Thanks for the reactions, I'll use the "from numpy import " from now on :)

Marc
--
http://mail.python.org/mailman/listinfo/python-list