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, double alpha, double beta)
4 {
5  int p, j, nz = 0, anz, *Cp, *Ci, *Bp, m, n, bnz, *w, values ;
6  double *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 = (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 ; 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 }
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
Definition: cs.h:16
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
cs * cs_add(const cs *A, const cs *B, double alpha, double beta)
Definition: cs_add.c:3
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