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];
211 int nonull,
count = 0;
213 G_math_spvector **Asp =
NULL;
217 #pragma omp parallel for schedule (static) private(i, j, nonull, count)
218 for (i = 0; i < rows; i++) {
221 for (j = 0; j < rows; j++) {
222 if (A[i][j] > epsilon)
229 for (j = 0; j < rows; j++) {
230 if (A[i][j] > epsilon) {
232 v->values[
count] = A[i][j];
void G_free(void *buf)
Free allocated memory.
G_math_spvector * G_math_alloc_spvector(int cols)
Allocate memory for a sparse vector.
double ** G_alloc_matrix(int rows, int cols)
Matrix memory allocation.
G_math_spvector ** G_math_A_to_Asp(double **A, int rows, double epsilon)
Convert a quadratic matrix into a sparse matrix.
G_math_spvector ** G_math_alloc_spmatrix(int rows)
Allocate memory for a sparse matrix.
void G_math_free_spvector(G_math_spvector *spvector)
Release the memory of the sparse vector.
void G_math_free_spmatrix(G_math_spvector **spmatrix, int rows)
Release the memory of the sparse matrix.
int G_debug(int level, const char *msg,...)
Print debugging message.
int G_math_add_spvector(G_math_spvector **Asp, G_math_spvector *spvector, int row)
Adds a sparse vector to a sparse linear equation system at position row.
void G_math_print_spmatrix(G_math_spvector **Asp, int rows)
print the sparse matrix Asp to stdout
double ** G_math_Asp_to_A(G_math_spvector **Asp, int rows)
Convert a sparse matrix into a quadratic matrix.