cheshirekow  v0.1.0
difference.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_DIFFERENCE_H_
26 #define FIBER_DIFFERENCE_H_
27 
28 
29 namespace fiber {
30 
32 template <typename Scalar, class Exp1, class Exp2>
33 class _Difference : public _RValue<Scalar, _Difference<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  _Difference(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  "Matrix sizes must agree in difference");
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[](Size i) const {
56  return (A_[i] - B_[i]);
57  }
58 
59  Scalar operator()(Size i, Size j) const {
60  return (A_(i, j) - B_(i, j));
61  }
62 };
63 
64 template <typename Scalar, class Exp1, class Exp2>
66  _RValue<Scalar, Exp1> const& A, _RValue<Scalar, Exp2> const& B) {
67  return _Difference<Scalar, Exp1, Exp2>(static_cast<Exp1 const&>(A),
68  static_cast<Exp2 const&>(B));
69 }
70 
71 } // namespace fiber
72 
73 
74 #endif // FIBER_DIFFERENCE_H_
Scalar operator[](Size i) const
Definition: difference.h:55
expression template for rvalues
Definition: rvalue.h:33
_Difference< Scalar, Exp1, Exp2 > operator-(_RValue< Scalar, Exp1 > const &A, _RValue< Scalar, Exp2 > const &B)
Definition: difference.h:65
unsigned int Size
Definition: fiber.h:32
_Difference(Exp1 const &A, Exp2 const &B)
Definition: difference.h:44
Size size() const
Definition: difference.h:51
Size cols() const
Definition: difference.h:53
Size rows() const
Definition: difference.h:52
Scalar operator()(Size i, Size j) const
Definition: difference.h:59
expression template for difference of two matrix expressions
Definition: difference.h:33
Exp2 const & B_
Definition: difference.h:35
Exp1 const & A_
Definition: difference.h:34