luSolve

Solves a system of linear equations \A * X = B, or \A**T * X = B with a general 'N x N' matrix 'A' using the LU factorization computed by luDecomp.

luSolve
(
Flag!"allowDestroy" allowDestroy = No.allowDestroy
SliceKind kindB
size_t[] n
IteratorB
IteratorLU
)
(
Slice!(Canonical, [2], IteratorLU) lut
,
Slice!(Contiguous, [1], lapackint*) ipiv
,
Slice!(kindB, n, IteratorB) b
,
char trans = 'N'
)

Parameters

allowDestroy

flag to delete the source matrix.

lut Slice!(Canonical, [2], IteratorLU)

factorization of matrix 'A', A = P * L * U.

ipiv Slice!(Contiguous, [1], lapackint*)

the pivot indices from luDecomp.

b Slice!(kindB, n, IteratorB)

the right hand side matrix B.

trans char

specifies the form of the system of equations: = 'N': A * X = B (No transpose) = 'T': A**T * X = B (Transpose) = 'C': A**T * X = B (Conjugate transpose = Transpose)

Return Value

Type: auto

Return solve of the system linear equations.

Examples

auto A =
    [ 1,  4, -3,  5,  6,
     -2,  8,  5,  7,  8,
      3,  4,  7,  9,  1,
      2,  4,  6,  3,  2,
      6,  8,  3,  5,  2 ]
        .sliced(5, 5)
        .as!double.slice
        .canonical;

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

auto LU = A.luDecomp();
auto X = LU.solve(B);

import mir.ndslice.algorithm: equal;
assert(equal!((a, b) => fabs(a - b) < 1e-12)(mtimes(A, X), B));
auto B =
    [ 3, -7, -2,  2,
     -3,  5,  1,  0,
      6, -4,  0, -5,
     -9,  5, -5, 12 ]
        .sliced(4, 4)
        .as!double.slice
        .canonical;
auto C = B.transposed.slice;

auto LU = B.transposed.luDecomp!(Yes.allowDestroy)();
auto res = mtimes(LU.l, LU.u);
moveRows(res, LU.ipiv);

import mir.ndslice.algorithm: equal;
import std.math: approxEqual;
assert(res.equal!approxEqual(C));

Meta