com.gengoai.apollo.math.linalg.decompose.LUDecomposition Maven / Gradle / Ivy
package com.gengoai.apollo.math.linalg.decompose;
import com.gengoai.Validation;
import com.gengoai.apollo.math.linalg.DenseMatrix;
import com.gengoai.apollo.math.linalg.NDArray;
import com.gengoai.apollo.math.linalg.RealMatrixWrapper;
import org.jblas.Decompose;
import org.jblas.DoubleMatrix;
import static com.gengoai.apollo.math.linalg.NDArrayFactory.ND;
/**
* Performs LU Decomposition on the
* given input NDArray. The returned array is in order {L, U, P}
*
* @author David B. Bracewell
*/
public class LUDecomposition extends Decomposition {
private static final long serialVersionUID = 1L;
/**
* Instantiates a new Lu decomposition.
*/
public LUDecomposition() {
super(3);
}
@Override
protected NDArray[] onMatrix(NDArray m) {
Validation.checkArgument(m.shape().isSquare(), "Only square matrices are supported");
if (m instanceof DenseMatrix) {
Decompose.LUDecomposition r = Decompose.lu(m.toDoubleMatrix()[0]);
return new NDArray[]{new DenseMatrix(r.l),
new DenseMatrix(r.u),
new DenseMatrix(r.p)};
}
org.apache.commons.math3.linear.LUDecomposition luDecomposition =
new org.apache.commons.math3.linear.LUDecomposition(new RealMatrixWrapper(m));
return new NDArray[]{
ND.array(luDecomposition.getL().getData()),
ND.array(luDecomposition.getU().getData()),
ND.array(luDecomposition.getP().getData()),
};
}
}// END OF LUDecomposition
© 2015 - 2025 Weber Informatics LLC | Privacy Policy