Matrix inverse or pseudoinverse

@param A matrix to invert
@return inverse(A) if A is square, pseudoinverse otherwise.
494827
public static double[][] inverse(double[][] A) {
    final int rows = A.length, cols = A[0].length;
    return rows == cols //
        ? (new LUDecomposition(A, rows, cols)).inverse() //
        : (new QRDecomposition(A, rows, cols)).inverse();
  }
931379
public Matrix4f invertAffine(Matrix4f dest) {
        float m11m00 = m00 * m11, m10m01 = m01 * m10, m10m02 = m02 * m10;
        float m12m00 = m00 * m12, m12m01 = m01 * m12, m11m02 = m02 * m11;
        float det = (m11m00 - m10m01) * m22 + (m10m02 - m12m00) * m21 + (m12m01 - m11m02) * m20;
        float s = 1.0f / det;
        float nm00, nm01, nm02, nm10, nm11, nm12, nm20, nm21, nm22, nm30, nm31, nm32;
        float m10m22 = m10 * m22, m10m21 = m10 * m21, m11m22 = m11 * m22;
        float m11m20 = m11 * m20, m12m21 = m12 * m21, m12m20 = m12 * m20;
        float m20m02 = m20 * m02, m20m01 = m20 * m01, m21m02 = m21 * m02;
        float m21m00 = m21 * m00, m22m01 = m22 * m01, m22m00 = m22 * m00;
        nm00 = (m11m22 - m12m21) * s;
        nm01 = (m21m02 - m22m01) * s;
        nm02 = (m12m01 - m11m02) * s;
        nm10 = (m12m20 - m10m22) * s;
        nm11 = (m22m00 - m20m02) * s;
        nm12 = (m10m02 - m12m00) * s;
        nm20 = (m10m21 - m11m20) * s;
        nm21 = (m20m01 - m21m00) * s;
        nm22 = (m11m00 - m10m01) * s;
        nm30 = (m10m22 * m31 - m10m21 * m32 + m11m20 * m32 - m11m22 * m30 + m12m21 * m30 - m12m20 * m31) * s;
        nm31 = (m20m02 * m31 - m20m01 * m32 + m21m00 * m32 - m21m02 * m30 + m22m01 * m30 - m22m00 * m31) * s;
        nm32 = (m11m02 * m30 - m12m01 * m30 + m12m00 * m31 - m10m02 * m31 + m10m01 * m32 - m11m00 * m32) * s;
        dest._m00(nm00);
        dest._m01(nm01);
        dest._m02(nm02);
        dest._m03(0.0f);
        dest._m10(nm10);
        dest._m11(nm11);
        dest._m12(nm12);
        dest._m13(0.0f);
        dest._m20(nm20);
        dest._m21(nm21);
        dest._m22(nm22);
        dest._m23(0.0f);
        dest._m30(nm30);
        dest._m31(nm31);
        dest._m32(nm32);
        dest._m33(1.0f);
        dest._properties(PROPERTY_AFFINE);
        return dest;
    }
1163175
function() {
            	if (!this.isSquare() || this.isSingular()) { return null; }
            	var n = this.elements.length, i = n, j;
            	var M = this.augment(Matrix$b.I(n)).toRightTriangular();
            	var np = M.elements[0].length, p, els, divisor;
            	var inverse_elements = [], new_element;
            	// Matrix is non-singular so there will be no zeros on the diagonal
            	// Cycle through rows from last to first
            	while (i--) {
            	    // First, normalise diagonal elements to 1
            	    els = [];
            	    inverse_elements[i] = [];
            	    divisor = M.elements[i][i];
            	    for (p = 0; p < np; p++) {
                    new_element = M.elements[i][p] / divisor;
            		els.push(new_element);
            		// Shuffle off the current row of the right hand side into the results
            		// array as it will not be modified by later runs through this loop
            		if (p >= n) { inverse_elements[i].push(new_element); }
            	    }
            	    M.elements[i] = els;
            	    // Then, subtract this row from those above it to
            	    // give the identity matrix on the left hand side
            	    j = i;
            	    while (j--) {
            		els = [];
            		for (p = 0; p < np; p++) {
            		    els.push(M.elements[j][p] - M.elements[i][p] * M.elements[j][i]);
            		}
            		M.elements[j] = els;
            	    }
            	}
            	return Matrix$b.create(inverse_elements);
                }
479201
public Matrix4d invertPerspective(Matrix4d dest) {
        double a =  1.0 / (m00 * m11);
        double l = -1.0 / (m23 * m32);
        dest.set(m11 * a, 0, 0, 0,
                 0, m00 * a, 0, 0,
                 0, 0, 0, -m23 * l,
                 0, 0, -m32 * l, m22 * l);
        return dest;
    }
681606
public Matrix3x2d invert(Matrix3x2d dest) {
        // client must make sure that matrix is invertible
        double s = 1.0 / (m00 * m11 - m01 * m10);
        double nm00 =  m11 * s;
        double nm01 = -m01 * s;
        double nm10 = -m10 * s;
        double nm11 =  m00 * s;
        double nm20 = (m10 * m21 - m20 * m11) * s;
        double nm21 = (m20 * m01 - m00 * m21) * s;
        dest.m00 = nm00;
        dest.m01 = nm01;
        dest.m10 = nm10;
        dest.m11 = nm11;
        dest.m20 = nm20;
        dest.m21 = nm21;
        return dest;
    }
700385
public void inverse(Transformation2D inverse) {
		double det = xx * yy - xy * yx;

		if (det == 0) {
			inverse.setZero();
			return;
		}

		det = 1 / det;

		inverse.xd = (xy * yd - xd * yy) * det;
		inverse.yd = (xd * yx - xx * yd) * det;
		inverse.xx = yy * det;
		inverse.xy = -xy * det;
		inverse.yx = -yx * det;
		inverse.yy = xx * det;
	}
1150632
public void setToInverseL( double a[] ) {
        // TODO reorder these operations to avoid cache misses
        
        // inverts the lower triangular system and saves the result
        // in the upper triangle to minimize cache misses
        for( int i =0; i < n; i++ ) {
            double el_ii = t[i*n+i];
            for( int j = 0; j <= i; j++ ) {
                double sum = (i==j) ? 1.0 : 0;
                for( int k=i-1; k >=j; k-- ) {
                    sum -= t[i*n+k]*a[j*n+k];
                }
                a[j*n+i] = sum / el_ii;
            }
        }
        // solve the system and handle the previous solution being in the upper triangle
        // takes advantage of symmetry
        for( int i=n-1; i>=0; i-- ) {
            double el_ii = t[i*n+i];

            for( int j = 0; j <= i; j++ ) {
                double sum = (i<j) ? 0 : a[j*n+i];
                for( int k=i+1;k<n;k++) {
                    sum -= t[k*n+i]*a[j*n+k];
                }
                a[i*n+j] = a[j*n+i] = sum / el_ii;
            }
        }
    }
1169640
public double[][] inverse() {
    // Build permuted identity matrix efficiently:
    double[][] b = new double[piv.length][m];
    for(int i = 0; i < piv.length; i++) {
      b[piv[i]][i] = 1.;
    }
    return solveInplace(b);
  }
1312264
private static double[] reversed(double[] a) {
    // TODO: there doesn't appear to be a nicer version in Java, unfortunately.
    Arrays.sort(a);
    for(int i = 0, j = a.length - 1; i < j; i++, j--) {
      double tmp = a[i];
      a[i] = a[j];
      a[j] = tmp;
    }
    return a;
  }
1169676
public DenseMatrix inverse() {
        int m = lu.nrows();
        int n = lu.ncols();

        if (m != n) {
            throw new IllegalArgumentException(String.format("Matrix is not square: %d x %d", m, n));
        }

        DenseMatrix inv = Matrix.zeros(n, n);
        for (int i = 0; i < n; i++) {
            inv.set(i, piv[i], 1.0);
        }

        solve(inv);
        return inv;
    }
812627
function reverse(a) {
  var len = a.length;
  for (var i = 0; i < Math.floor(len/2); i++) {
    [a[i], a[len-i-1]] = [a[len-i-1], a[i]];
  }
  return a;
}
783628
public static boolean isDiagonalPositive( DMatrixRMaj a ) {
        for( int i = 0; i < a.numRows; i++ ) {
            if( !(a.get(i,i) >= 0) )
                return false;
        }
        return true;
    }
54798
function() {
    if (!this.isSquare() || this.isSingular()) { return null; }
    var ni = this.elements.length, ki = ni, i, j;
    var M = this.augment(Matrix.I(ni)).toRightTriangular();
    var np, kp = M.elements[0].length, p, els, divisor;
    var inverse_elements = [], new_element;
    // Matrix is non-singular so there will be no zeros on the diagonal
    // Cycle through rows from last to first
    do { i = ni - 1;
      // First, normalise diagonal elements to 1
      els = []; np = kp;
      inverse_elements[i] = [];
      divisor = M.elements[i][i];
      do { p = kp - np;
        new_element = M.elements[i][p] / divisor;
        els.push(new_element);
        // Shuffle of the current row of the right hand side into the results
        // array as it will not be modified by later runs through this loop
        if (p >= ki) { inverse_elements[i].push(new_element); }
      } while (--np);
      M.elements[i] = els;
      // Then, subtract this row from those above it to
      // give the identity matrix on the left hand side
      for (j = 0; j < i; j++) {
        els = []; np = kp;
        do { p = kp - np;
          els.push(M.elements[j][p] - M.elements[i][p] * M.elements[j][i]);
        } while (--np);
        M.elements[j] = els;
      }
    } while (--ni);
    return Matrix.create(inverse_elements);
  }
1170455
public static INDArray pinvert(INDArray arr, boolean inPlace) {

        // TODO : do it natively instead of relying on commons-maths

        RealMatrix realMatrix = CheckUtil.convertToApacheMatrix(arr);
        QRDecomposition decomposition = new QRDecomposition(realMatrix, 0);
        DecompositionSolver solver = decomposition.getSolver();

        if (!solver.isNonSingular()) {
            throw new IllegalArgumentException("invalid array: must be singular matrix");
        }

        RealMatrix pinvRM = solver.getInverse();

        INDArray pseudoInverse = CheckUtil.convertFromApacheMatrix(pinvRM, arr.dataType());

        if (inPlace)
            arr.assign(pseudoInverse);
        return pseudoInverse;

    }
305442
public static FloatMatrix cholesky(FloatMatrix A) {
        FloatMatrix result = A.dup();
        int info = NativeBlas.spotrf('U', A.rows, result.data, 0, A.rows);
        if (info < 0) {
            throw new LapackArgumentException("DPOTRF", -info);
        } else if (info > 0) {
            throw new LapackPositivityException("DPOTRF", "Minor " + info + " was negative. Matrix must be positive definite.");
        }
        clearLower(result);
        return result;
    }
443386
public static SymmDenseEVD factorize(Matrix A) throws NotConvergedException {
        return new SymmDenseEVD(A.numRows(), true)
                .factor(new UpperSymmDenseMatrix(A));
    }
1123424
@Override
    public boolean decompose(DMatrixRBlock A) {
        if( A.numCols != A.numRows )
            throw new IllegalArgumentException("A must be square");

        this.T = A;

        if( lower )
            return decomposeLower();
        else
            return decomposeUpper();
    }