Java Code Examples for org.apache.commons.math3.linear.Array2DRowRealMatrix#getColumnDimension()

The following examples show how to use org.apache.commons.math3.linear.Array2DRowRealMatrix#getColumnDimension() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DataSet.java    From clust4j with Apache License 2.0 6 votes vote down vote up
public DataSet(Array2DRowRealMatrix data, int[] labels, String[] hdrz, MatrixFormatter formatter, boolean copyData) {
	
	/*// we should allow this behavior...
	if(null == labels)
		throw new IllegalArgumentException("labels cannot be null");
	*/

	if(null == data)
		throw new IllegalArgumentException("data cannot be null");
	if(null == hdrz)
		this.headers = genHeaders(data.getColumnDimension());
	else 
		this.headers = VecUtils.copy(hdrz);
	
	
	// Check to make sure dims match up...
	if((null != labels) && labels.length != data.getRowDimension())
		throw new DimensionMismatchException(labels.length, data.getRowDimension());
	if(this.headers.length != data.getColumnDimension())
		throw new DimensionMismatchException(this.headers.length, data.getColumnDimension());
	
	this.data = copyData ? (Array2DRowRealMatrix)data.copy() : data;
	this.labels = VecUtils.copy(labels);
	this.formatter = null == formatter ? DEF_FORMATTER : formatter;
}
 
Example 2
Source File: LibCommonsMath.java    From systemds with Apache License 2.0 5 votes vote down vote up
/**
 * Function to compute matrix inverse via matrix decomposition.
 * 
 * @param in commons-math3 Array2DRowRealMatrix
 * @return matrix block
 */
private static MatrixBlock computeMatrixInverse(Array2DRowRealMatrix in) {
	if ( !in.isSquare() )
		throw new DMLRuntimeException("Input to inv() must be square matrix -- given: a " + in.getRowDimension() + "x" + in.getColumnDimension() + " matrix.");
	
	QRDecomposition qrdecompose = new QRDecomposition(in);
	DecompositionSolver solver = qrdecompose.getSolver();
	RealMatrix inverseMatrix = solver.getInverse();

	return DataConverter.convertToMatrixBlock(inverseMatrix.getData());
}
 
Example 3
Source File: LibCommonsMath.java    From systemds with Apache License 2.0 5 votes vote down vote up
/**
 * Function to compute Cholesky decomposition of the given input matrix. 
 * The input must be a real symmetric positive-definite matrix.
 * 
 * @param in commons-math3 Array2DRowRealMatrix
 * @return matrix block
 */
private static MatrixBlock computeCholesky(Array2DRowRealMatrix in) {
	if ( !in.isSquare() )
		throw new DMLRuntimeException("Input to cholesky() must be square matrix -- given: a " + in.getRowDimension() + "x" + in.getColumnDimension() + " matrix.");
	CholeskyDecomposition cholesky = new CholeskyDecomposition(in, 1e-14,
		CholeskyDecomposition.DEFAULT_ABSOLUTE_POSITIVITY_THRESHOLD);
	RealMatrix rmL = cholesky.getL();
	return DataConverter.convertToMatrixBlock(rmL.getData());
}
 
Example 4
Source File: LibCommonsMath.java    From systemds with Apache License 2.0 5 votes vote down vote up
/**
 * Function to compute matrix inverse via matrix decomposition.
 * 
 * @param in commons-math3 Array2DRowRealMatrix
 * @return matrix block
 */
private static MatrixBlock computeMatrixInverse(Array2DRowRealMatrix in) {
	if ( !in.isSquare() )
		throw new DMLRuntimeException("Input to inv() must be square matrix -- given: a " + in.getRowDimension() + "x" + in.getColumnDimension() + " matrix.");
	
	QRDecomposition qrdecompose = new QRDecomposition(in);
	DecompositionSolver solver = qrdecompose.getSolver();
	RealMatrix inverseMatrix = solver.getInverse();

	return DataConverter.convertToMatrixBlock(inverseMatrix.getData());
}
 
Example 5
Source File: LibCommonsMath.java    From systemds with Apache License 2.0 5 votes vote down vote up
/**
 * Function to compute Cholesky decomposition of the given input matrix. 
 * The input must be a real symmetric positive-definite matrix.
 * 
 * @param in commons-math3 Array2DRowRealMatrix
 * @return matrix block
 */
private static MatrixBlock computeCholesky(Array2DRowRealMatrix in) {
	if ( !in.isSquare() )
		throw new DMLRuntimeException("Input to cholesky() must be square matrix -- given: a " + in.getRowDimension() + "x" + in.getColumnDimension() + " matrix.");
	CholeskyDecomposition cholesky = new CholeskyDecomposition(in, 1e-14,
		CholeskyDecomposition.DEFAULT_ABSOLUTE_POSITIVITY_THRESHOLD);
	RealMatrix rmL = cholesky.getL();
	return DataConverter.convertToMatrixBlock(rmL.getData());
}
 
Example 6
Source File: LinearAlgebra.java    From finmath-lib with Apache License 2.0 5 votes vote down vote up
/**
 * Find a solution of the linear equation A x = b where
 * <ul>
 * <li>A is an n x m - matrix given as double[n][m]</li>
 * <li>b is an m - vector given as double[m],</li>
 * <li>x is an n - vector given as double[n],</li>
 * </ul>
 *
 * @param matrixA The matrix A (left hand side of the linear equation).
 * @param b The vector (right hand of the linear equation).
 * @return A solution x to A x = b.
 */
public static double[] solveLinearEquation(final double[][] matrixA, final double[] b) {

	if(isSolverUseApacheCommonsMath) {
		final Array2DRowRealMatrix matrix = new Array2DRowRealMatrix(matrixA);

		DecompositionSolver solver;
		if(matrix.getColumnDimension() == matrix.getRowDimension()) {
			solver = new LUDecomposition(matrix).getSolver();
		}
		else {
			solver = new QRDecomposition(new Array2DRowRealMatrix(matrixA)).getSolver();
		}

		// Using SVD - very slow
		//			solver = new SingularValueDecomposition(new Array2DRowRealMatrix(A)).getSolver();

		return solver.solve(new Array2DRowRealMatrix(b)).getColumn(0);
	}
	else {
		return org.jblas.Solve.solve(new org.jblas.DoubleMatrix(matrixA), new org.jblas.DoubleMatrix(b)).data;

		// For use of colt:
		// cern.colt.matrix.linalg.Algebra linearAlgebra = new cern.colt.matrix.linalg.Algebra();
		// return linearAlgebra.solve(new DenseDoubleMatrix2D(A), linearAlgebra.transpose(new DenseDoubleMatrix2D(new double[][] { b }))).viewColumn(0).toArray();

		// For use of parallel colt:
		// return new cern.colt.matrix.tdouble.algo.decomposition.DenseDoubleLUDecomposition(new cern.colt.matrix.tdouble.impl.DenseDoubleMatrix2D(A)).solve(new cern.colt.matrix.tdouble.impl.DenseDoubleMatrix1D(b)).toArray();
	}
}
 
Example 7
Source File: LinearAlgebra.java    From finmath-lib with Apache License 2.0 5 votes vote down vote up
/**
 * Find a solution of the linear equation A x = b where
 * <ul>
 * <li>A is an n x m - matrix given as double[n][m]</li>
 * <li>b is an m - vector given as double[m],</li>
 * <li>x is an n - vector given as double[n],</li>
 * </ul>
 *
 * @param matrixA The matrix A (left hand side of the linear equation).
 * @param b The vector (right hand of the linear equation).
 * @return A solution x to A x = b.
 */
public static double[] solveLinearEquation(final double[][] matrixA, final double[] b) {

	if(isSolverUseApacheCommonsMath) {
		final Array2DRowRealMatrix matrix = new Array2DRowRealMatrix(matrixA);

		DecompositionSolver solver;
		if(matrix.getColumnDimension() == matrix.getRowDimension()) {
			solver = new LUDecomposition(matrix).getSolver();
		}
		else {
			solver = new QRDecomposition(new Array2DRowRealMatrix(matrixA)).getSolver();
		}

		// Using SVD - very slow
		//			solver = new SingularValueDecomposition(new Array2DRowRealMatrix(A)).getSolver();

		return solver.solve(new Array2DRowRealMatrix(b)).getColumn(0);
	}
	else {
		return org.jblas.Solve.solve(new org.jblas.DoubleMatrix(matrixA), new org.jblas.DoubleMatrix(b)).data;

		// For use of colt:
		// cern.colt.matrix.linalg.Algebra linearAlgebra = new cern.colt.matrix.linalg.Algebra();
		// return linearAlgebra.solve(new DenseDoubleMatrix2D(A), linearAlgebra.transpose(new DenseDoubleMatrix2D(new double[][] { b }))).viewColumn(0).toArray();

		// For use of parallel colt:
		// return new cern.colt.matrix.tdouble.algo.decomposition.DenseDoubleLUDecomposition(new cern.colt.matrix.tdouble.impl.DenseDoubleMatrix2D(A)).solve(new cern.colt.matrix.tdouble.impl.DenseDoubleMatrix1D(b)).toArray();
	}
}