All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
mdivide_right.hpp
Go to the documentation of this file.
#ifndef STAN_MATH_FWD_MAT_FUN_MDIVIDE_RIGHT_HPP
#define STAN_MATH_FWD_MAT_FUN_MDIVIDE_RIGHT_HPP
#include <stan/math/prim/mat/fun/Eigen.hpp>
#include <stan/math/prim/mat/fun/typedefs.hpp>
#include <stan/math/prim/mat/fun/mdivide_right.hpp>
#include <stan/math/prim/mat/err/check_multiplicable.hpp>
#include <stan/math/prim/mat/err/check_square.hpp>
#include <stan/math/fwd/core.hpp>
#include <stan/math/fwd/mat/fun/typedefs.hpp>
#include <stan/math/fwd/mat/fun/inverse.hpp>
#include <stan/math/fwd/mat/fun/multiply.hpp>
#include <stan/math/prim/mat/fun/multiply.hpp>
#include <stan/math/fwd/mat/fun/to_fvar.hpp>
#include <stan/math/prim/mat/fun/inverse.hpp>
#include <vector>
namespace stan {
namespace math {
template <typename T, int R1, int C1, int R2, int C2>
inline
Eigen::Matrix<fvar<T>, R1, C2>
mdivide_right(const Eigen::Matrix<fvar<T>, R1, C1> &A,
const Eigen::Matrix<fvar<T>, R2, C2> &b) {
using stan::math::multiply;
using stan::math::mdivide_right;
stan::math::check_square("mdivide_right", "b", b);
stan::math::check_multiplicable("mdivide_right",
"A", A,
"b", b);
Eigen::Matrix<T, R1, C2> A_mult_inv_b(A.rows(), b.cols());
Eigen::Matrix<T, R1, C2> deriv_A_mult_inv_b(A.rows(), b.cols());
Eigen::Matrix<T, R2, C2> deriv_b_mult_inv_b(b.rows(), b.cols());
Eigen::Matrix<T, R1, C1> val_A(A.rows(), A.cols());
Eigen::Matrix<T, R1, C1> deriv_A(A.rows(), A.cols());
Eigen::Matrix<T, R2, C2> val_b(b.rows(), b.cols());
Eigen::Matrix<T, R2, C2> deriv_b(b.rows(), b.cols());
for (int j = 0; j < A.cols(); j++) {
for (int i = 0; i < A.rows(); i++) {
val_A(i, j) = A(i, j).val_;
deriv_A(i, j) = A(i, j).d_;
}
}
for (int j = 0; j < b.cols(); j++) {
for (int i = 0; i < b.rows(); i++) {
val_b(i, j) = b(i, j).val_;
deriv_b(i, j) = b(i, j).d_;
}
}
A_mult_inv_b = mdivide_right(val_A, val_b);
deriv_A_mult_inv_b = mdivide_right(deriv_A, val_b);
deriv_b_mult_inv_b = mdivide_right(deriv_b, val_b);
Eigen::Matrix<T, R1, C2> deriv(A.rows(), b.cols());
deriv = deriv_A_mult_inv_b - multiply(A_mult_inv_b, deriv_b_mult_inv_b);
return stan::math::to_fvar(A_mult_inv_b, deriv);
}
template <typename T, int R1, int C1, int R2, int C2>
inline
Eigen::Matrix<fvar<T>, R1, C2>
mdivide_right(const Eigen::Matrix<fvar<T>, R1, C1> &A,
const Eigen::Matrix<double, R2, C2> &b) {
using stan::math::multiply;
using stan::math::mdivide_right;
stan::math::check_square("mdivide_right", "b", b);
stan::math::check_multiplicable("mdivide_right",
"A", A,
"b", b);
Eigen::Matrix<T, R2, C2> deriv_b_mult_inv_b(b.rows(), b.cols());
Eigen::Matrix<T, R1, C1> val_A(A.rows(), A.cols());
Eigen::Matrix<T, R1, C1> deriv_A(A.rows(), A.cols());
for (int j = 0; j < A.cols(); j++) {
for (int i = 0; i < A.rows(); i++) {
val_A(i, j) = A(i, j).val_;
deriv_A(i, j) = A(i, j).d_;
}
}
return stan::math::to_fvar(mdivide_right(val_A, b),
mdivide_right(deriv_A, b));
}
template <typename T, int R1, int C1, int R2, int C2>
inline
Eigen::Matrix<fvar<T>, R1, C2>
mdivide_right(const Eigen::Matrix<double, R1, C1> &A,
const Eigen::Matrix<fvar<T>, R2, C2> &b) {
using stan::math::multiply;
using stan::math::mdivide_right;
stan::math::check_square("mdivide_right", "b", b);
stan::math::check_multiplicable("mdivide_right",
"A", A,
"b", b);
Eigen::Matrix<T, R1, C2>
A_mult_inv_b(A.rows(), b.cols());
Eigen::Matrix<T, R2, C2> deriv_b_mult_inv_b(b.rows(), b.cols());
Eigen::Matrix<T, R2, C2> val_b(b.rows(), b.cols());
Eigen::Matrix<T, R2, C2> deriv_b(b.rows(), b.cols());
for (int j = 0; j < b.cols(); j++) {
for (int i = 0; i < b.rows(); i++) {
val_b(i, j) = b(i, j).val_;
deriv_b(i, j) = b(i, j).d_;
}
}
A_mult_inv_b = mdivide_right(A, val_b);
deriv_b_mult_inv_b = mdivide_right(deriv_b, val_b);
Eigen::Matrix<T, R1, C2>
deriv(A.rows(), b.cols());
deriv = -multiply(A_mult_inv_b, deriv_b_mult_inv_b);
return stan::math::to_fvar(A_mult_inv_b, deriv);
}
}
}
#endif
typedefs.hpp
multiply.hpp
core.hpp
check_multiplicable.hpp
stan::math::multiply
Eigen::Matrix< fvar< T >, R1, C1 > multiply(const Eigen::Matrix< fvar< T >, R1, C1 > &m, const fvar< T > &c)
Definition: multiply.hpp:20
multiply.hpp
stan::math::to_fvar
fvar< T > to_fvar(const T &x)
Definition: to_fvar.hpp:16
stan::math::check_multiplicable
bool check_multiplicable(const char *function, const char *name1, const T1 &y1, const char *name2, const T2 &y2)
Return true if the matrices can be multiplied.
Definition: check_multiplicable.hpp:33
inverse.hpp
inverse.hpp
Eigen.hpp
to_fvar.hpp
mdivide_right.hpp
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
stan::math::mdivide_right
Eigen::Matrix< fvar< T >, R1, C2 > mdivide_right(const Eigen::Matrix< fvar< T >, R1, C1 > &A, const Eigen::Matrix< fvar< T >, R2, C2 > &b)
Definition: mdivide_right.hpp:24
stan::math::fvar
Definition: fvar.hpp:13
typedefs.hpp
check_square.hpp

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