mldivide

Solve systems of linear equations AX = B for X. Computes minimum-norm solution to a linear least squares problem if A is not a square matrix.

  1. Slice!(BlasType!(IteratorA, IteratorB)*, 2) mldivide(Slice!(IteratorA, 2, kindA) a, Slice!(IteratorB, 2, kindB) b)
    Slice!(BlasType!(IteratorA, IteratorB)*, 2)
    mldivide
    (
    IteratorA
    SliceKind kindA
    IteratorB
    SliceKind kindB
    )
    (
    Slice!(IteratorA, 2, kindA) a
    ,
    Slice!(IteratorB, 2, kindB) b
    )
  2. Slice!(BlasType!(IteratorA, IteratorB)*) mldivide(Slice!(IteratorA, 2, kindA) a, Slice!(IteratorB, 1, kindB) b)

Examples

AX=B

import std.meta: AliasSeq;
import mir.ndslice;

foreach(C; AliasSeq!(double, cdouble))
{
    static if(is(C == cdouble))
        enum transform = "a+0i";
    else
        enum transform = "a";

    auto a = [
        1, -1,  1,
        2,  2, -4,
        -1,  5,  0].sliced(3, 3).map!transform;
    auto b = [
        2.0,  0,
        -6  , -6,
        9  ,  1].sliced(3, 2).map!transform;
    auto t = [
        1.0, -1,
        2  ,  0,
        3  ,  1].sliced(3, 2).map!transform;

    auto x = mldivide(a, b);
    assert(x == t);
}

Ax=B

import std.meta: AliasSeq;
import mir.ndslice;

foreach(C; AliasSeq!(double, cdouble))
{
    static if(is(C == cdouble))
        enum transform = "a+0i";
    else
        enum transform = "a";

    auto a = [
        1, -1,  1,
        2,  2, -4,
        -1,  5,  0].sliced(3, 3).map!transform;
    auto b = [
        2.0,
        -6  ,
        9  ].sliced(3).map!transform;
    auto t = [
        1.0,
        2  ,
        3  ].sliced(3).map!transform;

    auto x = mldivide(a, b);
    assert(x == t);
}

Least-Squares Solution of Underdetermined System

import std.meta: AliasSeq;
import mir.ndslice;

foreach(C; AliasSeq!(double, )) //cdouble fails for DMD>=2085
{
    static if(is(C == cdouble))
        enum transform = "a+0i";
    else
        enum transform = "a";

    auto a = [
        -0.57,  -1.28,  -0.39,   0.25,
        -1.93,   1.08,  -0.31,  -2.14,
        2.30,   0.24,   0.40,  -0.35,
        -1.93,   0.64,  -0.66,   0.08,
        0.15,   0.30,   0.15,  -2.13,
        -0.02,   1.03,  -1.43,   0.50,
        ].sliced(6, 4).map!transform;

    auto b = [
    -2.67,
    -0.55,
    3.34,
    -0.77,
    0.48,
    4.10,
    ].sliced.map!transform;

    auto x = [
        1.5339,
        1.8707,
        -1.5241,
        0.0392].sliced.map!transform;

    import std.math: approxEqual;
    static if(is(C == cdouble))
    {
        assert(a.mldivide(b).map!"a.re".approxEqual(x.map!"a.re"));
        assert(a.mldivide(b).map!"a.im".approxEqual(x.map!"a.im"));
    }
    else
    {
        assert(a.mldivide(b).approxEqual(x));
    }
}

Meta