123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676 |
- /*****************************************************************************
- *
- * MODULE: Grass numerical math interface
- * AUTHOR(S): Soeren Gebbert, Berlin (GER) Dec 2006
- * soerengebbert <at> googlemail <dot> com
- *
- * PURPOSE: grass blas implementation
- * part of the gmath library
- *
- * COPYRIGHT: (C) 2010 by the GRASS Development Team
- *
- * This program is free software under the GNU General Public
- * License (>=v2). Read the file COPYING that comes with GRASS
- * for details.
- *
- *****************************************************************************/
- #include <math.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <grass/gis.h>
- #include <grass/gmath.h>
- /* **************************************************************** */
- /* *************** D O U B L E ************************************ */
- /* **************************************************************** */
- /*!
- * \brief Compute the dot product of vector x and y
- *
- * \f[ a = {\bf x}^T {\bf y} \f]
- *
- * The functions creates its own parallel OpenMP region.
- * It can be called within a parallel OpenMP region if nested parallelism is supported
- * by the compiler.
- *
- * \param x (double *)
- * \param y (double *)
- * \param value (double *) -- the return value
- * \param rows (int)
- * \return (void)
- *
- * */
- void G_math_d_x_dot_y(double *x, double *y, double *value, int rows)
- {
- int i;
- double s = 0.0;
- #pragma omp parallel for schedule (static) reduction(+:s)
- for (i = rows - 1; i >= 0; i--) {
- s += x[i] * y[i];
- }
- #pragma omp single
- {
- *value = s;
- }
- return;
- }
- /*!
- * \brief Compute the euclid norm of vector x
- *
- * \f[ a = ||{\bf x}||_2 \f]
- *
- * The functions creates its own parallel OpenMP region.
- * It can be called within a parallel OpenMP region if nested parallelism is supported
- * by the compiler.
- *
- * \param x (double *) -- the vector
- * \param value (double *) -- the return value
- * \param rows (int)
- * \return (void)
- *
- * */
- void G_math_d_euclid_norm(double *x, double *value, int rows)
- {
- int i;
- double s = 0.0;
- #pragma omp parallel for schedule (static) reduction(+:s)
- for (i = rows - 1; i >= 0; i--) {
- s += x[i] * x[i];
- }
- #pragma omp single
- {
- *value = sqrt(s);
- }
- return;
- }
- /*!
- * \brief Compute the asum norm of vector x
- *
- * \f[ a = ||{\bf x}||_1 \f]
- *
- * The functions creates its own parallel OpenMP region.
- * It can be called within a parallel OpenMP region if nested parallelism is supported
- * by the compiler.
- *
- * \param x (double *)-- the vector
- * \param value (double *) -- the return value
- * \param rows (int)
- * \return (void)
- *
- * */
- void G_math_d_asum_norm(double *x, double *value, int rows)
- {
- int i = 0;
- double s = 0.0;
- #pragma omp parallel for schedule (static) reduction(+:s)
- for (i = rows - 1; i >= 0; i--) {
- s += fabs(x[i]);
- }
- #pragma omp single
- {
- *value = s;
- }
- return;
- }
- /*!
- * \brief Compute the maximum norm of vector x
- *
- * \f[ a = ||{\bf x}||_\infty \f]
- *
- * This function is not multi-threaded
- *
- * \param x (double *)-- the vector
- * \param value (double *) -- the return value
- * \param rows (int)
- * \return (void)
- *
- * */
- void G_math_d_max_norm(double *x, double *value, int rows)
- {
- int i;
- double max = 0.0;
- max = fabs(x[rows - 1]);
- for (i = rows - 2; i >= 0; i--) {
- if (max < fabs(x[i]))
- max = fabs(x[i]);
- }
- *value = max;
- }
- /*!
- * \brief Scales vectors x and y with the scalars a and b and adds them
- *
- * \f[ {\bf z} = a{\bf x} + b{\bf y} \f]
- *
- * This function is multi-threaded with OpenMP and can be called within a parallel OpenMP region.
- *
- * \param x (double *)
- * \param y (double *)
- * \param z (double *)
- * \param a (double)
- * \param b (double)
- * \param rows (int)
- * \return (void)
- *
- * */
- void G_math_d_ax_by(double *x, double *y, double *z, double a, double b,
- int rows)
- {
- int i;
- /*find specific cases */
- if (b == 0.0) {
- #pragma omp for schedule (static)
- for (i = rows - 1; i >= 0; i--) {
- z[i] = a * x[i];
- }
- }
- else if ((a == 1.0) && (b == 1.0)) {
- #pragma omp for schedule (static)
- for (i = rows - 1; i >= 0; i--) {
- z[i] = x[i] + y[i];
- }
- }
- else if ((a == 1.0) && (b == -1.0)) {
- #pragma omp for schedule (static)
- for (i = rows - 1; i >= 0; i--) {
- z[i] = x[i] - y[i];
- }
- }
- else if (a == b) {
- #pragma omp for schedule (static)
- for (i = rows - 1; i >= 0; i--) {
- z[i] = a * (x[i] + y[i]);
- }
- }
- else if (b == -1.0) {
- #pragma omp for schedule (static)
- for (i = rows - 1; i >= 0; i--) {
- z[i] = a * x[i] - y[i];
- }
- }
- else if (b == 1.0) {
- #pragma omp for schedule (static)
- for (i = rows - 1; i >= 0; i--) {
- z[i] = a * x[i] + y[i];
- }
- }
- else {
- #pragma omp for schedule (static)
- for (i = rows - 1; i >= 0; i--) {
- z[i] = a * x[i] + b * y[i];
- }
- }
- return;
- }
- /*!
- * \brief Copy the vector x to y
- *
- * \f[ {\bf y} = {\bf x} \f]
- *
- * This function is not multi-threaded
- *
- * \param x (double *)
- * \param y (double *)
- * \param rows (int)
- *
- * */
- void G_math_d_copy(double *x, double *y, int rows)
- {
- y = memcpy(y, x, rows * sizeof(double));
- return;
- }
- /* **************************************************************** */
- /* *************** F L O A T ************************************** */
- /* **************************************************************** */
- /*!
- * \brief Compute the dot product of vector x and y
- *
- * \f[ a = {\bf x}^T {\bf y} \f]
- *
- * The functions creates its own parallel OpenMP region.
- * It can be called within a parallel OpenMP region if nested parallelism is supported
- * by the compiler.
- *
- * \param x (float *)
- * \param y (float *)
- * \param value (float *) -- the return value
- * \param rows (int)
- * \return (void)
- *
- * */
- void G_math_f_x_dot_y(float *x, float *y, float *value, int rows)
- {
- int i;
- float s = 0.0;
- #pragma omp parallel for schedule (static) reduction(+:s)
- for (i = rows - 1; i >= 0; i--) {
- s += x[i] * y[i];
- }
- #pragma omp single
- {
- *value = s;
- }
- return;
- }
- /*!
- * \brief Compute the euclid norm of vector x
- *
- * \f[ a = ||{\bf x}||_2 \f]
- *
- * The functions creates its own parallel OpenMP region.
- * It can be called within a parallel OpenMP region if nested parallelism is supported
- * by the compiler.
- *
- * \param x (double *) -- the vector
- * \param value (float *) -- the return value
- * \param rows (int)
- * \return (void)
- *
- * */
- void G_math_f_euclid_norm(float *x, float *value, int rows)
- {
- int i;
- float s = 0.0;
- #pragma omp parallel for schedule (static) reduction(+:s)
- for (i = rows - 1; i >= 0; i--) {
- s += x[i] * x[i];
- }
- #pragma omp single
- {
- *value = sqrt(s);
- }
- return;
- }
- /*!
- * \brief Compute the asum norm of vector x
- *
- * \f[ a = ||{\bf x}||_1 \f]
- *
- * The functions creates its own parallel OpenMP region.
- * It can be called within a parallel OpenMP region if nested parallelism is supported
- * by the compiler.
- *
- * \param x (float *)-- the vector
- * \param value (float *) -- the return value
- * \param rows (int)
- * \return (void)
- *
- * */
- void G_math_f_asum_norm(float *x, float *value, int rows)
- {
- int i;
- int count = 0;
- float s = 0.0;
- #pragma omp parallel for schedule (static) private(i) reduction(+:s, count)
- for (i = 0; i < rows; i++) {
- s += fabs(x[i]);
- count++;
- }
- #pragma omp single
- {
- *value = s;
- }
- return;
- }
- /*!
- * \brief Compute the maximum norm of vector x
- *
- * \f[ a = ||{\bf x}||_\infty \f]
- *
- * This function is not multi-threaded
- *
- * \param x (float *)-- the vector
- * \param value (float *) -- the return value
- * \param rows (int)
- * \return (void)
- *
- * */
- void G_math_f_max_norm(float *x, float *value, int rows)
- {
- int i;
- float max = 0.0;
- max = fabs(x[rows - 1]);
- for (i = rows - 2; i >= 0; i--) {
- if (max < fabs(x[i]))
- max = fabs(x[i]);
- }
- *value = max;
- return;
- }
- /*!
- * \brief Scales vectors x and y with the scalars a and b and adds them
- *
- * \f[ {\bf z} = a{\bf x} + b{\bf y} \f]
- *
- * This function is multi-threaded with OpenMP and can be called within a parallel OpenMP region.
- *
- * \param x (float *)
- * \param y (float *)
- * \param z (float *)
- * \param a (float)
- * \param b (float)
- * \param rows (int)
- * \return (void)
- *
- * */
- void G_math_f_ax_by(float *x, float *y, float *z, float a, float b, int rows)
- {
- int i;
- /*find specific cases */
- if (b == 0.0) {
- #pragma omp for schedule (static)
- for (i = rows - 1; i >= 0; i--) {
- z[i] = a * x[i];
- }
- }
- else if ((a == 1.0) && (b == 1.0)) {
- #pragma omp for schedule (static)
- for (i = rows - 1; i >= 0; i--) {
- z[i] = x[i] + y[i];
- }
- }
- else if ((a == 1.0) && (b == -1.0)) {
- #pragma omp for schedule (static)
- for (i = rows - 1; i >= 0; i--) {
- z[i] = x[i] - y[i];
- }
- }
- else if (a == b) {
- #pragma omp for schedule (static)
- for (i = rows - 1; i >= 0; i--) {
- z[i] = a * (x[i] + y[i]);
- }
- }
- else if (b == -1.0) {
- #pragma omp for schedule (static)
- for (i = rows - 1; i >= 0; i--) {
- z[i] = a * x[i] - y[i];
- }
- }
- else if (b == 1.0) {
- #pragma omp for schedule (static)
- for (i = rows - 1; i >= 0; i--) {
- z[i] = a * x[i] + y[i];
- }
- }
- else {
- #pragma omp for schedule (static)
- for (i = rows - 1; i >= 0; i--) {
- z[i] = a * x[i] + b * y[i];
- }
- }
- return;
- }
- /*!
- * \brief Copy the vector x to y
- *
- * \f[ {\bf y} = {\bf x} \f]
- *
- * This function is not multi-threaded
- *
- * \param x (float *)
- * \param y (float *)
- * \param rows (int)
- *
- * */
- void G_math_f_copy(float *x, float *y, int rows)
- {
- y = memcpy(y, x, rows * sizeof(float));
- return;
- }
- /* **************************************************************** */
- /* *************** I N T E G E R ********************************** */
- /* **************************************************************** */
- /*!
- * \brief Compute the dot product of vector x and y
- *
- * \f[ a = {\bf x}^T {\bf y} \f]
- *
- * The functions creates its own parallel OpenMP region.
- * It can be called within a parallel OpenMP region if nested parallelism is supported
- * by the compiler.
- *
- * \param x (int *)
- * \param y (int *)
- * \param value (double *) -- the return value
- * \param rows (int)
- * \return (void)
- *
- * */
- void G_math_i_x_dot_y(int *x, int *y, double *value, int rows)
- {
- int i;
- double s = 0.0;
- #pragma omp parallel for schedule (static) reduction(+:s)
- for (i = rows - 1; i >= 0; i--) {
- s += x[i] * y[i];
- }
- #pragma omp single
- {
- *value = s;
- }
- return;
- }
- /*!
- * \brief Compute the euclid norm of vector x
- *
- * \f[ a = ||{\bf x}||_2 \f]
- *
- * The functions creates its own parallel OpenMP region.
- * It can be called within a parallel OpenMP region if nested parallelism is supported
- * by the compiler.
- *
- * \param x (int *) -- the vector
- * \param value (double *) -- the return value
- * \param rows (int)
- * \return (void)
- *
- * */
- void G_math_i_euclid_norm(int *x, double *value, int rows)
- {
- int i;
- double s = 0.0;
- #pragma omp parallel for schedule (static) reduction(+:s)
- for (i = rows - 1; i >= 0; i--) {
- s += x[i] * x[i];
- }
- #pragma omp single
- {
- *value = sqrt(s);
- }
- return;
- }
- /*!
- * \brief Compute the asum norm of vector x
- *
- * \f[ a = ||{\bf x}||_1 \f]
- *
- * The functions creates its own parallel OpenMP region.
- * It can be called within a parallel OpenMP region if nested parallelism is supported
- * by the compiler.
- *
- * \param x (int *)-- the vector
- * \param value (double *) -- the return value
- * \param rows (int)
- * \return (void)
- *
- * */
- void G_math_i_asum_norm(int *x, double *value, int rows)
- {
- int i;
- double s = 0.0;
- #pragma omp parallel for schedule (static) reduction(+:s)
- for (i = rows - 1; i >= 0; i--) {
- s += (double)abs(x[i]);
- }
- #pragma omp single
- {
- *value = s;
- }
- return;
- }
- /*!
- * \brief Compute the maximum norm of vector x
- *
- * \f[ a = ||{\bf x}||_\infty \f]
- *
- * This function is not multi-threaded
- *
- * \param x (int *)-- the vector
- * \param value (int *) -- the return value
- * \param rows (int)
- * \return (void)
- *
- * */
- void G_math_i_max_norm(int *x, int *value, int rows)
- {
- int i;
- int max = 0.0;
- max = abs(x[rows - 1]);
- for (i = rows - 2; i >= 0; i--) {
- if (max < abs(x[i]))
- max = abs(x[i]);
- }
- *value = max;
- }
- /*!
- * \brief Scales vectors x and y with the scalars a and b and adds them
- *
- * \f[ {\bf z} = a{\bf x} + b{\bf y} \f]
- *
- * This function is multi-threaded with OpenMP and can be called within a parallel OpenMP region.
- *
- * \param x (int *)
- * \param y (int *)
- * \param z (int *)
- * \param a (int)
- * \param b (int)
- * \param rows (int)
- * \return (void)
- *
- * */
- void G_math_i_ax_by(int *x, int *y, int *z, int a, int b, int rows)
- {
- int i;
- /*find specific cases */
- if (b == 0.0) {
- #pragma omp for schedule (static)
- for (i = rows - 1; i >= 0; i--) {
- z[i] = a * x[i];
- }
- }
- else if ((a == 1.0) && (b == 1.0)) {
- #pragma omp for schedule (static)
- for (i = rows - 1; i >= 0; i--) {
- z[i] = x[i] + y[i];
- }
- }
- else if ((a == 1.0) && (b == -1.0)) {
- #pragma omp for schedule (static)
- for (i = rows - 1; i >= 0; i--) {
- z[i] = x[i] - y[i];
- }
- }
- else if (a == b) {
- #pragma omp for schedule (static)
- for (i = rows - 1; i >= 0; i--) {
- z[i] = a * (x[i] + y[i]);
- }
- }
- else if (b == -1.0) {
- #pragma omp for schedule (static)
- for (i = rows - 1; i >= 0; i--) {
- z[i] = a * x[i] - y[i];
- }
- }
- else if (b == 1.0) {
- #pragma omp for schedule (static)
- for (i = rows - 1; i >= 0; i--) {
- z[i] = a * x[i] + y[i];
- }
- }
- else {
- #pragma omp for schedule (static)
- for (i = rows - 1; i >= 0; i--) {
- z[i] = a * x[i] + b * y[i];
- }
- }
- return;
- }
- /*!
- * \brief Copy the vector x to y
- *
- * \f[ {\bf y} = {\bf x} \f]
- *
- * This function is not multi-threaded
- *
- * \param x (int *)
- * \param y (int *)
- * \param rows (int)
- *
- * */
- void G_math_i_copy(int *x, int *y, int rows)
- {
- y = memcpy(y, x, rows * sizeof(int));
- return;
- }
|