xref: /petsc/src/mat/utils/axpy.c (revision 70f55243aafb320636e2a54ff30cab5d1e8d3d7b)
16f79c3a4SBarry Smith #ifndef lint
2*70f55243SBarry Smith static char vcid[] = "$Id: axpy.c,v 1.18 1996/07/08 22:20:46 bsmith Exp bsmith $";
36f79c3a4SBarry Smith #endif
46f79c3a4SBarry Smith 
5*70f55243SBarry Smith #include "src/mat/matimpl.h"  /*I   "mat.h"  I*/
66f79c3a4SBarry Smith 
706be10caSBarry Smith /*@
821c89e3eSBarry Smith    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 
2177c4ece6SBarry 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);
406d4a8577SBarry Smith     ierr = MatAssemblyBegin(Y,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
416d4a8577SBarry Smith     ierr = MatAssemblyEnd(Y,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
421987afe7SBarry Smith   }
436f79c3a4SBarry Smith   return 0;
446f79c3a4SBarry Smith }
45052efed2SBarry Smith 
46052efed2SBarry Smith /*@
476b9ee512SLois Curfman McInnes    MatShift - Computes Y =  Y + a I, where a is a scalar and I is the identity
486b9ee512SLois Curfman McInnes    matrix.
49052efed2SBarry Smith 
50052efed2SBarry Smith    Input Parameters:
51052efed2SBarry Smith .  Y - the matrices
52052efed2SBarry Smith .  a - the scalar
53052efed2SBarry Smith 
54052efed2SBarry Smith .keywords: matrix, add, shift
556b9ee512SLois Curfman McInnes 
566b9ee512SLois Curfman McInnes .seealso: MatDiagonalShift()
57052efed2SBarry Smith  @*/
58052efed2SBarry Smith int MatShift(Scalar *a,Mat Y)
59052efed2SBarry Smith {
60052efed2SBarry Smith   int    i,start,end,ierr;
61052efed2SBarry Smith 
6277c4ece6SBarry Smith   PetscValidHeaderSpecific(Y,MAT_COOKIE);
63052efed2SBarry Smith   if (Y->ops.shift) {
64052efed2SBarry Smith     ierr = (*Y->ops.shift)(a,Y); CHKERRQ(ierr);
65052efed2SBarry Smith   }
66052efed2SBarry Smith   else {
67052efed2SBarry Smith     MatGetOwnershipRange(Y,&start,&end);
68052efed2SBarry Smith     for ( i=start; i<end; i++ ) {
69052efed2SBarry Smith       ierr = MatSetValues(Y,1,&i,1,&i,a,ADD_VALUES); CHKERRQ(ierr);
70052efed2SBarry Smith     }
716d4a8577SBarry Smith     ierr = MatAssemblyBegin(Y,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
726d4a8577SBarry Smith     ierr = MatAssemblyEnd(Y,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
73052efed2SBarry Smith   }
74052efed2SBarry Smith   return 0;
75052efed2SBarry Smith }
766d84be18SBarry Smith 
776d84be18SBarry Smith /*@
786b9ee512SLois Curfman McInnes    MatDiagonalShift - Computes Y = Y + D, where D is a diagonal matrix
796b9ee512SLois Curfman McInnes    that is represented as a vector.
806d84be18SBarry Smith 
816d84be18SBarry Smith    Input Parameters:
826b9ee512SLois Curfman McInnes .  Y - the input matrix
836d84be18SBarry Smith .  D - the diagonal matrix, represented as a vector
846d84be18SBarry Smith 
856b9ee512SLois Curfman McInnes    Input Parameters:
866b9ee512SLois Curfman McInnes .  Y - the shifted ouput matrix
876d84be18SBarry Smith 
886b9ee512SLois Curfman McInnes .keywords: matrix, add, shift, diagonal
896b9ee512SLois Curfman McInnes 
906b9ee512SLois Curfman McInnes .seealso: MatShift()
916d84be18SBarry Smith @*/
926d84be18SBarry Smith int MatDiagonalShift(Mat Y,Vec D)
936d84be18SBarry Smith {
946d84be18SBarry Smith   int    i,start,end,ierr;
956d84be18SBarry Smith 
9677c4ece6SBarry Smith   PetscValidHeaderSpecific(Y,MAT_COOKIE);
976d84be18SBarry Smith   if (Y->ops.shift) {
986d84be18SBarry Smith     ierr = (*Y->ops.diagonalshift)(D,Y); CHKERRQ(ierr);
996d84be18SBarry Smith   }
1006d84be18SBarry Smith   else {
1016d84be18SBarry Smith     int    vstart,vend;
1026d84be18SBarry Smith     Scalar *v;
1036d84be18SBarry Smith     ierr = VecGetOwnershipRange(D,&vstart,&vend); CHKERRQ(ierr);
1046d84be18SBarry Smith     ierr = MatGetOwnershipRange(Y,&start,&end); CHKERRQ(ierr);
1056d84be18SBarry Smith     if (vstart != start || vend != end)
1066d84be18SBarry Smith       SETERRQ(1,"MatDiagonalShift:Vector shift not compatible with matrix");
1076d84be18SBarry Smith 
1086d84be18SBarry Smith     ierr = VecGetArray(D,&v); CHKERRQ(ierr);
1096d84be18SBarry Smith     for ( i=start; i<end; i++ ) {
1106d84be18SBarry Smith       ierr = MatSetValues(Y,1,&i,1,&i,v+i-start,ADD_VALUES); CHKERRQ(ierr);
1116d84be18SBarry Smith     }
1126d4a8577SBarry Smith     ierr = MatAssemblyBegin(Y,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
1136d4a8577SBarry Smith     ierr = MatAssemblyEnd(Y,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
1146d84be18SBarry Smith   }
1156d84be18SBarry Smith   return 0;
1166d84be18SBarry Smith }
117