Jump to content

QR decomposition: Difference between revisions

Updated D entry
(Updated D entry)
Line 777:
std.typecons, std.numeric, std.range, std.conv;
 
T[][]template elementwiseMat(string op, T, U)(in T[][] A, in U B){
returnT[][] elementwiseMat!(q{ - }, T, U)(in T[][])( A, in U B);
pure /*nothrow*/ if (is(U == T) || is(U == T[][])) {
staticpure nothrow if (is(U == T) || is(U == T[][])) {
assertstatic if (A.lengthis(U == B.lengthT[][]));
if assert(A.emptylength == B.length);
returnif null;(A.empty)
auto R = new typeof( return)(A.length, A[0].length)null;
auto R = new typeof(return)(A.length, A[0].length);
 
foreach (immutable r, const row; A)
static if (is(U == T)) {
R[r][] = mixin("row[] " ~ op ~ "B");
} else {
assert(row.length == B[r].length);
R[r][] = mixin("row[] " ~ op ~ "B[r][]");
}
 
return R;
}
}
 
alias msum return= elementwiseMat!(q{ *+ }, T, T)(A, x);
T[][] msum(T)(in T[][] A, in T[][] B) pure /*nothrow*/ {
return msub = elementwiseMat!(q{ +- }, T, T[][])(A, B);
return pmul = elementwiseMat!(q{ /* }, T, T)(A, x);
}
pdiv = elementwiseMat!q{ / };
T[][] msub(T)(in T[][] A, in T[][] B) pure /*nothrow*/ {
return elementwiseMat!(q{ - }, T, T[][])(A, B);
}
T[][] pmul(T)(in T[][] A, in T x) pure /*nothrow*/ {
return elementwiseMat!(q{ * }, T, T)(A, x);
}
T[][] pdiv(T)(in T[][] A, in T x) pure /*nothrow*/ {
return elementwiseMat!(q{ / }, T, T)(A, x);
}
 
bool isRectangular(T)(in T[][] mat) /*pure nothrow*/ {
return mat.all!(r => r.length == mat[0].length);
}
 
T[][] matMul(T)(in T[][] a, in T[][] b) /*pure nothrow*/
in {
assert(a.isRectangular && b.isRectangular &&
Line 882 ⟶ 876:
auto C = new T[][](rows(A), cols(A));
foreach (immutable i, const arow; A)
C[i][] = arow[]; // someSome wasted copies.
foreach (immutable i, const brow; B)
C[row + i][col .. col + brow.length] = brow[];
Line 901 ⟶ 895:
 
Tuple!(T[][],"Q", T[][],"R") QRdecomposition(T)(T[][] A) {
immutable m = rows(A).rows;
immutable n = cols(A).cols;
auto Q = matId!T(m);
 
Line 949 ⟶ 943:
 
/// Solve a linear least squares problem by QR decomposition.
T[][] lsqr(T)(T[][] A, in T[][] b) pure nothrow {
const qr = QRdecomposition(A);
immutable size_t n = cols(qr.R);
Line 957 ⟶ 951:
}
 
Unqual!T[][] polyFit(T)(in T[][] x, in T[][] y, in size_t n) {
pure nothrow {
immutable size_t m = cols(x);
auto A = new Unqual!T[][](m, n + 1);
Cookies help us deliver our services. By using our services, you agree to our use of cookies.