xref: /petsc/src/mat/utils/axpy.c (revision 052efed2d4d49f85092290ff2aaacf5d901780e1)
16f79c3a4SBarry Smith #ifndef lint
2*052efed2SBarry Smith static char vcid[] = "$Id: axpy.c,v 1.12 1995/11/01 23:19:34 bsmith Exp bsmith $";
36f79c3a4SBarry Smith #endif
46f79c3a4SBarry Smith 
548b35521SBarry Smith #include "matimpl.h"  /*I   "mat.h"  I*/
66f79c3a4SBarry Smith 
706be10caSBarry Smith /*@
89cf4f1e8SLois Curfman McInnes    MatAXPY - Computes Y = a*X + Y
96f79c3a4SBarry Smith 
106f79c3a4SBarry Smith    Input Parameters:
1106be10caSBarry Smith .  X,Y - the matrices
129cf4f1e8SLois Curfman McInnes .  a - the scalar multiplier
136f79c3a4SBarry Smith 
149cf4f1e8SLois Curfman McInnes .keywords: matrix, add
1506be10caSBarry Smith  @*/
1606be10caSBarry Smith int MatAXPY(Scalar *a,Mat X,Mat Y)
176f79c3a4SBarry Smith {
1806be10caSBarry Smith   int    m1,m2,n1,n2,i,*row,start,end,j,ncols,ierr;
1906be10caSBarry Smith   Scalar *val,*vals;
206f79c3a4SBarry Smith 
211a941147SBarry Smith   PETSCVALIDHEADERSPECIFIC(X,MAT_COOKIE);  PETSCVALIDHEADERSPECIFIC(Y,MAT_COOKIE);
2206be10caSBarry Smith   MatGetSize(X,&m1,&n1);  MatGetSize(X,&m2,&n2);
23bbb6d6a8SBarry Smith   if (m1 != m2 || n1 != n2) SETERRQ(1,"MatAXPY:Non conforming matrix add");
241987afe7SBarry Smith 
251987afe7SBarry Smith   if (X->ops.axpy) {
261987afe7SBarry Smith     ierr = (*X->ops.axpy)(a,X,Y); CHKERRQ(ierr);
271987afe7SBarry Smith   }
281987afe7SBarry Smith   else {
290452661fSBarry Smith     vals = (Scalar *) PetscMalloc( n1*sizeof(Scalar) ); CHKPTRQ(vals);
3006be10caSBarry Smith     MatGetOwnershipRange(X,&start,&end);
3106be10caSBarry Smith     for ( i=start; i<end; i++ ) {
3206be10caSBarry Smith       MatGetRow(X,i,&ncols,&row,&val);
3306be10caSBarry Smith       for ( j=0; j<ncols; j++ ) {
3406be10caSBarry Smith         vals[j] = (*a)*val[j];
356f79c3a4SBarry Smith       }
36dbb450caSBarry Smith       ierr = MatSetValues(Y,1,&i,ncols,row,vals,ADD_VALUES); CHKERRQ(ierr);
3706be10caSBarry Smith       MatRestoreRow(X,i,&ncols,&row,&val);
386f79c3a4SBarry Smith     }
390452661fSBarry Smith     PetscFree(vals);
4078b31e54SBarry Smith     ierr = MatAssemblyBegin(Y,FINAL_ASSEMBLY); CHKERRQ(ierr);
4178b31e54SBarry Smith     ierr = MatAssemblyEnd(Y,FINAL_ASSEMBLY); CHKERRQ(ierr);
421987afe7SBarry Smith   }
436f79c3a4SBarry Smith   return 0;
446f79c3a4SBarry Smith }
45*052efed2SBarry Smith 
46*052efed2SBarry Smith /*@
47*052efed2SBarry Smith    MatShift - Computes Y =  Y + a I
48*052efed2SBarry Smith 
49*052efed2SBarry Smith    Input Parameters:
50*052efed2SBarry Smith .  Y - the matrices
51*052efed2SBarry Smith .  a - the scalar
52*052efed2SBarry Smith 
53*052efed2SBarry Smith .keywords: matrix, add, shift
54*052efed2SBarry Smith  @*/
55*052efed2SBarry Smith int MatShift(Scalar *a,Mat Y)
56*052efed2SBarry Smith {
57*052efed2SBarry Smith   int    i,start,end,ierr;
58*052efed2SBarry Smith 
59*052efed2SBarry Smith   PETSCVALIDHEADERSPECIFIC(Y,MAT_COOKIE);
60*052efed2SBarry Smith   if (Y->ops.shift) {
61*052efed2SBarry Smith     ierr = (*Y->ops.shift)(a,Y); CHKERRQ(ierr);
62*052efed2SBarry Smith   }
63*052efed2SBarry Smith   else {
64*052efed2SBarry Smith     MatGetOwnershipRange(Y,&start,&end);
65*052efed2SBarry Smith     for ( i=start; i<end; i++ ) {
66*052efed2SBarry Smith       ierr = MatSetValues(Y,1,&i,1,&i,a,ADD_VALUES); CHKERRQ(ierr);
67*052efed2SBarry Smith     }
68*052efed2SBarry Smith     ierr = MatAssemblyBegin(Y,FINAL_ASSEMBLY); CHKERRQ(ierr);
69*052efed2SBarry Smith     ierr = MatAssemblyEnd(Y,FINAL_ASSEMBLY); CHKERRQ(ierr);
70*052efed2SBarry Smith   }
71*052efed2SBarry Smith   return 0;
72*052efed2SBarry Smith }
73