00001 #include "cs.h"
00002
00003 cs *cs_add (const cs *A, const cs *B, double alpha, double beta)
00004 {
00005 int p, j, nz = 0, anz, *Cp, *Ci, *Bp, m, n, bnz, *w, values ;
00006 double *x, *Bx, *Cx ;
00007 cs *C ;
00008 if (!CS_CSC (A) || !CS_CSC (B)) return (NULL) ;
00009 if (A->m != B->m || A->n != B->n) return (NULL) ;
00010 m = A->m ; anz = A->p [A->n] ;
00011 n = B->n ; Bp = B->p ; Bx = B->x ; bnz = Bp [n] ;
00012 w = (int*) cs_calloc (m, sizeof (int)) ;
00013 values = (A->x != NULL) && (Bx != NULL) ;
00014 x = values ? (double*) cs_malloc (m, sizeof (double)) : NULL ;
00015 C = cs_spalloc (m, n, anz + bnz, values, 0) ;
00016 if (!C || !w || (values && !x)) return (cs_done (C, w, x, 0)) ;
00017 Cp = C->p ; Ci = C->i ; Cx = C->x ;
00018 for (j = 0 ; j < n ; j++)
00019 {
00020 Cp [j] = nz ;
00021 nz = cs_scatter (A, j, alpha, w, x, j+1, C, nz) ;
00022 nz = cs_scatter (B, j, beta, w, x, j+1, C, nz) ;
00023 if (values) for (p = Cp [j] ; p < nz ; p++) Cx [p] = x [Ci [p]] ;
00024 }
00025 Cp [n] = nz ;
00026 cs_sprealloc (C, 0) ;
00027 return (cs_done (C, w, x, 1)) ;
00028 }