Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inverse of numpy.dot

I can easily calculate something like:

R = numpy.column_stack([A,np.ones(len(A))]) 
M = numpy.dot(R,[k,m0])

where A is a simple array and k,m0 are known values.

I want something different. Having fixed R, M and k, I need to obtain m0. Is there a way to calculate this by an inverse of the function numpy.dot()? Or it is only possible by rearranging the matrices?

like image 457
Germano Avatar asked Feb 19 '14 19:02

Germano


People also ask

How do you find the inverse of a Numpy?

Inverse of a Matrix using NumPy Python provides a very easy method to calculate the inverse of a matrix. The function numpy. linalg. inv() which is available in the python NumPy module is used to compute the inverse of a matrix.

Is Matmul same as dot?

matmul differs from dot in two important ways. Multiplication by scalars is not allowed. Stacks of matrices are broadcast together as if the matrices were elements.

What does Numpy dot do?

dot() in Python. The numpy module of Python provides a function to perform the dot product of two arrays. If both the arrays 'a' and 'b' are 1-dimensional arrays, the dot() function performs the inner product of vectors (without complex conjugation).

What is Numpy Linalg Inv?

NumPy linalg. inv() function in Python is used to compute the (multiplicative) inverse of a matrix. The inverse of a matrix is that matrix which when multiplied with the original matrix, results in an identity matrix.


1 Answers

M = numpy.dot(R,[k,m0])

is performing matrix multiplication. M = R * x.

So to compute the inverse, you could use np.linalg.lstsq(R, M):

import numpy as np
A = np.random.random(5)
R = np.column_stack([A,np.ones(len(A))]) 
k = np.random.random()
m0 = np.random.random()
M = R.dot([k,m0])

(k_inferred, m0_inferred), residuals, rank, s = np.linalg.lstsq(R, M)

assert np.allclose(m0, m0_inferred)
assert np.allclose(k, k_inferred)

Note that both k and m0 are determined, given M and R (assuming len(M) >= 2).

like image 90
unutbu Avatar answered Oct 03 '22 13:10

unutbu