Changeset 5902


Ignore:
Timestamp:
Nov 6, 2008, 3:32:23 PM (16 years ago)
Author:
rwilson
Message:

NumPy? conversion.

Location:
anuga_core/source_numpy_conversion/anuga/utilities
Files:
14 edited

Legend:

Unmodified
Added
Removed
  • anuga_core/source_numpy_conversion/anuga/utilities/cg_solve.py

    r5889 r5902  
    44class ConvergenceError(exceptions.Exception): pass
    55
    6 ##from numpy.oldnumeric import dot, array, Float, zeros
    7 from numpy import dot, array, float, zeros
     6import numpy
    87   
    98import logging, logging.config
     
    2625   
    2726    if x0 is None:
    28         x0 = zeros(b.shape, dtype=float)
     27        x0 = numpy.zeros(b.shape, dtype=numpy.float)
    2928    else:
    30         x0 = array(x0, dtype=float)
     29        x0 = numpy.array(x0, dtype=numpy.float)
    3130
    32     b  = array(b, dtype=float)
     31    b  = numpy.array(b, dtype=numpy.float)
    3332    if len(b.shape) != 1 :
    3433       
     
    5958
    6059
    61    b  = array(b, dtype=float)
     60   b  = numpy.array(b, dtype=numpy.float)
    6261   if len(b.shape) != 1 :
    6362      raise VectorShapeError, 'input vector should consist of only one column'
    6463
    6564   if x0 is None:
    66       x0 = zeros(b.shape, dtype=float)
     65      x0 = numpy.zeros(b.shape, dtype=numpy.float)
    6766   else:
    68       x0 = array(x0, dtype=float)
     67      x0 = numpy.array(x0, dtype=numpy.float)
    6968
    7069
     
    7776   r = b - A*x
    7877   d = r
    79    rTr = dot(r,r)
     78   rTr = numpy.dot(r,r)
    8079   rTr0 = rTr
    8180
    8281   while (i<imax and rTr>tol**2*rTr0):
    8382       q = A*d
    84        alpha = rTr/dot(d,q)
     83       alpha = rTr/numpy.dot(d,q)
    8584       x = x + alpha*d
    8685       if i%50 :
     
    8988           r = r - alpha*q
    9089       rTrOld = rTr
    91        rTr = dot(r,r)
     90       rTr = numpy.dot(r,r)
    9291       bt = rTr/rTrOld
    9392
  • anuga_core/source_numpy_conversion/anuga/utilities/compile.py

    r5889 r5902  
    1212
    1313#NumPy ------------------------------------
    14 # these lines recommended in "Converting from NUMARRAY to NUMPY"
     14# Something like these lines recommended in "Converting from NUMARRAY to NUMPY"
    1515import numpy
    16 import numpy.numarray as nn
    1716numpyincludedirs = numpy.get_include()
    18 numarrayincludedirs = nn.get_numarray_include_dirs()
    1917I_dirs = '-I"%s" ' % numpyincludedirs
    20 for d in numarrayincludedirs:
    21     I_dirs += '-I"%s" ' % d
    2218#NumPy ------------------------------------
    2319
  • anuga_core/source_numpy_conversion/anuga/utilities/interp.py

    r5889 r5902  
    1 ## Automatically adapted for numpy.oldnumeric Oct 28, 2008 by alter_code1.py
    2 
    31#!/usr/bin/python -tt
    42#=======================================================================
     
    140138    """
    141139    import arrayfns
    142 ##    import numpy.oldnumeric.ma as MA
    143 ##    import numpy.oldnumeric as N
    144140    import numpy.ma as MA
    145141    import numpy as N
  • anuga_core/source_numpy_conversion/anuga/utilities/numerical_tools.py

    r5889 r5902  
    77from warnings import warn
    88
    9 #Establish which Numeric package to use
    10 #(this should move to somewhere central)
    11 #try:
    12 #    from scipy import ArrayType, array, sum, innerproduct, ravel, sqrt,
    13 # searchsorted, sort, concatenate, Float, arange   
    14 #except:
    15 #    #print 'Could not find scipy - using Numeric'
    16 #    from numpy.oldnumeric import ArrayType, array, sum, innerproduct, ravel, sqrt,
    17 #searchsorted, sort, concatenate, Float, arange
    18 
    19 ##from numpy.oldnumeric import ArrayType, array, sum, innerproduct, ravel, sqrt,\
    20 ##     searchsorted, sort, concatenate, Float, arange   
    21 from numpy import ndarray, array, sum, inner, ravel, sqrt, searchsorted, sort, concatenate, float, arange   
     9##from numpy import ndarray, array, sum, inner, ravel, sqrt, searchsorted, sort, concatenate, float, arange
     10import numpy
    2211
    2312# Getting an infinite number to use when using Numeric
    2413#INF = (array([1])/0.)[0]
    2514
    26 NAN = (array([1])/0.)[0]
     15NAN = (numpy.array([1])/0.)[0]
    2716# Note, INF is used instead of NAN (Not a number), since Numeric has no NAN
    2817# if we use a package that has NAN, this should be updated to use NAN.
     
    9079        v2 = [1.0, 0.0] # Unit vector along the x-axis
    9180       
    92     v1 = ensure_numeric(v1, float)
    93     v2 = ensure_numeric(v2, float)   
     81    v1 = ensure_numeric(v1, numpy.float)
     82    v2 = ensure_numeric(v2, numpy.float)   
    9483   
    9584    # Normalise
    96     v1 = v1/sqrt(sum(v1**2))
    97     v2 = v2/sqrt(sum(v2**2))
     85    v1 = v1/numpy.sqrt(numpy.sum(v1**2))
     86    v2 = v2/numpy.sqrt(numpy.sum(v2**2))
    9887
    9988    # Compute angle
    100     p = inner(v1, v2)
    101     c = inner(v1, normal_vector(v2)) # Projection onto normal
     89    p = numpy.inner(v1, v2)
     90    c = numpy.inner(v1, normal_vector(v2)) # Projection onto normal
    10291                                            # (negative cross product)
    10392       
     
    141130    """
    142131   
    143     return array([-v[1], v[0]], float)
     132    return numpy.array([-v[1], v[0]], numpy.float)
    144133
    145134   
     
    151140    """Mean value of a vector
    152141    """
    153     return(float(sum(x))/len(x))
     142    return(float(numpy.sum(x))/len(x))
    154143
    155144
     
    172161    cy = y - mean(y) 
    173162
    174     p = inner(cx,cy)/N
     163    p = numpy.inner(cx,cy)/N
    175164    return(p)
    176165
     
    221210    """
    222211 
    223     y = ravel(x)
    224     p = sqrt(inner(y,y))
     212    y = numpy.ravel(x)
     213    p = numpy.sqrt(numpy.inner(y,y))
    225214    return p
    226215   
     
    264253    if typecode is None:
    265254##NumPy        if isinstance(A, ArrayType):
    266         if type(A) == ndarray:
     255        if type(A) == numpy.ndarray:
    267256            return A
    268257        else:
    269             return array(A)
     258            return numpy.array(A)
    270259    else:
    271260##NumPy        if isinstance(A, ArrayType):
    272         if type(A) == ndarray:
     261        if type(A) == numpy.ndarray:
    273262##NumPy            if A.typecode == typecode:
    274263            if A.dtype == typecode:
    275                 return array(A)  #FIXME: Shouldn't this just return A?
     264                return numpy.array(A)  #FIXME: Shouldn't this just return A?
    276265            else:
    277                 return array(A, typecode)
     266                return numpy.array(A, typecode)
    278267        else:
    279268            import types                            ##
    280269            from numpy import str                   ##
    281270            if isinstance(A, types.StringType):     ##
    282                 return array(A, dtype=int)          ##
    283             return array(A, typecode)
     271                return numpy.array(A, dtype=int)          ##
     272            return numpy.array(A, typecode)
    284273
    285274
     
    292281    """
    293282
    294     n = searchsorted(sort(a), bins)
    295     n = concatenate( [n, [len(a)]] )
     283    n = numpy.searchsorted(numpy.sort(a), bins)
     284    n = numpy.concatenate( [n, [len(a)]] )
    296285
    297286    hist = n[1:]-n[:-1]
    298287
    299288    if relative is True:
    300         hist = hist/float(sum(hist))
     289        hist = hist/float(numpy.sum(hist))
    301290       
    302291    return hist
     
    308297    """
    309298
    310     mx = max(data)
    311     mn = min(data)
     299    mx = numpy.max(data)
     300    mn = numpy.min(data)
    312301
    313302    if mx == mn:
    314         bins = array([mn])
     303        bins = numpy.array([mn])
    315304    else:
    316305        if number_of_bins is None:
    317306            number_of_bins = 10
    318307           
    319         bins = arange(mn, mx, (mx-mn)/number_of_bins)
     308        bins = numpy.arange(mn, mx, (mx-mn)/number_of_bins)
    320309
    321310    return bins
  • anuga_core/source_numpy_conversion/anuga/utilities/polygon.py

    r5889 r5902  
    1010#    #print 'Could not find scipy - using Numeric'
    1111
    12 from numpy import float, int, zeros, ones, array, concatenate, reshape, dot, allclose, newaxis, ascontiguousarray
     12##from numpy import float, int, zeros, ones, array, concatenate, reshape, dot, allclose, newaxis, ascontiguousarray
     13import numpy
    1314
    1415
     
    7677    # FIXME (Ole): Write this in C
    7778
    78     line0 = ensure_numeric(line0, float)
    79     line1 = ensure_numeric(line1, float)   
     79    line0 = ensure_numeric(line0, numpy.float)
     80    line1 = ensure_numeric(line1, numpy.float)   
    8081
    8182    x0 = line0[0,0]; y0 = line0[0,1]
     
    8990    u1 = (x2-x0)*(y1-y0) - (y2-y0)*(x1-x0)
    9091       
    91     if allclose(denom, 0.0):
     92    if numpy.allclose(denom, 0.0):
    9293        # Lines are parallel - check if they coincide on a shared a segment
    9394
    94         if allclose( [u0, u1], 0.0 ):
     95        if numpy.allclose( [u0, u1], 0.0 ):
    9596            # We now know that the lines if continued coincide
    9697            # The remaining check will establish if the finite lines share a segment
     
    120121            if line0_starts_on_line1 and line0_ends_on_line1:
    121122                # Shared segment is line0 fully included in line1
    122                 segment = array([[x0, y0], [x1, y1]])               
     123                segment = numpy.array([[x0, y0], [x1, y1]])               
    123124
    124125            if line1_starts_on_line0 and line1_ends_on_line0:
    125126                # Shared segment is line1 fully included in line0
    126                 segment = array([[x2, y2], [x3, y3]])
     127                segment = numpy.array([[x2, y2], [x3, y3]])
    127128           
    128129
     
    130131            if line0_starts_on_line1 and line1_ends_on_line0:
    131132                # Shared segment from line0 start to line 1 end
    132                 segment = array([[x0, y0], [x3, y3]])
     133                segment = numpy.array([[x0, y0], [x3, y3]])
    133134
    134135            if line1_starts_on_line0 and line0_ends_on_line1:
    135136                # Shared segment from line1 start to line 0 end
    136                 segment = array([[x2, y2], [x1, y1]])                               
     137                segment = numpy.array([[x2, y2], [x1, y1]])                               
    137138
    138139
     
    140141            if line0_starts_on_line1 and line1_starts_on_line0:
    141142                # Shared segment from line0 start to line 1 end
    142                 segment = array([[x0, y0], [x2, y2]])
     143                segment = numpy.array([[x0, y0], [x2, y2]])
    143144
    144145            if line0_ends_on_line1 and line1_ends_on_line0:
    145146                # Shared segment from line0 start to line 1 end
    146                 segment = array([[x3, y3], [x1, y1]])               
     147                segment = numpy.array([[x3, y3], [x1, y1]])               
    147148
    148149               
     
    161162
    162163        # Sanity check - can be removed to speed up if needed
    163         assert allclose(x, x2 + u1*(x3-x2))
    164         assert allclose(y, y2 + u1*(y3-y2))       
     164        assert numpy.allclose(x, x2 + u1*(x3-x2))
     165        assert numpy.allclose(y, y2 + u1*(y3-y2))       
    165166
    166167        # Check if point found lies within given line segments
     
    168169            # We have intersection
    169170
    170             return 1, array([x, y])
     171            return 1, numpy.array([x, y])
    171172        else:
    172173            # No intersection
     
    203204
    204205
    205     line0 = ensure_numeric(line0, float)
    206     line1 = ensure_numeric(line1, float)   
     206    line0 = ensure_numeric(line0, numpy.float)
     207    line1 = ensure_numeric(line1, numpy.float)   
    207208
    208209    status, value = _intersection(line0[0,0], line0[0,1],
     
    270271    if len(points.shape) == 1:
    271272        # Only one point was passed in. Convert to array of points
    272         points = reshape(points, (1,2))
     273        points = numpy.reshape(points, (1,2))
    273274
    274275    indices, count = separate_points_by_polygon(points, polygon,
     
    312313    #if verbose: print 'Checking input to outside_polygon'
    313314    try:
    314         points = ensure_numeric(points, float)
     315        points = ensure_numeric(points, numpy.float)
    315316    except NameError, e:
    316317        raise NameError, e
     
    320321
    321322    try:
    322         polygon = ensure_numeric(polygon, float)
     323        polygon = ensure_numeric(polygon, numpy.float)
    323324    except NameError, e:
    324325        raise NameError, e
     
    330331    if len(points.shape) == 1:
    331332        # Only one point was passed in. Convert to array of points
    332         points = reshape(points, (1,2))
     333        points = numpy.reshape(points, (1,2))
    333334
    334335    indices, count = separate_points_by_polygon(points, polygon,
     
    339340    if count == len(indices):
    340341        # No points are outside
    341         return array([])
     342        return numpy.array([])
    342343    else:
    343344        return indices[count:][::-1]  #return reversed
     
    354355    #if verbose: print 'Checking input to outside_polygon'
    355356    try:
    356         points = ensure_numeric(points, float)
     357        points = ensure_numeric(points, numpy.float)
    357358    except NameError, e:
    358359        raise NameError, e
     
    362363
    363364    try:
    364         polygon = ensure_numeric(polygon, float)
     365        polygon = ensure_numeric(polygon, numpy.float)
    365366    except NameError, e:
    366367        raise NameError, e
     
    371372    if len(points.shape) == 1:
    372373        # Only one point was passed in. Convert to array of points
    373         points = reshape(points, (1,2))
     374        points = numpy.reshape(points, (1,2))
    374375
    375376
     
    441442##    print 'Before: points=%s, flags=%s' % (type(points), str(points.flags))
    442443    try:
    443 ##        points = ascontiguousarray(ensure_numeric(points, float))
    444         points = ensure_numeric(points, float)
     444##        points = numpy.ascontiguousarray(ensure_numeric(points, numpy.float))
     445        points = ensure_numeric(points, numpy.float)
    445446    except NameError, e:
    446447        raise NameError, e
     
    452453    #if verbose: print 'Checking input to separate_points_by_polygon 2'
    453454    try:
    454 ##        polygon = ascontiguousarray(ensure_numeric(polygon, float))
    455         polygon = ensure_numeric(polygon, float)
     455##        polygon = numpy.ascontiguousarray(ensure_numeric(polygon, numpy.float))
     456        polygon = ensure_numeric(polygon, numpy.float)
    456457    except NameError, e:
    457458        raise NameError, e
     
    475476        # Only one point was passed in.
    476477        # Convert to array of points
    477         points = reshape(points, (1,2))
     478        points = numpy.reshape(points, (1,2))
    478479
    479480   
     
    494495
    495496
    496     indices = zeros( M, int )
     497    indices = numpy.zeros( M, numpy.int )
    497498
    498499    count = _separate_points_by_polygon(points, polygon, indices,
     
    621622
    622623    try:
    623         polygon = ensure_numeric(polygon, float)
     624        polygon = ensure_numeric(polygon, numpy.float)
    624625    except NameError, e:
    625626        raise NameError, e
     
    630631    x = polygon[:,0]
    631632    y = polygon[:,1]
    632     x = concatenate((x, [polygon[0,0]]), axis = 0)
    633     y = concatenate((y, [polygon[0,1]]), axis = 0)
     633    x = numpy.concatenate((x, [polygon[0,0]]), axis = 0)
     634    y = numpy.concatenate((y, [polygon[0,1]]), axis = 0)
    634635   
    635636    return x, y
     
    732733
    733734    def __call__(self, x, y):
    734         x = array(x).astype(float)
    735         y = array(y).astype(float)
     735        x = numpy.array(x).astype(numpy.float)
     736        y = numpy.array(y).astype(numpy.float)
    736737
    737738        assert len(x.shape) == 1 and len(y.shape) == 1
     
    740741        assert y.shape[0] == N
    741742
    742         points = ascontiguousarray(concatenate( (x[:,newaxis], y[:,newaxis]), axis=1 ))
     743        points = numpy.ascontiguousarray(numpy.concatenate( (x[:,numpy.newaxis], y[:,numpy.newaxis]), axis=1 ))
    743744       
    744745        if callable(self.default):
    745746            z = self.default(x, y)
    746747        else:
    747             z = ones(N, float) * self.default
     748            z = numpy.ones(N, numpy.float) * self.default
    748749
    749750        for polygon, value in self.regions:
     
    753754            if callable(value):
    754755                for i in indices:
    755                     xx = array([x[i]])
    756                     yy = array([y[i]])
     756                    xx = numpy.array([x[i]])
     757                    yy = numpy.array([y[i]])
    757758                    z[i] = value(xx, yy)[0]
    758759            else:
  • anuga_core/source_numpy_conversion/anuga/utilities/quad.py

    r5889 r5902  
    223223            triangles = {}
    224224            verts = self.retrieve_vertices()
    225             print "verts", verts
    226225            for vert in verts:
    227226                triangle_list = self.mesh.get_triangles_and_vertices_per_node(vert)
    228                 print 'triangle_list=%s' % str(triangle_list)
    229227                for k, _ in triangle_list:
    230228                    if not triangles.has_key(k):
     
    436434    """
    437435
    438     from numpy import minimum, maximum
    439 
    440 
    441436    #Make root cell
    442437    #print mesh.coordinates
  • anuga_core/source_numpy_conversion/anuga/utilities/sparse.py

    r5889 r5902  
    33"""
    44
     5import numpy
    56
    67class Sparse:
     
    1819           
    1920        if len(args) == 1:
    20             from numpy import array
    2121            try:
    22                 A = array(args[0])
     22                A = numpy.array(args[0])
    2323            except:
    2424                raise 'Input must be convertable to a Numeric array'
     
    9393
    9494    def todense(self):
    95         from numpy import zeros, float
    96 
    97         D = zeros( (self.M, self.N), float)
     95        D = numpy.zeros( (self.M, self.N), numpy.float)
    9896       
    9997        for i in range(self.M):
     
    110108        """
    111109
    112         from numpy import array, zeros, float
    113        
    114110        try:
    115             B = array(other)
     111            B = numpy.array(other)
    116112        except:
    117113            msg = 'FIXME: Only Numeric types implemented so far'
     
    131127            assert B.shape[0] == self.N, msg
    132128
    133             R = zeros(self.M, float) #Result
     129            R = numpy.zeros(self.M, numpy.float) #Result
    134130           
    135131            # Multiply nonzero elements
     
    141137       
    142138           
    143             R = zeros((self.M, B.shape[1]), float) #Result matrix
     139            R = numpy.zeros((self.M, B.shape[1]), numpy.float) #Result matrix
    144140
    145141            # Multiply nonzero elements
     
    197193        """
    198194
    199         from numpy import array, zeros, float
    200        
    201195        try:
    202             B = array(other)
     196            B = numpy.array(other)
    203197        except:
    204198            print 'FIXME: Only Numeric types implemented so far'
     
    211205            assert B.shape[0] == self.M, 'Mismatching dimensions'
    212206
    213             R = zeros((self.N,), float) #Result
     207            R = numpy.zeros((self.N,), numpy.float) #Result
    214208
    215209            #Multiply nonzero elements
     
    248242        """
    249243
    250         from numpy import zeros, float, int
    251 
    252244        if isinstance(A,Sparse):
    253245
     
    255247            keys.sort()
    256248            nnz = len(keys)
    257             data    = zeros ( (nnz,), float)
    258             colind  = zeros ( (nnz,), int)
    259             row_ptr = zeros ( (A.M+1,), int)
     249            data    = numpy.zeros ( (nnz,), numpy.float)
     250            colind  = numpy.zeros ( (nnz,), numpy.int)
     251            row_ptr = numpy.zeros ( (A.M+1,), numpy.int)
    260252            current_row = -1
    261253            k = 0
     
    295287
    296288    def todense(self):
    297         from numpy.oldnumeric import zeros, Float
    298 
    299         D = zeros( (self.M, self.N), Float)
     289
     290        D = numpy.zeros( (self.M, self.N), numpy.float)
    300291       
    301292        for i in range(self.M):
     
    310301        """
    311302
    312         from numpy.oldnumeric import array, zeros, Float
    313        
    314303        try:
    315             B = array(other)
     304            B = numpy.array(other)
    316305        except:
    317306            print 'FIXME: Only Numeric types implemented so far'
     
    329318if __name__ == '__main__':
    330319    # A little selftest
    331    
    332     from numpy.oldnumeric import allclose, array, Float
    333    
    334320    A = Sparse(3,3)
    335321
     
    362348    u = A*v
    363349    print u
    364     assert allclose(u, [6,14,4])
     350    assert numpy.allclose(u, [6,14,4])
    365351
    366352    u = A.trans_mult(v)
    367353    print u
    368     assert allclose(u, [6,6,10])
     354    assert numpy.allclose(u, [6,6,10])
    369355
    370356    #Right hand side column
    371     v = array([[2,4],[3,4],[4,4]])
     357    v = numpy.array([[2,4],[3,4],[4,4]])
    372358
    373359    u = A*v[:,0]
    374     assert allclose(u, [6,14,4])
     360    assert numpy.allclose(u, [6,14,4])
    375361
    376362    #u = A*v[:,1]
  • anuga_core/source_numpy_conversion/anuga/utilities/test_cg_solve.py

    r5889 r5902  
    55import unittest
    66
    7 
    8 from numpy import dot, allclose, array, transpose, arange, ones, float
     7import numpy
    98from anuga.utilities.cg_solve import *
    109from anuga.utilities.cg_solve import _conjugate_gradient
     
    3029        x = conjugate_gradient(A,b,x,iprint=0)
    3130
    32         assert allclose(x,xe)
     31        assert numpy.allclose(x,xe)
    3332
    3433    def test_max_iter(self):
     
    6160        A = Sparse(n,n)
    6261
    63         for i in arange(0,n):
     62        for i in numpy.arange(0,n):
    6463            A[i,i] = 1.0
    6564            if i > 0 :
     
    6867                A[i,i+1] = -0.5
    6968
    70         xe = ones( (n,), float)
     69        xe = numpy.ones( (n,), numpy.float)
    7170
    7271        b  = A*xe
    7372        x = conjugate_gradient(A,b,b,tol=1.0e-5,iprint=1)
    7473
    75         assert allclose(x,xe)
     74        assert numpy.allclose(x,xe)
    7675
    7776    def test_solve_large_2d(self):
     
    8382        A = Sparse(m*n, m*n)
    8483
    85         for i in arange(0,n):
    86             for j in arange(0,m):
     84        for i in numpy.arange(0,n):
     85            for j in numpy.arange(0,m):
    8786                I = j+m*i
    8887                A[I,I] = 4.0
     
    9695                    A[I,I+1] = -1.0
    9796
    98         xe = ones( (n*m,), float)
     97        xe = numpy.ones( (n*m,), numpy.float)
    9998
    10099        b  = A*xe
    101100        x = conjugate_gradient(A,b,b,iprint=0)
    102101
    103         assert allclose(x,xe)
     102        assert numpy.allclose(x,xe)
    104103
    105104    def test_solve_large_2d_csr_matrix(self):
     
    112111        A = Sparse(m*n, m*n)
    113112
    114         for i in arange(0,n):
    115             for j in arange(0,m):
     113        for i in numpy.arange(0,n):
     114            for j in numpy.arange(0,m):
    116115                I = j+m*i
    117116                A[I,I] = 4.0
     
    125124                    A[I,I+1] = -1.0
    126125
    127         xe = ones( (n*m,), float)
     126        xe = numpy.ones( (n*m,), numpy.float)
    128127
    129128        # Convert to csr format
     
    134133        x = conjugate_gradient(A,b,b,iprint=20)
    135134
    136         assert allclose(x,xe)
     135        assert numpy.allclose(x,xe)
    137136
    138137
     
    145144        A = Sparse(m*n, m*n)
    146145
    147         for i in arange(0,n):
    148             for j in arange(0,m):
     146        for i in numpy.arange(0,n):
     147            for j in numpy.arange(0,m):
    149148                I = j+m*i
    150149                A[I,I] = 4.0
     
    158157                    A[I,I+1] = -1.0
    159158
    160         xe = ones( (n*m,), float)
     159        xe = numpy.ones( (n*m,), numpy.float)
    161160
    162161        b  = A*xe
    163162        x = conjugate_gradient(A,b)
    164163
    165         assert allclose(x,xe)
     164        assert numpy.allclose(x,xe)
    166165
    167166
     
    202201        x = conjugate_gradient(A,b,x,iprint=0)
    203202
    204         assert allclose(x,xe)
     203        assert numpy.allclose(x,xe)
    205204
    206205#-------------------------------------------------------------
  • anuga_core/source_numpy_conversion/anuga/utilities/test_data_audit.py

    r5889 r5902  
    33
    44import unittest
    5 from numpy.oldnumeric import zeros, array, allclose
    65from tempfile import mkstemp
    76import os
  • anuga_core/source_numpy_conversion/anuga/utilities/test_numerical_tools.py

    r5889 r5902  
    33
    44import unittest
    5 from numpy import zeros, array, allclose
    6 from numpy import ndarray, float, int, array, alltrue
     5import numpy
    76
    87from math import sqrt, pi
     
    2322        """Test angles between one vector and the x-axis
    2423        """
    25         assert allclose(angle([1.0, 0.0])/pi*180, 0.0)     
    26         assert allclose(angle([1.0, 1.0])/pi*180, 45.0)
    27         assert allclose(angle([0.0, 1.0])/pi*180, 90.0)         
    28         assert allclose(angle([-1.0, 1.0])/pi*180, 135.0)               
    29         assert allclose(angle([-1.0, 0.0])/pi*180, 180.0)
    30         assert allclose(angle([-1.0, -1.0])/pi*180, 225.0)
    31         assert allclose(angle([0.0, -1.0])/pi*180, 270.0)
    32         assert allclose(angle([1.0, -1.0])/pi*180, 315.0)
     24        assert numpy.allclose(angle([1.0, 0.0])/pi*180, 0.0)       
     25        assert numpy.allclose(angle([1.0, 1.0])/pi*180, 45.0)
     26        assert numpy.allclose(angle([0.0, 1.0])/pi*180, 90.0)           
     27        assert numpy.allclose(angle([-1.0, 1.0])/pi*180, 135.0)         
     28        assert numpy.allclose(angle([-1.0, 0.0])/pi*180, 180.0)
     29        assert numpy.allclose(angle([-1.0, -1.0])/pi*180, 225.0)
     30        assert numpy.allclose(angle([0.0, -1.0])/pi*180, 270.0)
     31        assert numpy.allclose(angle([1.0, -1.0])/pi*180, 315.0)
    3332               
    3433                                                         
     
    3736        """   
    3837       
    39         assert allclose(angle([1.0, 0.0], [1.0, 1.0])/pi*180, 315.0)
    40         assert allclose(angle([1.0, 1.0], [1.0, 0.0])/pi*180, 45.0)
     38        assert numpy.allclose(angle([1.0, 0.0], [1.0, 1.0])/pi*180, 315.0)
     39        assert numpy.allclose(angle([1.0, 1.0], [1.0, 0.0])/pi*180, 45.0)
    4140               
    42         assert allclose(angle([-1.0, -1.0], [1.0, 1.0])/pi*180, 180)   
    43         assert allclose(angle([-1.0, -1.0], [-1.0, 1.0])/pi*180, 90.0) 
     41        assert numpy.allclose(angle([-1.0, -1.0], [1.0, 1.0])/pi*180, 180)     
     42        assert numpy.allclose(angle([-1.0, -1.0], [-1.0, 1.0])/pi*180, 90.0)   
    4443       
    45         assert allclose(angle([-1.0, 0.0], [1.0, 1.0])/pi*180, 135.0)
    46         assert allclose(angle([0.0, -1.0], [1.0, 1.0])/pi*180, 225.0)   
     44        assert numpy.allclose(angle([-1.0, 0.0], [1.0, 1.0])/pi*180, 135.0)
     45        assert numpy.allclose(angle([0.0, -1.0], [1.0, 1.0])/pi*180, 225.0)     
    4746       
    48         assert allclose(angle([1.0, -1.0], [1.0, 1.0])/pi*180, 270.0)   
    49         assert allclose(angle([1.0, 0.0], [0.0, 1.0])/pi*180, 270.0)
     47        assert numpy.allclose(angle([1.0, -1.0], [1.0, 1.0])/pi*180, 270.0)     
     48        assert numpy.allclose(angle([1.0, 0.0], [0.0, 1.0])/pi*180, 270.0)
    5049
    5150        #From test_get_boundary_polygon_V
    5251        v_prev = [-0.5, -0.5]
    5352        vc = [ 0.0,  -0.5]
    54         assert allclose(angle(vc, v_prev)/pi*180, 45.0)
     53        assert numpy.allclose(angle(vc, v_prev)/pi*180, 45.0)
    5554
    5655        vc = [ 0.5,  0.0]
    57         assert allclose(angle(vc, v_prev)/pi*180, 135.0)
     56        assert numpy.allclose(angle(vc, v_prev)/pi*180, 135.0)
    5857
    5958        vc = [ -0.5,  0.5]
    60         assert allclose(angle(vc, v_prev)/pi*180, 270.0)               
     59        assert numpy.allclose(angle(vc, v_prev)/pi*180, 270.0)               
    6160
    6261
    6362    def test_anglediff(self):
    64         assert allclose(anglediff([0.0, 1.], [1.0, 1.0])/pi*180, 45.0)
     63        assert numpy.allclose(anglediff([0.0, 1.], [1.0, 1.0])/pi*180, 45.0)
    6564
    6665       
     
    6867        A = [1,2,3,4]
    6968        B = ensure_numeric(A)
    70         assert isinstance(B, ndarray)
     69        assert isinstance(B, numpy.ndarray)
    7170        assert B.dtype.char == 'l'
    7271        assert B[0] == 1 and B[1] == 2 and B[2] == 3 and B[3] == 4
     
    7473        A = [1,2,3.14,4]
    7574        B = ensure_numeric(A)
    76         assert isinstance(B, ndarray)
     75        assert isinstance(B, numpy.ndarray)
    7776        assert B.dtype.char == 'd'
    7877        assert B[0] == 1 and B[1] == 2 and B[2] == 3.14 and B[3] == 4
    7978
    8079        A = [1,2,3,4]
    81         B = ensure_numeric(A, float)
    82         assert isinstance(B, ndarray)
     80        B = ensure_numeric(A, numpy.float)
     81        assert isinstance(B, numpy.ndarray)
    8382        assert B.dtype.char == 'd'
    8483        assert B[0] == 1.0 and B[1] == 2.0 and B[2] == 3.0 and B[3] == 4.0
    8584
    8685        A = [1,2,3,4]
    87         B = ensure_numeric(A, float)
    88         assert isinstance(B, ndarray)
     86        B = ensure_numeric(A, numpy.float)
     87        assert isinstance(B, numpy.ndarray)
    8988        assert B.dtype.char == 'd'
    9089        assert B[0] == 1.0 and B[1] == 2.0 and B[2] == 3.0 and B[3] == 4.0
    9190
    92         A = array([1,2,3,4])
     91        A = numpy.array([1,2,3,4])
    9392        B = ensure_numeric(A)
    94         assert isinstance(B, ndarray)
     93        assert isinstance(B, numpy.ndarray)
    9594        assert B.dtype.char == 'l'       
    96         assert alltrue(A == B)   
     95        assert numpy.alltrue(A == B)   
    9796        assert A is B   #Same object
    9897
    99         A = array([1,2,3,4])
    100         B = ensure_numeric(A, float)
    101         assert isinstance(B, ndarray)
     98        A = numpy.array([1,2,3,4])
     99        B = ensure_numeric(A, numpy.float)
     100        assert isinstance(B, numpy.ndarray)
    102101        assert B.dtype.char == 'd'       
    103         assert alltrue(A == B)   
     102        assert numpy.alltrue(A == B)   
    104103        assert A is not B   #Not the same object
    105104
    106105        # Check scalars
    107106        A = 1
    108         B = ensure_numeric(A, float)
     107        B = ensure_numeric(A, numpy.float)
    109108        #print A, B[0], len(B), type(B)
    110109        #print B.shape
    111         assert alltrue(A == B)
    112 
    113         B = ensure_numeric(A, int)       
     110        assert numpy.alltrue(A == B)
     111
     112        B = ensure_numeric(A, numpy.int)       
    114113        #print A, B
    115114        #print B.shape
    116         assert alltrue(A == B)
     115        assert numpy.alltrue(A == B)
    117116
    118117    def NO_test_ensure_numeric_char(self):
    119118        # Error situation
    120         B = ensure_numeric('hello', int)
     119        B = ensure_numeric('hello', numpy.int)
    121120        print 'B=%s %s' % (str(B), type(B))
    122         assert allclose(B, [104, 101, 108, 108, 111])
     121        assert numpy.allclose(B, [104, 101, 108, 108, 111])
    123122
    124123
     
    209208        #There are four elements greater than or equal to 3
    210209        bins = [3]
    211         assert allclose(histogram(a, bins), [4])
     210        assert numpy.allclose(histogram(a, bins), [4])
    212211
    213212        bins = [ min(a) ]
    214         assert allclose(histogram(a, bins), [len(a)])
     213        assert numpy.allclose(histogram(a, bins), [len(a)])
    215214
    216215        bins = [ max(a)+0.00001 ]
    217         assert allclose(histogram(a, bins), [0])       
     216        assert numpy.allclose(histogram(a, bins), [0])       
    218217       
    219218        bins = [1,2,3,4]
    220         assert allclose(histogram(a, bins), [8,3,3,1])
     219        assert numpy.allclose(histogram(a, bins), [8,3,3,1])
    221220
    222221        bins = [1.1,2,3.1,4]
    223222        #print histogram(a, bins)
    224         assert allclose(histogram(a, bins), [0,6,0,1])
     223        assert numpy.allclose(histogram(a, bins), [0,6,0,1])
    225224
    226225        bins = [0,1.5,2,3]
    227         assert allclose(histogram(a, bins), [8,0,3,4])
    228         assert allclose(histogram(a, [0,3]), histogram(a, [-0.5,3]))
     226        assert numpy.allclose(histogram(a, bins), [8,0,3,4])
     227        assert numpy.allclose(histogram(a, [0,3]), histogram(a, [-0.5,3]))
    229228
    230229        # Check situation with #bins >= #datapoints
    231230        a = [1.7]
    232231        bins = [0,1.5,2,3]
    233         assert allclose(histogram(a, bins), [0,1,0,0])
     232        assert numpy.allclose(histogram(a, bins), [0,1,0,0])
    234233
    235234        a = [1.7]
    236235        bins = [0]
    237         assert allclose(histogram(a, bins), [1])
     236        assert numpy.allclose(histogram(a, bins), [1])
    238237
    239238        a = [-1.7]
    240239        bins = [0]
    241         assert allclose(histogram(a, bins), [0])
     240        assert numpy.allclose(histogram(a, bins), [0])
    242241
    243242        a = [-1.7]
    244243        bins = [-1.7]
    245         assert allclose(histogram(a, bins), [1])       
     244        assert numpy.allclose(histogram(a, bins), [1])       
    246245       
    247246
  • anuga_core/source_numpy_conversion/anuga/utilities/test_polygon.py

    r5889 r5902  
    33
    44import unittest
    5 from numpy import zeros, array, allclose, alltrue
     5import numpy
    66from math import sqrt, pi
    77from anuga.utilities.numerical_tools import ensure_numeric
     
    4545        f = Polygon_function( [(p1, 1.0)], verbose=False )
    4646        z = f([5, 5, 27, 35], [5, 9, 8, -5]) #Two first inside p1
    47         assert allclose(z, [1,1,0,0])
     47        assert numpy.allclose(z, [1,1,0,0])
    4848
    4949
    5050        f = Polygon_function( [(p2, 2.0)] )
    5151        z = f([5, 5, 27, 35], [5, 9, 8, -5]) # First and last inside p2
    52         assert allclose(z, [2,0,0,2])
     52        assert numpy.allclose(z, [2,0,0,2])
    5353
    5454
     
    5656        f = Polygon_function( [(p1, 1.0), (p2, 2.0)] )
    5757        z = f([5, 5, 27, 35], [5, 9, 8, -5])
    58         assert allclose(z, [2,1,0,2])
     58        assert numpy.allclose(z, [2,1,0,2])
    5959
    6060
     
    7979        z = f([430000,480000], [490000,7720000]) # first outside, second inside
    8080       
    81         assert allclose(z, [0,10])
     81        assert numpy.allclose(z, [0,10])
    8282
    8383    def test_polygon_function_georef(self):
     
    9797        z = f([5, 5, 27, 35], [5, 9, 8, -5]) #Two first inside p1
    9898
    99         assert allclose(z, [1,1,0,0])
     99        assert numpy.allclose(z, [1,1,0,0])
    100100
    101101
    102102        f = Polygon_function( [(p2, 2.0)], geo_reference=geo)
    103103        z = f([5, 5, 27, 35], [5, 9, 8, -5]) #First and last inside p2
    104         assert allclose(z, [2,0,0,2])
     104        assert numpy.allclose(z, [2,0,0,2])
    105105
    106106
     
    108108        f = Polygon_function( [(p1, 1.0), (p2, 2.0)], geo_reference=geo)
    109109        z = f([5, 5, 27, 35], [5, 9, 8, -5])
    110         assert allclose(z, [2,1,0,2])
     110        assert numpy.allclose(z, [2,1,0,2])
    111111
    112112
     
    114114        f = Polygon_function( [(p1, 1.0), (p2, 2.0)])
    115115        z = f([5, 5, 27, 35], [5, 9, 8, -5])
    116         assert not allclose(z, [2,1,0,2])       
     116        assert not numpy.allclose(z, [2,1,0,2])       
    117117
    118118
     
    127127        f = Polygon_function( [(p1, test_function)] )
    128128        z = f([5, 5, 27, 35], [5, 9, 8, -5]) #Two first inside p1
    129         assert allclose(z, [10,14,0,0])
     129        assert numpy.allclose(z, [10,14,0,0])
    130130
    131131        # Combined
    132132        f = Polygon_function( [(p1, test_function), (p2, 2.0)] )
    133133        z = f([5, 5, 27, 35], [5, 9, 8, -5])
    134         assert allclose(z, [2,14,0,2])
     134        assert numpy.allclose(z, [2,14,0,2])
    135135
    136136
     
    138138        f = Polygon_function( [(p1, test_function), (p2, 2.0)], default = 3.14)
    139139        z = f([5, 5, 27, 35], [5, 9, 8, -5])
    140         assert allclose(z, [2,14,3.14,2])
     140        assert numpy.allclose(z, [2,14,3.14,2])
    141141
    142142
     
    145145                              default = test_function)
    146146        z = f([5, 5, 27, 35], [5, 9, 8, -5])
    147         assert allclose(z, [2,14,35,2])
     147        assert numpy.allclose(z, [2,14,35,2])
    148148
    149149
     
    217217        res = inside_polygon(points, polygon)
    218218        assert len(res) == 2
    219         assert allclose(res, [0,1])     ## alltrue?
     219        assert numpy.allclose(res, [0,1])     ## alltrue?
    220220
    221221
     
    312312        res = inside_polygon( points, polygon, verbose=False )
    313313
    314         assert allclose( res, [0,1,2] )
     314        assert numpy.allclose( res, [0,1,2] )
    315315
    316316    def test_outside_polygon(self):
     
    324324       
    325325        indices = outside_polygon( [[0.5, 0.5], [1, -0.5], [0.3, 0.2]], U )
    326         assert allclose( indices, [1] )
     326        assert numpy.allclose( indices, [1] )
    327327       
    328328        # One more test of vector formulation returning indices
     
    331331        res = outside_polygon( points, polygon )
    332332
    333         assert allclose( res, [3, 4] )
     333        assert numpy.allclose( res, [3, 4] )
    334334
    335335
     
    339339        res = outside_polygon( points, polygon )
    340340
    341         assert allclose( res, [0, 4, 5] )       
     341        assert numpy.allclose( res, [0, 4, 5] )       
    342342     
    343343    def test_outside_polygon2(self):
     
    362362        #print indices, count
    363363        assert count == 0 #None inside
    364         assert allclose(indices, [3,2,1,0])
     364        assert numpy.allclose(indices, [3,2,1,0])
    365365
    366366        indices = outside_polygon(points, U, closed = True)
    367         assert allclose(indices, [0,1,2,3])
     367        assert numpy.allclose(indices, [0,1,2,3])
    368368
    369369        indices = inside_polygon(points, U, closed = True)
    370         assert allclose(indices, [])               
     370        assert numpy.allclose(indices, [])               
    371371
    372372
     
    382382        indices, count = separate_points_by_polygon(points, U)
    383383        assert count == 3 #All inside
    384         assert allclose(indices, [0,1,2])
     384        assert numpy.allclose(indices, [0,1,2])
    385385
    386386        indices = outside_polygon(points, U, closed = True)
    387         assert allclose(indices, [])
     387        assert numpy.allclose(indices, [])
    388388
    389389        indices = inside_polygon(points, U, closed = True)
    390         assert allclose(indices, [0,1,2])
     390        assert numpy.allclose(indices, [0,1,2])
    391391       
    392392
     
    395395
    396396        indices, count = separate_points_by_polygon( [[0.5, 0.5], [1, -0.5], [0.3, 0.2]], U )
    397         assert allclose( indices, [0,2,1] )
     397        assert numpy.allclose( indices, [0,2,1] )
    398398        assert count == 2
    399399       
     
    403403        res, count = separate_points_by_polygon( points, polygon )
    404404
    405         assert allclose( res, [0,1,2,4,3] )
     405        assert numpy.allclose( res, [0,1,2,4,3] )
    406406        assert count == 3
    407407
     
    411411        res, count = separate_points_by_polygon( points, polygon )
    412412
    413         assert allclose( res, [1,2,3,5,4,0] )       
     413        assert numpy.allclose( res, [1,2,3,5,4,0] )       
    414414        assert count == 3
    415415       
     
    595595        status, value = intersection(line0, line1)
    596596        assert status == 1
    597         assert allclose(value, [0.0, 0.0])
     597        assert numpy.allclose(value, [0.0, 0.0])
    598598
    599599    def test_intersection2(self):
     
    603603        status, value = intersection(line0, line1)
    604604        assert status == 1
    605         assert allclose(value, [12.0, 6.0])
     605        assert numpy.allclose(value, [12.0, 6.0])
    606606
    607607        # Swap direction of one line
     
    610610        status, value = intersection(line0, line1)
    611611        assert status == 1
    612         assert allclose(value, [12.0, 6.0])
     612        assert numpy.allclose(value, [12.0, 6.0])
    613613
    614614        # Swap order of lines
    615615        status, value = intersection(line1, line0)
    616616        assert status == 1
    617         assert allclose(value, [12.0, 6.0])       
     617        assert numpy.allclose(value, [12.0, 6.0])       
    618618       
    619619    def test_intersection3(self):
     
    623623        status, value = intersection(line0, line1)
    624624        assert status == 1
    625         assert allclose(value, [14.068965517, 7.0344827586])
     625        assert numpy.allclose(value, [14.068965517, 7.0344827586])
    626626
    627627        # Swap direction of one line
     
    630630        status, value = intersection(line0, line1)
    631631        assert status == 1
    632         assert allclose(value, [14.068965517, 7.0344827586])       
     632        assert numpy.allclose(value, [14.068965517, 7.0344827586])       
    633633
    634634        # Swap order of lines
    635635        status, value = intersection(line1, line0)
    636636        assert status == 1       
    637         assert allclose(value, [14.068965517, 7.0344827586])       
     637        assert numpy.allclose(value, [14.068965517, 7.0344827586])       
    638638
    639639
     
    648648        status, value = intersection(line0, line1)
    649649        assert status == 1
    650         assert allclose(value, [1.0, 1.0])
     650        assert numpy.allclose(value, [1.0, 1.0])
    651651
    652652
     
    656656        status, value = intersection(line0, line1)
    657657        assert status == 1
    658         assert allclose(value, [1.0, 1.0])       
     658        assert numpy.allclose(value, [1.0, 1.0])       
    659659       
    660660
     
    686686                                                          common_end_point[1])
    687687            msg += ' gave %s, \nbut when reversed we got %s' %(p1, p2)
    688             assert allclose(p1, p2), msg
     688            assert numpy.allclose(p1, p2), msg
    689689
    690690            # Swap order of lines
     
    692692            assert status == 1                       
    693693            msg = 'Order of lines gave different results'
    694             assert allclose(p1, p3), msg
     694            assert numpy.allclose(p1, p3), msg
    695695           
    696696
     
    732732        status, value = intersection(line0, line1)
    733733        assert status == 2
    734         assert allclose(value, [[0,0], [3,0]])
     734        assert numpy.allclose(value, [[0,0], [3,0]])
    735735
    736736        # Overlap 2
     
    740740        status, value = intersection(line0, line1)
    741741        assert status == 2
    742         assert allclose(value, [[-3, 0], [5,0]])       
     742        assert numpy.allclose(value, [[-3, 0], [5,0]])       
    743743
    744744        # Inclusion 1
     
    748748        status, value = intersection(line0, line1)
    749749        assert status == 2       
    750         assert allclose(value, line1)
     750        assert numpy.allclose(value, line1)
    751751
    752752        # Inclusion 2
     
    756756        status, value = intersection(line0, line1)
    757757        assert status == 2       
    758         assert allclose(value, line0)                                       
     758        assert numpy.allclose(value, line0)                                       
    759759
    760760
     
    775775        status, value = intersection(line0, line1)
    776776        assert status == 2               
    777         assert allclose(value, [[1, 7], [7, 19]])
     777        assert numpy.allclose(value, [[1, 7], [7, 19]])
    778778
    779779        status, value = intersection(line1, line0)
    780780        assert status == 2
    781         assert allclose(value, [[1, 7], [7, 19]])
     781        assert numpy.allclose(value, [[1, 7], [7, 19]])
    782782
    783783        # Swap direction
     
    786786        status, value = intersection(line0, line1)
    787787        assert status == 2
    788         assert allclose(value, [[7, 19], [1, 7]])
     788        assert numpy.allclose(value, [[7, 19], [1, 7]])
    789789
    790790        line0 = [[0,5], [7,19]]
     
    792792        status, value = intersection(line0, line1)
    793793        assert status == 2
    794         assert allclose(value, [[1, 7], [7, 19]])       
     794        assert numpy.allclose(value, [[1, 7], [7, 19]])       
    795795       
    796796
     
    800800        status, value = intersection(line0, line1)
    801801        assert status == 2                       
    802         assert allclose(value, [[1,7], [7, 19]])               
     802        assert numpy.allclose(value, [[1,7], [7, 19]])               
    803803
    804804        line0 = [[0,5], [10,25]]
     
    806806        status, value = intersection(line0, line1)
    807807        assert status == 2                       
    808         assert allclose(value, [[1,7], [7, 19]])
     808        assert numpy.allclose(value, [[1,7], [7, 19]])
    809809
    810810
     
    813813        status, value = intersection(line0, line1)
    814814        assert status == 2                       
    815         assert allclose(value, [[7, 19], [1, 7]])                       
     815        assert numpy.allclose(value, [[7, 19], [1, 7]])                       
    816816       
    817817       
     
    850850        res = inside_polygon(points, polygon)
    851851        assert len(res) == 2
    852         assert allclose(res, [0,1])
     852        assert numpy.allclose(res, [0,1])
    853853
    854854    def test_polygon_area(self):
  • anuga_core/source_numpy_conversion/anuga/utilities/test_quad.py

    r5889 r5902  
    11import unittest
    2 from numpy import array, allclose
     2##import numpy
    33
    44from quad import Cell, build_quadtree
     
    236236                     [ 0.,  -1.]]
    237237                     
    238         # assert allclose(array(results),[[[ 2.,  1.],
     238        # assert numpy.allclose(numpy.array(results),[[[ 2.,  1.],
    239239        #[ 4.,  1.], [ 4.,  4.]], [[ 4.,  1.],[ 5.,  4.],[ 4.,  4.]]] )
    240240        results = Q.search(5,4.)
     
    253253                     [ 5.,  4.],
    254254                     [ 4.,  4.]]
    255         #assert allclose(array(results),[[[ 2.,  1.],[ 4.,  1.], [ 4.,  4.]]
     255        #assert numpy.allclose(numpy.array(results),[[[ 2.,  1.],[ 4.,  1.], [ 4.,  4.]]
    256256         #                               ,[[ 2.,  1.],[ 4.,  4.], [ 2.,  4.]],
    257257        #[[ 4.,  1.],  [ 5.,  4.], [ 4.,  4.]],
  • anuga_core/source_numpy_conversion/anuga/utilities/test_sparse.py

    r5889 r5902  
    55
    66from sparse import *
    7 from numpy.oldnumeric import allclose, array, transpose
     7import numpy
    88
    99class Test_Sparse(unittest.TestCase):
     
    4343        C = Sparse(B)
    4444
    45         assert allclose(C.todense(), B)
     45        assert numpy.allclose(C.todense(), B)
    4646
    4747
     
    5050        A[1,1] = 4
    5151
    52         assert allclose(A.todense(), [[0,0,0], [0,4,0], [0,0,0], [0,0,0]])
     52        assert numpy.allclose(A.todense(), [[0,0,0], [0,4,0], [0,0,0], [0,0,0]])
    5353
    5454
     
    6363
    6464        assert len(A) == 0
    65         assert allclose(A.todense(), [[0,0,0], [0,0,0], [0,0,0]])
     65        assert numpy.allclose(A.todense(), [[0,0,0], [0,0,0], [0,0,0]])
    6666
    6767        #Set an existing zero element to zero
    6868        A[1,2] = 0
    6969        assert len(A) == 0
    70         assert allclose(A.todense(), [[0,0,0], [0,0,0], [0,0,0]])
     70        assert numpy.allclose(A.todense(), [[0,0,0], [0,0,0], [0,0,0]])
    7171
    7272    def test_sparse_multiplication_vector(self):
     
    8282
    8383        u = A*v
    84         assert allclose(u, [6,14,4])
     84        assert numpy.allclose(u, [6,14,4])
    8585
    8686        #Right hand side column
    87         v = array([[2,4],[3,4],[4,4]])
     87        v = numpy.array([[2,4],[3,4],[4,4]])
    8888
    8989        u = A*v[:,0]
    90         assert allclose(u, [6,14,4])
     90        assert numpy.allclose(u, [6,14,4])
    9191
    9292        u = A*v[:,1]
    93         assert allclose(u, [12,16,4])
     93        assert numpy.allclose(u, [12,16,4])
    9494
    9595
     
    103103
    104104        #Right hand side matrix
    105         v = array([[2,4],[3,4],[4,4]])
     105        v = numpy.array([[2,4],[3,4],[4,4]])
    106106
    107107        u = A*v
    108         assert allclose(u, [[6,12], [14,16], [4,4]])
     108        assert numpy.allclose(u, [[6,12], [14,16], [4,4]])
    109109
    110110
     
    122122
    123123        u = A.trans_mult(v)
    124         assert allclose(u, [6,6,10])
     124        assert numpy.allclose(u, [6,6,10])
    125125
    126126
     
    137137
    138138        B = 3*A
    139         assert allclose(B.todense(), 3*A.todense())
     139        assert numpy.allclose(B.todense(), 3*A.todense())
    140140
    141141        B = A*3
    142         assert allclose(B.todense(), 3*A.todense())
     142        assert numpy.allclose(B.todense(), 3*A.todense())
    143143
    144144        try:
     
    166166        C = A+B
    167167
    168         assert allclose(C.todense(), [[12,0,0], [2,8,8], [0,0,4]])
     168        assert numpy.allclose(C.todense(), [[12,0,0], [2,8,8], [0,0,4]])
    169169
    170170    def test_sparse_tocsr(self):
     
    190190        C = [1, 2, 3]
    191191
    192         assert allclose(B*C, [15.0, 10.0 ,8.0, 0.0])
     192        assert numpy.allclose(B*C, [15.0, 10.0 ,8.0, 0.0])
    193193
    194194        C2 = [[1,2],[2,4],[3,6]]
     
    196196        #print B*C2
    197197
    198         assert allclose(B*C2, [[15.0, 30.0],[10.0, 20.0],[8.0, 16.0],[0.0, 0.0]])
     198        assert numpy.allclose(B*C2, [[15.0, 30.0],[10.0, 20.0],[8.0, 16.0],[0.0, 0.0]])
    199199
    200200
  • anuga_core/source_numpy_conversion/anuga/utilities/test_system_tools.py

    r5889 r5902  
    11#!/usr/bin/env python
    22
    3 
    43import unittest
    5 from numpy import zeros, array, allclose, float, dtype
     4import numpy
    65import zlib
    76from os.path import join, split, sep
     
    8180            pass
    8281        else:
    83             test_array = array([[7.0, 3.14], [-31.333, 0.0]])
     82            test_array = numpy.array([[7.0, 3.14], [-31.333, 0.0]])
    8483
    8584            # First file
     
    8786            fid = NetCDFFile(filename1, 'w')
    8887            fid.createDimension('two', 2)
    89             fid.createVariable('test_array', dtype(float).char, ('two', 'two'))
     88            fid.createVariable('test_array', numpy.dtype(numpy.float).char, ('two', 'two'))
    9089            fid.variables['test_array'][:] = test_array
    9190            fid.close()
     
    9594            fid = NetCDFFile(filename2, 'w')
    9695            fid.createDimension('two', 2)
    97             fid.createVariable('test_array', dtype(float).char, ('two', 'two'))
     96            fid.createVariable('test_array', numpy.dtype(numpy.float).char, ('two', 'two'))
    9897            fid.variables['test_array'][:] = test_array
    9998            fid.close()
Note: See TracChangeset for help on using the changeset viewer.