1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
|
/*****************************************************************************
*
* MODULE: Grass numerical math interface
* AUTHOR(S): Soeren Gebbert, Berlin (GER) Dec 2006
* soerengebbert <at> googlemail <dot> com
*
* PURPOSE: linear equation system solvers
* 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 <assert.h>
#include <stdlib.h>
#include <math.h>
#include <grass/gmath.h>
#include <grass/gis.h>
/*!
* \brief Adds a sparse vector to a sparse matrix at position row
*
* Return 1 for success and -1 for failure
*
* \param Asp G_math_spvector **
* \param spvector G_math_spvector *
* \param row int
* \return int 1 success, -1 failure
*
* */
int G_math_add_spvector(G_math_spvector **Asp, G_math_spvector *spvector,
int row)
{
if (Asp != NULL) {
G_debug(5,
"Add sparse vector %p to the sparse linear equation system at "
"row %i\n",
(void *)spvector, row);
Asp[row] = spvector;
}
else {
return -1;
}
return 1;
}
/*!
* \brief Allocate memory for a sparse matrix
*
* \param rows int
* \return G_math_spvector **
*
* */
G_math_spvector **G_math_alloc_spmatrix(int rows)
{
G_math_spvector **spmatrix;
G_debug(4, "Allocate memory for a sparse matrix with %i rows\n", rows);
spmatrix = (G_math_spvector **)G_calloc(rows, sizeof(G_math_spvector *));
return spmatrix;
}
/*!
* \brief Allocate memory for a sparse vector
*
* \param cols int
* \return G_math_spvector *
*
* */
G_math_spvector *G_math_alloc_spvector(int cols)
{
G_math_spvector *spvector;
G_debug(4, "Allocate memory for a sparse vector with %i cols\n", cols);
spvector = (G_math_spvector *)G_calloc(1, sizeof(G_math_spvector));
spvector->cols = cols;
spvector->index = (unsigned int *)G_calloc(cols, sizeof(unsigned int));
spvector->values = (double *)G_calloc(cols, sizeof(double));
return spvector;
}
/*!
* \brief Release the memory of the sparse vector
*
* \param spvector G_math_spvector *
* \return void
*
* */
void G_math_free_spvector(G_math_spvector *spvector)
{
if (spvector) {
if (spvector->values)
G_free(spvector->values);
if (spvector->index)
G_free(spvector->index);
G_free(spvector);
spvector = NULL;
}
return;
}
/*!
* \brief Release the memory of the sparse matrix
*
* \param Asp G_math_spvector **
* \param rows int
* \return void
*
* */
void G_math_free_spmatrix(G_math_spvector **Asp, int rows)
{
int i;
if (Asp) {
for (i = 0; i < rows; i++)
G_math_free_spvector(Asp[i]);
G_free(Asp);
Asp = NULL;
}
return;
}
/*!
*
* \brief print the sparse matrix Asp to stdout
*
*
* \param Asp (G_math_spvector **)
* \param rows (int)
* \return void
*
* */
void G_math_print_spmatrix(G_math_spvector **Asp, int rows)
{
int i, j, out;
unsigned int k;
for (i = 0; i < rows; i++) {
for (j = 0; j < rows; j++) {
out = 0;
for (k = 0; k < Asp[i]->cols; k++) {
if (Asp[i]->index[k] == (unsigned int)j) {
fprintf(stdout, "%4.5f ", Asp[i]->values[k]);
out = 1;
}
}
if (!out)
fprintf(stdout, "%4.5f ", 0.0);
}
fprintf(stdout, "\n");
}
return;
}
/*!
* \brief Convert a sparse matrix into a quadratic matrix
*
* This function is multi-threaded with OpenMP. It creates its own parallel
* OpenMP region.
*
* \param Asp (G_math_spvector **)
* \param rows (int)
* \return (double **)
*
* */
double **G_math_Asp_to_A(G_math_spvector **Asp, int rows)
{
int i;
unsigned int j;
double **A = NULL;
A = G_alloc_matrix(rows, rows);
#pragma omp parallel for schedule(static) private(i, j)
for (i = 0; i < rows; i++) {
for (j = 0; j < Asp[i]->cols; j++) {
A[i][Asp[i]->index[j]] = Asp[i]->values[j];
}
}
return A;
}
/*!
* \brief Convert a symmetric sparse matrix into a symmetric band matrix
*
\verbatim
Symmetric matrix with bandwidth of 3
5 2 1 0
2 5 2 1
1 2 5 2
0 1 2 5
will be converted into the band matrix
5 2 1
5 2 1
5 2 0
5 0 0
\endverbatim
* \param Asp (G_math_spvector **)
* \param rows (int)
* \param bandwidth (int)
* \return (double **) the resulting ymmetric band matrix [rows][bandwidth]
*
* */
double **G_math_Asp_to_sband_matrix(G_math_spvector **Asp, int rows,
int bandwidth)
{
unsigned int i, j;
double **A = NULL;
assert(rows >= 0 && bandwidth >= 0);
A = G_alloc_matrix(rows, bandwidth);
for (i = 0; i < (unsigned int)rows; i++) {
for (j = 0; j < Asp[i]->cols; j++) {
if (Asp[i]->index[j] == i) {
A[i][0] = Asp[i]->values[j];
}
else if (Asp[i]->index[j] > i) {
A[i][Asp[i]->index[j] - i] = Asp[i]->values[j];
}
}
}
return A;
}
/*!
* \brief Convert a quadratic matrix into a sparse matrix
*
* This function is multi-threaded with OpenMP. It creates its own parallel
* OpenMP region.
*
* \param A (double **)
* \param rows (int)
* \param epsilon (double) -- non-zero values are greater then epsilon
* \return (G_math_spvector **)
*
* */
G_math_spvector **G_math_A_to_Asp(double **A, int rows, double epsilon)
{
int i, j;
int nonull, count = 0;
G_math_spvector **Asp = NULL;
Asp = G_math_alloc_spmatrix(rows);
#pragma omp parallel for schedule(static) private(i, j, nonull, count)
for (i = 0; i < rows; i++) {
nonull = 0;
/*Count the number of non zero entries */
for (j = 0; j < rows; j++) {
if (A[i][j] > epsilon)
nonull++;
}
/*Allocate the sparse vector and insert values */
G_math_spvector *v = G_math_alloc_spvector(nonull);
count = 0;
for (j = 0; j < rows; j++) {
if (A[i][j] > epsilon) {
v->index[count] = j;
v->values[count] = A[i][j];
count++;
}
}
/*Add vector to sparse matrix */
G_math_add_spvector(Asp, v, i);
}
return Asp;
}
/*!
* \brief Convert a symmetric band matrix into a sparse matrix
*
* WARNING:
* This function is experimental, do not use.
* Only the upper triangle matrix of the band structure is copied.
*
* \param A (double **) the symmetric band matrix
* \param rows (int)
* \param bandwidth (int)
* \param epsilon (double) -- non-zero values are greater then epsilon
* \return (G_math_spvector **)
*
* */
G_math_spvector **G_math_sband_matrix_to_Asp(double **A, int rows,
int bandwidth, double epsilon)
{
int i, j;
int nonull, count = 0;
G_math_spvector **Asp = NULL;
Asp = G_math_alloc_spmatrix(rows);
for (i = 0; i < rows; i++) {
nonull = 0;
/*Count the number of non zero entries */
for (j = 0; j < bandwidth; j++) {
if (A[i][j] > epsilon)
nonull++;
}
/*Allocate the sparse vector and insert values */
G_math_spvector *v = G_math_alloc_spvector(nonull);
count = 0;
if (A[i][0] > epsilon) {
v->index[count] = i;
v->values[count] = A[i][0];
count++;
}
for (j = 1; j < bandwidth; j++) {
if (A[i][j] > epsilon && i + j < rows) {
v->index[count] = i + j;
v->values[count] = A[i][j];
count++;
}
}
/*Add vector to sparse matrix */
G_math_add_spvector(Asp, v, i);
}
return Asp;
}
/*!
* \brief Compute the matrix - vector product
* of sparse matrix **Asp and vector x.
*
* This function is multi-threaded with OpenMP and can be called within a
* parallel OpenMP region.
*
* y = A * x
*
*
* \param Asp (G_math_spvector **)
* \param x (double) *)
* \param y (double * )
* \param rows (int)
* \return (void)
*
* */
void G_math_Ax_sparse(G_math_spvector **Asp, double *x, double *y, int rows)
{
int i;
unsigned int j;
double tmp;
#pragma omp for schedule(static) private(i, j, tmp)
for (i = 0; i < rows; i++) {
tmp = 0;
for (j = 0; j < Asp[i]->cols; j++) {
tmp += Asp[i]->values[j] * x[Asp[i]->index[j]];
}
y[i] = tmp;
}
return;
}
|