cheshirekow  v0.1.0
normalize.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_NORMALIZE_H_
26 #define FIBER_NORMALIZE_H_
27 
28 #include <cmath>
29 #include <fiber/product.h>
30 
31 namespace fiber {
32 
34 template <typename Scalar, class Exp>
35 inline Scalar SquaredNorm(_RValue<Scalar, Exp> const& A) {
36  return Dot(A,A);
37 }
38 
40 template <typename Scalar, class Exp>
41 inline Scalar Norm(_RValue<Scalar, Exp> const& A) {
42  return std::sqrt(SquaredNorm(A));
43 }
44 
46 template <typename Scalar, class Exp>
47 inline Scalar Norm(_RValue<Scalar, Exp> const& A, int n) {
48  Scalar r(0);
49  for (int i = 0; i < A.size(); i++) {
50  r += std::pow(A[i], n);
51  }
52  return std::pow(r, 1.0/n);
53 }
54 
57 template <typename Scalar, class Exp>
58 class _Normalize : public _RValue<Scalar, _Normalize<Scalar, Exp> > {
59  Scalar norm_;
60  Exp const& v_;
61 
62  public:
63  enum {
64  ROWS_ = Exp::ROWS_,
65  COLS_ = Exp::COLS_,
67  };
68 
69  _Normalize(Exp const& A) : norm_(Norm(A)), v_(A) {}
70 
71  Size size() const { return v_.size(); }
72  Size rows() const { return v_.rows(); }
73  Size cols() const { return v_.cols(); }
74 
75  Scalar operator[](Index i) const {
76  return v_[i] / norm_;
77  }
78 
79  Scalar operator()(Index i, Index j) const {
80  return v_(i, j) / norm_;
81  }
82 };
83 
86 template <typename Scalar, class Exp>
88  return _Normalize<Scalar, Exp>(static_cast<Exp const&>(A));
89 }
90 
91 } // namespace fiber
92 
93 
94 #endif // FIBER_NORMALIZE_H_
Size cols() const
Definition: normalize.h:73
expression template for rvalues
Definition: rvalue.h:33
Scalar SquaredNorm(_RValue< Scalar, Exp > const &A)
Return the squared 2-norm of a vector.
Definition: normalize.h:35
Size size() const
Definition: normalize.h:71
unsigned int Size
Definition: fiber.h:32
Scalar operator()(Index i, Index j) const
Definition: normalize.h:79
Exp const & v_
underlying expression
Definition: normalize.h:60
_Normalize< Scalar, Exp > Normalize(_RValue< Scalar, Exp > const &A)
Prresents a view of a matrix or vector where each element is normalized, such that the sum of the ele...
Definition: normalize.h:87
_Normalize(Exp const &A)
Definition: normalize.h:69
Expression template presents a view of a matrix where each element is normalized, such that the sum o...
Definition: normalize.h:58
Scalar Norm(_RValue< Scalar, Exp > const &A)
Return the 2-norm of a vector.
Definition: normalize.h:41
int Index
Definition: fiber.h:31
Scalar operator[](Index i) const
Definition: normalize.h:75
Scalar Dot(_RValue< Scalar, Exp1 > const &A, _RValue< Scalar, Exp2 > const &B)
Dot product of two vectors.
Definition: product.h:50
Size rows() const
Definition: normalize.h:72
Scalar norm_
2-norm of elements in the expression
Definition: normalize.h:59
Size size() const
Definition: rvalue.h:35