All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Groups
log_determinant.hpp
Go to the documentation of this file.
#ifndef STAN_MATH_REV_MAT_FUN_LOG_DETERMINANT_HPP
#define STAN_MATH_REV_MAT_FUN_LOG_DETERMINANT_HPP
#include <stan/math/prim/mat/fun/Eigen.hpp>
#include <stan/math/prim/mat/err/check_square.hpp>
#include <stan/math/rev/core.hpp>
namespace stan {
namespace math {
template <int R, int C>
inline var log_determinant(const Eigen::Matrix<var, R, C>& m) {
using Eigen::Matrix;
math::check_square("log_determinant", "m", m);
Matrix<double, R, C> m_d(m.rows(), m.cols());
for (int i = 0; i < m.size(); ++i)
m_d(i) = m(i).val();
Eigen::FullPivHouseholderQR<Matrix<double, R, C> > hh
= m_d.fullPivHouseholderQr();
double val = hh.logAbsDeterminant();
vari** varis
= ChainableStack::memalloc_.alloc_array<vari*>(m.size());
for (int i = 0; i < m.size(); ++i)
varis[i] = m(i).vi_;
Matrix<double, R, C> m_inv_transpose = hh.inverse().transpose();
double* gradients
= ChainableStack::memalloc_.alloc_array<double>(m.size());
for (int i = 0; i < m.size(); ++i)
gradients[i] = m_inv_transpose(i);
return var(new precomputed_gradients_vari(val, m.size(),
varis, gradients));
}
}
}
#endif
core.hpp
stan::math::AutodiffStackStorage::memalloc_
static stack_alloc memalloc_
Definition: autodiffstackstorage.hpp:16
stan::math::vari
The variable implementation base class.
Definition: vari.hpp:28
stan::math::log_determinant
fvar< T > log_determinant(const Eigen::Matrix< fvar< T >, R, C > &m)
Definition: log_determinant.hpp:20
stan::math::var
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:32
stan::math::precomputed_gradients_vari
A variable implementation taking a sequence of operands and partial derivatives with respect to the o...
Definition: precomputed_gradients.hpp:21
Eigen.hpp
stan::math::stack_alloc::alloc_array
T * alloc_array(size_t n)
Allocate an array on the arena of the specified size to hold values of the specified template paramet...
Definition: stack_alloc.hpp:190
stan::math::check_square
bool check_square(const char *function, const char *name, const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y)
Return true if the specified matrix is square.
Definition: check_square.hpp:28
check_square.hpp

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