Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations 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>
8 #include <stan/agrad/rev/var.hpp>
13 
14 namespace stan {
15  namespace agrad {
16 
23  inline matrix_v
24  tcrossprod(const matrix_v& M) {
25  if (M.rows() == 0)
26  return matrix_v(0,0);
27  // if (M.rows() == 1)
28  // return M * M.transpose();
29 
30  // WAS JUST THIS
31  // matrix_v result(M.rows(),M.rows());
32  // return result.setZero().selfadjointView<Eigen::Upper>().rankUpdate(M);
33 
34  matrix_v MMt(M.rows(),M.rows());
35 
36  vari** vs
37  = (vari**)memalloc_.alloc((M.rows() * M.cols() ) * sizeof(vari*));
38  int pos = 0;
39  for (int m = 0; m < M.rows(); ++m)
40  for (int n = 0; n < M.cols(); ++n)
41  vs[pos++] = M(m,n).vi_;
42  for (int m = 0; m < M.rows(); ++m)
43  MMt(m,m) = var(new dot_self_vari(vs + m * M.cols(),M.cols()));
44  for (int m = 0; m < M.rows(); ++m) {
45  for (int n = 0; n < m; ++n) {
46  MMt(m,n) = var(new dot_product_vari<var,var>(vs + m * M.cols(),
47  vs + n * M.cols(),
48  M.cols()));
49  MMt(n,m) = MMt(m,n);
50  }
51  }
52  return MMt;
53  }
54 
55  }
56 }
57 #endif
memory::stack_alloc memalloc_
Definition: var_stack.cpp:16
Eigen::Matrix< var, Eigen::Dynamic, Eigen::Dynamic > matrix_v
The type of a matrix holding stan::agrad::var values.
Definition: typedefs.hpp:21
The variable implementation base class.
Definition: vari.hpp:28
void * alloc(size_t len)
Return a newly allocated block of memory of the appropriate size managed by the stack allocator...
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:27
Eigen::Matrix< fvar< T >, R, R > tcrossprod(const Eigen::Matrix< fvar< T >, R, C > &m)
Definition: tcrossprod.hpp:17

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