1 #ifndef __STAN__MATH__MATRIX_HPP__
2 #define __STAN__MATH__MATRIX_HPP__
9 #include <boost/math/tools/promotion.hpp>
11 #define EIGEN_DENSEBASE_PLUGIN "stan/math/EigenDenseBaseAddons.hpp"
12 #include <Eigen/Dense>
23 template <
typename F,
typename T>
25 inline static void promote(
const F& u, T& t) {
35 inline static void promote(
const T& u, T& t) {
44 template <
typename F,
typename T>
46 inline static void promote(
const std::vector<F>& u,
49 for (
size_t i = 0; i < u.size(); ++i)
52 inline static std::vector<T>
62 inline static void promote(
const std::vector<T>& u,
66 inline static std::vector<T>
promote_to(
const std::vector<T>& u) {
72 template <
typename F,
typename T,
int R,
int C>
74 inline static void promote(
const Eigen::Matrix<F,R,C>& u,
75 Eigen::Matrix<T,R,C>& t) {
76 t.resize(u.rows(), u.cols());
77 for (
int i = 0; i < u.size(); ++i)
80 inline static Eigen::Matrix<T,R,C>
82 Eigen::Matrix<T,R,C> t;
88 template <
typename T,
int R,
int C>
90 inline static void promote(
const Eigen::Matrix<T,R,C>& u,
91 Eigen::Matrix<T,R,C>& t) {
94 inline static Eigen::Matrix<T,R,C>
promote_to(
const Eigen::Matrix<T,R,C>& u) {
99 template <
typename T1,
typename T2>
101 typedef typename boost::math::tools::promote_args<T1,T2>::type
type;
104 template <
typename T1,
typename T2>
106 typedef std::vector<typename common_type<T1,T2>::type>
type;
109 template <
typename T1,
typename T2,
int R,
int C>
111 typedef Eigen::Matrix<typename common_type<T1,T2>::type,R,C>
type;
114 template <
typename T1,
typename T2,
typename F>
130 template <
typename T>
134 template <
typename F>
150 Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic>
157 Eigen::Matrix<double,Eigen::Dynamic,1>
164 Eigen::Matrix<double,1,Eigen::Dynamic>
169 template <
typename T>
170 void resize(Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>& x,
171 const std::vector<size_t>& dims,
173 x.resize(dims[pos],dims[pos+1]);
176 template <
typename T>
177 void resize(Eigen::Matrix<T,Eigen::Dynamic,1>& x,
178 const std::vector<size_t>& dims,
183 template <
typename T>
184 void resize(Eigen::Matrix<T,1,Eigen::Dynamic>& x,
185 const std::vector<size_t>& dims,
192 const std::vector<size_t>& dims,
197 template <
typename T>
198 void resize(std::vector<T>& x,
199 const std::vector<size_t>& dims,
203 if (pos >= dims.size())
return;
204 for (
size_t i = 0; i < x.size(); ++i)
219 template <
typename T>
220 inline void resize(T& x, std::vector<size_t> dims) {
229 void raise_range_error(
size_t max,
234 s <<
"INDEX OPERATOR [] OUT OF BOUNDS"
237 <<
"; upper bound=" <<
max
238 <<
"; index position=" << idx
241 throw std::out_of_range(s.str());
245 void check_range(
size_t max,
250 if (i < 1 || i >
max)
251 raise_range_error(
max,i,msg,idx);
271 template <
typename T>
275 const char* error_msg,
277 check_range(x.size(),i,error_msg,idx);
296 template <
typename T>
301 const char* error_msg,
303 check_range(x.size(),i1,error_msg,idx);
304 return get_base1(x[i1 - 1],i2,error_msg,idx+1);
323 template <
typename T>
325 T&
get_base1(std::vector<std::vector<std::vector<T> > >& x,
329 const char* error_msg,
331 check_range(x.size(),i1,error_msg,idx);
332 return get_base1(x[i1 - 1],i2,i3,error_msg,idx+1);
352 template <
typename T>
354 T&
get_base1(std::vector<std::vector<std::vector<std::vector<T> > > >& x,
359 const char* error_msg,
361 check_range(x.size(),i1,error_msg,idx);
362 return get_base1(x[i1 - 1],i2,i3,i4,error_msg,idx+1);
383 template <
typename T>
385 T&
get_base1(std::vector<std::vector<std::vector<std::vector<std::vector<T> > > > >& x,
391 const char* error_msg,
393 check_range(x.size(),i1,error_msg,idx);
394 return get_base1(x[i1 - 1],i2,i3,i4,i5,error_msg,idx+1);
416 template <
typename T>
418 T&
get_base1(std::vector<std::vector<std::vector<std::vector<std::vector<std::vector<T> > > > > >& x,
425 const char* error_msg,
427 check_range(x.size(),i1,error_msg,idx);
428 return get_base1(x[i1 - 1],i2,i3,i4,i5,i6,error_msg,idx+1);
452 template <
typename T>
454 T&
get_base1(std::vector<std::vector<std::vector<std::vector<std::vector<std::vector<std::vector<T> > > > > > >& x,
462 const char* error_msg,
464 check_range(x.size(),i1,error_msg,idx);
465 return get_base1(x[i1 - 1],i2,i3,i4,i5,i6,i7,error_msg,idx+1);
490 template <
typename T>
492 T&
get_base1(std::vector<std::vector<std::vector<std::vector<std::vector<std::vector<std::vector<std::vector<T> > > > > > > >& x,
501 const char* error_msg,
503 check_range(x.size(),i1,error_msg,idx);
504 return get_base1(x[i1 - 1],i2,i3,i4,i5,i6,i7,i8,error_msg,idx+1);
528 template <
typename T>
530 Eigen::Matrix<T,1,Eigen::Dynamic>
531 get_base1(Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>& x,
533 const char* error_msg,
535 check_range(x.rows(),m,error_msg,idx);
555 template <
typename T>
557 T&
get_base1(Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>& x,
560 const char* error_msg,
562 check_range(x.rows(),m,error_msg,idx);
563 check_range(x.cols(),n,error_msg,idx + 1);
564 return x(m - 1, n - 1);
581 template <
typename T>
585 const char* error_msg,
587 check_range(x.size(),m,error_msg,idx);
606 template <
typename T>
610 const char* error_msg,
612 check_range(x.size(),n,error_msg,idx);
619 template <
typename T,
int R,
int C>
622 rows(
const Eigen::Matrix<T,R,C>& m) {
625 template <
typename T,
int R,
int C>
628 cols(
const Eigen::Matrix<T,R,C>& m) {
632 template <
typename T1,
typename T2>
635 const char* x_name,
const char* y_name,
636 const char* fun_name) {
638 std::stringstream ss;
639 ss <<
"require " << x_name <<
" <= " << y_name
640 <<
" in " << fun_name
641 <<
"; found " << x_name <<
"=" << x
642 <<
", " << y_name <<
"=" << y;
643 throw std::domain_error(ss.str());
645 template <
typename T1,
typename T2>
648 const char* x_name,
const char* y_name,
649 const char* fun_name) {
651 std::stringstream ss;
652 ss <<
"require " << x_name <<
" < " << y_name
653 <<
" in " << fun_name
654 <<
"; found " << x_name <<
"=" << x
655 <<
", " << y_name <<
"=" << y;
656 throw std::domain_error(ss.str());
658 template <
typename T1,
typename T2>
661 const char* x_name,
const char* y_name,
662 const char* fun_name) {
664 std::stringstream ss;
665 ss <<
"require " << x_name <<
" >= " << y_name
666 <<
" in " << fun_name
667 <<
"; found " << x_name <<
"=" << x
668 <<
", " << y_name <<
"=" << y;
669 throw std::domain_error(ss.str());
671 template <
typename T1,
typename T2>
674 const char* x_name,
const char* y_name,
675 const char* fun_name) {
677 std::stringstream ss;
678 ss <<
"require " << x_name <<
" > " << y_name
679 <<
" in " << fun_name
680 <<
"; found " << x_name <<
"=" << x
681 <<
", " << y_name <<
"=" << y;
682 throw std::domain_error(ss.str());
687 template <
typename T,
int R,
int C>
691 if (j > 0 && j <=
static_cast<size_t>(m.cols()))
return;
692 std::stringstream ss;
693 ss <<
"require 0 < column index <= number of columns in" << msg;
694 ss <<
" found cols()=" << m.cols()
695 <<
"; index j=" << j;
696 throw std::domain_error(ss.str());
699 template <
typename T,
int R,
int C>
703 if (i > 0 && i <=
static_cast<size_t>(m.rows()))
return;
704 std::stringstream ss;
705 ss <<
"require 0 < row index <= number of rows in" << msg;
706 ss <<
" found rows()=" << m.rows()
707 <<
"; index i=" << i;
708 throw std::domain_error(ss.str());
711 template <
typename T,
int R,
int C>
714 if (x.rows() == x.cols())
return;
715 std::stringstream ss;
716 ss <<
"error in call to " << msg
717 <<
"; require square matrix, but found"
718 <<
" rows=" << x.rows()
719 <<
"; cols=" << x.cols();
720 throw std::domain_error(ss.str());
723 template <
typename T,
int R,
int C>
728 for (
int i = 0; i < x.rows(); ++i) {
729 for (
int j = 0; j < x.cols(); ++j) {
730 if (x(i,j) != x(j,i)) {
731 std::stringstream ss;
732 ss <<
"error in call to " << msg
733 <<
"; require symmetric matrix, but found"
734 <<
"; x[" << i <<
"," << j <<
"]=" << x(i,j)
735 <<
"; x[" << j <<
"," << i <<
"]=" << x(j,i);
736 throw std::domain_error(ss.str());
742 template <
typename T1,
int R1,
int C1,
typename T2,
int R2,
int C2>
744 const Eigen::Matrix<T2,R2,C2>& x2,
746 if (x1.rows() == x2.rows()
747 && x1.cols() == x2.cols())
return;
748 std::stringstream ss;
749 ss <<
"error in call to " << msg
750 <<
"; require matching dimensions, but found"
751 <<
" arg1 rows=" << x1.rows() <<
" arg1 cols=" << x1.cols()
752 <<
" arg2 rows=" << x2.rows() <<
" arg2 cols=" << x2.cols();
753 throw std::domain_error(ss.str());
756 template <
typename T1,
typename T2>
758 const std::vector<T2>& x2,
760 if (x1.size() == x2.size())
return;
761 std::stringstream ss;
762 ss <<
"require matching sizes in " << msg
763 <<
" found first argument size=" << x1.size()
764 <<
"; second argument size=" << x2.size();
765 throw std::domain_error(ss.str());
768 template <
typename T1,
int R1,
int C1,
typename T2,
int R2,
int C2>
770 const Eigen::Matrix<T2,R2,C2>& x2,
772 if (x1.size() == x2.size())
return;
773 std::stringstream ss;
774 ss <<
"error in call to " << msg
775 <<
"; require matching sizes, but found"
776 <<
" arg1 rows=" << x1.rows() <<
" arg1 cols=" << x1.cols()
777 <<
" arg1 size=" << (x1.rows() * x1.cols())
778 <<
" arg2 rows=" << x2.rows() <<
" arg2 cols=" << x2.cols()
779 <<
" arg2 size=" << (x2.rows() * x2.cols());
780 throw std::domain_error(ss.str());
783 template <
typename T1,
int R1,
int C1,
typename T2,
int R2,
int C2>
785 const Eigen::Matrix<T2,R2,C2>& x2,
787 if (x1.cols() == x2.rows())
return;
788 std::stringstream ss;
789 ss <<
"error in call to " << msg
790 <<
"; require cols of arg1 to match rows of arg2, but found "
791 <<
" arg1 rows=" << x1.rows() <<
" arg1 cols=" << x1.cols()
792 <<
" arg2 rows=" << x2.rows() <<
" arg2 cols=" << x2.cols();
793 throw std::domain_error(ss.str());
796 template <
typename T>
798 if (x.size() > 0)
return;
799 std::stringstream ss;
800 ss <<
"require non-zero size for " << msg
801 <<
"found size=" << x.size();
802 throw std::domain_error(ss.str());
805 template <
typename T,
int R,
int C>
808 if (x.rows() == 1 || x.cols() == 1)
return;
809 std::stringstream ss;
810 ss <<
"error in " << msg
811 <<
"; require vector, found "
812 <<
" rows=" << x.rows() <<
"cols=" << x.cols();
813 throw std::domain_error(ss.str());
829 template <
typename T>
832 determinant(
const Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>& m) {
834 return m.determinant();
845 template <
int R,
int C>
846 inline double dot_self(
const Eigen::Matrix<double, R, C>& v) {
849 for (
int i = 0; i < v.size(); ++i)
860 inline Eigen::Matrix<T,Eigen::Dynamic,1>
862 return x.colwise().squaredNorm();
874 template<
int R1,
int C1,
int R2,
int C2>
875 inline double dot_product(
const Eigen::Matrix<double, R1, C1>& v1,
876 const Eigen::Matrix<double, R2, C2>& v2) {
881 for (
int i = 0; i < v1.size(); ++i)
882 sum += v1[i] * v2[i];
894 for (
size_t i = 0; i <
length; i++)
895 result += v1[i] * v2[i];
905 const std::vector<double>& v2) {
917 inline int min(
const std::vector<int>& x) {
919 throw std::domain_error(
"error: cannot take min of empty int vector");
921 for (
size_t i = 1; i < x.size(); ++i)
934 template <
typename T>
935 inline T
min(
const std::vector<T>& x) {
937 return std::numeric_limits<T>::infinity();
939 for (
size_t i = 1; i < x.size(); ++i)
951 template <
typename T,
int R,
int C>
952 inline T
min(
const Eigen::Matrix<T,R,C>& m) {
954 return std::numeric_limits<double>::infinity();
968 inline int max(
const std::vector<int>& x) {
970 throw std::domain_error(
"error: cannot take max of empty int vector");
972 for (
size_t i = 1; i < x.size(); ++i)
985 template <
typename T>
986 inline T
max(
const std::vector<T>& x) {
988 return -std::numeric_limits<T>::infinity();
990 for (
size_t i = 1; i < x.size(); ++i)
1002 template <
typename T,
int R,
int C>
1003 inline T
max(
const Eigen::Matrix<T,R,C>& m) {
1005 return -std::numeric_limits<double>::infinity();
1006 return m.maxCoeff();
1017 template <
typename T>
1019 typename boost::math::tools::promote_args<T>::type
1023 for (
size_t i = 1; i < v.size(); ++i)
1025 return sum / v.size();
1034 template <
typename T,
int R,
int C>
1036 typename boost::math::tools::promote_args<T>::type
1037 mean(
const Eigen::Matrix<T,R,C>& m) {
1050 template <
typename T>
1052 typename boost::math::tools::promote_args<T>::type
1059 for (
size_t i = 0; i < v.size(); ++i) {
1060 T diff = v[i] - v_mean;
1061 sum_sq_diff += diff * diff;
1063 return sum_sq_diff / (v.size() - 1);
1072 template <
typename T,
int R,
int C>
1074 typename boost::math::tools::promote_args<T>::type
1079 typename boost::math::tools::promote_args<T>::type
1081 typename boost::math::tools::promote_args<T>::type
1083 for (
int i = 0; i < m.size(); ++i) {
1084 typename boost::math::tools::promote_args<T>::type
1086 sum_sq_diff += diff * diff;
1088 return sum_sq_diff / (m.size() - 1);
1097 template <
typename T>
1099 typename boost::math::tools::promote_args<T>::type
1100 sd(
const std::vector<T>& v) {
1102 if (v.size() == 1)
return 0.0;
1112 template <
typename T,
int R,
int C>
1114 typename boost::math::tools::promote_args<T>::type
1115 sd(
const Eigen::Matrix<T,R,C>& m) {
1118 if (m.size() == 1)
return 0.0;
1130 template <
typename T>
1131 inline T
sum(
const std::vector<T>& xs) {
1132 if (xs.size() == 0)
return 0;
1134 for (
size_t i = 1; i < xs.size(); ++i)
1145 template <
typename T,
int R,
int C>
1146 inline double sum(
const Eigen::Matrix<T,R,C>& v) {
1156 template <
typename T>
1157 inline T
prod(
const std::vector<T>& v) {
1158 if (v.size() == 0)
return 1;
1160 for (
size_t i = 1; i < v.size(); ++i)
1171 template <
typename T,
int R,
int C>
1172 inline T
prod(
const Eigen::Matrix<T,R,C>& v) {
1173 if (v.size() == 0)
return 1.0;
1186 template <
typename T>
1187 inline T
trace(
const Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>& m) {
1197 template<
typename T,
int Rows,
int Cols>
1198 inline Eigen::Matrix<T,Rows,Cols>
log(
const Eigen::Matrix<T,Rows,Cols>& m) {
1199 return m.array().log().matrix();
1208 template<
typename T,
int Rows,
int Cols>
1209 inline Eigen::Matrix<T,Rows,Cols>
exp(
const Eigen::Matrix<T,Rows,Cols>& m) {
1210 return m.array().exp().matrix();
1230 template <
typename T1,
typename T2,
int R,
int C>
1232 Eigen::Matrix<typename boost::math::tools::promote_args<T1,T2>::type,R,C>
1233 add(
const Eigen::Matrix<T1,R,C>& m1,
1234 const Eigen::Matrix<T2,R,C>& m2) {
1236 Eigen::Matrix<typename boost::math::tools::promote_args<T1,T2>::type,R,C>
1237 result(m1.rows(),m1.cols());
1238 for (
int i = 0; i < result.size(); ++i)
1239 result(i) = m1(i) + m2(i);
1251 template <
typename T1,
typename T2,
int R,
int C>
1253 Eigen::Matrix<typename boost::math::tools::promote_args<T1,T2>::type,R,C>
1254 add(
const Eigen::Matrix<T1,R,C>& m,
1256 Eigen::Matrix<typename boost::math::tools::promote_args<T1,T2>::type,R,C>
1257 result(m.rows(),m.cols());
1258 for (
int i = 0; i < result.size(); ++i)
1259 result(i) = m(i) + c;
1271 template <
typename T1,
typename T2,
int R,
int C>
1273 Eigen::Matrix<typename boost::math::tools::promote_args<T1,T2>::type,R,C>
1275 const Eigen::Matrix<T2,R,C>& m) {
1276 Eigen::Matrix<typename boost::math::tools::promote_args<T1,T2>::type,R,C>
1277 result(m.rows(),m.cols());
1278 for (
int i = 0; i < result.size(); ++i)
1279 result(i) = c + m(i);
1297 template <
typename T1,
typename T2,
int R,
int C>
1299 Eigen::Matrix<typename boost::math::tools::promote_args<T1,T2>::type, R, C>
1301 const Eigen::Matrix<T2,R,C>& m2) {
1303 Eigen::Matrix<typename boost::math::tools::promote_args<T1,T2>::type, R, C>
1304 result(m1.rows(), m1.cols());
1305 for (
int i = 0; i < result.size(); ++i)
1306 result(i) = m1(i) - m2(i);
1310 template <
typename T1,
typename T2,
int R,
int C>
1312 Eigen::Matrix<typename boost::math::tools::promote_args<T1,T2>::type, R, C>
1314 const Eigen::Matrix<T2,R,C>& m) {
1316 Eigen::Matrix<typename boost::math::tools::promote_args<T1,T2>::type, R, C>
1317 result(m.rows(),m.cols());
1318 for (
int i = 0; i < m.size(); ++i)
1319 result(i) = c - m(i);
1322 template <
typename T1,
typename T2,
int R,
int C>
1324 Eigen::Matrix<typename boost::math::tools::promote_args<T1,T2>::type, R, C>
1328 Eigen::Matrix<typename boost::math::tools::promote_args<T1,T2>::type, R, C>
1329 result(m.rows(),m.cols());
1330 for (
int i = 0; i < m.size(); ++i)
1331 result(i) = m(i) - c;
1344 template <
typename T>
1359 template <
int R,
int C>
1361 Eigen::Matrix<double,R,C>
1379 template <
typename T1,
typename T2,
int R,
int C>
1380 Eigen::Matrix<typename boost::math::tools::promote_args<T1,T2>::type, R, C>
1382 const Eigen::Matrix<T2,R,C>& m2) {
1384 Eigen::Matrix<typename boost::math::tools::promote_args<T1,T2>::type, R, C>
1385 result(m1.rows(),m2.cols());
1386 for (
int i = 0; i < m1.size(); ++i)
1387 result(i) = m1(i) * m2(i);
1391 template <
typename T1,
typename T2,
int R1,
int C1,
int R2,
int C2>
1392 Eigen::Matrix<typename boost::math::tools::promote_args<T1,T2>::type, R2, C2>
1394 const Eigen::Matrix<T2,R2,C2>& m2) {
1395 if (m1.cols() != 1 && m1.rows() != 1)
1396 throw std::domain_error(
"m1 must be a vector");
1397 if (m1.size() != m2.rows())
1398 throw std::domain_error(
"m1 must have same length as m2 has rows");
1399 Eigen::Matrix<typename boost::math::tools::promote_args<T1,T2>::type, R2, C2>
1400 result(m2.rows(),m2.cols());
1401 for (
int i = 0; i < m2.rows(); ++i)
1402 for (
int j = 0; i < m2.cols(); ++j)
1403 result(i,j) = m1(i) * m2(i,j);
1407 template <
typename T1,
typename T2,
int R1,
int C1,
int R2,
int C2>
1408 Eigen::Matrix<typename boost::math::tools::promote_args<T1,T2>::type, R1, C1>
1410 const Eigen::Matrix<T2,R2,C2>& m2) {
1411 if (m2.cols() != 1 && m2.rows() != 1)
1412 throw std::domain_error(
"m2 must be a vector");
1413 if (m2.size() != m1.cols())
1414 throw std::domain_error(
"m2 must have same length as m1 has columns");
1415 Eigen::Matrix<typename boost::math::tools::promote_args<T1,T2>::type, R1, C1>
1416 result(m1.rows(),m1.cols());
1417 for (
int i = 0; i < m1.rows(); ++i)
1418 for (
int j = 0; i < m1.cols(); ++j)
1419 result(i,j) = m2(i) * m1(i,j);
1436 template <
typename T1,
typename T2,
int R,
int C>
1437 Eigen::Matrix<typename boost::math::tools::promote_args<T1,T2>::type, R, C>
1439 const Eigen::Matrix<T2,R,C>& m2) {
1441 Eigen::Matrix<typename boost::math::tools::promote_args<T1,T2>::type, R, C>
1442 result(m1.rows(),m2.cols());
1443 for (
int i = 0; i < m1.size(); ++i)
1444 result(i) = m1(i) / m2(i);
1457 template <
int R,
int C>
1459 Eigen::Matrix<double,R,C>
1472 template <
int R,
int C>
1474 Eigen::Matrix<double,R,C>
1476 const Eigen::Matrix<double,R,C>& m) {
1491 template<
int R1,
int C1,
int R2,
int C2>
1492 inline Eigen::Matrix<double,R1,C2>
multiply(
const Eigen::Matrix<double,R1,C1>& m1,
1493 const Eigen::Matrix<double,R2,C2>& m2) {
1507 template<
int C1,
int R2>
1508 inline double multiply(
const Eigen::Matrix<double,1,C1>& rv,
1509 const Eigen::Matrix<double,R2,1>& v) {
1511 if (rv.size() != v.size())
1512 throw std::domain_error(
"rv.size() != v.size()");
1528 if (L.rows() == 1) {
1530 result(0,0) = L(0,0) * L(0,0);
1535 matrix_d L_tri = L.transpose().triangularView<Eigen::Upper>();
1536 return L.triangularView<Eigen::Lower>() * L_tri;
1550 return M * M.transpose();
1551 matrix_d result(M.rows(),M.rows());
1554 .selfadjointView<Eigen::Upper>()
1581 template <
typename T>
1583 Eigen::Matrix<T,1,Eigen::Dynamic>
1584 row(
const Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>& m,
1587 return m.row(i - 1);
1601 template <
typename T>
1603 Eigen::Matrix<T,Eigen::Dynamic,1>
1604 col(
const Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>& m,
1607 return m.col(j - 1);
1619 template <
typename T>
1621 Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>
1622 block(
const Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>& m,
1623 size_t i,
size_t j,
size_t nrows,
size_t ncols) {
1628 return m.block(i - 1,j - 1,nrows,ncols);
1638 template <
typename T>
1640 Eigen::Matrix<T,Eigen::Dynamic,1>
1641 diagonal(
const Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>& m) {
1642 return m.diagonal();
1651 template <
typename T>
1653 Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>
1655 return v.asDiagonal();
1658 template <
typename T,
int R,
int C>
1659 Eigen::Matrix<T,C,R>
1662 return m.transpose();
1670 template <
typename T>
1672 Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>
1673 inverse(
const Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>& m) {
1685 template <
typename T>
1687 Eigen::Matrix<T,Eigen::Dynamic,1>
1688 softmax(
const Eigen::Matrix<T,Eigen::Dynamic,1>& v) {
1691 Eigen::Matrix<T,Eigen::Dynamic,1> theta(v.size());
1693 T max_v = v.maxCoeff();
1694 for (
int i = 0; i < v.size(); ++i) {
1695 theta[i] =
exp(v[i] - max_v);
1698 for (
int i = 0; i < v.size(); ++i)
1704 template <
typename T1,
typename T2,
int R1,
int C1,
int R2,
int C2>
1706 Eigen::Matrix<typename boost::math::tools::promote_args<T1,T2>::type,
1709 const Eigen::Matrix<T2,R2,C2> &b) {
1712 return promote_common<Eigen::Matrix<T1,R1,C1>,
1713 Eigen::Matrix<T2,R1,C1> >(A)
1714 .
template triangularView<Eigen::Lower>()
1716 Eigen::Matrix<T2,R2,C2> >(b) );
1718 template <
typename T>
1720 Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>
1722 Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> &A) {
1725 Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> b;
1727 A.template triangularView<Eigen::Lower>().solveInPlace(b);
1741 template <
int TriView,
typename T1,
typename T2,
1742 int R1,
int C1,
int R2,
int C2>
1744 Eigen::Matrix<typename boost::math::tools::promote_args<T1,T2>::type,
1747 const Eigen::Matrix<T2,R2,C2> &b) {
1750 return promote_common<Eigen::Matrix<T1,R1,C1>,Eigen::Matrix<T2,R1,C1> >(A)
1751 .
template triangularView<TriView>()
1753 Eigen::Matrix<T2,R2,C2> >(b) );
1762 template<
int TriView,
typename T>
1764 Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>
1768 Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> b;
1770 A.template triangularView<TriView>().solveInPlace(b);
1782 template <
typename T1,
typename T2,
int R1,
int C1,
int R2,
int C2>
1784 Eigen::Matrix<typename boost::math::tools::promote_args<T1,T2>::type,R1,C2>
1786 const Eigen::Matrix<T2,R2,C2> &b) {
1789 return promote_common<Eigen::Matrix<T1,R1,C1>,
1790 Eigen::Matrix<T2,R1,C1> >(A)
1793 Eigen::Matrix<T2,R2,C2> >(b) );
1805 template <
int TriView,
typename T1,
typename T2,
1806 int R1,
int C1,
int R2,
int C2>
1808 Eigen::Matrix<typename boost::math::tools::promote_args<T1,T2>::type,R1,C2>
1810 const Eigen::Matrix<T2,R2,C2> &A) {
1813 return promote_common<Eigen::Matrix<T1,R1,C1>,
1814 Eigen::Matrix<T2,R1,C1> >(A)
1815 .
template triangularView<TriView>()
1818 Eigen::Matrix<T2,R2,C2> >(b)
1831 template <
typename T1,
typename T2,
int R1,
int C1,
int R2,
int C2>
1833 Eigen::Matrix<typename boost::math::tools::promote_args<T1,T2>::type,R1,C2>
1835 const Eigen::Matrix<T2,R2,C2> &A) {
1836 return mdivide_right_tri<Eigen::Lower>
1838 Eigen::Matrix<T2,R1,C1> >(b),
1840 Eigen::Matrix<T2,R2,C2> >(A));
1853 template <
typename T1,
typename T2,
int R1,
int C1,
int R2,
int C2>
1855 Eigen::Matrix<typename boost::math::tools::promote_args<T1,T2>::type,R1,C2>
1857 const Eigen::Matrix<T2,R2,C2> &A) {
1860 return promote_common<Eigen::Matrix<T1,R2,C2>,
1861 Eigen::Matrix<T2,R2,C2> >(A)
1865 Eigen::Matrix<T2,R1,C1> >(b)
1879 template <
typename T>
1880 Eigen::Matrix<T,Eigen::Dynamic,1>
1884 Eigen::SelfAdjointEigenSolver<Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> >
1885 solver(m,Eigen::EigenvaluesOnly);
1886 return solver.eigenvalues();
1889 template <
typename T>
1890 Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>
1894 Eigen::SelfAdjointEigenSolver<Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> >
1896 return solver.eigenvectors();
1912 template <
typename T>
1913 Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>
1916 Eigen::LLT<Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> >llt(m.rows());
1918 return llt.matrixL();
1929 template <
typename T>
1930 Eigen::Matrix<T,Eigen::Dynamic,1>
1932 return Eigen::JacobiSVD<Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> >(m)
1965 template <
typename T>
1968 cumulative_sum(
const std::vector<T>& x) {
1969 std::vector<T> result(x.size());
1973 for (
size_t i = 1; i < result.size(); ++i)
1974 result[i] = x[i] + result[i-1];
1991 template <
typename T,
int R,
int C>
1993 Eigen::Matrix<T,R,C>
1994 cumulative_sum(
const Eigen::Matrix<T,R,C>& m) {
1995 Eigen::Matrix<T,R,C> result(m.rows(),m.cols());
1999 for (
int i = 1; i < result.size(); ++i)
2000 result(i) = m(i) + result(i-1);
2007 template <
typename T>
2008 void stan_print(std::ostream* o,
const T& x) {
2012 template <
typename T>
2013 void stan_print(std::ostream* o,
const std::vector<T>& x) {
2015 for (
int i = 0; i < x.size(); ++i) {
2016 if (i > 0) *o <<
',';
2022 template <
typename T>
2023 void stan_print(std::ostream* o,
const Eigen::Matrix<T,Eigen::Dynamic,1>& x) {
2025 for (
int i = 0; i < x.size(); ++i) {
2026 if (i > 0) *o <<
',';
2032 template <
typename T>
2033 void stan_print(std::ostream* o,
const Eigen::Matrix<T,1,Eigen::Dynamic>& x) {
2035 for (
int i = 0; i < x.size(); ++i) {
2036 if (i > 0) *o <<
',';
2042 template <
typename T>
2044 const Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>& x) {
2046 for (
int i = 0; i < x.rows(); ++i) {
2047 if (i > 0) *o <<
',';
(Expert) Numerical traits for algorithmic differentiation variables.
void stan_print(std::ostream *o, const var &x)
var sqrt(const var &a)
Return the square root of the specified variable (cmath).
Eigen::Matrix< typename boost::math::tools::promote_args< T1, T2 >::type, R1, C1 > diag_post_multiply(const Eigen::Matrix< T1, R1, C1 > &m1, const Eigen::Matrix< T2, R2, C2 > &m2)
void validate_column_index(const Eigen::Matrix< T, R, C > &m, size_t j, const char *msg)
Eigen::Matrix< typename boost::math::tools::promote_args< T1, T2 >::type, R1, C2 > mdivide_right_tri_low(const Eigen::Matrix< T1, R1, C1 > &b, const Eigen::Matrix< T2, R2, C2 > &A)
Returns the solution of the system tri(A)x=b when tri(A) is a lower triangular view of the matrix A.
Eigen::Matrix< double, Eigen::Dynamic, 1 > vector_d
Type for (column) vector of double values.
void validate_greater_or_equal(const T1 &x, const T2 &y, const char *x_name, const char *y_name, const char *fun_name)
T prod(const std::vector< T > &v)
Returns the product of the coefficients of the specified standard vector.
matrix_d tcrossprod(const matrix_d &M)
Returns the result of post-multiplying a matrix by its own transpose.
Eigen::Matrix< typename boost::math::tools::promote_args< T1, T2 >::type, R1, C2 > mdivide_left_tri_low(const Eigen::Matrix< T1, R1, C1 > &A, const Eigen::Matrix< T2, R2, C2 > &b)
T sum(const std::vector< T > &xs)
Return the sum of the values in the specified standard vector.
Eigen::Matrix< T, Eigen::Dynamic, 1 > singular_values(const Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &m)
Return the vector of the singular values of the specified matrix in decreasing order of magnitude.
Eigen::Matrix< T, Eigen::Dynamic, 1 > columns_dot_self(const Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &x)
Returns the dot product of each column of a matrix with itself.
void validate_multiplicable(const Eigen::Matrix< T1, R1, C1 > &x1, const Eigen::Matrix< T2, R2, C2 > &x2, const char *msg)
boost::math::tools::promote_args< T >::type mean(const std::vector< T > &v)
Returns the sample mean (i.e., average) of the coefficients in the specified standard vector.
boost::math::tools::promote_args< T >::type sd(const std::vector< T > &v)
Returns the unbiased sample standard deviation of the coefficients in the specified column vector.
matrix_d multiply_lower_tri_self_transpose(const matrix_d &L)
Returns the result of multiplying the lower triangular portion of the input matrix by its own transpo...
Eigen::Matrix< typename boost::math::tools::promote_args< T1, T2 >::type, R, C > elt_divide(const Eigen::Matrix< T1, R, C > &m1, const Eigen::Matrix< T2, R, C > &m2)
Return the elementwise division of the specified matrices matrices.
Eigen::Matrix< typename boost::math::tools::promote_args< T1, T2 >::type, R, C > add(const Eigen::Matrix< T1, R, C > &m1, const Eigen::Matrix< T2, R, C > &m2)
Return the sum of the specified matrices.
void validate_symmetric(const Eigen::Matrix< T, R, C > &x, const char *msg)
int min(const std::vector< int > &x)
Returns the minimum coefficient in the specified column vector.
void validate_less_or_equal(const T1 &x, const T2 &y, const char *x_name, const char *y_name, const char *fun_name)
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > inverse(const Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &m)
Returns the inverse of the specified matrix.
int max(const std::vector< int > &x)
Returns the maximum coefficient in the specified column vector.
Eigen::Matrix< typename boost::math::tools::promote_args< T1, T2 >::type, R1, C2 > mdivide_right(const Eigen::Matrix< T1, R1, C1 > &b, const Eigen::Matrix< T2, R2, C2 > &A)
Returns the solution of the system Ax=b.
Eigen::Matrix< typename boost::math::tools::promote_args< T1, T2 >::type, R2, C2 > diag_pre_multiply(const Eigen::Matrix< T1, R1, C1 > &m1, const Eigen::Matrix< T2, R2, C2 > &m2)
matrix_d crossprod(const matrix_d &M)
Returns the result of pre-multiplying a matrix by its own transpose.
void validate_matching_dims(const Eigen::Matrix< T1, R1, C1 > &x1, const Eigen::Matrix< T2, R2, C2 > &x2, const char *msg)
T trace(const Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &m)
Returns the trace of the specified matrix.
Eigen::Matrix< T, Eigen::Dynamic, 1 > softmax(const Eigen::Matrix< T, Eigen::Dynamic, 1 > &v)
Return the softmax of the specified vector.
Eigen::Matrix< typename boost::math::tools::promote_args< T1, T2 >::type, R1, C2 > mdivide_left(const Eigen::Matrix< T1, R1, C1 > &A, const Eigen::Matrix< T2, R2, C2 > &b)
Returns the solution of the system Ax=b.
void validate_square(const Eigen::Matrix< T, R, C > &x, const char *msg)
void validate_less(const T1 &x, const T2 &y, const char *x_name, const char *y_name, const char *fun_name)
size_t cols(const Eigen::Matrix< T, R, C > &m)
Eigen::Matrix< T, Eigen::Dynamic, 1 > diagonal(const Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &m)
Return a column vector of the diagonal elements of the specified matrix.
T minus(const T &x)
Returns the negation of the specified scalar or matrix.
void validate_vector(const Eigen::Matrix< T, R, C > &x, const char *msg)
void validate_row_index(const Eigen::Matrix< T, R, C > &m, size_t i, const char *msg)
void resize(T &x, std::vector< size_t > dims)
Recursively resize the specified vector of vectors, which must bottom out at scalar values,...
Eigen::Matrix< typename boost::math::tools::promote_args< T1, T2 >::type, R, C > subtract(const Eigen::Matrix< T1, R, C > &m1, const Eigen::Matrix< T2, R, C > &m2)
Return the result of subtracting the second specified matrix from the first specified matrix.
common_type< T1, T2 >::type promote_common(const F &u)
void validate_greater(const T1 &x, const T2 &y, const char *x_name, const char *y_name, const char *fun_name)
Eigen::Matrix< typename boost::math::tools::promote_args< T1, T2 >::type, R, C > elt_multiply(const Eigen::Matrix< T1, R, C > &m1, const Eigen::Matrix< T2, R, C > &m2)
Return the elementwise multiplication of the specified matrices.
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > eigenvectors_sym(const Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &m)
Eigen::Matrix< T, C, R > transpose(const Eigen::Matrix< T, R, C > &m)
T & get_base1(std::vector< T > &x, size_t i, const char *error_msg, size_t idx)
Return a reference to the value of the specified vector at the specified base-one index.
Eigen::Matrix< T, Eigen::Dynamic, 1 > eigenvalues_sym(const Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &m)
Return the eigenvalues of the specified symmetric matrix in descending order of magnitude.
boost::math::tools::promote_args< T >::type variance(const std::vector< T > &v)
Returns the sample variance (divide by length - 1) of the coefficients in the specified standard vect...
void validate_nonzero_size(const T &x, const char *msg)
Eigen::Matrix< T, 1, Eigen::Dynamic > row(const Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &m, size_t i)
Return the specified row of the specified matrix, using start-at-1 indexing.
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > cholesky_decompose(const Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &m)
Return the lower-triangular Cholesky factor (i.e., matrix square root) of the specified square,...
Eigen::Matrix< double, 1, Eigen::Dynamic > row_vector_d
Type for (row) vector of double values.
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > matrix_d
Type for matrix of double values.
Eigen::Matrix< T, Rows, Cols > exp(const Eigen::Matrix< T, Rows, Cols > &m)
Return the element-wise exponentiation of the matrix or vector.
double dot_self(const Eigen::Matrix< double, R, C > &v)
Returns the dot product of the specified vector with itself.
Eigen::Matrix< typename boost::math::tools::promote_args< T1, T2 >::type, R1, C2 > mdivide_left_tri(const Eigen::Matrix< T1, R1, C1 > &A, const Eigen::Matrix< T2, R2, C2 > &b)
Returns the solution of the system Ax=b when A is triangular.
void validate_matching_sizes(const std::vector< T1 > &x1, const std::vector< T2 > &x2, const char *msg)
size_t rows(const Eigen::Matrix< T, R, C > &m)
Eigen::Matrix< typename boost::math::tools::promote_args< T1, T2 >::type, R1, C2 > mdivide_right_tri(const Eigen::Matrix< T1, R1, C1 > &b, const Eigen::Matrix< T2, R2, C2 > &A)
Returns the solution of the system Ax=b when A is triangular.
T determinant(const Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &m)
Returns the determinant of the specified square matrix.
Eigen::Matrix< T, Rows, Cols > log(const Eigen::Matrix< T, Rows, Cols > &m)
Return the element-wise logarithm of the matrix or vector.
Eigen::Matrix< T, Eigen::Dynamic, 1 > col(const Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &m, size_t j)
Return the specified column of the specified matrix using start-at-1 indexing.
Eigen::Matrix< double, R, C > multiply(const Eigen::Matrix< double, R, C > &m, double c)
Return specified matrix multiplied by specified scalar.
Eigen::Matrix< double, R, C > divide(const Eigen::Matrix< double, R, C > &m, double c)
Return specified matrix divided by specified scalar.
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > block(const Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &m, size_t i, size_t j, size_t nrows, size_t ncols)
Return a nrows x ncols submatrix starting at (i,j).
double dot_product(const Eigen::Matrix< double, R1, C1 > &v1, const Eigen::Matrix< double, R2, C2 > &v2)
Returns the dot product of the specified vectors.
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > diag_matrix(const Eigen::Matrix< T, Eigen::Dynamic, 1 > &v)
Return a square diagonal matrix with the specified vector of coefficients as the diagonal values.
Probability, optimization and sampling library.
size_t length(const T &x)
Template specification of functions in std for Stan.
Structure for building up arrays in an expression (rather than in statements) using an argumentchaini...
array_builder & add(const F &u)
Eigen::Matrix< typename common_type< T1, T2 >::type, R, C > type
std::vector< typename common_type< T1, T2 >::type > type
boost::math::tools::promote_args< T1, T2 >::type type