EVSL  1.1.0 EigenValues Slicing Library
cs_multiply.c
Go to the documentation of this file.
1 #include "cs.h"
2 /* C = A*B */
3 cs *cs_multiply (const cs *A, const cs *B)
4 {
5  CS_INT p, j, nz = 0, anz, *Cp, *Ci, *Bp, m, n, bnz, *w, values, *Bi ;
6  CS_ENTRY *x, *Bx, *Cx ;
7  cs *C ;
8  if (!CS_CSC (A) || !CS_CSC (B)) return (NULL) ; /* check inputs */
9  if (A->n != B->m) return (NULL) ;
10  m = A->m ; anz = A->p [A->n] ;
11  n = B->n ; Bp = B->p ; Bi = B->i ; Bx = B->x ; bnz = Bp [n] ;
12  w = cs_calloc (m, sizeof (CS_INT)) ; /* get workspace */
13  values = (A->x != NULL) && (Bx != NULL) ;
14  x = values ? cs_malloc (m, sizeof (CS_ENTRY)) : NULL ; /* get workspace */
15  C = cs_spalloc (m, n, anz + bnz, values, 0) ; /* allocate result */
16  if (!C || !w || (values && !x)) return (cs_done (C, w, x, 0)) ;
17  Cp = C->p ;
18  for (j = 0 ; j < n ; j++)
19  {
20  if (nz + m > C->nzmax && !cs_sprealloc (C, 2*(C->nzmax)+m))
21  {
22  return (cs_done (C, w, x, 0)) ; /* out of memory */
23  }
24  Ci = C->i ; Cx = C->x ; /* C->i and C->x may be reallocated */
25  Cp [j] = nz ; /* column j of C starts here */
26  for (p = Bp [j] ; p < Bp [j+1] ; p++)
27  {
28  nz = cs_scatter (A, Bi [p], Bx ? Bx [p] : 1, w, x, j+1, C, nz) ;
29  }
30  if (values) for (p = Cp [j] ; p < nz ; p++) Cx [p] = x [Ci [p]] ;
31  }
32  Cp [n] = nz ; /* finalize the last column of C */
33  cs_sprealloc (C, 0) ; /* remove extra space from C */
34  return (cs_done (C, w, x, 1)) ; /* success; free workspace, return C */
35 }
#define cs
Definition: cs.h:637
#define CS_ENTRY
Definition: cs.h:635
cs * cs_spalloc(CS_INT m, CS_INT n, CS_INT nzmax, CS_INT values, CS_INT triplet)
Definition: cs_util.c:3
#define CS_CSC(A)
Definition: cs.h:659
void * cs_calloc(CS_INT n, size_t size)
Definition: cs_malloc.c:16
cs * cs_multiply(const cs *A, const cs *B)
Definition: cs_multiply.c:3
#define CS_INT
Definition: cs.h:627
cs * cs_done(cs *C, void *w, void *x, CS_INT ok)
Definition: cs_util.c:90
CS_INT cs_sprealloc(cs *A, CS_INT nzmax)
Definition: cs_util.c:18
CS_INT cs_scatter(const cs *A, CS_INT j, CS_ENTRY beta, CS_INT *w, CS_ENTRY *x, CS_INT mark, cs *C, CS_INT nz)
Definition: cs_scatter.c:3
void * cs_malloc(CS_INT n, size_t size)
Definition: cs_malloc.c:10