Simon Berube | 1 May 14:05
Picon

Re: simpliest way to check: array x is float, not integer

When using numpy array the type of the array is given by the "dtype"
variable of the array. So if your array is int then array.dtype will
be 'int32'. Numpy uses more complex data types then just int and
floats so you might want to check all the available data types.

Ex:

In [168]: a = array([1,2,3])
In [169]: a.dtype
Out[169]: dtype('int32')

OR (Array)

In [170]: b = array([1.,2.,3.])
In [171]: b.dtype
Out[171]: dtype('float64')

OR (Strings)
In [172]: c = array(['sdf', 'fff', 'tye'])
In [173]: c.dtype
Out[173]: dtype('|S3')   # This could also be 'Sxxx' where xxx is the
length of the largest string in the array

Alternatively, as a hackjob type check you could also do an
"isinstance" check on the first element of the array since, unlike
lists, arrays have uniform elements all the way through.

Hope that helps,

- Simon Berube
(Continue reading)

Mark.Miller | 1 May 16:32

OverflowError: long too big to convert

Can someone explain this?  I can't seem to coerce numpy into storing 
large integer values.  I'm sure that I'm just overlooking something 
simple...

 >>> import numpy
 >>> a='1'*300
 >>> type(a)
<type 'str'>
 >>> b=int(a)
 >>> type(b)
<type 'long'>
 >>> c=numpy.empty((2,2),long)
 >>> c[:]=b
Traceback (most recent call last):
   File "<pyshell#15>", line 1, in <module>
     c[:]=b
OverflowError: long too big to convert
 >>>

Thanks,

-Mark
Robert Kern | 1 May 16:43
Picon
Gravatar

Re: OverflowError: long too big to convert

Mark.Miller wrote:
> Can someone explain this?  I can't seem to coerce numpy into storing 
> large integer values.  I'm sure that I'm just overlooking something 
> simple...
> 
> 
>  >>> import numpy
>  >>> a='1'*300
>  >>> type(a)
> <type 'str'>
>  >>> b=int(a)
>  >>> type(b)
> <type 'long'>
>  >>> c=numpy.empty((2,2),long)
>  >>> c[:]=b
> Traceback (most recent call last):
>    File "<pyshell#15>", line 1, in <module>
>      c[:]=b
> OverflowError: long too big to convert
>  >>>

Use object arrays explicitly:

  c = numpy.empty((2, 2), dtype=object)

Using dtype=long gets interpreted as requesting the largest available integer
type (or maybe just int64, I'm not sure). Those aren't unbounded.

--

-- 
Robert Kern
(Continue reading)

Mark.Miller | 1 May 16:59

Re: OverflowError: long too big to convert

OK...so just for future reference...does a Numpy 'long' not directly 
correspond to a Python 'long'?

Robert Kern wrote:
> Mark.Miller wrote:
>> Can someone explain this?  I can't seem to coerce numpy into storing 
>> large integer values.  I'm sure that I'm just overlooking something 
>> simple...
>>
>>
>>  >>> import numpy
>>  >>> a='1'*300
>>  >>> type(a)
>> <type 'str'>
>>  >>> b=int(a)
>>  >>> type(b)
>> <type 'long'>
>>  >>> c=numpy.empty((2,2),long)
>>  >>> c[:]=b
>> Traceback (most recent call last):
>>    File "<pyshell#15>", line 1, in <module>
>>      c[:]=b
>> OverflowError: long too big to convert
>>  >>>
> 
> Use object arrays explicitly:
> 
>   c = numpy.empty((2, 2), dtype=object)
> 
> Using dtype=long gets interpreted as requesting the largest available integer
(Continue reading)

Robert Kern | 1 May 17:18
Picon
Gravatar

Re: OverflowError: long too big to convert

Mark.Miller wrote:
> OK...so just for future reference...does a Numpy 'long' not directly 
> correspond to a Python 'long'?

There is no Numpy "long", per se. There is a numpy.long symbol exposed, but it
is just the builtin long type. However, numpy has no special support for
Python's unbounded long type. You have to use object arrays if you want to hold
them in numpy 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
Charles R Harris | 1 May 17:19
Picon

Re: OverflowError: long too big to convert



On 5/1/07, Mark.Miller <mpmusu <at> cc.usu.edu> wrote:
OK...so just for future reference...does a Numpy 'long' not directly
correspond to a Python 'long'?

No. A  numpy long corresponds, more or less, to the C long long int.

In [2]: array([1],dtype=long)
Out[2]: array([1], dtype=int64)

Chuck


_______________________________________________
Numpy-discussion mailing list
Numpy-discussion <at> scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion
Picon
Picon
Favicon

Re: simpliest way to check: array x is float, not integer

On Tue, May 01, 2007 at 12:05:20PM -0000, Simon Berube wrote:
> Alternatively, as a hackjob type check you could also do an
> "isinstance" check on the first element of the array since, unlike
> lists, arrays have uniform elements all the way through.

Or use

N.issubdtype(x.dtype,int)  and  N.issubdtype(x.dtype,float)

Cheers
Stéfan
Picon

ctypes TypeError. What I am doing wrong?

Hi

I wrote the next function just to learn how ctypes work with numpy arrays.  I 
am not trying to write yet another wrapper to lapack, it's just an 
experiment. (you can cut and paste the code)

from ctypes import c_int
from numpy import array,float64
from numpy.ctypeslib import load_library,ndpointer

def dgesv(N,A,B):
#    A=array([[1,2],[3,4]],dtype=float64,order='FORTRAN')
#    B=array([1,2],dtype=float64,order='FORTRAN')
    cN=c_int(N)
    NRHS=c_int(1)
    LDA=c_int(N)
    IPIV=(c_int * N)()
    LDB=c_int(N)
    INFO=c_int(1)

    lapack=load_library('liblapack.so','/usr/lib/')

    lapack.argtypes=[c_int,c_int,
                     ndpointer(dtype=float64,
                               ndim=2,
                               flags='FORTRAN'),
                     c_int,c_int,
                     ndpointer(dtype=float64,
                               ndim=1,
                               flags='FORTRAN'),
                     c_int,c_int]

    lapack.dgesv_(cN,NRHS,A,LDA,IPIV,B,LDB,INFO)

    return B

And...

In [2]: from lapackw import dgesv
In [3]: from numpy import array
In [4]: dgesv(2,array([[1,2],[3,4]]),array([1,2]))
...

ArgumentError: argument 3: exceptions.TypeError: Don't know how to convert 
parameter 3

Maybe I am sooo dumb but I just don't know what I am doing wrong. numpy-1.0.1, 
ctypes 1.0.1, python 2.4.3.

thanks

guillem
Picon
Picon
Favicon

Re: ctypes TypeError. What I am doing wrong?

Hi Guillem

On Wed, May 02, 2007 at 12:38:12AM +0200, Guillem Borrell i Nogueras wrote:
> I wrote the next function just to learn how ctypes work with numpy arrays.  I 
> am not trying to write yet another wrapper to lapack, it's just an 
> experiment. (you can cut and paste the code)
> 
> from ctypes import c_int
> from numpy import array,float64
> from numpy.ctypeslib import load_library,ndpointer
> 
> def dgesv(N,A,B):
> #    A=array([[1,2],[3,4]],dtype=float64,order='FORTRAN')
> #    B=array([1,2],dtype=float64,order='FORTRAN')

Here, I'd use something like

A = asfortranarray(A.astype(float64))
B = asfortranarray(B.astype(float64))

>     cN=c_int(N)
>     NRHS=c_int(1)
>     LDA=c_int(N)
>     IPIV=(c_int * N)()
>     LDB=c_int(N)
>     INFO=c_int(1)
>     
>     lapack=load_library('liblapack.so','/usr/lib/')
> 
>     lapack.argtypes=[c_int,c_int,
>                      ndpointer(dtype=float64,
>                                ndim=2,
>                                flags='FORTRAN'),
>                      c_int,c_int,
>                      ndpointer(dtype=float64,
>                                ndim=1,
>                                flags='FORTRAN'),
>                      c_int,c_int]

This should be lapack.dgesv_.argtypes.

Cheers
Stéfan
Picon
Picon
Favicon

Re: ctypes TypeError. What I am doing wrong?

On Wed, May 02, 2007 at 12:38:12AM +0200, Guillem Borrell i Nogueras wrote:
>     lapack.argtypes=[c_int,c_int,
>                      ndpointer(dtype=float64,
>                                ndim=2,
>                                flags='FORTRAN'),
>                      c_int,c_int,
>                      ndpointer(dtype=float64,
>                                ndim=1,
>                                flags='FORTRAN'),
>                      c_int,c_int]

This also isn't correct, according to the dgesv documentation.  It
should be

    lapack.dgesv_.argtypes=[POINTER(c_int),POINTER(c_int),
                            ndpointer(dtype=np.float64,
                            ndim=2,
                            flags='FORTRAN'),
                            POINTER(c_int), POINTER(c_int),

                            ndpointer(dtype=np.float64,
                                      ndim=2,
                                      flags='FORTRAN'),
                            POINTER(c_int),POINTER(c_int)]

I attach a working version of your script.

Cheers
Stéfan
Attachment (lapack.py): text/x-python, 1047 bytes
_______________________________________________
Numpy-discussion mailing list
Numpy-discussion <at> scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion

Gmane