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?
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.
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.
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).
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.
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
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With