xref: /petsc/src/mat/utils/axpy.c (revision 5615d1e584023db9367fb782d85b1b4ebbb8df18)
16f79c3a4SBarry Smith #ifndef lint
2*5615d1e5SSatish Balay static char vcid[] = "$Id: axpy.c,v 1.23 1997/01/01 03:38:57 bsmith Exp balay $";
36f79c3a4SBarry Smith #endif
46f79c3a4SBarry Smith 
570f55243SBarry Smith #include "src/mat/matimpl.h"  /*I   "mat.h"  I*/
66f79c3a4SBarry Smith 
7*5615d1e5SSatish Balay #undef __FUNC__
8*5615d1e5SSatish Balay #define __FUNC__ "MatAXPY"
906be10caSBarry Smith /*@
1021c89e3eSBarry Smith    MatAXPY - Computes Y = a*X + Y.
116f79c3a4SBarry Smith 
126f79c3a4SBarry Smith    Input Parameters:
1306be10caSBarry Smith .  X,Y - the matrices
149cf4f1e8SLois Curfman McInnes .  a - the scalar multiplier
156f79c3a4SBarry Smith 
169cf4f1e8SLois Curfman McInnes .keywords: matrix, add
1706be10caSBarry Smith  @*/
1806be10caSBarry Smith int MatAXPY(Scalar *a,Mat X,Mat Y)
196f79c3a4SBarry Smith {
2006be10caSBarry Smith   int    m1,m2,n1,n2,i,*row,start,end,j,ncols,ierr;
2106be10caSBarry Smith   Scalar *val,*vals;
226f79c3a4SBarry Smith 
2377c4ece6SBarry Smith   PetscValidHeaderSpecific(X,MAT_COOKIE);  PetscValidHeaderSpecific(Y,MAT_COOKIE);
2490f02eecSBarry Smith   PetscValidScalarPointer(a);
2590f02eecSBarry Smith 
2606be10caSBarry Smith   MatGetSize(X,&m1,&n1);  MatGetSize(X,&m2,&n2);
27e3372554SBarry Smith   if (m1 != m2 || n1 != n2) SETERRQ(1,0,"Non conforming matrix add");
281987afe7SBarry Smith 
291987afe7SBarry Smith   if (X->ops.axpy) {
301987afe7SBarry Smith     ierr = (*X->ops.axpy)(a,X,Y); CHKERRQ(ierr);
311987afe7SBarry Smith   }
321987afe7SBarry Smith   else {
330452661fSBarry Smith     vals = (Scalar *) PetscMalloc( n1*sizeof(Scalar) ); CHKPTRQ(vals);
3490f02eecSBarry Smith     ierr = MatGetOwnershipRange(X,&start,&end); CHKERRQ(ierr);
3506be10caSBarry Smith     for ( i=start; i<end; i++ ) {
3690f02eecSBarry Smith       ierr = MatGetRow(X,i,&ncols,&row,&val); CHKERRQ(ierr);
3706be10caSBarry Smith       for ( j=0; j<ncols; j++ ) {
3806be10caSBarry Smith         vals[j] = (*a)*val[j];
396f79c3a4SBarry Smith       }
40dbb450caSBarry Smith       ierr = MatSetValues(Y,1,&i,ncols,row,vals,ADD_VALUES); CHKERRQ(ierr);
4190f02eecSBarry Smith       ierr = MatRestoreRow(X,i,&ncols,&row,&val); CHKERRQ(ierr);
426f79c3a4SBarry Smith     }
430452661fSBarry Smith     PetscFree(vals);
446d4a8577SBarry Smith     ierr = MatAssemblyBegin(Y,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
456d4a8577SBarry Smith     ierr = MatAssemblyEnd(Y,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
461987afe7SBarry Smith   }
476f79c3a4SBarry Smith   return 0;
486f79c3a4SBarry Smith }
49052efed2SBarry Smith 
50*5615d1e5SSatish Balay #undef __FUNC__
51*5615d1e5SSatish Balay #define __FUNC__ "MatShift"
52052efed2SBarry Smith /*@
536b9ee512SLois Curfman McInnes    MatShift - Computes Y =  Y + a I, where a is a scalar and I is the identity
546b9ee512SLois Curfman McInnes    matrix.
55052efed2SBarry Smith 
56052efed2SBarry Smith    Input Parameters:
57052efed2SBarry Smith .  Y - the matrices
58052efed2SBarry Smith .  a - the scalar
59052efed2SBarry Smith 
60052efed2SBarry Smith .keywords: matrix, add, shift
616b9ee512SLois Curfman McInnes 
626b9ee512SLois Curfman McInnes .seealso: MatDiagonalShift()
63052efed2SBarry Smith  @*/
64052efed2SBarry Smith int MatShift(Scalar *a,Mat Y)
65052efed2SBarry Smith {
66052efed2SBarry Smith   int    i,start,end,ierr;
67052efed2SBarry Smith 
6877c4ece6SBarry Smith   PetscValidHeaderSpecific(Y,MAT_COOKIE);
6990f02eecSBarry Smith   PetscValidScalarPointer(a);
70052efed2SBarry Smith   if (Y->ops.shift) {
71052efed2SBarry Smith     ierr = (*Y->ops.shift)(a,Y); CHKERRQ(ierr);
72052efed2SBarry Smith   }
73052efed2SBarry Smith   else {
74052efed2SBarry Smith     MatGetOwnershipRange(Y,&start,&end);
75052efed2SBarry Smith     for ( i=start; i<end; i++ ) {
76052efed2SBarry Smith       ierr = MatSetValues(Y,1,&i,1,&i,a,ADD_VALUES); CHKERRQ(ierr);
77052efed2SBarry Smith     }
786d4a8577SBarry Smith     ierr = MatAssemblyBegin(Y,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
796d4a8577SBarry Smith     ierr = MatAssemblyEnd(Y,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
80052efed2SBarry Smith   }
81052efed2SBarry Smith   return 0;
82052efed2SBarry Smith }
836d84be18SBarry Smith 
84*5615d1e5SSatish Balay #undef __FUNC__
85*5615d1e5SSatish Balay #define __FUNC__ "MatDiagonalShift"
866d84be18SBarry Smith /*@
876b9ee512SLois Curfman McInnes    MatDiagonalShift - Computes Y = Y + D, where D is a diagonal matrix
886b9ee512SLois Curfman McInnes    that is represented as a vector.
896d84be18SBarry Smith 
906d84be18SBarry Smith    Input Parameters:
916b9ee512SLois Curfman McInnes .  Y - the input matrix
926d84be18SBarry Smith .  D - the diagonal matrix, represented as a vector
936d84be18SBarry Smith 
946b9ee512SLois Curfman McInnes    Input Parameters:
956b9ee512SLois Curfman McInnes .  Y - the shifted ouput matrix
966d84be18SBarry Smith 
976b9ee512SLois Curfman McInnes .keywords: matrix, add, shift, diagonal
986b9ee512SLois Curfman McInnes 
996b9ee512SLois Curfman McInnes .seealso: MatShift()
1006d84be18SBarry Smith @*/
1016d84be18SBarry Smith int MatDiagonalShift(Mat Y,Vec D)
1026d84be18SBarry Smith {
1036d84be18SBarry Smith   int    i,start,end,ierr;
1046d84be18SBarry Smith 
10577c4ece6SBarry Smith   PetscValidHeaderSpecific(Y,MAT_COOKIE);
10690f02eecSBarry Smith   PetscValidHeaderSpecific(D,VEC_COOKIE);
1076d84be18SBarry Smith   if (Y->ops.shift) {
1086d84be18SBarry Smith     ierr = (*Y->ops.diagonalshift)(D,Y); CHKERRQ(ierr);
1096d84be18SBarry Smith   }
1106d84be18SBarry Smith   else {
1116d84be18SBarry Smith     int    vstart,vend;
1126d84be18SBarry Smith     Scalar *v;
1136d84be18SBarry Smith     ierr = VecGetOwnershipRange(D,&vstart,&vend); CHKERRQ(ierr);
1146d84be18SBarry Smith     ierr = MatGetOwnershipRange(Y,&start,&end); CHKERRQ(ierr);
1156d84be18SBarry Smith     if (vstart != start || vend != end)
116e3372554SBarry Smith       SETERRQ(1,0,"Vector shift not compatible with matrix");
1176d84be18SBarry Smith 
1186d84be18SBarry Smith     ierr = VecGetArray(D,&v); CHKERRQ(ierr);
1196d84be18SBarry Smith     for ( i=start; i<end; i++ ) {
1206d84be18SBarry Smith       ierr = MatSetValues(Y,1,&i,1,&i,v+i-start,ADD_VALUES); CHKERRQ(ierr);
1216d84be18SBarry Smith     }
1226d4a8577SBarry Smith     ierr = MatAssemblyBegin(Y,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
1236d4a8577SBarry Smith     ierr = MatAssemblyEnd(Y,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
1246d84be18SBarry Smith   }
1256d84be18SBarry Smith   return 0;
1266d84be18SBarry Smith }
127