xref: /petsc/src/mat/utils/getcolv.c (revision 0925cddd633754a1749601abe9f94d96888487ca)
1*0925cdddSBarry Smith #ifdef PETSC_RCS_HEADER
2*0925cdddSBarry Smith static char vcid[] = "$Id: axpy.c,v 1.30 1997/12/01 01:55:30 bsmith Exp $";
3*0925cdddSBarry Smith #endif
4*0925cdddSBarry Smith 
5*0925cdddSBarry Smith #include "src/mat/matimpl.h"  /*I   "mat.h"  I*/
6*0925cdddSBarry Smith 
7*0925cdddSBarry Smith #undef __FUNC__
8*0925cdddSBarry Smith #define __FUNC__ "MatAXPY"
9*0925cdddSBarry Smith /*@
10*0925cdddSBarry Smith    MatAXPY - Computes Y = a*X + Y.
11*0925cdddSBarry Smith 
12*0925cdddSBarry Smith    Input Parameters:
13*0925cdddSBarry Smith .  X,Y - the matrices
14*0925cdddSBarry Smith .  a - the scalar multiplier
15*0925cdddSBarry Smith 
16*0925cdddSBarry Smith    Contributed by: Matthew Knepley
17*0925cdddSBarry Smith 
18*0925cdddSBarry Smith .keywords: matrix, add
19*0925cdddSBarry Smith 
20*0925cdddSBarry Smith  @*/
21*0925cdddSBarry Smith int MatAXPY(Scalar *a,Mat X,Mat Y)
22*0925cdddSBarry Smith {
23*0925cdddSBarry Smith   int    m1,m2,n1,n2,i,*row,start,end,j,ncols,ierr;
24*0925cdddSBarry Smith   Scalar *val,*vals;
25*0925cdddSBarry Smith 
26*0925cdddSBarry Smith   PetscFunctionBegin;
27*0925cdddSBarry Smith   PetscValidHeaderSpecific(X,MAT_COOKIE);
28*0925cdddSBarry Smith   PetscValidHeaderSpecific(Y,MAT_COOKIE);
29*0925cdddSBarry Smith   PetscValidScalarPointer(a);
30*0925cdddSBarry Smith 
31*0925cdddSBarry Smith   MatGetSize(X,&m1,&n1);  MatGetSize(Y,&m2,&n2);
32*0925cdddSBarry Smith   if (m1 != m2 || n1 != n2) SETERRQ(PETSC_ERR_ARG_SIZ,0,"Non conforming matrix add");
33*0925cdddSBarry Smith 
34*0925cdddSBarry Smith   if (X->ops.axpy) {
35*0925cdddSBarry Smith     ierr = (*X->ops.axpy)(a,X,Y); CHKERRQ(ierr);
36*0925cdddSBarry Smith   } else {
37*0925cdddSBarry Smith     ierr = MatGetOwnershipRange(X,&start,&end); CHKERRQ(ierr);
38*0925cdddSBarry Smith     if (*a == 1.0) {
39*0925cdddSBarry Smith       for (i = start; i < end; i++) {
40*0925cdddSBarry Smith         ierr = MatGetRow(X, i, &ncols, &row, &vals);                 CHKERRQ(ierr);
41*0925cdddSBarry Smith         ierr = MatSetValues(Y, 1, &i, ncols, row, vals, ADD_VALUES); CHKERRQ(ierr);
42*0925cdddSBarry Smith         ierr = MatRestoreRow(X, i, &ncols, &row, &vals);             CHKERRQ(ierr);
43*0925cdddSBarry Smith       }
44*0925cdddSBarry Smith     } else {
45*0925cdddSBarry Smith       vals = (Scalar *) PetscMalloc( (n1+1)*sizeof(Scalar) ); CHKPTRQ(vals);
46*0925cdddSBarry Smith       for ( i=start; i<end; i++ ) {
47*0925cdddSBarry Smith         ierr = MatGetRow(X,i,&ncols,&row,&val); CHKERRQ(ierr);
48*0925cdddSBarry Smith         for ( j=0; j<ncols; j++ ) {
49*0925cdddSBarry Smith           vals[j] = (*a)*val[j];
50*0925cdddSBarry Smith         }
51*0925cdddSBarry Smith         ierr = MatSetValues(Y,1,&i,ncols,row,vals,ADD_VALUES); CHKERRQ(ierr);
52*0925cdddSBarry Smith         ierr = MatRestoreRow(X,i,&ncols,&row,&val); CHKERRQ(ierr);
53*0925cdddSBarry Smith       }
54*0925cdddSBarry Smith       PetscFree(vals);
55*0925cdddSBarry Smith     }
56*0925cdddSBarry Smith     ierr = MatAssemblyBegin(Y,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
57*0925cdddSBarry Smith     ierr = MatAssemblyEnd(Y,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
58*0925cdddSBarry Smith   }
59*0925cdddSBarry Smith   PetscFunctionReturn(0);
60*0925cdddSBarry Smith }
61*0925cdddSBarry Smith 
62*0925cdddSBarry Smith #undef __FUNC__
63*0925cdddSBarry Smith #define __FUNC__ "MatShift"
64*0925cdddSBarry Smith /*@
65*0925cdddSBarry Smith    MatShift - Computes Y =  Y + a I, where a is a scalar and I is the identity
66*0925cdddSBarry Smith    matrix.
67*0925cdddSBarry Smith 
68*0925cdddSBarry Smith    Input Parameters:
69*0925cdddSBarry Smith .  Y - the matrices
70*0925cdddSBarry Smith .  a - the scalar
71*0925cdddSBarry Smith 
72*0925cdddSBarry Smith .keywords: matrix, add, shift
73*0925cdddSBarry Smith 
74*0925cdddSBarry Smith .seealso: MatDiagonalShift()
75*0925cdddSBarry Smith  @*/
76*0925cdddSBarry Smith int MatShift(Scalar *a,Mat Y)
77*0925cdddSBarry Smith {
78*0925cdddSBarry Smith   int    i,start,end,ierr;
79*0925cdddSBarry Smith 
80*0925cdddSBarry Smith   PetscFunctionBegin;
81*0925cdddSBarry Smith   PetscValidHeaderSpecific(Y,MAT_COOKIE);
82*0925cdddSBarry Smith   PetscValidScalarPointer(a);
83*0925cdddSBarry Smith   if (Y->ops.shift) {
84*0925cdddSBarry Smith     ierr = (*Y->ops.shift)(a,Y); CHKERRQ(ierr);
85*0925cdddSBarry Smith   }
86*0925cdddSBarry Smith   else {
87*0925cdddSBarry Smith     ierr = MatGetOwnershipRange(Y,&start,&end); CHKERRQ(ierr);
88*0925cdddSBarry Smith     for ( i=start; i<end; i++ ) {
89*0925cdddSBarry Smith       ierr = MatSetValues(Y,1,&i,1,&i,a,ADD_VALUES); CHKERRQ(ierr);
90*0925cdddSBarry Smith     }
91*0925cdddSBarry Smith     ierr = MatAssemblyBegin(Y,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
92*0925cdddSBarry Smith     ierr = MatAssemblyEnd(Y,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
93*0925cdddSBarry Smith   }
94*0925cdddSBarry Smith   PetscFunctionReturn(0);
95*0925cdddSBarry Smith }
96*0925cdddSBarry Smith 
97*0925cdddSBarry Smith #undef __FUNC__
98*0925cdddSBarry Smith #define __FUNC__ "MatDiagonalShift"
99*0925cdddSBarry Smith /*@
100*0925cdddSBarry Smith    MatDiagonalShift - Computes Y = Y + D, where D is a diagonal matrix
101*0925cdddSBarry Smith    that is represented as a vector.
102*0925cdddSBarry Smith 
103*0925cdddSBarry Smith    Input Parameters:
104*0925cdddSBarry Smith .  Y - the input matrix
105*0925cdddSBarry Smith .  D - the diagonal matrix, represented as a vector
106*0925cdddSBarry Smith 
107*0925cdddSBarry Smith    Input Parameters:
108*0925cdddSBarry Smith .  Y - the shifted ouput matrix
109*0925cdddSBarry Smith 
110*0925cdddSBarry Smith .keywords: matrix, add, shift, diagonal
111*0925cdddSBarry Smith 
112*0925cdddSBarry Smith .seealso: MatShift()
113*0925cdddSBarry Smith @*/
114*0925cdddSBarry Smith int MatDiagonalShift(Mat Y,Vec D)
115*0925cdddSBarry Smith {
116*0925cdddSBarry Smith   int    i,start,end,ierr;
117*0925cdddSBarry Smith 
118*0925cdddSBarry Smith   PetscFunctionBegin;
119*0925cdddSBarry Smith   PetscValidHeaderSpecific(Y,MAT_COOKIE);
120*0925cdddSBarry Smith   PetscValidHeaderSpecific(D,VEC_COOKIE);
121*0925cdddSBarry Smith   if (Y->ops.shift) {
122*0925cdddSBarry Smith     ierr = (*Y->ops.diagonalshift)(D,Y); CHKERRQ(ierr);
123*0925cdddSBarry Smith   }
124*0925cdddSBarry Smith   else {
125*0925cdddSBarry Smith     int    vstart,vend;
126*0925cdddSBarry Smith     Scalar *v;
127*0925cdddSBarry Smith     ierr = VecGetOwnershipRange(D,&vstart,&vend); CHKERRQ(ierr);
128*0925cdddSBarry Smith     ierr = MatGetOwnershipRange(Y,&start,&end); CHKERRQ(ierr);
129*0925cdddSBarry Smith     if (vstart != start || vend != end) {
130*0925cdddSBarry Smith       SETERRQ(PETSC_ERR_ARG_SIZ,0,"Vector ownership range not compatible with matrix");
131*0925cdddSBarry Smith     }
132*0925cdddSBarry Smith 
133*0925cdddSBarry Smith     ierr = VecGetArray(D,&v); CHKERRQ(ierr);
134*0925cdddSBarry Smith     for ( i=start; i<end; i++ ) {
135*0925cdddSBarry Smith       ierr = MatSetValues(Y,1,&i,1,&i,v+i-start,ADD_VALUES); CHKERRQ(ierr);
136*0925cdddSBarry Smith     }
137*0925cdddSBarry Smith     ierr = MatAssemblyBegin(Y,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
138*0925cdddSBarry Smith     ierr = MatAssemblyEnd(Y,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
139*0925cdddSBarry Smith   }
140*0925cdddSBarry Smith   PetscFunctionReturn(0);
141*0925cdddSBarry Smith }
142*0925cdddSBarry Smith 
143*0925cdddSBarry Smith #undef __FUNC__
144*0925cdddSBarry Smith #define __FUNC__ "MatAYPX"
145*0925cdddSBarry Smith /*@
146*0925cdddSBarry Smith    MatAYPX - Computes Y = X + a*Y.
147*0925cdddSBarry Smith 
148*0925cdddSBarry Smith    Input Parameters:
149*0925cdddSBarry Smith .  X,Y - the matrices
150*0925cdddSBarry Smith .  a - the scalar multiplier
151*0925cdddSBarry Smith 
152*0925cdddSBarry Smith    Contributed by: Matthew Knepley
153*0925cdddSBarry Smith 
154*0925cdddSBarry Smith .keywords: matrix, add
155*0925cdddSBarry Smith 
156*0925cdddSBarry Smith  @*/
157*0925cdddSBarry Smith int MatAYPX(Scalar *a,Mat X,Mat Y)
158*0925cdddSBarry Smith {
159*0925cdddSBarry Smith   Scalar one = 1.0;
160*0925cdddSBarry Smith   int    mX, mY,nX, nY,ierr;
161*0925cdddSBarry Smith 
162*0925cdddSBarry Smith   PetscFunctionBegin;
163*0925cdddSBarry Smith   PetscValidHeaderSpecific(X, MAT_COOKIE);
164*0925cdddSBarry Smith   PetscValidHeaderSpecific(Y, MAT_COOKIE);
165*0925cdddSBarry Smith   PetscValidScalarPointer(a);
166*0925cdddSBarry Smith 
167*0925cdddSBarry Smith   MatGetSize(X, &mX, &nX);
168*0925cdddSBarry Smith   MatGetSize(X, &mY, &nY);
169*0925cdddSBarry Smith   if (mX != mY || nX != nY) SETERRQ(PETSC_ERR_ARG_SIZ,0,"Non conforming matrices");
170*0925cdddSBarry Smith 
171*0925cdddSBarry Smith   ierr = MatScale(a, Y);      CHKERRQ(ierr);
172*0925cdddSBarry Smith   ierr = MatAXPY(&one, X, Y); CHKERRQ(ierr);
173*0925cdddSBarry Smith   PetscFunctionReturn(0);
174*0925cdddSBarry Smith }
175