Efficient & pythonic check for singular matrix

So based on the inputs here, I’m marking my original code block with the explicit test as the solution: if linalg.cond(x) < 1/sys.float_info.epsilon: i = linalg.inv(x) else: #handle it Surprisingly, the numpy.linalg.inv function doesn’t perform this test. I checked the code and found it goes through all it’s machinations, then just calls the lapack routine … Read more

Sample from multivariate normal/Gaussian distribution in C++

Since this question has garnered a lot of views, I thought I’d post code for the final answer that I found, in part, by posting to the Eigen forums. The code uses Boost for the univariate normal and Eigen for matrix handling. It feels rather unorthodox, since it involves using the “internal” namespace, but it … Read more

Why does numpy.linalg.solve() offer more precise matrix inversions than numpy.linalg.inv()?

np.linalg.solve(A, b) does not compute the inverse of A. Instead it calls one of the gesv LAPACK routines, which first factorizes A using LU decomposition, then solves for x using forward and backward substitution (see here). np.linalg.inv uses the same method to compute the inverse of A by solving for A-1 in A·A-1 = I … Read more

whats the fastest way to find eigenvalues/vectors in python?

**if your matrix is sparse, then instantiate your matrix using a constructor from scipy.sparse then use the analogous eigenvector/eigenvalue methods in spicy.sparse.linalg. From a performance point of view, this has two advantages: your matrix, built from the spicy.sparse constructor, will be smaller in proportion to how sparse it is. the eigenvalue/eigenvector methods for sparse matrices … Read more

Transforming a row vector into a column vector in Numpy

you can use the transpose operation to do this: Example: In [2]: a = np.array([[1,2], [3,4], [5,6]]) In [5]: a.shape Out[5]: (3, 2) In [6]: a_trans = a.T #or: np.transpose(a), a.transpose() In [8]: a_trans.shape Out[8]: (2, 3) In [7]: a_trans Out[7]: array([[1, 3, 5], [2, 4, 6]]) Note that the original array a will still … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)