#include #include #include /// #include "InputOutput.h" #include "ScalarVectors.h" #include "hb_io.h" #include "SparseProduct.h" /*********************************************************************************/ // This routine creates a sparseMatrix from the next parameters // * numR defines the number of rows // * numC defines the number of columns // * numE defines the number of nonzero elements // * msr indicates if the MSR is the format used to the sparse matrix // If msr is actived, numE doesn't include the diagonal elements // The parameter index indicates if 0-indexing or 1-indexing is used. void CreateSparseMatrix (ptr_SparseMatrix p_spr, int index, int numR, int numC, int numE, int msr) { // printf (" index = %d , numR = %d , numC = %d , numE = %d\n", index, numR, numC, numE); // The scalar components of the structure are initiated p_spr->dim1 = numR; p_spr->dim2 = numC; // Only one malloc is made for the vectors of indices CreateInts (&(p_spr->vptr), numE+numR+1); // The first component of the vectors depends on the used format *(p_spr->vptr) = ((msr)? (numR+1): 0) + index; p_spr->vpos = p_spr->vptr + ((msr)? 0: (numR+1)); // The number of nonzero elements depends on the format used CreateDoubles (&(p_spr->vval), numE+(numR+1)*msr); } // This routine liberates the memory related to matrix spr void RemoveSparseMatrix (ptr_SparseMatrix spr) { // First the scalar are initiated spr->dim1 = -1; spr->dim2 = -1; // The vectors are liberated RemoveInts (&(spr->vptr)); RemoveDoubles (&(spr->vval)); } /*********************************************************************************/ // This routine creates de sparse matrix dst from the symmetric matrix spr. // The parameters indexS and indexD indicate, respectivaly, if 0-indexing or 1-indexing is used // to store the sparse matrices. void DesymmetrizeSparseMatrices (SparseMatrix src, int indexS, ptr_SparseMatrix dst, int indexD) { int n = src.dim1, nnz = 0; int *sizes = NULL; int *pp1 = NULL, *pp2 = NULL, *pp3 = NULL, *pp4 = NULL, *pp5 = NULL; int i, j, dim, indexDS = indexD - indexS; double *pd3 = NULL, *pd4 = NULL; // The vector sizes is created and initiated CreateInts (&sizes, n); InitInts (sizes, n, 0, 0); // This loop counts the number of elements in each row pp1 = src.vptr; pp3 = src.vpos + *pp1 - indexS; pp2 = pp1 + 1 ; pp4 = sizes - indexS; for (i=indexS; i<(n+indexS); i++) { // The size of the corresponding row is accumulated dim = (*pp2 - *pp1); pp4[i] += dim; // Now each component of the row is analyzed for (j=0; jvptr) + 1, n); dst->vptr[0] = indexD; TransformLengthtoHeader (dst->vptr, n); // The vector sizes is initiated with the beginning of each row CopyInts (dst->vptr, sizes, n); // This loop fills the contents of vector vpos pp1 = src.vptr; pp3 = src.vpos + *pp1 - indexS; pp2 = pp1 + 1 ; pp4 = dst->vpos - indexD; pp5 = sizes - indexS; pd3 = src.vval + *pp1 - indexS; pd4 = dst->vval - indexD; for (i=indexS; i<(n+indexS); i++) { dim = (*pp2 - *pp1); for (j=0; jvptr) + 1, n); dst->vptr[0] = indexD; TransformLengthtoHeader (dst->vptr, n); // The vector sizes is initiated with the beginning of each row CopyInts (dst->vptr, sizes, n); // This loop fills the contents of vector vpos pp1 = src.vptr; pp3 = src.vpos + *pp1 - indexS; pp2 = pp1 + 1 ; pp4 = dst->vpos - indexD; pp5 = sizes - indexS; pd3 = src.vval + *pp1 - indexS; pd4 = dst->vval - indexD; for (i=indexS; i<(n+indexS); i++) { dim = (*pp2 - *pp1); for (j=0; jdim1 = nrow ; p_spr->dim2 = ncol ; p_spr->vptr = colptr; p_spr->vpos = rowind; p_spr->vval = values; // Memory liberation free (exact ); free (guess ); free (indfmt); free (key ); free (mxtype); free (ptrfmt); free (rhsfmt); free (rhsind); free (rhsptr); free (rhstyp); free (rhsval); free (rhsvec); free (title ); free (valfmt); return 0; } /*********************************************************************************/ // This routine computes the product { res += spr * vec }. // The parameter index indicates if 0-indexing or 1-indexing is used, void ProdSparseMatrixVector2 (SparseMatrix spr, int index, double *vec, double *res) { int i, j; int *pp1 = spr.vptr, *pp2 = pp1+1, *pi1 = spr.vpos + *pp1 - index; double aux, *pvec = vec - index, *pd2 = res; double *pd1 = spr.vval + *pp1 - index; // If the MSR format is used, first the diagonal has to be processed if (spr.vptr == spr.vpos) VvecDoubles (1.0, spr.vval, vec, 1.0, res, spr.dim1); for (i=0; i 0) && (*pi1 < (i+shft))) { pi1++; pd1++; j--; } *(pd2++) = ((j > 0) && (*pi1 == (i+shft))) ? *pd1: 0.0; //*(pi2++) = ((j > 0) && (*pi1 == (i+shft))) ? *pp2-j: -1; pi1 += j; pd1 += j; pp1 = (pp2++); j = (*pp2-*pp1); } } } /*********************************************************************************/