GRASS Programmer's Manual  6.5.svn(2012)-r51648
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
blas_level_2.c
Go to the documentation of this file.
00001 
00002 /*****************************************************************************
00003  *
00004  * MODULE:       Grass PDE Numerical Library
00005  * AUTHOR(S):    Soeren Gebbert, Berlin (GER) Dec 2007
00006  *              soerengebbert <at> gmx <dot> de
00007  *               
00008  * PURPOSE:      linear equation system solvers
00009  *              part of the gpde library
00010  *               
00011  * COPYRIGHT:    (C) 2007 by the GRASS Development Team
00012  *
00013  *               This program is free software under the GNU General Public
00014  *               License (>=v2). Read the file COPYING that comes with GRASS
00015  *               for details.
00016  *
00017  *****************************************************************************/
00018 
00019 #include <math.h>
00020 #include <unistd.h>
00021 #include <stdio.h>
00022 #include <string.h>
00023 #include <stdlib.h>
00024 #include <grass/gmath.h>
00025 #include <grass/gis.h>
00026 #include <grass/gisdefs.h>
00027 
00028 #define EPSILON 0.00000000000000001
00029 
00045 void G_math_Ax_sparse(G_math_spvector ** Asp, double *x, double *y, int rows)
00046 {
00047     int i, j;
00048 
00049     double tmp;
00050 
00051 #pragma omp for schedule (static) private(i, j, tmp)
00052     for (i = 0; i < rows; i++) {
00053         tmp = 0;
00054         for (j = 0; j < Asp[i]->cols; j++) {
00055             tmp += Asp[i]->values[j] * x[Asp[i]->index[j]];
00056         }
00057         y[i] = tmp;
00058     }
00059     return;
00060 }
00061 
00079 void G_math_d_Ax(double **A, double *x, double *y, int rows, int cols)
00080 {
00081     int i, j;
00082 
00083     double tmp;
00084 
00085 #pragma omp for schedule (static) private(i, j, tmp)
00086     for (i = 0; i < rows; i++) {
00087         tmp = 0;
00088         for (j = cols - 1; j >= 0; j--) {
00089             tmp += A[i][j] * x[j];
00090         }
00091         y[i] = tmp;
00092     }
00093     return;
00094 }
00095 
00113 void G_math_f_Ax(float **A, float *x, float *y, int rows, int cols)
00114 {
00115     int i, j;
00116 
00117     float tmp;
00118 
00119 #pragma omp for schedule (static) private(i, j, tmp)
00120     for (i = 0; i < rows; i++) {
00121         tmp = 0;
00122         for (j = cols - 1; j >= 0; j--) {
00123             tmp += A[i][j] * x[j];
00124         }
00125         y[i] = tmp;
00126     }
00127     return;
00128 }
00129 
00147 void G_math_d_x_dyad_y(double *x, double *y, double **A, int rows, int cols)
00148 {
00149     int i, j;
00150 
00151 #pragma omp for schedule (static) private(i, j)
00152     for (i = 0; i < rows; i++) {
00153         for (j = cols - 1; j >= 0; j--) {
00154             A[i][j] = x[i] * y[j];
00155         }
00156     }
00157     return;
00158 }
00159 
00177 void G_math_f_x_dyad_y(float *x, float *y, float **A, int rows, int cols)
00178 {
00179     int i, j;
00180 
00181 #pragma omp for schedule (static) private(i, j)
00182     for (i = 0; i < rows; i++) {
00183         for (j = cols - 1; j >= 0; j--) {
00184             A[i][j] = x[i] * y[j];
00185         }
00186     }
00187     return;
00188 }
00189 
00211 void G_math_d_aAx_by(double **A, double *x, double *y, double a, double b,
00212                      double *z, int rows, int cols)
00213 {
00214     int i, j;
00215 
00216     double tmp;
00217 
00218     /*catch specific cases */
00219     if (a == b) {
00220 #pragma omp for schedule (static) private(i, j, tmp)
00221         for (i = 0; i < rows; i++) {
00222             tmp = 0;
00223             for (j = cols - 1; j >= 0; j--) {
00224                 tmp += A[i][j] * x[j] + y[j];
00225             }
00226             z[i] = a * tmp;
00227         }
00228     }
00229     else if (b == -1.0) {
00230 #pragma omp for schedule (static) private(i, j, tmp)
00231         for (i = 0; i < rows; i++) {
00232             tmp = 0;
00233             for (j = cols - 1; j >= 0; j--) {
00234                 tmp += a * A[i][j] * x[j] - y[j];
00235             }
00236             z[i] = tmp;
00237         }
00238     }
00239     else if (b == 0.0) {
00240 #pragma omp for schedule (static) private(i, j, tmp)
00241         for (i = 0; i < rows; i++) {
00242             tmp = 0;
00243             for (j = cols - 1; j >= 0; j--) {
00244                 tmp += A[i][j] * x[j];
00245             }
00246             z[i] = a * tmp;
00247         }
00248     }
00249     else if (a == -1.0) {
00250 #pragma omp for schedule (static) private(i, j, tmp)
00251         for (i = 0; i < rows; i++) {
00252             tmp = 0;
00253             for (j = cols - 1; j >= 0; j--) {
00254                 tmp += b * y[j] - A[i][j] * x[j];
00255             }
00256             z[i] = tmp;
00257         }
00258     }
00259     else {
00260 #pragma omp for schedule (static) private(i, j, tmp)
00261         for (i = 0; i < rows; i++) {
00262             tmp = 0;
00263             for (j = cols - 1; j >= 0; j--) {
00264                 tmp += a * A[i][j] * x[j] + b * y[j];
00265             }
00266             z[i] = tmp;
00267         }
00268     }
00269     return;
00270 }
00271 
00293 void G_math_f_aAx_by(float **A, float *x, float *y, float a, float b,
00294                      float *z, int rows, int cols)
00295 {
00296     int i, j;
00297 
00298     float tmp;
00299 
00300     /*catch specific cases */
00301     if (a == b) {
00302 #pragma omp for schedule (static) private(i, j, tmp)
00303         for (i = 0; i < rows; i++) {
00304             tmp = 0;
00305             for (j = cols - 1; j >= 0; j--) {
00306                 tmp += A[i][j] * x[j] + y[j];
00307             }
00308             z[i] = a * tmp;
00309         }
00310     }
00311     else if (b == -1.0) {
00312 #pragma omp for schedule (static) private(i, j, tmp)
00313         for (i = 0; i < rows; i++) {
00314             tmp = 0;
00315             for (j = cols - 1; j >= 0; j--) {
00316                 tmp += a * A[i][j] * x[j] - y[j];
00317             }
00318             z[i] = tmp;
00319         }
00320     }
00321     else if (b == 0.0) {
00322 #pragma omp for schedule (static) private(i, j, tmp)
00323         for (i = 0; i < rows; i++) {
00324             tmp = 0;
00325             for (j = cols - 1; j >= 0; j--) {
00326                 tmp += A[i][j] * x[j];
00327             }
00328             z[i] = a * tmp;
00329         }
00330     }
00331     else if (a == -1.0) {
00332 #pragma omp for schedule (static) private(i, j, tmp)
00333         for (i = 0; i < rows; i++) {
00334             tmp = 0;
00335             for (j = cols - 1; j >= 0; j--) {
00336                 tmp += b * y[j] - A[i][j] * x[j];
00337             }
00338             z[i] = tmp;
00339         }
00340     }
00341     else {
00342 #pragma omp for schedule (static) private(i, j, tmp)
00343         for (i = 0; i < rows; i++) {
00344             tmp = 0;
00345             for (j = cols - 1; j >= 0; j--) {
00346                 tmp += a * A[i][j] * x[j] + b * y[j];
00347             }
00348             z[i] = tmp;
00349         }
00350     }
00351     return;
00352 }
00353 
00354 
00355 
00371 int G_math_d_A_T(double **A, int rows)
00372 {
00373     int i, j;
00374 
00375     double tmp;
00376 
00377 #pragma omp for schedule (static) private(i, j, tmp)
00378     for (i = 0; i < rows; i++)
00379         for (j = 0; j < i; j++) {
00380             tmp = A[i][j];
00381 
00382             A[i][j] = A[j][i];
00383             A[j][i] = tmp;
00384         }
00385 
00386     return 0;
00387 }
00388 
00404 int G_math_f_A_T(float **A, int rows)
00405 {
00406     int i, j;
00407 
00408     float tmp;
00409 
00410 #pragma omp for schedule (static) private(i, j, tmp)
00411     for (i = 0; i < rows; i++)
00412         for (j = 0; j < i; j++) {
00413             tmp = A[i][j];
00414 
00415             A[i][j] = A[j][i];
00416             A[j][i] = tmp;
00417         }
00418 
00419     return 0;
00420 }