List of usage examples for org.apache.commons.math3.linear NonSquareMatrixException NonSquareMatrixException
public NonSquareMatrixException(int wrong, int expected)
From source file:edu.cudenver.bios.matrix.MatrixUtilities.java
/** * Force a square RealMatrix to be symmetric. * * @param rm The RealMatrix./* w w w. j a v a 2 s . c om*/ * * @return The same RealMatrix, modified if necessary * to be symmetric. * * @throws NonSquareMatrixException if the RealMatrix is * not square. */ public static RealMatrix forceSymmetric(RealMatrix rm) { int m = rm.getRowDimension(); int n = rm.getColumnDimension(); if (m != n) { throw new NonSquareMatrixException(m, n); } for (int i = 0; i < m; ++i) { for (int j = i + 1; j < n; ++j) { double value = (rm.getEntry(i, j) + rm.getEntry(j, i)) / 2; rm.setEntry(i, j, value); rm.setEntry(j, i, value); } } return rm; }
From source file:com.bolatu.gezkoncsvlogger.GyroOrientation.RotationKalmanFilter.java
/** * Creates a new Kalman filter with the given process and measurement * models.//from w w w .j a va 2 s. c o m * * @param process * the model defining the underlying process dynamics * @param measurement * the model defining the given measurement characteristics * @throws NullArgumentException * if any of the given inputs is null (except for the control * matrix) * @throws NonSquareMatrixException * if the transition matrix is non square * @throws DimensionMismatchException * if the column dimension of the transition matrix does not * match the dimension of the initial state estimation vector * @throws MatrixDimensionMismatchException * if the matrix dimensions do not fit together */ public RotationKalmanFilter(final ProcessModel process, final MeasurementModel measurement) throws NullArgumentException, NonSquareMatrixException, DimensionMismatchException, MatrixDimensionMismatchException { MathUtils.checkNotNull(process); MathUtils.checkNotNull(measurement); this.processModel = process; this.measurementModel = measurement; transitionMatrix = processModel.getStateTransitionMatrix(); MathUtils.checkNotNull(transitionMatrix); transitionMatrixT = transitionMatrix.transpose(); // create an empty matrix if no control matrix was given if (processModel.getControlMatrix() == null) { controlMatrix = new Array2DRowRealMatrix(); } else { controlMatrix = processModel.getControlMatrix(); } measurementMatrix = measurementModel.getMeasurementMatrix(); MathUtils.checkNotNull(measurementMatrix); measurementMatrixT = measurementMatrix.transpose(); // check that the process and measurement noise matrices are not null // they will be directly accessed from the model as they may change // over time RealMatrix processNoise = processModel.getProcessNoise(); MathUtils.checkNotNull(processNoise); RealMatrix measNoise = measurementModel.getMeasurementNoise(); MathUtils.checkNotNull(measNoise); // set the initial state estimate to a zero vector if it is not // available from the process model if (processModel.getInitialStateEstimate() == null) { stateEstimation = new ArrayRealVector(transitionMatrix.getColumnDimension()); } else { stateEstimation = processModel.getInitialStateEstimate(); } if (transitionMatrix.getColumnDimension() != stateEstimation.getDimension()) { throw new DimensionMismatchException(transitionMatrix.getColumnDimension(), stateEstimation.getDimension()); } // initialize the error covariance to the process noise if it is not // available from the process model if (processModel.getInitialErrorCovariance() == null) { errorCovariance = processNoise.copy(); } else { errorCovariance = processModel.getInitialErrorCovariance(); } // sanity checks, the control matrix B may be null // A must be a square matrix if (!transitionMatrix.isSquare()) { throw new NonSquareMatrixException(transitionMatrix.getRowDimension(), transitionMatrix.getColumnDimension()); } // row dimension of B must be equal to A // if no control matrix is available, the row and column dimension will // be 0 if (controlMatrix != null && controlMatrix.getRowDimension() > 0 && controlMatrix.getColumnDimension() > 0 && controlMatrix.getRowDimension() != transitionMatrix.getRowDimension()) { throw new MatrixDimensionMismatchException(controlMatrix.getRowDimension(), controlMatrix.getColumnDimension(), transitionMatrix.getRowDimension(), controlMatrix.getColumnDimension()); } // Q must be equal to A MatrixUtils.checkAdditionCompatible(transitionMatrix, processNoise); // column dimension of H must be equal to row dimension of A if (measurementMatrix.getColumnDimension() != transitionMatrix.getRowDimension()) { throw new MatrixDimensionMismatchException(measurementMatrix.getRowDimension(), measurementMatrix.getColumnDimension(), measurementMatrix.getRowDimension(), transitionMatrix.getRowDimension()); } // row dimension of R must be equal to row dimension of H if (measNoise.getRowDimension() != measurementMatrix.getRowDimension()) { throw new MatrixDimensionMismatchException(measNoise.getRowDimension(), measNoise.getColumnDimension(), measurementMatrix.getRowDimension(), measNoise.getColumnDimension()); } }
From source file:lirmm.inria.fr.math.BigSparseRealMatrixTest.java
/** * extracts the l and u matrices from compact lu representation *//*from w ww .j ava2 s . com*/ protected void splitLU(RealMatrix lu, double[][] lowerData, double[][] upperData) { if (!lu.isSquare()) { throw new NonSquareMatrixException(lu.getRowDimension(), lu.getColumnDimension()); } if (lowerData.length != lowerData[0].length) { throw new DimensionMismatchException(lowerData.length, lowerData[0].length); } if (upperData.length != upperData[0].length) { throw new DimensionMismatchException(upperData.length, upperData[0].length); } if (lowerData.length != upperData.length) { throw new DimensionMismatchException(lowerData.length, upperData.length); } if (lowerData.length != lu.getRowDimension()) { throw new DimensionMismatchException(lowerData.length, lu.getRowDimension()); } int n = lu.getRowDimension(); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (j < i) { lowerData[i][j] = lu.getEntry(i, j); upperData[i][j] = 0d; } else if (i == j) { lowerData[i][j] = 1d; upperData[i][j] = lu.getEntry(i, j); } else { lowerData[i][j] = 0d; upperData[i][j] = lu.getEntry(i, j); } } } }
From source file:lirmm.inria.fr.math.BigSparseRealMatrixTest.java
/** * Returns the result of applying the given row permutation to the matrix *///from ww w. ja v a 2 s . c om protected RealMatrix permuteRows(RealMatrix matrix, int[] permutation) { if (!matrix.isSquare()) { throw new NonSquareMatrixException(matrix.getRowDimension(), matrix.getColumnDimension()); } if (matrix.getRowDimension() != permutation.length) { throw new DimensionMismatchException(matrix.getRowDimension(), permutation.length); } int n = matrix.getRowDimension(); int m = matrix.getColumnDimension(); double out[][] = new double[m][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { out[i][j] = matrix.getEntry(permutation[i], j); } } return new BigSparseRealMatrix(out); }
From source file:org.pmad.gmm.HessenbergTransformer.java
/** * Build the transformation to Hessenberg form of a general matrix. * * @param matrix matrix to transform//from w w w . j ava2s . c o m * @throws NonSquareMatrixException if the matrix is not square */ public HessenbergTransformer(final RealMatrix matrix) { if (!matrix.isSquare()) { throw new NonSquareMatrixException(matrix.getRowDimension(), matrix.getColumnDimension()); } final int m = matrix.getRowDimension(); householderVectors = matrix.getData(); ort = new double[m]; cachedP = null; cachedPt = null; cachedH = null; // transform matrix transform(); }
From source file:org.pmad.gmm.SchurTransformer.java
/** * Build the transformation to Schur form of a general real matrix. * * @param matrix matrix to transform//w ww . j a v a2 s . co m * @throws NonSquareMatrixException if the matrix is not square */ public SchurTransformer(final RealMatrix matrix) { if (!matrix.isSquare()) { throw new NonSquareMatrixException(matrix.getRowDimension(), matrix.getColumnDimension()); } HessenbergTransformer transformer = new HessenbergTransformer(matrix); matrixT = transformer.getH().getData(); matrixP = transformer.getP().getData(); cachedT = null; cachedP = null; cachedPt = null; // transform matrix transform(); }
From source file:org.pmad.gmm.TriDiagonalTransformer.java
/** * Build the transformation to tridiagonal shape of a symmetrical matrix. * <p>The specified matrix is assumed to be symmetrical without any check. * Only the upper triangular part of the matrix is used.</p> * * @param matrix Symmetrical matrix to transform. * @throws NonSquareMatrixException if the matrix is not square. *//* ww w . j ava 2 s .co m*/ public TriDiagonalTransformer(RealMatrix matrix) { if (!matrix.isSquare()) { throw new NonSquareMatrixException(matrix.getRowDimension(), matrix.getColumnDimension()); } final int m = matrix.getRowDimension(); householderVectors = matrix.getData(); main = new double[m]; secondary = new double[m - 1]; cachedQ = null; cachedQt = null; cachedT = null; // transform matrix transform(); }