Miscellaneous functions

class SpectralToolbox.Misc.ExpandingArray(initData, allocInitDim=None, dtype=<class 'numpy.float64'>, maxIncrement=None)[source]

Bases: object

ExpandingArray is used for the dynamic allocation of memory in applications where the total allocated memory needed cannot be predicted. Memory is preallocated with increases of 50% all the time data exceed the allocated memory.

Initialization of the Expanding Array.

>>> EA = ExpandingArray(initData,[allocInitDim=None,[dtype=np.float64,[maxIncrement=None]]])
Parameters:
  • initData (ndarray) – InitialData with which to be initially filled. This must provide the number of dimensions of the array
  • allocInitDim (1darray-integer) – Initial allocated dimension (optional)
  • dtype (dtype) – type for the data that will be contained in EA (optional,default=np.float64)
  • maxIncrement (integer) – upper limit for the allocation increment
concatenate(X, axis=0)[source]

Concatenate data to the existing array. If needed the array is resized in the axis direction by a factor of 50%.

Parameters:
  • X (ndarray) – data to be concatenated to the array. Note that X.shape[i]==EA.shape()[i] is required for all i!=axis
  • axis (integer) – axis along which to concatenate the additional data (optional)
>>> import numpy as np
>>> EA = Misc.ExpandingArray(np.random.rand(25,4))
>>> EA.shape()
(25, 4)
>>> EA.getAllocArray().shape
(25, 4)
>>> EA.concatenate(np.random.rand(13,4))
>>> EA.shape()
(38, 4)
getAllocArray()[source]

Return the allocated array.

Returns:allocated array
Return type:ndarray
>>> import numpy as np
>>> EA = Misc.ExpandingArray(np.random.rand(25,4))
>>> EA.shape()
(25, 4)
>>> EA.getAllocArray().shape
(25, 4)
>>> EA.concatenate(np.random.rand(13,4))
>>> EA.shape()
(38, 4)
>>> EA.getAllocArray().shape
(45, 4)
getDataArray()[source]

Get the view of the array with data.

Returns:allocated array
Return type:ndarray
>>> EA.shape()
(38, 4)
>>> EA.getAllocArray().shape
(45, 4)
>>> EA.getDataArray().shape
(38, 4)
shape()[source]

Returns the shape of the data inside the array. Note that the allocated memory is always bigger or equal to shape().

Returns:shape of the data
Return type:tuple of integer
>>> import numpy as np
>>> EA = Misc.ExpandingArray(np.random.rand(25,4))
>>> EA.shape()
(25, 4)
>>> EA.getAllocArray().shape
(25, 4)
>>> EA.concatenate(np.random.rand(13,4))
>>> EA.shape()
(38, 4)
>>> EA.getAllocArray().shape
(45, 4)
trim(N, axis=0)[source]

Trim the axis dimension of N elements. The allocated data is not reinitialized or deallocated. Only the dimensions of the view are redefined.

Parameters:
  • N (integer) – number of elements to be removed along the axis dimension
  • axis (integer) – axis along which to remove elements (optional)
>>> EA = Misc.ExpandingArray(np.random.rand(4,2))
>>> EA.getDataArray()
array([[ 0.42129746,  0.76220921],
       [ 0.9238783 ,  0.11256142],
       [ 0.42031437,  0.87349243],
       [ 0.83187297,  0.555708  ]])
>>> EA.trim(2,axis=0)
>>> EA.getDataArray()
array([[ 0.42129746,  0.76220921],
       [ 0.9238783 ,  0.11256142]])
>>> EA.getAllocArray()
array([[ 0.42129746,  0.76220921],
       [ 0.9238783 ,  0.11256142],
       [ 0.42031437,  0.87349243],
       [ 0.83187297,  0.555708  ]])
SpectralToolbox.Misc.MultiIndex(d, N)[source]

Generates the multi index ordering for the construction of multidimensional Generalized Vandermonde matrices

Parameters:
  • d (integer) – dimension of the simplex
  • N (integer) – maximum value of the sum of the indices
Returns:

array containing the ordered multi-indices

Return type:

2d-array of integer

>>> Misc.MultiIndex(2,3)
array([[0, 0],
       [1, 0],
       [0, 1],
       [2, 0],
       [1, 1],
       [0, 2],
       [3, 0],
       [2, 1],
       [1, 2],
       [0, 3]])
SpectralToolbox.Misc.almostEqual(x, y, tol)[source]

Check equality of two arrays objects up to a certain tolerance

Parameters:
  • x,y (numpy.ndarray objects of floats) – values to be compared
  • tol (float) – tolerance to be used
Returns:

true if equal, false otherwise

Return type:

bool

>>> eps2 = 2.*Misc.machineEpsilon(np.float64)
>>> Misc.almostEqual(np.array([2.]),np.array([2.+0.5*eps2]),eps2)
True
>>> Misc.almostEqual(np.array([2.]),np.array([2.+2.*eps2]),eps2)
False
SpectralToolbox.Misc.almostEqualList(xArray, y, tol)[source]

Check equality of a list of floats against an iterable value up to certain tolerance

Parameters:
  • xArray (2d-array of floats) – values to be compared to y
  • y (iterable objects of floats) – values to be compared to
  • tol (float) – tolerance to be used
Returns:

array of booleans containing true where equal, false elsewhere.

Return type:

1d-array of bool

Syntax:
b = almostEqualList(xArray,y,tol)
>>> eps2 = 2.*Misc.machineEpsilon(np.float64)
>>> X = np.random.rand(4,2)
>>> Misc.almostEqualList(X,X[1,:],eps2)
array([False,  True, False, False], dtype=bool)
SpectralToolbox.Misc.argsort_insertion(X, tol, start_idx=1, end_idx=None)[source]

Implements the insertion sort with binary_search. Returns permutation indices.

Parameters:
  • X (2d-array of floats) – values ordered by row according to the compare function
  • tol (float) – tolerance to be used
  • start_idx,end_idx (int) – starting and ending indices for the ordering (optional)
Returns:

permutation indices

Return type:

1d-array of integers

>>> X = np.random.rand(5,2)
>>> X
array([[ 0.56865133,  0.18490129],
       [ 0.01411459,  0.46076606],
       [ 0.64384365,  0.24998971],
       [ 0.47840414,  0.32554137],
       [ 0.12961966,  0.43712056]])
>>> perm = Misc.argsort_insertion(X,eps2)
>>> X[perm,:]
array([[ 0.01411459,  0.46076606],
       [ 0.12961966,  0.43712056],
       [ 0.47840414,  0.32554137],
       [ 0.56865133,  0.18490129],
       [ 0.64384365,  0.24998971]])

Search for the minimum X bigger than val

Parameters:
  • X (2d-array of floats) – values ordered by row according to the compare function
  • val (1d-array of floats) – value to be compared to
  • lo,hi (integer) – staring and ending indices
  • tol (float) – tolerance to be used
  • perm (1d-array of integers) – possible permutation to be used prior to the search (optional)
Returns:

index pointing to the maximum X smaller than val. If perm is provided, perm[idx] points to the maximum X smaller than val

Return type:

integer

>>> X = np.arange(1,5).reshape((4,1))
>>> X
array([[1],
       [2],
       [3],
       [4]])
>>> Misc.binary_search(X,np.array([2.5]),0,4,eps2)
>>> idx = Misc.binary_search(X,np.array([2.5]),0,4,eps2)
>>> idx
2
>>> X[idx,:]
array([3])
SpectralToolbox.Misc.compare(x, y, tol)[source]

Compares two iterable objects up to a certain tolerance

Parameters:
  • x,y (iterable objects of floats) – values to be compared
  • tol (float) – tolerance to be used
Returns:

-1 if (x-y) < tol, 1 if (x-y) > tol, 0 otherwise

Return type:

integer

>>> eps2 = 2.*Misc.machineEpsilon(np.float64)
>>> Misc.compare(np.array([2.]),np.array([2.+0.5*eps2]),eps2)
0
>>> Misc.compare(np.array([2.]),np.array([2.+2.*eps2]),eps2)
-1
SpectralToolbox.Misc.findOverlapping(XF, X, tol)[source]

Finds overlapping points of XF on X grids of points. The two grids are ordered with respect to Misc.compare().

Parameters:
  • XF,X (2d-array of floats) – values ordered by row according to the Misc.compare().
  • tol (float) – tolerance to be used
Returns:

true values for overlapping points of XF on X, false for not overlapping points. Note: the overlapping return argument is a true-false indexing for XF.

Return type:

1d-array of bool

Example

>>> XF
array([[ -1.73205081e+00,   0.00000000e+00],
       [ -1.00000000e+00,  -1.00000000e+00],
       [ -1.00000000e+00,   0.00000000e+00],
       [ -1.00000000e+00,   1.00000000e+00],
       [  0.00000000e+00,  -1.73205081e+00],
       [  0.00000000e+00,  -1.00000000e+00],
       [  0.00000000e+00,   2.16406754e-16],
       [  0.00000000e+00,   1.00000000e+00],
       [  0.00000000e+00,   1.73205081e+00],
       [  1.00000000e+00,  -1.00000000e+00],
       [  1.00000000e+00,   0.00000000e+00],
       [  1.00000000e+00,   1.00000000e+00],
       [  1.73205081e+00,   0.00000000e+00]])
>>> X
array([[ -1.73205081e+00,   0.00000000e+00],
       [ -1.00000000e+00,  -1.00000000e+00],
       [ -1.00000000e+00,   0.00000000e+00],
       [ -1.00000000e+00,   1.00000000e+00],
       [  0.00000000e+00,  -1.00000000e+00],
       [  2.16406754e-16,   0.00000000e+00],
       [  0.00000000e+00,   1.00000000e+00],
       [  1.00000000e+00,  -1.00000000e+00],
       [  1.00000000e+00,   0.00000000e+00],
       [  1.00000000e+00,   1.00000000e+00],
       [  1.73205081e+00,   0.00000000e+00]])
>>> tol = 2. * Misc.machineEpsilon()
>>> bool_idx_over = Misc.findOverlapping(XF,X,tol)
>>> XF[np.logical_not(bool_idx_over),:]
array([[ 0.        , -1.73205081],
       [ 0.        ,  1.73205081]])
SpectralToolbox.Misc.machineEpsilon(func=<class 'float'>)[source]

Returns the abolute machine precision for the type passed as argument

Parameters:func (dtype) – type
Returns:absolute machine precision
Return type:float
>>> Misc.machineEpsilon(np.float64)
2.2204460492503131e-16
>>> Misc.machineEpsilon(np.float128)
1.084202172485504434e-19
SpectralToolbox.Misc.powerset(iterable)[source]

Compute the power set of an iterable object.

SpectralToolbox.Misc.unique_cuts(X, tol, retIdxs=False)[source]

Returns the unique values and a list of arrays of boolean indicating the positions of the unique values. If retIdx is true, then it returns the group of indices with the same values as a indicator function (true-false array)