Stan  1.0
probability, sampling & optimization
matrix.hpp
Go to the documentation of this file.
1 #ifndef __STAN__MATH__MATRIX_HPP__
2 #define __STAN__MATH__MATRIX_HPP__
3 
4 #include <stdarg.h>
5 #include <stdexcept>
6 #include <ostream>
7 #include <vector>
8 
9 #include <boost/math/tools/promotion.hpp>
10 
11 #define EIGEN_DENSEBASE_PLUGIN "stan/math/EigenDenseBaseAddons.hpp"
12 #include <Eigen/Dense>
13 
15 
16 namespace stan {
17 
18  namespace math {
19 
20  // from input type F to output type T
21 
22  // scalar, F != T (base template)
23  template <typename F, typename T>
24  struct promoter {
25  inline static void promote(const F& u, T& t) {
26  t = u;
27  }
28  inline static T promote_to(const F& u) {
29  return u;
30  }
31  };
32  // scalar, F == T
33  template <typename T>
34  struct promoter<T,T> {
35  inline static void promote(const T& u, T& t) {
36  t = u;
37  }
38  inline static T promote_to(const T& u) {
39  return u;
40  }
41  };
42 
43  // std::vector, F != T
44  template <typename F, typename T>
45  struct promoter<std::vector<F>, std::vector<T> > {
46  inline static void promote(const std::vector<F>& u,
47  std::vector<T>& t) {
48  t.resize(u.size());
49  for (size_t i = 0; i < u.size(); ++i)
50  promoter<F,T>::promote(u[i],t[i]);
51  }
52  inline static std::vector<T>
53  promote_to(const std::vector<F>& u) {
54  std::vector<T> t;
55  promoter<std::vector<F>,std::vector<T> >::promote(u,t);
56  return t;
57  }
58  };
59  // std::vector, F == T
60  template <typename T>
61  struct promoter<std::vector<T>, std::vector<T> > {
62  inline static void promote(const std::vector<T>& u,
63  std::vector<T>& t) {
64  t = u;
65  }
66  inline static std::vector<T> promote_to(const std::vector<T>& u) {
67  return u;
68  }
69  };
70 
71  // Eigen::Matrix, F != T
72  template <typename F, typename T, int R, int C>
73  struct promoter<Eigen::Matrix<F,R,C>, Eigen::Matrix<T,R,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)
78  promoter<F,T>::promote(u(i),t(i));
79  }
80  inline static Eigen::Matrix<T,R,C>
81  promote_to(const Eigen::Matrix<F,R,C>& u) {
82  Eigen::Matrix<T,R,C> t;
83  promoter<Eigen::Matrix<F,R,C>,Eigen::Matrix<T,R,C> >::promote(u,t);
84  return t;
85  }
86  };
87  // Eigen::Matrix, F == T
88  template <typename T, int R, int C>
89  struct promoter<Eigen::Matrix<T,R,C>, Eigen::Matrix<T,R,C> > {
90  inline static void promote(const Eigen::Matrix<T,R,C>& u,
91  Eigen::Matrix<T,R,C>& t) {
92  t = u;
93  }
94  inline static Eigen::Matrix<T,R,C> promote_to(const Eigen::Matrix<T,R,C>& u) {
95  return u;
96  }
97  };
98 
99  template <typename T1, typename T2>
100  struct common_type {
101  typedef typename boost::math::tools::promote_args<T1,T2>::type type;
102  };
103 
104  template <typename T1, typename T2>
105  struct common_type<std::vector<T1>, std::vector<T2> > {
106  typedef std::vector<typename common_type<T1,T2>::type> type;
107  };
108 
109  template <typename T1, typename T2, int R, int C>
110  struct common_type<Eigen::Matrix<T1,R,C>, Eigen::Matrix<T2,R,C> > {
111  typedef Eigen::Matrix<typename common_type<T1,T2>::type,R,C> type;
112  };
113 
114  template <typename T1, typename T2, typename F>
115  inline
116  typename common_type<T1,T2>::type
117  promote_common(const F& u) {
119  ::promote_to(u);
120  }
121 
122 
123 
124 
130  template <typename T>
131  struct array_builder {
132  std::vector<T> x_;
133  array_builder() : x_() { }
134  template <typename F>
135  array_builder& add(const F& u) {
136  T t;
138  x_.push_back(t);
139  return *this;
140  }
141  std::vector<T> array() {
142  return x_;
143  }
144  };
145 
149  typedef
150  Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic>
152 
156  typedef
157  Eigen::Matrix<double,Eigen::Dynamic,1>
159 
163  typedef
164  Eigen::Matrix<double,1,Eigen::Dynamic>
166 
167  namespace {
168 
169  template <typename T>
170  void resize(Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>& x,
171  const std::vector<size_t>& dims,
172  size_t pos) {
173  x.resize(dims[pos],dims[pos+1]);
174  }
175 
176  template <typename T>
177  void resize(Eigen::Matrix<T,Eigen::Dynamic,1>& x,
178  const std::vector<size_t>& dims,
179  size_t pos) {
180  x.resize(dims[pos]);
181  }
182 
183  template <typename T>
184  void resize(Eigen::Matrix<T,1,Eigen::Dynamic>& x,
185  const std::vector<size_t>& dims,
186  size_t pos) {
187  x.resize(dims[pos]);
188  }
189 
190 
191  void resize(double x,
192  const std::vector<size_t>& dims,
193  size_t pos) {
194  // no-op
195  }
196 
197  template <typename T>
198  void resize(std::vector<T>& x,
199  const std::vector<size_t>& dims,
200  size_t pos) {
201  x.resize(dims[pos]);
202  ++pos;
203  if (pos >= dims.size()) return; // skips lowest loop to scalar
204  for (size_t i = 0; i < x.size(); ++i)
205  resize(x[i],dims,pos);
206  }
207 
208  }
209 
219  template <typename T>
220  inline void resize(T& x, std::vector<size_t> dims) {
221  resize(x,dims,0U);
222  }
223 
224  // polymorphic gets with bounds checking
225 
226 
227  namespace {
228 
229  void raise_range_error(size_t max,
230  size_t i,
231  const char* msg,
232  size_t idx) {
233  std::stringstream s;
234  s << "INDEX OPERATOR [] OUT OF BOUNDS"
235  << "; index=" << i
236  << "; lower bound=1"
237  << "; upper bound=" << max
238  << "; index position=" << idx
239  << "; " << msg
240  << std::endl;
241  throw std::out_of_range(s.str());
242  }
243 
244  inline
245  void check_range(size_t max,
246  size_t i,
247  const char* msg,
248  size_t idx) {
249 #ifndef NDEBUG
250  if (i < 1 || i > max)
251  raise_range_error(max,i,msg,idx);
252 #endif
253  }
254 
255  }
256 
271  template <typename T>
272  inline
273  T& get_base1(std::vector<T>& x,
274  size_t i,
275  const char* error_msg,
276  size_t idx) {
277  check_range(x.size(),i,error_msg,idx);
278  return x[i - 1];
279  }
280 
296  template <typename T>
297  inline
298  T& get_base1(std::vector<std::vector<T> >& x,
299  size_t i1,
300  size_t i2,
301  const char* error_msg,
302  size_t idx) {
303  check_range(x.size(),i1,error_msg,idx);
304  return get_base1(x[i1 - 1],i2,error_msg,idx+1);
305  }
306 
323  template <typename T>
324  inline
325  T& get_base1(std::vector<std::vector<std::vector<T> > >& x,
326  size_t i1,
327  size_t i2,
328  size_t i3,
329  const char* error_msg,
330  size_t idx) {
331  check_range(x.size(),i1,error_msg,idx);
332  return get_base1(x[i1 - 1],i2,i3,error_msg,idx+1);
333  }
334 
352  template <typename T>
353  inline
354  T& get_base1(std::vector<std::vector<std::vector<std::vector<T> > > >& x,
355  size_t i1,
356  size_t i2,
357  size_t i3,
358  size_t i4,
359  const char* error_msg,
360  size_t idx) {
361  check_range(x.size(),i1,error_msg,idx);
362  return get_base1(x[i1 - 1],i2,i3,i4,error_msg,idx+1);
363  }
364 
383  template <typename T>
384  inline
385  T& get_base1(std::vector<std::vector<std::vector<std::vector<std::vector<T> > > > >& x,
386  size_t i1,
387  size_t i2,
388  size_t i3,
389  size_t i4,
390  size_t i5,
391  const char* error_msg,
392  size_t idx) {
393  check_range(x.size(),i1,error_msg,idx);
394  return get_base1(x[i1 - 1],i2,i3,i4,i5,error_msg,idx+1);
395  }
396 
416  template <typename T>
417  inline
418  T& get_base1(std::vector<std::vector<std::vector<std::vector<std::vector<std::vector<T> > > > > >& x,
419  size_t i1,
420  size_t i2,
421  size_t i3,
422  size_t i4,
423  size_t i5,
424  size_t i6,
425  const char* error_msg,
426  size_t idx) {
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);
429  }
430 
431 
452  template <typename T>
453  inline
454  T& get_base1(std::vector<std::vector<std::vector<std::vector<std::vector<std::vector<std::vector<T> > > > > > >& x,
455  size_t i1,
456  size_t i2,
457  size_t i3,
458  size_t i4,
459  size_t i5,
460  size_t i6,
461  size_t i7,
462  const char* error_msg,
463  size_t idx) {
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);
466  }
467 
468 
490  template <typename T>
491  inline
492  T& get_base1(std::vector<std::vector<std::vector<std::vector<std::vector<std::vector<std::vector<std::vector<T> > > > > > > >& x,
493  size_t i1,
494  size_t i2,
495  size_t i3,
496  size_t i4,
497  size_t i5,
498  size_t i6,
499  size_t i7,
500  size_t i8,
501  const char* error_msg,
502  size_t idx) {
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);
505  }
506 
507 
508 
528  template <typename T>
529  inline
530  Eigen::Matrix<T,1,Eigen::Dynamic>
531  get_base1(Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>& x,
532  size_t m,
533  const char* error_msg,
534  size_t idx) {
535  check_range(x.rows(),m,error_msg,idx);
536  return x.row(m - 1);
537  }
538 
555  template <typename T>
556  inline
557  T& get_base1(Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>& x,
558  size_t m,
559  size_t n,
560  const char* error_msg,
561  size_t idx) {
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);
565  }
566 
581  template <typename T>
582  inline
583  T& get_base1(Eigen::Matrix<T,Eigen::Dynamic,1>& x,
584  size_t m,
585  const char* error_msg,
586  size_t idx) {
587  check_range(x.size(),m,error_msg,idx);
588  return x(m - 1);
589 
590  }
591 
606  template <typename T>
607  inline
608  T& get_base1(Eigen::Matrix<T,1,Eigen::Dynamic>& x,
609  size_t n,
610  const char* error_msg,
611  size_t idx) {
612  check_range(x.size(),n,error_msg,idx);
613  return x(n - 1);
614  }
615 
616 
617  // int returns
618 
619  template <typename T, int R, int C>
620  inline
621  size_t
622  rows(const Eigen::Matrix<T,R,C>& m) {
623  return m.rows();
624  }
625  template <typename T, int R, int C>
626  inline
627  size_t
628  cols(const Eigen::Matrix<T,R,C>& m) {
629  return m.cols();
630  }
631 
632  template <typename T1, typename T2>
633  inline
634  void validate_less_or_equal(const T1& x, const T2& y,
635  const char* x_name, const char* y_name,
636  const char* fun_name) {
637  if (x <= y) return;
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());
644  }
645  template <typename T1, typename T2>
646  inline
647  void validate_less(const T1& x, const T2& y,
648  const char* x_name, const char* y_name,
649  const char* fun_name) {
650  if (x < y) return;
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());
657  }
658  template <typename T1, typename T2>
659  inline
660  void validate_greater_or_equal(const T1& x, const T2& y,
661  const char* x_name, const char* y_name,
662  const char* fun_name) {
663  if (x >= y) return;
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());
670  }
671  template <typename T1, typename T2>
672  inline
673  void validate_greater(const T1& x, const T2& y,
674  const char* x_name, const char* y_name,
675  const char* fun_name) {
676  if (x > y) return;
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());
683  }
684 
685 
686 
687  template <typename T, int R, int C>
688  void validate_column_index(const Eigen::Matrix<T,R,C>& m,
689  size_t j,
690  const char* msg) {
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());
697  }
698 
699  template <typename T, int R, int C>
700  void validate_row_index(const Eigen::Matrix<T,R,C>& m,
701  size_t i,
702  const char* msg) {
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());
709  }
710 
711  template <typename T, int R, int C>
712  void validate_square(const Eigen::Matrix<T,R,C>& x,
713  const char* msg) {
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());
721  }
722 
723  template <typename T, int R, int C>
724  void validate_symmetric(const Eigen::Matrix<T,R,C>& x,
725  const char* msg) {
726  // tolerance = 1E-8
727  validate_square(x,msg);
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());
737  }
738  }
739  }
740  }
741 
742  template <typename T1, int R1, int C1, typename T2, int R2, int C2>
743  inline void validate_matching_dims(const Eigen::Matrix<T1,R1,C1>& x1,
744  const Eigen::Matrix<T2,R2,C2>& x2,
745  const char* msg) {
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());
754  }
755 
756  template <typename T1, typename T2>
757  inline void validate_matching_sizes(const std::vector<T1>& x1,
758  const std::vector<T2>& x2,
759  const char* msg) {
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());
766  }
767 
768  template <typename T1, int R1, int C1, typename T2, int R2, int C2>
769  inline void validate_matching_sizes(const Eigen::Matrix<T1,R1,C1>& x1,
770  const Eigen::Matrix<T2,R2,C2>& x2,
771  const char* msg) {
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());
781  }
782 
783  template <typename T1, int R1, int C1, typename T2, int R2, int C2>
784  inline void validate_multiplicable(const Eigen::Matrix<T1,R1,C1>& x1,
785  const Eigen::Matrix<T2,R2,C2>& x2,
786  const char* msg) {
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());
794  }
795 
796  template <typename T>
797  inline void validate_nonzero_size(const T& x, const char* msg) {
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());
803  }
804 
805  template <typename T, int R, int C>
806  inline void validate_vector(const Eigen::Matrix<T,R,C>& x,
807  const char* msg) {
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());
814  }
815 
816 
817 
818 
819 
820  // scalar returns
821 
829  template <typename T>
830  inline
831  T
832  determinant(const Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>& m) {
833  stan::math::validate_square(m,"determinant");
834  return m.determinant();
835  }
836 
837 
845  template <int R, int C>
846  inline double dot_self(const Eigen::Matrix<double, R, C>& v) {
847  validate_vector(v,"dot_self");
848  double sum = 0.0;
849  for (int i = 0; i < v.size(); ++i)
850  sum += v(i) * v(i);
851  return sum;
852  }
853 
859  template<typename T>
860  inline Eigen::Matrix<T,Eigen::Dynamic,1>
861  columns_dot_self(const Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>& x) {
862  return x.colwise().squaredNorm();
863  }
864 
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) {
877  validate_vector(v1,"dot_product");
878  validate_vector(v2,"dot_product");
879  validate_matching_sizes(v1,v2,"dot_product");
880  double sum = 0.0;
881  for (int i = 0; i < v1.size(); ++i)
882  sum += v1[i] * v2[i];
883  return sum;
884  }
891  inline double dot_product(const double* v1, const double* v2,
892  size_t length) {
893  double result = 0;
894  for (size_t i = 0; i < length; i++)
895  result += v1[i] * v2[i];
896  return result;
897  }
904  inline double dot_product(const std::vector<double>& v1,
905  const std::vector<double>& v2) {
906  validate_matching_sizes(v1,v2,"dot_product");
907  return dot_product(&v1[0], &v2[0], v1.size());
908  }
909 
917  inline int min(const std::vector<int>& x) {
918  if (x.size() == 0)
919  throw std::domain_error("error: cannot take min of empty int vector");
920  int min = x[0];
921  for (size_t i = 1; i < x.size(); ++i)
922  if (x[i] < min)
923  min = x[i];
924  return min;
925  }
926 
934  template <typename T>
935  inline T min(const std::vector<T>& x) {
936  if (x.size() == 0)
937  return std::numeric_limits<T>::infinity();
938  T min = x[0];
939  for (size_t i = 1; i < x.size(); ++i)
940  if (x[i] < min)
941  min = x[i];
942  return min;
943  }
944 
951  template <typename T, int R, int C>
952  inline T min(const Eigen::Matrix<T,R,C>& m) {
953  if (m.size() == 0)
954  return std::numeric_limits<double>::infinity();
955  return m.minCoeff();
956  }
957 
958 
959 
968  inline int max(const std::vector<int>& x) {
969  if (x.size() == 0)
970  throw std::domain_error("error: cannot take max of empty int vector");
971  int max = x[0];
972  for (size_t i = 1; i < x.size(); ++i)
973  if (x[i] > max)
974  max = x[i];
975  return max;
976  }
977 
985  template <typename T>
986  inline T max(const std::vector<T>& x) {
987  if (x.size() == 0)
988  return -std::numeric_limits<T>::infinity();
989  T max = x[0];
990  for (size_t i = 1; i < x.size(); ++i)
991  if (x[i] > max)
992  max = x[i];
993  return max;
994  }
995 
1002  template <typename T, int R, int C>
1003  inline T max(const Eigen::Matrix<T,R,C>& m) {
1004  if (m.size() == 0)
1005  return -std::numeric_limits<double>::infinity();
1006  return m.maxCoeff();
1007  }
1008 
1017  template <typename T>
1018  inline
1019  typename boost::math::tools::promote_args<T>::type
1020  mean(const std::vector<T>& v) {
1021  validate_nonzero_size(v,"mean");
1022  T sum(v[0]);
1023  for (size_t i = 1; i < v.size(); ++i)
1024  sum += v[i];
1025  return sum / v.size();
1026  }
1027 
1034  template <typename T, int R, int C>
1035  inline
1036  typename boost::math::tools::promote_args<T>::type
1037  mean(const Eigen::Matrix<T,R,C>& m) {
1038  validate_nonzero_size(m,"mean");
1039  return m.mean();
1040  }
1041 
1050  template <typename T>
1051  inline
1052  typename boost::math::tools::promote_args<T>::type
1053  variance(const std::vector<T>& v) {
1054  validate_nonzero_size(v,"variance");
1055  if (v.size() == 1)
1056  return 0.0;
1057  T v_mean(mean(v));
1058  T sum_sq_diff(0);
1059  for (size_t i = 0; i < v.size(); ++i) {
1060  T diff = v[i] - v_mean;
1061  sum_sq_diff += diff * diff;
1062  }
1063  return sum_sq_diff / (v.size() - 1);
1064  }
1065 
1072  template <typename T, int R, int C>
1073  inline
1074  typename boost::math::tools::promote_args<T>::type
1075  variance(const Eigen::Matrix<T,R,C>& m) {
1076  validate_nonzero_size(m,"variance");
1077  if (m.size() == 1)
1078  return 0.0;
1079  typename boost::math::tools::promote_args<T>::type
1080  mn(mean(m));
1081  typename boost::math::tools::promote_args<T>::type
1082  sum_sq_diff(0);
1083  for (int i = 0; i < m.size(); ++i) {
1084  typename boost::math::tools::promote_args<T>::type
1085  diff = m(i) - mn;
1086  sum_sq_diff += diff * diff;
1087  }
1088  return sum_sq_diff / (m.size() - 1);
1089  }
1090 
1097  template <typename T>
1098  inline
1099  typename boost::math::tools::promote_args<T>::type
1100  sd(const std::vector<T>& v) {
1101  validate_nonzero_size(v,"sd");
1102  if (v.size() == 1) return 0.0;
1103  return sqrt(variance(v));
1104  }
1105 
1112  template <typename T, int R, int C>
1113  inline
1114  typename boost::math::tools::promote_args<T>::type
1115  sd(const Eigen::Matrix<T,R,C>& m) {
1116  // FIXME: redundant with test in variance; second line saves sqrt
1117  validate_nonzero_size(m,"sd");
1118  if (m.size() == 1) return 0.0;
1119  return sqrt(variance(m));
1120  }
1121 
1130  template <typename T>
1131  inline T sum(const std::vector<T>& xs) {
1132  if (xs.size() == 0) return 0;
1133  T sum(xs[0]);
1134  for (size_t i = 1; i < xs.size(); ++i)
1135  sum += xs[i];
1136  return sum;
1137  }
1138 
1145  template <typename T, int R, int C>
1146  inline double sum(const Eigen::Matrix<T,R,C>& v) {
1147  return v.sum();
1148  }
1149 
1156  template <typename T>
1157  inline T prod(const std::vector<T>& v) {
1158  if (v.size() == 0) return 1;
1159  T product = v[0];
1160  for (size_t i = 1; i < v.size(); ++i)
1161  product *= v[i];
1162  return product;
1163  }
1164 
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;
1174  return v.prod();
1175  }
1176 
1186  template <typename T>
1187  inline T trace(const Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>& m) {
1188  return m.trace();
1189  }
1190 
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();
1200  }
1201 
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();
1211  }
1212 
1213 
1214  // vector and matrix returns
1215 
1230  template <typename T1, typename T2, int R, int C>
1231  inline
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);
1240  return result;
1241  }
1251  template <typename T1, typename T2, int R, int C>
1252  inline
1253  Eigen::Matrix<typename boost::math::tools::promote_args<T1,T2>::type,R,C>
1254  add(const Eigen::Matrix<T1,R,C>& m,
1255  const T2& c) {
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;
1260  return result;
1261  }
1271  template <typename T1, typename T2, int R, int C>
1272  inline
1273  Eigen::Matrix<typename boost::math::tools::promote_args<T1,T2>::type,R,C>
1274  add(const T1& 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);
1280  return result;
1281  }
1282 
1283 
1297  template <typename T1, typename T2, int R, int C>
1298  inline
1299  Eigen::Matrix<typename boost::math::tools::promote_args<T1,T2>::type, R, C>
1300  subtract(const Eigen::Matrix<T1,R,C>& m1,
1301  const Eigen::Matrix<T2,R,C>& m2) {
1302  stan::math::validate_matching_dims(m1,m2,"subtract");
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);
1307  return result;
1308  }
1309 
1310  template <typename T1, typename T2, int R, int C>
1311  inline
1312  Eigen::Matrix<typename boost::math::tools::promote_args<T1,T2>::type, R, C>
1313  subtract(const T1& c,
1314  const Eigen::Matrix<T2,R,C>& m) {
1315 
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);
1320  return result;
1321  }
1322  template <typename T1, typename T2, int R, int C>
1323  inline
1324  Eigen::Matrix<typename boost::math::tools::promote_args<T1,T2>::type, R, C>
1325  subtract(const Eigen::Matrix<T1,R,C>& m,
1326  const T2& c) {
1327 
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;
1332  return result;
1333  }
1334 
1335 
1336 
1344  template <typename T>
1345  inline
1346  T minus(const T& x) {
1347  return -x;
1348  }
1349 
1350 
1359  template <int R, int C>
1360  inline
1361  Eigen::Matrix<double,R,C>
1362  divide(const Eigen::Matrix<double,R,C>& m,
1363  double c) {
1364  return m / c;
1365  }
1366 
1379  template <typename T1, typename T2, int R, int C>
1380  Eigen::Matrix<typename boost::math::tools::promote_args<T1,T2>::type, R, C>
1381  elt_multiply(const Eigen::Matrix<T1,R,C>& m1,
1382  const Eigen::Matrix<T2,R,C>& m2) {
1383  stan::math::validate_matching_dims(m1,m2,"elt_multiply");
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);
1388  return result;
1389  }
1390 
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>
1393  diag_pre_multiply(const Eigen::Matrix<T1,R1,C1>& m1,
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);
1404  return result;
1405  }
1406 
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>
1409  diag_post_multiply(const Eigen::Matrix<T1,R1,C1>& m1,
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);
1420  return result;
1421  }
1422 
1423 
1436  template <typename T1, typename T2, int R, int C>
1437  Eigen::Matrix<typename boost::math::tools::promote_args<T1,T2>::type, R, C>
1438  elt_divide(const Eigen::Matrix<T1,R,C>& m1,
1439  const Eigen::Matrix<T2,R,C>& m2) {
1440  stan::math::validate_matching_dims(m1,m2,"elt_multiply");
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);
1445  return result;
1446  }
1447 
1448 
1457  template <int R, int C>
1458  inline
1459  Eigen::Matrix<double,R,C>
1460  multiply(const Eigen::Matrix<double,R,C>& m,
1461  double c) {
1462  return c * m;
1463  }
1472  template <int R, int C>
1473  inline
1474  Eigen::Matrix<double,R,C>
1475  multiply(double c,
1476  const Eigen::Matrix<double,R,C>& m) {
1477  return c * m;
1478  }
1479 
1480 
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) {
1494 
1495  validate_multiplicable(m1,m2,"multiply");
1496  return m1*m2;
1497  }
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) {
1510  stan::math::validate_matching_sizes(rv,v,"multiply");
1511  if (rv.size() != v.size())
1512  throw std::domain_error("rv.size() != v.size()");
1513  return rv.dot(v);
1514  }
1515 
1524  inline matrix_d
1526  if (L.rows() == 0)
1527  return matrix_d(0,0);
1528  if (L.rows() == 1) {
1529  matrix_d result(1,1);
1530  result(0,0) = L(0,0) * L(0,0);
1531  return result;
1532  }
1533  // FIXME: write custom following agrad/matrix because can't get L_tri into
1534  // multiplication as no template support for tri * tri
1535  matrix_d L_tri = L.transpose().triangularView<Eigen::Upper>();
1536  return L.triangularView<Eigen::Lower>() * L_tri;
1537  }
1538 
1545  inline matrix_d
1546  tcrossprod(const matrix_d& M) {
1547  if (M.rows() == 0)
1548  return matrix_d(0,0);
1549  if (M.rows() == 1)
1550  return M * M.transpose();
1551  matrix_d result(M.rows(),M.rows());
1552  return result
1553  .setZero()
1554  .selfadjointView<Eigen::Upper>()
1555  .rankUpdate(M);
1556  }
1557 
1564  inline matrix_d
1565  crossprod(const matrix_d& M) {
1566  return tcrossprod(M.transpose());
1567  }
1568 
1581  template <typename T>
1582  inline
1583  Eigen::Matrix<T,1,Eigen::Dynamic>
1584  row(const Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>& m,
1585  size_t i) {
1586  validate_row_index(m,i,"row");
1587  return m.row(i - 1);
1588  }
1589 
1601  template <typename T>
1602  inline
1603  Eigen::Matrix<T,Eigen::Dynamic,1>
1604  col(const Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>& m,
1605  size_t j) {
1606  validate_column_index(m,j,"col");
1607  return m.col(j - 1);
1608  }
1609 
1619  template <typename T>
1620  inline
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) {
1624  validate_row_index(m,i,"block");
1625  validate_row_index(m,i+nrows-1,"block");
1626  validate_column_index(m,j,"block");
1627  validate_column_index(m,j+ncols-1,"block");
1628  return m.block(i - 1,j - 1,nrows,ncols);
1629  }
1630 
1631 
1638  template <typename T>
1639  inline
1640  Eigen::Matrix<T,Eigen::Dynamic,1>
1641  diagonal(const Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>& m) {
1642  return m.diagonal();
1643  }
1644 
1651  template <typename T>
1652  inline
1653  Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>
1654  diag_matrix(const Eigen::Matrix<T,Eigen::Dynamic,1>& v) {
1655  return v.asDiagonal();
1656  }
1657 
1658  template <typename T, int R, int C>
1659  Eigen::Matrix<T,C,R>
1660  inline
1661  transpose(const Eigen::Matrix<T,R,C>& m) {
1662  return m.transpose();
1663  }
1664 
1670  template <typename T>
1671  inline
1672  Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>
1673  inverse(const Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>& m) {
1674  validate_square(m,"matrix inverse");
1675  return m.inverse();
1676  }
1677 
1678 
1685  template <typename T>
1686  inline
1687  Eigen::Matrix<T,Eigen::Dynamic,1>
1688  softmax(const Eigen::Matrix<T,Eigen::Dynamic,1>& v) {
1689  using std::exp;
1690  stan::math::validate_nonzero_size(v,"vector softmax");
1691  Eigen::Matrix<T,Eigen::Dynamic,1> theta(v.size());
1692  T sum(0.0);
1693  T max_v = v.maxCoeff();
1694  for (int i = 0; i < v.size(); ++i) {
1695  theta[i] = exp(v[i] - max_v);
1696  sum += theta[i];
1697  }
1698  for (int i = 0; i < v.size(); ++i)
1699  theta[i] /= sum;
1700  return theta;
1701  }
1702 
1703 
1704  template <typename T1, typename T2, int R1,int C1,int R2,int C2>
1705  inline
1706  Eigen::Matrix<typename boost::math::tools::promote_args<T1,T2>::type,
1707  R1,C2>
1708  mdivide_left_tri_low(const Eigen::Matrix<T1,R1,C1> &A,
1709  const Eigen::Matrix<T2,R2,C2> &b) {
1710  stan::math::validate_square(A,"mdivide_left_tri_low/2");
1711  stan::math::validate_multiplicable(A,b,"mdivide_left_tri_low");
1712  return promote_common<Eigen::Matrix<T1,R1,C1>,
1713  Eigen::Matrix<T2,R1,C1> >(A)
1714  .template triangularView<Eigen::Lower>()
1715  .solve( promote_common<Eigen::Matrix<T1,R2,C2>,
1716  Eigen::Matrix<T2,R2,C2> >(b) );
1717  }
1718  template <typename T>
1719  inline
1720  Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>
1722  Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> &A) {
1723  stan::math::validate_square(A,"mdivide_left_tri_low/1");
1724  int n = A.rows();
1725  Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> b;
1726  b.setIdentity(n,n);
1727  A.template triangularView<Eigen::Lower>().solveInPlace(b);
1728  return b;
1729  }
1730 
1731 
1741  template <int TriView, typename T1, typename T2,
1742  int R1, int C1, int R2, int C2>
1743  inline
1744  Eigen::Matrix<typename boost::math::tools::promote_args<T1,T2>::type,
1745  R1,C2>
1746  mdivide_left_tri(const Eigen::Matrix<T1,R1,C1> &A,
1747  const Eigen::Matrix<T2,R2,C2> &b) {
1748  stan::math::validate_square(A,"mdivide_left_tri_low");
1749  stan::math::validate_multiplicable(A,b,"mdivide_left_tri");
1750  return promote_common<Eigen::Matrix<T1,R1,C1>,Eigen::Matrix<T2,R1,C1> >(A)
1751  .template triangularView<TriView>()
1752  .solve( promote_common<Eigen::Matrix<T1,R2,C2>,
1753  Eigen::Matrix<T2,R2,C2> >(b) );
1754  }
1762  template<int TriView, typename T>
1763  inline
1764  Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>
1765  mdivide_left_tri(const Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> &A) {
1766  stan::math::validate_square(A,"mdivide_left_tri");
1767  int n = A.rows();
1768  Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> b;
1769  b.setIdentity(n,n);
1770  A.template triangularView<TriView>().solveInPlace(b);
1771  return b;
1772  }
1773 
1782  template <typename T1, typename T2, int R1, int C1, int R2, int C2>
1783  inline
1784  Eigen::Matrix<typename boost::math::tools::promote_args<T1,T2>::type,R1,C2>
1785  mdivide_left(const Eigen::Matrix<T1,R1,C1> &A,
1786  const Eigen::Matrix<T2,R2,C2> &b) {
1787  stan::math::validate_square(A,"mdivide_left");
1788  stan::math::validate_multiplicable(A,b,"mdivide_left");
1789  return promote_common<Eigen::Matrix<T1,R1,C1>,
1790  Eigen::Matrix<T2,R1,C1> >(A)
1791  .lu()
1792  .solve( promote_common<Eigen::Matrix<T1,R2,C2>,
1793  Eigen::Matrix<T2,R2,C2> >(b) );
1794  }
1795 
1805  template <int TriView, typename T1, typename T2,
1806  int R1, int C1, int R2, int C2>
1807  inline
1808  Eigen::Matrix<typename boost::math::tools::promote_args<T1,T2>::type,R1,C2>
1809  mdivide_right_tri(const Eigen::Matrix<T1,R1,C1> &b,
1810  const Eigen::Matrix<T2,R2,C2> &A) {
1811  stan::math::validate_square(A,"mdivide_left_tri_low");
1812  stan::math::validate_multiplicable(b,A,"mdivide_right_tri");
1813  return promote_common<Eigen::Matrix<T1,R1,C1>,
1814  Eigen::Matrix<T2,R1,C1> >(A)
1815  .template triangularView<TriView>()
1816  .transpose()
1817  .solve(promote_common<Eigen::Matrix<T1,R2,C2>,
1818  Eigen::Matrix<T2,R2,C2> >(b)
1819  .transpose())
1820  .transpose();
1821  }
1831  template <typename T1, typename T2, int R1,int C1,int R2,int C2>
1832  inline
1833  Eigen::Matrix<typename boost::math::tools::promote_args<T1,T2>::type,R1,C2>
1834  mdivide_right_tri_low(const Eigen::Matrix<T1,R1,C1> &b,
1835  const Eigen::Matrix<T2,R2,C2> &A) {
1836  return mdivide_right_tri<Eigen::Lower>
1837  (promote_common<Eigen::Matrix<T1,R1,C1>,
1838  Eigen::Matrix<T2,R1,C1> >(b),
1839  promote_common<Eigen::Matrix<T1,R2,C2>,
1840  Eigen::Matrix<T2,R2,C2> >(A));
1841  }
1842 
1843 
1844 
1853  template <typename T1, typename T2, int R1, int C1, int R2, int C2>
1854  inline
1855  Eigen::Matrix<typename boost::math::tools::promote_args<T1,T2>::type,R1,C2>
1856  mdivide_right(const Eigen::Matrix<T1,R1,C1> &b,
1857  const Eigen::Matrix<T2,R2,C2> &A) {
1858  stan::math::validate_square(A,"mdivide_right");
1859  stan::math::validate_multiplicable(b,A,"mdivide_right");
1860  return promote_common<Eigen::Matrix<T1,R2,C2>,
1861  Eigen::Matrix<T2,R2,C2> >(A)
1862  .transpose()
1863  .lu()
1864  .solve(promote_common<Eigen::Matrix<T1,R1,C1>,
1865  Eigen::Matrix<T2,R1,C1> >(b)
1866  .transpose())
1867  .transpose();
1868  }
1869 
1879  template <typename T>
1880  Eigen::Matrix<T,Eigen::Dynamic,1>
1881  eigenvalues_sym(const Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>& m) {
1882  validate_nonzero_size(m,"eigenvalues_sym");
1883  validate_symmetric(m,"eigenvalues_sym");
1884  Eigen::SelfAdjointEigenSolver<Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> >
1885  solver(m,Eigen::EigenvaluesOnly);
1886  return solver.eigenvalues();
1887  }
1888 
1889  template <typename T>
1890  Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>
1891  eigenvectors_sym(const Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>& m) {
1892  validate_nonzero_size(m,"eigenvectors_sym");
1893  validate_symmetric(m,"eigenvectors_sym");
1894  Eigen::SelfAdjointEigenSolver<Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> >
1895  solver(m);
1896  return solver.eigenvectors();
1897  }
1898 
1899 
1900 
1901 
1912  template <typename T>
1913  Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>
1914  cholesky_decompose(const Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>& m) {
1915  validate_symmetric(m,"cholesky decomposition");
1916  Eigen::LLT<Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> >llt(m.rows());
1917  llt.compute(m);
1918  return llt.matrixL();
1919  }
1920 
1929  template <typename T>
1930  Eigen::Matrix<T,Eigen::Dynamic,1>
1931  singular_values(const Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>& m) {
1932  return Eigen::JacobiSVD<Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> >(m)
1933  .singularValues();
1934  }
1935 
1936  // void eigen_decompose_sym(const matrix_d& m,
1937  // vector_d& eigenvalues,
1938  // matrix_d& eigenvectors) {
1939  // Eigen::SelfAdjointEigenSolver<matrix_d> solver(m);
1940  // eigenvalues = solver.eigenvalues().real();
1941  // eigenvectors = solver.eigenvectors().real();
1942  // }
1943 
1944 
1945  // void svd(const matrix_d& m, matrix_d& u, matrix_d& v, vector_d& s) {
1946  // static const unsigned int THIN_SVD_OPTIONS
1947  // = Eigen::ComputeThinU | Eigen::ComputeThinV;
1948  // Eigen::JacobiSVD<matrix_d> svd(m, THIN_SVD_OPTIONS);
1949  // u = svd.matrixU();
1950  // v = svd.matrixV();
1951  // s = svd.singularValues();
1952  // }
1953 
1965  template <typename T>
1966  inline
1967  std::vector<T>
1968  cumulative_sum(const std::vector<T>& x) {
1969  std::vector<T> result(x.size());
1970  if (x.size() == 0)
1971  return result;
1972  result[0] = x[0];
1973  for (size_t i = 1; i < result.size(); ++i)
1974  result[i] = x[i] + result[i-1];
1975  return result;
1976  }
1991  template <typename T, int R, int C>
1992  inline
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());
1996  if (m.size() == 0)
1997  return result;
1998  result(0) = m(0);
1999  for (int i = 1; i < result.size(); ++i)
2000  result(i) = m(i) + result(i-1);
2001  return result;
2002  }
2003 
2004 
2005  // prints used in generator for print() statements in modeling language
2006 
2007  template <typename T>
2008  void stan_print(std::ostream* o, const T& x) {
2009  *o << x;
2010  }
2011 
2012  template <typename T>
2013  void stan_print(std::ostream* o, const std::vector<T>& x) {
2014  *o << '[';
2015  for (int i = 0; i < x.size(); ++i) {
2016  if (i > 0) *o << ',';
2017  stan_print(o,x[i]);
2018  }
2019  *o << ']';
2020  }
2021 
2022  template <typename T>
2023  void stan_print(std::ostream* o, const Eigen::Matrix<T,Eigen::Dynamic,1>& x) {
2024  *o << '[';
2025  for (int i = 0; i < x.size(); ++i) {
2026  if (i > 0) *o << ',';
2027  stan_print(o,x(i));
2028  }
2029  *o << ']';
2030  }
2031 
2032  template <typename T>
2033  void stan_print(std::ostream* o, const Eigen::Matrix<T,1,Eigen::Dynamic>& x) {
2034  *o << '[';
2035  for (int i = 0; i < x.size(); ++i) {
2036  if (i > 0) *o << ',';
2037  stan_print(o,x(i));
2038  }
2039  *o << ']';
2040  }
2041 
2042  template <typename T>
2043  void stan_print(std::ostream* o,
2044  const Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>& x) {
2045  *o << '[';
2046  for (int i = 0; i < x.rows(); ++i) {
2047  if (i > 0) *o << ',';
2048  stan_print(o,x.row(i));
2049  }
2050  *o << ']';
2051  }
2052 
2053 
2054  }
2055 
2056 }
2057 
2058 #endif
2059 
(Expert) Numerical traits for algorithmic differentiation variables.
Definition: matrix.hpp:53
void stan_print(std::ostream *o, const var &x)
Definition: matrix.hpp:1263
var sqrt(const var &a)
Return the square root of the specified variable (cmath).
Definition: agrad.hpp:1761
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)
Definition: matrix.hpp:1409
void validate_column_index(const Eigen::Matrix< T, R, C > &m, size_t j, const char *msg)
Definition: matrix.hpp:688
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.
Definition: matrix.hpp:1834
Eigen::Matrix< double, Eigen::Dynamic, 1 > vector_d
Type for (column) vector of double values.
Definition: matrix.hpp:158
void validate_greater_or_equal(const T1 &x, const T2 &y, const char *x_name, const char *y_name, const char *fun_name)
Definition: matrix.hpp:660
T prod(const std::vector< T > &v)
Returns the product of the coefficients of the specified standard vector.
Definition: matrix.hpp:1157
matrix_d tcrossprod(const matrix_d &M)
Returns the result of post-multiplying a matrix by its own transpose.
Definition: matrix.hpp:1546
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)
Definition: matrix.hpp:1708
T sum(const std::vector< T > &xs)
Return the sum of the values in the specified standard vector.
Definition: matrix.hpp:1131
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.
Definition: matrix.hpp:1931
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.
Definition: matrix.hpp:861
void validate_multiplicable(const Eigen::Matrix< T1, R1, C1 > &x1, const Eigen::Matrix< T2, R2, C2 > &x2, const char *msg)
Definition: matrix.hpp:784
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.
Definition: matrix.hpp:1020
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.
Definition: matrix.hpp:1100
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...
Definition: matrix.hpp:1525
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.
Definition: matrix.hpp:1438
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.
Definition: matrix.hpp:1233
void validate_symmetric(const Eigen::Matrix< T, R, C > &x, const char *msg)
Definition: matrix.hpp:724
int min(const std::vector< int > &x)
Returns the minimum coefficient in the specified column vector.
Definition: matrix.hpp:917
void validate_less_or_equal(const T1 &x, const T2 &y, const char *x_name, const char *y_name, const char *fun_name)
Definition: matrix.hpp:634
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > inverse(const Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &m)
Returns the inverse of the specified matrix.
Definition: matrix.hpp:1673
int max(const std::vector< int > &x)
Returns the maximum coefficient in the specified column vector.
Definition: matrix.hpp:968
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.
Definition: matrix.hpp:1856
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)
Definition: matrix.hpp:1393
matrix_d crossprod(const matrix_d &M)
Returns the result of pre-multiplying a matrix by its own transpose.
Definition: matrix.hpp:1565
void validate_matching_dims(const Eigen::Matrix< T1, R1, C1 > &x1, const Eigen::Matrix< T2, R2, C2 > &x2, const char *msg)
Definition: matrix.hpp:743
T trace(const Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &m)
Returns the trace of the specified matrix.
Definition: matrix.hpp:1187
Eigen::Matrix< T, Eigen::Dynamic, 1 > softmax(const Eigen::Matrix< T, Eigen::Dynamic, 1 > &v)
Return the softmax of the specified vector.
Definition: matrix.hpp:1688
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.
Definition: matrix.hpp:1785
void validate_square(const Eigen::Matrix< T, R, C > &x, const char *msg)
Definition: matrix.hpp:712
void validate_less(const T1 &x, const T2 &y, const char *x_name, const char *y_name, const char *fun_name)
Definition: matrix.hpp:647
size_t cols(const Eigen::Matrix< T, R, C > &m)
Definition: matrix.hpp:628
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.
Definition: matrix.hpp:1641
T minus(const T &x)
Returns the negation of the specified scalar or matrix.
Definition: matrix.hpp:1346
void validate_vector(const Eigen::Matrix< T, R, C > &x, const char *msg)
Definition: matrix.hpp:806
void validate_row_index(const Eigen::Matrix< T, R, C > &m, size_t i, const char *msg)
Definition: matrix.hpp:700
void resize(T &x, std::vector< size_t > dims)
Recursively resize the specified vector of vectors, which must bottom out at scalar values,...
Definition: matrix.hpp:220
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.
Definition: matrix.hpp:1300
common_type< T1, T2 >::type promote_common(const F &u)
Definition: matrix.hpp:117
void validate_greater(const T1 &x, const T2 &y, const char *x_name, const char *y_name, const char *fun_name)
Definition: matrix.hpp:673
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.
Definition: matrix.hpp:1381
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > eigenvectors_sym(const Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &m)
Definition: matrix.hpp:1891
Eigen::Matrix< T, C, R > transpose(const Eigen::Matrix< T, R, C > &m)
Definition: matrix.hpp:1661
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.
Definition: matrix.hpp:273
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.
Definition: matrix.hpp:1881
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...
Definition: matrix.hpp:1053
void validate_nonzero_size(const T &x, const char *msg)
Definition: matrix.hpp:797
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.
Definition: matrix.hpp:1584
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,...
Definition: matrix.hpp:1914
Eigen::Matrix< double, 1, Eigen::Dynamic > row_vector_d
Type for (row) vector of double values.
Definition: matrix.hpp:165
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > matrix_d
Type for matrix of double values.
Definition: matrix.hpp:151
Eigen::Matrix< T, Rows, Cols > exp(const Eigen::Matrix< T, Rows, Cols > &m)
Return the element-wise exponentiation of the matrix or vector.
Definition: matrix.hpp:1209
double dot_self(const Eigen::Matrix< double, R, C > &v)
Returns the dot product of the specified vector with itself.
Definition: matrix.hpp:846
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.
Definition: matrix.hpp:1746
void validate_matching_sizes(const std::vector< T1 > &x1, const std::vector< T2 > &x2, const char *msg)
Definition: matrix.hpp:757
size_t rows(const Eigen::Matrix< T, R, C > &m)
Definition: matrix.hpp:622
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.
Definition: matrix.hpp:1809
T determinant(const Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &m)
Returns the determinant of the specified square matrix.
Definition: matrix.hpp:832
Eigen::Matrix< T, Rows, Cols > log(const Eigen::Matrix< T, Rows, Cols > &m)
Return the element-wise logarithm of the matrix or vector.
Definition: matrix.hpp:1198
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.
Definition: matrix.hpp:1604
Eigen::Matrix< double, R, C > multiply(const Eigen::Matrix< double, R, C > &m, double c)
Return specified matrix multiplied by specified scalar.
Definition: matrix.hpp:1460
Eigen::Matrix< double, R, C > divide(const Eigen::Matrix< double, R, C > &m, double c)
Return specified matrix divided by specified scalar.
Definition: matrix.hpp:1362
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).
Definition: matrix.hpp:1622
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.
Definition: matrix.hpp:875
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.
Definition: matrix.hpp:1654
Probability, optimization and sampling library.
Definition: agrad.cpp:6
size_t length(const T &x)
Definition: traits.hpp:111
Template specification of functions in std for Stan.
Definition: agrad.hpp:2306
Structure for building up arrays in an expression (rather than in statements) using an argumentchaini...
Definition: matrix.hpp:131
array_builder & add(const F &u)
Definition: matrix.hpp:135
std::vector< T > array()
Definition: matrix.hpp:141
std::vector< T > x_
Definition: matrix.hpp:132
Eigen::Matrix< typename common_type< T1, T2 >::type, R, C > type
Definition: matrix.hpp:111
std::vector< typename common_type< T1, T2 >::type > type
Definition: matrix.hpp:106
boost::math::tools::promote_args< T1, T2 >::type type
Definition: matrix.hpp:101
static Eigen::Matrix< T, R, C > promote_to(const Eigen::Matrix< F, R, C > &u)
Definition: matrix.hpp:81
static void promote(const Eigen::Matrix< F, R, C > &u, Eigen::Matrix< T, R, C > &t)
Definition: matrix.hpp:74
static Eigen::Matrix< T, R, C > promote_to(const Eigen::Matrix< T, R, C > &u)
Definition: matrix.hpp:94
static void promote(const Eigen::Matrix< T, R, C > &u, Eigen::Matrix< T, R, C > &t)
Definition: matrix.hpp:90
static T promote_to(const T &u)
Definition: matrix.hpp:38
static void promote(const T &u, T &t)
Definition: matrix.hpp:35
static void promote(const std::vector< F > &u, std::vector< T > &t)
Definition: matrix.hpp:46
static std::vector< T > promote_to(const std::vector< F > &u)
Definition: matrix.hpp:53
static std::vector< T > promote_to(const std::vector< T > &u)
Definition: matrix.hpp:66
static void promote(const std::vector< T > &u, std::vector< T > &t)
Definition: matrix.hpp:62
static T promote_to(const F &u)
Definition: matrix.hpp:28
static void promote(const F &u, T &t)
Definition: matrix.hpp:25

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