cs *cs_compress(const cs *T) { int m, n, nz, p, k, *Cp, *Ci, *w, *Ti, *Tj; double *Cx, *Tx; cs *C; if (!CS_TRIPLET (T)) return (NULL); /* check inputs */ m = T->m; n = T->n; Ti = T->i; Tj = T->p; Tx = T->x; nz = T->nz; C = cs_spalloc(m, n, nz, Tx != NULL, 0); /* allocate result */ w = (int *) cs_calloc(n, sizeof(int)); /* get workspace */ if (!C || !w) return (cs_done(C, w, NULL, 0)); /* out of memory */ Cp = C->p; Ci = C->i; Cx = C->x; for (k = 0; k < nz; k++) w[Tj[k]]++; /* column counts */ cs_cumsum(Cp, w, n); /* column pointers */ for (k = 0; k < nz; k++) { Ci[p = w[Tj[k]]++] = Ti[k]; /* A(i,j) is the pth entry in C */ if (Cx) Cx[p] = Tx[k]; } return (cs_done(C, w, NULL, 1)); /* success; free w and return C */ }
/* Initialize a sparse Jacobian for the solver. */ int KINKluSetJacGraph(KINMem kin_memory, cs_di *jac){ //check inputs if(!kin_memory||!jac) return 1; //grab the linear solver memory block KINKluMem kin_klu_mem=(KINKluMem)kin_memory->kin_lmem; if(!kin_klu_mem) return 1; //grab needed kinklu objects cs_di *kin_klu_jac=kin_klu_mem->jac; //check whether the jacobian data has already been allocated. if so, free it. if(kin_klu_jac){ cs_di_spfree(kin_klu_jac); kin_klu_jac=NULL; } //check if the input jacobian is in triplet form. compress if so. if(CS_TRIPLET(jac)){ kin_klu_jac=cs_di_compress(jac); if(!kin_klu_jac) return 1; cs_di_spfree(jac); jac = kin_klu_jac; return 0; } //assign the input jacobian to the kinklu jacobian kin_klu_mem->jac=jac; //return success return 0; };
/* add an entry to a triplet matrix; return 1 if ok, 0 otherwise */ int cs_entry (cs *T, int i, int j, double x) { if (!CS_TRIPLET (T) || i < 0 || j < 0) return (0) ; /* check inputs */ if (T->nz >= T->nzmax && !cs_sprealloc (T,2*(T->nzmax))) return (0) ; if (T->x) T->x [T->nz] = x ; T->i [T->nz] = i ; T->p [T->nz++] = j ; T->m = CS_MAX (T->m, i+1) ; T->n = CS_MAX (T->n, j+1) ; return (1) ; }
/* change the max # of entries sparse matrix */ int cs_sprealloc (cs *A, int nzmax) { int ok, oki, okj = 1, okx = 1 ; if (!A) return (0) ; if (nzmax <= 0) nzmax = (CS_CSC (A)) ? (A->p [A->n]) : A->nz ; A->i = cs_realloc (A->i, nzmax, sizeof (int), &oki) ; if (CS_TRIPLET (A)) A->p = cs_realloc (A->p, nzmax, sizeof (int), &okj) ; if (A->x) A->x = cs_realloc (A->x, nzmax, sizeof (double), &okx) ; ok = (oki && okj && okx) ; if (ok) A->nzmax = nzmax ; return (ok) ; }
/* change the max # of entries sparse matrix */ int csr_sprealloc (csr *A, int nzmax) { int ok, oki, okj = 1, okx = 1 ; if (!A) return (0) ; nzmax = (nzmax <= 0) ? (A->p [A->m]) : nzmax ; A->j = (int*)csr_realloc (A->j, nzmax, sizeof (int), &oki) ; if (CS_TRIPLET (A)) A->p = (int*)csr_realloc (A->p, nzmax, sizeof (int), &okj) ; if (A->x) A->x = (double*)csr_realloc (A->x, nzmax, sizeof (double), &okx) ; ok = (oki && okj && okx) ; if (ok) A->nzmax = nzmax ; return (ok) ; }