1 #ifndef __STAN__AGRAD__MATRIX_HPP__
2 #define __STAN__AGRAD__MATRIX_HPP__
25 size_t length,
double *dotprod) {
27 for (
size_t i = 0; i <
length; i++)
28 result += v1[i*stride1].vi_->
val_ * v2[i*stride2].
vi_->
val_;
30 return alpha->vi_->
val_ * result;
40 for (
size_t i = 0; i <
length_; i++)
41 v1_[i] = v1[i*stride1].vi_;
42 for (
size_t i = 0; i <
length_; i++)
43 v2_[i] = v2[i*stride2].vi_;
47 for (
size_t i = 0; i <
length_; i++) {
63 template <>
struct NumTraits<stan::agrad::var>
131 RequireInitialization = 0,
135 HasFloatingPoint = 1,
144 struct significant_decimals_default_impl<stan::agrad::var,false>
146 static inline int run()
159 struct scalar_product_traits<stan::agrad::var,double> {
168 struct scalar_product_traits<double,stan::agrad::var> {
175 template<
typename Index,
bool ConjugateLhs,
bool ConjugateRhs>
176 struct general_matrix_vector_product<Index,stan::agrad::var,ColMajor,ConjugateLhs,
stan::agrad::var,ConjugateRhs>
180 typedef typename scalar_product_traits<LhsScalar, RhsScalar>::ReturnType
ResScalar;
181 enum { LhsStorageOrder = ColMajor };
183 EIGEN_DONT_INLINE
static void run(
189 for (Index i = 0; i <
rows; i++) {
190 res[i*resIncr] +=
stan::agrad::var(
new stan::agrad::gevv_vvv_vari(&alpha,((
int)LhsStorageOrder == (
int)ColMajor)?(&lhs[i]):(&lhs[i*lhsStride]),((
int)LhsStorageOrder == (
int)ColMajor)?(lhsStride):(1),rhs,rhsIncr,cols));
194 template<
typename Index,
bool ConjugateLhs,
bool ConjugateRhs>
195 struct general_matrix_vector_product<Index,stan::agrad::var,RowMajor,ConjugateLhs,
stan::agrad::var,ConjugateRhs>
199 typedef typename scalar_product_traits<LhsScalar, RhsScalar>::ReturnType
ResScalar;
200 enum { LhsStorageOrder = RowMajor };
202 EIGEN_DONT_INLINE
static void run(
208 for (Index i = 0; i <
rows; i++) {
209 res[i*resIncr] +=
stan::agrad::var(
new stan::agrad::gevv_vvv_vari(&alpha,((
int)LhsStorageOrder == (
int)ColMajor)?(&lhs[i]):(&lhs[i*lhsStride]),((
int)LhsStorageOrder == (
int)ColMajor)?(lhsStride):(1),rhs,rhsIncr,cols));
213 template<
typename Index,
int LhsStorageOrder,
bool ConjugateLhs,
int RhsStorageOrder,
bool ConjugateRhs>
214 struct general_matrix_matrix_product<Index,stan::agrad::var,LhsStorageOrder,ConjugateLhs,
stan::agrad::var,RhsStorageOrder,ConjugateRhs,ColMajor>
218 typedef typename scalar_product_traits<LhsScalar, RhsScalar>::ReturnType
ResScalar;
219 static void run(Index
rows, Index
cols, Index depth,
224 level3_blocking<LhsScalar,RhsScalar>& blocking,
225 GemmParallelInfo<Index>* info = 0)
227 for (Index i = 0; i <
cols; i++) {
228 general_matrix_vector_product<Index,LhsScalar,LhsStorageOrder,ConjugateLhs,RhsScalar,ConjugateRhs>::run(
229 rows,depth,_lhs,lhsStride,
230 &_rhs[((
int)RhsStorageOrder == (
int)ColMajor)?(i*rhsStride):(i)],((
int)RhsStorageOrder == (
int)ColMajor)?(1):(rhsStride),
231 &res[i*resStride],1,alpha);
272 template <
int R,
int C>
274 for (
int i = 0; i < matrix.size(); ++i)
281 template <
typename T>
283 for (
size_t i = 0; i < variables.size(); ++i)
344 for (
int j = 0; j < m.cols(); ++j)
345 for (
int i = 0; i < m.rows(); ++i)
360 m_v.resize(m.rows(), m.cols());
361 for (
int j = 0; j < m.cols(); ++j)
362 for (
int i = 0; i < m.rows(); ++i)
400 for (
int i = 0; i < v.size(); ++i)
415 v_v.resize(v.size());
416 for (
int i = 0; i < v.size(); ++i)
455 for (
int i = 0; i < rv.size(); ++i)
470 rv_v.resize(rv.size());
471 for (
int i = 0; i < rv.size(); ++i)
584 var
determinant(
const Eigen::Matrix<var, Eigen::Dynamic, Eigen::Dynamic>& m);
587 class dot_self_vari :
public vari {
592 dot_self_vari(vari** v,
size_t size)
593 : vari(var_dot_self(v,size)),
597 template <
int R,
int C>
598 dot_self_vari(
const Eigen::Matrix<var,R,C>& v) :
599 vari(var_dot_self(v)),
size_(v.size()) {
601 for (
size_t i = 0; i <
size_; ++i)
604 inline static double square(
double x) {
return x * x; }
605 inline static double var_dot_self(vari** v,
size_t size) {
607 for (
size_t i = 0; i < size; ++i)
608 sum +=
square(v[i]->val_);
611 template <
int R,
int C>
612 inline static double var_dot_self(
const Eigen::Matrix<var,R,C> &v) {
614 for (
int i = 0; i < v.size(); ++i)
615 sum +=
square(v(i).vi_->val_);
619 for (
size_t i = 0; i <
size_; ++i)
620 v_[i]->adj_ += adj_ * 2.0 *
v_[i]->val_;
624 class sum_v_vari :
public vari{
628 inline static double var_sum(
const var *v,
size_t length) {
630 for (
size_t i = 0; i <
length; i++)
631 result += v[i].vi_->val_;
634 template<
typename Derived>
635 inline static double var_sum(
const Eigen::DenseBase<Derived> &v) {
637 for (
int i = 0; i < v.size(); i++)
638 result += v(i).vi_->val_;
642 template<
typename Derived>
643 sum_v_vari(
const Eigen::DenseBase<Derived> &v) :
644 vari(var_sum(v)),
length_(v.size()) {
646 for (
size_t i = 0; i <
length_; i++)
649 template<
int R1,
int C1>
650 sum_v_vari(
const Eigen::Matrix<var,R1,C1> &v1) :
651 vari(var_sum(v1)), length_(v1.size()) {
653 for (
size_t i = 0; i <
length_; i++)
656 sum_v_vari(
const var *v,
size_t len) :
657 vari(var_sum(v,len)), length_(len) {
659 for (
size_t i = 0; i <
length_; i++)
663 for (
size_t i = 0; i <
length_; i++) {
668 class dot_product_vv_vari :
public vari {
673 inline static double var_dot(
const var* v1,
const var* v2,
676 for (
size_t i = 0; i <
length; i++)
677 result += v1[i].vi_->val_ * v2[i].vi_->val_;
680 template<
typename Derived1,
typename Derived2>
681 inline static double var_dot(
const Eigen::DenseBase<Derived1> &v1,
682 const Eigen::DenseBase<Derived2> &v2) {
684 for (
int i = 0; i < v1.size(); i++)
685 result += v1[i].vi_->val_ * v2[i].vi_->val_;
688 inline static double var_dot(vari** v1, vari** v2,
size_t length) {
690 for (
size_t i = 0; i <
length; ++i)
691 result += v1[i]->val_ * v2[i]->val_;
695 dot_product_vv_vari(vari** v1, vari** v2,
size_t length)
696 : vari(var_dot(v1,v2,length)),
702 dot_product_vv_vari(
const var* v1,
const var* v2,
size_t length,
703 dot_product_vv_vari* shared_v1 = NULL,
704 dot_product_vv_vari* shared_v2 = NULL) :
705 vari(var_dot(v1, v2, length)), length_(length) {
706 if (shared_v1 == NULL) {
708 for (
size_t i = 0; i <
length_; i++)
712 v1_ = shared_v1->v1_;
714 if (shared_v2 == NULL) {
716 for (
size_t i = 0; i <
length_; i++)
720 v2_ = shared_v2->v2_;
723 template<
typename Derived1,
typename Derived2>
724 dot_product_vv_vari(
const Eigen::DenseBase<Derived1> &v1,
725 const Eigen::DenseBase<Derived2> &v2,
726 dot_product_vv_vari* shared_v1 = NULL,
727 dot_product_vv_vari* shared_v2 = NULL) :
728 vari(var_dot(v1, v2)), length_(v1.size()) {
729 if (shared_v1 == NULL) {
731 for (
size_t i = 0; i <
length_; i++)
735 v1_ = shared_v1->v1_;
737 if (shared_v2 == NULL) {
739 for (
size_t i = 0; i <
length_; i++)
743 v2_ = shared_v2->v2_;
746 template<
int R1,
int C1,
int R2,
int C2>
747 dot_product_vv_vari(
const Eigen::Matrix<var,R1,C1> &v1,
748 const Eigen::Matrix<var,R2,C2> &v2,
749 dot_product_vv_vari* shared_v1 = NULL,
750 dot_product_vv_vari* shared_v2 = NULL) :
751 vari(var_dot(v1, v2)), length_(v1.size()) {
752 if (shared_v1 == NULL) {
754 for (
size_t i = 0; i <
length_; i++)
758 v1_ = shared_v1->v1_;
760 if (shared_v2 == NULL) {
762 for (
size_t i = 0; i <
length_; i++)
766 v2_ = shared_v2->v2_;
770 for (
size_t i = 0; i <
length_; i++) {
771 v1_[i]->adj_ += adj_ *
v2_[i]->val_;
772 v2_[i]->adj_ += adj_ *
v1_[i]->val_;
777 class dot_product_vd_vari :
public vari {
782 inline static double var_dot(
const var* v1,
const double* v2,
785 for (
size_t i = 0; i <
length; i++)
786 result += v1[i].vi_->val_ * v2[i];
789 template<
typename Derived1,
typename Derived2>
790 inline static double var_dot(
const Eigen::DenseBase<Derived1> &v1,
791 const Eigen::DenseBase<Derived2> &v2) {
793 for (
int i = 0; i < v1.size(); i++)
794 result += v1[i].vi_->val_ * v2[i];
798 dot_product_vd_vari(
const var* v1,
const double* v2,
size_t length,
799 dot_product_vd_vari *shared_v1 = NULL,
800 dot_product_vd_vari *shared_v2 = NULL) :
801 vari(var_dot(v1, v2, length)), length_(length) {
802 if (shared_v1 == NULL) {
804 for (
size_t i = 0; i <
length_; i++)
807 v1_ = shared_v1->v1_;
809 if (shared_v2 == NULL) {
811 for (
size_t i = 0; i <
length_; i++)
814 v2_ = shared_v2->v2_;
817 template<
typename Derived1,
typename Derived2>
818 dot_product_vd_vari(
const Eigen::DenseBase<Derived1> &v1,
819 const Eigen::DenseBase<Derived2> &v2,
820 dot_product_vd_vari *shared_v1 = NULL,
821 dot_product_vd_vari *shared_v2 = NULL) :
822 vari(var_dot(v1, v2)), length_(v1.size()) {
823 if (shared_v1 == NULL) {
825 for (
size_t i = 0; i <
length_; i++)
828 v1_ = shared_v1->v1_;
830 if (shared_v2 == NULL) {
832 for (
size_t i = 0; i <
length_; i++)
835 v2_ = shared_v2->v2_;
838 template<
int R1,
int C1,
int R2,
int C2>
839 dot_product_vd_vari(
const Eigen::Matrix<var,R1,C1> &v1,
840 const Eigen::Matrix<double,R2,C2> &v2,
841 dot_product_vd_vari *shared_v1 = NULL,
842 dot_product_vd_vari *shared_v2 = NULL) :
843 vari(var_dot(v1, v2)), length_(v1.size()) {
844 if (shared_v1 == NULL) {
846 for (
size_t i = 0; i <
length_; i++)
849 v1_ = shared_v1->v1_;
851 if (shared_v2 == NULL) {
853 for (
size_t i = 0; i <
length_; i++)
856 v2_ = shared_v2->v2_;
860 for (
size_t i = 0; i <
length_; i++) {
861 v1_[i]->adj_ += adj_ *
v2_[i];
878 template<
int R,
int C>
881 return var(
new dot_self_vari(v));
892 template<
int R1,
int C1,
int R2,
int C2>
894 const Eigen::Matrix<var, R2, C2>& v2) {
898 return var(
new dot_product_vv_vari(v1,v2));
909 template<
int R1,
int C1,
int R2,
int C2>
911 const Eigen::Matrix<double, R2, C2>& v2) {
915 return var(
new dot_product_vd_vari(v1,v2));
926 template<
int R1,
int C1,
int R2,
int C2>
928 const Eigen::Matrix<var, R2, C2>& v2) {
932 return var(
new dot_product_vd_vari(v2,v1));
943 return var(
new dot_product_vv_vari(v1, v2, length));
954 return var(
new dot_product_vd_vari(v1, v2, length));
965 return var(
new dot_product_vd_vari(v2, v1, length));
976 const std::vector<var>& v2) {
978 return var(
new dot_product_vv_vari(&v1[0], &v2[0], v1.size()));
989 const std::vector<double>& v2) {
991 return var(
new dot_product_vd_vari(&v1[0], &v2[0], v1.size()));
1002 const std::vector<var>& v2) {
1004 return var(
new dot_product_vd_vari(&v2[0], &v1[0], v1.size()));
1013 template <
int R,
int C>
1014 inline var sum(
const Eigen::Matrix<var,R,C>& m) {
1017 return var(
new sum_v_vari(m));
1031 template <
typename T1,
typename T2,
int R,
int C>
1032 inline Eigen::Matrix<typename boost::math::tools::promote_args<T1,T2>::type, R, C>
1033 add(
const Eigen::Matrix<T1, R, C>& v1,
1034 const Eigen::Matrix<T2, R, C>& v2) {
1044 template <
typename T1,
typename T2,
int R,
int C>
1045 inline Eigen::Matrix<typename boost::math::tools::promote_args<T1,T2>::type, R, C>
1046 add(
const Eigen::Matrix<T1, R, C>& m,
1056 template <
typename T1,
typename T2,
int R,
int C>
1057 inline Eigen::Matrix<typename boost::math::tools::promote_args<T1,T2>::type, R, C>
1059 const Eigen::Matrix<T2, R, C>& m) {
1072 template <
typename T1,
typename T2,
int R,
int C>
1073 inline Eigen::Matrix<typename boost::math::tools::promote_args<T1,T2>::type, R, C>
1075 const Eigen::Matrix<T2, R, C>& v2) {
1085 template <
typename T1,
typename T2,
int R,
int C>
1086 inline Eigen::Matrix<typename boost::math::tools::promote_args<T1,T2>::type, R, C>
1097 template <
typename T1,
typename T2,
int R,
int C>
1098 inline Eigen::Matrix<typename boost::math::tools::promote_args<T1,T2>::type, R, C>
1100 const Eigen::Matrix<T2, R, C>& m) {
1110 template <
typename T>
1120 template <
typename T,
int R,
int C>
1121 inline Eigen::Matrix<T,R,C>
minus(
const Eigen::Matrix<T, R, C>& v) {
1136 template <
typename T1,
typename T2>
1148 template <
typename T1,
typename T2,
int R,
int C>
1149 inline Eigen::Matrix<var,R,C>
1150 divide(
const Eigen::Matrix<T1, R,C>& v,
const T2& c) {
1160 template <
typename T1,
typename T2>
1161 inline Eigen::Matrix<stan::agrad::var,Eigen::Dynamic,Eigen::Dynamic>
1163 const Eigen::Matrix<T2,Eigen::Dynamic,Eigen::Dynamic>& m2) {
1165 Eigen::Matrix<stan::agrad::var,Eigen::Dynamic, Eigen::Dynamic>
1166 result(m1.rows(),m1.cols());
1167 for (
int j = 0; j < m1.cols(); ++j)
1168 for (
int i = 0; i < m1.rows(); ++i)
1169 result(i,j) = m1(i,j) * m2(i,j);
1178 template <
typename T1,
typename T2>
1179 inline Eigen::Matrix<stan::agrad::var,Eigen::Dynamic,1>
1181 const Eigen::Matrix<T2,Eigen::Dynamic,1>& v2) {
1183 Eigen::Matrix<stan::agrad::var,Eigen::Dynamic,1> result(v1.size());
1184 for (
int i = 0; i < v1.size(); ++i)
1185 result(i) = v1(i) * v2(i);
1194 template <
typename T1,
typename T2>
1195 inline Eigen::Matrix<stan::agrad::var,1,Eigen::Dynamic>
1197 const Eigen::Matrix<T2,1,Eigen::Dynamic>& v2) {
1199 Eigen::Matrix<stan::agrad::var,1,Eigen::Dynamic> result(v1.size());
1200 for (
int i = 0; i < v1.size(); ++i)
1201 result(i) = v1(i) * v2(i);
1212 template <
typename T1,
typename T2>
1213 inline Eigen::Matrix<stan::agrad::var,Eigen::Dynamic,Eigen::Dynamic>
1214 elt_divide(
const Eigen::Matrix<T1,Eigen::Dynamic,Eigen::Dynamic>& m1,
1215 const Eigen::Matrix<T2,Eigen::Dynamic,Eigen::Dynamic>& m2) {
1217 Eigen::Matrix<stan::agrad::var,Eigen::Dynamic, Eigen::Dynamic>
1218 result(m1.rows(),m1.cols());
1219 for (
int j = 0; j < m1.cols(); ++j)
1220 for (
int i = 0; i < m1.rows(); ++i)
1221 result(i,j) = m1(i,j) / m2(i,j);
1230 template <
typename T1,
typename T2>
1231 inline Eigen::Matrix<stan::agrad::var,Eigen::Dynamic,1>
1233 const Eigen::Matrix<T2,Eigen::Dynamic,1>& v2) {
1235 Eigen::Matrix<stan::agrad::var,Eigen::Dynamic,1> result(v1.size());
1236 for (
int i = 0; i < v1.size(); ++i)
1237 result(i) = v1(i) / v2(i);
1246 template <
typename T1,
typename T2>
1247 inline Eigen::Matrix<stan::agrad::var,1,Eigen::Dynamic>
1249 const Eigen::Matrix<T2,1,Eigen::Dynamic>& v2) {
1251 Eigen::Matrix<stan::agrad::var,1,Eigen::Dynamic> result(v1.size());
1252 for (
int i = 0; i < v1.size(); ++i)
1253 result(i) = v1(i) / v2(i);
1263 template <
typename T1,
typename T2>
1265 typename boost::math::tools::promote_args<T1,T2>::type
1276 template<
typename T1,
typename T2,
int R2,
int C2>
1278 const Eigen::Matrix<T2, R2, C2>& m) {
1290 template<
typename T1,
int R1,
int C1,
typename T2>
1291 inline Eigen::Matrix<var,R1,C1>
multiply(
const Eigen::Matrix<T1, R1, C1>& m,
1306 template<
int R1,
int C1,
int R2,
int C2>
1307 inline Eigen::Matrix<var,R1,C2>
multiply(
const Eigen::Matrix<var,R1,C1>& m1,
1308 const Eigen::Matrix<var,R2,C2>& m2) {
1310 Eigen::Matrix<var,R1,C2> result(m1.rows(),m2.cols());
1311 for (
int i = 0; i < m1.rows(); i++) {
1312 typename Eigen::Matrix<var,R1,C1>::ConstRowXpr crow(m1.row(i));
1313 for (
int j = 0; j < m2.cols(); j++) {
1314 typename Eigen::Matrix<var,R2,C2>::ConstColXpr ccol(m2.col(j));
1317 result(i,j) =
var(
new dot_product_vv_vari(crow,ccol));
1320 dot_product_vv_vari *v2 =
static_cast<dot_product_vv_vari*
>(result(0,j).vi_);
1321 result(i,j) =
var(
new dot_product_vv_vari(crow,ccol,NULL,v2));
1326 dot_product_vv_vari *v1 =
static_cast<dot_product_vv_vari*
>(result(i,0).vi_);
1327 result(i,j) =
var(
new dot_product_vv_vari(crow,ccol,v1));
1330 dot_product_vv_vari *v1 =
static_cast<dot_product_vv_vari*
>(result(i,0).vi_);
1331 dot_product_vv_vari *v2 =
static_cast<dot_product_vv_vari*
>(result(0,j).vi_);
1332 result(i,j) =
var(
new dot_product_vv_vari(crow,ccol,v1,v2));
1350 template<
int R1,
int C1,
int R2,
int C2>
1351 inline Eigen::Matrix<var,R1,C2>
multiply(
const Eigen::Matrix<double,R1,C1>& m1,
1352 const Eigen::Matrix<var,R2,C2>& m2) {
1354 Eigen::Matrix<var,R1,C2> result(m1.rows(),m2.cols());
1355 for (
int i = 0; i < m1.rows(); i++) {
1356 typename Eigen::Matrix<double,R1,C1>::ConstRowXpr crow(m1.row(i));
1357 for (
int j = 0; j < m2.cols(); j++) {
1358 typename Eigen::Matrix<var,R2,C2>::ConstColXpr ccol(m2.col(j));
1362 result(i,j) =
var(
new dot_product_vd_vari(ccol,crow));
1365 dot_product_vd_vari *v2 =
static_cast<dot_product_vd_vari*
>(result(0,j).vi_);
1366 result(i,j) =
var(
new dot_product_vd_vari(ccol,crow,v2,NULL));
1371 dot_product_vd_vari *v1 =
static_cast<dot_product_vd_vari*
>(result(i,0).vi_);
1372 result(i,j) =
var(
new dot_product_vd_vari(ccol,crow,NULL,v1));
1375 dot_product_vd_vari *v1 =
static_cast<dot_product_vd_vari*
>(result(i,0).vi_);
1376 dot_product_vd_vari *v2 =
static_cast<dot_product_vd_vari*
>(result(0,j).vi_);
1377 result(i,j) =
var(
new dot_product_vd_vari(ccol,crow,v2,v1));
1395 template<
int R1,
int C1,
int R2,
int C2>
1396 inline Eigen::Matrix<var,R1,C2>
multiply(
const Eigen::Matrix<var,R1,C1>& m1,
1397 const Eigen::Matrix<double,R2,C2>& m2) {
1399 Eigen::Matrix<var,R1,C2> result(m1.rows(),m2.cols());
1400 for (
int i = 0; i < m1.rows(); i++) {
1401 typename Eigen::Matrix<var,R1,C1>::ConstRowXpr crow(m1.row(i));
1402 for (
int j = 0; j < m2.cols(); j++) {
1403 typename Eigen::Matrix<double,R2,C2>::ConstColXpr ccol(m2.col(j));
1407 result(i,j) =
var(
new dot_product_vd_vari(crow,ccol));
1410 dot_product_vd_vari *v2 =
static_cast<dot_product_vd_vari*
>(result(0,j).vi_);
1411 result(i,j) =
var(
new dot_product_vd_vari(crow,ccol,NULL,v2));
1416 dot_product_vd_vari *v1 =
static_cast<dot_product_vd_vari*
>(result(i,0).vi_);
1417 result(i,j) =
var(
new dot_product_vd_vari(crow,ccol,v1,NULL));
1420 dot_product_vd_vari *v1 =
static_cast<dot_product_vd_vari*
>(result(i,0).vi_);
1421 dot_product_vd_vari *v2 =
static_cast<dot_product_vd_vari*
>(result(0,j).vi_);
1422 result(i,j) =
var(
new dot_product_vd_vari(crow,ccol,v1,v2));
1439 template <
int C1,
int R2>
1441 const Eigen::Matrix<var, R2, 1>& v) {
1442 if (rv.size() != v.size())
1443 throw std::domain_error(
"row vector and vector must be same length in multiply");
1455 template <
int C1,
int R2>
1457 const Eigen::Matrix<var, R2, 1>& v) {
1470 template <
int C1,
int R2>
1472 const Eigen::Matrix<double, R2, 1>& v) {
1482 if (K == 0)
return LLt;
1487 int Knz = (K * (K + 1)) / 2;
1491 for (
int m = 0; m < K; ++m)
1492 for (
int n = 0; n <= m; ++n)
1493 vs[pos++] = L(m,n).vi_;
1494 for (
int m = 0, mpos=0; m < K; ++m, mpos += m) {
1496 LLt(m,m) =
var(
new dot_self_vari(vs + mpos, m + 1));
1497 for (
int n = 0, npos = 0; n < m; ++n, npos += n)
1498 LLt(m,n) = LLt(n,m) =
var(
new dot_product_vv_vari(vs + mpos, vs + npos, n + 1));
1514 return M * M.transpose();
1516 matrix_v result(M.rows(),M.rows());
1517 return result.setZero().selfadjointView<Eigen::Upper>().rankUpdate(M);
1608 template<
int R1,
int C1,
int R2,
int C2>
1610 const Eigen::Matrix<var,R2,C2> &b) {
1613 return A.template triangularView<Eigen::Lower>().solve(b);
1617 template<
int R1,
int C1,
int R2,
int C2>
1619 const Eigen::Matrix<double,R2,C2> &b) {
1622 return A.template triangularView<Eigen::Lower>().solve(
to_var(b));
1624 template<
int R1,
int C1,
int R2,
int C2>
1626 const Eigen::Matrix<var,R2,C2> &b) {
1629 return to_var(A).template triangularView<Eigen::Lower>().solve(b);
1632 inline Eigen::Matrix<var,Eigen::Dynamic,Eigen::Dynamic>
1636 Eigen::Matrix<var,Eigen::Dynamic,Eigen::Dynamic> b;
1638 A.triangularView<Eigen::Lower>().solveInPlace(b);
1651 template<
int TriView,
int R1,
int C1,
int R2,
int C2>
1653 const Eigen::Matrix<var,R2,C2> &b) {
1656 return A.template triangularView<TriView>().solve(b);
1667 template<
int TriView,
int R1,
int C1,
int R2,
int C2>
1669 const Eigen::Matrix<double,R2,C2> &b) {
1672 return A.template triangularView<TriView>().solve(
to_var(b));
1683 template<
int TriView,
int R1,
int C1,
int R2,
int C2>
1685 const Eigen::Matrix<var,R2,C2> &b) {
1688 return to_var(A).template triangularView<TriView>().solve(b);
1698 template<
int TriView>
1699 inline Eigen::Matrix<var,Eigen::Dynamic,Eigen::Dynamic>
1703 Eigen::Matrix<var,Eigen::Dynamic,Eigen::Dynamic> b;
1705 A.triangularView<TriView>().solveInPlace(b);
1717 template<
int R1,
int C1,
int R2,
int C2>
1718 inline Eigen::Matrix<var,R1,C2>
mdivide_left(
const Eigen::Matrix<var,R1,C1> &A,
1719 const Eigen::Matrix<var,R2,C2> &b) {
1722 return A.lu().solve(b);
1732 template<
int R1,
int C1,
int R2,
int C2>
1733 inline Eigen::Matrix<var,R1,C2>
mdivide_left(
const Eigen::Matrix<double,R1,C1> &A,
1734 const Eigen::Matrix<var,R2,C2> &b) {
1738 return to_var(A).lu().solve(b);
1748 template<
int R1,
int C1,
int R2,
int C2>
1749 inline Eigen::Matrix<var,R1,C2>
mdivide_left(
const Eigen::Matrix<var,R1,C1> &A,
1750 const Eigen::Matrix<double,R2,C2> &b) {
1753 return A.lu().solve(
to_var(b));
1764 template<
int R1,
int C1,
int R2,
int C2>
1765 inline Eigen::Matrix<var,R1,C2>
mdivide_right(
const Eigen::Matrix<var,R1,C1> &b,
1766 const Eigen::Matrix<var,R2,C2> &A) {
1769 return A.transpose().lu().solve(b.transpose()).
transpose();
1779 template<
int R1,
int C1,
int R2,
int C2>
1780 inline Eigen::Matrix<var,R1,C2>
mdivide_right(
const Eigen::Matrix<double,R1,C1> &b,
1781 const Eigen::Matrix<var,R2,C2> &A) {
1794 template<
int R1,
int C1,
int R2,
int C2>
1795 inline Eigen::Matrix<var,R1,C2>
mdivide_right(
const Eigen::Matrix<var,R1,C1> &b,
1796 const Eigen::Matrix<double,R2,C2> &A) {
1811 template<
int TriView,
typename T1,
typename T2,
int R1,
int C1,
int R2,
int C2>
1812 inline Eigen::Matrix<typename boost::math::tools::promote_args<T1,T2>::type,R1,C2>
1814 const Eigen::Matrix<T2,R2,C2> &A) {
1818 .template triangularView<TriView>()
1833 template<
typename T1,
typename T2,
int R1,
int C1,
int R2,
int C2>
1834 inline Eigen::Matrix<typename boost::math::tools::promote_args<T1,T2>::type,R1,C2>
1836 const Eigen::Matrix<T2,R2,C2> &A) {
1837 return mdivide_right_tri<Eigen::Lower,T1,T2,R1,C1,R2,C2>(b,A);
1979 template <
typename T_result,
class Policy>
1981 const Eigen::Matrix<var,Eigen::Dynamic,Eigen::Dynamic>& y,
1988 Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic> y_d(y.rows(),y.cols());
1989 for (size_type i = 0; i < y_d.rows(); i++)
1990 for (size_type j = 0; j < y_d.cols(); j++)
1991 y_d(i,j) = y(i,j).val();
1995 template <
typename LHS,
typename RHS>
1997 for (
size_t i = 0; i < x.size(); ++i)
2000 template <
typename LHS,
typename RHS>
2002 const Eigen::Matrix<RHS,Eigen::Dynamic,1>& y) {
2006 for (size_type i = 0; i < x.size(); ++i)
2009 template <
typename LHS,
typename RHS>
2011 const Eigen::Matrix<RHS,1,Eigen::Dynamic>& y) {
2015 for (size_type i = 0; i < x.size(); ++i)
2018 template <
typename LHS,
typename RHS>
2020 const Eigen::Matrix<RHS,Eigen::Dynamic,Eigen::Dynamic>& y) {
2027 for (size_type1 n = 0; n < x.cols(); ++n)
2028 for (size_type2 m = 0; m < x.rows(); ++m)
2033 template <
typename LHS,
typename RHS>
2039 template <
bool PromoteRHS,
typename LHS,
typename RHS>
2042 throw std::domain_error(
"should not call base class of assigner");
2046 template <
typename LHS,
typename RHS>
2053 template <
typename LHS,
typename RHS>
2061 template <
typename LHS,
typename RHS>
2066 void stan_print(std::ostream* o,
const var& x);