Blender V2.61 - r43446
|
00001 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 00029 00030 // Triangular Solves 00031 00032 #ifndef TRISLV_H 00033 #define TRISLV_H 00034 00035 00036 #include "triang.h" 00037 00038 namespace TNT 00039 { 00040 00041 template <class MaTriX, class VecToR> 00042 VecToR Lower_triangular_solve(/*const*/ MaTriX &A, /*const*/ VecToR &b) 00043 { 00044 Subscript N = A.num_rows(); 00045 00046 // make sure matrix sizes agree; A must be square 00047 00048 assert(A.num_cols() == N); 00049 assert(b.dim() == N); 00050 00051 VecToR x(N); 00052 00053 Subscript i; 00054 for (i=1; i<=N; i++) 00055 { 00056 typename MaTriX::element_type tmp=0; 00057 00058 for (Subscript j=1; j<i; j++) 00059 tmp = tmp + A(i,j)*x(j); 00060 00061 x(i) = (b(i) - tmp)/ A(i,i); 00062 } 00063 00064 return x; 00065 } 00066 00067 00068 template <class MaTriX, class VecToR> 00069 VecToR Unit_lower_triangular_solve(/*const*/ MaTriX &A, /*const*/ VecToR &b) 00070 { 00071 Subscript N = A.num_rows(); 00072 00073 // make sure matrix sizes agree; A must be square 00074 00075 assert(A.num_cols() == N); 00076 assert(b.dim() == N); 00077 00078 VecToR x(N); 00079 00080 Subscript i; 00081 for (i=1; i<=N; i++) 00082 { 00083 00084 typename MaTriX::element_type tmp=0; 00085 00086 for (Subscript j=1; j<i; j++) 00087 tmp = tmp + A(i,j)*x(j); 00088 00089 x(i) = b(i) - tmp; 00090 } 00091 00092 return x; 00093 } 00094 00095 00096 template <class MaTriX, class VecToR> 00097 VecToR linear_solve(/*const*/ LowerTriangularView<MaTriX> &A, 00098 /*const*/ VecToR &b) 00099 { 00100 return Lower_triangular_solve(A, b); 00101 } 00102 00103 template <class MaTriX, class VecToR> 00104 VecToR linear_solve(/*const*/ UnitLowerTriangularView<MaTriX> &A, 00105 /*const*/ VecToR &b) 00106 { 00107 return Unit_lower_triangular_solve(A, b); 00108 } 00109 00110 00111 00112 //********************** Upper triangular section **************** 00113 00114 template <class MaTriX, class VecToR> 00115 VecToR Upper_triangular_solve(/*const*/ MaTriX &A, /*const*/ VecToR &b) 00116 { 00117 Subscript N = A.num_rows(); 00118 00119 // make sure matrix sizes agree; A must be square 00120 00121 assert(A.num_cols() == N); 00122 assert(b.dim() == N); 00123 00124 VecToR x(N); 00125 00126 Subscript i; 00127 for (i=N; i>=1; i--) 00128 { 00129 00130 typename MaTriX::element_type tmp=0; 00131 00132 for (Subscript j=i+1; j<=N; j++) 00133 tmp = tmp + A(i,j)*x(j); 00134 00135 x(i) = (b(i) - tmp)/ A(i,i); 00136 } 00137 00138 return x; 00139 } 00140 00141 00142 template <class MaTriX, class VecToR> 00143 VecToR Unit_upper_triangular_solve(/*const*/ MaTriX &A, /*const*/ VecToR &b) 00144 { 00145 Subscript N = A.num_rows(); 00146 00147 // make sure matrix sizes agree; A must be square 00148 00149 assert(A.num_cols() == N); 00150 assert(b.dim() == N); 00151 00152 VecToR x(N); 00153 00154 Subscript i; 00155 for (i=N; i>=1; i--) 00156 { 00157 00158 typename MaTriX::element_type tmp=0; 00159 00160 for (Subscript j=i+1; j<i; j++) 00161 tmp = tmp + A(i,j)*x(j); 00162 00163 x(i) = b(i) - tmp; 00164 } 00165 00166 return x; 00167 } 00168 00169 00170 template <class MaTriX, class VecToR> 00171 VecToR linear_solve(/*const*/ UpperTriangularView<MaTriX> &A, 00172 /*const*/ VecToR &b) 00173 { 00174 return Upper_triangular_solve(A, b); 00175 } 00176 00177 template <class MaTriX, class VecToR> 00178 VecToR linear_solve(/*const*/ UnitUpperTriangularView<MaTriX> &A, 00179 /*const*/ VecToR &b) 00180 { 00181 return Unit_upper_triangular_solve(A, b); 00182 } 00183 00184 00185 } // namespace TNT 00186 00187 #endif // TRISLV_H 00188