Changeset 21
- Timestamp:
- 02/18/08 09:04:46 (17 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
bdm/math/libDC.cpp
r19 r21 42 42 D0 = ones(dim); 43 43 44 45 using std::cout;46 cout<<V;47 44 chol(V,F); 48 45 // L and D will be allocated by ldform() … … 205 202 } 206 203 207 void ldmat::ldform( mat &A,vec &D0 ) { 204 void ldmat::ldform( mat &A,vec &D0 ) 205 { 208 206 int m = A.rows(); 209 207 int n = A.cols(); … … 215 213 L=concat_vertical( zeros( n,n ), diag( sqrt( D0 ) )*A ); 216 214 D=zeros( n+m ); 217 218 //unnecessary big L and D will be made smaller at the end of file 219 220 vec w=zeros( n ); 221 vec v=zeros(n); 215 216 //unnecessary big L and D will be made smaller at the end of file 217 vec w=zeros( n+m ); 218 222 219 double sum, beta, pom; 223 220 224 221 int cc=0; 225 int i=n; // +1 in .m222 int i=n; // indexovani o 1 niz, nez v matlabu 226 223 int ii,j,jj; 227 while (( i> ( n-mn+1-cc ) )&&( i>1 ) ) { 224 while ( (i>n-mn-cc) && (i>0) ) 225 { 228 226 i--; 229 227 sum = 0.0; 230 v.set_size( m+i-( n-cc ) ); //prepare v 231 for ( ii=n-cc;ii<m+i;i++ ) { 228 229 int last_v = m+i-n+cc+1; 230 231 vec v = zeros( last_v + 1 ); //prepare v 232 for ( ii=n-cc-1;ii<m+i+1;ii++ ) 233 { 232 234 sum+= L( ii,i )*L( ii,i ); 233 v( ii )=L( ii,i ); //assign v 234 } 235 if ( L( m+i,i )==0 ) { 236 beta = sqrt( sum ); 237 } else { 238 beta = L( m+i,i )+sign( L( m+i,i ) )*sqrt( sum ) ; 239 } 240 if ( std::fabs( beta )<eps ) { 235 v( ii-n+cc+1 )=L( ii,i ); //assign v 236 } 237 238 if ( L( m+i,i )==0 ) 239 beta = sqrt( sum ); 240 else 241 beta = L( m+i,i )+sign( L( m+i,i ) )*sqrt( sum ); 242 243 if ( std::fabs( beta )<eps ) 244 { 241 245 cc++; 242 L.set_row( n +1-cc, L.get_row( m+i ) );246 L.set_row( n-cc, L.get_row( m+i ) ); 243 247 L.set_row( m+i,0 ); 244 248 D( m+i )=0; L( m+i,i )=1; 245 L.set_submatrix( n +1-cc,m+i,i,i,0 );249 L.set_submatrix( n-cc,m+i-1,i,i,0 ); 246 250 continue; 247 251 } 248 252 249 sum-=v( v.length()-1 )*v( v.length()-1 ); //253 sum-=v(last_v)*v(last_v); 250 254 sum/=beta*beta; 251 255 sum++; 252 256 253 257 v/=beta; 254 v( v.length()-1 )=1; 255 256 pom=-2/sum; 257 for ( j=i;i>=0;i-- ) { 258 w( j )=0.0; 259 for ( ii=n-cc;ii<m+i;ii++ ) { 260 w( j )+= v( ii )*L( ii,j ); 261 } 262 w( j )*=pom; 263 } 264 265 for ( ii=n-cc;ii<m+i;ii++ ) { 266 for ( jj=0;jj<i-1;jj++ ) { 267 L( ii,jj )+= v( ii )*w( jj ); 268 } 269 } 270 for ( ii=n-cc;ii<m+i;ii++ ) { 258 v(last_v)=1; 259 260 pom=-2.0/sum; 261 // echo to venca 262 263 for ( j=i;j>=0;j-- ) 264 { 265 double w_elem = 0; 266 for ( ii=n- cc;ii<=m+i+1;ii++ ) 267 w_elem+= v( ii-n+cc )*L( ii-1,j ); 268 w(j)=w_elem*pom; 269 } 270 271 for ( ii=n-cc-1;ii<=m+i;ii++ ) 272 for ( jj=0;jj<i;jj++ ) 273 L( ii,jj )+= v( ii-n+cc+1)*w( jj ); 274 275 for ( ii=n-cc-1;ii<m+i;ii++ ) 271 276 L( ii,i )= 0; 272 } 277 273 278 L( m+i,i )+=w( i ); 274 275 279 D( m+i )=L( m+i,i )*L( m+i,i ); 276 for ( ii=0;ii<i;ii++ ) { 277 L( m+i,ii )/=L( m+i,i ); 278 } 279 } 280 if ( i>0 ) { 281 for ( ii=0;ii<i-1;ii++ ) { 280 281 for ( ii=0;ii<=i;ii++ ) 282 L( m+i,ii )/=L( m+i,i ); 283 } 284 285 if ( i>=0 ) 286 for ( ii=0;ii<i;ii++ ) 287 { 282 288 jj = D.length()-1-n+ii; 289 D(jj) = 0; 283 290 L.set_row(jj,0); 284 291 L(jj,jj)=1; 285 292 } 286 } 287 288 //cut-out L and D; 293 289 294 L.del_rows(0,m-1); 290 295 D.del(0,m-1);