$search
00001 #include "cs.h" 00002 /* C = A*B */ 00003 cs *cs_multiply (const cs *A, const cs *B) 00004 { 00005 int p, j, nz = 0, anz, *Cp, *Ci, *Bp, m, n, bnz, *w, values, *Bi ; 00006 double *x, *Bx, *Cx ; 00007 cs *C ; 00008 if (!CS_CSC (A) || !CS_CSC (B)) return (NULL) ; /* check inputs */ 00009 if (A->n != B->m) return (NULL) ; 00010 m = A->m ; anz = A->p [A->n] ; 00011 n = B->n ; Bp = B->p ; Bi = B->i ; Bx = B->x ; bnz = Bp [n] ; 00012 w = cs_calloc (m, sizeof (int)) ; /* get workspace */ 00013 values = (A->x != NULL) && (Bx != NULL) ; 00014 x = values ? cs_malloc (m, sizeof (double)) : NULL ; /* get workspace */ 00015 C = cs_spalloc (m, n, anz + bnz, values, 0) ; /* allocate result */ 00016 if (!C || !w || (values && !x)) return (cs_done (C, w, x, 0)) ; 00017 Cp = C->p ; 00018 for (j = 0 ; j < n ; j++) 00019 { 00020 if (nz + m > C->nzmax && !cs_sprealloc (C, 2*(C->nzmax)+m)) 00021 { 00022 return (cs_done (C, w, x, 0)) ; /* out of memory */ 00023 } 00024 Ci = C->i ; Cx = C->x ; /* C->i and C->x may be reallocated */ 00025 Cp [j] = nz ; /* column j of C starts here */ 00026 for (p = Bp [j] ; p < Bp [j+1] ; p++) 00027 { 00028 nz = cs_scatter (A, Bi [p], Bx ? Bx [p] : 1, w, x, j+1, C, nz) ; 00029 } 00030 if (values) for (p = Cp [j] ; p < nz ; p++) Cx [p] = x [Ci [p]] ; 00031 } 00032 Cp [n] = nz ; /* finalize the last column of C */ 00033 cs_sprealloc (C, 0) ; /* remove extra space from C */ 00034 return (cs_done (C, w, x, 1)) ; /* success; free workspace, return C */ 00035 }