DefaultProcessModel.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.math4.legacy.filter;

  18. import org.apache.commons.math4.legacy.exception.DimensionMismatchException;
  19. import org.apache.commons.math4.legacy.exception.NoDataException;
  20. import org.apache.commons.math4.legacy.exception.NullArgumentException;
  21. import org.apache.commons.math4.legacy.linear.Array2DRowRealMatrix;
  22. import org.apache.commons.math4.legacy.linear.ArrayRealVector;
  23. import org.apache.commons.math4.legacy.linear.RealMatrix;
  24. import org.apache.commons.math4.legacy.linear.RealVector;

  25. /**
  26.  * Default implementation of a {@link ProcessModel} for the use with a {@link KalmanFilter}.
  27.  *
  28.  * @since 3.0
  29.  */
  30. public class DefaultProcessModel implements ProcessModel {
  31.     /**
  32.      * The state transition matrix, used to advance the internal state estimation each time-step.
  33.      */
  34.     private final RealMatrix stateTransitionMatrix;

  35.     /**
  36.      * The control matrix, used to integrate a control input into the state estimation.
  37.      */
  38.     private final RealMatrix controlMatrix;

  39.     /** The process noise covariance matrix. */
  40.     private final RealMatrix processNoiseCovMatrix;

  41.     /** The initial state estimation of the observed process. */
  42.     private final RealVector initialStateEstimateVector;

  43.     /** The initial error covariance matrix of the observed process. */
  44.     private final RealMatrix initialErrorCovMatrix;

  45.     /**
  46.      * Create a new {@link ProcessModel}, taking double arrays as input parameters.
  47.      *
  48.      * @param stateTransition
  49.      *            the state transition matrix
  50.      * @param control
  51.      *            the control matrix
  52.      * @param processNoise
  53.      *            the process noise matrix
  54.      * @param initialStateEstimate
  55.      *            the initial state estimate vector
  56.      * @param initialErrorCovariance
  57.      *            the initial error covariance matrix
  58.      * @throws NullArgumentException
  59.      *             if any of the input arrays is {@code null}
  60.      * @throws NoDataException
  61.      *             if any row / column dimension of the input matrices is zero
  62.      * @throws DimensionMismatchException
  63.      *             if any of the input matrices is non-rectangular
  64.      */
  65.     public DefaultProcessModel(final double[][] stateTransition,
  66.                                final double[][] control,
  67.                                final double[][] processNoise,
  68.                                final double[] initialStateEstimate,
  69.                                final double[][] initialErrorCovariance)
  70.             throws NullArgumentException, NoDataException, DimensionMismatchException {

  71.         this(new Array2DRowRealMatrix(stateTransition),
  72.                 new Array2DRowRealMatrix(control),
  73.                 new Array2DRowRealMatrix(processNoise),
  74.                 new ArrayRealVector(initialStateEstimate),
  75.                 new Array2DRowRealMatrix(initialErrorCovariance));
  76.     }

  77.     /**
  78.      * Create a new {@link ProcessModel}, taking double arrays as input parameters.
  79.      * <p>
  80.      * The initial state estimate and error covariance are omitted and will be initialized by the
  81.      * {@link KalmanFilter} to default values.
  82.      *
  83.      * @param stateTransition
  84.      *            the state transition matrix
  85.      * @param control
  86.      *            the control matrix
  87.      * @param processNoise
  88.      *            the process noise matrix
  89.      * @throws NullArgumentException
  90.      *             if any of the input arrays is {@code null}
  91.      * @throws NoDataException
  92.      *             if any row / column dimension of the input matrices is zero
  93.      * @throws DimensionMismatchException
  94.      *             if any of the input matrices is non-rectangular
  95.      */
  96.     public DefaultProcessModel(final double[][] stateTransition,
  97.                                final double[][] control,
  98.                                final double[][] processNoise)
  99.             throws NullArgumentException, NoDataException, DimensionMismatchException {

  100.         this(new Array2DRowRealMatrix(stateTransition),
  101.                 new Array2DRowRealMatrix(control),
  102.                 new Array2DRowRealMatrix(processNoise), null, null);
  103.     }

  104.     /**
  105.      * Create a new {@link ProcessModel}, taking double arrays as input parameters.
  106.      *
  107.      * @param stateTransition
  108.      *            the state transition matrix
  109.      * @param control
  110.      *            the control matrix
  111.      * @param processNoise
  112.      *            the process noise matrix
  113.      * @param initialStateEstimate
  114.      *            the initial state estimate vector
  115.      * @param initialErrorCovariance
  116.      *            the initial error covariance matrix
  117.      */
  118.     public DefaultProcessModel(final RealMatrix stateTransition,
  119.                                final RealMatrix control,
  120.                                final RealMatrix processNoise,
  121.                                final RealVector initialStateEstimate,
  122.                                final RealMatrix initialErrorCovariance) {
  123.         this.stateTransitionMatrix = stateTransition;
  124.         this.controlMatrix = control;
  125.         this.processNoiseCovMatrix = processNoise;
  126.         this.initialStateEstimateVector = initialStateEstimate;
  127.         this.initialErrorCovMatrix = initialErrorCovariance;
  128.     }

  129.     /** {@inheritDoc} */
  130.     @Override
  131.     public RealMatrix getStateTransitionMatrix() {
  132.         return stateTransitionMatrix;
  133.     }

  134.     /** {@inheritDoc} */
  135.     @Override
  136.     public RealMatrix getControlMatrix() {
  137.         return controlMatrix;
  138.     }

  139.     /** {@inheritDoc} */
  140.     @Override
  141.     public RealMatrix getProcessNoise() {
  142.         return processNoiseCovMatrix;
  143.     }

  144.     /** {@inheritDoc} */
  145.     @Override
  146.     public RealVector getInitialStateEstimate() {
  147.         return initialStateEstimateVector;
  148.     }

  149.     /** {@inheritDoc} */
  150.     @Override
  151.     public RealMatrix getInitialErrorCovariance() {
  152.         return initialErrorCovMatrix;
  153.     }
  154. }