Stan  1.3
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
tcrossprod.hpp
Go to the documentation of this file.
1 #ifndef __STAN__AGRAD__REV__MATRIX__TCROSSPROD_HPP__
2 #define __STAN__AGRAD__REV__MATRIX__TCROSSPROD_HPP__
3 
4 #include <vector>
5 #include <boost/math/tools/promotion.hpp>
11 #include <stan/agrad/rev/var.hpp>
16 
17 namespace stan {
18  namespace agrad {
19 
26  inline matrix_v
27  tcrossprod(const matrix_v& M) {
28  if (M.rows() == 0)
29  return matrix_v(0,0);
30  // if (M.rows() == 1)
31  // return M * M.transpose();
32 
33  // WAS JUST THIS
34  // matrix_v result(M.rows(),M.rows());
35  // return result.setZero().selfadjointView<Eigen::Upper>().rankUpdate(M);
36 
37  matrix_v MMt(M.rows(),M.rows());
38 
39  vari** vs
40  = (vari**)memalloc_.alloc((M.rows() * M.cols() ) * 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_vv_vari(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

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