lib/ops.js
function slogdet(A) {
    if (A.shape.length !== 2) {
        throw new Error('Input is not matrix');
    }
    if (A.shape[0] !== A.shape[1]) {
        throw new Error('Input is not square matrix');
    }
    var m = A.shape[0];
    var copyA = A.clone();
    copyA.swapOrder();
    var ipiv = new common_1.NDArray({ shape: [m], datatype: 'i32' });
    lapack.getrf(copyA.data, m, m, ipiv.data);
    copyA.swapOrder();
    // Multiply diagonal elements of Upper triangular matrix to get determinant
    // For large matrices this value could get really big, hence we add 
    // natural logarithms of the diagonal elements and save the sign
    // separately
    var sign_acc = 1;
    var log_acc = 0;
    // Note: The pivot vector affects the sign of the determinant
    // I do not understand why, but this is what numpy implementation does
    for (var i = 0; i < m; i++) {
        if (ipiv.get(i) !== i + 1) {
            sign_acc = -sign_acc;
        }
    }
    for (var i = 0; i < m; i++) {
        var e = copyA.get(i, i);
        // TODO:Complex
        var e_abs = Math.abs(e);
        var e_sign = e / e_abs;
        sign_acc *= e_sign;
        log_acc += Math.log(e_abs);
    }
    return [sign_acc, log_acc];
}
function slogdet(A) {
    if (A.shape.length !== 2) {
        throw new Error('Input is not matrix');
    }
    if (A.shape[0] !== A.shape[1]) {
        throw new Error('Input is not square matrix');
    }
    var m = A.shape[0];
    var copyA = A.clone();
    copyA.swapOrder();
    var ipiv = new common_1.NDArray({ shape: [m], datatype: 'i32' });
    lapack.getrf(copyA.data, m, m, ipiv.data);
    copyA.swapOrder();
    // Multiply diagonal elements of Upper triangular matrix to get determinant
    // For large matrices this value could get really big, hence we add 
    // natural logarithms of the diagonal elements and save the sign
    // separately
    var sign_acc = 1;
    var log_acc = 0;
    // Note: The pivot vector affects the sign of the determinant
    // I do not understand why, but this is what numpy implementation does
    for (var i = 0; i < m; i++) {
        if (ipiv.get(i) !== i + 1) {
            sign_acc = -sign_acc;
        }
    }
    for (var i = 0; i < m; i++) {
        var e = copyA.get(i, i);
        // TODO:Complex
        var e_abs = Math.abs(e);
        var e_sign = e / e_abs;
        sign_acc *= e_sign;
        log_acc += Math.log(e_abs);
    }
    return [sign_acc, log_acc];
}
function slogdet ( A ) { if ( A . shape . length !== 2 ) { throw new Error ( 'Input is not matrix' ) ; } if ( A . shape [ 0 ] !== A . shape [ 1 ] ) { throw new Error ( 'Input is not square matrix' ) ; } var m = A . shape [ 0 ] ; var copyA = A . clone ( ) ; copyA . swapOrder ( ) ; var ipiv = new common_1 . NDArray ( { shape : [ m ] , datatype : 'i32' } ) ; lapack . getrf ( copyA . data , m , m , ipiv . data ) ; copyA . swapOrder ( ) ; // Multiply diagonal elements of Upper triangular matrix to get determinant // For large matrices this value could get really big, hence we add  // natural logarithms of the diagonal elements and save the sign // separately var sign_acc = 1 ; var log_acc = 0 ; // Note: The pivot vector affects the sign of the determinant // I do not understand why, but this is what numpy implementation does for ( var i = 0 ; i < m ; i ++ ) { if ( ipiv . get ( i ) !== i + 1 ) { sign_acc = - sign_acc ; } } for ( var i = 0 ; i < m ; i ++ ) { var e = copyA . get ( i , i ) ; // TODO:Complex var e_abs = Math . abs ( e ) ; var e_sign = e / e_abs ; sign_acc *= e_sign ; log_acc += Math . log ( e_abs ) ; } return [ sign_acc , log_acc ] ; }
Compute sign and natural logarithm of the determinant of given Matrix
If an array has a very small or very large determinant, then a call to
`det` may overflow or underflow. This routine is more robust against such
issues, because it computes the logarithm of the determinant rather than
the determinant itself.
@param A Square matrix to compute sign and log-determinant of
Compute sign and natural logarithm of the determinant of given Matrix If an array has a very small or very large determinant then a call to det may overflow or underflow . This routine is more robust against such issues because it computes the logarithm of the determinant rather than the determinant itself .