Ignore:
Timestamp:
Feb 10, 2009, 11:11:04 AM (15 years ago)
Author:
rwilson
Message:

Initial commit of numpy changes. Still a long way to go.

Location:
branches/numpy
Files:
1 edited
1 copied

Legend:

Unmodified
Added
Removed
  • branches/numpy/anuga/utilities/numerical_tools.py

    r6174 r6304  
    77from warnings import warn
    88
    9 import Numeric as num
     9import numpy as num
    1010
    1111NAN = (num.array([1])/0.)[0]
     
    7070    """
    7171 
    72     # Prepare two Numeric vectors
     72    # Prepare two numeric vectors
    7373    if v2 is None:
    7474        v2 = [1.0, 0.0] # Unit vector along the x-axis
    7575       
    76     v1 = ensure_numeric(v1, num.Float)
    77     v2 = ensure_numeric(v2, num.Float)   
     76    v1 = ensure_numeric(v1, num.float)
     77    v2 = ensure_numeric(v2, num.float)   
    7878   
    7979    # Normalise
     
    8282
    8383    # Compute angle
    84     p = num.innerproduct(v1, v2)
    85     c = num.innerproduct(v1, normal_vector(v2)) # Projection onto normal
     84    p = num.inner(v1, v2)
     85    c = num.inner(v1, normal_vector(v2)) # Projection onto normal
    8686                                            # (negative cross product)
    8787       
     
    125125    """
    126126   
    127     return num.array([-v[1], v[0]], num.Float)
     127    return num.array([-v[1], v[0]], num.float)
    128128
    129129   
     
    156156    cy = y - mean(y) 
    157157
    158     p = num.innerproduct(cx,cy)/N
     158    p = num.inner(cx,cy)/N
    159159    return(p)
    160160
     
    206206 
    207207    y = num.ravel(x)
    208     p = num.sqrt(num.innerproduct(y,y))
     208    p = num.sqrt(num.inner(y,y))
    209209    return p
    210210   
     
    230230
    231231
    232        
    233 def ensure_numeric(A, typecode = None):
    234     """Ensure that sequence is a Numeric array.
     232
     233##
     234# @brief Ensure that a sequence is a numeric array of the required type.
     235# @param A The sequence object to convert to a numeric array.
     236# @param typecode The required numeric type of object A (a numeric dtype).
     237# @return A numeric array of the required type.
     238def ensure_numeric(A, typecode=None):
     239    """Ensure that sequence is a numeric array.
    235240    Inputs:
    236         A: Sequence. If A is already a Numeric array it will be returned
     241        A: Sequence. If A is already a numeric array it will be returned
    237242                     unaltered
    238                      If not, an attempt is made to convert it to a Numeric
     243                     If not, an attempt is made to convert it to a numeric
    239244                     array
    240         A: Scalar.   Return 0-dimensional array of length 1, containing that value
    241         A: String.   Array of ASCII values
    242         typecode: Numeric type. If specified, use this in the conversion.
    243                                 If not, let Numeric decide
     245        A: Scalar.   Return 0-dimensional array containing that value. Note
     246                     that a 0-dim array DOES NOT HAVE A LENGTH UNDER numpy.
     247        A: String.   Array of ASCII values (numpy can't handle this)
     248
     249        typecode:    numeric type. If specified, use this in the conversion.
     250                     If not, let numeric package decide.
     251                     numpy assumes float64 if no type in A.
    244252
    245253    This function is necessary as array(A) can cause memory overflow.
    246254    """
    247255
     256    if isinstance(A, basestring):
     257        msg = 'Sorry, cannot handle string in ensure_numeric()'
     258        raise Exception, msg
     259
    248260    if typecode is None:
    249         if type(A) == num.ArrayType:
     261        if isinstance(A, num.ndarray):
    250262            return A
    251263        else:
    252264            return num.array(A)
    253265    else:
    254         if type(A) == num.ArrayType:
    255             if A.typecode == typecode:
     266        if isinstance(A, num.ndarray):
     267            if A.dtype == typecode:
    256268#                return num.array(A)  #FIXME: Shouldn't this just return A?
    257269                return A
    258270            else:
    259                 return num.array(A,typecode)
     271                return num.array(A, dtype=typecode)
    260272        else:
    261             return num.array(A,typecode)
     273            return num.array(A, dtype=typecode)
    262274
    263275
     
    265277
    266278def histogram(a, bins, relative=False):
    267     """Standard histogram straight from the Numeric manual
     279    """Standard histogram straight from the numeric manual
    268280
    269281    If relative is True, values will be normalised againts the total and
     
    358370    return a, b       
    359371
     372################################################################################
     373# Decision functions for numeric package objects.
     374# It is a little tricky to decide if a numpy thing is of type float.
     375# These functions hide numpy-specific details of how we do this.
     376################################################################################
     377
     378##
     379# @brief Decide if an object is a numeric package object with datatype of float.
     380# @param obj The object to decide on.
     381# @return True if 'obj' is a numeric package object, and some sort of float.
     382def is_num_float(obj):
     383    '''Is an object a numeric package float object?'''
     384
     385    try:
     386        return obj.dtype.char in num.typecodes['Float']
     387    except AttributeError:
     388        return False
     389
     390##
     391# @brief Decide if an object is a numeric package object with datatype of int.
     392# @param obj The object to decide on.
     393# @return True if 'obj' is a numeric package object, and some sort of int.
     394def is_num_int(obj):
     395    '''Is an object a numeric package int object?'''
     396
     397    try:
     398        return obj.dtype.char in num.typecodes['Integer']
     399    except AttributeError:
     400        return False
     401
    360402
    361403#-----------------
Note: See TracChangeset for help on using the changeset viewer.