Package parsimony :: Package utils :: Module maths
[hide private]
[frames] | no frames]

Module maths

source code

Created on Tue Jul 30 20:55:58 2013

Copyright (c) 2013-2014, CEA/DSV/I2BM/Neurospin. All rights reserved.


Author: Tommy Löfstedt

License: BSD 3-clause.

Functions [hide private]
 
norm(x)
Returns the L2-norm for matrices (i.e.
source code
 
normFro(X)
Returns the Frobenius norm for matrices or the L2-norm for vectors.
source code
 
norm1(x)
Returns the L1-norm or a matrix or vector.
source code
 
norm0(x)
Returns the L0-norm of a vector.
source code
 
normInf(x)
Return the infinity norm of a matrix or vector.
source code
 
corr(a, b)
Example ------- >>> import numpy as np >>> from parsimony.utils.maths import corr >>> v1 = np.asarray([[1., 2., 3.], [1., 2., 3.]]) >>> v2 = np.asarray([[1., 2., 3.], [1., 2., 3.]]) >>> print corr(v1, v2) [[ 1.
source code
 
cov(a, b)
Example ------- >>> import numpy as np >>> from parsimony.utils.maths import cov >>> v1 = np.asarray([[1., 2., 3.], [1., 2., 3.]]) >>> v2 = np.asarray([[1., 2., 3.], [1., 2., 3.]]) >>> print cov(v1, v2) [[ 2.
source code
 
positive(x)
The function
source code
Variables [hide private]
  __package__ = 'parsimony.utils'
Function Details [hide private]

norm(x)

source code 
Returns the L2-norm for matrices (i.e. the Frobenius norm) or vectors.

Examples
--------
>>> import numpy as np
>>> from parsimony.utils.maths import norm
>>> matrix = np.array([[0.2, 1.0, 0.4], [2.0, 1.5, 0.1]])
>>> round(norm(matrix), 15)
2.731300056749533
>>> vector = np.array([[0.2], [1.0], [0.4]])
>>> round(norm(vector), 15)
1.095445115010332

normFro(X)

source code 
Returns the Frobenius norm for matrices or the L2-norm for vectors.

This is an alias for norm(.).

Examples
--------
>>> import numpy as np
>>> from parsimony.utils.maths import norm
>>> matrix = np.array([[0.2, 1.0, 0.4], [2.0, 1.5, 0.1]])
>>> round(norm(matrix), 15)
2.731300056749533
>>> vector = np.array([[0.2], [1.0], [0.4]])
>>> round(norm(vector), 15)
1.095445115010332

norm1(x)

source code 
Returns the L1-norm or a matrix or vector.

For vectors: sum(abs(x)**2)**(1./2)
For matrices: max(sum(abs(x), axis=0))

Examples
--------
>>> from parsimony.utils.maths import norm1
>>> matrix = np.array([[0.2, 1.0, 0.4], [2.0, 1.5, 0.1]])
>>> norm1(matrix)
2.5
>>> vector = np.array([[0.2], [1.0], [0.4]])
>>> norm1(vector)
1.6000000000000001

norm0(x)

source code 
Returns the L0-norm of a vector.

Examples
--------
>>> from parsimony.utils.maths import norm0
>>> matrix = np.array([[0.2, 1.0, 0.4], [2.0, 1.5, 0.1]])
>>> norm0(matrix)
Traceback (most recent call last):
    ...
ValueError: The L0 norm is not defined for matrices.
>>> vector = np.array([[0.2], [1.0], [0.4]])
>>> norm0(vector)
3

normInf(x)

source code 
Return the infinity norm of a matrix or vector.

For vectors : max(abs(x))
For matrices : max(sum(abs(x), axis=1))

Examples
--------
>>> from parsimony.utils.maths import normInf
>>> matrix = np.array([[0.2, 1.0, 0.4], [2.0, 1.5, 0.1]])
>>> normInf(matrix)
3.6000000000000001
>>> vector = np.array([[0.2], [1.0], [0.4]])
>>> normInf(vector)
1.0

corr(a, b)

source code 

Example
-------
>>> import numpy as np
>>> from parsimony.utils.maths import corr
>>> v1 = np.asarray([[1., 2., 3.], [1., 2., 3.]])
>>> v2 = np.asarray([[1., 2., 3.], [1., 2., 3.]])
>>> print corr(v1, v2)
[[ 1.  0. -1.]
 [ 0.  0.  0.]
 [-1.  0.  1.]]

cov(a, b)

source code 

Example
-------
>>> import numpy as np
>>> from parsimony.utils.maths import cov
>>> v1 = np.asarray([[1., 2., 3.], [1., 2., 3.]])
>>> v2 = np.asarray([[1., 2., 3.], [1., 2., 3.]])
>>> print cov(v1, v2)
[[ 2.  0. -2.]
 [ 0.  0.  0.]
 [-2.  0.  2.]]

positive(x)

source code 
The function

    max(x, 0).

Returns a numpy array.

Example
-------
>>> import numpy as np
>>> from parsimony.utils.maths import positive
>>> np.maximum([1,-1,2], [0,0,0])
array([1, 0, 2])