Stan Math Library: stan/math/rev/mat/fun/tcrossprod.hpp Source File

| Stan Math Library 2.10.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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/prim/mat/fun/Eigen.hpp>
5 #include <stan/math/prim/mat/fun/typedefs.hpp>
6 #include <stan/math/rev/core.hpp>
7 #include <stan/math/rev/mat/fun/Eigen_NumTraits.hpp>
8 #include <stan/math/rev/mat/fun/typedefs.hpp>
9 #include <stan/math/rev/mat/fun/dot_product.hpp>
10 #include <stan/math/rev/mat/fun/dot_self.hpp>
11 #include <stan/math/rev/mat/fun/columns_dot_self.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
core.hpp
Eigen_NumTraits.hpp
stan
Definition: log_sum_exp.hpp:8
stan::math::AutodiffStackStorage::memalloc_
static stack_alloc memalloc_
Definition: autodiffstackstorage.hpp:16
stan::math::vari
The variable implementation base class.
Definition: vari.hpp:30
stan::math::tcrossprod
Eigen::Matrix< fvar< T >, R, R > tcrossprod(const Eigen::Matrix< fvar< T >, R, C > &m)
Definition: tcrossprod.hpp:17
stan::math::var
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:31
columns_dot_self.hpp
stan::math::matrix_v
Eigen::Matrix< var, Eigen::Dynamic, Eigen::Dynamic > matrix_v
The type of a matrix holding stan::math::var values.
Definition: typedefs.hpp:21
dot_self.hpp
typedefs.hpp
dot_product.hpp
Eigen.hpp
stan::math::stack_alloc::alloc
void * alloc(size_t len)
Return a newly allocated block of memory of the appropriate size managed by the stack allocator...
Definition: stack_alloc.hpp:166
typedefs.hpp

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