39 #ifndef KALMANLINEARALGEBRA_H 40 #define KALMANLINEARALGEBRA_H 42 #include "boost/numeric/ublas/lu.hpp" 43 #include "boost/numeric/ublas/matrix.hpp" 44 #include "boost/numeric/ublas/symmetric.hpp" 45 #include "boost/numeric/ublas/vector.hpp" 46 #include "boost/serialization/array_wrapper.hpp" 52 namespace ublas = boost::numeric::ublas;
57 typedef ublas::vector<double, ublas::bounded_array<double, N>>
type;
63 typedef ublas::symmetric_matrix<double,
66 ublas::bounded_array<double, N*(N + 1) / 2>>
71 template <
int N,
int M>
73 typedef ublas::matrix<double, ublas::row_major, ublas::bounded_array<double, N * M>>
type;
107 template <
class T,
class TRI,
class L,
class A>
108 bool syminvert(ublas::symmetric_matrix<T, TRI, L, A>& m)
110 typedef typename ublas::symmetric_matrix<T, TRI, L, A>::size_type size_type;
111 typedef typename ublas::symmetric_matrix<T, TRI, L, A>::value_type value_type;
117 for (size_type i = 0; i < m.size1(); ++i) {
118 for (size_type j = 0; j <= i; ++j) {
120 value_type ele = m(i, j);
122 for (size_type k = 0; k < j; ++k)
123 ele -= m(k, k) * m(i, k) * m(j, k);
128 if (ele == 0.)
return false;
145 for (size_type i = 0; i < m.size1(); ++i) {
146 for (size_type j = 0; j <= i; ++j) {
148 value_type ele = m(i, j);
152 if (i == j) m(i, i) = 1. / ele;
157 value_type
sum = -ele;
158 for (size_type k = j + 1; k < i; ++k)
159 sum -= m(i, k) * m(k, j);
167 for (size_type i = 0; i < m.size1(); ++i) {
168 for (size_type j = 0; j <= i; ++j) {
170 value_type
sum = m(i, i);
171 if (i != j) sum *= m(i, j);
173 for (size_type k = i + 1; k < m.size1(); ++k)
174 sum += m(k, k) * m(k, i) * m(k, j);
188 template <
class T,
class L,
class A>
193 if (m.size1() != m.size2())
return false;
197 ublas::permutation_matrix<std::size_t> pm(m.size1());
201 ublas::matrix<T, L, A> mcopy(m);
206 int res = lu_factorize(mcopy, pm);
207 if (res != 0)
return false;
211 m.assign(ublas::identity_matrix<T>(m.size1()));
215 lu_substitute(mcopy, pm, m);
225 typename M::value_type
trace(
const M& m)
227 typename M::size_type
n = std::min(m.size1(), m.size2());
228 typename M::value_type result = 0.;
230 for (
typename M::size_type i = 0; i <
n; ++i)
Kalman gain matrix, dimension 5xN.
KSymMatrix< 5 >::type TrackError
Track error matrix, dimension 5x5.
Define a shortened alias for ublas namespace.
M::value_type trace(const M &m)
KMatrix< 5, 5 >::type TrackMatrix
General 5x5 matrix.
KMatrix< 5, N >::type type
ublas::vector< double, ublas::bounded_array< double, N > > type
bool syminvert(ublas::symmetric_matrix< T, TRI, L, A > &m)
General matrix, dimension NxM.
bool invert(ublas::matrix< T, L, A > &m)
KVector< 5 >::type TrackVector
Track state vector, dimension 5.
Kalman H-matrix, dimension Nx5.
KMatrix< N, 5 >::type type
ublas::matrix< double, ublas::row_major, ublas::bounded_array< double, N *M > > type
Symmetric matrix, dimension NxN.