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-2017 Shark Development Team
11  *
12  * <BR><HR>
13  * This file is part of Shark.
14  * <http://shark-ml.org/>
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 ) );
53  SHARK_RUNTIME_CHECK(result < std::numeric_limits<std::size_t>::max(), "number of neurons is too big for calculation");
54  return static_cast<std::size_t>(result);
55 
56  }
57 
58  ///\brief Returns the i-th state vector.
59  ///
60  /// @param vec the vector the i-th state vector is stored in
61  /// @param stateNumber the number of the state
62  template<class Vector>
63  static void state(Vector& vec,std::size_t stateNumber){
64  for (std::size_t i = 0; i != vec.size(); i++) {
65  bool secondState = stateNumber & (std::size_t(1)<<i);
66  vec(i) = secondState? State2 : State1;
67  }
68  }
69  ///\brief Returns the i-th state vector for a matrix row
70  ///
71  /// @param vec the vector the i-th state vector is stored in
72  /// @param stateNumber the number of the state
73  template<class Matrix>
74  static void state(blas::matrix_row<Matrix> vec,std::size_t stateNumber){
75  for (std::size_t i = 0; i != vec.size(); i++) {
76  bool secondState = stateNumber & (std::size_t(1)<<i);
77  vec(i) = secondState? State2 : State1;
78  }
79  }
80 };
81 
84 
85 }
86 #endif