aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/MatrixBase.h
blob: ab86f328964b3c363efa8c435265ac086d9d0d18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// This file is part of Eigen, a lightweight C++ template library
// for linear algebra. Eigen itself is part of the KDE project.
//
// Copyright (C) 2006-2007 Benoit Jacob <jacob@math.jussieu.fr>
//
// Eigen is free software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the Free Software
// Foundation; either version 2 or (at your option) any later version.
//
// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public License along
// with Eigen; if not, write to the Free Software Foundation, Inc., 51
// Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
//
// As a special exception, if other files instantiate templates or use macros
// or functions from this file, or you compile this file and link it
// with other works to produce a work based on this file, this file does not
// by itself cause the resulting work to be covered by the GNU General Public
// License. This exception does not invalidate any other reasons why a work
// based on this file might be covered by the GNU General Public License.

#ifndef EIGEN_MATRIXBASE_H
#define EIGEN_MATRIXBASE_H

#include"Util.h"
#include"MatrixXpr.h"

namespace Eigen
{

template<typename MatrixType> class MatrixConstRef
{
  public:
    typedef typename ForwardDecl<MatrixType>::Scalar Scalar;
    
    MatrixConstRef(const MatrixType& matrix) : m_matrix(matrix) {}
    MatrixConstRef(const MatrixConstRef& other) : m_matrix(other.m_matrix) {}
    ~MatrixConstRef() {}

    static bool hasDynamicNumRows()
    {
      return MatrixType::hasDynamicNumRows();
    }

    static bool hasDynamicNumCols()
    {
      return MatrixType::hasDynamicNumCols();
    }
    
    int rows() const { return m_matrix.rows(); }
    int cols() const { return m_matrix.cols(); }

    const Scalar& operator()(int row, int col) const
    {
      return m_matrix(row, col);
    }

  protected:
    const MatrixType& m_matrix;
};

template<typename MatrixType> class MatrixRef
{
  public:
    typedef typename ForwardDecl<MatrixType>::Scalar Scalar;
    typedef MatrixXpr<MatrixRef<MatrixType> > Xpr;
    
    MatrixRef(MatrixType& matrix) : m_matrix(matrix) {}
    MatrixRef(const MatrixRef& other) : m_matrix(other.m_matrix) {}
    ~MatrixRef() {}

    static bool hasDynamicNumRows()
    {
      return MatrixType::hasDynamicNumRows();
    }

    static bool hasDynamicNumCols()
    {
      return MatrixType::hasDynamicNumCols();
    }
    
    int rows() const { return m_matrix.rows(); }
    int cols() const { return m_matrix.cols(); }

    Scalar& operator()(int row, int col)
    {
      return m_matrix(row, col);
    }
    
    MatrixType& matrix() { return m_matrix; }

    Xpr xpr()
    {
      return Xpr(*this);
    }

  protected:
    MatrixType& m_matrix;
};

template<typename Derived>
class MatrixBase
{
  public:

    typedef typename ForwardDecl<Derived>::Scalar Scalar;
    typedef MatrixConstRef<MatrixBase<Derived> > ConstRef;
    typedef MatrixRef<MatrixBase<Derived> > Ref;
    typedef MatrixConstXpr<ConstRef> ConstXpr;
    typedef MatrixXpr<Ref> Xpr;
    typedef MatrixAlias<Derived> Alias;

    Ref ref()
    {
      return Ref(*this);
    }
    
    ConstRef constRef() const
    {
      return ConstRef(*this);
    }
    
    Xpr xpr()
    {
      return Xpr(ref());
    }
    
    ConstXpr constXpr() const
    {
      return ConstXpr(constRef());
    }
    
    Alias alias();
    
    static bool hasDynamicNumRows()
    {
      return Derived::_hasDynamicNumRows();
    }

    static bool hasDynamicNumCols()
    {
      return Derived::_hasDynamicNumCols();
    }
    
    int rows() const
    {
      return static_cast<const Derived*>(this)->_rows();
    }
    
    int cols() const
    {
      return static_cast<const Derived*>(this)->_cols();
    }
    
    void resize(int rows, int cols)
    {
      static_cast<Derived*>(this)->_resize(rows, cols);
    }

    const Scalar* array() const
    {
      return static_cast<const Derived*>(this)->m_array;
    }

    Scalar* array()
    {
      return static_cast<Derived*>(this)->m_array;
    }

    const Scalar& operator()(int row, int col = 0) const
    {
      EIGEN_CHECK_RANGES(*this, row, col);
      return array()[row + col * rows()];
    }

    Scalar& operator()(int row, int col = 0)
    {
      EIGEN_CHECK_RANGES(*this, row, col);
      return array()[row + col * rows()];
    }
    
    template<typename XprContent> 
    MatrixBase& operator=(const MatrixConstXpr<XprContent> &otherXpr)
    {
      resize(otherXpr.rows(), otherXpr.cols());
      xpr() = otherXpr;
      return *this;
    }
    
    MatrixBase& operator=(const MatrixBase &other)
    {
      return *this = other.constXpr();
    }
    
    MatrixConstXpr<MatrixRow<const ConstRef> > row(int i) const;
    MatrixConstXpr<MatrixCol<const ConstRef> > col(int i) const;
    MatrixConstXpr<MatrixMinor<const ConstRef> > minor(int row, int col) const;
    MatrixConstXpr<MatrixBlock<const ConstRef> >
      block(int startRow, int endRow, int startCol = 0, int endCol = 0) const;
    
    template<typename Content>
    MatrixBase& operator+=(const MatrixConstXpr<Content> &xpr);
    template<typename Content>
    MatrixBase& operator-=(const MatrixConstXpr<Content> &xpr);
    template<typename Content>
    MatrixBase& operator*=(const MatrixConstXpr<Content> &xpr);
    template<typename Derived2>
    MatrixBase& operator+=(const MatrixBase<Derived2> &other);
    template<typename Derived2>
    MatrixBase& operator-=(const MatrixBase<Derived2> &other);
    template<typename Derived2>
    MatrixBase& operator*=(const MatrixBase<Derived2> &other);
    
  protected:
  
    MatrixBase() {};
};

template<typename Content>
template<typename Derived>
MatrixXpr<Content>& MatrixXpr<Content>::operator=(const MatrixBase<Derived>& matrix)
{
  assert(rows() == matrix.rows() && cols() == matrix.cols());
  for(int i = 0; i < rows(); i++)
    for(int j = 0; j < cols(); j++)
      this->operator()(i, j) = matrix(i, j);
  return *this;
}

template<typename Derived>
std::ostream & operator <<
( std::ostream & s,
  const MatrixBase<Derived> & m )
{
  for( int i = 0; i < m.rows(); i++ )
  {
    s << m( i, 0 );
    for (int j = 1; j < m.cols(); j++ )
      s << " " << m( i, j );
    if( i < m.rows() - 1)
      s << std::endl;
  }
  return s;
}

template<typename Content>
std::ostream & operator << (std::ostream & s,
                            const MatrixConstXpr<Content>& m)
{
  for( int i = 0; i < m.rows(); i++ )
  {
    s << m( i, 0 );
    for (int j = 1; j < m.cols(); j++ )
      s << " " << m( i, j );
    if( i < m.rows() - 1)
      s << std::endl;
  }
  return s;
}

template<typename Derived> class MatrixAlias
{
  public:
    typedef typename Derived::Scalar Scalar;
    typedef MatrixRef<MatrixAlias<Derived> > Ref;
    typedef MatrixXpr<Ref> Xpr;
    
    MatrixAlias(Derived& matrix) : m_ref(matrix), m_tmp(matrix) {}
    MatrixAlias(const MatrixAlias& other) : m_ref(other.m_ref), m_tmp(other.m_tmp) {}
    
    ~MatrixAlias()
    {
      m_ref.xpr() = m_tmp;
    }
    
    Xpr xpr()
    {
      return Xpr(ref());
    }
    
    static bool hasDynamicNumRows()
    {
      return MatrixBase<Derived>::hasDynamicNumRows();
    }

    static bool hasDynamicNumCols()
    {
      return MatrixBase<Derived>::hasDynamicNumCols();
    }
    
    int rows() const { return m_tmp.rows(); }
    int cols() const { return m_tmp.cols(); }
    
    Scalar& operator()(int row, int col)
    {
      return m_tmp(row, col);
    }
    
    Ref ref()
    {
      return Ref(*this);
    }
    
    template<typename XprContent> 
    void operator=(const MatrixConstXpr<XprContent> &xpr)
    {
      ref().xpr() = xpr;
    }
    
    template<typename XprContent> 
    void operator+=(const MatrixConstXpr<XprContent> &xpr)
    {
      ref().xpr() += xpr;
    }
    
    template<typename XprContent> 
    void operator-=(const MatrixConstXpr<XprContent> &xpr)
    {
      ref().xpr() -= xpr;
    }
    
  protected:
    MatrixRef<MatrixBase<Derived> > m_ref;
    Derived m_tmp;
};

template<typename Derived>
typename MatrixBase<Derived>::Alias
MatrixBase<Derived>::alias()
{
  return Alias(*static_cast<Derived*>(this));
}

} // namespace Eigen

#endif // EIGEN_MATRIXBASE_H