Ignore:
Timestamp:
Jun 30, 2009, 2:07:41 PM (15 years ago)
Author:
ole
Message:

Merged numpy branch back into the trunk.

In ~/sandpit/anuga/anuga_core/source
svn merge -r 6246:HEAD ../../branches/numpy .

In ~/sandpit/anuga/anuga_validation
svn merge -r 6417:HEAD ../branches/numpy_anuga_validation .

In ~/sandpit/anuga/misc
svn merge -r 6809:HEAD ../branches/numpy_misc .

For all merges, I used numpy version where conflicts existed

The suites test_all.py (in source/anuga) and validate_all.py passed using Python2.5 with numpy on my Ubuntu Linux box.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • anuga_core/source/anuga/utilities/numerical_tools.py

    r6534 r7276  
    77from warnings import warn
    88
    9 import Numeric as num
    10 
     9import numpy as num
     10
     11#After having migrated to numpy we should use the native NAN.
     12#num.seterr(divide='warn')
     13num.seterr(divide='ignore') # Ignore division error here for the time being
    1114NAN = (num.array([1])/0.)[0]
    12 # if we use a package that has NAN, this should be updated to use NAN.
    1315
    1416# Static variable used by get_machine_precision
     
    7072    """
    7173 
    72     # Prepare two Numeric vectors
     74    # Prepare two numeric vectors
    7375    if v2 is None:
    7476        v2 = [1.0, 0.0] # Unit vector along the x-axis
    7577       
    76     v1 = ensure_numeric(v1, num.Float)
    77     v2 = ensure_numeric(v2, num.Float)   
     78    v1 = ensure_numeric(v1, num.float)
     79    v2 = ensure_numeric(v2, num.float)   
    7880   
    7981    # Normalise
     
    8284
    8385    # Compute angle
    84     p = num.innerproduct(v1, v2)
    85     c = num.innerproduct(v1, normal_vector(v2)) # Projection onto normal
    86                                                 # (negative cross product)
     86    p = num.inner(v1, v2)
     87    c = num.inner(v1, normal_vector(v2))    # Projection onto normal
     88                                            # (negative cross product)
    8789       
    8890    theta = safe_acos(p)
     
    125127    """
    126128   
    127     return num.array([-v[1], v[0]], num.Float)
     129    return num.array([-v[1], v[0]], num.float)
    128130
    129131   
     
    156158    cy = y - mean(y) 
    157159
    158     p = num.innerproduct(cx,cy)/N
     160    p = num.inner(cx,cy)/N
    159161    return(p)
    160162
     
    206208 
    207209    y = num.ravel(x)
    208     p = num.sqrt(num.innerproduct(y,y))
     210    p = num.sqrt(num.inner(y,y))
    209211    return p
    210212   
     
    230232
    231233
    232        
     234
     235##
     236# @brief Ensure that a sequence is a numeric array of the required type.
     237# @param A The sequence object to convert to a numeric array.
     238# @param typecode The required numeric type of object A (a numeric dtype).
     239# @return A numeric array of the required type.
    233240def ensure_numeric(A, typecode=None):
    234241    """Ensure that sequence is a numeric array.
    235    
     242
    236243    Inputs:
    237         A: Sequence. If A is already a Numeric array it will be returned
     244        A: Sequence. If A is already a numeric array it will be returned
    238245                     unaltered
    239                      If not, an attempt is made to convert it to a Numeric
     246                     If not, an attempt is made to convert it to a numeric
    240247                     array
    241         A: Scalar.   Return 0-dimensional array of length 1, containing that value
    242         A: String.   Array of ASCII values
    243         typecode: Numeric type. If specified, use this in the conversion.
    244                                 If not, let Numeric decide
     248        A: Scalar.   Return 0-dimensional array containing that value. Note
     249                     that a 0-dim array DOES NOT HAVE A LENGTH UNDER numpy.
     250        A: String.   Array of ASCII values (numpy can't handle this)
     251
     252        typecode:    numeric type. If specified, use this in the conversion.
     253                     If not, let numeric package decide.
     254                     typecode will always be one of num.float, num.int, etc.
     255
     256    Note that num.array(A, dtype) will sometimes copy.  Use 'copy=False' to
     257    copy only when required.
    245258
    246259    This function is necessary as array(A) can cause memory overflow.
    247260    """
    248261
     262#    if isinstance(A, basestring):
     263#        msg = 'Sorry, cannot handle strings in ensure_numeric()'
     264#        raise Exception, msg
     265
    249266    if typecode is None:
    250         if type(A) == num.ArrayType:
     267        if isinstance(A, num.ndarray):
    251268            return A
    252269        else:
    253270            return num.array(A)
    254271    else:
    255         if type(A) == num.ArrayType:
    256             if A.typecode() == typecode:
    257                 return A
    258             else:
    259                 return num.array(A, typecode)
    260         else:
    261             return num.array(A, typecode)
    262 
    263 
     272        return num.array(A, dtype=typecode, copy=False)
    264273
    265274
    266275def histogram(a, bins, relative=False):
    267     """Standard histogram straight from the Numeric manual
     276    """Standard histogram straight from the numeric manual
    268277
    269278    If relative is True, values will be normalised againts the total and
     
    272281
    273282    n = num.searchsorted(num.sort(a), bins)
    274     n = num.concatenate( [n, [len(a)]] )
     283    n = num.concatenate([n, [len(a)]], axis=0)    #??default#
    275284
    276285    hist = n[1:]-n[:-1]
     
    358367    return a, b       
    359368
     369################################################################################
     370# Decision functions for numeric package objects.
     371# It is a little tricky to decide if a numpy thing is of type float.
     372# These functions hide numpy-specific details of how we do this.
     373################################################################################
     374
     375##
     376# @brief Decide if an object is a numeric package object with datatype of float.
     377# @param obj The object to decide on.
     378# @return True if 'obj' is a numeric package object, and some sort of float.
     379def is_num_float(obj):
     380    '''Is an object a numeric package float object?'''
     381
     382    try:
     383        return obj.dtype.char in num.typecodes['Float']
     384    except AttributeError:
     385        return False
     386
     387##
     388# @brief Decide if an object is a numeric package object with datatype of int.
     389# @param obj The object to decide on.
     390# @return True if 'obj' is a numeric package object, and some sort of int.
     391def is_num_int(obj):
     392    '''Is an object a numeric package int object?'''
     393
     394    try:
     395        return obj.dtype.char in num.typecodes['Integer']
     396    except AttributeError:
     397        return False
     398
    360399
    361400#-----------------
Note: See TracChangeset for help on using the changeset viewer.