Stan Math Library  2.20.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 
4 #include <stan/math/rev/meta.hpp>
6 #include <stan/math/rev/core.hpp>
11 
12 namespace stan {
13 namespace math {
14 
21 inline matrix_v tcrossprod(const matrix_v& M) {
22  if (M.rows() == 0)
23  return matrix_v(0, 0);
24  // if (M.rows() == 1)
25  // return M * M.transpose();
26 
27  // WAS JUST THIS
28  // matrix_v result(M.rows(), M.rows());
29  // return result.setZero().selfadjointView<Eigen::Upper>().rankUpdate(M);
30 
31  matrix_v MMt(M.rows(), M.rows());
32 
33  vari** vs
34  = reinterpret_cast<vari**>(ChainableStack::instance_->memalloc_.alloc(
35  (M.rows() * M.cols()) * sizeof(vari*)));
36  int pos = 0;
37  for (int m = 0; m < M.rows(); ++m)
38  for (int n = 0; n < M.cols(); ++n)
39  vs[pos++] = M(m, n).vi_;
40  for (int m = 0; m < M.rows(); ++m)
41  MMt(m, m) = var(new internal::dot_self_vari(vs + m * M.cols(), M.cols()));
42  for (int m = 0; m < M.rows(); ++m) {
43  for (int n = 0; n < m; ++n) {
45  vs + m * M.cols(), vs + n * M.cols(), M.cols()));
46  MMt(n, m) = MMt(m, n);
47  }
48  }
49  return MMt;
50 }
51 
52 } // namespace math
53 } // namespace stan
54 #endif
The variable implementation base class.
Definition: vari.hpp:30
static STAN_THREADS_DEF AutodiffStackStorage * instance_
Eigen::Matrix< fvar< T >, R, R > tcrossprod(const Eigen::Matrix< fvar< T >, R, C > &m)
Definition: tcrossprod.hpp:12
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:33
Eigen::Matrix< var, Eigen::Dynamic, Eigen::Dynamic > matrix_v
The type of a matrix holding var values.
Definition: typedefs.hpp:17
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–2018, Stan Development Team.