Example usage for org.apache.commons.math3.linear MatrixDimensionMismatchException MatrixDimensionMismatchException

List of usage examples for org.apache.commons.math3.linear MatrixDimensionMismatchException MatrixDimensionMismatchException

Introduction

In this page you can find the example usage for org.apache.commons.math3.linear MatrixDimensionMismatchException MatrixDimensionMismatchException.

Prototype

public MatrixDimensionMismatchException(int wrongRowDim, int wrongColDim, int expectedRowDim,
        int expectedColDim) 

Source Link

Document

Construct an exception from the mismatched dimensions.

Usage

From source file:com.bolatu.gezkoncsvlogger.GyroOrientation.RotationKalmanFilter.java

/**
 * Creates a new Kalman filter with the given process and measurement
 * models.// w  ww .java2  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());
    }
}