linear-algebra

Change of basis in numpy

走远了吗. 提交于 2020-05-31 04:20:14
问题 Given two basis matrices basis_old and basis_new in numpy, is there a function somewhere in the library to get the transformation matrix to convert a vector vec in basis_old to its representation in basis_new ? For example, if I have a vector vec = [1,2,3] in the standard basis [1,0,0], [0,1,0], [0,0,1] , how to I convert it to another basis, say, e1 = [1 0 0] e2 = [0 0 1] e3 = [0 1 0] basis_new = np.array([e1, e2, e3]) # I want something like this vec_new = np.linalg.change_of_basis(vec_old,

How to compute scipy sparse matrix determinant without turning it to dense?

冷暖自知 提交于 2020-05-09 19:03:06
问题 I am trying to figure out the fastest method to find the determinant of sparse symmetric and real matrices in python. using scipy sparse module but really surprised that there is no determinant function. I am aware I could use LU factorization to compute determinant but don't see a easy way to do it because the return of scipy.sparse.linalg.splu is an object and instantiating a dense L and U matrix is not worth it - I may as well do sp.linalg.det(A.todense()) where A is my scipy sparse matrix

How to implement ILU precondioner in scipy?

强颜欢笑 提交于 2020-04-30 06:58:05
问题 For the iterative solvers in the scipy.sparse.linalg such as bicg , gmres , etc, there is an option to add the precondioner for the matrix A . However, the documentation is not very clear about what I should give as the preconditioner. If I use ilu = sp.sparse.linalg.spilu(A) , ilu is not any matrices but an object that encompasses many things. Someone asked about a similar question here for Python 2.7, but I doesn't work for me (Python 3.7, scipy version 1.1.0) So my question is how to

Python tensor matrix multiply

我的未来我决定 提交于 2020-03-21 07:00:11
问题 I have the tensor A = [[[a,b], [c,d]], [[e,f], [g,h]]] and the matrix B = [[1,2], [3,4]] I need to get C = [[a*1+e*2,b*1+f*2], [c*3+g*4,d*3+h*4]] How can I do this using numpy in matrix form? I've looked into np.tensordot() but it doesn't seem to help in this case. 回答1: You can try this: >>> import numpy as np >>> a = np.arange(1,9).reshape(2,2,2) >>> a array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) >>> b = np.arange(1,5).reshape(2,2) >>> b array([[1, 2], [3, 4]]) >>> (a * b[None,:,:].T).sum

How can I find the joint eigenvalues of two matrices in MATLAB?

十年热恋 提交于 2020-03-04 03:43:30
问题 If the joint eigenvalues of matrices A and B are defined as the roots of the equation det(lambda * A - B ) = 0, how can I solve this in MATLAB? In particular, I am not sure how exactly lambda is defined - it obviously needs to be a matrix or vector, as otherwise there would only be one joint eigenvalue. Also, I am not sure if there is any in-built function, or if, say, fzero for finding the roots of nonlinear functions needs to be used. 回答1: There is a built-in function for this. http://www

Pseudoinverse calculation in Python

匆匆过客 提交于 2020-02-02 16:22:28
问题 Problem I was working on the problem described here. I have two goals. For any given system of linear equations, figure out which variables have unique solutions. For those variables with unique solutions, return the minimal list of equations such that knowing those equations determines the value of that variable. For example, in the following set of equations X = a + b Y = a + b + c Z = a + b + c + d The appropriate output should be c and d, where X and Y determine c and Y and Z determine d.

Stack numpy array onto diagonal

[亡魂溺海] 提交于 2020-01-30 06:19:07
问题 Given N 2d numpy arrays, is there a neat way in which I can 'stack' or 'bolt' them together on the diagonal, filling any new slots with 0? E.g. given: arr1 = np.array([[1, 2], [3, 4]]) arr2 = np.array([[9, 8, 7], [6, 5, 4], [3, 2, 1]]) I want to create: arr = np.array([[1, 2, 0, 0, 0], [3, 4, 0, 0, 0], [0, 0, 9, 8, 7], [0, 0, 6, 5, 4], [0, 0, 3, 2, 1]]) 回答1: There's a function for that. scipy.linalg.block_diag(arr1, arr2) It takes arbitrarily many parameters: scipy.linalg.block_diag(*list_of

Find all solutions of row reduced echelon matrix in pure python

杀马特。学长 韩版系。学妹 提交于 2020-01-26 02:07:30
问题 I have toggle = [[1, 1, 1, 1, 0, 0, 1, 0, 0], [0, 1, 1, 0, 1, 1, 0, 0, 0], [0, 0, 1, 1, 0, 0, 0, 1, 0], [0, 0, 0, 1, 1, 0, 1, 1, 0], [0, 0, 0, 0, 1, 1, 0, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0]] puzzle = [0, 0, 0, 0, 1, 0, 0, 0, 0] toggle is a matrix I already transformed to [row echelon form] with Gaussian elimination. This is in GF(2) space, which means that -1 = 1 and 1 + 1 = 0 . I already have a working

Find all solutions of row reduced echelon matrix in pure python

妖精的绣舞 提交于 2020-01-26 02:07:22
问题 I have toggle = [[1, 1, 1, 1, 0, 0, 1, 0, 0], [0, 1, 1, 0, 1, 1, 0, 0, 0], [0, 0, 1, 1, 0, 0, 0, 1, 0], [0, 0, 0, 1, 1, 0, 1, 1, 0], [0, 0, 0, 0, 1, 1, 0, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0]] puzzle = [0, 0, 0, 0, 1, 0, 0, 0, 0] toggle is a matrix I already transformed to [row echelon form] with Gaussian elimination. This is in GF(2) space, which means that -1 = 1 and 1 + 1 = 0 . I already have a working

run time error when using a mex file, maybe due to memory allocation

混江龙づ霸主 提交于 2020-01-25 21:24:12
问题 I've written a mex function named mx_minimum_power that I'm calling it in MATLAB as follows: [Fs,Fd,pow_remained] = mx_minimum_power(A11,A12_real,A12_imag,A13_real,A13_imag,A22,A23_real,A23_imag,A33,MatSize); A11, A12_real, A13_real, A22, A23_real, A33 are 30555x1 single matrices A12_imag, A13_imag, A23_imag are 1x1 single variables MatSize is a 1x1 double variable with the value 30555 , that is the size of matrices In an iteration, each time the hermitian matrix A is constructed and