scijs/ndarray-householder-qr

Name: ndarray-householder-qr

Owner: scijs

Description: Householder triangularization for QR Factorization of ndarrays

Created: 2015-05-08 21:13:59.0

Updated: 2018-03-27 22:28:15.0

Pushed: 2018-01-15 23:28:14.0

Homepage: null

Size: 34

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

ndarray-householder-qr

Build Status npm version Dependency Status

A module for calculating the in-place QR decomposition of an ndarray using Householder triangularization

Introduction

The algorithm is the Householder QR Factorization algorithm as found on p. 73 of Trefethen and Bau's Numerical Linear Algebra. In pseudocode, the algorithm is:

k = 1 to n
= A[k:m,k]
k = sign(x_1) ||x||_2 e_1 + x
k = v_k / ||v_k||_2
k:m,k:n] = A[k:m,k:n] - 2 v_k (v_k^* A[k:m,k:n])

The specific implementation is based on the pseudocode from Walter Gander's Algorithms for the QR-Decomposition. This algorithm computes both R and the Householder reflectors in place, storing R in the upper-triangular portion of A, the diagonal of R in a separate vector and the Householder reflectors in the columns of A. To eliminate unnecessary operations, the Householder reflectors are normalized so that norm(v) = sqrt(2).

Example

A straightforward example of the usefulness of QR factorization is the solution of least squares problems. To fit the model y = a0 * x + a1 to the data points [x1,y1] = [0,1], [x2,y2] = [1,2], [x3,y3] = [2,3]:

qr = require('ndarray-householder-qr'),
vander = require('ndarray-vandermonde'),

m = 3,
n = 2,

x = ndarray([0,1,2]), // independent variable
y = ndarray([1,2,3]), // data points

d = pool.zeros([n]),
A = vander(x, n);

actor(A, d);
olve(A, d, y);

esult: y = ndarray([1, 1, 0]) --> y = 1 * x + 1

After this calculation, the factorization can be reused to solve for other inputs:

y2 = ndarray([2, 3, 4]);

olve(A, d, y2);

esult: y = ndarray([2, 1, 0]) --> y = 1 * x + 2
Usage
qr.factor( A, d )

Computes the in-place triangularization of A, returning the Householder reflectors in the lower-triangular portion of A (including the diagonal) and R in the upper-triangular portion of A (excluding diagonal) with the diagonal of R stored in d. d must be a one-dimensional vector with length at least n.

qr.multiplyByQ(A, x)

Compute the product Q * x in-place, replacing x with Q * x. A is the in-place factored matrix.

qr.multiplyByQinv(A, x)

A is the in-place factored matrix. Compute the product Q^-1 * x in-place, replacing x with Q^-1 * x. Since the product is shorter than x for m > n, the entries of x from n+1 to m will be zero.

qr.constructQ(A, Q)

Given the in-place factored matrix A (diagonal not necessary), construct the matrix Q by applying the reflectors to a sequence of unit vectors. The dimensions of Q must be between m x n and m x m. When the dimensions of Q are m x n, Q corresponds to the Reduced QR Factorization. When the dimensions are m x m, Q corresponds to the Full QR Factorization.

qr.solve(A, d, x)

Use the previously-calculated triangularization to find the vector x that minimizes the L-2 norm of (Ax - b). Note that the vector b is modified in the process.

Benchmarks
m run bench
Credits

(c) 2015 Ricky Reusser. MIT License


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.