cs_add.c
Go to the documentation of this file.
00001 #include "cs.h"
00002 /* C = alpha*A + beta*B */
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) ;         /* check inputs */
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 = cs_calloc (m, sizeof (int)) ;                       /* get workspace */
00013     values = (A->x != NULL) && (Bx != NULL) ;
00014     x = values ? cs_malloc (m, sizeof (double)) : NULL ;    /* get workspace */
00015     C = cs_spalloc (m, n, anz + bnz, values, 0) ;           /* allocate result*/
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 ;                   /* column j of C starts here */
00021         nz = cs_scatter (A, j, alpha, w, x, j+1, C, nz) ;   /* alpha*A(:,j)*/
00022         nz = cs_scatter (B, j, beta, w, x, j+1, C, nz) ;    /* beta*B(:,j) */
00023         if (values) for (p = Cp [j] ; p < nz ; p++) Cx [p] = x [Ci [p]] ;
00024     }
00025     Cp [n] = nz ;                       /* finalize the last column of C */
00026     cs_sprealloc (C, 0) ;               /* remove extra space from C */
00027     return (cs_done (C, w, x, 1)) ;     /* success; free workspace, return C */
00028 }


hogman_minimal
Author(s): Maintained by Juergen Sturm
autogenerated on Mon Oct 6 2014 00:06:58