source: inundation/utilities/numerical_tools.py @ 2309

Last change on this file since 2309 was 2040, checked in by ole, 18 years ago

Comments and added 'relative' keyword to err function

File size: 2.6 KB
Line 
1#!/usr/bin/env python
2"""Auxiliary numerical tools
3
4"""
5
6import Numeric
7
8def mean(x):
9    """Mean value of a vector
10    """
11    return(float(Numeric.sum(x))/len(x))
12
13
14def cov(x, y=None):
15    """Covariance of vectors x and y.
16
17    If y is None: return cov(x, x)
18    """
19   
20    if y is None:
21        y = x
22
23    assert(len(x)==len(y))
24    N = len(x)
25 
26    cx = x - mean(x) 
27    cy = y - mean(y) 
28
29    p = Numeric.innerproduct(cx,cy)/N
30    return(p)
31
32
33def err(x, y=0, n=2, relative=True):
34    """Relative error of ||x-y|| to ||y||
35       n = 2:    Two norm
36       n = None: Max norm
37
38       If denominator evaluates to zero or if y is omitted,
39       absolute error is returned
40    """
41
42    x = ensure_numeric(x)
43    if y:
44        y = ensure_numeric(y)       
45
46    if n == 2:
47        err = norm(x-y)
48        if relative is True:
49            try:
50                err = err/norm(y)
51            except:
52                pass
53
54    else:
55        err = max(abs(x-y))
56        if relative is True:
57            try:
58                err = err/max(abs(y))   
59            except:
60                pass
61         
62    return err
63 
64
65def norm(x):
66    """2-norm of x
67    """
68 
69    y = Numeric.ravel(x)
70    p = Numeric.sqrt(Numeric.innerproduct(y,y))
71    return p
72   
73 
74def corr(x, y=None):
75    """Correlation of x and y
76    If y is None return autocorrelation of x
77    """
78
79    from math import sqrt
80    if y is None:
81        y = x
82
83    varx = cov(x)
84    vary = cov(y)
85
86    if varx == 0 or vary == 0:
87        C = 0
88    else: 
89        C = cov(x,y)/sqrt(varx * vary)   
90
91    return(C)
92
93
94       
95def ensure_numeric(A, typecode = None):
96    """Ensure that sequence is a Numeric array.
97    Inputs:
98        A: Sequence. If A is already a Numeric array it will be returned
99                     unaltered
100                     If not, an attempt is made to convert it to a Numeric
101                     array
102        typecode: Numeric type. If specified, use this in the conversion.
103                                If not, let Numeric decide
104
105    This function is necessary as array(A) can cause memory overflow.
106    """
107
108    from Numeric import ArrayType, array
109
110    if typecode is None:
111        if type(A) == ArrayType:
112            return A
113        else:
114            return array(A)
115    else:
116        if type(A) == ArrayType:
117            if A.typecode == typecode:
118                return array(A)  #FIXME: Shouldn't this just return A?
119            else:
120                return A.astype(typecode)
121        else:
122            return array(A).astype(typecode)
123
124
125
Note: See TracBrowser for help on using the repository browser.