11 SymmetricMatrix_Wrapper::cholesky_semidefinite(
MyMatrix& a)
const 18 for (
int k=1; k<sz+1; ++k) {
20 if (a(k,k)< std::numeric_limits<double>::epsilon() && a(k,k)> -std::numeric_limits<double>::epsilon()){
24 std::cout<<
"Warning: matrix of which cholesky decomposition is asked, is negative definite!: returning zero matrix" << std::endl;
25 std::cout<<
"a(" << k <<
"," << k <<
")=" << a(k,k) << std::endl;
26 a = 0.0;
return false;
28 else a(k,k)=sqrt(a(k,k));
29 for (
int i=k+1; i<sz+1; ++i) {
30 if (a(k,k)< std::numeric_limits<double>::epsilon()){
35 for (
int j=k+1; j<sz+1; ++j) {
36 for (
int i=j; i<sz+1; ++i) a(i,j)-=a(i,k)*a(j,k);
40 for (
int i=1; i<sz+1; i++) {
41 for (
int j=i+1; j<sz+1; j++) a(i,j)=0.0;
50 const int rows=this->rows();
52 const int cols=this->columns();
56 static const int maxIter=150;
59 V.resize(cols,cols,
false,
true);
60 int i(-1),its(-1),j(-1),jj(-1),k(-1),nm(0);
63 double maxarg1, maxarg2;
64 double anorm(0),c(0),
f(0),g(0),h(0),s(0),scale(0),x(0),y(0),z(0);
67 std::vector<double> rv1(cols,0.0);
71 for (i=1; i <= cols; i++){
77 for(k=i;k<=rows;k++) scale += fabs(U(k,i));
81 for (k=i;k<=rows;k++){
89 for (j=ppi; j <= cols; j++) {
91 for (s=0.0,k=i;k<=rows;k++) s += U(k,i) * U(k,j);
94 for (k=i;k<=rows;k++) U(k,j) +=
f*U(k,i);
96 for (k=i;k<=rows;k++) U(k,i) *= scale;
102 if ((i <= rows) && (i != cols)) {
104 for(k=ppi;k<=cols;k++) scale += fabs(U(i,k));
106 for(k=ppi;k<=cols;k++){
114 for ( k=ppi; k <= cols; k++) rv1.at(k-1)=U(i,k)/h;
115 for ( j=ppi; j <= rows; j++) {
116 for( s=0.0,k=ppi;k<=cols;k++) s += U(j,k) * U(i,k);
117 for ( k=ppi; k <= cols; k++) U(j,k) += s*rv1.at(k-1);
119 for( k=ppi;k<=cols;k++) U(i,k) *= scale;
123 maxarg2=(fabs(w(i))+fabs(rv1.at(i-1)));
124 anorm = maxarg1 > maxarg2 ? maxarg1 : maxarg2;
129 for (i= cols ; i>=1; i--) {
132 for ( j=ppi; j <= cols; j++) V(j,i)=(U(i,j)/U(i,ppi))/g;
133 for ( j=ppi; j <= cols; j++) {
134 for (s=0.0,k=ppi;k <= cols;k++) s += U(i,k)*V(k,j);
135 for ( k=ppi; k <= cols;k++) V(k,j) += s*V(k,i);
138 for( j=ppi; j<=cols ; j++) V(i,j)=V(j,i)=0.0;
146 for (i= cols < rows ? cols: rows; i>=1; i--) {
149 for( j=ppi; j<=cols ; j++) U(i,j)=0.0;
152 for ( j=ppi; j <= cols; j++) {
153 for( s=0.0, k=ppi; k<=rows ; k++) s += U(k,i)*U(k,j);
155 for ( k=i; k <= rows;k++) U(k,j) +=
f*U(k,i);
157 for( j=i; j<=rows ; j++) U(j,i) *=g;
159 for( j=i; j<=rows ; j++) U(j,i) = 0.0;
166 for ( k=cols; k >= 1; k--) {
167 for (its=1; its <= maxIter; its++) {
170 for (ppi=k; ppi >= 1; ppi--) {
172 if ((fabs(rv1.at(ppi-1))+anorm) == anorm) {
176 if ((fabs(w(nm)+anorm) == anorm)) {
184 for ( i=ppi; i <= k ;i++) {
186 rv1.at(i-1)=c*rv1.at(i-1);
187 if ((fabs(
f)+anorm) == anorm) {
196 for ( j=1;j <= rows; j++) {
210 for (j=1; j <= cols; j++) V(j,k)=-V(j,k);
215 if (its == maxIter) {
217 std::cout <<
"SVD did not converge after " << maxIter <<
" iterations!" << std::endl;
228 f=((y-z)*(y+z)+(g-h)*(g+h))/(2.0*h*y);
231 f = ((x-z)*(x+z)+h*((y/(
f+SIGN(g,
f)))-h))/x;
235 for ( j=ppi; j<=nm ;j++){
249 for (jj=1; jj<=cols ;jj++){
265 for (jj=1; jj<=rows; jj++){
283 Matrix_Wrapper::PYTHAG(
double a,
double b)
const 290 return at*sqrt(1.0+ct*ct);
296 return bt*sqrt(1.0+ct*ct);
303 Matrix_Wrapper::SIGN(
double a,
double b)
const 305 return ((b) >= 0.0 ? fabs(a) : -fabs(a));
310 Matrix_Wrapper::pseudoinverse(
double epsilon)
const 314 int cols = this->columns();
323 Matrix Dinv(cols,cols);
325 for (
unsigned int i=0; i<D.rows(); i++)
326 if ( D(i+1) < epsilon )
329 Dinv(i+1,i+1) = 1/D(i+1);
332 std::cout <<
"MATRIX::pseudoinverse() Dinv =\n" << Dinv << std::endl;
335 return V * Dinv * U.transpose();
#define MySymmetricMatrix