GRASS 8 Programmer's Manual 8.6.0dev(2026)-4bb960b182
Loading...
Searching...
No Matches
blas_level_3.c
Go to the documentation of this file.
1/*****************************************************************************
2 *
3 * MODULE: Grass numerical math interface
4 * AUTHOR(S): Soeren Gebbert, Berlin (GER) Dec 2006
5 * soerengebbert <at> googlemail <dot> com
6 *
7 * PURPOSE: grass blas implementation
8 * part of the gmath library
9 *
10 * SPDX-FileCopyrightText: 2010 GRASS Development Team
11 * SPDX-License-Identifier: GPL-2.0-or-later
12 *
13 *****************************************************************************/
14
15#include <math.h>
16#include <unistd.h>
17#include <stdio.h>
18#include <string.h>
19#include <stdlib.h>
20#include <grass/gis.h>
21#include <grass/gmath.h>
22
23/*!
24 * \brief Add two matrices and scale matrix A with the scalar a
25 *
26 * \f[ {\bf C} = a {\bf A} + {\bf B} \f]
27 *
28 * In case B == NULL, matrix A will be scaled by scalar a. \n
29 * In case a == 1.0, a simple matrix addition is performed. \n
30 * In case a == -1.0 matrix A is subtracted from matrix B. \n
31 * The result is written into matrix C.
32 *
33 *
34 * This function is multi-threaded with OpenMP and can be called within a
35 * parallel OpenMP region.
36 *
37 * \param A (double **)
38 * \param B (double **) if NULL, matrix A is scaled by scalar a only
39 * \param a (double)
40 * \param C (double **)
41 * \param rows (int)
42 * \param cols (int)
43 * \return (void)
44 *
45 * */
46void G_math_d_aA_B(double **A, double **B, double a, double **C, int rows,
47 int cols)
48{
49 int i, j;
50
51 /*If B is null, scale the matrix A with th scalar a */
52 if (B == NULL) {
53#pragma omp for schedule(static) private(i, j)
54 for (i = rows - 1; i >= 0; i--)
55 for (j = cols - 1; j >= 0; j--)
56 C[i][j] = a * A[i][j];
57
58 return;
59 }
60
61 /*select special cases */
62 if (a == 1.0) {
63#pragma omp for schedule(static) private(i, j)
64 for (i = rows - 1; i >= 0; i--)
65 for (j = cols - 1; j >= 0; j--)
66 C[i][j] = A[i][j] + B[i][j];
67 }
68 else if (a == -1.0) {
69#pragma omp for schedule(static) private(i, j)
70 for (i = rows - 1; i >= 0; i--)
71 for (j = cols - 1; j >= 0; j--)
72 C[i][j] = B[i][j] - A[i][j];
73 }
74 else {
75#pragma omp for schedule(static) private(i, j)
76 for (i = rows - 1; i >= 0; i--)
77 for (j = cols - 1; j >= 0; j--)
78 C[i][j] = a * A[i][j] + B[i][j];
79 }
80
81 return;
82}
83
84/*!
85 * \brief Add two matrices and scale matrix A with the scalar a
86 *
87 * \f[ {\bf C} = a {\bf A} + {\bf B} \f]
88 *
89 * In case B == NULL, matrix A will be scaled by scalar a. \n
90 * In case a == 1.0, a simple matrix addition is performed. \n
91 * In case a == -1.0 matrix A is subtracted from matrix B. \n
92 * The result is written into matrix C.
93 *
94 *
95 *
96 * This function is multi-threaded with OpenMP and can be called within a
97 parallel OpenMP region.
98 *
99 * \param A (float **)
100 * \param B (float **) if NULL, matrix A is scaled by scalar a only
101 * \param a (float)
102 * \param C (float **)
103 * \param rows (int)
104 * \param cols (int)
105
106 * \return (void)
107 *
108 * */
109void G_math_f_aA_B(float **A, float **B, float a, float **C, int rows, int cols)
110{
111 int i, j;
112
113 /*If B is null, scale the matrix A with th scalar a */
114 if (B == NULL) {
115#pragma omp for schedule(static) private(i, j)
116 for (i = rows - 1; i >= 0; i--)
117 for (j = cols - 1; j >= 0; j--)
118 C[i][j] = a * A[i][j];
119 return;
120 }
121
122 /*select special cases */
123 if (a == 1.0) {
124#pragma omp for schedule(static) private(i, j)
125 for (i = rows - 1; i >= 0; i--)
126 for (j = cols - 1; j >= 0; j--)
127 C[i][j] = A[i][j] + B[i][j];
128 }
129 else if (a == -1.0) {
130#pragma omp for schedule(static) private(i, j)
131 for (i = rows - 1; i >= 0; i--)
132 for (j = cols - 1; j >= 0; j--)
133 C[i][j] = B[i][j] - A[i][j];
134 }
135 else {
136#pragma omp for schedule(static) private(i, j)
137 for (i = rows - 1; i >= 0; i--)
138 for (j = cols - 1; j >= 0; j--)
139 C[i][j] = a * A[i][j] + B[i][j];
140 }
141
142 return;
143}
144
145/*!
146 * \brief Matrix multiplication
147 *
148 * \f[ {\bf C} = {\bf A}{\bf B} \f]
149 *
150 * The result is written into matrix C.
151 *
152 * A must be of size rows_A * cols_A
153 * B must be of size rows_B * cols_B with rows_B == cols_A
154 * C must be of size rows_A * cols_B
155 *
156 *
157 * This function is multi-threaded with OpenMP and can be called within a
158 * parallel OpenMP region.
159 *
160 * \param A (double **)
161 * \param B (double **)
162 * \param C (double **)
163 * \param rows_A (int)
164 * \param cols_A (int)
165 * \param cols_B (int)
166 * \return (void)
167 *
168 * */
169void G_math_d_AB(double **A, double **B, double **C, int rows_A, int cols_A,
170 int cols_B)
171{
172 int i, j, k;
173
174#pragma omp for schedule(static) private(i, j, k)
175 for (i = 0; i < rows_A; i++) {
176 for (j = 0; j < cols_B; j++) {
177 C[i][j] = 0.0;
178 for (k = cols_A - 1; k >= 0; k--) {
179 C[i][j] += A[i][k] * B[k][j];
180 }
181 }
182 }
183
184 return;
185}
186
187/*!
188 * \brief Matrix multiplication
189 *
190 * \f[ {\bf C} = {\bf A}{\bf B} \f]
191 *
192 * The result is written into matrix C.
193 *
194 * A must be of size rows_A * cols_A
195 * B must be of size rows_B * cols_B with rows_B == cols_A
196 * C must be of size rows_A * cols_B
197 *
198 *
199 * This function is multi-threaded with OpenMP and can be called within a
200 * parallel OpenMP region.
201 *
202 * \param A (float **)
203 * \param B (float **)
204 * \param C (float **)
205 * \param rows_A (int)
206 * \param cols_A (int)
207 * \param cols_B (int)
208 * \return (void)
209 *
210 * */
211void G_math_f_AB(float **A, float **B, float **C, int rows_A, int cols_A,
212 int cols_B)
213{
214 int i, j, k;
215
216#pragma omp for schedule(static) private(i, j, k)
217 for (i = 0; i < rows_A; i++) {
218 for (j = 0; j < cols_B; j++) {
219 C[i][j] = 0.0;
220 for (k = cols_A - 1; k >= 0; k--) {
221 C[i][j] += A[i][k] * B[k][j];
222 }
223 }
224 }
225
226 return;
227}
void G_math_f_aA_B(float **A, float **B, float a, float **C, int rows, int cols)
Add two matrices and scale matrix A with the scalar a.
void G_math_f_AB(float **A, float **B, float **C, int rows_A, int cols_A, int cols_B)
Matrix multiplication.
void G_math_d_aA_B(double **A, double **B, double a, double **C, int rows, int cols)
Add two matrices and scale matrix A with the scalar a.
void G_math_d_AB(double **A, double **B, double **C, int rows_A, int cols_A, int cols_B)
Matrix multiplication.
#define NULL
Definition ccmath.h:32