Stan  1.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
matrix.cpp
Go to the documentation of this file.
1 #ifndef __STAN__AGRAD__MATRIX_CPP__
2 #define __STAN__AGRAD__MATRIX_CPP__
3 
4 #include "stan/agrad/matrix.hpp"
5 
6 namespace stan {
7 
8  namespace agrad {
9 
10  var determinant(const Eigen::Matrix<var, Eigen::Dynamic, Eigen::Dynamic>& m) {
11  if (m.rows() != m.cols())
12  throw std::domain_error ("m must be a square matrix");
13  return m.determinant();
14  }
15 
16  row_vector_v row(const matrix_v& m, size_t i) {
18  return m.row(i - 1);
19  }
20 
21  vector_v col(const matrix_v& m, size_t j) {
23  return m.col(j - 1);
24  }
25 
27  return m.diagonal();
28  }
29 
31  return v.asDiagonal();
32  }
33 
35  return v.transpose();
36  }
37 
39  return rv.transpose();
40  }
41 
43  return m.transpose();
44  }
45 
47  stan::math::validate_square(m,"inverse");
48  return m.inverse();
49  }
50 
52  stan::math::validate_nonzero_size(x,"vector softmax");
53  vector_v theta(x.size());
54  stan::math::softmax<vector_v,stan::agrad::var>(x,theta);
55  return theta;
56  }
57 
59  stan::math::validate_square(m,"eigenvalues");
60  // false == no vectors
61  Eigen::EigenSolver<matrix_v> solver(m,false);
62  // FIXME: test imag() all 0?
63  return solver.eigenvalues().real();
64  }
65 
67  stan::math::validate_nonzero_size(m,"eigenvectors");
68  stan::math::validate_square(m,"eigenvectors");
69  Eigen::EigenSolver<matrix_v> solver(m);
70  return solver.eigenvectors().real();
71  }
72 
73  void eigen_decompose(const matrix_v& m,
76  Eigen::EigenSolver<matrix_v> solver(m);
77  eigenvalues = solver.eigenvalues().real();
78  eigenvectors = solver.eigenvectors().real();
79  }
80 
82  stan::math::validate_nonzero_size(m,"eigenvalues_sym");
83  stan::math::validate_square(m,"eigenvalues_sym");
84  Eigen::SelfAdjointEigenSolver<matrix_v> solver(m,Eigen::EigenvaluesOnly);
85  return solver.eigenvalues().real();
86  }
87 
89  stan::math::validate_nonzero_size(m,"eigenvectors_sym");
90  stan::math::validate_square(m,"eigenvectors_sym");
91  Eigen::SelfAdjointEigenSolver<matrix_v> solver(m);
92  return solver.eigenvectors().real();
93  }
94 
98  Eigen::SelfAdjointEigenSolver<matrix_v> solver(m);
99  eigenvalues = solver.eigenvalues().real();
100  eigenvectors = solver.eigenvectors().real();
101  }
102 
104  stan::math::validate_square(m,"cholesky decomposition");
105  Eigen::LLT<matrix_v> llt(m.rows());
106  llt.compute(m);
107  return llt.matrixL();
108  }
109 
111  Eigen::JacobiSVD<matrix_v> svd(m); // no U or V
112  return svd.singularValues();
113  }
114 
115  void svd(const matrix_v& m,
116  matrix_v& u,
117  matrix_v& v,
118  vector_v& s) {
119  static const unsigned int THIN_SVD_OPTIONS
120  = Eigen::ComputeThinU | Eigen::ComputeThinV;
121  Eigen::JacobiSVD<matrix_v> svd(m, THIN_SVD_OPTIONS);
122  u = svd.matrixU();
123  v = svd.matrixV();
124  s = svd.singularValues();
125  }
126 
127  void stan_print(std::ostream* o, const var& x) {
128  *o << x.val();
129  }
130 
131 
132  }
133 
134 }
135 
136 #endif

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