Changeset 5893


Ignore:
Timestamp:
Nov 5, 2008, 5:26:26 PM (16 years ago)
Author:
rwilson
Message:

Initial numpy conversion.

Location:
anuga_core/source/anuga/caching
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • anuga_core/source/anuga/caching/caching.py

    r5860 r5893  
    5050else:
    5151  unix = 1
     52
     53import numpy
     54
    5255
    5356cache_dir = '.python_cache'
     
    13391342
    13401343  from types import TupleType, ListType, DictType, InstanceType 
    1341   from Numeric import ArrayType, average
     1344##  from numpy.oldnumeric import ArrayType, average
    13421345
    13431346   
     
    13781381      I.sort()   
    13791382      val = myhash(I, ids)
    1380   elif type(T) == ArrayType:
     1383  elif isinstance(T, numpy.ndarray):
    13811384      # Use mean value for efficiency 
    1382       val = hash(average(T.flat))
     1385      val = hash(numpy.average(T.ravel()))
    13831386  elif type(T) == InstanceType:
    13841387      val = myhash(T.__dict__, ids)
     
    14041407
    14051408    from types import TupleType, ListType, DictType, InstanceType
    1406     from Numeric import ArrayType, alltrue   
     1409##    from numpy.oldnumeric import ArrayType, alltrue   
    14071410   
    14081411    # Keep track of unique id's to protect against infinite recursion
     
    14471450            identical = compare(a, b, ids)
    14481451           
    1449     elif type(A) == ArrayType:
     1452    elif isinstance(A, numpy.ndarray):
    14501453        # Use element by element comparison
    1451         identical = alltrue(A==B)
     1454        identical = numpy.alltrue(A==B)
    14521455
    14531456    elif type(A) == InstanceType:
     
    23962399      argstr = argstr + "'"+str(args)+"'"
    23972400    else:
    2398       # Truncate large Numeric arrays before using str()
    2399       import Numeric
    2400       if type(args) == Numeric.ArrayType:
    2401 #        if len(args.flat) > textwidth: 
    2402 #        Changed by Duncan and Nick 21/2/07 .flat has problems with
    2403 #        non-contigous arrays and ravel is equal to .flat except it
     2401      # Truncate large arrays before using str()
     2402      if isinstance(args, numpy.ndarray):
     2403#        if len(args.ravel()) > textwidth: 
     2404#        Changed by Duncan and Nick 21/2/07 .ravel() has problems with
     2405#        non-contigous arrays and ravel is equal to .ravel() except it
    24042406#        can work with non-contiguous  arrays
    2405         if len(Numeric.ravel(args)) > textwidth:
     2407        if len(numpy.ravel(args)) > textwidth:
    24062408          args = 'Array: ' + str(args.shape)
    24072409
  • anuga_core/source/anuga/caching/test_caching.py

    r5860 r5893  
    1 
    21import unittest
    3 from Numeric import arange, array
     2import numpy
    43
    54from copy import deepcopy
     
    128127       
    129128        # Make some test input arguments
    130         A0 = arange(5)
    131         B0 = array([1.1, 2.2, 0.0, -5, -5])
     129        A0 = numpy.arange(5)
     130        B0 = numpy.array([1.1, 2.2, 0.0, -5, -5])
    132131       
    133132        A1 = A0.copy()
     
    159158
    160159
    161             assert T1 == T2, 'Cached result does not match computed result'
    162             assert T2 == T3, 'Cached result does not match computed result'
     160            assert numpy.alltrue(T1 == T2), 'Cached result does not match computed result'
     161            assert numpy.alltrue(T2 == T3), 'Cached result does not match computed result'
    163162           
    164163
     
    172171       
    173172        # Make test input arguments
    174         A0 = arange(5)*1.0
     173        A0 = numpy.arange(5)*1.0
    175174        B = ('x', 15)
    176175       
    177176        # Create different A that hashes to the same address (having the same average)
    178         A1 = array([2.0, 2.0, 2.0, 2.0, 2.0])       
     177        A1 = numpy.array([2.0, 2.0, 2.0, 2.0, 2.0])       
    179178       
    180179        assert myhash(A0) == myhash(A1)
     
    203202           
    204203
    205             #print T1
    206             #print T2
    207             assert T2 != T1
     204            print 'T1=%s' % str(T1)
     205            print 'T2=%s' % str(T2)
     206            assert numpy.alltrue(T2 != T1)
    208207
    209208           
     
    389388        A = Dummy(5, 7)
    390389        B = {'x': 10, 'A': A}
    391         C = [B, array([1.2, 3, 5, 0.1])]
     390        C = [B, numpy.array([1.2, 3, 5, 0.1])]
    392391        A.value = C # Make it circular
    393392
     
    395394        AA = Dummy(None, None)
    396395        BB = {'A': AA, 'x': 10}
    397         CC = [BB, array([1.200, 3.000, 5.00, 1.0/10])]
     396        CC = [BB, numpy.array([1.200, 3.000, 5.00, 1.0/10])]
    398397        AA.value = CC # Make it circular
    399398        AA.another = 3+4       
Note: See TracChangeset for help on using the changeset viewer.