BayesOpt
ublas_trace.hpp
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 #ifndef __TRACE_UBLAS_HPP__
21 #define __TRACE_UBLAS_HPP__
22 
23 #include <boost/numeric/ublas/matrix.hpp>
24 #include <boost/numeric/ublas/matrix_proxy.hpp>
25 
26 namespace bayesopt
27 {
28  namespace utils
29  {
30 
31  template<class E>
32  typename E::value_type trace(const E &A)
33  {
34  const size_t n = (std::min)(A.size1(),A.size2());
35  typename E::value_type sum = 0;
36  for (size_t i=0; i<n; ++i)
37  sum += A(i,i);
38 
39  return sum;
40  }
41 
42  template<class E>
43  typename E::value_type log_trace(const E &A)
44  {
45  const size_t n = (std::min)(A.size1(),A.size2());
46  typename E::value_type sum = 0;
47  for (size_t i=0; i<n; ++i)
48  sum += std::log(A(i,i));
49 
50  return sum;
51  }
52 
53 
54  template<class E1, class E2>
55  typename E1::value_type trace_prod(const E1 & A, const E2 & B )
56  {
57  namespace ublas = boost::numeric::ublas;
58 
59  const size_t n = (std::min)(A.size1(),B.size2());
60  typename E1::value_type sum = 0;
61  for (size_t i=0; i<n; ++i)
62  sum += ublas::inner_prod(ublas::row(A,i),ublas::column(B,i));
63 
64  return sum;
65  }
66 
67  } // namespace utils
68 } //namespace bayesopt
69 
70 #endif
Namespace of the library interface.
Definition: using.dox:1