lib/ops.js
function lstsq(A, B, rcond) {
    if (rcond === void 0) { rcond = -1; }
    var copyA = A.clone();
    var copyB = B.clone();
    var is_1d = false;
    if (copyB.shape.length === 1) {
        copyB.reshape([copyB.shape[0], 1]);
        is_1d = true;
    }
    var m;
    var n;
    m = copyA.shape[0];
    n = copyA.shape[1];
    var nrhs = copyB.shape[1];
    copyA.swapOrder();
    copyB.swapOrder();
    var S = new common_1.NDArray({ shape: [Math.min(m, n)] });
    var rank = lapack.gelsd(copyA.data, m, n, nrhs, rcond, copyB.data, S.data);
    copyB.swapOrder();
    // Residuals - TODO: test more
    var residuals = new common_1.NDArray([]);
    if (rank === n && m > n) {
        var i = void 0;
        if (is_1d) {
            residuals = new common_1.NDArray({ shape: [1] });
            var sum = 0;
            for (i = n; i < m; i++) {
                var K = copyB.shape[1];
                for (var j = 0; j < K; j++) {
                    // TODO:Complex
                    sum += copyB.get(i, j) * copyB.get(i, j);
                }
            }
            residuals.set(0, sum);
        }
        else {
            residuals = new common_1.NDArray({ shape: [m - n] });
            for (i = n; i < m; i++) {
                var K = copyB.shape[1];
                var sum = 0;
                for (var j = 0; j < K; j++) {
                    // TODO:Complex
                    sum += copyB.get(i, j) * copyB.get(i, j);
                }
                residuals.set(i - n, sum);
            }
        }
    }
    return {
        x: copyB,
        residuals: residuals,
        rank: rank,
        singulars: S
    };
}
function lstsq(A, B, rcond) {
    if (rcond === void 0) { rcond = -1; }
    var copyA = A.clone();
    var copyB = B.clone();
    var is_1d = false;
    if (copyB.shape.length === 1) {
        copyB.reshape([copyB.shape[0], 1]);
        is_1d = true;
    }
    var m;
    var n;
    m = copyA.shape[0];
    n = copyA.shape[1];
    var nrhs = copyB.shape[1];
    copyA.swapOrder();
    copyB.swapOrder();
    var S = new common_1.NDArray({ shape: [Math.min(m, n)] });
    var rank = lapack.gelsd(copyA.data, m, n, nrhs, rcond, copyB.data, S.data);
    copyB.swapOrder();
    // Residuals - TODO: test more
    var residuals = new common_1.NDArray([]);
    if (rank === n && m > n) {
        var i = void 0;
        if (is_1d) {
            residuals = new common_1.NDArray({ shape: [1] });
            var sum = 0;
            for (i = n; i < m; i++) {
                var K = copyB.shape[1];
                for (var j = 0; j < K; j++) {
                    // TODO:Complex
                    sum += copyB.get(i, j) * copyB.get(i, j);
                }
            }
            residuals.set(0, sum);
        }
        else {
            residuals = new common_1.NDArray({ shape: [m - n] });
            for (i = n; i < m; i++) {
                var K = copyB.shape[1];
                var sum = 0;
                for (var j = 0; j < K; j++) {
                    // TODO:Complex
                    sum += copyB.get(i, j) * copyB.get(i, j);
                }
                residuals.set(i - n, sum);
            }
        }
    }
    return {
        x: copyB,
        residuals: residuals,
        rank: rank,
        singulars: S
    };
}
function lstsq ( A , B , rcond ) { if ( rcond === void 0 ) { rcond = - 1 ; } var copyA = A . clone ( ) ; var copyB = B . clone ( ) ; var is_1d = false ; if ( copyB . shape . length === 1 ) { copyB . reshape ( [ copyB . shape [ 0 ] , 1 ] ) ; is_1d = true ; } var m ; var n ; m = copyA . shape [ 0 ] ; n = copyA . shape [ 1 ] ; var nrhs = copyB . shape [ 1 ] ; copyA . swapOrder ( ) ; copyB . swapOrder ( ) ; var S = new common_1 . NDArray ( { shape : [ Math . min ( m , n ) ] } ) ; var rank = lapack . gelsd ( copyA . data , m , n , nrhs , rcond , copyB . data , S . data ) ; copyB . swapOrder ( ) ; // Residuals - TODO: test more var residuals = new common_1 . NDArray ( [ ] ) ; if ( rank === n && m > n ) { var i = void 0 ; if ( is_1d ) { residuals = new common_1 . NDArray ( { shape : [ 1 ] } ) ; var sum = 0 ; for ( i = n ; i < m ; i ++ ) { var K = copyB . shape [ 1 ] ; for ( var j = 0 ; j < K ; j ++ ) { // TODO:Complex sum += copyB . get ( i , j ) * copyB . get ( i , j ) ; } } residuals . set ( 0 , sum ) ; } else { residuals = new common_1 . NDArray ( { shape : [ m - n ] } ) ; for ( i = n ; i < m ; i ++ ) { var K = copyB . shape [ 1 ] ; var sum = 0 ; for ( var j = 0 ; j < K ; j ++ ) { // TODO:Complex sum += copyB . get ( i , j ) * copyB . get ( i , j ) ; } residuals . set ( i - n , sum ) ; } } } return { x : copyB , residuals : residuals , rank : rank , singulars : S } ; }
Return the least-squares solution to a linear matrix equation.

Solves the equation `a x = b` by computing a vector `x` that
minimizes the Euclidean 2-norm `|| b - a x ||^2`.  The equation may
be under-, well-, or over- determined (i.e., the number of
linearly independent rows of `a` can be less than, equal to, or
greater than its number of linearly independent columns).  If `a`
is square and of full rank, then `x` (but for round-off error) is
the "exact" solution of the equation.

@param A Coefficient matrix (m-by-n)
@param B Values on RHS of equation system. Could be array of length
m or it could be 2D with dimensions m-by-k
@param rcond Cut-off ratio for small singular values of `a`
Return the least - squares solution to a linear matrix equation .