Stan Math Library  2.14.0
reverse mode automatic differentiation
multiply_lower_tri_self_transpose.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_REV_MAT_FUN_MULTIPLY_LOWER_TRI_SELF_TRANSPOSE_HPP
2 #define STAN_MATH_REV_MAT_FUN_MULTIPLY_LOWER_TRI_SELF_TRANSPOSE_HPP
3 
7 #include <stan/math/rev/core.hpp>
12 #include <boost/math/tools/promotion.hpp>
13 #include <vector>
14 
15 namespace stan {
16  namespace math {
17 
18  inline matrix_v
20  // check_square("multiply_lower_tri_self_transpose",
21  // L, "L", (double*)0);
22  int K = L.rows();
23  int J = L.cols();
24  matrix_v LLt(K, K);
25  if (K == 0) return LLt;
26  // if (K == 1) {
27  // LLt(0, 0) = L(0, 0) * L(0, 0);
28  // return LLt;
29  // }
30  int Knz;
31  if (K >= J)
32  Knz = (K-J)*J + (J * (J + 1)) / 2;
33  else // if (K < J)
34  Knz = (K * (K + 1)) / 2;
35  vari** vs
36  = reinterpret_cast<vari**>(ChainableStack::memalloc_
37  .alloc(Knz * sizeof(vari*)));
38  int pos = 0;
39  for (int m = 0; m < K; ++m)
40  for (int n = 0; n < ((J < (m+1)) ? J : (m+1)); ++n) {
41  vs[pos++] = L(m, n).vi_;
42  }
43  for (int m = 0, mpos=0; m < K; ++m, mpos += (J < m) ? J : m) {
44  LLt(m, m) = var(new dot_self_vari(vs + mpos, (J < (m+1)) ? J : (m+1)));
45  for (int n = 0, npos = 0; n < m; ++n, npos += (J < n) ? J : n) {
46  LLt(m, n)
47  = LLt(n, m)
48  = var(new dot_product_vari<var, var>(vs + mpos, vs + npos,
49  (J < (n+1))?J:(n+1)));
50  }
51  }
52  return LLt;
53  }
54 
55  }
56 }
57 #endif
The variable implementation base class.
Definition: vari.hpp:30
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:30
Eigen::Matrix< var, Eigen::Dynamic, Eigen::Dynamic > matrix_v
The type of a matrix holding var values.
Definition: typedefs.hpp:20
Eigen::Matrix< fvar< T >, R, R > multiply_lower_tri_self_transpose(const Eigen::Matrix< fvar< T >, R, C > &m)
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–2016, Stan Development Team.