qrSolve

Solve the least squares problem: \min ||A * X - B|| Using the QR factorization: \A = Q * R computed by qrDecomp.

qrSolve
(
Flag!"allowDestroy" allowDestroy = No.allowDestroy
SliceKind kindB
size_t[] n
IteratorB
IteratorA
IteratorT
)
(
Slice!(Canonical, [2], IteratorA) a
,
Slice!(Contiguous, [1], IteratorT) tau
,
Slice!(kindB, n, IteratorB) b
)

Parameters

allowDestroy

flag to delete the source matrix.

a Slice!(Canonical, [2], IteratorA)

detalis of the QR factorization of the original matrix as returned by qrDecomp.

tau Slice!(Contiguous, [1], IteratorT)

details of the orhtogonal matrix Q.

b Slice!(kindB, n, IteratorB)

right hand side matrix.

Return Value

Type: auto

solution matrix.

Examples

auto A =
        [ 3,  1, -1,  2,
         -5,  1,  3, -4,
          2,  0,  1, -1,
          1, -5,  3, -3 ]
          .sliced(4, 4)
          .as!double.slice;

import mir.random.variable;
import mir.random.algorithm;
auto B = randomSlice!double(uniformVar(-100, 100), 4, 100);

auto C = qrDecomp(A);
auto X = C.solve(B);

import std.math: approxEqual;
import mir.ndslice.algorithm: equal;
assert(equal!approxEqual(mtimes(A, X), B));

Meta