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 mir.complex;
import std.meta: AliasSeq;
import mir.ndslice;

foreach(C; AliasSeq!(double, Complex!double))
{
    static if(is(C == Complex!double))
        alias transform = a => C(a, 0);
    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 mir.complex;
import std.meta: AliasSeq;
import mir.ndslice;

foreach(C; AliasSeq!(double, Complex!double))
{
    static if(is(C == Complex!double))
        alias transform = a => C(a, 0);
    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 mir.complex;
import std.meta: AliasSeq;
import mir.ndslice;

foreach(C; AliasSeq!(double, )) //Complex!double fails for DMD>=2085
{
    static if(is(C == Complex!double))
        alias transform = a => C(a, 0);
    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 mir.math.common: approxEqual;
    import mir.algorithm.iteration: all;
    alias appr = all!((a, b) => approxEqual(a, b, 1e-3, 1e-3));
    assert(appr(a.mldivide(b), x));

Meta