|
Blender
V2.59
|
00001 00005 /* 00006 00007 * 00008 * Template Numerical Toolkit (TNT): Linear Algebra Module 00009 * 00010 * Mathematical and Computational Sciences Division 00011 * National Institute of Technology, 00012 * Gaithersburg, MD USA 00013 * 00014 * 00015 * This software was developed at the National Institute of Standards and 00016 * Technology (NIST) by employees of the Federal Government in the course 00017 * of their official duties. Pursuant to title 17 Section 105 of the 00018 * United States Code, this software is not subject to copyright protection 00019 * and is in the public domain. The Template Numerical Toolkit (TNT) is 00020 * an experimental system. NIST assumes no responsibility whatsoever for 00021 * its use by other parties, and makes no guarantees, expressed or implied, 00022 * about its quality, reliability, or any other characteristic. 00023 * 00024 * BETA VERSION INCOMPLETE AND SUBJECT TO CHANGE 00025 * see http://math.nist.gov/tnt for latest updates. 00026 * 00027 */ 00028 00029 00030 00031 // Triangular Solves 00032 00033 #ifndef TRISLV_H 00034 #define TRISLV_H 00035 00036 00037 #include "triang.h" 00038 00039 namespace TNT 00040 { 00041 00042 template <class MaTriX, class VecToR> 00043 VecToR Lower_triangular_solve(/*const*/ MaTriX &A, /*const*/ VecToR &b) 00044 { 00045 Subscript N = A.num_rows(); 00046 00047 // make sure matrix sizes agree; A must be square 00048 00049 assert(A.num_cols() == N); 00050 assert(b.dim() == N); 00051 00052 VecToR x(N); 00053 00054 Subscript i; 00055 for (i=1; i<=N; i++) 00056 { 00057 typename MaTriX::element_type tmp=0; 00058 00059 for (Subscript j=1; j<i; j++) 00060 tmp = tmp + A(i,j)*x(j); 00061 00062 x(i) = (b(i) - tmp)/ A(i,i); 00063 } 00064 00065 return x; 00066 } 00067 00068 00069 template <class MaTriX, class VecToR> 00070 VecToR Unit_lower_triangular_solve(/*const*/ MaTriX &A, /*const*/ VecToR &b) 00071 { 00072 Subscript N = A.num_rows(); 00073 00074 // make sure matrix sizes agree; A must be square 00075 00076 assert(A.num_cols() == N); 00077 assert(b.dim() == N); 00078 00079 VecToR x(N); 00080 00081 Subscript i; 00082 for (i=1; i<=N; i++) 00083 { 00084 00085 typename MaTriX::element_type tmp=0; 00086 00087 for (Subscript j=1; j<i; j++) 00088 tmp = tmp + A(i,j)*x(j); 00089 00090 x(i) = b(i) - tmp; 00091 } 00092 00093 return x; 00094 } 00095 00096 00097 template <class MaTriX, class VecToR> 00098 VecToR linear_solve(/*const*/ LowerTriangularView<MaTriX> &A, 00099 /*const*/ VecToR &b) 00100 { 00101 return Lower_triangular_solve(A, b); 00102 } 00103 00104 template <class MaTriX, class VecToR> 00105 VecToR linear_solve(/*const*/ UnitLowerTriangularView<MaTriX> &A, 00106 /*const*/ VecToR &b) 00107 { 00108 return Unit_lower_triangular_solve(A, b); 00109 } 00110 00111 00112 00113 //********************** Upper triangular section **************** 00114 00115 template <class MaTriX, class VecToR> 00116 VecToR Upper_triangular_solve(/*const*/ MaTriX &A, /*const*/ VecToR &b) 00117 { 00118 Subscript N = A.num_rows(); 00119 00120 // make sure matrix sizes agree; A must be square 00121 00122 assert(A.num_cols() == N); 00123 assert(b.dim() == N); 00124 00125 VecToR x(N); 00126 00127 Subscript i; 00128 for (i=N; i>=1; i--) 00129 { 00130 00131 typename MaTriX::element_type tmp=0; 00132 00133 for (Subscript j=i+1; j<=N; j++) 00134 tmp = tmp + A(i,j)*x(j); 00135 00136 x(i) = (b(i) - tmp)/ A(i,i); 00137 } 00138 00139 return x; 00140 } 00141 00142 00143 template <class MaTriX, class VecToR> 00144 VecToR Unit_upper_triangular_solve(/*const*/ MaTriX &A, /*const*/ VecToR &b) 00145 { 00146 Subscript N = A.num_rows(); 00147 00148 // make sure matrix sizes agree; A must be square 00149 00150 assert(A.num_cols() == N); 00151 assert(b.dim() == N); 00152 00153 VecToR x(N); 00154 00155 Subscript i; 00156 for (i=N; i>=1; i--) 00157 { 00158 00159 typename MaTriX::element_type tmp=0; 00160 00161 for (Subscript j=i+1; j<i; j++) 00162 tmp = tmp + A(i,j)*x(j); 00163 00164 x(i) = b(i) - tmp; 00165 } 00166 00167 return x; 00168 } 00169 00170 00171 template <class MaTriX, class VecToR> 00172 VecToR linear_solve(/*const*/ UpperTriangularView<MaTriX> &A, 00173 /*const*/ VecToR &b) 00174 { 00175 return Upper_triangular_solve(A, b); 00176 } 00177 00178 template <class MaTriX, class VecToR> 00179 VecToR linear_solve(/*const*/ UnitUpperTriangularView<MaTriX> &A, 00180 /*const*/ VecToR &b) 00181 { 00182 return Unit_upper_triangular_solve(A, b); 00183 } 00184 00185 00186 } // namespace TNT 00187 00188 #endif // TRISLV_H 00189