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  int p, j, nz = 0, anz, *Cp, *Ci, *Bp, m, n, bnz, *w, values, *Bi ;
6  double *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 = (int*) cs_calloc (m, sizeof (int)) ; /* get workspace */
13  values = (A->x != NULL) && (Bx != NULL) ;
14  x = values ? (double*) cs_malloc (m, sizeof (double)) : 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 }
cs * cs_spalloc(int m, int n, int nzmax, int values, int triplet)
Definition: cs_util.c:3
cs * cs_done(cs *C, void *w, void *x, int ok)
Definition: cs_util.c:89
int n
Definition: cs.h:20
int cs_sprealloc(cs *A, int nzmax)
Definition: cs_util.c:18
#define CS_CSC(A)
Definition: cs.h:140
int * p
Definition: cs.h:21
int cs_scatter(const cs *A, int j, double beta, int *w, double *x, int mark, cs *C, int nz)
Definition: cs_scatter.c:3
cs * cs_multiply(const cs *A, const cs *B)
Definition: cs_multiply.c:3
Definition: cs.h:16
int * i
Definition: cs.h:22
void * cs_calloc(int n, size_t size)
Definition: cs_malloc.c:16
void * cs_malloc(int n, size_t size)
Definition: cs_malloc.c:10
double * x
Definition: cs.h:23
int m
Definition: cs.h:19


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Mon Jun 10 2019 12:34:31