Esempio n. 1
0
 /**
  * Converts argument to an automatic differentiation variable.
  *
  * Returns a stan::math::var variable with the input value.
  *
  * @param[in] m A Matrix with scalars
  * @return A Matrix with automatic differentiation variables
  */
 inline matrix_v to_var(const stan::math::matrix_d& m) {
   matrix_v m_v(m.rows(), m.cols());
   for (int j = 0; j < m.cols(); ++j)
     for (int i = 0; i < m.rows(); ++i)
       m_v(i, j) = m(i, j);
   return m_v;
 }
Esempio n. 2
0
std::vector<double> finite_differences(const size_t row, const size_t col,
                                         const stan::math::matrix_d A,
                                         const bool calc_A_partials,
                                         const stan::math::matrix_d B,
                                         const bool calc_B_partials) {
  const double e = 1e-8;
  std::vector<double> finite_diff;
  stan::math::matrix_d C_plus, C_minus;

  if (calc_A_partials) {
    for (size_type j = 0; j < A.cols(); j++) {
      for (size_type i = 0; i < A.rows(); i++) {
        stan::math::matrix_d A_plus(A), A_minus(A);
        A_plus(i,j) += e;
        A_plus(j,i) = A_plus(i,j);
        A_minus(i,j) -= e;
        A_minus(j,i) = A_minus(i,j);
        stan::math::LDLT_factor<double,-1,-1> ldlt_A_plus;
        stan::math::LDLT_factor<double,-1,-1> ldlt_A_minus;
        ldlt_A_plus.compute(A_plus);
        ldlt_A_minus.compute(A_minus);
      
        C_plus = stan::math::mdivide_left_ldlt(ldlt_A_plus, B);
        C_minus = stan::math::mdivide_left_ldlt(ldlt_A_minus, B);
        finite_diff.push_back((C_plus(row,col) - C_minus(row,col)) / (2*e));
      }
    }
  }
  if (calc_B_partials) {
    stan::math::LDLT_factor<double,-1,-1> ldlt_A;
    ldlt_A.compute(A);
    for (size_type j = 0; j < B.cols(); j++) {
      for (size_type i = 0; i < B.rows(); i++) {
        stan::math::matrix_d B_plus(B);
        stan::math::matrix_d B_minus(B);
        B_plus(i,j) += e;
        B_minus(i,j) -= e;
        
        C_plus = stan::math::mdivide_left_ldlt(ldlt_A, B_plus);
        C_minus = stan::math::mdivide_left_ldlt(ldlt_A, B_minus);
        finite_diff.push_back((C_plus(row,col) - C_minus(row,col)) / (2*e));
      }
    }
  }
  return finite_diff;
}