OpenGM  2.3.x
Discrete Graphical Model Library
dddualvariableblock.hxx
Go to the documentation of this file.
1 #pragma once
2 #ifndef OPENGM_DD_DUALVARIABLEBLOCK_HXX
3 #define OPENGM_DD_DUALVARIABLEBLOCK_HXX
4 
7 
8 namespace opengm {
10 
11  template<class DUALVAR = marray::Marray<double> >
12  class DDDualVariableBlock{
13  public:
14  typedef DUALVAR DualVariableType;
15  typedef typename DUALVAR::ValueType ValueType;
16  typedef typename GraphicalModelDecomposition::SubFactor SubFactorType;
17  typedef typename GraphicalModelDecomposition::SubFactorListType SubFactorListType;
18 
19  // Methods
20  DDDualVariableBlock(){};
21  template<class ITERATOR> DDDualVariableBlock(const SubFactorListType& subFactorList, ITERATOR shapeBegin, ITERATOR shapeEnd);
22  std::vector<DUALVAR*> getPointers();
23  void test() const;
24 
25  // Members
26  std::vector<DualVariableType> duals_;
27  const SubFactorListType* subFactorList_;
28  };
29 
30 
31  template<class DUALVAR = marray::Marray<double> >
32  class DDDualVariableBlock2{
33  public:
34  typedef DUALVAR DualVariableType;
35  typedef typename DUALVAR::ValueType ValueType;
36  typedef typename GraphicalModelDecomposition::SubFactor SubFactorType;
37  typedef typename GraphicalModelDecomposition::SubFactorListType SubFactorListType;
38 
39  // Methods
40  DDDualVariableBlock2(){};
41  template<class ITERATOR> DDDualVariableBlock2(const SubFactorListType& subFactorList, ITERATOR shapeBegin, ITERATOR shapeEnd);
42  std::vector<DUALVAR*> getPointers();
43  void test() const;
44 
45  // Members
46  std::vector<DualVariableType> duals_;
47  std::vector<DualVariableType> duals2_;
48  const SubFactorListType* subFactorList_;
49  };
50 
51 
54 
55  template<class DUALVAR>
56  template<class ITERATOR>
57  DDDualVariableBlock<DUALVAR>::DDDualVariableBlock
58  (
59  const typename GraphicalModelDecomposition::SubFactorListType& subFactorList,
60  ITERATOR shapeBegin,
61  ITERATOR shapeEnd
62  )
63  {
64  const size_t numDuals = subFactorList.size();
65  duals_.resize(numDuals, DUALVAR(shapeBegin,shapeEnd,0));
66  subFactorList_ = &subFactorList;
67  }
68  template<>
69  template<class ITERATOR>
70  DDDualVariableBlock<marray::View<double,false> >::DDDualVariableBlock
71  (
72  const SubFactorListType& subFactorList,
73  ITERATOR shapeBegin,
74  ITERATOR shapeEnd
75  )
76  {
77  const size_t numDuals = subFactorList.size();
78  double tmp;
79  duals_.resize(numDuals, marray::View<double,false>(shapeBegin,shapeEnd,&tmp));
80  subFactorList_ = &subFactorList;
81  }
82 
83  template<>
84  template<class ITERATOR>
85  DDDualVariableBlock<marray::View<float,false> >::DDDualVariableBlock
86  (
87  const SubFactorListType& subFactorList,
88  ITERATOR shapeBegin,
89  ITERATOR shapeEnd
90  )
91  {
92  const size_t numDuals = subFactorList.size();
93  double tmp;
94  duals_.resize(numDuals, marray::View<float,false>(shapeBegin,shapeEnd,&tmp));
95  subFactorList_ = &subFactorList;
96  }
97 
98  template<class DUALVAR>
99  std::vector<DUALVAR*> DDDualVariableBlock<DUALVAR>::getPointers()
100  {
101  std::vector<DualVariableType*> ret(duals_.size());
102  for(size_t i=0; i<duals_.size(); ++i) ret[i] = &(duals_[i]);
103  return ret;
104  }
105 
106  template<class DUALVAR>
107  void DDDualVariableBlock<DUALVAR>::test() const
108  {
109  marray::Marray<double> temp(duals_[0].shapeBegin(), duals_[0].shapeEnd() ,0);
110  for(size_t i=0; i<duals_.size(); ++i) {
111  temp += duals_[i];
112  }
113  //std::cout<<" temp size "<<temp.size()<<"\n";
114  for(size_t j=0; j<temp.size(); ++j) {
115  if( (temp(j)<0.001 && temp(j)>-0.001)==false ){
116  std::cout<<"temp("<<j<<") = "<<temp(j)<<"\n";
117  }
118  //OPENGM_ASSERT(temp(i)<0.00001 && temp(i)>-0.00001);
119  }
120  }
121 
123 
124  template<class DUALVAR>
125  template<class ITERATOR>
126  DDDualVariableBlock2<DUALVAR>::DDDualVariableBlock2
127  (
128  const typename GraphicalModelDecomposition::SubFactorListType& subFactorList,
129  ITERATOR shapeBegin,
130  ITERATOR shapeEnd
131  )
132  {
133  const size_t numDuals = subFactorList.size();
134  duals_.resize(numDuals, DUALVAR(shapeBegin,shapeEnd,0));
135  duals2_.resize(numDuals, DUALVAR(shapeBegin,shapeEnd,0));
136  subFactorList_ = &subFactorList;
137  }
138  template<>
139  template<class ITERATOR>
140  DDDualVariableBlock2<marray::View<double,false> >::DDDualVariableBlock2
141  (
142  const SubFactorListType& subFactorList,
143  ITERATOR shapeBegin,
144  ITERATOR shapeEnd
145  )
146  {
147  const size_t numDuals = subFactorList.size();
148  double tmp;
149  duals_.resize(numDuals, marray::View<double,false>(shapeBegin,shapeEnd,&tmp));
150  duals2_.resize(numDuals, marray::View<double,false>(shapeBegin,shapeEnd,&tmp));
151  subFactorList_ = &subFactorList;
152  }
153  template<>
154  template<class ITERATOR>
155  DDDualVariableBlock2<marray::View<float,false> >::DDDualVariableBlock2
156  (
157  const SubFactorListType& subFactorList,
158  ITERATOR shapeBegin,
159  ITERATOR shapeEnd
160  )
161  {
162  const size_t numDuals = subFactorList.size();
163  float tmp;
164  duals_.resize(numDuals, marray::View<float,false>(shapeBegin,shapeEnd,&tmp));
165  duals2_.resize(numDuals, marray::View<float,false>(shapeBegin,shapeEnd,&tmp));
166  subFactorList_ = &subFactorList;
167  }
168 
169  template<class DUALVAR>
170  std::vector<DUALVAR*> DDDualVariableBlock2<DUALVAR>::getPointers()
171  {
172  std::vector<DualVariableType*> ret(duals_.size());
173  for(size_t i=0; i<duals_.size(); ++i) ret[i] = &(duals_[i]);
174  return ret;
175  }
176 
177  template<class DUALVAR>
178  void DDDualVariableBlock2<DUALVAR>::test() const
179  {
180  marray::Marray<double> temp(duals_[0].shapeBegin(),duals_[0].shapeEnd(),0);
181  for(size_t i=0; i<duals_.size(); ++i) temp += duals_[i];
182  for(size_t i=0; i<temp.size(); ++i) OPENGM_ASSERT(temp(i)<0.00001 && temp(i)>-0.00001);
183  }
184 
186 } // namespace opengm
187 
188 #endif
The OpenGM namespace.
Definition: config.hxx:43
Array-Interface to an interval of memory.
Definition: marray.hxx:44
#define OPENGM_ASSERT(expression)
Definition: opengm.hxx:77
const size_t size() const
Runtime-flexible multi-dimensional array.
Definition: marray.hxx:52