xref: /petsc/src/mat/utils/axpy.c (revision 90f02eec332fcca4c33b4e7b21cabed76bf0614e)
16f79c3a4SBarry Smith #ifndef lint
2*90f02eecSBarry Smith static char vcid[] = "$Id: axpy.c,v 1.19 1996/08/08 14:44:19 bsmith Exp bsmith $";
36f79c3a4SBarry Smith #endif
46f79c3a4SBarry Smith 
570f55243SBarry 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);
22*90f02eecSBarry Smith   PetscValidScalarPointer(a);
23*90f02eecSBarry Smith 
2406be10caSBarry Smith   MatGetSize(X,&m1,&n1);  MatGetSize(X,&m2,&n2);
25bbb6d6a8SBarry Smith   if (m1 != m2 || n1 != n2) SETERRQ(1,"MatAXPY:Non conforming matrix add");
261987afe7SBarry Smith 
271987afe7SBarry Smith   if (X->ops.axpy) {
281987afe7SBarry Smith     ierr = (*X->ops.axpy)(a,X,Y); CHKERRQ(ierr);
291987afe7SBarry Smith   }
301987afe7SBarry Smith   else {
310452661fSBarry Smith     vals = (Scalar *) PetscMalloc( n1*sizeof(Scalar) ); CHKPTRQ(vals);
32*90f02eecSBarry Smith     ierr = MatGetOwnershipRange(X,&start,&end); CHKERRQ(ierr);
3306be10caSBarry Smith     for ( i=start; i<end; i++ ) {
34*90f02eecSBarry Smith       ierr = MatGetRow(X,i,&ncols,&row,&val); CHKERRQ(ierr);
3506be10caSBarry Smith       for ( j=0; j<ncols; j++ ) {
3606be10caSBarry Smith         vals[j] = (*a)*val[j];
376f79c3a4SBarry Smith       }
38dbb450caSBarry Smith       ierr = MatSetValues(Y,1,&i,ncols,row,vals,ADD_VALUES); CHKERRQ(ierr);
39*90f02eecSBarry Smith       ierr = MatRestoreRow(X,i,&ncols,&row,&val); CHKERRQ(ierr);
406f79c3a4SBarry Smith     }
410452661fSBarry Smith     PetscFree(vals);
426d4a8577SBarry Smith     ierr = MatAssemblyBegin(Y,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
436d4a8577SBarry Smith     ierr = MatAssemblyEnd(Y,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
441987afe7SBarry Smith   }
456f79c3a4SBarry Smith   return 0;
466f79c3a4SBarry Smith }
47052efed2SBarry Smith 
48052efed2SBarry Smith /*@
496b9ee512SLois Curfman McInnes    MatShift - Computes Y =  Y + a I, where a is a scalar and I is the identity
506b9ee512SLois Curfman McInnes    matrix.
51052efed2SBarry Smith 
52052efed2SBarry Smith    Input Parameters:
53052efed2SBarry Smith .  Y - the matrices
54052efed2SBarry Smith .  a - the scalar
55052efed2SBarry Smith 
56052efed2SBarry Smith .keywords: matrix, add, shift
576b9ee512SLois Curfman McInnes 
586b9ee512SLois Curfman McInnes .seealso: MatDiagonalShift()
59052efed2SBarry Smith  @*/
60052efed2SBarry Smith int MatShift(Scalar *a,Mat Y)
61052efed2SBarry Smith {
62052efed2SBarry Smith   int    i,start,end,ierr;
63052efed2SBarry Smith 
6477c4ece6SBarry Smith   PetscValidHeaderSpecific(Y,MAT_COOKIE);
65*90f02eecSBarry Smith   PetscValidScalarPointer(a);
66052efed2SBarry Smith   if (Y->ops.shift) {
67052efed2SBarry Smith     ierr = (*Y->ops.shift)(a,Y); CHKERRQ(ierr);
68052efed2SBarry Smith   }
69052efed2SBarry Smith   else {
70052efed2SBarry Smith     MatGetOwnershipRange(Y,&start,&end);
71052efed2SBarry Smith     for ( i=start; i<end; i++ ) {
72052efed2SBarry Smith       ierr = MatSetValues(Y,1,&i,1,&i,a,ADD_VALUES); CHKERRQ(ierr);
73052efed2SBarry Smith     }
746d4a8577SBarry Smith     ierr = MatAssemblyBegin(Y,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
756d4a8577SBarry Smith     ierr = MatAssemblyEnd(Y,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
76052efed2SBarry Smith   }
77052efed2SBarry Smith   return 0;
78052efed2SBarry Smith }
796d84be18SBarry Smith 
806d84be18SBarry Smith /*@
816b9ee512SLois Curfman McInnes    MatDiagonalShift - Computes Y = Y + D, where D is a diagonal matrix
826b9ee512SLois Curfman McInnes    that is represented as a vector.
836d84be18SBarry Smith 
846d84be18SBarry Smith    Input Parameters:
856b9ee512SLois Curfman McInnes .  Y - the input matrix
866d84be18SBarry Smith .  D - the diagonal matrix, represented as a vector
876d84be18SBarry Smith 
886b9ee512SLois Curfman McInnes    Input Parameters:
896b9ee512SLois Curfman McInnes .  Y - the shifted ouput matrix
906d84be18SBarry Smith 
916b9ee512SLois Curfman McInnes .keywords: matrix, add, shift, diagonal
926b9ee512SLois Curfman McInnes 
936b9ee512SLois Curfman McInnes .seealso: MatShift()
946d84be18SBarry Smith @*/
956d84be18SBarry Smith int MatDiagonalShift(Mat Y,Vec D)
966d84be18SBarry Smith {
976d84be18SBarry Smith   int    i,start,end,ierr;
986d84be18SBarry Smith 
9977c4ece6SBarry Smith   PetscValidHeaderSpecific(Y,MAT_COOKIE);
100*90f02eecSBarry Smith   PetscValidHeaderSpecific(D,VEC_COOKIE);
1016d84be18SBarry Smith   if (Y->ops.shift) {
1026d84be18SBarry Smith     ierr = (*Y->ops.diagonalshift)(D,Y); CHKERRQ(ierr);
1036d84be18SBarry Smith   }
1046d84be18SBarry Smith   else {
1056d84be18SBarry Smith     int    vstart,vend;
1066d84be18SBarry Smith     Scalar *v;
1076d84be18SBarry Smith     ierr = VecGetOwnershipRange(D,&vstart,&vend); CHKERRQ(ierr);
1086d84be18SBarry Smith     ierr = MatGetOwnershipRange(Y,&start,&end); CHKERRQ(ierr);
1096d84be18SBarry Smith     if (vstart != start || vend != end)
1106d84be18SBarry Smith       SETERRQ(1,"MatDiagonalShift:Vector shift not compatible with matrix");
1116d84be18SBarry Smith 
1126d84be18SBarry Smith     ierr = VecGetArray(D,&v); CHKERRQ(ierr);
1136d84be18SBarry Smith     for ( i=start; i<end; i++ ) {
1146d84be18SBarry Smith       ierr = MatSetValues(Y,1,&i,1,&i,v+i-start,ADD_VALUES); CHKERRQ(ierr);
1156d84be18SBarry Smith     }
1166d4a8577SBarry Smith     ierr = MatAssemblyBegin(Y,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
1176d4a8577SBarry Smith     ierr = MatAssemblyEnd(Y,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
1186d84be18SBarry Smith   }
1196d84be18SBarry Smith   return 0;
1206d84be18SBarry Smith }
121