## Using numpy.einsum for transpose times matrix: x^T * x

For a 2D matrix X (shape (m,n)), I'm trying to calculate X.T * X where * is matrix multiplication. Following the explanation on this post I expected to be able to do this using, np.einsum('ji,ik->jk', X, X) where on the LHS writing ji first takes...
more »

## Diagonalization of a tridiagonal, symmetric sparse matrix with Python

I have an NxN symmetric and tridiagonal matrix computed by a Python code and I want to diagonalize it. In the specific case I'm dealing with N = 6000, but the matrix can become larger. Since it is sparse, I assumed the best way to diagonalize it wa...
more »

## How to specify dtype correctly in python when dealing with complex numbers and numpy?

I need to check whether a matrix is unitary in python, for that I use this function: def is_unitary(m): return np.allclose(np.eye(m.shape[0]), m.H * m) but when I'm trying to specify a matrix by: m1=np.matrix([complex(1/math.sqrt(2)),cmath.e...
more »

## Python: fastest way of adding columns containing pairwise products of column elements

Suppose I have a numpy array X = np.array([[1,2,3], [4,5,6], [7,8,9]]) I want to extend this matrix by adding (on the left) the columns resulting by multiplying together all possible pairs of columns. In this example it...
more »

## Python: Multiplying a list of vectors by a list of matrices as a single matrix operation

I have a list of 100 N-dimensional vectors and a list of 100 MxN matrices. So you can think of the two data structures as a 100xN list (or numpy array) and a 100xMxN list (or numpy array). What I want to do is take the dot product of each vector and...
more »

## Swift structs: Protocol (w/asoc. types) v/s Composition

I’m refactoring a Linear Algebra library. It includes a Vector, Matrix and Tensor structs. So far so good, but since the three of them are conceptually Tensors, they share a lot of properties and underlying behavior. Therefore, I’m trying a refac...
more »

## Fastest way to solve least square for overdetermined system

I have a matrix A of size m*n( m order of ~100K and n ~500) and a vector b. Also, my matrix is ill-conditioned and rank-deficient. Now I want to find out the least-square solution to Ax = b and to this end I have compared some of the methods: scipy...
more »

## Is there a built-in/easy LDU decomposition method in Numpy?

I see cholesky decomposition in numpy.linalg.cholesky, but could not find a LDU decompositon. Can anyone suggest a function to use? ...
more »

## More efficient way to multiply each column of a 2-d matrix by each slice of a 3-d matrix

I have an 8x8x25000 array W and an 8 x 25000 array r. I want to multiple each 8x8 slice of W by each column (8x1) of r and save the result in Wres, which will end up being an 8x25000 matrix. I am accomplishing this using a for loop as such: for i i...
more »

## Python CashFlow Calculator

I am trying to formulate an equation that can calculate the outstanding_balance in one go using python. It is quite simple using the iterative process. For example: for month in range(1, self.amortMonths + 1): # Calculate intial and future ...
more »

## Is there a way to check for linearly dependent columns in a dataframe?

Is there a way to check for linear dependency for columns in a pandas dataframe? For example: columns = ['A','B', 'C'] df = pd.DataFrame(columns=columns) df.A = [0,2,3,4] df.B = df.A*2 df.C = [8,3,5,4] print(df) A B C 0 0 0 8 1 2 4 3 2 ...
more »

## Calculate log of determinant in TensorFlow when determinant overflows/underflows

My cost function involves the calculation of log(det(A)) (assuming the det(A) is positive so the log makes sense, but A is not Hermitian so that the Cholesky decomposition is not applicable here). When det(A) is very large/small, a direct call to det...
more »

## NumPy's dot product gives two different results depending on array dtype

I have some grayscale image data (0-255). Depending on the NumPy dtype, I am getting different dot product results. For example, x0 and x1 are the same image: >>> x0 array([0, 0, 0, ..., 0, 0, 0], dtype=uint8) >>> x1 array([0, 0, 0...
more »

## How to identify columns that are sums of other columns in a dataset

I would like to write a function (preferably in R, but other languages are welcome), which would identify relationships between columns (limited to additions/substractions) in a dataset. A practical application of this would be to run it on large mul...
more »

## Numpy dot too clever about symmetric multiplications

Anybody know about documentation for this behaviour? import numpy as np A = np.random.uniform(0,1,(10,5)) w = np.ones(5) Aw = A*w Sym1 = Aw.dot(Aw.T) Sym2 = (A*w).dot((A*w).T) diff = Sym1 - Sym2 diff.max() is near machine-precision non-zero, e.g...
more »

## Random solutions of undetermined linear systems

Consider an underdetermined linear system of equations Ax=b. I would like to find a set of vectors x_1, ..., x_n such that they all solve Ax=b and they are as different between each other as possible. The second part is actually less important; I ...
more »

## checking if a matrix is diagonally dominant in python

I know that my code is wrong because np.sum(abs(X),axis=1)) also sums the diagonal value, therefore my code will always return 'NOT diagonally dominant'. I have tried putting '-np.diag(X)' but i get an error message. Thank you in advance! import num...
more »

## Vector dot product along one dimension for multidimensional arrays

I want to compute the sum product along one dimension of two multidimensional arrays, using Theano. I'll describe precisely what I want to do using numpy first. numpy.tensordot and numpy.dot seem to always do a matrix product, whereas I'm in essenc...
more »

## Adding two matrix with different dimension

Let A be a matrix with [m x n] elements and B another matrix with [m x n x o] elements. Is there any linear algebraic way to add both matrices such that C = A + B where C will be in [m x n x o] without any sort of looping along the o dimension? Exam...
more »

## Matrix multiplication issue for LU decomposition?

I'm trying to solve an Ax=b by using LU decomposition, but somehow I can't get the A by multiplying L*U. Here's the code and the results; A = array([2,3,5,4]).reshape(2,2) b = array([4,3]) P,L, U = lu(A) And the results for L and U L: array([[ 1...
more »

## Numpy linalg: linear system with unlikely results

Considering the following matrix equation: x=Ab where: In[1]:A Out[1]: matrix([[ 0.477, -0.277, -0.2 ], [-0.277, 0.444, -0.167], [-0.2 , -0.167, 0.367]]) In[2]: b Out[2]: [0, 60, 40] how come that when I use numpy.linalg() ...
more »

## How do I use variable size vectors in Edward Kmett's "Linear" library?

I'm trying to use ekmett's linear library, and I'm having some trouble with variable length vectors, in Linear.V. How do I use the dim function to get the size of a vector? How do I use trace on a large square matrix made of nested Vs ? I get errors ...
more »

## How do you utilize np.linalg.pinv to return this matrix

For matrix X, If np.linalg.pinv(X) equates to (X^T X)^(-1)(X^T) For Constant C, and Identity Matrix I, How do you utilize np.linalg.pinv to return (X^T X + CI)^(-1)(X^T) ...
more »

## Compute inverse of 2D arrays along the third axis in a 3D array without loops

I have an array A whose shape is (N, N, K) and I would like to compute another array B with the same shape where B[:, :, i] = np.linalg.inv(A[:, :, i]). As solutions, I see map and for loops but I am wondering if numpy provides a function to do this...
more »

## Numpy inverts a non-invertible matrix

I friend and I executed this line of code in Python 2 and Python 3: import numpy as np mat = np.array([[1,0,0],[-1,3,3],[1,2,2]]) np.linalg.inv(mat) Which returns: array([[ 1.00000000e+00, 0.00000000e+00, 0.00000000e+00], [ 1.5011998...
more »

## Error with NumPy transpose

I'm trying to figure out what is going on here, but I'm a little bit baffled. I am getting unexpected results working with a transposed NumPy identity matrix (which should have no effect). For example: import numpy as np N = 1000 # case 1: A = np....
more »

## Computing a company's shareholders ownership percentage

I have a graph that contains two types of nodes: Companies and Persons. A Company node has a list of edges that represent Shareholders. A Shareholder has a percentage of shares and is either a Company or a Person. A Person node is always a leaf. He...
more »

## associativity of matrix multiplication in numpy

I am playing with a simple numpy example and having hard time to understand why associative property of matrix multiplication ABC = (AB)C = A(BC) does not exactly hold. I assume the problem is with numeric stability. But how to address it? What i...
more »

## Numpy: make batched version of quaternion multiplication

I transformed the following function def quaternion_multiply(quaternion0, quaternion1): """Return multiplication of two quaternions. >>> q = quaternion_multiply([1, -2, 3, 4], [-5, 6, 7, 8]) >>> numpy.allclose(q, [-44,...
more »

## Solving linear equation with Gauss-Seidel method gives wrong results

Using R package optR for solving linear equation gives incorrect results for gauss-seidel method. I tried solving with different methods and it looks ok. For instance solve or using optR but with method gauss. Example is here. Or am I doing something...
more »

## Solve a multitude of linear least square system efficiently

I have to find the best solution for >10^7 equation systems with 5 equations in 2 variables each (5 measurements to find 2 parameters with the least amount of error in a long series). The following code (normally used to do curve fitting) does what I...
more »

## Why do Mathematica and Python's answers differ when dealing with singular matrix equations?

I have been dealing with linear algebra problems of the form A = Bx in Python and comparing this to a colleague's code in MATLAB and Mathematica. We have noticed differences between Python and the others when B is a singular matrix. When using numpy....
more »

## Non-linear Least Squares Fitting (2-dimensional) in Python

I was wondering what the correct approach to fitting datapoints to a non-linear function should be in python. I am trying to fit a series of data-points t = [0., 0.5, 1., 1.5, ...., 4.] y = [6.3, 4.5,.................] using the following model f...
more »

## 2D Orthogonal projection of vector onto line with numpy yields wrong result

I have 350 document scores that, when I plot them, have this shape: docScores = [(0, 68.62998962), (1, 60.21374512), (2, 54.72480392), (3, 50.71389389), (4, 49.39723969), ..., (345, 28.3756237), (346, 28.37126923), ...
more »