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
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
I need to check whether a matrix is unitary in python, for that I use this function:
return np.allclose(np.eye(m.shape), m.H * m)
but when I'm trying to specify a matrix by:
Suppose I have a numpy array
X = np.array([[1,2,3],
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
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
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
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:
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
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 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]
A B C
0 0 0 8
1 2 4 3
2 ... more
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
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:
array([0, 0, 0, ..., 0, 0, 0], dtype=uint8)
array([0, 0, 0... more
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
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
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
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
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
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?
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
array([[ 1... more
Considering the following matrix equation:
matrix([[ 0.477, -0.277, -0.2 ],
[-0.277, 0.444, -0.167],
[-0.2 , -0.167, 0.367]])
Out: [0, 60, 40]
how come that when I use numpy.linalg() ... more
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
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
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]])
array([[ 1.00000000e+00, 0.00000000e+00, 0.00000000e+00],
[ 1.5011998... more
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
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.
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
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
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
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
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
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
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),
I understand how to solve Ax=b, but what if b is dependent on x? See in the pictures E3 = function(E4). I guess this is done iteratively.. what is such a problem called? what methods do I use to solve it?
I am trying to solve the following system: