xref: /petsc/src/mat/utils/axpy.c (revision 77c4ece699e97450631aa6fc5b0ef04ff52df029)
1 #ifndef lint
2 static char vcid[] = "$Id: axpy.c,v 1.14 1996/03/04 05:16:31 bsmith Exp bsmith $";
3 #endif
4 
5 #include "matimpl.h"  /*I   "mat.h"  I*/
6 
7 /*@
8    MatAXPY - Computes Y = a*X + Y
9 
10    Input Parameters:
11 .  X,Y - the matrices
12 .  a - the scalar multiplier
13 
14 .keywords: matrix, add
15  @*/
16 int MatAXPY(Scalar *a,Mat X,Mat Y)
17 {
18   int    m1,m2,n1,n2,i,*row,start,end,j,ncols,ierr;
19   Scalar *val,*vals;
20 
21   PetscValidHeaderSpecific(X,MAT_COOKIE);  PetscValidHeaderSpecific(Y,MAT_COOKIE);
22   MatGetSize(X,&m1,&n1);  MatGetSize(X,&m2,&n2);
23   if (m1 != m2 || n1 != n2) SETERRQ(1,"MatAXPY:Non conforming matrix add");
24 
25   if (X->ops.axpy) {
26     ierr = (*X->ops.axpy)(a,X,Y); CHKERRQ(ierr);
27   }
28   else {
29     vals = (Scalar *) PetscMalloc( n1*sizeof(Scalar) ); CHKPTRQ(vals);
30     MatGetOwnershipRange(X,&start,&end);
31     for ( i=start; i<end; i++ ) {
32       MatGetRow(X,i,&ncols,&row,&val);
33       for ( j=0; j<ncols; j++ ) {
34         vals[j] = (*a)*val[j];
35       }
36       ierr = MatSetValues(Y,1,&i,ncols,row,vals,ADD_VALUES); CHKERRQ(ierr);
37       MatRestoreRow(X,i,&ncols,&row,&val);
38     }
39     PetscFree(vals);
40     ierr = MatAssemblyBegin(Y,FINAL_ASSEMBLY); CHKERRQ(ierr);
41     ierr = MatAssemblyEnd(Y,FINAL_ASSEMBLY); CHKERRQ(ierr);
42   }
43   return 0;
44 }
45 
46 /*@
47    MatShift - Computes Y =  Y + a I
48 
49    Input Parameters:
50 .  Y - the matrices
51 .  a - the scalar
52 
53 .seealso: MatDiagonalShift()
54 
55 .keywords: matrix, add, shift
56  @*/
57 int MatShift(Scalar *a,Mat Y)
58 {
59   int    i,start,end,ierr;
60 
61   PetscValidHeaderSpecific(Y,MAT_COOKIE);
62   if (Y->ops.shift) {
63     ierr = (*Y->ops.shift)(a,Y); CHKERRQ(ierr);
64   }
65   else {
66     MatGetOwnershipRange(Y,&start,&end);
67     for ( i=start; i<end; i++ ) {
68       ierr = MatSetValues(Y,1,&i,1,&i,a,ADD_VALUES); CHKERRQ(ierr);
69     }
70     ierr = MatAssemblyBegin(Y,FINAL_ASSEMBLY); CHKERRQ(ierr);
71     ierr = MatAssemblyEnd(Y,FINAL_ASSEMBLY); CHKERRQ(ierr);
72   }
73   return 0;
74 }
75 
76 /*@
77    MatDiagonalShift - Computes Y =  Y + D. Where D is a diagonal matrix
78         represented as a vector.
79 
80    Input Parameters:
81 .  Y - the matrices
82 .  D - the diagonal matrix, represented as a vector
83 
84 .seealso: MatDiagonalShift()
85 
86 .keywords: matrix, add, shift
87  @*/
88 int MatDiagonalShift(Mat Y,Vec D)
89 {
90   int    i,start,end,ierr;
91 
92   PetscValidHeaderSpecific(Y,MAT_COOKIE);
93   if (Y->ops.shift) {
94     ierr = (*Y->ops.diagonalshift)(D,Y); CHKERRQ(ierr);
95   }
96   else {
97     int    vstart,vend;
98     Scalar *v;
99     ierr = VecGetOwnershipRange(D,&vstart,&vend); CHKERRQ(ierr);
100     ierr = MatGetOwnershipRange(Y,&start,&end); CHKERRQ(ierr);
101     if (vstart != start || vend != end)
102       SETERRQ(1,"MatDiagonalShift:Vector shift not compatible with matrix");
103 
104     ierr = VecGetArray(D,&v); CHKERRQ(ierr);
105     for ( i=start; i<end; i++ ) {
106       ierr = MatSetValues(Y,1,&i,1,&i,v+i-start,ADD_VALUES); CHKERRQ(ierr);
107     }
108     ierr = MatAssemblyBegin(Y,FINAL_ASSEMBLY); CHKERRQ(ierr);
109     ierr = MatAssemblyEnd(Y,FINAL_ASSEMBLY); CHKERRQ(ierr);
110   }
111   return 0;
112 }
113