Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any inverse np.dot function?

If I have two matrices a and b, is there any function I can find the matrix x, that when dot multiplied by a makes b? Looking for python solutions, for matrices in the form of numpy arrays.

like image 476
Victor Sim Avatar asked Mar 02 '23 04:03

Victor Sim


2 Answers

This problem of finding X such as A*X=B is equivalent to search the "inverse of A", i.e. a matrix such as X = Ainverse * B.

For information, in math Ainverse is noted A^(-1) ("A to the power -1", but you can say "A inverse" instead).

In numpy, this is a builtin function to find the inverse of a matrix a:

import numpy as np

ainv = np.linalg.inv(a)

see for instance this tutorial for explanations.

You need to be aware that some matrices are not "invertible", most obvious examples (roughly) are:

  • matrix that are not square
  • matrix that represent a projection

numpy can still approximate some value in certain cases.

like image 127
Pac0 Avatar answered Mar 12 '23 07:03

Pac0


if A is a full rank, square matrix

import numpy as np
from numpy.linalg import inv

X = inv(A) @ B

if not, then such a matrix does not exist, but we can approximate it

import numpy as np
from numpy.linalg import inv

X = inv(A.T @ A) @ A.T @ B
like image 38
Hammad Avatar answered Mar 12 '23 08:03

Hammad