cheshirekow  v0.1.0
sum.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 Josh Bialkowski (jbialk@mit.edu)
3  *
4  * This file is part of fiber.
5  *
6  * fiber is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * fiber is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with fiber. If not, see <http://www.gnu.org/licenses/>.
18  */
25 #ifndef FIBER_SUM_H_
26 #define FIBER_SUM_H_
27 
28 
29 namespace fiber {
30 
32 template <typename Scalar, class Exp1, class Exp2>
33 class _Sum : public _RValue<Scalar, _Sum<Scalar, Exp1, Exp2> > {
34  Exp1 const& A_;
35  Exp2 const& B_;
36 
37  public:
38  enum {
39  ROWS_ = Exp1::ROWS_,
40  COLS_ = Exp1::COLS_,
41  SIZE_ = Exp1::SIZE_
42  };
43 
44  _Sum(Exp1 const& A, Exp2 const& B) : A_(A), B_(B) {
45  static_assert(Exp1::ROWS_ == Exp2::ROWS_
46  && Exp1::COLS_ == Exp2::COLS_
47  && Exp1::SIZE_ == Exp2::SIZE_,
48  "Cannot sum matrices of different sizes");
49  }
50 
51  Size size() const { return A_.size(); }
52  Size rows() const { return A_.rows(); }
53  Size cols() const { return A_.cols(); }
54 
55  Scalar operator[](Index i) const {
56  return (A_[i] + B_[i]);
57  }
58 
59  Scalar operator()(Index i, Index j) const {
60  return (A_(i, j) + B_(i, j));
61  }
62 };
63 
64 template <typename Scalar, class Exp1, class Exp2>
66  _RValue<Scalar, Exp2> const& B) {
67  return _Sum<Scalar, Exp1, Exp2>(static_cast<Exp1 const&>(A),
68  static_cast<Exp2 const&>(B));
69 }
70 
71 } // namespace fiber
72 
73 
74 #endif // FIBER_SUM_H_SUM_H_
Size rows() const
Definition: sum.h:52
expression template for rvalues
Definition: rvalue.h:33
Exp1 const & A_
Definition: sum.h:34
unsigned int Size
Definition: fiber.h:32
_Sum(Exp1 const &A, Exp2 const &B)
Definition: sum.h:44
Scalar operator[](Index i) const
Definition: sum.h:55
Size cols() const
Definition: sum.h:53
Size size() const
Definition: sum.h:51
Scalar operator()(Index i, Index j) const
Definition: sum.h:59
int Index
Definition: fiber.h:31
_Sum< Scalar, Exp1, Exp2 > operator+(_RValue< Scalar, Exp1 > const &A, _RValue< Scalar, Exp2 > const &B)
Definition: sum.h:65
expression template for sum of two matrix expressions
Definition: sum.h:33
Exp2 const & B_
Definition: sum.h:35