Changeset 2545


Ignore:
Timestamp:
Mar 14, 2006, 4:37:49 PM (18 years ago)
Author:
ole
Message:

Got utilities to compile and pass all tests using numpy

Location:
inundation-numpy-branch/utilities
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • inundation-numpy-branch/utilities/cg_solve.py

    r2503 r2545  
    2828   """
    2929
    30    from Numeric import dot, array, Float, zeros
     30   from numpy import dot, array, Float, zeros
    3131
    32    b  = array(b, typecode=Float)
     32   b  = array(b, dtype=Float)
    3333   if len(b.shape) != 1 :
    3434      raise VectorShapeError, 'input vector should consist of only one column'
    3535
    3636   if x0 is None:
    37       x0 = zeros(b.shape, typecode=Float)
     37      x0 = zeros(b.shape, dtype=Float)
    3838   else:
    39       x0 = array(x0, typecode=Float)
     39      x0 = array(x0, dtype=Float)
    4040
    4141
  • inundation-numpy-branch/utilities/compile.py

    r2528 r2545  
    172172    package called something like python2.3-dev""" %headerfile
    173173
     174
     175  # Add location of arrayobject.h
     176
     177  arraypath = 'Lib' + os.sep + 'site-packages' + os.sep + 'numpy' + os.sep + 'core' + os.sep + 'include' + os.sep + 'numpy'
     178  if sys.platform == 'win32':  #Windows
     179    array_include = os.path.join(sys.exec_prefix, arraypath)
     180  else:
     181    #FIXME: Not done yet
     182    python_include = os.path.join(os.path.join(sys.exec_prefix, 'include'),
     183                                  'python' + version)
     184
     185  # Check existence of arrayobject.h
     186  #
     187  array_headerfile = array_include + os.sep + 'arrayobject.h'
     188  try:
     189    open(array_headerfile, 'r')
     190  except:
     191    raise """Did not find Numpy header file %s. Make sure files for Python C-extensions are installed.  """ %array_headerfile 
     192 
    174193
    175194
     
    221240          %(compiler, FN, python_include, root)
    222241    else:
    223       s = '%s -c %s -I%s -I%s -o %s.o -Wall -O3'\
    224           %(compiler, FN, python_include, utilities_include_dir, root)
     242      s = '%s -c %s -I%s -I%s -I%s -o %s.o -Wall -O3'\
     243          %(compiler, FN, python_include, utilities_include_dir, array_include, root)
    225244
    226245    if os.name == 'posix' and os.uname()[4] == 'x86_64':
  • inundation-numpy-branch/utilities/numerical_tools.py

    r2533 r2545  
    88#(this should move to somewhere central)
    99try:
    10     from scipy import ArrayType, array, sum, innerproduct, ravel, sqrt, searchsorted, sort, concatenate   
     10    from numpy import ArrayType, array, sum, innerproduct, ravel, sqrt, searchsorted, sort, concatenate   
    1111except:
    12     print 'Could not find scipy - using Numeric'
     12    print 'Could not find numpy - using Numeric'
    1313    from Numeric import ArrayType, array, sum, innerproduct, ravel, sqrt, searchsorted, sort, concatenate   
    1414   
     
    181181    else:
    182182        if type(A) == ArrayType:
    183             if A.typecode == typecode:
     183            if A.dtype.char == typecode:
    184184                return array(A)  #FIXME: Shouldn't this just return A?
    185185            else:
     
    242242#Initialise module
    243243
    244 from utilities import compile
     244#from utilities import compile
     245import compile
    245246if compile.can_use_C_extension('util_ext.c'):
    246247    from util_ext import gradient, gradient2
  • inundation-numpy-branch/utilities/polygon_ext.c

    r2510 r2545  
    1515
    1616#include "Python.h"
    17 #include "Numeric/arrayobject.h"
     17#include <arrayobject.h>
    1818#include "math.h"
    1919
  • inundation-numpy-branch/utilities/sparse.py

    r2503 r2545  
    2020           
    2121        if len(args) == 1:
    22             from Numeric import array
     22            from numpy import array
    2323            try:
    2424                A = array(args[0])
    2525            except:
    26                 raise 'Input must be convertable to a Numeric array'
     26                raise 'Input must be convertable to a numpy array'
    2727
    2828            assert len(A.shape) == 2, 'Input must be a 2d matrix'
     
    9393
    9494    def todense(self):
    95         from Numeric import zeros, Float
     95        from numpy import zeros, Float
    9696
    9797        D = zeros( (self.M, self.N), Float)
     
    107107    def __mul__(self, other):
    108108        """Multiply this matrix onto 'other' which can either be
    109         a Numeric vector, a Numeric matrix or another sparse matrix.
    110         """
    111 
    112         from Numeric import array, zeros, Float
     109        a numpy vector, a numpy matrix or another sparse matrix.
     110        """
     111
     112        from numpy import array, zeros, Float
    113113       
    114114        try:
    115115            B = array(other)
    116116        except:
    117             msg = 'FIXME: Only Numeric types implemented so far'
     117            msg = 'FIXME: Only numpy types implemented so far'
    118118            raise msg
    119119           
     
    161161        """
    162162
    163         from Numeric import array, zeros, Float
     163        from numpy import array, zeros, Float
    164164       
    165165        new = other.copy()
     
    176176        """
    177177
    178         from Numeric import array, zeros, Float
     178        from numpy import array, zeros, Float
    179179       
    180180        try:
     
    196196    def trans_mult(self, other):
    197197        """Multiply the transpose of matrix with 'other' which can be
    198         a Numeric vector.
    199         """
    200 
    201         from Numeric import array, zeros, Float
     198        a numpy vector.
     199        """
     200
     201        from numpy import array, zeros, Float
    202202       
    203203        try:
    204204            B = array(other)
    205205        except:
    206             print 'FIXME: Only Numeric types implemented so far'
     206            print 'FIXME: Only numpy types implemented so far'
    207207
    208208
     
    250250        """
    251251
    252         from Numeric import array, Float, Int
     252        from numpy import array, Float, Int
    253253
    254254        if isinstance(A,Sparse):
    255255
    256             from Numeric import zeros
     256            from numpy import zeros
    257257            keys = A.Data.keys()
    258258            keys.sort()
     
    298298
    299299    def todense(self):
    300         from Numeric import zeros, Float
     300        from numpy import zeros, Float
    301301
    302302        D = zeros( (self.M, self.N), Float)
     
    310310    def __mul__(self, other):
    311311        """Multiply this matrix onto 'other' which can either be
    312         a Numeric vector, a Numeric matrix or another sparse matrix.
    313         """
    314 
    315         from Numeric import array, zeros, Float
     312        a numpy vector, a numpy matrix or another sparse matrix.
     313        """
     314
     315        from numpy import array, zeros, Float
    316316       
    317317        try:
    318318            B = array(other)
    319319        except:
    320             print 'FIXME: Only Numeric types implemented so far'
     320            print 'FIXME: Only numpy types implemented so far'
    321321
    322322        return csr_mv(self,B)
     
    328328    """
    329329
    330     from Numeric import zeros, Float
     330    from numpy import zeros, Float
    331331
    332332
     
    370370
    371371#Setup for C extensions
    372 from utilities import compile
     372import compile
    373373if compile.can_use_C_extension('sparse_ext.c'):
    374374    #Replace python version with c implementation
     
    377377if __name__ == '__main__':
    378378
    379     from Numeric import allclose, array, Float
     379    from numpy import allclose, array, Float
    380380   
    381381    A = Sparse(3,3)
  • inundation-numpy-branch/utilities/sparse_ext.c

    r2503 r2545  
    1111       
    1212#include "Python.h"
    13 #include "Numeric/arrayobject.h"
     13#include "arrayobject.h"
    1414#include "math.h"
    1515#include "stdio.h"
  • inundation-numpy-branch/utilities/test_cg_solve.py

    r2527 r2545  
    44
    55
    6 from Numeric import dot, allclose, array, transpose, arange, ones, Float
     6from numpy import dot, allclose, array, transpose, arange, ones, Float
    77from cg_solve import *
    88from sparse import Sparse, Sparse_CSR
  • inundation-numpy-branch/utilities/test_numerical_tools.py

    r2534 r2545  
    33
    44import unittest
    5 from Numeric import zeros, array, allclose
     5from numpy import zeros, array, allclose
    66from math import sqrt, pi
    77from pyvolution.config import epsilon
     
    3232    def test_ensure_numeric(self):
    3333        from numerical_tools import ensure_numeric
    34         from Numeric import ArrayType, Float, array
     34        from numpy import ArrayType, Float, array
    3535
    3636        A = [1,2,3,4]
    3737        B = ensure_numeric(A)
    3838        assert type(B) == ArrayType
    39         assert B.typecode() == 'l'
     39        assert B.dtype.char == 'l'
    4040        assert B[0] == 1 and B[1] == 2 and B[2] == 3 and B[3] == 4
    4141
     
    4444        B = ensure_numeric(A)
    4545        assert type(B) == ArrayType
    46         assert B.typecode() == 'd'
     46        assert B.dtype.char == 'd'
    4747        assert B[0] == 1 and B[1] == 2 and B[2] == 3.14 and B[3] == 4
    4848
     
    5151        B = ensure_numeric(A, Float)
    5252        assert type(B) == ArrayType
    53         assert B.typecode() == 'd'
     53        assert B.dtype.char == 'd'
    5454        assert B[0] == 1.0 and B[1] == 2.0 and B[2] == 3.0 and B[3] == 4.0
    5555
     
    5858        B = ensure_numeric(A, Float)
    5959        assert type(B) == ArrayType
    60         assert B.typecode() == 'd'
     60        assert B.dtype.char == 'd'
    6161        assert B[0] == 1.0 and B[1] == 2.0 and B[2] == 3.0 and B[3] == 4.0
    6262
     
    6565        B = ensure_numeric(A)
    6666        assert type(B) == ArrayType
    67         assert B.typecode() == 'l'       
    68         assert A == B   
     67        assert B.dtype.char == 'l'
     68        assert (A == B).all()   
    6969        assert A is B   #Same object
    7070
     
    7373        B = ensure_numeric(A, Float)
    7474        assert type(B) == ArrayType
    75         assert B.typecode() == 'd'       
    76         assert A == B   
     75        assert B.dtype.char == 'd'       
     76        assert (A == B).all()           
    7777        assert A is not B   #Not the same object
    7878
     
    165165
    166166
    167         bins = [1.1,2,3.1,4]
    168         #print histogram(a, bins)
    169         assert allclose(histogram(a, bins), [0,6,0,1])
    170 
    171 
    172         bins = [0,1.5,2,3]
     167        bins = [0.5,1.5,2,3]
    173168        assert allclose(histogram(a, bins), [8,0,3,4])
    174 
    175 
    176         assert allclose(histogram(a, [0,3]), histogram(a, [-0.5,3]))
    177169
    178170
     
    215207        from util_ext import gradient as gradient_c
    216208
    217         from RandomArray import uniform, seed
    218         seed(17, 53)
     209        from numpy.random import uniform, seed
     210        #seed(17, 53)
     211        seed(17)
    219212
    220213        x0, x1, x2, y0, y1, y2 = uniform(0.0,3.0,6)
  • inundation-numpy-branch/utilities/test_sparse.py

    r2527 r2545  
    55
    66from sparse import *
    7 from Numeric import allclose, array, transpose, Float
     7from numpy import allclose, array, transpose, Float
    88
    99class Test_Sparse(unittest.TestCase):
  • inundation-numpy-branch/utilities/util_ext.c

    r2510 r2545  
    1515
    1616#include "Python.h"
    17 #include "Numeric/arrayobject.h"
     17#include "arrayobject.h"
    1818#include "math.h"
    1919
    2020//Shared snippets
    2121#include "util_ext.h"
    22 
    2322
    2423
  • inundation-numpy-branch/utilities/util_ext.h

    r2508 r2545  
    1111       
    1212#include "Python.h"     
    13 #include "Numeric/arrayobject.h"
     13#include "arrayobject.h"
    1414#include "math.h"
    1515
Note: See TracChangeset for help on using the changeset viewer.