View Javadoc
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  
19  import org.apache.commons.math4.legacy.exception.DimensionMismatchException;
20  import org.apache.commons.math4.legacy.exception.NoDataException;
21  import org.apache.commons.math4.legacy.exception.NullArgumentException;
22  import org.apache.commons.math4.legacy.linear.Array2DRowRealMatrix;
23  import org.apache.commons.math4.legacy.linear.ArrayRealVector;
24  import org.apache.commons.math4.legacy.linear.RealMatrix;
25  import org.apache.commons.math4.legacy.linear.RealVector;
26  
27  /**
28   * Default implementation of a {@link ProcessModel} for the use with a {@link KalmanFilter}.
29   *
30   * @since 3.0
31   */
32  public class DefaultProcessModel implements ProcessModel {
33      /**
34       * The state transition matrix, used to advance the internal state estimation each time-step.
35       */
36      private final RealMatrix stateTransitionMatrix;
37  
38      /**
39       * The control matrix, used to integrate a control input into the state estimation.
40       */
41      private final RealMatrix controlMatrix;
42  
43      /** The process noise covariance matrix. */
44      private final RealMatrix processNoiseCovMatrix;
45  
46      /** The initial state estimation of the observed process. */
47      private final RealVector initialStateEstimateVector;
48  
49      /** The initial error covariance matrix of the observed process. */
50      private final RealMatrix initialErrorCovMatrix;
51  
52      /**
53       * Create a new {@link ProcessModel}, taking double arrays as input parameters.
54       *
55       * @param stateTransition
56       *            the state transition matrix
57       * @param control
58       *            the control matrix
59       * @param processNoise
60       *            the process noise matrix
61       * @param initialStateEstimate
62       *            the initial state estimate vector
63       * @param initialErrorCovariance
64       *            the initial error covariance matrix
65       * @throws NullArgumentException
66       *             if any of the input arrays is {@code null}
67       * @throws NoDataException
68       *             if any row / column dimension of the input matrices is zero
69       * @throws DimensionMismatchException
70       *             if any of the input matrices is non-rectangular
71       */
72      public DefaultProcessModel(final double[][] stateTransition,
73                                 final double[][] control,
74                                 final double[][] processNoise,
75                                 final double[] initialStateEstimate,
76                                 final double[][] initialErrorCovariance)
77              throws NullArgumentException, NoDataException, DimensionMismatchException {
78  
79          this(new Array2DRowRealMatrix(stateTransition),
80                  new Array2DRowRealMatrix(control),
81                  new Array2DRowRealMatrix(processNoise),
82                  new ArrayRealVector(initialStateEstimate),
83                  new Array2DRowRealMatrix(initialErrorCovariance));
84      }
85  
86      /**
87       * Create a new {@link ProcessModel}, taking double arrays as input parameters.
88       * <p>
89       * The initial state estimate and error covariance are omitted and will be initialized by the
90       * {@link KalmanFilter} to default values.
91       *
92       * @param stateTransition
93       *            the state transition matrix
94       * @param control
95       *            the control matrix
96       * @param processNoise
97       *            the process noise matrix
98       * @throws NullArgumentException
99       *             if any of the input arrays is {@code null}
100      * @throws NoDataException
101      *             if any row / column dimension of the input matrices is zero
102      * @throws DimensionMismatchException
103      *             if any of the input matrices is non-rectangular
104      */
105     public DefaultProcessModel(final double[][] stateTransition,
106                                final double[][] control,
107                                final double[][] processNoise)
108             throws NullArgumentException, NoDataException, DimensionMismatchException {
109 
110         this(new Array2DRowRealMatrix(stateTransition),
111                 new Array2DRowRealMatrix(control),
112                 new Array2DRowRealMatrix(processNoise), null, null);
113     }
114 
115     /**
116      * Create a new {@link ProcessModel}, taking double arrays as input parameters.
117      *
118      * @param stateTransition
119      *            the state transition matrix
120      * @param control
121      *            the control matrix
122      * @param processNoise
123      *            the process noise matrix
124      * @param initialStateEstimate
125      *            the initial state estimate vector
126      * @param initialErrorCovariance
127      *            the initial error covariance matrix
128      */
129     public DefaultProcessModel(final RealMatrix stateTransition,
130                                final RealMatrix control,
131                                final RealMatrix processNoise,
132                                final RealVector initialStateEstimate,
133                                final RealMatrix initialErrorCovariance) {
134         this.stateTransitionMatrix = stateTransition;
135         this.controlMatrix = control;
136         this.processNoiseCovMatrix = processNoise;
137         this.initialStateEstimateVector = initialStateEstimate;
138         this.initialErrorCovMatrix = initialErrorCovariance;
139     }
140 
141     /** {@inheritDoc} */
142     @Override
143     public RealMatrix getStateTransitionMatrix() {
144         return stateTransitionMatrix;
145     }
146 
147     /** {@inheritDoc} */
148     @Override
149     public RealMatrix getControlMatrix() {
150         return controlMatrix;
151     }
152 
153     /** {@inheritDoc} */
154     @Override
155     public RealMatrix getProcessNoise() {
156         return processNoiseCovMatrix;
157     }
158 
159     /** {@inheritDoc} */
160     @Override
161     public RealVector getInitialStateEstimate() {
162         return initialStateEstimateVector;
163     }
164 
165     /** {@inheritDoc} */
166     @Override
167     public RealMatrix getInitialErrorCovariance() {
168         return initialErrorCovMatrix;
169     }
170 }