Source code for PyDynamic.misc.tools

# -*- coding: utf-8 -*-
"""
.. moduleauthor:: Sascha Eichstaedt (sascha.eichstaedt@ptb.de)
"""
import numpy as np

[docs]def col_hstack(vectors): """ From tuple of 1D ndarrays make a 2D ndarray where the tuple elements are as column vectors horizontally stacked :param vectors: list of K 1D-ndarrays of dimension N :returns matrix: 2D ndarray of shape (N,K) """ if isinstance(vectors,list): col_vectors = map(lambda x: x[:,np.newaxis], vectors) else: raise ValueError("Input must be of type list\n") return np.hstack(col_vectors)
[docs]def find(assertions): """ MATLAB-like determination of occurence of assertion in an array using the numpy nonzero function """ if not isinstance(assertions,tuple): raise ValueError("Input to 'find' needs to be a tuple.") inds = np.arange(len(assertions[0])) for assertion in assertions: inds = inds[np.nonzero(assertion[inds])[0]] return inds
[docs]def zerom(shape): """ Generate a numpy.matrix of zeros of given shape """ from numpy import zeros, matrix return matrix(zeros(shape))
[docs]def stack(elements): def make_matrix(v): if len(v.shape())>1: return v else: return v[:,np.newaxis] return np.hstack(map(lambda x: make_matrix(x), elements))
[docs]def make_semiposdef(matrix,maxiter=10,tol=1e-12): n,m = matrix.shape if n!=m: raise ValueError("Matrix has to be quadratic") matrix = 0.5*(matrix+matrix.T) e = np.real(np.linalg.eigvals(matrix)).min() count = 0 while e<tol and count<maxiter: matrix += (np.abs(e)+tol)*np.eye(n) e = np.real(np.linalg.eigvals(matrix)).min() count += 1 return matrix