|
GRASS Programmer's Manual
6.5.svn(2012)-r51648
|
00001 00002 /***************************************************************************** 00003 * 00004 * MODULE: Grass Gmath Library 00005 * AUTHOR(S): Soeren Gebbert, Berlin (GER) Dec 2007 00006 * soerengebbert <at> gmx <dot> de 00007 * 00008 * PURPOSE: blas level 1 like functions 00009 * part of the gmath library 00010 * 00011 * COPYRIGHT: (C) 2000 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 00027 /* **************************************************************** */ 00028 /* *************** D O U B L E ************************************ */ 00029 /* **************************************************************** */ 00030 00047 void G_math_d_x_dot_y(double *x, double *y, double *value, int rows) 00048 { 00049 int i; 00050 00051 double s = 0.0; 00052 00053 #pragma omp parallel for schedule (static) reduction(+:s) 00054 for (i = rows - 1; i >= 0; i--) { 00055 s += x[i] * y[i]; 00056 } 00057 #pragma omp single 00058 { 00059 *value = s; 00060 } 00061 return; 00062 } 00063 00079 void G_math_d_euclid_norm(double *x, double *value, int rows) 00080 { 00081 int i; 00082 00083 double s = 0.0; 00084 00085 #pragma omp parallel for schedule (static) reduction(+:s) 00086 for (i = rows - 1; i >= 0; i--) { 00087 s += x[i] * x[i]; 00088 } 00089 #pragma omp single 00090 { 00091 *value = sqrt(s); 00092 } 00093 return; 00094 } 00095 00111 void G_math_d_asum_norm(double *x, double *value, int rows) 00112 { 00113 int i = 0; 00114 00115 double s = 0.0; 00116 00117 #pragma omp parallel for schedule (static) reduction(+:s) 00118 for (i = rows - 1; i >= 0; i--) { 00119 s += fabs(x[i]); 00120 } 00121 #pragma omp single 00122 { 00123 *value = s; 00124 } 00125 return; 00126 } 00127 00141 void G_math_d_max_norm(double *x, double *value, int rows) 00142 { 00143 int i; 00144 00145 double max = 0.0; 00146 00147 max = fabs(x[rows - 1]); 00148 for (i = rows - 2; i >= 0; i--) { 00149 if (max < fabs(x[i])) 00150 max = fabs(x[i]); 00151 } 00152 00153 *value = max; 00154 } 00155 00172 void G_math_d_ax_by(double *x, double *y, double *z, double a, double b, 00173 int rows) 00174 { 00175 int i; 00176 00177 /*find specific cases */ 00178 if (b == 0.0) { 00179 #pragma omp for schedule (static) 00180 for (i = rows - 1; i >= 0; i--) { 00181 z[i] = a * x[i]; 00182 } 00183 } 00184 else if ((a == 1.0) && (b == 1.0)) { 00185 #pragma omp for schedule (static) 00186 for (i = rows - 1; i >= 0; i--) { 00187 z[i] = x[i] + y[i]; 00188 } 00189 } 00190 else if ((a == 1.0) && (b == -1.0)) { 00191 #pragma omp for schedule (static) 00192 for (i = rows - 1; i >= 0; i--) { 00193 z[i] = x[i] - y[i]; 00194 } 00195 } 00196 else if (a == b) { 00197 #pragma omp for schedule (static) 00198 for (i = rows - 1; i >= 0; i--) { 00199 z[i] = a * (x[i] + y[i]); 00200 } 00201 } 00202 else if (b == -1.0) { 00203 #pragma omp for schedule (static) 00204 for (i = rows - 1; i >= 0; i--) { 00205 z[i] = a * x[i] - y[i]; 00206 } 00207 } 00208 else if (b == 1.0) { 00209 #pragma omp for schedule (static) 00210 for (i = rows - 1; i >= 0; i--) { 00211 z[i] = a * x[i] + y[i]; 00212 } 00213 } 00214 else { 00215 #pragma omp for schedule (static) 00216 for (i = rows - 1; i >= 0; i--) { 00217 z[i] = a * x[i] + b * y[i]; 00218 } 00219 } 00220 00221 return; 00222 } 00223 00236 void G_math_d_copy(double *x, double *y, int rows) 00237 { 00238 y = memcpy(y, x, rows * sizeof(double)); 00239 00240 return; 00241 } 00242 00243 /* **************************************************************** */ 00244 /* *************** F L O A T ************************************** */ 00245 /* **************************************************************** */ 00246 00263 void G_math_f_x_dot_y(float *x, float *y, float *value, int rows) 00264 { 00265 int i; 00266 00267 float s = 0.0; 00268 00269 #pragma omp parallel for schedule (static) reduction(+:s) 00270 for (i = rows - 1; i >= 0; i--) { 00271 s += x[i] * y[i]; 00272 } 00273 #pragma omp single 00274 { 00275 *value = s; 00276 } 00277 return; 00278 } 00279 00295 void G_math_f_euclid_norm(float *x, float *value, int rows) 00296 { 00297 int i; 00298 00299 float s = 0.0; 00300 00301 #pragma omp parallel for schedule (static) reduction(+:s) 00302 for (i = rows - 1; i >= 0; i--) { 00303 s += x[i] * x[i]; 00304 } 00305 #pragma omp single 00306 { 00307 *value = sqrt(s); 00308 } 00309 return; 00310 } 00311 00327 void G_math_f_asum_norm(float *x, float *value, int rows) 00328 { 00329 int i; 00330 00331 int count = 0; 00332 00333 float s = 0.0; 00334 00335 #pragma omp parallel for schedule (static) private(i) reduction(+:s, count) 00336 for (i = 0; i < rows; i++) { 00337 s += fabs(x[i]); 00338 count++; 00339 } 00340 #pragma omp single 00341 { 00342 *value = s; 00343 } 00344 return; 00345 } 00346 00360 void G_math_f_max_norm(float *x, float *value, int rows) 00361 { 00362 int i; 00363 00364 float max = 0.0; 00365 00366 max = fabs(x[rows - 1]); 00367 for (i = rows - 2; i >= 0; i--) { 00368 if (max < fabs(x[i])) 00369 max = fabs(x[i]); 00370 } 00371 *value = max; 00372 return; 00373 } 00374 00391 void G_math_f_ax_by(float *x, float *y, float *z, float a, float b, int rows) 00392 { 00393 int i; 00394 00395 /*find specific cases */ 00396 if (b == 0.0) { 00397 #pragma omp for schedule (static) 00398 for (i = rows - 1; i >= 0; i--) { 00399 z[i] = a * x[i]; 00400 } 00401 } 00402 else if ((a == 1.0) && (b == 1.0)) { 00403 #pragma omp for schedule (static) 00404 for (i = rows - 1; i >= 0; i--) { 00405 z[i] = x[i] + y[i]; 00406 } 00407 } 00408 else if ((a == 1.0) && (b == -1.0)) { 00409 #pragma omp for schedule (static) 00410 for (i = rows - 1; i >= 0; i--) { 00411 z[i] = x[i] - y[i]; 00412 } 00413 } 00414 else if (a == b) { 00415 #pragma omp for schedule (static) 00416 for (i = rows - 1; i >= 0; i--) { 00417 z[i] = a * (x[i] + y[i]); 00418 } 00419 } 00420 else if (b == -1.0) { 00421 #pragma omp for schedule (static) 00422 for (i = rows - 1; i >= 0; i--) { 00423 z[i] = a * x[i] - y[i]; 00424 } 00425 } 00426 else if (b == 1.0) { 00427 #pragma omp for schedule (static) 00428 for (i = rows - 1; i >= 0; i--) { 00429 z[i] = a * x[i] + y[i]; 00430 } 00431 } 00432 else { 00433 #pragma omp for schedule (static) 00434 for (i = rows - 1; i >= 0; i--) { 00435 z[i] = a * x[i] + b * y[i]; 00436 } 00437 } 00438 00439 return; 00440 } 00441 00454 void G_math_f_copy(float *x, float *y, int rows) 00455 { 00456 y = memcpy(y, x, rows * sizeof(float)); 00457 00458 return; 00459 } 00460 00461 /* **************************************************************** */ 00462 /* *************** I N T E G E R ********************************** */ 00463 /* **************************************************************** */ 00464 00481 void G_math_i_x_dot_y(int *x, int *y, double *value, int rows) 00482 { 00483 int i; 00484 00485 double s = 0.0; 00486 00487 #pragma omp parallel for schedule (static) reduction(+:s) 00488 for (i = rows - 1; i >= 0; i--) { 00489 s += x[i] * y[i]; 00490 } 00491 #pragma omp single 00492 { 00493 *value = s; 00494 } 00495 return; 00496 } 00497 00513 void G_math_i_euclid_norm(int *x, double *value, int rows) 00514 { 00515 int i; 00516 00517 double s = 0.0; 00518 00519 #pragma omp parallel for schedule (static) reduction(+:s) 00520 for (i = rows - 1; i >= 0; i--) { 00521 s += x[i] * x[i]; 00522 } 00523 #pragma omp single 00524 { 00525 *value = sqrt(s); 00526 } 00527 return; 00528 } 00529 00545 void G_math_i_asum_norm(int *x, double *value, int rows) 00546 { 00547 int i; 00548 00549 double s = 0.0; 00550 00551 #pragma omp parallel for schedule (static) reduction(+:s) 00552 for (i = rows - 1; i >= 0; i--) { 00553 s += fabs(x[i]); 00554 } 00555 #pragma omp single 00556 { 00557 *value = s; 00558 } 00559 return; 00560 } 00561 00575 void G_math_i_max_norm(int *x, int *value, int rows) 00576 { 00577 int i; 00578 00579 int max = 0.0; 00580 00581 max = fabs(x[rows - 1]); 00582 for (i = rows - 2; i >= 0; i--) { 00583 if (max < fabs(x[i])) 00584 max = fabs(x[i]); 00585 } 00586 00587 *value = max; 00588 } 00589 00606 void G_math_i_ax_by(int *x, int *y, int *z, int a, int b, int rows) 00607 { 00608 int i; 00609 00610 /*find specific cases */ 00611 if (b == 0.0) { 00612 #pragma omp for schedule (static) 00613 for (i = rows - 1; i >= 0; i--) { 00614 z[i] = a * x[i]; 00615 } 00616 } 00617 else if ((a == 1.0) && (b == 1.0)) { 00618 #pragma omp for schedule (static) 00619 for (i = rows - 1; i >= 0; i--) { 00620 z[i] = x[i] + y[i]; 00621 } 00622 } 00623 else if ((a == 1.0) && (b == -1.0)) { 00624 #pragma omp for schedule (static) 00625 for (i = rows - 1; i >= 0; i--) { 00626 z[i] = x[i] - y[i]; 00627 } 00628 } 00629 else if (a == b) { 00630 #pragma omp for schedule (static) 00631 for (i = rows - 1; i >= 0; i--) { 00632 z[i] = a * (x[i] + y[i]); 00633 } 00634 } 00635 else if (b == -1.0) { 00636 #pragma omp for schedule (static) 00637 for (i = rows - 1; i >= 0; i--) { 00638 z[i] = a * x[i] - y[i]; 00639 } 00640 } 00641 else if (b == 1.0) { 00642 #pragma omp for schedule (static) 00643 for (i = rows - 1; i >= 0; i--) { 00644 z[i] = a * x[i] + y[i]; 00645 } 00646 } 00647 else { 00648 #pragma omp for schedule (static) 00649 for (i = rows - 1; i >= 0; i--) { 00650 z[i] = a * x[i] + b * y[i]; 00651 } 00652 } 00653 00654 return; 00655 } 00656 00669 void G_math_i_copy(int *x, int *y, int rows) 00670 { 00671 y = memcpy(y, x, rows * sizeof(int)); 00672 00673 return; 00674 }