GRASS GIS 7 Programmer's Manual
7.8.5(2020)-exported
|
Go to the documentation of this file.
21 #include <grass/gmath.h>
22 #include <grass/gis.h>
40 "Add sparse vector %p to the sparse linear equation system at row %i\n",
60 G_math_spvector **spmatrix;
62 G_debug(4,
"Allocate memory for a sparse matrix with %i rows\n", rows);
64 spmatrix = (G_math_spvector **) G_calloc(rows,
sizeof(G_math_spvector *));
78 G_math_spvector *spvector;
80 G_debug(4,
"Allocate memory for a sparse vector with %i cols\n", cols);
82 spvector = (G_math_spvector *) G_calloc(1,
sizeof(G_math_spvector));
84 spvector->cols = cols;
85 spvector->index = (
unsigned int *)G_calloc(cols,
sizeof(
unsigned int));
86 spvector->values = (
double *)G_calloc(cols,
sizeof(
double));
101 if (spvector->values)
126 for (i = 0; i < rows; i++)
150 for (i = 0; i < rows; i++) {
151 for (j = 0; j < rows; j++) {
153 for (k = 0; k < Asp[i]->cols; k++) {
154 if (Asp[i]->index[k] == j) {
155 fprintf(stdout,
"%4.5f ", Asp[i]->values[k]);
160 fprintf(stdout,
"%4.5f ", 0.0);
162 fprintf(stdout,
"\n");
187 #pragma omp parallel for schedule (static) private(i, j)
188 for (i = 0; i < rows; i++) {
189 for (j = 0; j < Asp[i]->cols; j++) {
190 A[i][Asp[i]->index[j]] = Asp[i]->values[j];
229 for (i = 0; i < rows; i++) {
230 for (j = 0; j < Asp[i]->cols; j++) {
231 if(Asp[i]->index[j] == i) {
232 A[i][0] = Asp[i]->values[j];
233 }
else if (Asp[i]->index[j] > i) {
234 A[i][Asp[i]->index[j] - i] = Asp[i]->values[j];
257 int nonull,
count = 0;
259 G_math_spvector **Asp =
NULL;
263 #pragma omp parallel for schedule (static) private(i, j, nonull, count)
264 for (i = 0; i < rows; i++) {
267 for (j = 0; j < rows; j++) {
268 if (A[i][j] > epsilon)
275 for (j = 0; j < rows; j++) {
276 if (A[i][j] > epsilon) {
278 v->values[
count] = A[i][j];
308 int nonull,
count = 0;
310 G_math_spvector **Asp =
NULL;
314 for (i = 0; i < rows; i++) {
317 for (j = 0; j < bandwidth; j++) {
318 if (A[i][j] > epsilon)
327 if (A[i][0] > epsilon) {
329 v->values[
count] = A[i][0];
333 for (j = 1; j < bandwidth; j++) {
334 if (A[i][j] > epsilon && i + j < rows) {
335 v->index[
count] = i + j;
336 v->values[
count] = A[i][j];
369 #pragma omp for schedule (static) private(i, j, tmp)
370 for (i = 0; i < rows; i++) {
372 for (j = 0; j < Asp[i]->cols; j++) {
373 tmp += Asp[i]->values[j] *
x[Asp[i]->index[j]];
int G_math_add_spvector(G_math_spvector **Asp, G_math_spvector *spvector, int row)
Adds a sparse vector to a sparse matrix at position row.
G_math_spvector ** G_math_sband_matrix_to_Asp(double **A, int rows, int bandwidth, double epsilon)
Convert a symmetric band matrix into a sparse matrix.
G_math_spvector ** G_math_alloc_spmatrix(int rows)
Allocate memory for a sparse matrix.
void G_math_print_spmatrix(G_math_spvector **Asp, int rows)
print the sparse matrix Asp to stdout
double ** G_alloc_matrix(int rows, int cols)
Matrix memory allocation.
G_math_spvector * G_math_alloc_spvector(int cols)
Allocate memory for a sparse vector.
G_math_spvector ** G_math_A_to_Asp(double **A, int rows, double epsilon)
Convert a quadratic matrix into a sparse matrix.
void G_free(void *buf)
Free allocated memory.
double ** G_math_Asp_to_sband_matrix(G_math_spvector **Asp, int rows, int bandwidth)
Convert a symmetric sparse matrix into a symmetric band matrix.
double ** G_math_Asp_to_A(G_math_spvector **Asp, int rows)
Convert a sparse matrix into a quadratic matrix.
int G_debug(int level, const char *msg,...)
Print debugging message.
void G_math_free_spvector(G_math_spvector *spvector)
Release the memory of the sparse vector.
void G_math_Ax_sparse(G_math_spvector **Asp, double *x, double *y, int rows)
Compute the matrix - vector product of sparse matrix **Asp and vector x.
void G_math_free_spmatrix(G_math_spvector **Asp, int rows)
Release the memory of the sparse matrix.