detSymmetric

Matrix determinant.

  1. auto detSymmetric(char store, Slice!(Iterator, 2, kind) a)
    detSymmetric
    (
    Iterator
    SliceKind kind
    )
    (
    char store
    ,
    Slice!(Iterator, 2, kind) a
    )
  2. auto det(Slice!(Iterator, 2, kind) a)

Examples

import mir.ndslice;
import mir.math;

// Check for zero-determinant shortcut.
auto ssing = [4, 2, 2, 1].sliced(2, 2);
auto ssingd = det(ssing);
assert (det(ssing) == 0);
assert (detSymmetric('L', ssing) == 0);

// check determinant of empty matrix
assert(slice!double(0, 0).det == 1);
// check determinant of zero matrix
assert(repeat(0, 9).sliced(3, 3).det == 0);

// General dense matrix.
int dn = 101;
auto d = uninitSlice!double(dn, dn);
foreach (k; 0 .. dn)
foreach (l; 0 .. dn)
    d[k,l] = 0.5 * (k == l ? (k + 1) * (k + 1) + 1 : 2 * (k + 1) * (l + 1));

auto dd = det(d);
import mir.math.common: approxEqual;
assert (approxEqual(dd, 3.539152633479803e289, double.epsilon.sqrt));

// Symmetric packed matrix
auto spa = [ 1.0, -2, 3, 4, 5, -6, -7, -8, -9, 10].sliced.stairs!"+"(4);
auto sp = [spa.length, spa.length].uninitSlice!double;
import mir.algorithm.iteration: each;
sp.stairs!"+".each!"a[] = b"(spa);
assert (detSymmetric('L', sp).approxEqual(5874.0, double.epsilon.sqrt));
assert (detSymmetric('U', sp.universal.transposed).approxEqual(5874.0, double.epsilon.sqrt));

Meta