1
2 """
3 Created on Tue Jul 30 20:55:58 2013
4
5 Copyright (c) 2013-2014, CEA/DSV/I2BM/Neurospin. All rights reserved.
6
7 @author: Tommy Löfstedt
8 @email: tommy.loefstedt@cea.fr
9 @license: BSD 3-clause.
10 """
11 import numpy as np
12
13 from parsimony.utils.consts import TOLERANCE
14 from . import linalgs
15
16 __all__ = ["norm", "normFro", "norm1", "norm0", "normInf", "corr", "cov",
17 "positive"]
18
19
21 """Returns the L2-norm for matrices (i.e. the Frobenius norm) or vectors.
22
23 Examples
24 --------
25 >>> import numpy as np
26 >>> from parsimony.utils.maths import norm
27 >>> matrix = np.array([[0.2, 1.0, 0.4], [2.0, 1.5, 0.1]])
28 >>> round(norm(matrix), 15)
29 2.731300056749533
30 >>> vector = np.array([[0.2], [1.0], [0.4]])
31 >>> round(norm(vector), 15)
32 1.095445115010332
33 """
34 n, p = x.shape
35 if p == 1:
36 if isinstance(x, linalgs.MultipartArray):
37 return np.sqrt(x.T.dot(x))[0, 0]
38 else:
39 return np.sqrt(np.dot(x.T, x))[0, 0]
40 elif n == 1:
41 if isinstance(x, linalgs.MultipartArray):
42 return np.sqrt(x.dot(x.T))[0, 0]
43 else:
44 return np.sqrt(np.dot(x, x.T))[0, 0]
45 else:
46 return np.linalg.norm(x)
47
48
50 """Returns the Frobenius norm for matrices or the L2-norm for vectors.
51
52 This is an alias for norm(.).
53
54 Examples
55 --------
56 >>> import numpy as np
57 >>> from parsimony.utils.maths import norm
58 >>> matrix = np.array([[0.2, 1.0, 0.4], [2.0, 1.5, 0.1]])
59 >>> round(norm(matrix), 15)
60 2.731300056749533
61 >>> vector = np.array([[0.2], [1.0], [0.4]])
62 >>> round(norm(vector), 15)
63 1.095445115010332
64 """
65 return norm(X)
66
67
69 """Returns the L1-norm or a matrix or vector.
70
71 For vectors: sum(abs(x)**2)**(1./2)
72 For matrices: max(sum(abs(x), axis=0))
73
74 Examples
75 --------
76 >>> from parsimony.utils.maths import norm1
77 >>> matrix = np.array([[0.2, 1.0, 0.4], [2.0, 1.5, 0.1]])
78 >>> norm1(matrix)
79 2.5
80 >>> vector = np.array([[0.2], [1.0], [0.4]])
81 >>> norm1(vector)
82 1.6000000000000001
83 """
84 n, p = x.shape
85 if p == 1 or n == 1:
86 return np.sum(np.abs(x))
87 else:
88 return np.max(np.sum(np.abs(x), axis=0))
89
90
91
93 """Returns the L0-norm of a vector.
94
95 Examples
96 --------
97 >>> from parsimony.utils.maths import norm0
98 >>> matrix = np.array([[0.2, 1.0, 0.4], [2.0, 1.5, 0.1]])
99 >>> norm0(matrix)
100 Traceback (most recent call last):
101 ...
102 ValueError: The L0 norm is not defined for matrices.
103 >>> vector = np.array([[0.2], [1.0], [0.4]])
104 >>> norm0(vector)
105 3
106 """
107 n, p = x.shape
108 if n > 1 and p > 1:
109 raise ValueError("The L0 norm is not defined for matrices.")
110
111 return np.sum(x != 0)
112
113
114
115
117 """Return the infinity norm of a matrix or vector.
118
119 For vectors : max(abs(x))
120 For matrices : max(sum(abs(x), axis=1))
121
122 Examples
123 --------
124 >>> from parsimony.utils.maths import normInf
125 >>> matrix = np.array([[0.2, 1.0, 0.4], [2.0, 1.5, 0.1]])
126 >>> normInf(matrix)
127 3.6000000000000001
128 >>> vector = np.array([[0.2], [1.0], [0.4]])
129 >>> normInf(vector)
130 1.0
131 """
132 n, p = x.shape
133 if p == 1 or n == 1:
134 return np.max(np.abs(x))
135 else:
136 return np.max(np.sum(np.abs(x), axis=1))
137
138
139
141 """
142 Example
143 -------
144 >>> import numpy as np
145 >>> from parsimony.utils.maths import corr
146 >>> v1 = np.asarray([[1., 2., 3.], [1., 2., 3.]])
147 >>> v2 = np.asarray([[1., 2., 3.], [1., 2., 3.]])
148 >>> print corr(v1, v2)
149 [[ 1. 0. -1.]
150 [ 0. 0. 0.]
151 [-1. 0. 1.]]
152 """
153 ma = np.mean(a)
154 mb = np.mean(b)
155
156 a_ = a - ma
157 b_ = b - mb
158
159 norma = np.sqrt(np.sum(a_ ** 2.0, axis=0))
160 normb = np.sqrt(np.sum(b_ ** 2.0, axis=0))
161
162 norma[norma < TOLERANCE] = 1.0
163 normb[normb < TOLERANCE] = 1.0
164
165 a_ *= 1.0 / norma
166 b_ *= 1.0 / normb
167
168 ip = np.dot(a_.T, b_)
169
170 if ip.shape == (1, 1):
171 return ip[0, 0]
172 else:
173 return ip
174
175
177 """
178 Example
179 -------
180 >>> import numpy as np
181 >>> from parsimony.utils.maths import cov
182 >>> v1 = np.asarray([[1., 2., 3.], [1., 2., 3.]])
183 >>> v2 = np.asarray([[1., 2., 3.], [1., 2., 3.]])
184 >>> print cov(v1, v2)
185 [[ 2. 0. -2.]
186 [ 0. 0. 0.]
187 [-2. 0. 2.]]
188 """
189 ma = np.mean(a)
190 mb = np.mean(b)
191
192 a_ = a - ma
193 b_ = b - mb
194
195 ip = np.dot(a_.T, b_) * (1.0 / (a_.shape[0] - 1.0))
196
197 if ip.shape == (1, 1):
198 return ip[0, 0]
199 else:
200 return ip
201
202
204 """The function
205
206 max(x, 0).
207
208 Returns a numpy array.
209
210 Example
211 -------
212 >>> import numpy as np
213 >>> from parsimony.utils.maths import positive
214 >>> np.maximum([1,-1,2], [0,0,0])
215 array([1, 0, 2])
216 """
217 return np.maximum(x, 0.0)
218
219
220 if __name__ == "__main__":
221 import doctest
222 doctest.testmod()
223