Stan Math Library  2.9.0
reverse mode automatic differentiation
tcrossprod.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_REV_MAT_FUN_TCROSSPROD_HPP
2 #define STAN_MATH_REV_MAT_FUN_TCROSSPROD_HPP
3 
6 #include <stan/math/rev/core.hpp>
12 #include <boost/math/tools/promotion.hpp>
13 #include <vector>
14 
15 namespace stan {
16  namespace math {
17 
24  inline matrix_v
25  tcrossprod(const matrix_v& M) {
26  if (M.rows() == 0)
27  return matrix_v(0, 0);
28  // if (M.rows() == 1)
29  // return M * M.transpose();
30 
31  // WAS JUST THIS
32  // matrix_v result(M.rows(), M.rows());
33  // return result.setZero().selfadjointView<Eigen::Upper>().rankUpdate(M);
34 
35  matrix_v MMt(M.rows(), M.rows());
36 
37  vari** vs
38  = reinterpret_cast<vari**>(ChainableStack::memalloc_
39  .alloc((M.rows() * M.cols())
40  * sizeof(vari*)));
41  int pos = 0;
42  for (int m = 0; m < M.rows(); ++m)
43  for (int n = 0; n < M.cols(); ++n)
44  vs[pos++] = M(m, n).vi_;
45  for (int m = 0; m < M.rows(); ++m)
46  MMt(m, m) = var(new dot_self_vari(vs + m * M.cols(), M.cols()));
47  for (int m = 0; m < M.rows(); ++m) {
48  for (int n = 0; n < m; ++n) {
49  MMt(m, n) = var(new dot_product_vari<var, var>(vs + m * M.cols(),
50  vs + n * M.cols(),
51  M.cols()));
52  MMt(n, m) = MMt(m, n);
53  }
54  }
55  return MMt;
56  }
57 
58  }
59 }
60 #endif
The variable implementation base class.
Definition: vari.hpp:30
Eigen::Matrix< fvar< T >, R, R > tcrossprod(const Eigen::Matrix< fvar< T >, R, C > &m)
Definition: tcrossprod.hpp:17
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:31
Eigen::Matrix< var, Eigen::Dynamic, Eigen::Dynamic > matrix_v
The type of a matrix holding stan::math::var values.
Definition: typedefs.hpp:21
void * alloc(size_t len)
Return a newly allocated block of memory of the appropriate size managed by the stack allocator...

     [ Stan Home Page ] © 2011–2015, Stan Development Team.