EigenBase.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2009 Benoit Jacob <jacob.benoit.1@gmail.com>
5 // Copyright (C) 2009 Gael Guennebaud <gael.guennebaud@inria.fr>
6 //
7 // Eigen is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Lesser General Public
9 // License as published by the Free Software Foundation; either
10 // version 3 of the License, or (at your option) any later version.
11 //
12 // Alternatively, you can redistribute it and/or
13 // modify it under the terms of the GNU General Public License as
14 // published by the Free Software Foundation; either version 2 of
15 // the License, or (at your option) any later version.
16 //
17 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
20 // GNU General Public License for more details.
21 //
22 // You should have received a copy of the GNU Lesser General Public
23 // License and a copy of the GNU General Public License along with
24 // Eigen. If not, see <http://www.gnu.org/licenses/>.
25 
26 #ifndef EIGEN_EIGENBASE_H
27 #define EIGEN_EIGENBASE_H
28 
29 namespace Eigen {
30 
41 template<typename Derived> struct EigenBase
42 {
43 // typedef typename internal::plain_matrix_type<Derived>::type PlainObject;
44 
45  typedef typename internal::traits<Derived>::StorageKind StorageKind;
46  typedef typename internal::traits<Derived>::Index Index;
47 
49  Derived& derived() { return *static_cast<Derived*>(this); }
51  const Derived& derived() const { return *static_cast<const Derived*>(this); }
52 
53  inline Derived& const_cast_derived() const
54  { return *static_cast<Derived*>(const_cast<EigenBase*>(this)); }
55  inline const Derived& const_derived() const
56  { return *static_cast<const Derived*>(this); }
57 
59  inline Index rows() const { return derived().rows(); }
61  inline Index cols() const { return derived().cols(); }
64  inline Index size() const { return rows() * cols(); }
65 
67  template<typename Dest> inline void evalTo(Dest& dst) const
68  { derived().evalTo(dst); }
69 
71  template<typename Dest> inline void addTo(Dest& dst) const
72  {
73  // This is the default implementation,
74  // derived class can reimplement it in a more optimized way.
75  typename Dest::PlainObject res(rows(),cols());
76  evalTo(res);
77  dst += res;
78  }
79 
81  template<typename Dest> inline void subTo(Dest& dst) const
82  {
83  // This is the default implementation,
84  // derived class can reimplement it in a more optimized way.
85  typename Dest::PlainObject res(rows(),cols());
86  evalTo(res);
87  dst -= res;
88  }
89 
91  template<typename Dest> inline void applyThisOnTheRight(Dest& dst) const
92  {
93  // This is the default implementation,
94  // derived class can reimplement it in a more optimized way.
95  dst = dst * this->derived();
96  }
97 
99  template<typename Dest> inline void applyThisOnTheLeft(Dest& dst) const
100  {
101  // This is the default implementation,
102  // derived class can reimplement it in a more optimized way.
103  dst = this->derived() * dst;
104  }
105 
106 };
107 
108 /***************************************************************************
109 * Implementation of matrix base methods
110 ***************************************************************************/
111 
120 template<typename Derived>
121 template<typename OtherDerived>
123 {
124  other.derived().evalTo(derived());
125  return derived();
126 }
127 
128 template<typename Derived>
129 template<typename OtherDerived>
131 {
132  other.derived().addTo(derived());
133  return derived();
134 }
135 
136 template<typename Derived>
137 template<typename OtherDerived>
139 {
140  other.derived().subTo(derived());
141  return derived();
142 }
143 
148 template<typename Derived>
149 template<typename OtherDerived>
150 inline Derived&
152 {
153  other.derived().applyThisOnTheRight(derived());
154  return derived();
155 }
156 
158 template<typename Derived>
159 template<typename OtherDerived>
161 {
162  other.derived().applyThisOnTheRight(derived());
163 }
164 
166 template<typename Derived>
167 template<typename OtherDerived>
169 {
170  other.derived().applyThisOnTheLeft(derived());
171 }
172 
173 } // end namespace Eigen
174 
175 #endif // EIGEN_EIGENBASE_H