EVSL  1.1.0
EigenValues Slicing Library
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
cs_add.c
Go to the documentation of this file.
1 #include "cs.h"
2 /* C = alpha*A + beta*B */
3 cs *cs_add (const cs *A, const cs *B, CS_ENTRY alpha, CS_ENTRY beta)
4 {
5  CS_INT p, j, nz = 0, anz, *Cp, *Ci, *Bp, m, n, bnz, *w, values ;
6  CS_ENTRY *x, *Bx, *Cx ;
7  cs *C ;
8  if (!CS_CSC (A) || !CS_CSC (B)) return (NULL) ; /* check inputs */
9  if (A->m != B->m || A->n != B->n) return (NULL) ;
10  m = A->m ; anz = A->p [A->n] ;
11  n = B->n ; Bp = B->p ; 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 ; Ci = C->i ; Cx = C->x ;
18  for (j = 0 ; j < n ; j++)
19  {
20  Cp [j] = nz ; /* column j of C starts here */
21  nz = cs_scatter (A, j, alpha, w, x, j+1, C, nz) ; /* alpha*A(:,j)*/
22  nz = cs_scatter (B, j, beta, w, x, j+1, C, nz) ; /* beta*B(:,j) */
23  if (values) for (p = Cp [j] ; p < nz ; p++) Cx [p] = x [Ci [p]] ;
24  }
25  Cp [n] = nz ; /* finalize the last column of C */
26  cs_sprealloc (C, 0) ; /* remove extra space from C */
27  return (cs_done (C, w, x, 1)) ; /* success; free workspace, return C */
28 }
#define cs
Definition: cs.h:637
#define CS_ENTRY
Definition: cs.h:635
cs * cs_add(const cs *A, const cs *B, CS_ENTRY alpha, CS_ENTRY beta)
Definition: cs_add.c:3
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
#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