inv

Calculates the inverse of a matrix.

inv
(
Iterator
SliceKind kind
)
(
Slice!(Iterator, 2, kind) a
)

Examples

import mir.complex;
import mir.ndslice;

auto a =  [
    1, 0, 2,
    2, 2, 0,
    0, 1, 1]
    .sliced(3, 3);

enum : double { _13 = 1.0/3.0, _16 = 1.0/6.0, _23 = 2.0/3.0 }
auto ans = [
    _13, _13, -_23,
    -_13,_16, _23,
    _13, -_16, _13]
    .sliced(a.shape);

import mir.algorithm.iteration: equal;
import mir.math.common: approxEqual;
assert(equal!((a, b) => a.approxEqual(b, 1e-10L, 1e-10L))(a.inv, ans));
assert(equal!((a, b) => a.approxEqual(b, 1e-10L, 1e-10L))(a.map!(a => Complex!double(a, 0)).inv.member!"re", ans));
import mir.ndslice.topology: iota;

try
{
    auto m = [3, 3].iota.inv;
    assert (false, "Matrix should be detected as singular");
}
catch (Exception e)
{
    assert (true);
}

Meta