xref: /petsc/src/mat/utils/axpy.c (revision 98a79cdb33ba8153fc7d563227bccbb966faeb05)
1a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER
2*98a79cdbSBarry Smith static char vcid[] = "$Id: axpy.c,v 1.32 1998/04/13 17:43:46 bsmith Exp bsmith $";
36f79c3a4SBarry Smith #endif
46f79c3a4SBarry Smith 
570f55243SBarry Smith #include "src/mat/matimpl.h"  /*I   "mat.h"  I*/
66f79c3a4SBarry Smith 
75615d1e5SSatish Balay #undef __FUNC__
85615d1e5SSatish Balay #define __FUNC__ "MatAXPY"
906be10caSBarry Smith /*@
1021c89e3eSBarry Smith    MatAXPY - Computes Y = a*X + Y.
116f79c3a4SBarry Smith 
12fee21e36SBarry Smith    Collective on Mat
13fee21e36SBarry Smith 
14*98a79cdbSBarry Smith    Input Parameters:
15*98a79cdbSBarry Smith +  X,Y - the matrices
16*98a79cdbSBarry Smith -  a - the scalar multiplier
17*98a79cdbSBarry Smith 
18e182c471SBarry Smith    Contributed by: Matthew Knepley
19d4bb536fSBarry Smith 
209cf4f1e8SLois Curfman McInnes .keywords: matrix, add
21d4bb536fSBarry Smith 
2206be10caSBarry Smith  @*/
2306be10caSBarry Smith int MatAXPY(Scalar *a,Mat X,Mat Y)
246f79c3a4SBarry Smith {
2506be10caSBarry Smith   int    m1,m2,n1,n2,i,*row,start,end,j,ncols,ierr;
2606be10caSBarry Smith   Scalar *val,*vals;
276f79c3a4SBarry Smith 
283a40ed3dSBarry Smith   PetscFunctionBegin;
29d4bb536fSBarry Smith   PetscValidHeaderSpecific(X,MAT_COOKIE);
30d4bb536fSBarry Smith   PetscValidHeaderSpecific(Y,MAT_COOKIE);
3190f02eecSBarry Smith   PetscValidScalarPointer(a);
3290f02eecSBarry Smith 
33d4bb536fSBarry Smith   MatGetSize(X,&m1,&n1);  MatGetSize(Y,&m2,&n2);
34a8c6a408SBarry Smith   if (m1 != m2 || n1 != n2) SETERRQ(PETSC_ERR_ARG_SIZ,0,"Non conforming matrix add");
351987afe7SBarry Smith 
36f830108cSBarry Smith   if (X->ops->axpy) {
37f830108cSBarry Smith     ierr = (*X->ops->axpy)(a,X,Y); CHKERRQ(ierr);
38d4bb536fSBarry Smith   } else {
3990f02eecSBarry Smith     ierr = MatGetOwnershipRange(X,&start,&end); CHKERRQ(ierr);
40d4bb536fSBarry Smith     if (*a == 1.0) {
41d4bb536fSBarry Smith       for (i = start; i < end; i++) {
42d4bb536fSBarry Smith         ierr = MatGetRow(X, i, &ncols, &row, &vals);                 CHKERRQ(ierr);
43d4bb536fSBarry Smith         ierr = MatSetValues(Y, 1, &i, ncols, row, vals, ADD_VALUES); CHKERRQ(ierr);
44d4bb536fSBarry Smith         ierr = MatRestoreRow(X, i, &ncols, &row, &vals);             CHKERRQ(ierr);
45d4bb536fSBarry Smith       }
46d4bb536fSBarry Smith     } else {
47d4bb536fSBarry Smith       vals = (Scalar *) PetscMalloc( (n1+1)*sizeof(Scalar) ); CHKPTRQ(vals);
4806be10caSBarry Smith       for ( i=start; i<end; i++ ) {
4990f02eecSBarry Smith         ierr = MatGetRow(X,i,&ncols,&row,&val); CHKERRQ(ierr);
5006be10caSBarry Smith         for ( j=0; j<ncols; j++ ) {
5106be10caSBarry Smith           vals[j] = (*a)*val[j];
526f79c3a4SBarry Smith         }
53dbb450caSBarry Smith         ierr = MatSetValues(Y,1,&i,ncols,row,vals,ADD_VALUES); CHKERRQ(ierr);
5490f02eecSBarry Smith         ierr = MatRestoreRow(X,i,&ncols,&row,&val); CHKERRQ(ierr);
556f79c3a4SBarry Smith       }
560452661fSBarry Smith       PetscFree(vals);
57d4bb536fSBarry Smith     }
586d4a8577SBarry Smith     ierr = MatAssemblyBegin(Y,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
596d4a8577SBarry Smith     ierr = MatAssemblyEnd(Y,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
601987afe7SBarry Smith   }
613a40ed3dSBarry Smith   PetscFunctionReturn(0);
626f79c3a4SBarry Smith }
63052efed2SBarry Smith 
645615d1e5SSatish Balay #undef __FUNC__
655615d1e5SSatish Balay #define __FUNC__ "MatShift"
66052efed2SBarry Smith /*@
676b9ee512SLois Curfman McInnes    MatShift - Computes Y =  Y + a I, where a is a scalar and I is the identity
686b9ee512SLois Curfman McInnes    matrix.
69052efed2SBarry Smith 
70fee21e36SBarry Smith    Collective on Mat
71fee21e36SBarry Smith 
72*98a79cdbSBarry Smith    Input Parameters:
73*98a79cdbSBarry Smith +  Y - the matrices
74*98a79cdbSBarry Smith -  a - the scalar
75*98a79cdbSBarry Smith 
76052efed2SBarry Smith .keywords: matrix, add, shift
776b9ee512SLois Curfman McInnes 
786b9ee512SLois Curfman McInnes .seealso: MatDiagonalShift()
79052efed2SBarry Smith  @*/
80052efed2SBarry Smith int MatShift(Scalar *a,Mat Y)
81052efed2SBarry Smith {
82052efed2SBarry Smith   int    i,start,end,ierr;
83052efed2SBarry Smith 
843a40ed3dSBarry Smith   PetscFunctionBegin;
8577c4ece6SBarry Smith   PetscValidHeaderSpecific(Y,MAT_COOKIE);
8690f02eecSBarry Smith   PetscValidScalarPointer(a);
87f830108cSBarry Smith   if (Y->ops->shift) {
88f830108cSBarry Smith     ierr = (*Y->ops->shift)(a,Y); CHKERRQ(ierr);
89052efed2SBarry Smith   }
90052efed2SBarry Smith   else {
91d4bb536fSBarry Smith     ierr = MatGetOwnershipRange(Y,&start,&end); CHKERRQ(ierr);
92052efed2SBarry Smith     for ( i=start; i<end; i++ ) {
93052efed2SBarry Smith       ierr = MatSetValues(Y,1,&i,1,&i,a,ADD_VALUES); CHKERRQ(ierr);
94052efed2SBarry Smith     }
956d4a8577SBarry Smith     ierr = MatAssemblyBegin(Y,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
966d4a8577SBarry Smith     ierr = MatAssemblyEnd(Y,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
97052efed2SBarry Smith   }
983a40ed3dSBarry Smith   PetscFunctionReturn(0);
99052efed2SBarry Smith }
1006d84be18SBarry Smith 
1015615d1e5SSatish Balay #undef __FUNC__
1025615d1e5SSatish Balay #define __FUNC__ "MatDiagonalShift"
1036d84be18SBarry Smith /*@
1046b9ee512SLois Curfman McInnes    MatDiagonalShift - Computes Y = Y + D, where D is a diagonal matrix
1056b9ee512SLois Curfman McInnes    that is represented as a vector.
1066d84be18SBarry Smith 
1076d84be18SBarry Smith    Input Parameters:
108*98a79cdbSBarry Smith +  Y - the input matrix
109*98a79cdbSBarry Smith -  D - the diagonal matrix, represented as a vector
1106d84be18SBarry Smith 
1116b9ee512SLois Curfman McInnes    Input Parameters:
1126b9ee512SLois Curfman McInnes .  Y - the shifted ouput matrix
1136d84be18SBarry Smith 
114fee21e36SBarry Smith    Collective on Mat and Vec
115fee21e36SBarry Smith 
1166b9ee512SLois Curfman McInnes .keywords: matrix, add, shift, diagonal
1176b9ee512SLois Curfman McInnes 
1186b9ee512SLois Curfman McInnes .seealso: MatShift()
1196d84be18SBarry Smith @*/
1206d84be18SBarry Smith int MatDiagonalShift(Mat Y,Vec D)
1216d84be18SBarry Smith {
1226d84be18SBarry Smith   int    i,start,end,ierr;
1236d84be18SBarry Smith 
1243a40ed3dSBarry Smith   PetscFunctionBegin;
12577c4ece6SBarry Smith   PetscValidHeaderSpecific(Y,MAT_COOKIE);
12690f02eecSBarry Smith   PetscValidHeaderSpecific(D,VEC_COOKIE);
127f830108cSBarry Smith   if (Y->ops->shift) {
128f830108cSBarry Smith     ierr = (*Y->ops->diagonalshift)(D,Y); CHKERRQ(ierr);
1296d84be18SBarry Smith   }
1306d84be18SBarry Smith   else {
1316d84be18SBarry Smith     int    vstart,vend;
1326d84be18SBarry Smith     Scalar *v;
1336d84be18SBarry Smith     ierr = VecGetOwnershipRange(D,&vstart,&vend); CHKERRQ(ierr);
1346d84be18SBarry Smith     ierr = MatGetOwnershipRange(Y,&start,&end); CHKERRQ(ierr);
135d4bb536fSBarry Smith     if (vstart != start || vend != end) {
136a8c6a408SBarry Smith       SETERRQ(PETSC_ERR_ARG_SIZ,0,"Vector ownership range not compatible with matrix");
137d4bb536fSBarry Smith     }
1386d84be18SBarry Smith 
1396d84be18SBarry Smith     ierr = VecGetArray(D,&v); CHKERRQ(ierr);
1406d84be18SBarry Smith     for ( i=start; i<end; i++ ) {
1416d84be18SBarry Smith       ierr = MatSetValues(Y,1,&i,1,&i,v+i-start,ADD_VALUES); CHKERRQ(ierr);
1426d84be18SBarry Smith     }
1436d4a8577SBarry Smith     ierr = MatAssemblyBegin(Y,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
1446d4a8577SBarry Smith     ierr = MatAssemblyEnd(Y,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
1456d84be18SBarry Smith   }
1463a40ed3dSBarry Smith   PetscFunctionReturn(0);
1476d84be18SBarry Smith }
148d4bb536fSBarry Smith 
149d4bb536fSBarry Smith #undef __FUNC__
150d4bb536fSBarry Smith #define __FUNC__ "MatAYPX"
151d4bb536fSBarry Smith /*@
152d4bb536fSBarry Smith    MatAYPX - Computes Y = X + a*Y.
153d4bb536fSBarry Smith 
154fee21e36SBarry Smith    Collective on Mat
155fee21e36SBarry Smith 
156*98a79cdbSBarry Smith    Input Parameters:
157*98a79cdbSBarry Smith +  X,Y - the matrices
158*98a79cdbSBarry Smith -  a - the scalar multiplier
159*98a79cdbSBarry Smith 
160e182c471SBarry Smith    Contributed by: Matthew Knepley
161d4bb536fSBarry Smith 
162d4bb536fSBarry Smith .keywords: matrix, add
163d4bb536fSBarry Smith 
164d4bb536fSBarry Smith  @*/
165d4bb536fSBarry Smith int MatAYPX(Scalar *a,Mat X,Mat Y)
166d4bb536fSBarry Smith {
167d4bb536fSBarry Smith   Scalar one = 1.0;
168d4bb536fSBarry Smith   int    mX, mY,nX, nY,ierr;
169d4bb536fSBarry Smith 
1703a40ed3dSBarry Smith   PetscFunctionBegin;
171d4bb536fSBarry Smith   PetscValidHeaderSpecific(X, MAT_COOKIE);
172d4bb536fSBarry Smith   PetscValidHeaderSpecific(Y, MAT_COOKIE);
173d4bb536fSBarry Smith   PetscValidScalarPointer(a);
174d4bb536fSBarry Smith 
175d4bb536fSBarry Smith   MatGetSize(X, &mX, &nX);
176d4bb536fSBarry Smith   MatGetSize(X, &mY, &nY);
177a8c6a408SBarry Smith   if (mX != mY || nX != nY) SETERRQ(PETSC_ERR_ARG_SIZ,0,"Non conforming matrices");
178d4bb536fSBarry Smith 
179d4bb536fSBarry Smith   ierr = MatScale(a, Y);      CHKERRQ(ierr);
180d4bb536fSBarry Smith   ierr = MatAXPY(&one, X, Y); CHKERRQ(ierr);
1813a40ed3dSBarry Smith   PetscFunctionReturn(0);
182d4bb536fSBarry Smith }
183