static inline void NM_dense_to_sparse(const NumericsMatrix* const A, NumericsMatrix* B) { assert(A->matrix0); assert(B->matrix2->triplet); for (int i = 0; i < A->size0; ++i) { for (int j = 0; j < A->size1; ++j) { CHECK_RETURN(cs_zentry(B->matrix2->triplet, i, j, A->matrix0[i + A->size0*j])); } } }
/* NumericsMatrix : initialize triplet storage from sparse block storage */ CSparseMatrix* NM_triplet(NumericsMatrix* A) { if (!NM_sparse(A)->triplet) { if (A->storageType == NM_SPARSE_BLOCK || A->storageType == NM_DENSE) { /* Invalidation of previously constructed csc storage. */ /* If we want to avoid this -> rewrite cs_compress with reallocation. */ NM_clearCSC(A); NM_clearCSCTranspose(A); A->matrix2->triplet = cs_spalloc(0,0,1,1,1); if (A->matrix1) { /* iteration on row, cr : current row */ for(unsigned int cr = 0; cr < A->matrix1->filled1-1; ++cr) { for(size_t bn = A->matrix1->index1_data[cr]; bn < A->matrix1->index1_data[cr + 1]; ++bn) { /* cc : current column */ size_t cc = A->matrix1->index2_data[bn]; unsigned int inbr = A->matrix1->blocksize0[cr]; unsigned int roffset = 0; unsigned int coffset = 0; if(cr != 0) { roffset = A->matrix1->blocksize0[cr - 1]; inbr -= roffset; } unsigned int inbc = A->matrix1->blocksize1[cc]; if(cc != 0) { coffset = A->matrix1->blocksize1[cc - 1]; inbc -= coffset; } for(unsigned j = 0; j < inbc; ++j) { for(unsigned i = 0; i < inbr; ++i) { CHECK_RETURN(cs_zentry(A->matrix2->triplet, i + roffset, j + coffset, A->matrix1->block[bn][i + j*inbr])); } } } } } else if (A->matrix0) { NM_dense_to_sparse(A, A); } else { fprintf(stderr, "NM_triplet: sparse matrix cannot be constructed.\n"); exit(EXIT_FAILURE); } } } assert (A->matrix2->triplet); return A->matrix2->triplet; }
/* init memory for jacobian */ csi initACPsiJacobian( CSparseMatrix* M, CSparseMatrix* H, CSparseMatrix *A, CSparseMatrix *B, CSparseMatrix *J) { /* only triplet matrix */ assert(M->nz>=0); assert(H->nz>=0); assert(A->nz>=0); assert(B->nz>=0); /* M square */ assert(M->m == M->n); /* A & B squares */ assert(A->m == A->n); assert(B->m == B->n); assert(A->nz == B->nz); /* - M */ for(int e = 0; e < M->nz; ++e) { DEBUG_PRINTF("e=%d, M->i[e]=%td, M->p[e]=%td, M->x[e]=%g\n", e, M->i[e], M->p[e], M->x[e]); CHECK_RETURN(cs_zentry(J, M->i[e], M->p[e], - M->x[e])); } /* H */ assert(M->n == H->m); for(int e = 0; e < H->nz; ++e) { DEBUG_PRINTF("e=%d, H->i[e]=%td, H->p[e] + M->n + A->n=%td, H->x[e]=%g\n", e, H->i[e], H->p[e] + M->n + A->n , H->x[e]); CHECK_RETURN(cs_zentry(J, H->i[e], H->p[e] + M->n + A->n, H->x[e])); } /* Ht */ for(int e = 0; e < H->nz; ++e) { CHECK_RETURN(cs_zentry(J, H->p[e] + M->m, H->i[e], H->x[e])); } /* -I */ for(int e = 0; e < A->m; ++e) { CHECK_RETURN(cs_zentry(J, e + M->m, e + M->n, -1.)); } /* keep A start indice for update */ csi Astart = J->nz; /* A */ for(int e = 0; e < A->nz; ++e) { CHECK_RETURN(cs_zentry(J, A->i[e] + M->m + H->n, A->p[e] + M->n, A->x[e])); } /* B */ for(int e = 0; e < B->nz; ++e) { CHECK_RETURN(cs_zentry(J, B->i[e] + M->m + H->n, B->p[e] + M->n + A->n, B->x[e])); } return Astart; }