cheshirekow  v0.1.0
product.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_PRODUCT_H_
26 #define FIBER_PRODUCT_H_
27 
28 #include <fiber/matrix.h>
29 
30 namespace fiber {
31 
33 template<typename Scalar, class Exp1, class Exp2>
35  _RValue<Scalar, Exp1> const& A, _RValue<Scalar, Exp2> const& B) {
36  static_assert(Exp1::COLS_ == Exp2::ROWS_,
37  "Inner dimensions of matrix multiplication must agree");
38 
40  for(int i=0; i < Exp1::ROWS_; i++) {
41  for(int j=0; j < Exp2::COLS_; j++) {
42  M(i,j) = Dot(GetRow(A, i), GetColumn(B, j));
43  }
44  }
45  return M;
46 }
47 
49 template <typename Scalar, class Exp1, class Exp2>
50 inline Scalar Dot(_RValue<Scalar, Exp1> const& A,
51  _RValue<Scalar, Exp2> const& B) {
52  static_assert(Exp1::SIZE_ == Exp2::SIZE_,
53  "Cannot compute a dot product of vectors that are not the"
54  "same size");
55  Scalar r(0);
56  for (int i = 0; i < A.size(); i++) {
57  r += A[i] * B[i];
58  }
59  return r;
60 }
61 
62 } // namespace fiber
63 
64 #endif // FIBER_PRODUCT_H_
_RView< Scalar, Exp, Exp::ROWS_, 1 > GetColumn(_RValue< Scalar, Exp > const &A, int j)
Definition: view.h:89
expression template for rvalues
Definition: rvalue.h:33
_RView< Scalar, Exp, 1, Exp::COLS_ > GetRow(_RValue< Scalar, Exp > const &A, int i)
Definition: view.h:76
Scalar Dot(_RValue< Scalar, Exp1 > const &A, _RValue< Scalar, Exp2 > const &B)
Dot product of two vectors.
Definition: product.h:50
Matrix< Scalar, Exp1::ROWS_, Exp2::COLS_ > operator*(_RValue< Scalar, Exp1 > const &A, _RValue< Scalar, Exp2 > const &B)
Matrix multiplication.
Definition: product.h:34
Size size() const
Definition: rvalue.h:35