BayesOpt
ublas_extra.hpp
Go to the documentation of this file.
1 
2 /*
3 -----------------------------------------------------------------------------
4  Copyright (C) 2011 Ruben Martinez-Cantin <rmcantin@unizar.es>
5 
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero 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  This program 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 Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 -----------------------------------------------------------------------------
19 */
20 
21 #ifndef __UBLAS_EXTRA_HPP__
22 #define __UBLAS_EXTRA_HPP__
23 
24 #include <typeinfo>
25 #include <boost/numeric/ublas/vector.hpp>
26 
27 namespace bayesopt
28 {
30  namespace utils
31  {
32  template<class V, class D>
33  void append(V& vect, D element)
34  {
35  typedef typename V::value_type VD;
36  assert(typeid(VD) == typeid(D));
37 
38  // This method is super inefficient but there seems to be the uBlas style.
39  const size_t size = vect.size();
40  vect.resize(size+1,true);
41  vect(size) = element;
42  };
43 
44  template<class V, class I>
45  void erase(V& vect, I begin)
46  {
47  typedef typename V::iterator VI;
48  assert(typeid(VI) == typeid(I));
49 
50  for(VI it = begin; it != vect.end()-1; ++it)
51  {
52  *it = *(it+1);
53  }
54  vect.resize(vect.size()-1);
55  };
56 
57  template<class M>
58  void erase_column(M& mat, size_t pos)
59  {
60  for(size_t i = pos; i < mat.size2()-1; ++i)
61  {
62  column(mat,i) = column(mat,i+1);
63  }
64  mat.resize(mat.size1(),mat.size2()-1);
65  };
66 
67  template<class M, class V>
68  void add_to_diagonal(M& mat, const V& vec)
69  {
70  assert(mat.size1()==mat.size2());
71  assert(mat.size1()==vec.size());
72  const size_t ll = vec.size();
73  for(size_t ii = 0; ii < ll; ++ii)
74  {
75  mat(ii,ii) += vec(ii);
76  }
77  };
78 
79  boost::numeric::ublas::vector<double> array2vector(const double array[],
80  const size_t n);
81 
82  } // namespace utils
83 } //namespace bayesopt
84 
85 #endif
Namespace of the library interface.
Definition: using.dox:1