Eigne Matrix Class Library

Dependents:   Eigen_test Odometry_test AttitudeEstimation_usingTicker MPU9250_Quaternion_Binary_Serial ... more

Eigen Matrix Class Library for mbed.

Finally, you can use Eigen on your mbed!!!

Committer:
ykuroda
Date:
Thu Oct 13 04:07:23 2016 +0000
Revision:
0:13a5d365ba16
First commint, Eigne Matrix Class Library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ykuroda 0:13a5d365ba16 1 // This file is part of Eigen, a lightweight C++ template library
ykuroda 0:13a5d365ba16 2 // for linear algebra.
ykuroda 0:13a5d365ba16 3 //
ykuroda 0:13a5d365ba16 4 // Copyright (C) 2007-2010 Benoit Jacob <jacob.benoit.1@gmail.com>
ykuroda 0:13a5d365ba16 5 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
ykuroda 0:13a5d365ba16 6 //
ykuroda 0:13a5d365ba16 7 // This Source Code Form is subject to the terms of the Mozilla
ykuroda 0:13a5d365ba16 8 // Public License v. 2.0. If a copy of the MPL was not distributed
ykuroda 0:13a5d365ba16 9 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
ykuroda 0:13a5d365ba16 10
ykuroda 0:13a5d365ba16 11 #ifndef EIGEN_MAP_H
ykuroda 0:13a5d365ba16 12 #define EIGEN_MAP_H
ykuroda 0:13a5d365ba16 13
ykuroda 0:13a5d365ba16 14 namespace Eigen {
ykuroda 0:13a5d365ba16 15
ykuroda 0:13a5d365ba16 16 /** \class Map
ykuroda 0:13a5d365ba16 17 * \ingroup Core_Module
ykuroda 0:13a5d365ba16 18 *
ykuroda 0:13a5d365ba16 19 * \brief A matrix or vector expression mapping an existing array of data.
ykuroda 0:13a5d365ba16 20 *
ykuroda 0:13a5d365ba16 21 * \tparam PlainObjectType the equivalent matrix type of the mapped data
ykuroda 0:13a5d365ba16 22 * \tparam MapOptions specifies whether the pointer is \c #Aligned, or \c #Unaligned.
ykuroda 0:13a5d365ba16 23 * The default is \c #Unaligned.
ykuroda 0:13a5d365ba16 24 * \tparam StrideType optionally specifies strides. By default, Map assumes the memory layout
ykuroda 0:13a5d365ba16 25 * of an ordinary, contiguous array. This can be overridden by specifying strides.
ykuroda 0:13a5d365ba16 26 * The type passed here must be a specialization of the Stride template, see examples below.
ykuroda 0:13a5d365ba16 27 *
ykuroda 0:13a5d365ba16 28 * This class represents a matrix or vector expression mapping an existing array of data.
ykuroda 0:13a5d365ba16 29 * It can be used to let Eigen interface without any overhead with non-Eigen data structures,
ykuroda 0:13a5d365ba16 30 * such as plain C arrays or structures from other libraries. By default, it assumes that the
ykuroda 0:13a5d365ba16 31 * data is laid out contiguously in memory. You can however override this by explicitly specifying
ykuroda 0:13a5d365ba16 32 * inner and outer strides.
ykuroda 0:13a5d365ba16 33 *
ykuroda 0:13a5d365ba16 34 * Here's an example of simply mapping a contiguous array as a \ref TopicStorageOrders "column-major" matrix:
ykuroda 0:13a5d365ba16 35 * \include Map_simple.cpp
ykuroda 0:13a5d365ba16 36 * Output: \verbinclude Map_simple.out
ykuroda 0:13a5d365ba16 37 *
ykuroda 0:13a5d365ba16 38 * If you need to map non-contiguous arrays, you can do so by specifying strides:
ykuroda 0:13a5d365ba16 39 *
ykuroda 0:13a5d365ba16 40 * Here's an example of mapping an array as a vector, specifying an inner stride, that is, the pointer
ykuroda 0:13a5d365ba16 41 * increment between two consecutive coefficients. Here, we're specifying the inner stride as a compile-time
ykuroda 0:13a5d365ba16 42 * fixed value.
ykuroda 0:13a5d365ba16 43 * \include Map_inner_stride.cpp
ykuroda 0:13a5d365ba16 44 * Output: \verbinclude Map_inner_stride.out
ykuroda 0:13a5d365ba16 45 *
ykuroda 0:13a5d365ba16 46 * Here's an example of mapping an array while specifying an outer stride. Here, since we're mapping
ykuroda 0:13a5d365ba16 47 * as a column-major matrix, 'outer stride' means the pointer increment between two consecutive columns.
ykuroda 0:13a5d365ba16 48 * Here, we're specifying the outer stride as a runtime parameter. Note that here \c OuterStride<> is
ykuroda 0:13a5d365ba16 49 * a short version of \c OuterStride<Dynamic> because the default template parameter of OuterStride
ykuroda 0:13a5d365ba16 50 * is \c Dynamic
ykuroda 0:13a5d365ba16 51 * \include Map_outer_stride.cpp
ykuroda 0:13a5d365ba16 52 * Output: \verbinclude Map_outer_stride.out
ykuroda 0:13a5d365ba16 53 *
ykuroda 0:13a5d365ba16 54 * For more details and for an example of specifying both an inner and an outer stride, see class Stride.
ykuroda 0:13a5d365ba16 55 *
ykuroda 0:13a5d365ba16 56 * \b Tip: to change the array of data mapped by a Map object, you can use the C++
ykuroda 0:13a5d365ba16 57 * placement new syntax:
ykuroda 0:13a5d365ba16 58 *
ykuroda 0:13a5d365ba16 59 * Example: \include Map_placement_new.cpp
ykuroda 0:13a5d365ba16 60 * Output: \verbinclude Map_placement_new.out
ykuroda 0:13a5d365ba16 61 *
ykuroda 0:13a5d365ba16 62 * This class is the return type of PlainObjectBase::Map() but can also be used directly.
ykuroda 0:13a5d365ba16 63 *
ykuroda 0:13a5d365ba16 64 * \sa PlainObjectBase::Map(), \ref TopicStorageOrders
ykuroda 0:13a5d365ba16 65 */
ykuroda 0:13a5d365ba16 66
ykuroda 0:13a5d365ba16 67 namespace internal {
ykuroda 0:13a5d365ba16 68 template<typename PlainObjectType, int MapOptions, typename StrideType>
ykuroda 0:13a5d365ba16 69 struct traits<Map<PlainObjectType, MapOptions, StrideType> >
ykuroda 0:13a5d365ba16 70 : public traits<PlainObjectType>
ykuroda 0:13a5d365ba16 71 {
ykuroda 0:13a5d365ba16 72 typedef traits<PlainObjectType> TraitsBase;
ykuroda 0:13a5d365ba16 73 typedef typename PlainObjectType::Index Index;
ykuroda 0:13a5d365ba16 74 typedef typename PlainObjectType::Scalar Scalar;
ykuroda 0:13a5d365ba16 75 enum {
ykuroda 0:13a5d365ba16 76 InnerStrideAtCompileTime = StrideType::InnerStrideAtCompileTime == 0
ykuroda 0:13a5d365ba16 77 ? int(PlainObjectType::InnerStrideAtCompileTime)
ykuroda 0:13a5d365ba16 78 : int(StrideType::InnerStrideAtCompileTime),
ykuroda 0:13a5d365ba16 79 OuterStrideAtCompileTime = StrideType::OuterStrideAtCompileTime == 0
ykuroda 0:13a5d365ba16 80 ? int(PlainObjectType::OuterStrideAtCompileTime)
ykuroda 0:13a5d365ba16 81 : int(StrideType::OuterStrideAtCompileTime),
ykuroda 0:13a5d365ba16 82 HasNoInnerStride = InnerStrideAtCompileTime == 1,
ykuroda 0:13a5d365ba16 83 HasNoOuterStride = StrideType::OuterStrideAtCompileTime == 0,
ykuroda 0:13a5d365ba16 84 HasNoStride = HasNoInnerStride && HasNoOuterStride,
ykuroda 0:13a5d365ba16 85 IsAligned = bool(EIGEN_ALIGN) && ((int(MapOptions)&Aligned)==Aligned),
ykuroda 0:13a5d365ba16 86 IsDynamicSize = PlainObjectType::SizeAtCompileTime==Dynamic,
ykuroda 0:13a5d365ba16 87 KeepsPacketAccess = bool(HasNoInnerStride)
ykuroda 0:13a5d365ba16 88 && ( bool(IsDynamicSize)
ykuroda 0:13a5d365ba16 89 || HasNoOuterStride
ykuroda 0:13a5d365ba16 90 || ( OuterStrideAtCompileTime!=Dynamic
ykuroda 0:13a5d365ba16 91 && ((static_cast<int>(sizeof(Scalar))*OuterStrideAtCompileTime)%16)==0 ) ),
ykuroda 0:13a5d365ba16 92 Flags0 = TraitsBase::Flags & (~NestByRefBit),
ykuroda 0:13a5d365ba16 93 Flags1 = IsAligned ? (int(Flags0) | AlignedBit) : (int(Flags0) & ~AlignedBit),
ykuroda 0:13a5d365ba16 94 Flags2 = (bool(HasNoStride) || bool(PlainObjectType::IsVectorAtCompileTime))
ykuroda 0:13a5d365ba16 95 ? int(Flags1) : int(Flags1 & ~LinearAccessBit),
ykuroda 0:13a5d365ba16 96 Flags3 = is_lvalue<PlainObjectType>::value ? int(Flags2) : (int(Flags2) & ~LvalueBit),
ykuroda 0:13a5d365ba16 97 Flags = KeepsPacketAccess ? int(Flags3) : (int(Flags3) & ~PacketAccessBit)
ykuroda 0:13a5d365ba16 98 };
ykuroda 0:13a5d365ba16 99 private:
ykuroda 0:13a5d365ba16 100 enum { Options }; // Expressions don't have Options
ykuroda 0:13a5d365ba16 101 };
ykuroda 0:13a5d365ba16 102 }
ykuroda 0:13a5d365ba16 103
ykuroda 0:13a5d365ba16 104 template<typename PlainObjectType, int MapOptions, typename StrideType> class Map
ykuroda 0:13a5d365ba16 105 : public MapBase<Map<PlainObjectType, MapOptions, StrideType> >
ykuroda 0:13a5d365ba16 106 {
ykuroda 0:13a5d365ba16 107 public:
ykuroda 0:13a5d365ba16 108
ykuroda 0:13a5d365ba16 109 typedef MapBase<Map> Base;
ykuroda 0:13a5d365ba16 110 EIGEN_DENSE_PUBLIC_INTERFACE(Map)
ykuroda 0:13a5d365ba16 111
ykuroda 0:13a5d365ba16 112 typedef typename Base::PointerType PointerType;
ykuroda 0:13a5d365ba16 113 #if EIGEN2_SUPPORT_STAGE <= STAGE30_FULL_EIGEN3_API
ykuroda 0:13a5d365ba16 114 typedef const Scalar* PointerArgType;
ykuroda 0:13a5d365ba16 115 inline PointerType cast_to_pointer_type(PointerArgType ptr) { return const_cast<PointerType>(ptr); }
ykuroda 0:13a5d365ba16 116 #else
ykuroda 0:13a5d365ba16 117 typedef PointerType PointerArgType;
ykuroda 0:13a5d365ba16 118 inline PointerType cast_to_pointer_type(PointerArgType ptr) { return ptr; }
ykuroda 0:13a5d365ba16 119 #endif
ykuroda 0:13a5d365ba16 120
ykuroda 0:13a5d365ba16 121 inline Index innerStride() const
ykuroda 0:13a5d365ba16 122 {
ykuroda 0:13a5d365ba16 123 return StrideType::InnerStrideAtCompileTime != 0 ? m_stride.inner() : 1;
ykuroda 0:13a5d365ba16 124 }
ykuroda 0:13a5d365ba16 125
ykuroda 0:13a5d365ba16 126 inline Index outerStride() const
ykuroda 0:13a5d365ba16 127 {
ykuroda 0:13a5d365ba16 128 return StrideType::OuterStrideAtCompileTime != 0 ? m_stride.outer()
ykuroda 0:13a5d365ba16 129 : IsVectorAtCompileTime ? this->size()
ykuroda 0:13a5d365ba16 130 : int(Flags)&RowMajorBit ? this->cols()
ykuroda 0:13a5d365ba16 131 : this->rows();
ykuroda 0:13a5d365ba16 132 }
ykuroda 0:13a5d365ba16 133
ykuroda 0:13a5d365ba16 134 /** Constructor in the fixed-size case.
ykuroda 0:13a5d365ba16 135 *
ykuroda 0:13a5d365ba16 136 * \param dataPtr pointer to the array to map
ykuroda 0:13a5d365ba16 137 * \param a_stride optional Stride object, passing the strides.
ykuroda 0:13a5d365ba16 138 */
ykuroda 0:13a5d365ba16 139 inline Map(PointerArgType dataPtr, const StrideType& a_stride = StrideType())
ykuroda 0:13a5d365ba16 140 : Base(cast_to_pointer_type(dataPtr)), m_stride(a_stride)
ykuroda 0:13a5d365ba16 141 {
ykuroda 0:13a5d365ba16 142 PlainObjectType::Base::_check_template_params();
ykuroda 0:13a5d365ba16 143 }
ykuroda 0:13a5d365ba16 144
ykuroda 0:13a5d365ba16 145 /** Constructor in the dynamic-size vector case.
ykuroda 0:13a5d365ba16 146 *
ykuroda 0:13a5d365ba16 147 * \param dataPtr pointer to the array to map
ykuroda 0:13a5d365ba16 148 * \param a_size the size of the vector expression
ykuroda 0:13a5d365ba16 149 * \param a_stride optional Stride object, passing the strides.
ykuroda 0:13a5d365ba16 150 */
ykuroda 0:13a5d365ba16 151 inline Map(PointerArgType dataPtr, Index a_size, const StrideType& a_stride = StrideType())
ykuroda 0:13a5d365ba16 152 : Base(cast_to_pointer_type(dataPtr), a_size), m_stride(a_stride)
ykuroda 0:13a5d365ba16 153 {
ykuroda 0:13a5d365ba16 154 PlainObjectType::Base::_check_template_params();
ykuroda 0:13a5d365ba16 155 }
ykuroda 0:13a5d365ba16 156
ykuroda 0:13a5d365ba16 157 /** Constructor in the dynamic-size matrix case.
ykuroda 0:13a5d365ba16 158 *
ykuroda 0:13a5d365ba16 159 * \param dataPtr pointer to the array to map
ykuroda 0:13a5d365ba16 160 * \param nbRows the number of rows of the matrix expression
ykuroda 0:13a5d365ba16 161 * \param nbCols the number of columns of the matrix expression
ykuroda 0:13a5d365ba16 162 * \param a_stride optional Stride object, passing the strides.
ykuroda 0:13a5d365ba16 163 */
ykuroda 0:13a5d365ba16 164 inline Map(PointerArgType dataPtr, Index nbRows, Index nbCols, const StrideType& a_stride = StrideType())
ykuroda 0:13a5d365ba16 165 : Base(cast_to_pointer_type(dataPtr), nbRows, nbCols), m_stride(a_stride)
ykuroda 0:13a5d365ba16 166 {
ykuroda 0:13a5d365ba16 167 PlainObjectType::Base::_check_template_params();
ykuroda 0:13a5d365ba16 168 }
ykuroda 0:13a5d365ba16 169
ykuroda 0:13a5d365ba16 170 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Map)
ykuroda 0:13a5d365ba16 171
ykuroda 0:13a5d365ba16 172 protected:
ykuroda 0:13a5d365ba16 173 StrideType m_stride;
ykuroda 0:13a5d365ba16 174 };
ykuroda 0:13a5d365ba16 175
ykuroda 0:13a5d365ba16 176 template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
ykuroda 0:13a5d365ba16 177 inline Array<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>
ykuroda 0:13a5d365ba16 178 ::Array(const Scalar *data)
ykuroda 0:13a5d365ba16 179 {
ykuroda 0:13a5d365ba16 180 this->_set_noalias(Eigen::Map<const Array>(data));
ykuroda 0:13a5d365ba16 181 }
ykuroda 0:13a5d365ba16 182
ykuroda 0:13a5d365ba16 183 template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
ykuroda 0:13a5d365ba16 184 inline Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>
ykuroda 0:13a5d365ba16 185 ::Matrix(const Scalar *data)
ykuroda 0:13a5d365ba16 186 {
ykuroda 0:13a5d365ba16 187 this->_set_noalias(Eigen::Map<const Matrix>(data));
ykuroda 0:13a5d365ba16 188 }
ykuroda 0:13a5d365ba16 189
ykuroda 0:13a5d365ba16 190 } // end namespace Eigen
ykuroda 0:13a5d365ba16 191
ykuroda 0:13a5d365ba16 192 #endif // EIGEN_MAP_H