xref: /petsc/src/mat/utils/axpy.c (revision 1a9411475029bd0c092ff088e6540bae5153ea53)
1 #ifndef lint
2 static char vcid[] = "$Id: axpy.c,v 1.6 1995/07/17 20:41:50 bsmith Exp bsmith $";
3 #endif
4 
5 #include "matimpl.h"  /*I   "mat.h"  I*/
6 
7 /*@
8    MatAXPY - Y = a*X + Y
9 
10    Input Parameters:
11 .  X,Y - the matrices
12 .  a - the scalar multiple
13 
14 .keywods: Mat, add
15  @*/
16 
17 int MatAXPY(Scalar *a,Mat X,Mat Y)
18 {
19   int    m1,m2,n1,n2,i,*row,start,end,j,ncols,ierr;
20   Scalar *val,*vals;
21 
22   PETSCVALIDHEADERSPECIFIC(X,MAT_COOKIE);  PETSCVALIDHEADERSPECIFIC(Y,MAT_COOKIE);
23   MatGetSize(X,&m1,&n1);  MatGetSize(X,&m2,&n2);
24   if (m1 != m2 || n1 != n2) SETERRQ(1,"MatAXPY:Non conforming matrix add");
25   vals = (Scalar *) PETSCMALLOC( n1*sizeof(Scalar) ); CHKPTRQ(vals);
26   MatGetOwnershipRange(X,&start,&end);
27   for ( i=start; i<end; i++ ) {
28     MatGetRow(X,i,&ncols,&row,&val);
29     for ( j=0; j<ncols; j++ ) {
30       vals[j] = (*a)*val[j];
31     }
32     ierr = MatSetValues(Y,1,&i,ncols,row,vals,ADDVALUES); CHKERRQ(ierr);
33     MatRestoreRow(X,i,&ncols,&row,&val);
34   }
35   PETSCFREE(vals);
36   ierr = MatAssemblyBegin(Y,FINAL_ASSEMBLY); CHKERRQ(ierr);
37   ierr = MatAssemblyEnd(Y,FINAL_ASSEMBLY); CHKERRQ(ierr);
38   return 0;
39 }
40