TwoStateSpace.h
Go to the documentation of this file.
1 /*!
2  *
3  *
4  * \brief -
5  *
6  * \author -
7  * \date -
8  *
9  *
10  * \par Copyright 1995-2015 Shark Development Team
11  *
12  * <BR><HR>
13  * This file is part of Shark.
14  * <http://image.diku.dk/shark/>
15  *
16  * Shark is free software: you can redistribute it and/or modify
17  * it under the terms of the GNU Lesser General Public License as published
18  * by the Free Software Foundation, either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * Shark is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24  * GNU Lesser General Public License for more details.
25  *
26  * You should have received a copy of the GNU Lesser General Public License
27  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
28  *
29  */
30 #ifndef SHARK_UNSUPERVISED_RBM_STATESPACES_TWOSTATESPACE_H
31 #define SHARK_UNSUPERVISED_RBM_STATESPACES_TWOSTATESPACE_H
32 
33 #include <limits>
34 #include <cmath>
36 #include <shark/Core/Exception.h>
37 
38 namespace shark{
39 ///\brief The TwoStateSpace is a discrete Space with only two values, for example {0,1} or {-1,1}.
40 template<int State1,int State2>
42 
43  ///\brief Tag which tells an approximating function of the partition function, that this space can be enumerated.
45 
46  ///\brief Returns the number of states a vector of n random variables (neurons) with values in this state space may have.
47  ///
48  ///For example if {a,b} is the state space it returns the cardinality of the set \f$ {a,b}^n = 2^n \f$
49  /// @param numberOfNeurons the size of the vector
50  /// @return the number of States.
51  static std::size_t numberOfStates(std::size_t numberOfNeurons){
52  long double result = std::pow( 2., static_cast< int >( numberOfNeurons ) );
54  SHARKEXCEPTION("number of neurons is too big for calculation");
55  }
56  return static_cast<std::size_t>(result);
57 
58  }
59 
60  ///\brief Returns the i-th state vector.
61  ///
62  /// @param vec the vector the i-th state vector is stored in
63  /// @param stateNumber the number of the state
64  template<class Vector>
65  static void state(Vector& vec,std::size_t stateNumber){
66  for (std::size_t i = 0; i != vec.size(); i++) {
67  bool secondState = stateNumber & (std::size_t(1)<<i);
68  vec(i) = secondState? State2 : State1;
69  }
70  }
71  ///\brief Returns the i-th state vector for a matrix row
72  ///
73  /// @param vec the vector the i-th state vector is stored in
74  /// @param stateNumber the number of the state
75  template<class Matrix>
76  static void state(blas::matrix_row<Matrix> vec,std::size_t stateNumber){
77  for (std::size_t i = 0; i != vec.size(); i++) {
78  bool secondState = stateNumber & (std::size_t(1)<<i);
79  vec(i) = secondState? State2 : State1;
80  }
81  }
82 };
83 
86 
87 }
88 #endif