From numpy-svn at scipy.org Sat Jul 1 12:53:18 2006 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Sat, 1 Jul 2006 11:53:18 -0500 (CDT) Subject: [Numpy-svn] r2717 - in trunk/numpy/core: include/numpy src Message-ID: <20060701165318.629FDD48659@new.scipy.org> Author: oliphant Date: 2006-07-01 11:53:14 -0500 (Sat, 01 Jul 2006) New Revision: 2717 Modified: trunk/numpy/core/include/numpy/arrayobject.h trunk/numpy/core/src/arrayobject.c trunk/numpy/core/src/scalartypes.inc.src Log: Fix typo shown in #164 and change dummy to two in PyArrayInterface Modified: trunk/numpy/core/include/numpy/arrayobject.h =================================================================== --- trunk/numpy/core/include/numpy/arrayobject.h 2006-06-30 21:56:21 UTC (rev 2716) +++ trunk/numpy/core/include/numpy/arrayobject.h 2006-07-01 16:53:14 UTC (rev 2717) @@ -1463,7 +1463,7 @@ http://numeric.scipy.org/array_interface.html for the full documentation. */ typedef struct { - int dummy; /* contains the integer 2 as a sanity check */ + int two; /* contains the integer 2 as a sanity check */ int nd; /* number of dimensions */ char typekind; /* kind in array --- character code of typestr */ int itemsize; /* size of each element */ Modified: trunk/numpy/core/src/arrayobject.c =================================================================== --- trunk/numpy/core/src/arrayobject.c 2006-06-30 21:56:21 UTC (rev 2716) +++ trunk/numpy/core/src/arrayobject.c 2006-07-01 16:53:14 UTC (rev 2717) @@ -540,7 +540,7 @@ #elif SIZEOF_INTP == SIZEOF_INT descr = &INT_Descr; #else - descr = &LONGLONG_DESCR; + descr = &LONGLONG_Descr; #endif arr = NULL; @@ -5787,7 +5787,7 @@ PyArrayInterface *inter; inter = (PyArrayInterface *)_pya_malloc(sizeof(PyArrayInterface)); - inter->dummy = 2; + inter->two = 2; inter->nd = self->nd; inter->typekind = self->descr->kind; inter->itemsize = self->descr->elsize; @@ -6253,7 +6253,7 @@ if (PyCObject_Check(e)) { PyArrayInterface *inter; inter = (PyArrayInterface *)PyCObject_AsVoidPtr(e); - if (inter->dummy == 2) { + if (inter->two == 2) { d = inter->nd; } } @@ -6453,7 +6453,7 @@ char buf[40]; if (PyCObject_Check(ip)) { inter=(PyArrayInterface *)PyCObject_AsVoidPtr(ip); - if (inter->dummy == 2) { + if (inter->two == 2) { snprintf(buf, 40, "|%c%d", inter->typekind, inter->itemsize); chktype = _array_typedescr_fromstr(buf); @@ -7214,7 +7214,7 @@ } if (!PyCObject_Check(attr)) goto fail; inter = PyCObject_AsVoidPtr(attr); - if (inter->dummy != 2) goto fail; + if (inter->two != 2) goto fail; if ((inter->flags & NOTSWAPPED) != NOTSWAPPED) { endian = PyArray_OPPBYTE; inter->flags &= ~NOTSWAPPED; Modified: trunk/numpy/core/src/scalartypes.inc.src =================================================================== --- trunk/numpy/core/src/scalartypes.inc.src 2006-06-30 21:56:21 UTC (rev 2716) +++ trunk/numpy/core/src/scalartypes.inc.src 2006-07-01 16:53:14 UTC (rev 2717) @@ -729,7 +729,7 @@ arr = (PyArrayObject *)PyArray_FromScalar(self, NULL); inter = (PyArrayInterface *)_pya_malloc(sizeof(PyArrayInterface)); - inter->dummy = 2; + inter->two = 2; inter->nd = 0; inter->flags = arr->flags; inter->typekind = arr->descr->kind; From numpy-svn at scipy.org Sat Jul 1 13:11:31 2006 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Sat, 1 Jul 2006 12:11:31 -0500 (CDT) Subject: [Numpy-svn] r2718 - in trunk/numpy: lib lib/tests oldnumeric Message-ID: <20060701171131.E16EBD48659@new.scipy.org> Author: oliphant Date: 2006-07-01 12:11:27 -0500 (Sat, 01 Jul 2006) New Revision: 2718 Modified: trunk/numpy/lib/tests/test_twodim_base.py trunk/numpy/lib/twodim_base.py trunk/numpy/oldnumeric/olddefaults.py Log: Added histogram2d #161 Modified: trunk/numpy/lib/tests/test_twodim_base.py =================================================================== --- trunk/numpy/lib/tests/test_twodim_base.py 2006-07-01 16:53:14 UTC (rev 2717) +++ trunk/numpy/lib/tests/test_twodim_base.py 2006-07-01 17:11:27 UTC (rev 2718) @@ -16,7 +16,7 @@ data = add.outer(data,data) return data -class test_eye(ScipyTestCase): +class test_eye(NumpyTestCase): def check_basic(self): assert_equal(eye(4),array([[1,0,0,0], [0,1,0,0], @@ -52,7 +52,7 @@ [1,0,0], [0,1,0]])) -class test_diag(ScipyTestCase): +class test_diag(NumpyTestCase): def check_vector(self): vals = (100*arange(5)).astype('l') b = zeros((5,5)) @@ -81,7 +81,7 @@ b[k] = vals[k+2,k] assert_equal(diag(vals,-2),b[:3]) -class test_fliplr(ScipyTestCase): +class test_fliplr(NumpyTestCase): def check_basic(self): self.failUnlessRaises(ValueError, fliplr, ones(4)) a = get_mat(4) @@ -93,7 +93,7 @@ [5,4,3]] assert_equal(fliplr(a),b) -class test_flipud(ScipyTestCase): +class test_flipud(NumpyTestCase): def check_basic(self): a = get_mat(4) b = a[::-1,:] @@ -104,7 +104,7 @@ [0,1,2]] assert_equal(flipud(a),b) -class test_rot90(ScipyTestCase): +class test_rot90(NumpyTestCase): def check_basic(self): self.failUnlessRaises(ValueError, rot90, ones(4)) @@ -130,5 +130,26 @@ for k in range(0,13,4): assert_equal(rot90(a,k=k),b4) + +class test_histogram2d(NumpyTestCase): + def check_simple(self): + import numpy as np + np.random.seed(1) + x = np.rand(5) + y = np.rand(5) + xedges = np.linspace(0,1,10) + yedges = np.linspace(0,1,10) + H = np.histogram2d(x,y, (xedges, yedges))[0] + answer = np.array([[0, 0, 0, 1, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 1, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0], + [1, 0, 1, 0, 0, 0, 0, 0, 0], + [0, 1, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0]]) + assert_equal(H, answer) + if __name__ == "__main__": - ScipyTest().run() + NumpyTest().run() Modified: trunk/numpy/lib/twodim_base.py =================================================================== --- trunk/numpy/lib/twodim_base.py 2006-07-01 16:53:14 UTC (rev 2717) +++ trunk/numpy/lib/twodim_base.py 2006-07-01 17:11:27 UTC (rev 2718) @@ -3,7 +3,7 @@ """ __all__ = ['diag','eye','fliplr','flipud','rot90','tri','triu','tril', - 'vander'] + 'vander','histogram2d'] from numpy.core.numeric import asanyarray, int_, equal, subtract, arange, \ zeros, arange, greater_equal, multiply, ones, asarray @@ -123,3 +123,75 @@ for i in range(N-1): X[:,i] = x**(N-i-1) return X + + +# David Huard, June 2006 +# Adapted for python from the matlab function hist2d written by Laszlo Balkay, 2006. +# Numpy compatible license. +def histogram2d(x,y, bins, normed = False): + """Compute the 2D histogram for a dataset (x,y) given the edges or + the number of bins. + + Returns histogram, xedges, yedges. + The histogram array is a count of the number of samples in each bin. + The array is oriented such that H[i,j] is the number of samples falling + into binx[j] and biny[i]. + Setting normed to True returns a density rather than a bin count. + Data falling outside of the edges are not counted. + """ + import numpy as np + if len(bins)==2: + if np.isscalar(bins[0]): + xnbin = bins[0] + xedges = np.linspace(x.min(), x.max(), xnbin+1) + + else: + xedges = bins[0] + xnbin = len(xedges)-1 + + if np.isscalar(bins[1]): + ynbin = bins[1] + yedges = np.linspace(y.min(), y.max(), ynbin+1) + else: + yedges = bins[1] + ynbin = len(yedges)-1 + else: + raise AttributeError, 'bins must be a sequence of length 2, with either the number of bins or the bin edges.' + + + # Flattened histogram matrix (1D) + hist = np.zeros((xnbin)*(ynbin), int) + + # Count the number of sample in each bin (1D) + xbin = np.digitize(x,xedges) + ybin = np.digitize(y,yedges) + + # Remove the outliers + outliers = (xbin==0) | (xbin==xnbin+1) | (ybin==0) | (ybin == ynbin+1) + + xbin = xbin[outliers==False] + ybin = ybin[outliers == False] + + # Compute the sample indices in the flattened histogram matrix. + if xnbin >= ynbin: + xy = ybin*(xnbin) + xbin + shift = xnbin + else: + xy = xbin*(ynbin) + ybin + shift = ynbin + + # Compute the number of repetitions in xy and assign it to the flattened + # histogram matrix. + edges = np.unique(xy) + edges.sort() + flatcount = np.histogram(xy, edges)[0] + indices = edges - shift - 1 + hist[indices] = flatcount + + # Shape into a proper matrix + histmat = hist.reshape(xnbin, ynbin) + + if normed: + diff2 = np.outer(np.diff(yedges), np.diff(xedges)) + histmat = histmat / diff2 / histmat.sum() + return histmat, xedges, yedges Modified: trunk/numpy/oldnumeric/olddefaults.py =================================================================== --- trunk/numpy/oldnumeric/olddefaults.py 2006-07-01 16:53:14 UTC (rev 2717) +++ trunk/numpy/oldnumeric/olddefaults.py 2006-07-01 17:11:27 UTC (rev 2718) @@ -27,7 +27,7 @@ and everything else is zeros. """ if M is None: M = N - m = equal(subtract.outer(arange(N), arange(M)),-k) + m = nn.equal(nn.subtract.outer(nn.arange(N), nn.arange(M)),-k) if m.dtype != dtype: return m.astype(dtype) @@ -36,7 +36,7 @@ lower left corner up to the k-th are all ones. """ if M is None: M = N - m = greater_equal(subtract.outer(arange(N), arange(M)),-k) + m = nn.greater_equal(nn.subtract.outer(nn.arange(N), nn.arange(M)),-k) if m.dtype != dtype: return m.astype(dtype) From numpy-svn at scipy.org Sat Jul 1 13:36:16 2006 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Sat, 1 Jul 2006 12:36:16 -0500 (CDT) Subject: [Numpy-svn] r2719 - trunk/numpy/lib Message-ID: <20060701173616.A4B28D4865D@new.scipy.org> Author: oliphant Date: 2006-07-01 12:36:14 -0500 (Sat, 01 Jul 2006) New Revision: 2719 Modified: trunk/numpy/lib/function_base.py trunk/numpy/lib/twodim_base.py Log: Adjust histogram2d a bit. Fix doc for logspace. Modified: trunk/numpy/lib/function_base.py =================================================================== --- trunk/numpy/lib/function_base.py 2006-07-01 17:11:27 UTC (rev 2718) +++ trunk/numpy/lib/function_base.py 2006-07-01 17:36:14 UTC (rev 2719) @@ -29,8 +29,8 @@ def linspace(start, stop, num=50, endpoint=True, retstep=False): """Return evenly spaced numbers. - Return 'num' evenly spaced samples from 'start' to 'stop'. If - 'endpoint' is True, the last sample is 'stop'. If 'retstep' is + Return num evenly spaced samples from start to stop. If + endpoint is True, the last sample is stop. If retstep is True then return the step value used. """ num = int(num) @@ -51,9 +51,8 @@ def logspace(start,stop,num=50,endpoint=True,base=10.0): """Evenly spaced numbers on a logarithmic scale. - Computes int(num) evenly spaced exponents from start to stop. - If endpoint=True, then last exponent is stop. - Returns base**exponents. + Computes int(num) evenly spaced exponents from base**start to + base**stop. If endpoint=True, then last number is base**stop """ y = linspace(start,stop,num=num,endpoint=endpoint) return _nx.power(base,y) Modified: trunk/numpy/lib/twodim_base.py =================================================================== --- trunk/numpy/lib/twodim_base.py 2006-07-01 17:11:27 UTC (rev 2718) +++ trunk/numpy/lib/twodim_base.py 2006-07-01 17:36:14 UTC (rev 2719) @@ -140,7 +140,12 @@ Data falling outside of the edges are not counted. """ import numpy as np - if len(bins)==2: + try: + N = len(bins) + except TypeError: + N = 1 + bins = [bins] + if N == 2: if np.isscalar(bins[0]): xnbin = bins[0] xedges = np.linspace(x.min(), x.max(), xnbin+1) @@ -155,9 +160,15 @@ else: yedges = bins[1] ynbin = len(yedges)-1 + elif N == 1: + ynbin = xnbin = bins[0] + xedges = np.linspace(x.min(), x.max(), xnbin+1) + yedges = np.linspace(y.max(), y.min(), ynbin+1) else: - raise AttributeError, 'bins must be a sequence of length 2, with either the number of bins or the bin edges.' - + yedges = asarray(bins) + xedges = yedges.copy() + ynbin = len(yedges)-1 + xnbin = len(xedges)-1 # Flattened histogram matrix (1D) hist = np.zeros((xnbin)*(ynbin), int) @@ -170,7 +181,7 @@ outliers = (xbin==0) | (xbin==xnbin+1) | (ybin==0) | (ybin == ynbin+1) xbin = xbin[outliers==False] - ybin = ybin[outliers == False] + ybin = ybin[outliers==False] # Compute the sample indices in the flattened histogram matrix. if xnbin >= ynbin: From numpy-svn at scipy.org Sat Jul 1 16:54:30 2006 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Sat, 1 Jul 2006 15:54:30 -0500 (CDT) Subject: [Numpy-svn] r2720 - in trunk/numpy: core/include/numpy doc/pyrex lib Message-ID: <20060701205430.0A972158A0A@new.scipy.org> Author: oliphant Date: 2006-07-01 15:54:24 -0500 (Sat, 01 Jul 2006) New Revision: 2720 Modified: trunk/numpy/core/include/numpy/arrayobject.h trunk/numpy/doc/pyrex/c_numpy.pxd trunk/numpy/lib/twodim_base.py trunk/numpy/lib/utils.py Log: Improve c_numpy.pxd a bit. Re-name get_numpy_include() to get_include() and deprecate old name. Add PyArray_ZEROS and PyArray_EMPTY macros which take type-number instead of data-type object. Modified: trunk/numpy/core/include/numpy/arrayobject.h =================================================================== --- trunk/numpy/core/include/numpy/arrayobject.h 2006-07-01 17:36:14 UTC (rev 2719) +++ trunk/numpy/core/include/numpy/arrayobject.h 2006-07-01 20:54:24 UTC (rev 2720) @@ -1536,6 +1536,10 @@ PyArray_FromAny(m, PyArray_DescrFromType(type), min, max, \ (((flags) & ENSURECOPY) ? \ (flags) | DEFAULT_FLAGS : (flags)), NULL) +#define PyArray_ZEROS(m, dims, type, fortran) \ + PyArray_Zeros(m, dims, PyArray_DescrFromType(type), fortran) +#define PyArray_EMPTY(m, dims, type, fortran) \ + PyArray_Empty(m, dims, PyArray_DescrFromType(type), fortran) #define PyArray_FILLWBYTE(obj, val) memset(PyArray_DATA(obj), (val), PyArray_NBYTES(obj)) Modified: trunk/numpy/doc/pyrex/c_numpy.pxd =================================================================== --- trunk/numpy/doc/pyrex/c_numpy.pxd 2006-07-01 17:36:14 UTC (rev 2719) +++ trunk/numpy/doc/pyrex/c_numpy.pxd 2006-07-01 20:54:24 UTC (rev 2720) @@ -4,7 +4,8 @@ cdef extern from "numpy/arrayobject.h": - ctypedef enum PyArray_TYPES: + + cdef enum PyArray_TYPES: PyArray_BOOL PyArray_BYTE PyArray_UBYTE @@ -14,8 +15,8 @@ PyArray_UINT PyArray_LONG PyArray_ULONG - PyArray_LONGLONG - PyArray_ULONGLONG + PyArray_LONGLONG + PyArray_ULONGLONG PyArray_FLOAT PyArray_DOUBLE PyArray_LONGDOUBLE @@ -29,12 +30,51 @@ PyArray_NTYPES PyArray_NOTYPE + cdef enum requirements: + CONTIGUOUS + FORTRAN + OWNDATA + FORCECAST + ENSURECOPY + ENSUREARRAY + ELEMENTSTRIDES + ALIGNED + NOTSWAPPED + WRITEABLE + UPDATEIFCOPY + ARR_HAS_DESCR + + BEHAVED_FLAGS + BEHAVED_NS_FLAGS + CARRAY_FLAGS + CARRAY_FLAGS_RO + FARRAY_FLAGS + FARRAY_FLAGS_RO + DEFAULT_FLAGS + + IN_ARRAY + OUT_ARRAY + INOUT_ARRAY + IN_FARRAY + OUT_FARRAY + INOUT_FARRAY + + UPDATE_ALL_FLAGS + + ctypedef struct cdouble: + double real + double imag + + ctypedef struct cfloat: + double real + double imag + ctypedef int intp ctypedef extern class numpy.dtype [object PyArray_Descr]: cdef int type_num, elsize, alignment cdef char type, kind, byteorder, hasobject - cdef object fields, typeobj + cdef object fields, typeobj ctypedef extern class numpy.ndarray [object PyArrayObject]: cdef char *data @@ -45,15 +85,22 @@ cdef dtype descr cdef int flags - ndarray PyArray_SimpleNew(int ndims, intp* dims, int item_type) + object PyArray_ZEROS(int ndims, intp* dims, PyArray_TYPES type_num, int fortran) + object PyArray_EMPTY(int ndims, intp* dims, PyArray_TYPES type_num, int fortran) + dtype PyArray_DescrFromTypeNum(PyArray_TYPES type_num) + object PyArray_SimpleNew(int ndims, intp* dims, PyArray_TYPES type_num) int PyArray_Check(object obj) - ndarray PyArray_ContiguousFromObject(object obj, PyArray_TYPES type, + object PyArray_ContiguousFromAny(object obj, PyArray_TYPES type, int mindim, int maxdim) intp PyArray_SIZE(ndarray arr) + intp PyArray_NBYTES(ndarray arr) void *PyArray_DATA(ndarray arr) - ndarray PyArray_FromAny(object obj, dtype newtype, int mindim, int maxdim, + object PyArray_FromAny(object obj, dtype newtype, int mindim, int maxdim, int requirements, object context) - ndarray PyArray_NewFromDescr(object subtype, dtype newtype, int nd, intp* dims, - intp* strides, void* data, int flags, object parent) + object PyArray_FROMANY(object obj, PyArray_TYPES type_num, int min, + int max, int requirements) + object PyArray_NewFromDescr(object subtype, dtype newtype, int nd, + intp* dims, intp* strides, void* data, + int flags, object parent) void import_array() Modified: trunk/numpy/lib/twodim_base.py =================================================================== --- trunk/numpy/lib/twodim_base.py 2006-07-01 17:36:14 UTC (rev 2719) +++ trunk/numpy/lib/twodim_base.py 2006-07-01 20:54:24 UTC (rev 2720) @@ -52,7 +52,7 @@ def diag(v, k=0): """ returns a copy of the the k-th diagonal if v is a 2-d array or returns a 2-d array with v as the k-th diagonal if v is a - 1-d array. + 1-d array. """ v = asarray(v) s = v.shape Modified: trunk/numpy/lib/utils.py =================================================================== --- trunk/numpy/lib/utils.py 2006-07-01 17:36:14 UTC (rev 2719) +++ trunk/numpy/lib/utils.py 2006-07-01 20:54:24 UTC (rev 2720) @@ -1,7 +1,8 @@ import sys from numpy.core.numerictypes import obj2sctype -__all__ = ['issubclass_', 'get_numpy_include', 'issubsctype', 'deprecate'] +__all__ = ['issubclass_', 'get_numpy_include', 'issubsctype', 'deprecate', + 'get_include'] def issubclass_(arg1, arg2): try: @@ -12,7 +13,7 @@ def issubsctype(arg1, arg2): return issubclass(obj2sctype(arg1), obj2sctype(arg2)) -def get_numpy_include(): +def get_include(): """Return the directory in the package that contains the numpy/*.h header files. @@ -21,7 +22,7 @@ import numpy Extension('extension_name', ... - include_dirs=[numpy.get_numpy_include()]) + include_dirs=[numpy.get_include()]) """ from numpy.distutils.misc_util import get_numpy_include_dirs include_dirs = get_numpy_include_dirs() @@ -61,3 +62,6 @@ else: newfunc.__dict__.update(d) return newfunc + + +get_numpy_include = deprecate(get_include, 'get_numpy_include', 'get_include') From numpy-svn at scipy.org Sat Jul 1 23:00:04 2006 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Sat, 1 Jul 2006 22:00:04 -0500 (CDT) Subject: [Numpy-svn] r2721 - in trunk: benchmarks numpy/core numpy/core/include/numpy numpy/core/src Message-ID: <20060702030004.B8D49D4866A@new.scipy.org> Author: oliphant Date: 2006-07-01 21:59:54 -0500 (Sat, 01 Jul 2006) New Revision: 2721 Modified: trunk/benchmarks/casting.py trunk/benchmarks/creating.py trunk/numpy/core/_internal.py trunk/numpy/core/include/numpy/arrayobject.h trunk/numpy/core/numerictypes.py trunk/numpy/core/src/arraymethods.c trunk/numpy/core/src/arrayobject.c trunk/numpy/core/src/multiarraymodule.c trunk/numpy/core/src/scalartypes.inc.src Log: Remove dependency on _internal.py from pickles. Clean up Py_ssize_t usage. Add .ctypes attribute for use with the ctypes module if it's available. Modified: trunk/benchmarks/casting.py =================================================================== --- trunk/benchmarks/casting.py 2006-07-01 20:54:24 UTC (rev 2720) +++ trunk/benchmarks/casting.py 2006-07-02 02:59:54 UTC (rev 2721) @@ -1,9 +1,9 @@ import timeit -N = [1000,100] +N = [10,10] t1 = timeit.Timer('b = a.astype(int)','import numpy;a=numpy.zeros(shape=%s,dtype=float)'%N) t2 = timeit.Timer('b = a.astype("l")','import Numeric;a=Numeric.zeros(shape=%s,typecode="d")'%N) t3 = timeit.Timer("b = a.astype('l')","import numarray; a=numarray.zeros(shape=%s,typecode='d')"%N) print "1-D length = ", N -print "NumPy: ", t1.repeat(3,100) -print "Numeric: ", t2.repeat(3,100) -print "Numarray: ", t3.repeat(3,100) +print "NumPy: ", t1.repeat(3,1000) +print "Numeric: ", t2.repeat(3,1000) +print "Numarray: ", t3.repeat(3,1000) Modified: trunk/benchmarks/creating.py =================================================================== --- trunk/benchmarks/creating.py 2006-07-01 20:54:24 UTC (rev 2720) +++ trunk/benchmarks/creating.py 2006-07-02 02:59:54 UTC (rev 2721) @@ -1,9 +1,9 @@ import timeit -N = [1000,100] +N = [10,10] t1 = timeit.Timer('a=N.zeros(shape,type)','import numpy as N; shape=%s;type=float'%N) t2 = timeit.Timer('a=N.zeros(shape,type)','import Numeric as N; shape=%s;type=N.Float'%N) t3 = timeit.Timer('a=N.zeros(shape,type)',"import numarray as N; shape=%s;type=N.Float"%N) print "shape = ", N -print "NumPy: ", t1.repeat(3,100) -print "Numeric: ", t2.repeat(3,100) -print "Numarray: ", t3.repeat(3,100) +print "NumPy: ", t1.repeat(3,10000) +print "Numeric: ", t2.repeat(3,10000) +print "Numarray: ", t3.repeat(3,10000) Modified: trunk/numpy/core/_internal.py =================================================================== --- trunk/numpy/core/_internal.py 2006-07-01 20:54:24 UTC (rev 2720) +++ trunk/numpy/core/_internal.py 2006-07-02 02:59:54 UTC (rev 2721) @@ -102,7 +102,8 @@ # Build a new array from the information in a pickle. # Note that the name numpy.core._internal._reconstruct is embedded in -# the pickles of ndarrays, so don't remove the name here, or you'll +# pickles of ndarrays made with NumPy before release 1.0 +# so don't remove the name here, or you'll # break backward compatibilty. def _reconstruct(subtype, shape, dtype): return ndarray.__new__(subtype, shape, dtype) @@ -168,4 +169,42 @@ return result +def _getintp_ctype(): + if _getintp_ctype.cache: + return _getintp_ctype.cache + import ctypes + char = dtype('p').char + if (char == 'i'): + val = ctypes.c_int + elif char == 'l': + val = ctypes.c_long + elif char == 'q': + val = ctypes.c_longlong + else: + raise ValueError, "confused about intp->ctypes." + _getintp_ctype.cache = val + return val +_getintp_ctype.cache = None +# Used for .ctypes attribute of ndarray +class _ctypes(object): + def __init__(self, array): + try: + import ctypes + self._ctypes = ctypes + except ImportError: + raise AttributeError, "ctypes not available" + self._arr = array + + def get_data(self): + return self._ctypes.c_void_p(self._arr.__array_interface__['data'][0]) + + def get_dims(self): + return (_getintp_ctype()*self._arr.ndim)(*self._arr.shape) + + def get_strides(self): + return (_getintp_ctype()*self._arr.ndim)(*self._arr.strides) + + data = property(get_data, None, doc="c-types data") + dims = property(get_dims, None, doc="c-types dims") + strides = property(get_strides, None, doc="c-types strides") Modified: trunk/numpy/core/include/numpy/arrayobject.h =================================================================== --- trunk/numpy/core/include/numpy/arrayobject.h 2006-07-01 20:54:24 UTC (rev 2720) +++ trunk/numpy/core/include/numpy/arrayobject.h 2006-07-02 02:59:54 UTC (rev 2721) @@ -730,10 +730,10 @@ #define SIZEOF_INTP SIZEOF_PY_INTPTR_T #define SIZEOF_UINTP SIZEOF_PY_INTPTR_T -#if PY_VERSION_HEX >= 0x02050000 -#define _int_or_ssize_t Py_ssize_t -#else -#define _int_or_ssize_t int +#if (PY_VERSION_HEX < 0x02050000) +typedef int Py_ssize_t; +#define PY_SSIZE_T_MAX INT_MAX +#define PY_SSIZE_T_MIN INT_MIN #endif #if SIZEOF_PY_INTPTR_T == SIZEOF_INT Modified: trunk/numpy/core/numerictypes.py =================================================================== --- trunk/numpy/core/numerictypes.py 2006-07-01 20:54:24 UTC (rev 2720) +++ trunk/numpy/core/numerictypes.py 2006-07-02 02:59:54 UTC (rev 2721) @@ -76,8 +76,9 @@ """ # we add more at the bottom -__all__ = ['typeDict', 'typeNA', 'sctypes', 'ScalarType', 'obj2sctype', 'cast', 'nbytes', - 'sctype2char', 'maximum_sctype', 'issctype', 'typecodes'] +__all__ = ['typeDict', 'typeNA', 'sctypes', 'ScalarType', 'obj2sctype', + 'cast', 'nbytes', 'sctype2char', 'maximum_sctype', 'issctype', + 'typecodes'] from multiarray import typeinfo, ndarray, array, empty import types as _types Modified: trunk/numpy/core/src/arraymethods.c =================================================================== --- trunk/numpy/core/src/arraymethods.c 2006-07-01 20:54:24 UTC (rev 2720) +++ trunk/numpy/core/src/arraymethods.c 2006-07-02 02:59:54 UTC (rev 2721) @@ -877,7 +877,7 @@ ret = PyTuple_New(3); if (ret == NULL) return NULL; - mod = PyImport_ImportModule("numpy.core._internal"); + mod = PyImport_ImportModule("numpy.core.multiarray"); if (mod == NULL) {Py_DECREF(ret); return NULL;} obj = PyObject_GetAttrString(mod, "_reconstruct"); Py_DECREF(mod); Modified: trunk/numpy/core/src/arrayobject.c =================================================================== --- trunk/numpy/core/src/arrayobject.c 2006-07-01 20:54:24 UTC (rev 2720) +++ trunk/numpy/core/src/arrayobject.c 2006-07-02 02:59:54 UTC (rev 2721) @@ -1771,7 +1771,7 @@ **************** Implement Mapping Protocol *************************** *************************************************************************/ -static _int_or_ssize_t +static Py_ssize_t array_length(PyArrayObject *self) { if (self->nd != 0) { @@ -1812,7 +1812,7 @@ /* contains optimization for 1-d arrays */ static PyObject * -array_item_nice(PyArrayObject *self, _int_or_ssize_t i) +array_item_nice(PyArrayObject *self, Py_ssize_t i) { if (self->nd == 1) { char *item; @@ -1877,7 +1877,7 @@ #endif #ifndef array_ass_item static int -array_ass_item(PyArrayObject *self, _int_or_ssize_t i, PyObject *v) +array_ass_item(PyArrayObject *self, Py_ssize_t i, PyObject *v) { return array_ass_big_item(self, (intp) i, v); } @@ -2825,8 +2825,8 @@ /* removed multiple segment interface */ -static _int_or_ssize_t -array_getsegcount(PyArrayObject *self, _int_or_ssize_t *lenp) +static Py_ssize_t +array_getsegcount(PyArrayObject *self, Py_ssize_t *lenp) { if (lenp) *lenp = PyArray_NBYTES(self); @@ -2840,8 +2840,8 @@ return 0; } -static _int_or_ssize_t -array_getreadbuf(PyArrayObject *self, _int_or_ssize_t segment, void **ptrptr) +static Py_ssize_t +array_getreadbuf(PyArrayObject *self, Py_ssize_t segment, void **ptrptr) { if (segment != 0) { PyErr_SetString(PyExc_ValueError, @@ -2859,8 +2859,8 @@ } -static _int_or_ssize_t -array_getwritebuf(PyArrayObject *self, _int_or_ssize_t segment, void **ptrptr) +static Py_ssize_t +array_getwritebuf(PyArrayObject *self, Py_ssize_t segment, void **ptrptr) { if (PyArray_CHKFLAGS(self, WRITEABLE)) return array_getreadbuf(self, segment, (void **) ptrptr); @@ -2871,8 +2871,8 @@ } } -static _int_or_ssize_t -array_getcharbuf(PyArrayObject *self, _int_or_ssize_t segment, const char **ptrptr) +static Py_ssize_t +array_getcharbuf(PyArrayObject *self, Py_ssize_t segment, const char **ptrptr) { if (self->descr->type_num == PyArray_STRING || \ self->descr->type_num == PyArray_UNICODE) @@ -3670,11 +3670,11 @@ static PyObject * -array_slice(PyArrayObject *self, _int_or_ssize_t ilow, - _int_or_ssize_t ihigh) +array_slice(PyArrayObject *self, Py_ssize_t ilow, + Py_ssize_t ihigh) { PyArrayObject *r; - _int_or_ssize_t l; + Py_ssize_t l; char *data; if (self->nd == 0) { @@ -3715,8 +3715,8 @@ static int -array_ass_slice(PyArrayObject *self, _int_or_ssize_t ilow, - _int_or_ssize_t ihigh, PyObject *v) { +array_ass_slice(PyArrayObject *self, Py_ssize_t ilow, + Py_ssize_t ihigh, PyObject *v) { int ret; PyArrayObject *tmp; @@ -5546,6 +5546,13 @@ } static PyObject * +array_ctypes_get(PyArrayObject *self) +{ + return PyObject_CallMethod(_numpy_internal, "_ctypes", + "O", self); +} + +static PyObject * array_interface_get(PyArrayObject *self) { PyObject *dict; @@ -6102,6 +6109,10 @@ (getter)array_flat_get, (setter)array_flat_set, "a 1-d view of a contiguous array"}, + {"ctypes", + (getter)array_ctypes_get, + NULL, + "Ctypes interface object"}, {"__array_interface__", (getter)array_interface_get, NULL, @@ -7997,7 +8008,7 @@ _pya_free(it); } -static _int_or_ssize_t +static Py_ssize_t iter_length(PyArrayIterObject *self) { return self->size; @@ -10290,9 +10301,11 @@ **************** Implement Mapping Protocol *************************** *************************************************************************/ -static _int_or_ssize_t -descr_length(PyArray_Descr *self) +static Py_ssize_t +descr_length(PyObject *self0) { + + PyArray_Descr *self = (PyArray_Descr *)self0; if (self->fields && self->fields != Py_None) /* Remove the last entry (root) */ @@ -10335,13 +10348,9 @@ } static PyMappingMethods descr_as_mapping = { -#if PY_VERSION_HEX >= 0x02050000 - (lenfunc)descr_length, /*mp_length*/ -#else - (inquiry)descr_length, /*mp_length*/ -#endif + descr_length, /*mp_length*/ (binaryfunc)descr_subscript, /*mp_subscript*/ - (objobjargproc)NULL, /*mp_ass_subscript*/ + (objobjargproc)NULL, /*mp_ass_subscript*/ }; /****************** End of Mapping Protocol ******************************/ Modified: trunk/numpy/core/src/multiarraymodule.c =================================================================== --- trunk/numpy/core/src/multiarraymodule.c 2006-07-01 20:54:24 UTC (rev 2720) +++ trunk/numpy/core/src/multiarraymodule.c 2006-07-02 02:59:54 UTC (rev 2721) @@ -15,6 +15,7 @@ /* $Id: multiarraymodule.c,v 1.36 2005/09/14 00:14:00 teoliphant Exp $ */ +#define PY_SSIZE_T_CLEAN #include "Python.h" #include "structmember.h" /*#include @@ -5031,7 +5032,7 @@ char *data; longlong nin=-1; char *sep=NULL; - int s; + Py_ssize_t s; static char *kwlist[] = {"string", "dtype", "count", "sep", NULL}; PyArray_Descr *descr=NULL; @@ -5807,6 +5808,37 @@ return PyInt_FromLong( (long) PyArray_GetNDArrayCVersion() ); } +static char +doc__reconstruct[] = "_reconstruct(subtype, shape, dtype) constructs an empty array. Used by Pickles."; + +static PyObject * +array__reconstruct(PyObject *dummy, PyObject *args) +{ + + PyTypeObject *subtype; + PyArray_Dims shape = {NULL, 0}; + PyArray_Descr *dtype=NULL; + if (!PyArg_ParseTuple(args, "O!O&O&", &PyType_Type, &subtype, + PyArray_IntpConverter, &shape, + PyArray_DescrConverter, &dtype)) + goto fail; + + if (!PyType_IsSubtype(subtype, &PyArray_Type)) { + PyErr_SetString(PyExc_TypeError, + "_reconstruct: First argument must be " \ + "a sub-type of ndarray"); + goto fail; + } + + return PyArray_NewFromDescr(subtype, dtype, + (int)shape.len, shape.ptr, + NULL, NULL, 0, NULL); + fail: + Py_XDECREF(dtype); + if (shape.ptr) PyDimMem_FREE(shape.ptr); + return NULL; +} + static char doc_set_string_function[] = "set_string_function(f, repr=1) sets the python function f to be the function used to obtain a pretty printable string version of a array whenever a array is printed. f(M) should expect a array argument M, and should return a string consisting of the desired representation of M for printing."; @@ -6047,6 +6079,8 @@ static struct PyMethodDef array_module_methods[] = { {"_get_ndarray_c_version", (PyCFunction)array__get_ndarray_c_version, METH_VARARGS|METH_KEYWORDS, doc__get_ndarray_c_version}, + {"_reconstruct", (PyCFunction)array__reconstruct, + METH_VARARGS, doc__reconstruct}, {"set_string_function", (PyCFunction)array_set_string_function, METH_VARARGS|METH_KEYWORDS, doc_set_string_function}, {"set_numeric_ops", (PyCFunction)array_set_ops_function, Modified: trunk/numpy/core/src/scalartypes.inc.src =================================================================== --- trunk/numpy/core/src/scalartypes.inc.src 2006-07-01 20:54:24 UTC (rev 2720) +++ trunk/numpy/core/src/scalartypes.inc.src 2006-07-02 02:59:54 UTC (rev 2721) @@ -1400,7 +1400,7 @@ /************* As_mapping functions for void array scalar ************/ -static _int_or_ssize_t +static Py_ssize_t voidtype_length(PyVoidScalarObject *self) { if (!self->descr->fields || self->descr->fields == Py_None) { @@ -1418,7 +1418,7 @@ } static PyObject * -voidtype_item(PyVoidScalarObject *self, _int_or_ssize_t n) +voidtype_item(PyVoidScalarObject *self, Py_ssize_t n) { intp m; PyObject *flist=NULL, *key, *fieldinfo; @@ -1467,7 +1467,7 @@ n = PyArray_PyIntAsIntp(ind); if (error_converting(n)) goto fail; - return voidtype_item(self, (_int_or_ssize_t)n); + return voidtype_item(self, (Py_ssize_t)n); fail: PyErr_SetString(PyExc_IndexError, "invalid index"); @@ -1476,7 +1476,7 @@ } static int -voidtype_ass_item(PyVoidScalarObject *self, _int_or_ssize_t n, PyObject *val) +voidtype_ass_item(PyVoidScalarObject *self, Py_ssize_t n, PyObject *val) { intp m; PyObject *flist=NULL, *key, *fieldinfo, *newtup; @@ -1542,7 +1542,7 @@ /* try to convert it to a number */ n = PyArray_PyIntAsIntp(ind); if (error_converting(n)) goto fail; - return voidtype_ass_item(self, (_int_or_ssize_t)n, val); + return voidtype_ass_item(self, (Py_ssize_t)n, val); fail: PyErr_SetString(PyExc_IndexError, msg); @@ -2001,14 +2001,14 @@ return PySequence_Concat(self->obval, other); } -static _int_or_ssize_t +static Py_ssize_t object_arrtype_length(PyObjectScalarObject *self) { return PyObject_Length(self->obval); } static PyObject * -object_arrtype_repeat(PyObjectScalarObject *self, _int_or_ssize_t count) +object_arrtype_repeat(PyObjectScalarObject *self, Py_ssize_t count) { return PySequence_Repeat(self->obval, count); } @@ -2039,7 +2039,7 @@ } static PyObject * -object_arrtype_inplace_repeat(PyObjectScalarObject *self, _int_or_ssize_t count) +object_arrtype_inplace_repeat(PyObjectScalarObject *self, Py_ssize_t count) { return PySequence_InPlaceRepeat(self->obval, count); } @@ -2082,8 +2082,8 @@ #endif }; -static _int_or_ssize_t -object_arrtype_getsegcount(PyObjectScalarObject *self, _int_or_ssize_t *lenp) +static Py_ssize_t +object_arrtype_getsegcount(PyObjectScalarObject *self, Py_ssize_t *lenp) { int newlen; int cnt; @@ -2100,8 +2100,8 @@ return cnt; } -static _int_or_ssize_t -object_arrtype_getreadbuf(PyObjectScalarObject *self, _int_or_ssize_t segment, void **ptrptr) +static Py_ssize_t +object_arrtype_getreadbuf(PyObjectScalarObject *self, Py_ssize_t segment, void **ptrptr) { PyBufferProcs *pb = self->obval->ob_type->tp_as_buffer; @@ -2116,8 +2116,8 @@ return (*pb->bf_getreadbuffer)(self->obval, segment, ptrptr); } -static _int_or_ssize_t -object_arrtype_getwritebuf(PyObjectScalarObject *self, _int_or_ssize_t segment, void **ptrptr) +static Py_ssize_t +object_arrtype_getwritebuf(PyObjectScalarObject *self, Py_ssize_t segment, void **ptrptr) { PyBufferProcs *pb = self->obval->ob_type->tp_as_buffer; @@ -2132,8 +2132,8 @@ return (*pb->bf_getwritebuffer)(self->obval, segment, ptrptr); } -static _int_or_ssize_t -object_arrtype_getcharbuf(PyObjectScalarObject *self, _int_or_ssize_t segment, +static Py_ssize_t +object_arrtype_getcharbuf(PyObjectScalarObject *self, Py_ssize_t segment, const char **ptrptr) { PyBufferProcs *pb = self->obval->ob_type->tp_as_buffer; From numpy-svn at scipy.org Sun Jul 2 01:30:20 2006 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Sun, 2 Jul 2006 00:30:20 -0500 (CDT) Subject: [Numpy-svn] r2722 - in trunk/numpy/core: . tests Message-ID: <20060702053020.5F9D8D4866A@new.scipy.org> Author: oliphant Date: 2006-07-02 00:30:14 -0500 (Sun, 02 Jul 2006) New Revision: 2722 Modified: trunk/numpy/core/_internal.py trunk/numpy/core/ma.py trunk/numpy/core/tests/test_defmatrix.py Log: Fix up failing tests. Modified: trunk/numpy/core/_internal.py =================================================================== --- trunk/numpy/core/_internal.py 2006-07-02 02:59:54 UTC (rev 2721) +++ trunk/numpy/core/_internal.py 2006-07-02 05:30:14 UTC (rev 2722) @@ -199,12 +199,12 @@ def get_data(self): return self._ctypes.c_void_p(self._arr.__array_interface__['data'][0]) - def get_dims(self): + def get_shape(self): return (_getintp_ctype()*self._arr.ndim)(*self._arr.shape) def get_strides(self): return (_getintp_ctype()*self._arr.ndim)(*self._arr.strides) data = property(get_data, None, doc="c-types data") - dims = property(get_dims, None, doc="c-types dims") + shape = property(get_shape, None, doc="c-types dims") strides = property(get_strides, None, doc="c-types strides") Modified: trunk/numpy/core/ma.py =================================================================== --- trunk/numpy/core/ma.py 2006-07-02 02:59:54 UTC (rev 2721) +++ trunk/numpy/core/ma.py 2006-07-02 05:30:14 UTC (rev 2722) @@ -1388,6 +1388,9 @@ self._mask = make_mask (self._mask, copy=1, flag=0) self._shared_mask = 0 + def _get_ctypes(self): + return self._data.ctypes + shape = property(_get_shape, _set_shape, doc = 'tuple giving the shape of the array') @@ -1402,6 +1405,8 @@ imag = imaginary + ctypes = property(_get_ctypes, None, doc="ctypes") + #end class MaskedArray array = MaskedArray Modified: trunk/numpy/core/tests/test_defmatrix.py =================================================================== --- trunk/numpy/core/tests/test_defmatrix.py 2006-07-02 02:59:54 UTC (rev 2721) +++ trunk/numpy/core/tests/test_defmatrix.py 2006-07-02 05:30:14 UTC (rev 2722) @@ -149,7 +149,7 @@ 'searchsorted', 'setflags', 'setfield', 'sort', 'take', 'tofile', 'tolist', 'tostring', 'all', 'any', 'sum', 'argmax', 'argmin', 'min', 'max', 'mean', 'var', 'ptp', - 'prod', 'std' + 'prod', 'std', 'ctypes' ] for attrib in dir(a): if attrib.startswith('_') or attrib in excluded_methods: From numpy-svn at scipy.org Sun Jul 2 03:58:56 2006 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Sun, 2 Jul 2006 02:58:56 -0500 (CDT) Subject: [Numpy-svn] r2723 - in trunk/numpy/core: . src Message-ID: <20060702075856.979BAD48647@new.scipy.org> Author: oliphant Date: 2006-07-02 02:58:48 -0500 (Sun, 02 Jul 2006) New Revision: 2723 Modified: trunk/numpy/core/_internal.py trunk/numpy/core/src/arrayobject.c Log: Add _as_parameter_ method so arrays can be used directly as ctypes Modified: trunk/numpy/core/_internal.py =================================================================== --- trunk/numpy/core/_internal.py 2006-07-02 05:30:14 UTC (rev 2722) +++ trunk/numpy/core/_internal.py 2006-07-02 07:58:48 UTC (rev 2723) @@ -206,5 +206,5 @@ return (_getintp_ctype()*self._arr.ndim)(*self._arr.strides) data = property(get_data, None, doc="c-types data") - shape = property(get_shape, None, doc="c-types dims") + shape = property(get_shape, None, doc="c-types shape") strides = property(get_strides, None, doc="c-types strides") Modified: trunk/numpy/core/src/arrayobject.c =================================================================== --- trunk/numpy/core/src/arrayobject.c 2006-07-02 05:30:14 UTC (rev 2722) +++ trunk/numpy/core/src/arrayobject.c 2006-07-02 07:58:48 UTC (rev 2723) @@ -5553,6 +5553,12 @@ } static PyObject * +array_as_parameter_get(PyArrayObject *self) +{ + return PyLong_FromVoidPtr(self->data); +} + +static PyObject * array_interface_get(PyArrayObject *self) { PyObject *dict; @@ -6113,6 +6119,10 @@ (getter)array_ctypes_get, NULL, "Ctypes interface object"}, + {"_as_parameter_", + (getter)array_as_parameter_get, + NULL, + "interpret array directly"}, {"__array_interface__", (getter)array_interface_get, NULL, From numpy-svn at scipy.org Sun Jul 2 04:18:11 2006 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Sun, 2 Jul 2006 03:18:11 -0500 (CDT) Subject: [Numpy-svn] r2724 - trunk/numpy/core/src Message-ID: <20060702081811.177A6D4866F@new.scipy.org> Author: oliphant Date: 2006-07-02 03:18:08 -0500 (Sun, 02 Jul 2006) New Revision: 2724 Modified: trunk/numpy/core/src/arrayobject.c Log: Improve docstrings a little. Modified: trunk/numpy/core/src/arrayobject.c =================================================================== --- trunk/numpy/core/src/arrayobject.c 2006-07-02 07:58:48 UTC (rev 2723) +++ trunk/numpy/core/src/arrayobject.c 2006-07-02 08:18:08 UTC (rev 2724) @@ -6118,11 +6118,11 @@ {"ctypes", (getter)array_ctypes_get, NULL, - "Ctypes interface object"}, + "ctypes interface object"}, {"_as_parameter_", (getter)array_as_parameter_get, NULL, - "interpret array directly"}, + "allow array to be interpreted as a ctypes object"}, {"__array_interface__", (getter)array_interface_get, NULL, From numpy-svn at scipy.org Sun Jul 2 13:19:31 2006 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Sun, 2 Jul 2006 12:19:31 -0500 (CDT) Subject: [Numpy-svn] r2725 - trunk/numpy/f2py/lib Message-ID: <20060702171931.B2C28D40B37@new.scipy.org> Author: pearu Date: 2006-07-02 12:19:26 -0500 (Sun, 02 Jul 2006) New Revision: 2725 Modified: trunk/numpy/f2py/lib/block_statements.py trunk/numpy/f2py/lib/statements.py trunk/numpy/f2py/lib/test_parser.py Log: Added more Fortran parser tests. Modified: trunk/numpy/f2py/lib/block_statements.py =================================================================== --- trunk/numpy/f2py/lib/block_statements.py 2006-07-02 08:18:08 UTC (rev 2724) +++ trunk/numpy/f2py/lib/block_statements.py 2006-07-02 17:19:26 UTC (rev 2725) @@ -637,7 +637,7 @@ from statements import * from typedecl_statements import * -f2py_stmt = [ThreadSafe, FortranName, Depend, Check, CallStatement, +f2py_stmt = [Threadsafe, FortranName, Depend, Check, CallStatement, CallProtoArgument] access_spec = [Public, Private] Modified: trunk/numpy/f2py/lib/statements.py =================================================================== --- trunk/numpy/f2py/lib/statements.py 2006-07-02 08:18:08 UTC (rev 2724) +++ trunk/numpy/f2py/lib/statements.py 2006-07-02 17:19:26 UTC (rev 2725) @@ -1507,7 +1507,7 @@ def process_item(self): line = self.item.get_line()[5:].lstrip() i = line.index(')') - self.expr = line[1:i].strip() + self.expr = self.item.apply_map(line[1:i].strip()) line = line[i+1:].lstrip() newitem = self.item.copy(line) cls = Assignment @@ -1518,9 +1518,10 @@ return self.isvalid = False return + def __str__(self): tab = self.get_indent_tab() - return tab + 'WHERE (%s) %s' % (self.expr, self.content[0]) + return tab + 'WHERE ( %s ) %s' % (self.expr, str(self.content[0]).lstrip()) WhereStmt = Where @@ -1536,14 +1537,15 @@ if line.startswith('('): i = line.index(')') assert i != -1,`line` - self.expr = line[1:i].strip() + self.expr = self.item.apply_map(line[1:i].strip()) line = line[i+1:].lstrip() self.name = line - if self.name and not self.name==self.parent.name: + parent_name = getattr(self.parent,'name','') + if self.name and not self.name==parent_name: message = self.reader.format_message(\ 'WARNING', 'expected where-construct-name %r but got %r, skipping.'\ - % (self.parent.name, self.name), + % (parent_name, self.name), self.item.span[0],self.item.span[1]) print >> sys.stderr, message self.isvalid = False @@ -1551,9 +1553,12 @@ def __str__(self): tab = self.get_indent_tab() - if self.expr is None: - return tab + 'ELSE WHERE ' + self.name - return tab + 'ELSE WHERE (%s) ' % (self.expr) + self.name + s = 'ELSE WHERE' + if self.expr is not None: + s += ' ( %s )' % (self.expr) + if self.name: + s += ' ' + self.name + return tab + s # Enum construct statements @@ -1567,10 +1572,10 @@ line = self.item.get_line()[10:].lstrip() if line.startswith('::'): line = line[2:].lstrip() - self.rest = line + self.items = split_comma(line, self.item) return def __str__(self): - return self.get_indent_tab() + 'ENUMERATOR ' + self.rest + return self.get_indent_tab() + 'ENUMERATOR ' + ', '.join(self.items) # F2PY specific statements @@ -1585,7 +1590,7 @@ def __str__(self): return self.get_indent_tab() + 'FORTRANNAME ' + self.value -class ThreadSafe(Statement): +class Threadsafe(Statement): """ THREADSAFE """ @@ -1604,34 +1609,36 @@ def process_item(self): line = self.item.get_line()[6:].lstrip() i = line.find(')') - self.depends = [s.strip() for s in line[1:i].strip().split(',')] + self.depends = split_comma(line[1:i].strip(), self.item) line = line[i+1:].lstrip() if line.startswith('::'): line = line[2:].lstrip() - self.items = [s.strip() for s in line.split(',')] + self.items = split_comma(line) return + def __str__(self): - return self.get_indent_tab() + 'DEPEND (%s) %s' \ + return self.get_indent_tab() + 'DEPEND ( %s ) %s' \ % (', '.join(self.depends), ', '.join(self.items)) class Check(Statement): """ - CHECK ( ) [ :: ] + CHECK ( ) [ :: ] """ match = re.compile(r'check\s*\(',re.I).match def process_item(self): line = self.item.get_line()[5:].lstrip() i = line.find(')') - self.expr = line[1:i].strip() + assert i!=-1,`line` + self.expr = self.item.apply_map(line[1:i].strip()) line = line[i+1:].lstrip() if line.startswith('::'): line = line[2:].lstrip() - self.items = [s.strip() for s in line.split(',')] + self.value = line return def __str__(self): - return self.get_indent_tab() + 'CHECK (%s) %s' \ - % (self.expr, ', '.join(self.items)) + return self.get_indent_tab() + 'CHECK ( %s ) %s' \ + % (self.expr, self.value) class CallStatement(Statement): """ @@ -1639,7 +1646,7 @@ """ match = re.compile(r'callstatement\b', re.I).match def process_item(self): - self.expr = self.item.get_line()[13:].lstrip() + self.expr = self.item.apply_map(self.item.get_line()[13:].lstrip()) return def __str__(self): return self.get_indent_tab() + 'CALLSTATEMENT ' + self.expr @@ -1650,7 +1657,7 @@ """ match = re.compile(r'callprotoargument\b', re.I).match def process_item(self): - self.specs = self.item.get_line()[17:].lstrip() + self.specs = self.item.apply_map(self.item.get_line()[17:].lstrip()) return def __str__(self): return self.get_indent_tab() + 'CALLPROTOARGUMENT ' + self.specs @@ -1661,10 +1668,11 @@ """ PAUSE [ ] """ - match = re.compile(r'pause\b\s*(\d+|\'\w*\')\Z', re.I).match + match = re.compile(r'pause\s*(\d+|\'\w*\'|"\w*"|)\Z', re.I).match def process_item(self): - self.value = self.item.get_line()[5:].lstrip() + self.value = self.item.apply_map(self.item.get_line()[5:].lstrip()) return def __str__(self): - return self.get_indent_tab() + 'PAUSE ' + self.value - + if self.value: + return self.get_indent_tab() + 'PAUSE ' + self.value + return self.get_indent_tab() + 'PAUSE' Modified: trunk/numpy/f2py/lib/test_parser.py =================================================================== --- trunk/numpy/f2py/lib/test_parser.py 2006-07-02 08:18:08 UTC (rev 2724) +++ trunk/numpy/f2py/lib/test_parser.py 2006-07-02 17:19:26 UTC (rev 2725) @@ -4,10 +4,11 @@ from readfortran import Line, FortranStringReader -def parse(cls, line, label=''): +def parse(cls, line, label='', + isfree=True, isstrict=False): if label: line = label + ' : ' + line - reader = FortranStringReader(line, True, False) + reader = FortranStringReader(line, isfree, isstrict) item = reader.next() if not cls.match(item.get_line()): raise ValueError, '%r does not match %s pattern' % (line, cls.__name__) @@ -371,6 +372,56 @@ assert_equal(parse(Case,'case default'),'CASE DEFAULT') assert_equal(parse(Case,'case (1:2 ,3:4)'),'CASE ( 1 : 2, 3 : 4 )') assert_equal(parse(Case,'case (a(1,:):)'),'CASE ( a(1,:) : )') - + assert_equal(parse(Case,'case default'),'CASE DEFAULT') + + def check_where(self): + assert_equal(parse(WhereStmt,'where (1) a=1'),'WHERE ( 1 ) a = 1') + assert_equal(parse(WhereStmt,'where (a(1,2)) a=1'),'WHERE ( a(1,2) ) a = 1') + + def check_elsewhere(self): + assert_equal(parse(ElseWhere,'else where'),'ELSE WHERE') + assert_equal(parse(ElseWhere,'elsewhere (1)'),'ELSE WHERE ( 1 )') + assert_equal(parse(ElseWhere,'elsewhere(a(1,2))'),'ELSE WHERE ( a(1,2) )') + + def check_enumerator(self): + assert_equal(parse(Enumerator,'enumerator a'), 'ENUMERATOR a') + assert_equal(parse(Enumerator,'enumerator:: a'), 'ENUMERATOR a') + assert_equal(parse(Enumerator,'enumerator a,b'), 'ENUMERATOR a, b') + assert_equal(parse(Enumerator,'enumerator a=1'), 'ENUMERATOR a=1') + assert_equal(parse(Enumerator,'enumerator a=1 , b=c(1,2)'), 'ENUMERATOR a=1, b=c(1,2)') + + def check_fortranname(self): + assert_equal(parse(FortranName,'fortranname a'),'FORTRANNAME a') + + def check_threadsafe(self): + assert_equal(parse(Threadsafe,'threadsafe'),'THREADSAFE') + + def check_depend(self): + assert_equal(parse(Depend,'depend( a) b'), 'DEPEND ( a ) b') + assert_equal(parse(Depend,'depend( a) ::b'), 'DEPEND ( a ) b') + assert_equal(parse(Depend,'depend( a,c) b,e'), 'DEPEND ( a, c ) b, e') + + def check_check(self): + assert_equal(parse(Check,'check(1) a'), 'CHECK ( 1 ) a') + assert_equal(parse(Check,'check(1) :: a'), 'CHECK ( 1 ) a') + assert_equal(parse(Check,'check(b(1,2)) a'), 'CHECK ( b(1,2) ) a') + assert_equal(parse(Check,'check(a>1) :: a'), 'CHECK ( a>1 ) a') + + def check_callstatement(self): + assert_equal(parse(CallStatement,'callstatement (*func)()',isstrict=1), + 'CALLSTATEMENT (*func)()') + assert_equal(parse(CallStatement,'callstatement i=1;(*func)()',isstrict=1), + 'CALLSTATEMENT i=1;(*func)()') + + def check_callprotoargument(self): + assert_equal(parse(CallProtoArgument,'callprotoargument int(*), double'), + 'CALLPROTOARGUMENT int(*), double') + + def check_pause(self): + assert_equal(parse(Pause,'pause'),'PAUSE') + assert_equal(parse(Pause,'pause 1'),'PAUSE 1') + assert_equal(parse(Pause,'pause "hey"'),'PAUSE "hey"') + assert_equal(parse(Pause,'pause "hey pa"'),'PAUSE "hey pa"') + if __name__ == "__main__": NumpyTest().run() From numpy-svn at scipy.org Sun Jul 2 17:44:27 2006 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Sun, 2 Jul 2006 16:44:27 -0500 (CDT) Subject: [Numpy-svn] r2726 - trunk/numpy/core/src Message-ID: <20060702214427.B71CCD40B3F@new.scipy.org> Author: oliphant Date: 2006-07-02 16:44:24 -0500 (Sun, 02 Jul 2006) New Revision: 2726 Modified: trunk/numpy/core/src/arrayobject.c Log: Fixed missing case in casting call. Modified: trunk/numpy/core/src/arrayobject.c =================================================================== --- trunk/numpy/core/src/arrayobject.c 2006-07-02 17:19:26 UTC (rev 2725) +++ trunk/numpy/core/src/arrayobject.c 2006-07-02 21:44:24 UTC (rev 2726) @@ -897,7 +897,13 @@ multi = (PyArrayMultiIterObject *)PyArray_MultiIterNew(2, dest, src); if (multi == NULL) return -1; maxaxis = PyArray_RemoveLargest(multi); - if (maxaxis < 0) return -1; + if (maxaxis < 0) { /* copy 1 0-d array to another */ + PyArray_XDECREF(dest); + memcpy(dest->data, src->data, elsize); + if (swap) byte_swap_vector(dest->data, 1, elsize); + PyArray_INCREF(dest); + return 0; + } maxdim = multi->dimensions[maxaxis]; PyArray_XDECREF(dest); @@ -6866,7 +6872,7 @@ { int delsize, selsize, maxaxis, i, N; PyArrayMultiIterObject *multi; - intp maxdim; + intp maxdim, ostrides, istrides; char *buffers[2]; PyArray_CopySwapNFunc *ocopyfunc, *icopyfunc; char *obptr; @@ -6875,10 +6881,22 @@ selsize = PyArray_ITEMSIZE(in); multi = (PyArrayMultiIterObject *)PyArray_MultiIterNew(2, out, in); if (multi == NULL) return -1; + icopyfunc = in->descr->f->copyswapn; + ocopyfunc = out->descr->f->copyswapn; maxaxis = PyArray_RemoveLargest(multi); - if (maxaxis < 0) return -1; - maxdim = multi->dimensions[maxaxis]; - N = (int) (MIN(maxdim, PyArray_BUFSIZE)); + if (maxaxis < 0) { /* cast 1 0-d array to another */ + N = 1; + maxdim = 1; + ostrides = delsize; + istrides = selsize; + } + else { + maxdim = multi->dimensions[maxaxis]; + N = (int) (MIN(maxdim, PyArray_BUFSIZE)); + ostrides = multi->iters[0]->strides[maxaxis]; + istrides = multi->iters[1]->strides[maxaxis]; + + } buffers[0] = _pya_malloc(N*delsize); if (buffers[0] == NULL) { PyErr_NoMemory(); @@ -6886,7 +6904,7 @@ } buffers[1] = _pya_malloc(N*selsize); if (buffers[1] == NULL) { - free(buffers[0]); + _pya_free(buffers[0]); PyErr_NoMemory(); return -1; } @@ -6895,15 +6913,12 @@ if (in->descr->hasobject) memset(buffers[1], 0, N*selsize); - icopyfunc = in->descr->f->copyswapn; - ocopyfunc = out->descr->f->copyswapn; - while(multi->index < multi->size) { _strided_buffered_cast(multi->iters[0]->dataptr, - multi->iters[0]->strides[maxaxis], + ostrides, delsize, oswap, ocopyfunc, multi->iters[1]->dataptr, - multi->iters[1]->strides[maxaxis], + istrides, selsize, iswap, icopyfunc, maxdim, buffers, N, castfunc, out, in); From numpy-svn at scipy.org Mon Jul 3 05:41:52 2006 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Mon, 3 Jul 2006 04:41:52 -0500 (CDT) Subject: [Numpy-svn] r2727 - in trunk/numpy/core: . src Message-ID: <20060703094152.0D521D40B4B@new.scipy.org> Author: oliphant Date: 2006-07-03 04:41:49 -0500 (Mon, 03 Jul 2006) New Revision: 2727 Modified: trunk/numpy/core/_internal.py trunk/numpy/core/src/arrayobject.c trunk/numpy/core/src/multiarraymodule.c Log: Convert docstrings to reflect change to floating-point defaults. Modified: trunk/numpy/core/_internal.py =================================================================== --- trunk/numpy/core/_internal.py 2006-07-02 21:44:24 UTC (rev 2726) +++ trunk/numpy/core/_internal.py 2006-07-03 09:41:49 UTC (rev 2727) @@ -195,9 +195,18 @@ except ImportError: raise AttributeError, "ctypes not available" self._arr = array - + + def data_as(self, obj): + return self._ctypes.cast(self._arr._as_parameter_, obj) + + def shape_as(self, obj): + return (obj*self._arr.ndim)(*self._arr.shape) + + def strides_as(self, obj): + return (obj*self._arr.ndim)(*self._arr.strides) + def get_data(self): - return self._ctypes.c_void_p(self._arr.__array_interface__['data'][0]) + return self._ctypes.c_void_p(self._arr._as_parameter_) def get_shape(self): return (_getintp_ctype()*self._arr.ndim)(*self._arr.shape) Modified: trunk/numpy/core/src/arrayobject.c =================================================================== --- trunk/numpy/core/src/arrayobject.c 2006-07-02 21:44:24 UTC (rev 2726) +++ trunk/numpy/core/src/arrayobject.c 2006-07-03 09:41:49 UTC (rev 2727) @@ -6170,7 +6170,7 @@ " command. Arrays are sequence, mapping and numeric objects.\n" " More information is available in the numpy module and by looking\n" " at the methods and attributes of an array.\n\n" - " ndarray.__new__(subtype, shape=, dtype=int, buffer=None, \n" + " ndarray.__new__(subtype, shape=, dtype=float, buffer=None, \n" " offset=0, strides=None, order=None)\n\n" " There are two modes of creating an array using __new__:\n" " 1) If buffer is None, then only shape, dtype, and order \n" Modified: trunk/numpy/core/src/multiarraymodule.c =================================================================== --- trunk/numpy/core/src/multiarraymodule.c 2006-07-02 21:44:24 UTC (rev 2726) +++ trunk/numpy/core/src/multiarraymodule.c 2006-07-03 09:41:49 UTC (rev 2727) @@ -4684,7 +4684,7 @@ } -static char doc_empty[] = "empty((d1,...,dn),dtype=int,order='C') will return a new array\n of shape (d1,...,dn) and given type with all its entries uninitialized. This can be faster than zeros."; +static char doc_empty[] = "empty((d1,...,dn),dtype=float,order='C') will return a new array\n of shape (d1,...,dn) and given type with all its entries uninitialized. This can be faster than zeros."; static PyObject * array_empty(PyObject *ignored, PyObject *args, PyObject *kwds) @@ -4814,7 +4814,7 @@ } -static char doc_zeros[] = "zeros((d1,...,dn),dtype=int,order='C') will return a new array of shape (d1,...,dn) and type typecode with all it's entries initialized to zero."; +static char doc_zeros[] = "zeros((d1,...,dn),dtype=float,order='C') will return a new array of shape (d1,...,dn) and type typecode with all it's entries initialized to zero."; static PyObject * @@ -5024,7 +5024,7 @@ return (PyObject *)ret; } -static char doc_fromString[] = "fromstring(string, dtype=int, count=-1, sep='') returns a new 1d array initialized from the raw binary data in string. If count is positive, the new array will have count elements, otherwise it's 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)."; +static char doc_fromString[] = "fromstring(string, dtype=float, count=-1, sep='') returns a new 1d array initialized from the raw binary data in string. If count is positive, the new array will have count elements, otherwise it's 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)."; static PyObject * array_fromString(PyObject *ignored, PyObject *args, PyObject *keywds) @@ -5294,7 +5294,7 @@ } static char doc_fromfile[] = \ - "fromfile(file=, dtype=int, count=-1, sep='')\n" \ + "fromfile(file=, dtype=float, count=-1, sep='')\n" \ "\n"\ " Return an array of the given data type from a \n"\ " (text or binary) file. The file argument can be an open file\n"\ @@ -5447,7 +5447,7 @@ } static char doc_frombuffer[] = \ - "frombuffer(buffer=, dtype=int, count=-1, offset=0)\n"\ + "frombuffer(buffer=, dtype=float, count=-1, offset=0)\n"\ "\n" \ " Returns a 1-d array of data type dtype from buffer. The buffer\n"\ " argument must be an object that exposes the buffer interface.\n"\ From numpy-svn at scipy.org Mon Jul 3 16:17:40 2006 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Mon, 3 Jul 2006 15:17:40 -0500 (CDT) Subject: [Numpy-svn] r2728 - in trunk/numpy: core lib Message-ID: <20060703201740.A359CD40B6A@new.scipy.org> Author: oliphant Date: 2006-07-03 15:17:32 -0500 (Mon, 03 Jul 2006) New Revision: 2728 Modified: trunk/numpy/core/numeric.py trunk/numpy/lib/utils.py Log: Add a require function, a issubdtype, and a load_ctypes_function to NumPy Modified: trunk/numpy/core/numeric.py =================================================================== --- trunk/numpy/core/numeric.py 2006-07-03 09:41:49 UTC (rev 2727) +++ trunk/numpy/core/numeric.py 2006-07-03 20:17:32 UTC (rev 2728) @@ -10,7 +10,7 @@ 'alterdot', 'restoredot', 'cross', 'array2string', 'get_printoptions', 'set_printoptions', 'array_repr', 'array_str', 'set_string_function', - 'little_endian', + 'little_endian', 'require', 'fromiter', 'indices', 'fromfunction', 'load', 'loads', 'isscalar', 'binary_repr', 'base_repr', @@ -133,6 +133,39 @@ """ return array(a, dtype, copy=False, order='F', ndmin=1) +def require(a, dtype=None, requirements=None): + if requirements is None: + requirements = [] + else: + requirements = [x.upper() for x in requirements] + + if not requirements: + return asanyarray(a, dtype=dtype) + + if 'ENSUREARRAY' in requirements or 'E' in requirements: + func = asarray + else: + func = asanyarray + + if 'FORCECAST' in requirements or 'FORCE' in requirements: + arr = func(a) + if dtype is not None and arr.dtype != dtype: + arr = arr.astype(dtype) + else: + arr = func(a, dtype=dtype) + + copychar = 'A' + if 'FORTRAN' in requirements or 'F' in requirements: + copychar = 'F' + elif 'CONTIGUOUS' in requirements or 'C' in requirements: + copychar = 'C' + + for prop in requirements: + if not arr.flags[prop]: + arr = arr.copy(copychar) + break + return arr + def isfortran(a): """Returns True if 'a' is laid out in Fortran-order in memory. """ Modified: trunk/numpy/lib/utils.py =================================================================== --- trunk/numpy/lib/utils.py 2006-07-03 09:41:49 UTC (rev 2727) +++ trunk/numpy/lib/utils.py 2006-07-03 20:17:32 UTC (rev 2728) @@ -1,8 +1,10 @@ -import sys +import sys, os from numpy.core.numerictypes import obj2sctype +from numpy.core.multiarray import dtype -__all__ = ['issubclass_', 'get_numpy_include', 'issubsctype', 'deprecate', - 'get_include'] +__all__ = ['issubclass_', 'get_numpy_include', 'issubsctype', + 'issubdtype', 'deprecate', + 'get_include', 'load_ctypes_library'] def issubclass_(arg1, arg2): try: @@ -13,6 +15,9 @@ def issubsctype(arg1, arg2): return issubclass(obj2sctype(arg1), obj2sctype(arg2)) +def issubdtype(arg1, arg2): + return issubclass(dtype(arg1).type, dtype(arg2).type) + def get_include(): """Return the directory in the package that contains the numpy/*.h header files. @@ -29,6 +34,25 @@ assert len(include_dirs)==1,`include_dirs` return include_dirs[0] +# Adapted from Albert Strasheim +def load_ctypes_library(libname, loader_path): + if '.' not in libname: + if sys.platform == 'win32': + libname = '%s.dll' % libname + elif sys.platform == 'darwin': + libname = '%s.dylib' % libname + else: + libname = '%s.so' % libname + loader_path = os.path.abspath(loader_path) + if not os.path.isdir(loader_path): + libdir = os.path.dirname(loader_path) + else: + libdir = loader_path + import ctypes + libpath = os.path.join(libdir, libname) + return ctypes.cdll[libpath] + + if sys.version_info < (2, 4): # Can't set __name__ in 2.3 import new @@ -65,3 +89,5 @@ get_numpy_include = deprecate(get_include, 'get_numpy_include', 'get_include') + + From numpy-svn at scipy.org Mon Jul 3 16:18:59 2006 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Mon, 3 Jul 2006 15:18:59 -0500 (CDT) Subject: [Numpy-svn] r2729 - trunk/numpy/lib Message-ID: <20060703201859.ED0CFD40B6A@new.scipy.org> Author: oliphant Date: 2006-07-03 15:18:57 -0500 (Mon, 03 Jul 2006) New Revision: 2729 Modified: trunk/numpy/lib/utils.py Log: Change the name to ctypes_load_library Modified: trunk/numpy/lib/utils.py =================================================================== --- trunk/numpy/lib/utils.py 2006-07-03 20:17:32 UTC (rev 2728) +++ trunk/numpy/lib/utils.py 2006-07-03 20:18:57 UTC (rev 2729) @@ -4,7 +4,7 @@ __all__ = ['issubclass_', 'get_numpy_include', 'issubsctype', 'issubdtype', 'deprecate', - 'get_include', 'load_ctypes_library'] + 'get_include', 'ctypes_load_library'] def issubclass_(arg1, arg2): try: @@ -35,7 +35,7 @@ return include_dirs[0] # Adapted from Albert Strasheim -def load_ctypes_library(libname, loader_path): +def ctypes_load_library(libname, loader_path): if '.' not in libname: if sys.platform == 'win32': libname = '%s.dll' % libname From numpy-svn at scipy.org Mon Jul 3 17:10:55 2006 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Mon, 3 Jul 2006 16:10:55 -0500 (CDT) Subject: [Numpy-svn] r2730 - trunk/numpy/core Message-ID: <20060703211055.4DF21D40B51@new.scipy.org> Author: oliphant Date: 2006-07-03 16:10:52 -0500 (Mon, 03 Jul 2006) New Revision: 2730 Modified: trunk/numpy/core/_internal.py Log: Fix .ctypes.strides and .ctypes.shape to return None (interpreted as NULL by ctypes) if the array is 0-d. Modified: trunk/numpy/core/_internal.py =================================================================== --- trunk/numpy/core/_internal.py 2006-07-03 20:18:57 UTC (rev 2729) +++ trunk/numpy/core/_internal.py 2006-07-03 21:10:52 UTC (rev 2730) @@ -195,23 +195,35 @@ except ImportError: raise AttributeError, "ctypes not available" self._arr = array + if self._arr.ndim == 0: + self._zerod = True + else: + self._zerod = False def data_as(self, obj): return self._ctypes.cast(self._arr._as_parameter_, obj) def shape_as(self, obj): + if self._zerod: + return None return (obj*self._arr.ndim)(*self._arr.shape) def strides_as(self, obj): + if self._zerod: + return None return (obj*self._arr.ndim)(*self._arr.strides) def get_data(self): return self._ctypes.c_void_p(self._arr._as_parameter_) def get_shape(self): + if self._zerod: + return None return (_getintp_ctype()*self._arr.ndim)(*self._arr.shape) def get_strides(self): + if self._zerod: + return None return (_getintp_ctype()*self._arr.ndim)(*self._arr.strides) data = property(get_data, None, doc="c-types data") From numpy-svn at scipy.org Mon Jul 3 18:32:36 2006 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Mon, 3 Jul 2006 17:32:36 -0500 (CDT) Subject: [Numpy-svn] r2731 - in trunk/numpy/core: . src Message-ID: <20060703223236.9741BD40898@new.scipy.org> Author: oliphant Date: 2006-07-03 17:32:32 -0500 (Mon, 03 Jul 2006) New Revision: 2731 Modified: trunk/numpy/core/numeric.py trunk/numpy/core/src/ufuncobject.c Log: Check for error and asthetic spacing change. Modified: trunk/numpy/core/numeric.py =================================================================== --- trunk/numpy/core/numeric.py 2006-07-03 21:10:52 UTC (rev 2730) +++ trunk/numpy/core/numeric.py 2006-07-03 22:32:32 UTC (rev 2731) @@ -537,9 +537,11 @@ """Set the size of the buffer used in ufuncs. """ if size > 10e6: - raise ValueError, "Buffer size too big... %s" % size + raise ValueError, "Buffer size, %s, is too big." % size if size < 5: - raise ValueError, "Buffer size too small... %s" %size + raise ValueError, "Buffer size, %s, is too small." %size + if size % 16 != 0: + raise ValueError, "Buffer size, %s, is not a multiple of 16." %size pyvals = umath.geterrobj() old = getbufsize() Modified: trunk/numpy/core/src/ufuncobject.c =================================================================== --- trunk/numpy/core/src/ufuncobject.c 2006-07-03 21:10:52 UTC (rev 2730) +++ trunk/numpy/core/src/ufuncobject.c 2006-07-03 22:32:32 UTC (rev 2731) @@ -2490,7 +2490,8 @@ } /* Check to see that type (and otype) is not FLEXIBLE */ - if (PyArray_ISFLEXIBLE(mp) || (otype && PyTypeNum_ISFLEXIBLE(otype->type_num))) { + if (PyArray_ISFLEXIBLE(mp) || + (otype && PyTypeNum_ISFLEXIBLE(otype->type_num))) { PyErr_Format(PyExc_TypeError, "cannot perform %s with flexible type", _reduce_type[operation]); From numpy-svn at scipy.org Tue Jul 4 16:43:14 2006 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Tue, 4 Jul 2006 15:43:14 -0500 (CDT) Subject: [Numpy-svn] r2733 - trunk/numpy/linalg Message-ID: <20060704204314.08AF9D408BE@new.scipy.org> Author: cookedm Date: 2006-07-04 15:43:11 -0500 (Tue, 04 Jul 2006) New Revision: 2733 Modified: trunk/numpy/linalg/linalg.py Log: Convert linalg to use dtypes instead of typecodes Modified: trunk/numpy/linalg/linalg.py =================================================================== --- trunk/numpy/linalg/linalg.py 2006-07-04 12:12:47 UTC (rev 2732) +++ trunk/numpy/linalg/linalg.py 2006-07-04 20:43:11 UTC (rev 2733) @@ -19,31 +19,38 @@ from numpy.lib import * import lapack_lite +fortran_int = int32 + # Error object class LinAlgError(Exception): pass -# Helper routines -_array_kind = {'i':0, 'l': 0, 'q': 0, 'f': 0, 'd': 0, 'F': 1, 'D': 1} -_array_precision = {'i': 1, 'l': 1, 'q': 1, 'f': 0, 'd': 1, 'F': 0, 'D': 1} -_array_type = [['f', 'd'], ['F', 'D']] - def _makearray(a): new = asarray(a) wrap = getattr(a, "__array_wrap__", new.__array_wrap__) return new, wrap def _commonType(*arrays): - kind = 0 -# precision = 0 -# force higher precision in lite version - precision = 1 + # in lite version, use higher precision (always double or cdouble) + maxtype = (0, double) for a in arrays: - t = a.dtype.char - kind = max(kind, _array_kind[t]) - precision = max(precision, _array_precision[t]) - return _array_type[kind][precision] + if issubclass(a.dtype.type, inexact): + if a.dtype.type in (single, double): + t = (0, double) + elif a.dtype.type in (csingle, cdouble): + t = (1, cdouble) + else: + # unsupported inexact scalar + raise TypeError("array type %s is unsupported in linalg" % + (a.dtype.name,)) + else: + t = (0, double) + maxtype = max(maxtype, t) + return maxtype[1] +def _realType(t): + return {double : double, cdouble : double} + def _castCopyAndTranspose(type, *arrays): if len(arrays) == 1: return transpose(arrays[0]).astype(type) @@ -93,12 +100,12 @@ raise LinAlgError, 'Incompatible dimensions' t =_commonType(a, b) # lapack_routine = _findLapackRoutine('gesv', t) - if _array_kind[t] == 1: # Complex routines take different arguments + if issubclass(t, complexfloating): lapack_routine = lapack_lite.zgesv else: lapack_routine = lapack_lite.dgesv a, b = _fastCopyAndTranspose(t, a, b) - pivots = zeros(n_eq, 'i') + pivots = zeros(n_eq, fortran_int) results = lapack_routine(n_eq, n_rhs, a, n_eq, pivots, b, n_eq, 0) if results['info'] > 0: raise LinAlgError, 'Singular matrix' @@ -123,7 +130,7 @@ a = _castCopyAndTranspose(t, a) m = a.shape[0] n = a.shape[1] - if _array_kind[t] == 1: + if issubclass(t, complexfloating): lapack_routine = lapack_lite.zpotrf else: lapack_routine = lapack_lite.dpotrf @@ -138,11 +145,11 @@ _assertRank2(a) _assertSquareness(a) t =_commonType(a) - real_t = _array_type[0][_array_precision[t]] + real_t = _realType(t) a = _fastCopyAndTranspose(t, a) n = a.shape[0] dummy = zeros((1,), t) - if _array_kind[t] == 1: # Complex routines take different arguments + if issubclass(t, complexfloating): lapack_routine = lapack_lite.zgeev w = zeros((n,), t) rwork = zeros((n,),real_t) @@ -178,13 +185,13 @@ def eigvalsh(a, UPLO='L'): _assertRank2(a) _assertSquareness(a) - t =_commonType(a) - real_t = _array_type[0][_array_precision[t]] + t = _commonType(a) + real_t = _realType(t) a = _castCopyAndTranspose(t, a) n = a.shape[0] liwork = 5*n+3 - iwork = zeros((liwork,),'i') - if _array_kind[t] == 1: # Complex routines take different arguments + iwork = zeros((liwork,), fortran_int) + if issubclass(t, complexfloating): lapack_routine = lapack_lite.zheevd w = zeros((n,), real_t) lwork = 1 @@ -288,13 +295,13 @@ a, wrap = _makearray(a) _assertRank2(a) _assertSquareness(a) - t =_commonType(a) - real_t = _array_type[0][_array_precision[t]] + t = _commonType(a) + real_t = _realType(t) a = _castCopyAndTranspose(t, a) n = a.shape[0] liwork = 5*n+3 - iwork = zeros((liwork,),'i') - if _array_kind[t] == 1: # Complex routines take different arguments + iwork = zeros((liwork,), fortran_int) + if issubclass(t, complexfloating): lapack_routine = lapack_lite.zheevd w = zeros((n,), real_t) lwork = 1 @@ -343,8 +350,8 @@ a, wrap = _makearray(a) _assertRank2(a) m, n = a.shape - t =_commonType(a) - real_t = _array_type[0][_array_precision[t]] + t = _commonType(a) + real_t = _realType(t) a = _fastCopyAndTranspose(t, a) s = zeros((min(n,m),), real_t) if compute_uv: @@ -365,8 +372,8 @@ u = empty((1,1),t) vt = empty((1,1),t) - iwork = zeros((8*min(m,n),), 'i') - if _array_kind[t] == 1: # Complex routines take different arguments + iwork = zeros((8*min(m,n),), fortran_int) + if issubclass(t, complexfloating): lapack_routine = lapack_lite.zgesdd rwork = zeros((5*min(m,n)*min(m,n) + 5*min(m,n),), real_t) lwork = 1 @@ -438,11 +445,11 @@ t =_commonType(a) a = _fastCopyAndTranspose(t, a) n = a.shape[0] - if _array_kind[t] == 1: + if issubclass(t, complexfloating): lapack_routine = lapack_lite.zgetrf else: lapack_routine = lapack_lite.dgetrf - pivots = zeros((n,), 'i') + pivots = zeros((n,), fortran_int) results = lapack_routine(n, n, a, n, pivots, 0) sign = add.reduce(not_equal(pivots, arange(1, n+1))) % 2 return (1.-2.*sign)*multiply.reduce(diagonal(a),axis=-1) @@ -475,15 +482,15 @@ ldb = max(n,m) if m != b.shape[0]: raise LinAlgError, 'Incompatible dimensions' - t =_commonType(a, b) - real_t = _array_type[0][_array_precision[t]] + t = _commonType(a, b) + real_t = _realType(t) bstar = zeros((ldb,n_rhs),t) bstar[:b.shape[0],:n_rhs] = b.copy() a,bstar = _castCopyAndTranspose(t, a, bstar) s = zeros((min(m,n),),real_t) nlvl = max( 0, int( math.log( float(min( m,n ))/2. ) ) + 1 ) - iwork = zeros((3*min(m,n)*nlvl+11*min(m,n),), 'i') - if _array_kind[t] == 1: # Complex routines take different arguments + iwork = zeros((3*min(m,n)*nlvl+11*min(m,n),), fortran_int) + if issubclass(t, complexfloating): lapack_routine = lapack_lite.zgelsd lwork = 1 rwork = zeros((lwork,), real_t) From numpy-svn at scipy.org Tue Jul 4 08:13:00 2006 From: numpy-svn at scipy.org (numpy-svn at scipy.org) Date: Tue, 4 Jul 2006 07:13:00 -0500 (CDT) Subject: [Numpy-svn] r2732 - in trunk/numpy/f2py/lib: . research/rat Message-ID: <20060704121300.A444ED408B4@new.scipy.org> Author: pearu Date: 2006-07-04 07:12:47 -0500 (Tue, 04 Jul 2006) New Revision: 2732 Added: trunk/numpy/f2py/lib/research/rat/wrap.f90 trunk/numpy/f2py/lib/utils.py Modified: trunk/numpy/f2py/lib/base_classes.py trunk/numpy/f2py/lib/block_statements.py trunk/numpy/f2py/lib/parsefortran.py trunk/numpy/f2py/lib/research/rat/rational.f90 trunk/numpy/f2py/lib/splitline.py trunk/numpy/f2py/lib/statements.py trunk/numpy/f2py/lib/test_parser.py trunk/numpy/f2py/lib/typedecl_statements.py Log: Working on Fortran analyzer. Modified: trunk/numpy/f2py/lib/base_classes.py =================================================================== --- trunk/numpy/f2py/lib/base_classes.py 2006-07-03 22:32:32 UTC (rev 2731) +++ trunk/numpy/f2py/lib/base_classes.py 2006-07-04 12:12:47 UTC (rev 2732) @@ -3,9 +3,156 @@ import re import sys +import copy from readfortran import Line +from utils import split_comma, specs_split_comma +class AttributeHolder: + # copied from symbolic.base module + """ + Defines a object with predefined attributes. Only those attributes + are allowed that are specified as keyword arguments of a constructor. + When an argument is callable then the corresponding attribute will + be read-only and set by the value the callable object returns. + """ + def __init__(self, **kws): + self._attributes = {} + self._readonly = [] + for k,v in kws.items(): + self._attributes[k] = v + if callable(v): + self._readonly.append(k) + return + def __getattr__(self, name): + if name not in self._attributes: + raise AttributeError,'%s instance has no attribute %r, '\ + 'expected attributes: %s' \ + % (self.__class__.__name__,name, + ','.join(self._attributes.keys())) + value = self._attributes[name] + if callable(value): + value = value() + self._attributes[name] = value + return value + + def __setattr__(self, name, value): + if name in ['_attributes','_readonly']: + self.__dict__[name] = value + return + if name in self._readonly: + raise AttributeError,'%s instance attribute %r is readonly' \ + % (self.__class__.__name__, name) + if name not in self._attributes: + raise AttributeError,'%s instance has no attribute %r, '\ + 'expected attributes: %s' \ + % (self.__class__.__name__,name,','.join(self._attributes.keys())) + self._attributes[name] = value + + def __repr__(self): + l = [] + for k in self._attributes.keys(): + v = getattr(self,k) + l.append('%s=%r' % (k,v)) + return '%s(%s)' % (self.__class__.__name__,', '.join(l)) + + def todict(self): + d = {} + for k in self._attributes.keys(): + v = getattr(self, k) + d[k] = v + return d + +def get_base_classes(cls): + bases = () + for c in cls.__bases__: + bases += get_base_classes(c) + return bases + cls.__bases__ + (cls,) + +class Variable: + """ + Variable instance has attributes: + name + typedecl + dimension + attributes + intent + parent - Statement instances defining the variable + """ + def __init__(self, parent, name): + self.parent = parent + self.name = name + self.typedecl = None + self.dimension = None + self.attributes = [] + self.intent = None + self.bind = [] + self.check = [] + return + + def set_type(self, typedecl): + if self.typedecl is not None: + if not self.typedecl==typedecl: + message = 'Warning: variable %r already has type %s' \ + % (self.name, self.typedecl.tostr()) + message += '.. resetting to %s' % (typedecl.tostr()) + self.parent.show_message(message) + self.typedecl = typedecl + return + + def update(self, attrs): + attributes = self.attributes + for attr in attrs: + lattr = attr.lower() + uattr = attr.upper() + if lattr.startswith('dimension'): + assert self.dimension is None, `self.dimension,attr` + l = attr[9:].lstrip() + assert l[0]+l[-1]=='()',`l` + self.dimension = split_comma(l[1:-1].strip(), self.parent.item) + continue + if lattr.startswith('intent'): + l = attr[6:].lstrip() + assert l[0]+l[-1]=='()',`l` + self.intent = intent = [] + for i in split_comma(l[1:-1].strip(), self.parent.item): + if i not in intent: + intent.append(i) + continue + if lattr.startswith('bind'): + l = attr[4:].lstrip() + assert l[0]+l[-1]=='()',`l` + self.bind = specs_split_comma(l[1:-1].strip(), self.parent.item) + continue + if lattr.startswith('check'): + l = attr[5:].lstrip() + assert l[0]+l[-1]=='()',`l` + self.check.extend(split_comma(l[1:-1].strip()), self.parent.item) + continue + if uattr not in attributes: + attributes.append(uattr) + return + + def __str__(self): + s = '' + if self.typedecl is not None: + s += self.typedecl.tostr() + ' ' + a = self.attributes[:] + if self.dimension is not None: + a.append('DIMENSION(%s)' % (', '.join(self.dimension))) + if self.intent is not None: + a.append('INTENT(%s)' % (', '.join(self.intent))) + if self.bind: + a.append('BIND(%s)' % (', '.join(self.bind))) + if self.check: + a.append('CHECK(%s)' % (', '.join(self.check))) + if a: + s += ', '.join(a) + ' :: ' + return s + self.name + +class ProgramBlock: + pass + class Statement: """ Statement instance has attributes: @@ -18,6 +165,14 @@ def __init__(self, parent, item): self.parent = parent self.reader = parent.reader + self.top = getattr(parent,'top',None) + if isinstance(parent, ProgramBlock): + self.programblock = parent + elif isinstance(self, ProgramBlock): + self.programblock = self + elif hasattr(parent,'programblock'): + self.programblock = parent.programblock + self.item = item # when a statement instance is constructed by error, set isvalid to False @@ -25,8 +180,19 @@ # when a statement should be ignored, set ignore to True self.ignore = False + # attribute a will hold analyze information. + a_dict = {} + for cls in get_base_classes(self.__class__): + if hasattr(cls,'a'): + a_dict.update(copy.deepcopy(cls.a.todict())) + self.a = AttributeHolder(**a_dict) + if hasattr(self.__class__,'a'): + assert self.a is not self.__class__.a + self.process_item() + return + def get_indent_tab(self,colon=None,deindent=False): if self.reader.isfix: tab = ' '*6 @@ -60,6 +226,10 @@ sys.stderr.flush() return + def analyze(self): + self.show_message('nothing analyzed in %s' % (self.__class__.__name__)) + return + class BeginStatement(Statement): """ @@ -237,6 +407,10 @@ #sys.exit() return + def analyze(self): + for stmt in self.content: + stmt.analyze() + return class EndStatement(Statement): """ @@ -274,6 +448,9 @@ self.isvalid = False self.name = self.parent.name + def analyze(self): + return + def __str__(self): return self.get_indent_tab()[:-2] + 'END %s %s'\ % (self.blocktype.upper(),self.name or '') Modified: trunk/numpy/f2py/lib/block_statements.py =================================================================== --- trunk/numpy/f2py/lib/block_statements.py 2006-07-03 22:32:32 UTC (rev 2731) +++ trunk/numpy/f2py/lib/block_statements.py 2006-07-04 12:12:47 UTC (rev 2732) @@ -5,31 +5,115 @@ import re import sys -from base_classes import BeginStatement, EndStatement, Statement +from base_classes import BeginStatement, EndStatement, Statement,\ + AttributeHolder, ProgramBlock from readfortran import Line +from utils import filter_stmts, parse_bind, parse_result +class HasImplicitStmt: + + a = AttributeHolder(implicit_rules = None) + + def get_type(self, name): + implicit_rules = self.a.implicit_rules + if implicit_rules=={}: + raise ValueError,'Implicit rules mapping is null' + l = name[0].lower() + if implicit_rules.has_key(l): + return implicit_rules[l] + # default rules + if l in 'ijklmn': + return implicit_rules['default_integer'] + return implicit_rules['default_real'] + + def initialize(self): + implicit_rules = self.a.implicit_rules + if implicit_rules is not None: + return + self.a.implicit_rules = implicit_rules = {} + real = Real(self, self.item.copy('real')) + assert real.isvalid + integer = Integer(self, self.item.copy('integer')) + assert integer.isvalid + implicit_rules['default_real'] = real + implicit_rules['default_integer'] = integer + return + +class HasUseStmt: + + a = AttributeHolder(use = {}) + + def get_entity(self, name): + for modname, modblock in self.top.a.module.items(): + for stmt in modblock.content: + if getattr(stmt,'name','') == name: + return stmt + return + + def initialize(self): + + return + +class HasVariables: + + a = AttributeHolder(variables = {}) + +class HasTypeDecls: + + a = AttributeHolder(type_decls = {}) + +class HasExternal: + + a = AttributeHolder(external = []) + +class HasAttributes: + + a = AttributeHolder(attributes = []) + # File block class EndSource(EndStatement): """ + Dummy End statement for BeginSource. """ match = staticmethod(lambda s: False) class BeginSource(BeginStatement): """ + Fortran source content. """ match = staticmethod(lambda s: True) - end_stmt_cls = EndSource + a = AttributeHolder(module = {}, + external_subprogram = {}, + blockdata = {}, + ) def tostr(self): return '!' + self.blocktype.upper() + ' '+ self.name def process_item(self): self.name = self.reader.name + self.top = self self.fill(end_flag = True) return + def analyze(self): + for stmt in self.content: + if isinstance(stmt, Module): + stmt.analyze() + self.a.module[stmt.name] = stmt + elif isinstance(stmt, SubProgramStatement): + stmt.analyze() + self.a.external_subprogram[stmt.name] = stmt + elif isinstance(stmt, BlockData): + stmt.analyze() + self.a.blockdata[stmt.name] = stmt + else: + message = 'Unexpected statement %r in %r block. Ignoring.' \ + % (stmt.__class__, self.__class__) + return + def get_classes(self): return program_unit @@ -58,7 +142,9 @@ class EndModule(EndStatement): match = re.compile(r'end(\s*module\s*\w*|)\Z', re.I).match -class Module(BeginStatement): +class Module(BeginStatement, HasAttributes, + HasImplicitStmt, HasUseStmt, HasVariables, + HasTypeDecls): """ MODULE .. @@ -66,6 +152,9 @@ """ match = re.compile(r'module\s*\w+\Z', re.I).match end_stmt_cls = EndModule + a = AttributeHolder(module_subprogram = {}, + module_data = {}, + ) def get_classes(self): return access_spec + specification_part + module_subprogram_part @@ -75,17 +164,37 @@ self.name = name return BeginStatement.process_item(self) - #def __str__(self): - # s = self.get_indent_tab(deindent=True) - # s += 'MODULE '+ self.name - # return s + def analyze(self): + content = self.content[:] + [stmt.analyze() for stmt in filter_stmts(content, Use)] + HasUseStmt.initialize(self) + + [stmt.analyze() for stmt in filter_stmts(content, Implicit)] + HasImplicitStmt.initialize(self) + + while content: + stmt = content.pop(0) + if isinstance(stmt, Contains): + for stmt in filter_stmts(content, SubProgramStatement): + stmt.analyze() + self.a.module_subprogram[stmt.name] = stmt + stmt = content.pop(0) + assert isinstance(stmt, EndModule),`stmt` + continue + stmt.analyze() + + if content: + print 'Not analyzed content:',content + + return + # Python Module class EndPythonModule(EndStatement): match = re.compile(r'end(\s*python\s*module\s*\w*|)\Z', re.I).match -class PythonModule(BeginStatement): +class PythonModule(BeginStatement, HasImplicitStmt, HasUseStmt): """ PYTHON MODULE .. @@ -111,7 +220,8 @@ """ match = re.compile(r'end(\s*program\s*\w*|)\Z', re.I).match -class Program(BeginStatement): +class Program(BeginStatement, ProgramBlock, HasAttributes, + HasImplicitStmt, HasUseStmt): """ PROGRAM [name] """ match = re.compile(r'program\s*\w*\Z', re.I).match @@ -137,7 +247,8 @@ match = re.compile(r'end(\s*block\s*data\s*\w*|)\Z', re.I).match blocktype = 'blockdata' -class BlockData(BeginStatement): +class BlockData(BeginStatement, HasImplicitStmt, HasUseStmt, + HasVariables): """ BLOCK DATA [ ] """ @@ -157,7 +268,7 @@ match = re.compile(r'end\s*interface\s*\w*\Z', re.I).match blocktype = 'interface' -class Interface(BeginStatement): +class Interface(BeginStatement, HasImplicitStmt, HasUseStmt): """ INTERFACE [] | ABSTRACT INTERFACE END INTERFACE [] @@ -198,10 +309,14 @@ # Subroutine -class SubProgramStatement(BeginStatement): +class SubProgramStatement(BeginStatement, ProgramBlock, + HasImplicitStmt, HasAttributes, + HasUseStmt, HasVariables, HasTypeDecls, + HasExternal): """ [ ] [ ( ) ] [ ] """ + a = AttributeHolder(internal_subprogram = {}) def process_item(self): clsname = self.__class__.__name__.lower() @@ -223,7 +338,17 @@ if not a: continue args.append(a) line = line[i+1:].lstrip() - self.suffix = item.apply_map(line) + suffix = item.apply_map(line) + self.bind, suffix = parse_bind(suffix, item) + self.result = None + if isinstance(self, Function): + self.result, suffix = parse_result(suffix, item) + if suffix: + assert self.bind is None,`self.bind` + self.bind, suffix = parse_result(suffix, item) + if self.result is None: + self.result = self.name + assert not suffix,`suffix` self.args = args self.typedecl = None return BeginStatement.process_item(self) @@ -237,12 +362,60 @@ assert isinstance(self, Function),`self.__class__.__name__` s += self.typedecl.tostr() + ' ' s += clsname - return '%s %s(%s) %s' % (s, self.name,', '.join(self.args),self.suffix) + suf = '' + if self.result and self.result!=self.name: + suf += ' RESULT ( %s )' % (self.result) + if self.bind: + suf += ' BIND ( %s )' % (', '.join(self.bind)) + return '%s %s(%s)%s' % (s, self.name,', '.join(self.args),suf) def get_classes(self): return f2py_stmt + specification_part + execution_part \ + internal_subprogram_part + def analyze(self): + content = self.content[:] + + variables = self.a.variables + for a in self.args: + assert not variables.has_key(a) + assert is_name(a) + variables[a] = Variable(self, a) + + if isinstance(self, Function): + variables[self.result] = Variable(self, self.result) + + [stmt.analyze() for stmt in filter_stmts(content, Use)] + HasUseStmt.initialize(self) + + [stmt.analyze() for stmt in filter_stmts(content, Implicit)] + HasImplicitStmt.initialize(self) + + while content: + stmt = content.pop(0) + if isinstance(stmt, Contains): + for stmt in filter_stmts(content, SubProgramStatement): + stmt.analyze() + self.a.internal_subprogram[stmt.name] = stmt + stmt = content.pop(0) + assert isinstance(stmt, self.end_stmt_cls),`stmt` + elif isinstance(stmt, TypeDecl): + stmt.analyze() + self.a.type_declaration[stmt.name] = stmt + elif isinstance(stmt, self.end_stmt_cls): + continue + elif isinstance(stmt, External): + self.a.external.extend(stmt.items) + continue + elif isinstance(stmt, tuple(declaration_type_spec)): + stmt.analyze() + else: + stmt.analyze() + if content: + print 'Not analyzed content:',content + + return + class EndSubroutine(EndStatement): """ END [SUBROUTINE [name]] @@ -252,7 +425,7 @@ class Subroutine(SubProgramStatement): """ - [prefix] SUBROUTINE [ ( [] ) []] + [ ] SUBROUTINE [ ( [ ] ) [ ]] """ end_stmt_cls = EndSubroutine match = re.compile(r'(recursive|pure|elemental|\s)*subroutine\s*\w+', re.I).match @@ -271,6 +444,8 @@ = [ ]... = | RECURSIVE | PURE | ELEMENTAL + = [ RESULT ( ) ] + | RESULT ( ) [ ] """ end_stmt_cls = EndFunction match = re.compile(r'(recursive|pure|elemental|\s)*function\s*\w+', re.I).match @@ -292,7 +467,7 @@ self.item.clone(rest) self.isvalid = False return - if self.parent.__class__ not in [Function,Subroutine]: + if self.parent.__class__ not in [Function, Subroutine]: self.isvalid = False return prefix = prefix + ' ' + self.parent.prefix @@ -558,7 +733,7 @@ match = re.compile(r'end\s*type\s*\w*\Z', re.I).match blocktype = 'type' -class Type(BeginStatement): +class Type(BeginStatement, HasVariables, HasAttributes): """ TYPE [ [, ] ::] [ ( ) ] = | EXTENDS ( ) @@ -607,6 +782,12 @@ return [Integer] + private_or_sequence + component_part +\ type_bound_procedure_part + def analyze(self): + BeginStatement.analyze(self) + assert isinstance(self.parent,HasTypeDecls) + self.parent.a.type_decls[self.name] = self + return + TypeDecl = Type # Enum Modified: trunk/numpy/f2py/lib/parsefortran.py =================================================================== --- trunk/numpy/f2py/lib/parsefortran.py 2006-07-03 22:32:32 UTC (rev 2731) +++ trunk/numpy/f2py/lib/parsefortran.py 2006-07-04 12:12:47 UTC (rev 2732) @@ -17,6 +17,7 @@ from readfortran import FortranFileReader, FortranStringReader from block_statements import BeginSource +from utils import AnalyzeError class FortranParser: @@ -37,7 +38,7 @@ def parse(self): try: - return BeginSource(self) + block = self.block = BeginSource(self) except KeyboardInterrupt: raise except: @@ -50,8 +51,18 @@ reader = reader.reader traceback.print_exc(file=sys.stdout) self.reader.show_message(red_text('STOPPED PARSING'), sys.stdout) + return return + def analyze(self): + try: + self.block.analyze() + except AnalyzeError: + pass + except: + raise + return + def test_pyf(): string = """ python module foo @@ -123,8 +134,9 @@ reader = FortranFileReader(filename) print yellow_text('Processing '+filename+' (mode=%r)' % (reader.mode)) parser = FortranParser(reader) - block = parser.parse() - #print block + parser.parse() + parser.analyze() + #print parser.block def profile_main(): import hotshot, hotshot.stats Modified: trunk/numpy/f2py/lib/research/rat/rational.f90 =================================================================== --- trunk/numpy/f2py/lib/research/rat/rational.f90 2006-07-03 22:32:32 UTC (rev 2731) +++ trunk/numpy/f2py/lib/research/rat/rational.f90 2006-07-04 12:12:47 UTC (rev 2732) @@ -41,8 +41,4 @@ end module rational -subroutine init_f90_funcs(set_f90_funcs) - use rational - external set_f90_funcs - call set_f90_funcs(4, rat_create, rat_show, rat_set, rat_add) -end subroutine init_f90_funcs + Added: trunk/numpy/f2py/lib/research/rat/wrap.f90 =================================================================== --- trunk/numpy/f2py/lib/research/rat/wrap.f90 2006-07-03 22:32:32 UTC (rev 2731) +++ trunk/numpy/f2py/lib/research/rat/wrap.f90 2006-07-04 12:12:47 UTC (rev 2732) @@ -0,0 +1,5 @@ +subroutine init_f90_funcs(set_f90_funcs) + use rational + external set_f90_funcs + call set_f90_funcs(4, rat_create, rat_show, rat_set, rat_add) +end subroutine init_f90_funcs Modified: trunk/numpy/f2py/lib/splitline.py =================================================================== --- trunk/numpy/f2py/lib/splitline.py 2006-07-03 22:32:32 UTC (rev 2731) +++ trunk/numpy/f2py/lib/splitline.py 2006-07-04 12:12:47 UTC (rev 2732) @@ -65,7 +65,7 @@ _cache['pindex'] += 1 index = _cache['pindex'] key = 'F2PY_EXPR_TUPLE_%s' % (index) - it = item[1:-1] + it = item[1:-1].strip() string_map[key] = it rev_string_map[it] = key expr_keys.append(key) Modified: trunk/numpy/f2py/lib/statements.py =================================================================== --- trunk/numpy/f2py/lib/statements.py 2006-07-03 22:32:32 UTC (rev 2731) +++ trunk/numpy/f2py/lib/statements.py 2006-07-04 12:12:47 UTC (rev 2732) @@ -2,41 +2,16 @@ import re import sys -from base_classes import Statement +from base_classes import Statement, Variable +from expression import Expression # Auxiliary tools +from utils import split_comma, specs_split_comma, AnalyzeError, ParseError,\ + get_module_file, parse_bind, parse_result + is_name = re.compile(r'\w+\Z').match -def split_comma(line, item = None, comma=','): - items = [] - if item is None: - for s in line.split(comma): - s = s.strip() - if not s: continue - items.append(s) - return items - newitem = item.copy(line, True) - apply_map = newitem.apply_map - for s in newitem.get_line().split(comma): - s = apply_map(s).strip() - if not s: continue - items.append(s) - return items - -def specs_split_comma(line, item): - specs0 = split_comma(line, item) - specs = [] - for spec in specs0: - i = spec.find('=') - if i!=-1: - kw = spec[:i].strip().upper() - v = spec[i+1:].strip() - specs.append('%s = %s' % (kw, v)) - else: - specs.append(spec) - return specs - class StatementWithNamelist(Statement): """ [ :: ] @@ -113,7 +88,7 @@ match = re.compile(r'assign\s*\d+\s*to\s*\w+\s*\Z',re.I).match def process_item(self): line = self.item.get_line()[6:].lstrip() - i = line.find('to') + i = line.lower().find('to') assert not self.item.has_map() self.items = [line[:i].rstrip(),line[i+2:].lstrip()] return @@ -121,7 +96,6 @@ return self.get_indent_tab() + 'ASSIGN %s TO %s' \ % (self.items[0], self.items[1]) - class Call(Statement): """Call statement class CALL [ ( [ ] ) ] @@ -172,6 +146,14 @@ s += '('+', '.join(map(str,self.items))+ ')' return s + def analyze(self): + a = self.programblock.a + if hasattr(a, 'external'): + external = a.external + if self.designator in external: + print 'Need to analyze:',self + return + class Goto(Statement): """ GO TO