Blender  V2.59
cholesky.h
Go to the documentation of this file.
00001 /*
00002  * $Id: cholesky.h 228 2002-12-26 18:25:17Z mein $
00003  */
00004 
00005 /*
00006 *
00007 * Template Numerical Toolkit (TNT): Linear Algebra Module
00008 *
00009 * Mathematical and Computational Sciences Division
00010 * National Institute of Technology,
00011 * Gaithersburg, MD USA
00012 *
00013 *
00014 * This software was developed at the National Institute of Standards and
00015 * Technology (NIST) by employees of the Federal Government in the course
00016 * of their official duties. Pursuant to title 17 Section 105 of the
00017 * United States Code, this software is not subject to copyright protection
00018 * and is in the public domain.  The Template Numerical Toolkit (TNT) is
00019 * an experimental system.  NIST assumes no responsibility whatsoever for
00020 * its use by other parties, and makes no guarantees, expressed or implied,
00021 * about its quality, reliability, or any other characteristic.
00022 *
00023 * BETA VERSION INCOMPLETE AND SUBJECT TO CHANGE
00024 * see http://math.nist.gov/tnt for latest updates.
00025 *
00026 */
00027 
00028 #ifndef CHOLESKY_H
00029 #define CHOLESKY_H
00030 
00031 #include <cmath>
00032 
00033 // index method
00034 
00035 namespace TNT
00036 {
00037 
00038 
00039 //
00040 // Only upper part of A is used.  Cholesky factor is returned in
00041 // lower part of L.  Returns 0 if successful, 1 otherwise.
00042 //
00043 template <class SPDMatrix, class SymmMatrix>
00044 int Cholesky_upper_factorization(SPDMatrix &A, SymmMatrix &L)
00045 {
00046     Subscript M = A.dim(1);
00047     Subscript N = A.dim(2);
00048 
00049     assert(M == N);                 // make sure A is square
00050 
00051     // readjust size of L, if necessary
00052 
00053     if (M != L.dim(1) || N != L.dim(2))
00054         L = SymmMatrix(N,N);
00055 
00056     Subscript i,j,k;
00057 
00058 
00059     typename SPDMatrix::element_type dot=0;
00060 
00061 
00062     for (j=1; j<=N; j++)                // form column j of L
00063     {
00064         dot= 0;
00065 
00066         for (i=1; i<j; i++)             // for k= 1 TO j-1
00067             dot = dot +  L(j,i)*L(j,i);
00068 
00069         L(j,j) = A(j,j) - dot;
00070 
00071         for (i=j+1; i<=N; i++)
00072         {
00073             dot = 0;
00074             for (k=1; k<j; k++)
00075                 dot = dot +  L(i,k)*L(j,k);
00076             L(i,j) = A(j,i) - dot;
00077         }
00078 
00079         if (L(j,j) <= 0.0) return 1;
00080 
00081         L(j,j) = sqrt( L(j,j) );
00082 
00083         for (i=j+1; i<=N; i++)
00084             L(i,j) = L(i,j) / L(j,j);
00085 
00086     }
00087 
00088     return 0;
00089 }
00090 
00091 
00092 
00093 
00094 }  
00095 // namespace TNT
00096 
00097 #endif
00098 // CHOLESKY_H
00099