00001 // ------------------------------------------------------------------------ 00002 // matvec.h - prototypes of functions to handle matrices and vectors. 00003 // ------------------------------------------------------------------------ 00004 // 00005 // ------------------------------------------------------------------------ 00006 // Public functions: 00007 // ------------------------------------------------------------------------ 00008 // 00009 // double *VecAlloc(int n) 00010 // 00011 // n - vector dimension (in) 00012 // 00013 // This function allocates memory for a vector of the given size. 00014 // ------------------------------------------------------------------------ 00015 // 00016 // void VecFree(double *v) 00017 // 00018 // v - given vector (in) 00019 // 00020 // This function releases the memory allocated to the given vector. 00021 // ------------------------------------------------------------------------ 00022 // 00023 // void VecZero(int n, double *v) 00024 // 00025 // n - vector dimension (in) 00026 // v - given vector (in/out) 00027 // 00028 // This function assigns 0.0 to all elements of the given vector. 00029 // ------------------------------------------------------------------------ 00030 // 00031 // void VecAssign(int n, double *u, double *v) 00032 // 00033 // n - vector dimension (in) 00034 // u - input vector (in) 00035 // v - output vector (out) 00036 // 00037 // This function copies a given vector to another vector: {v} = {u}. 00038 // ------------------------------------------------------------------------ 00039 // 00040 // void VecMult(int n, double a, double *u, double *v) 00041 // 00042 // n - vector dimension (in) 00043 // a - given scalar (in) 00044 // u - input vector (in) 00045 // v - output vector (out) 00046 // 00047 // This function multiplies a given vector by a scalar: {v} = a*{u}. 00048 // ------------------------------------------------------------------------ 00049 // 00050 // void VecAdd(int n, double *u, double *v) 00051 // 00052 // n - vector dimension (in) 00053 // u - input vector (in) 00054 // v - output vector (out) 00055 // 00056 // This function adds a given vector to another vector: {v} += {u}. 00057 // ------------------------------------------------------------------------ 00058 // 00059 // void VecAdd(int n, double *u, double *v, double *w) 00060 // 00061 // n - vector dimension (in) 00062 // u,v - input vectors (in) 00063 // w - output vector (out) 00064 // 00065 // This function performs the operation {w} = {u} + {v}. 00066 // ------------------------------------------------------------------------ 00067 // 00068 // void VecSclAdd(int n, double a, double *u, double *v, double *w) 00069 // 00070 // n - vector dimension (in) 00071 // a - given scalar (in) 00072 // u,v - input vectors (in) 00073 // w - output vector (out) 00074 // 00075 // This function performs the operation {w} = a*{u} + {v}. 00076 // ------------------------------------------------------------------------ 00077 // 00078 // void VecSub(int n, double *u, double *v) 00079 // 00080 // n - vector dimension (in) 00081 // u - input vector (in) 00082 // v - output vector (out) 00083 // 00084 // This function performs the operation {v} -= {u}. 00085 // ------------------------------------------------------------------------ 00086 // 00087 // void VecSub(int n, double *u, double *v, double *w) 00088 // 00089 // n - vector dimension (in) 00090 // u,v - input vectors (in) 00091 // w - output vector (out) 00092 // 00093 // This function performs the operation {w} = {u} - {v}. 00094 // ------------------------------------------------------------------------ 00095 // 00096 // void VecSclSub(int n, double a, double *u, double *v, double *w) 00097 // 00098 // n - vector dimension (in) 00099 // a - given scalar (in) 00100 // u,v - input vectors (in) 00101 // w - output vector (out) 00102 // 00103 // This function performs the operation {w} = a*{u} - {v}. 00104 // ------------------------------------------------------------------------ 00105 // 00106 // void VecDiv(int n, double *u, double *v, double *w) 00107 // 00108 // n - vector dimension (in) 00109 // u,v - input vectors (in) 00110 // w - output vector (out) 00111 // 00112 // This function performs the operation {w} = {u}/{v} => w[i] = u[i]/v[i]. 00113 // ------------------------------------------------------------------------ 00114 // 00115 // double VecDot(int n, double *u, double *v) 00116 // 00117 // n - vector dimension (in) 00118 // u,v - given vectors (in) 00119 // 00120 // This function returns the scalar (dot) product between two vectors. 00121 // ------------------------------------------------------------------------ 00122 // 00123 // double VecLen(int n, double *u) 00124 // 00125 // n - vector dimension (in) 00126 // u - given vector (in) 00127 // 00128 // This function returns the length of the given vector. 00129 // ------------------------------------------------------------------------ 00130 // 00131 // double **MatAlloc(int n, int m) 00132 // 00133 // n - number of rows (in) 00134 // m - number of colums (in) 00135 // 00136 // This function allocates memory for a matrix of the given dimensions. 00137 // ------------------------------------------------------------------------ 00138 // 00139 // void MatZero(int n, int m, double **A) 00140 // 00141 // n - number of rows (in) 00142 // m - number of colums (in) 00143 // A - given matrix (in/out) 00144 // 00145 // This function assigns 0.0 to all elements of the given matrix. 00146 // ------------------------------------------------------------------------ 00147 // 00148 // void MatAssign(int n, int m, double **A, double **B) 00149 // 00150 // n - number of rows (in) 00151 // m - number of colums (in) 00152 // A - given matrix (in) 00153 // B - output matrix (out) 00154 // 00155 // This function performs the operation [B] = [A]. 00156 // ------------------------------------------------------------------------ 00157 // 00158 // void MatAdd(int n, int m, double **A, double **B) 00159 // 00160 // n - number of rows (in) 00161 // m - number of colums (in) 00162 // A - given matrix (in) 00163 // B - output matrix (out) 00164 // 00165 // This function performs the operation [B] += [A]. 00166 // ------------------------------------------------------------------------ 00167 // 00168 // void MatAdd(int n, int m, double **A, double **B, double **C) 00169 // 00170 // n - number of rows (in) 00171 // m - number of colums (in) 00172 // A,B - given matrices (in) 00173 // C - output matrix (out) 00174 // 00175 // This function performs the operation [C] = [A] + [B]. 00176 // ------------------------------------------------------------------------ 00177 // 00178 // void MatSub(int n, int m, double **A, double **B) 00179 // 00180 // n - number of rows (in) 00181 // m - number of colums (in) 00182 // A - given matrix (in) 00183 // B - output matrix (out) 00184 // 00185 // This function performs the operation [B] -= [A]. 00186 // ------------------------------------------------------------------------ 00187 // 00188 // void MatSub(int n, int m, double **A, double **B, double **C) 00189 // 00190 // n - number of rows (in) 00191 // m - number of colums (in) 00192 // A,B - given matrices (in) 00193 // C - output matrix (out) 00194 // 00195 // This function performs the operation [C] = [A] - [B]. 00196 // ------------------------------------------------------------------------ 00197 // 00198 // void MatMult(int n, int m, double f, double **A, double **B) 00199 // 00200 // n - number of rows (in) 00201 // m - number of colums (in) 00202 // f - given scalar (in) 00203 // A - given matrix (in) 00204 // B - output matrix (out) 00205 // 00206 // This function performs the operation [B] = f*[A]. 00207 // ------------------------------------------------------------------------ 00208 // 00209 // void MatMult(int n, int m, int l, double **A, double **B, double **C) 00210 // 00211 // n,m,l - dimension of given matrices (in) 00212 // A - left matrix (n x l) (in) 00213 // B - right matrix (l x m) (in) 00214 // C - output matrix (n x m) (out) 00215 // 00216 // This function performs the operation [C] = [A] * [B]. 00217 // nxm nxl lxm 00218 // ------------------------------------------------------------------------ 00219 // 00220 // void MatVecMult(int n, int m, double **A, double *u, double *v) 00221 // 00222 // n - number of rows (in) 00223 // m - number of colums (in) 00224 // A - given matrix (in) 00225 // u - given vector (in) 00226 // v - output vector (out) 00227 // 00228 // This function performs the operation {v} = [A]*{u}. 00229 // ------------------------------------------------------------------------ 00230 // 00231 // void MatVecMultT(int m, int n, double **A, double *u, double *v) 00232 // 00233 // m - number of colums (in) 00234 // n - number of rows (in) 00235 // A - given matrix (in) 00236 // u - given vector (in) 00237 // v - output vector (out) 00238 // 00239 // This function performs the operation {v} = [A]t*{u}. 00240 // ------------------------------------------------------------------------ 00241 // 00242 // void MatVecMultTAcc(double a, int m, int n, double **A, double *u, 00243 // double *v) 00244 // 00245 // a - given scalar (in) 00246 // m - number of colums (in) 00247 // n - number of rows (in) 00248 // A - given matrix (in) 00249 // u - given vector (in) 00250 // v - output vector (out) 00251 // 00252 // This function performs the operation {v} += a*[A]t*{u}. 00253 // ------------------------------------------------------------------------ 00254 // 00255 // void MatTripBtCB(int m, int n, double **B, double **C, double w, 00256 // double **K) 00257 // 00258 // m,n - matrix dimensions (in) 00259 // B - rectangular matrix => m x n (in) 00260 // C - square matrix => m x m (in) 00261 // w - scalar (in) 00262 // K - output matrix (out) 00263 // 00264 // This function performs the operation [K] += w*[B]t*[C]*[B]. 00265 // ------------------------------------------------------------------------ 00266 // 00267 // double **SkylAlloc(int n, int *p) 00268 // 00269 // n - number of rows (in) 00270 // p - matrix profile (in) 00271 // 00272 // This function allocates a symmetric square matrix stored in skyline 00273 // form. The profile vector stores the column of the first non-zero 00274 // element of each row of the matrix. 00275 // ------------------------------------------------------------------------ 00276 // 00277 // void SkylFree(double **A, int n, int *p) 00278 // 00279 // A - given matrix (in) 00280 // n - number of rows (in) 00281 // p - matrix profile (in) 00282 // 00283 // This function releases the memory allocated to the given profile matrix. 00284 // ------------------------------------------------------------------------ 00285 // 00286 // void SkylZero(int n, int *p, double **A) 00287 // 00288 // n - number of rows (in) 00289 // p - matrix profile (in) 00290 // A - given matrix (in/out) 00291 // 00292 // This function assigns 0.0 to all elements of the given matrix. 00293 // ------------------------------------------------------------------------ 00294 // 00295 // int CroutSolver(int op, double **A, double *b, int n, int *p) 00296 // 00297 // op - (1) complete sol. / (2) factorization / (3) backsub. (in) 00298 // A - system matrix (in) 00299 // b - r.h.s. vector / solution vector (in/out) 00300 // n - system dimension (in) 00301 // p - matrix skyline (in) 00302 // 00303 // This function solves the system [A]{x} = {b} using the [L][D][L]t 00304 // factorization. It returns (1) for a successful operation and (0) 00305 // otherwise. 00306 // ------------------------------------------------------------------------ 00307 00308 #ifndef _MATVEC_H 00309 #define _MATVEC_H 00310 00311 #include <string.h> 00312 00313 // ------------------------------------------------------------------------ 00314 // Vector functions: 00315 // 00316 double *VecAlloc(int n); 00317 void VecFree(double *v); 00318 void VecZero(int n, double *u); 00319 void VecAssign(int n, double *u, double *v); 00320 void VecSet(int n, double a, double *v); 00321 void VecMul(int n, double a, double *u, double *v); 00322 void VecMulAdd(int n, double a, double *u, double *v); 00323 void VecAdd(int n, double *u, double *v); 00324 void VecAdd(int n, double *u, double *v, double *w); 00325 void VecSclAdd(int n, double a, double *u, double *v, double *w); 00326 void VecSclAddInc(int n, double a, double *u, double *v, double *w); 00327 void VecSub(int n, double *u, double *v); 00328 void VecSub(int n, double *u, double *v, double *w); 00329 void VecSclSub(int n, double a, double *u, double *v, double *w); 00330 void VecSclSubInc(int n, double a, double *u, double *v, double *w); 00331 void VecDiv(int n, double *u, double *v, double *w); 00332 double VecDot(int n, double *u, double *v); 00333 double VecLen(int n, double *u); 00334 00335 // ================================ VecZero ================================ 00336 00337 inline void VecZero(int n, double *u) 00338 { 00339 memset(u, '\0', n*sizeof(double)); 00340 } 00341 00342 // =============================== VecAssign =============================== 00343 00344 inline void VecAssign(int n, double *u, double *v) 00345 { 00346 memcpy(v, u, n*sizeof(double)); 00347 } 00348 00349 // ------------------------------------------------------------------------ 00350 // Matrix functions: 00351 // 00352 double **MatAlloc(int n, int m); 00353 void MatFree(double **A, int n); 00354 void MatZero(int n, int m, double **A); 00355 void MatAssign(int n, int m, double **A, double **B); 00356 void MatAdd(int n, int m, double **A, double **B); 00357 void MatAdd(int n, int m, double **A, double **B, double **C); 00358 void MatSub(int n, int m, double **A, double **B); 00359 void MatSub(int n, int m, double **A, double **B, double **C); 00360 void MatMult(int n, int m, double f, double **A, double **B); 00361 void MatMult(int n, int m, int l, double **A, double **B, double **C); 00362 void MatVecMult(int n, int m, double **A, double *u, double *v); 00363 void MatVecMultT(int m, int n, double **A, double *u, double *v); 00364 void MatVecMultTAcc(double a, int m, int n, double **A, double *u, 00365 double *v); 00366 void MatTripBtCB(int m, int n, double **B, double **C, double w, 00367 double **K); 00368 00369 // ------------------------------------------------------------------------ 00370 // Skyline matrix functions: 00371 // 00372 double **SkylAlloc(int n, int *p); 00373 void SkylFree(double **A, int n, int *p); 00374 void SkylZero(int n, int *p, double **A); 00375 void SkylAssign(int n, int *p, double **A, double **B); 00376 void SkylAdd(int n, int *p, double **A, double **B); 00377 void SkylAdd(double a, int n, int *p, double **A, double **B); 00378 void SkylAdd(int n, int *p, double **A, double **B, double **C); 00379 void SkylSub(int n, int *p, double **A, double **B); 00380 void SkylSub(int n, int *p, double **A, double **B, double **C); 00381 void SkylMult(double a, int n, int *p, double **A); 00382 void SkylMult(double a, int n, int *p, double **A, double **B); 00383 void SkylMultAcc(double a, int n, int *p, double **A, double **B); 00384 void SkylVecMult(int n, int *p, double **A, double *u, double *v); 00385 int CroutSolver(int op, double **A, double *b, int n, int *p); 00386 00387 #endif