Package Biskit :: Module mathUtils
[hide private]
[frames] | no frames]

Module mathUtils

source code

general purpose math methods

Classes [hide private]
  MathUtilError
  Test
Test case

Functions [hide private]
float accumulate(a)
cumulative sum of a[0], a[0]+a[1], a[0]+a[1]+[a2], ... normalized by N.sum( a )
float variance(x, avg=None)
Variance, σ^2
float SD(x, avg=None)
Standard deviation, σ
array('f') or float wMean(x, w=None)
Weighted mean: Mean of data (x) weighted by (w).
array('f') or float wVar(x, w)
Variance of weighted (w) data (x).
array('f') or float wSD(x, w)
Standard deviation of weighted data.
array('f') or float cross(v, w)
Cross product between two coordinates.
list aboveDiagonal(pw_m)
Collect att the values above the diagonal in a square matrix.
1|0 arrayEqual(a, b)
Compare 2 arrays or lists of numbers for equality.
array pairwiseDistances(u, v)
Pairwise distances between two arrays.
array( 1|0 ) randomMask(nOnes, length)
Create random array of given lenght and number of ones.
2D array random2DArray(matrix, ranNr=1, mask=None)
Create randomized 2D array containing ones and zeros.
[ float ] runningAverage(x, interval=2, preserve_boundaries=0)
Running average (smoothing) over a given data window.
dict packBinaryMatrix(cm)
Compress sparse array of 0 and ones to list of one-positions (space saving function, upack with unpackBinaryMatrix).
2D array unpackBinaryMatrix(pcm, raveled=0)
Uncompress array of 0 and 1 that was compressed with packBinaryMatrix.
dict matrixToList(cm)
Convert matrix into standard python list remembering the dimensions.
  listToMatrix(lcm)
Convert result of matrixToList back into Numeric array
array eulerRotation(alpha, beta, gamma)
Builds a rotation matrix from successive rotations:
array randomRotation()
Get random rotation matrix.
[any] intersection(a, b, optimize=1)
Intersection of the two lists (i.e.
[any] nonredundant(l)
All members of a list without duplicates noredundant( [any] ) -> [any]
[any] union(a, b)
Union of two lists (without duplicates) union( [any], [any] ) -> [any]
[any] difference(a, b)
All members of a that are not in b difference([any], [any]) -> [any]
[any] removeFromList(l, v, all=1)
Remove all or first occurrence(s) of v from l.
[int] randomRange(start, stop, n)
Creates a set of n unique integers randomly distributed between start and stop.
float, float, float linfit(x, y)
Calculate linear least-square fit to the points given by x and y.
array cartesianToPolar(xyz)
Convert cartesian coordinate array to polar coordinate array: x,y,z -> r, θ, φ
array polarToCartesian(rtp)
Convert polar coordinate array to cartesian coordinate array: r, θ, φ -> x,y,z
array projectOnSphere(xyz, radius=None, center=None)
Project the coordinates xyz on a sphere with a given radius around a given center.

Function Details [hide private]

accumulate(a)

source code 

cumulative sum of a[0], a[0]+a[1], a[0]+a[1]+[a2], ... normalized by N.sum( a )
Parameters:
  • a (array) - array('f') or float
Returns: float
float

variance(x, avg=None)

source code 

Variance, σ^2
Parameters:
  • x (array('f') or float) - data
  • avg (float OR None) - use this average, otherwise calculated from x
Returns: float
float

SD(x, avg=None)

source code 

Standard deviation, σ
Parameters:
  • x (array('f') or float) - data
  • avg (float OR None) - use this average, otherwise calculated from x
Returns: float
float

wMean(x, w=None)

source code 

Weighted mean: Mean of data (x) weighted by (w).
Parameters:
  • x (array) - X-D array with numbers
  • w (array) - 1-D array of same length as x with weight factors
Returns: array('f') or float
array('f') or float

wVar(x, w)

source code 

Variance of weighted (w) data (x).
Parameters:
  • x (array) - X-D array with numbers
  • w (array) - 1-D array of same length as x with weight factors
Returns: array('f') or float
array('f') or float

wSD(x, w)

source code 

Standard deviation of weighted data.
Parameters:
  • x (array) - X-D array with numbers
  • w (array) - 1-D array of same length as x with weight factors
Returns: array('f') or float
array('f') or float

cross(v, w)

source code 

Cross product between two coordinates.
Parameters:
  • v (list or array) - coordinate vector
  • w (list or array) - coordinate vector
Returns: array('f') or float
array('f') or float

aboveDiagonal(pw_m)

source code 

Collect att the values above the diagonal in a square matrix.
Parameters:
  • pw_m (2-D array) - symmetric square matrix
Returns: list
raveled list of 'unique' values without diagonal

arrayEqual(a, b)

source code 

Compare 2 arrays or lists of numbers for equality.
Parameters:
  • a (array / list) - first array (multi-dimensional is supported)
  • b (array / list) - second array (multi-dimensional is supported)
Returns: 1|0
1 if array/list a equals array/list b

pairwiseDistances(u, v)

source code 

Pairwise distances between two arrays.
Parameters:
  • u (array) - first array
  • v (array) - second array
Returns: array
Numeric.array( len(u) x len(v) ) of double

randomMask(nOnes, length)

source code 

Create random array of given lenght and number of ones.
Parameters:
  • nOnes (int) - number of ones
  • length (int) - lenght of array
Returns: array( 1|0 )
array with ones and zeros

random2DArray(matrix, ranNr=1, mask=None)

source code 

Create randomized 2D array containing ones and zeros.
Parameters:
  • matrix (2D array) - matrix to randomize
  • mask (list(1|0)) - mask OR None (default: None)
  • ranNr (integer) - number of matricies to add up (default: 1)
Returns: 2D array
2D array or |ranNr| added contact matricies
Raises:

runningAverage(x, interval=2, preserve_boundaries=0)

source code 

Running average (smoothing) over a given data window.
Parameters:
  • x (list of int/float) - data
  • interval (int) - window size (-(interval-1)/2 to +(interval-1)/2) (default: 2)
  • preserve_boundaries (0|1) - shrink window at edges to keep original start and end value (default: 0)
Returns: [ float ]
list of floats

packBinaryMatrix(cm)

source code 

Compress sparse array of 0 and ones to list of one-positions (space saving function, upack with unpackBinaryMatrix).
Parameters:
  • cm (2D array) - X by Y array of int
Returns: dict
{'shape':(X,Y), 'nonzero':[int] }

unpackBinaryMatrix(pcm, raveled=0)

source code 

Uncompress array of 0 and 1 that was compressed with packBinaryMatrix.
Parameters:
  • pcm (dict) - {'shape':(X,Y,..), 'nonzero':[int]}
  • raveled (1|0) - return raveled (default: 0)
Returns: 2D array
N.array(X by Y by ..) of int

matrixToList(cm)

source code 

Convert matrix into standard python list remembering the dimensions. Unpack with listToMatrix.
Parameters:
  • cm (2D array) - array of int
Returns: dict
{'shape':(int,..), 'lst':[..] }

Note: Not used right now.

listToMatrix(lcm)

source code 

Convert result of matrixToList back into Numeric array
Parameters:
  • lcm (dict) - {'shape':(int,..), 'lst':[..] }
Returns:
Numeric.array

Note: Not used right now.

eulerRotation(alpha, beta, gamma)

source code 

Builds a rotation matrix from successive rotations:
  • rotation about y-axis by angle alpha
  • rotation about x-axis by angle beta
  • rotation about z-axis by angle gamma
  • Parameters:
    • alpha (float) - euler angle α
    • beta (float) - euler angle β
    • gamma (float) - euler angle γ
    Returns: array
    3 x 3 array of float

    Author: Michael Habeck

    randomRotation()

    source code 

    Get random rotation matrix.
    Returns: array
    3 x 3 array of float

    Author: Michael Habeck

    intersection(a, b, optimize=1)

    source code 

    Intersection of the two lists (i.e. all values common to both two lists). intersection( [any], [any] ) -> [any]
    Parameters:
    • a ([any]) - first list
    • b ([any]) - second list
    • optimize (1|0) - result is sorted like the shorter of the two lists (default: 1)
    Returns: [any]
    list

    nonredundant(l)

    source code 

    All members of a list without duplicates noredundant( [any] ) -> [any]
    Parameters:
    • l ([any]) - list
    Returns: [any]
    list

    union(a, b)

    source code 

    Union of two lists (without duplicates) union( [any], [any] ) -> [any]
    Parameters:
    • a ([any]) - first list
    • b ([any]) - second list
    Returns: [any]
    list

    difference(a, b)

    source code 

    All members of a that are not in b difference([any], [any]) -> [any]
    Parameters:
    • a ([any]) - first list
    • b ([any]) - second list
    Returns: [any]
    list

    removeFromList(l, v, all=1)

    source code 

    Remove all or first occurrence(s) of v from l. removeFromList( l, v, [all=1] )
    Parameters:
    • l ([ any ]) - list
    • v (any or [ any ]) - remove these values
    • all (0|1) - remove all occurrences (1) or only first one (0) (default: 1)
    Returns: [any]
    list

    randomRange(start, stop, n)

    source code 

    Creates a set of n unique integers randomly distributed between start and stop.
    Parameters:
    • start (int) - minimal index
    • stop (int) - 1+maximal index
    • n (int) - number of indices
    Returns: [int]
    set of unique integers evenly distributed between start and stop

    linfit(x, y)

    source code 

    Calculate linear least-square fit to the points given by x and y. see http://mathworld.wolfram.com/LeastSquaresFitting.html
    Parameters:
    • x ([ float ]) - x-data
    • y ([ float ]) - y-data
    Returns: float, float, float
    float, float - m, n, r^2 (slope, intersection, corr. coefficient)
    Raises:
    • BiskitError - if x and y have different number of elements

    cartesianToPolar(xyz)

    source code 

    Convert cartesian coordinate array to polar coordinate array: x,y,z -> r, θ, φ
    Parameters:
    • xyz (array) - array of cartesian coordinates (x, y, z)
    Returns: array
    array of polar coordinates (r, theta, phi)

    polarToCartesian(rtp)

    source code 

    Convert polar coordinate array to cartesian coordinate array: r, θ, φ -> x,y,z
    Parameters:
    • rtp (array) - array of cartesian coordinates (r, theta, phi)
    Returns: array
    array of cartesian coordinates (x, y, z)

    projectOnSphere(xyz, radius=None, center=None)

    source code 

    Project the coordinates xyz on a sphere with a given radius around a given center.
    Parameters:
    • xyz (array N x 3 of float) - cartesian coordinates
    • radius (float) - radius of target sphere, if not provided the maximal distance to center will be used (default: None)
    • center (array 0 x 3 of float) - center of the sphere, if not given the average of xyz will be assigned to the center (default: None)
    Returns: array
    array of cartesian coordinates (x, y, z)