pub fn inverse_tensor<T, S, I>(tensor: I) -> Option<Tensor<T, 2>>
where T: Numeric, for<'a> &'a T: NumericRef<T>, I: Into<TensorView<T, S, 2>>, S: TensorRef<T, 2>,
Expand description

Computes the inverse of a matrix provided that it exists. To have an inverse a matrix must be square (same number of rows and columns) and it must also have a non zero determinant.

The first dimension in the Tensor’s shape will be taken as the rows of the matrix, and the second dimension as the columns. If you instead have columns and then rows for the Tensor’s shape, you should reorder the Tensor before calling this function to get the appropriate matrix.

The inverse of a matrix A is the matrix A^-1 which when multiplied by A in either order yields the identity matrix I.

A(A^-1) == (A^-1)A == I.

*The inverse is like the reciprocal of a number, except for matrices instead of scalars. With scalars, there is no inverse for 0 because 1 / 0 is not defined. Similarly to compute the inverse of a matrix we divide by the determinant, so matrices with a determinant of 0 have no inverse, even if they are square.

This algorithm performs the analytic solution described by wikipedia and should compute the inverse for any size of square matrix if it exists, but is inefficient for large matrices.

§Warning

With some uses of this function the Rust compiler gets confused about what type T should be and you will get the error:

overflow evaluating the requirement &'a _: easy_ml::numeric::NumericByValue<_, _>

In this case you need to manually specify the type of T by using the turbofish syntax like: linear_algebra::inverse:_tensor:<f32>(&tensor)

Alternatively, the compiler doesn’t seem to run into this problem if you use the equivalent methods on the tensor type like so: tensor.inverse()