00001 // This is core/vnl/algo/vnl_sparse_lu.h 00002 #ifndef vnl_sparse_lu_h_ 00003 #define vnl_sparse_lu_h_ 00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE 00005 #pragma interface 00006 #endif 00007 //: 00008 // \file 00009 // \brief Solution of the linear system Mx = b for sparse matrices 00010 // \author J. L. Mundy 00011 // \date 27 Dec 2006 00012 // 00013 // \verbatim 00014 // Modifications 00015 // None 00016 // \endverbatim 00017 00018 #include <vnl/vnl_vector.h> 00019 #include <vnl/vnl_sparse_matrix.h> 00020 00021 //: Linear system solver for Mx = b using LU decomposition of a sparse matrix 00022 // Encapsulating Sparse 1.3 by Kenneth S. Kundert. 00023 // The matrix is factored before solution. Any number of b vectors can 00024 // be applied after the matrix is factored with out recomputation of the 00025 // LU form. It is advised to construct with mode==estimate_condition and 00026 // check that rcond()>sqrt(machine precision). If this is not the case 00027 // the solution may be suspect. An upper bound on error is provided. 00028 // The solution of M^t x = b is also available. 00029 00030 class vnl_sparse_lu 00031 { 00032 public: 00033 //: Modes of computation. See constructor for details. 00034 enum operation { 00035 quiet, 00036 verbose, 00037 estimate_condition, 00038 estimate_condition_verbose 00039 }; 00040 00041 //: Make sparse_lu decomposition of M optionally computing the reciprocal condition number. 00042 vnl_sparse_lu(vnl_sparse_matrix<double> const& M, operation mode = quiet); 00043 ~vnl_sparse_lu(); 00044 00045 //: set the relative pivot threshold should be between 0 and 1 00046 // If set to one then pivoting is complete and slow 00047 // If near zero then roundoff error may be prohibitive but computation is fast 00048 // Typical values are between 0.01 and 0.1. 00049 void set_pivot_thresh(double pivot_thresh){pivot_thresh_=pivot_thresh;} 00050 00051 //: set the threshold on absolute element magnitude for pivoting 00052 // Should be either zero or significantly smaller than the absolute 00053 // value of the smallest diagonal element. 00054 void set_absolute_thresh(double absolute_thresh){absolute_thresh_=absolute_thresh;} 00055 //: set diagonal pivoting mode, normally 1 which gives priority to diagonal elements. 00056 void set_diagonal_pivoting(int diag_pivoting){diag_pivoting_=diag_pivoting;} 00057 00058 //: Solve problem M x = b 00059 vnl_vector<double> solve(vnl_vector<double> const& b); 00060 00061 //: Solve problem M x = b 00062 void solve(vnl_vector<double> const& b, vnl_vector<double>* x); 00063 00064 //: Solve problem M^t x = b 00065 vnl_vector<double> solve_transpose(vnl_vector<double> const& b); 00066 00067 //: Solve problem M^t x = b 00068 void solve_transpose(vnl_vector<double> const& b, vnl_vector<double>* x); 00069 00070 //: Compute determinant 00071 double determinant(); 00072 00073 //: Return reciprocal condition number (smallest/largest singular values). 00074 // As long as rcond()>sqrt(precision) the decomposition can be used for 00075 // solving equations safely. 00076 // Not calculated unless operation mode at construction includes estimate_condition. 00077 double rcond(); 00078 00079 //: An estimate of maximum error in solution. 00080 // Not calculated unless operation mode at construction includes estimate_condition. 00081 double max_error_bound(); 00082 00083 #if 0 // not immediately useful but left for future development 00084 //: Return the matrix after pivoting 00085 vnl_sparse_matrix<double> lu_matrix(); 00086 #endif 00087 00088 protected: 00089 // Internals 00090 void decompose_matrix(); 00091 bool est_condition(); 00092 // Data Members-------------------------------------------------------------- 00093 vnl_sparse_matrix<double> A_; 00094 bool factored_; 00095 bool condition_computed_; 00096 operation mode_; 00097 double norm_; 00098 double rcond_; 00099 double largest_; 00100 double pivot_thresh_; 00101 double absolute_thresh_; 00102 int diag_pivoting_; 00103 private: 00104 //: Copy constructor - privatised to avoid it being used 00105 vnl_sparse_lu(vnl_sparse_lu const & that); 00106 //: Assignment operator - privatised to avoid it being used 00107 vnl_sparse_lu& operator=(vnl_sparse_lu const & that); 00108 //: The internal matrix representation 00109 // 00110 // We don't use the typedef spMatrix directly here to avoid exposing 00111 // the implementation detail (sparse/spMatrix.h) to the user. 00112 void* pmatrix_; 00113 }; 00114 00115 #endif // vnl_sparse_lu_h_