All Downloads are FREE. Search and download functionalities are using the official Maven repository.

mikera.matrixx.algo.PseudoInverse Maven / Gradle / Ivy

Go to download

Fast double-precision vector and matrix maths library for Java, supporting N-dimensional numeric arrays.

There is a newer version: 0.67.0
Show newest version
package mikera.matrixx.algo;

import mikera.matrixx.AMatrix;
import mikera.matrixx.decompose.ISVDResult;
import mikera.matrixx.decompose.SVD;
import mikera.matrixx.impl.DiagonalMatrix;
import mikera.vectorz.Vector;

public class PseudoInverse {
	/**
	 * Computes the Moore–Penrose pseudoinverse of a matrix
	 * 
	 * @param a
	 * @return
	 */
	public static AMatrix calculate(AMatrix a) {
		// compute a compact SVD decomposition. All singular values should be positive!
		ISVDResult svd = SVD.decomposeNonZero(a);
		
		Vector s=svd.getSingularValues().toVector();
		s.reciprocal(); // reciprocal of singular values
		
		AMatrix U=svd.getU();
		AMatrix V=svd.getV();
		
		return V.innerProduct(DiagonalMatrix.create(s).innerProduct(U.getTranspose()));
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy