16f79c3a4SBarry Smith #ifndef lint 2*77c4ece6SBarry Smith static char vcid[] = "$Id: axpy.c,v 1.14 1996/03/04 05:16:31 bsmith Exp bsmith $"; 36f79c3a4SBarry Smith #endif 46f79c3a4SBarry Smith 548b35521SBarry Smith #include "matimpl.h" /*I "mat.h" I*/ 66f79c3a4SBarry Smith 706be10caSBarry Smith /*@ 89cf4f1e8SLois Curfman McInnes 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 21*77c4ece6SBarry Smith PetscValidHeaderSpecific(X,MAT_COOKIE); PetscValidHeaderSpecific(Y,MAT_COOKIE); 2206be10caSBarry Smith MatGetSize(X,&m1,&n1); MatGetSize(X,&m2,&n2); 23bbb6d6a8SBarry Smith if (m1 != m2 || n1 != n2) SETERRQ(1,"MatAXPY:Non conforming matrix add"); 241987afe7SBarry Smith 251987afe7SBarry Smith if (X->ops.axpy) { 261987afe7SBarry Smith ierr = (*X->ops.axpy)(a,X,Y); CHKERRQ(ierr); 271987afe7SBarry Smith } 281987afe7SBarry Smith else { 290452661fSBarry Smith vals = (Scalar *) PetscMalloc( n1*sizeof(Scalar) ); CHKPTRQ(vals); 3006be10caSBarry Smith MatGetOwnershipRange(X,&start,&end); 3106be10caSBarry Smith for ( i=start; i<end; i++ ) { 3206be10caSBarry Smith MatGetRow(X,i,&ncols,&row,&val); 3306be10caSBarry Smith for ( j=0; j<ncols; j++ ) { 3406be10caSBarry Smith vals[j] = (*a)*val[j]; 356f79c3a4SBarry Smith } 36dbb450caSBarry Smith ierr = MatSetValues(Y,1,&i,ncols,row,vals,ADD_VALUES); CHKERRQ(ierr); 3706be10caSBarry Smith MatRestoreRow(X,i,&ncols,&row,&val); 386f79c3a4SBarry Smith } 390452661fSBarry Smith PetscFree(vals); 4078b31e54SBarry Smith ierr = MatAssemblyBegin(Y,FINAL_ASSEMBLY); CHKERRQ(ierr); 4178b31e54SBarry Smith ierr = MatAssemblyEnd(Y,FINAL_ASSEMBLY); CHKERRQ(ierr); 421987afe7SBarry Smith } 436f79c3a4SBarry Smith return 0; 446f79c3a4SBarry Smith } 45052efed2SBarry Smith 46052efed2SBarry Smith /*@ 47052efed2SBarry Smith MatShift - Computes Y = Y + a I 48052efed2SBarry Smith 49052efed2SBarry Smith Input Parameters: 50052efed2SBarry Smith . Y - the matrices 51052efed2SBarry Smith . a - the scalar 52052efed2SBarry Smith 536d84be18SBarry Smith .seealso: MatDiagonalShift() 546d84be18SBarry Smith 55052efed2SBarry Smith .keywords: matrix, add, shift 56052efed2SBarry Smith @*/ 57052efed2SBarry Smith int MatShift(Scalar *a,Mat Y) 58052efed2SBarry Smith { 59052efed2SBarry Smith int i,start,end,ierr; 60052efed2SBarry Smith 61*77c4ece6SBarry Smith PetscValidHeaderSpecific(Y,MAT_COOKIE); 62052efed2SBarry Smith if (Y->ops.shift) { 63052efed2SBarry Smith ierr = (*Y->ops.shift)(a,Y); CHKERRQ(ierr); 64052efed2SBarry Smith } 65052efed2SBarry Smith else { 66052efed2SBarry Smith MatGetOwnershipRange(Y,&start,&end); 67052efed2SBarry Smith for ( i=start; i<end; i++ ) { 68052efed2SBarry Smith ierr = MatSetValues(Y,1,&i,1,&i,a,ADD_VALUES); CHKERRQ(ierr); 69052efed2SBarry Smith } 70052efed2SBarry Smith ierr = MatAssemblyBegin(Y,FINAL_ASSEMBLY); CHKERRQ(ierr); 71052efed2SBarry Smith ierr = MatAssemblyEnd(Y,FINAL_ASSEMBLY); CHKERRQ(ierr); 72052efed2SBarry Smith } 73052efed2SBarry Smith return 0; 74052efed2SBarry Smith } 756d84be18SBarry Smith 766d84be18SBarry Smith /*@ 776d84be18SBarry Smith MatDiagonalShift - Computes Y = Y + D. Where D is a diagonal matrix 786d84be18SBarry Smith represented as a vector. 796d84be18SBarry Smith 806d84be18SBarry Smith Input Parameters: 816d84be18SBarry Smith . Y - the matrices 826d84be18SBarry Smith . D - the diagonal matrix, represented as a vector 836d84be18SBarry Smith 846d84be18SBarry Smith .seealso: MatDiagonalShift() 856d84be18SBarry Smith 866d84be18SBarry Smith .keywords: matrix, add, shift 876d84be18SBarry Smith @*/ 886d84be18SBarry Smith int MatDiagonalShift(Mat Y,Vec D) 896d84be18SBarry Smith { 906d84be18SBarry Smith int i,start,end,ierr; 916d84be18SBarry Smith 92*77c4ece6SBarry Smith PetscValidHeaderSpecific(Y,MAT_COOKIE); 936d84be18SBarry Smith if (Y->ops.shift) { 946d84be18SBarry Smith ierr = (*Y->ops.diagonalshift)(D,Y); CHKERRQ(ierr); 956d84be18SBarry Smith } 966d84be18SBarry Smith else { 976d84be18SBarry Smith int vstart,vend; 986d84be18SBarry Smith Scalar *v; 996d84be18SBarry Smith ierr = VecGetOwnershipRange(D,&vstart,&vend); CHKERRQ(ierr); 1006d84be18SBarry Smith ierr = MatGetOwnershipRange(Y,&start,&end); CHKERRQ(ierr); 1016d84be18SBarry Smith if (vstart != start || vend != end) 1026d84be18SBarry Smith SETERRQ(1,"MatDiagonalShift:Vector shift not compatible with matrix"); 1036d84be18SBarry Smith 1046d84be18SBarry Smith ierr = VecGetArray(D,&v); CHKERRQ(ierr); 1056d84be18SBarry Smith for ( i=start; i<end; i++ ) { 1066d84be18SBarry Smith ierr = MatSetValues(Y,1,&i,1,&i,v+i-start,ADD_VALUES); CHKERRQ(ierr); 1076d84be18SBarry Smith } 1086d84be18SBarry Smith ierr = MatAssemblyBegin(Y,FINAL_ASSEMBLY); CHKERRQ(ierr); 1096d84be18SBarry Smith ierr = MatAssemblyEnd(Y,FINAL_ASSEMBLY); CHKERRQ(ierr); 1106d84be18SBarry Smith } 1116d84be18SBarry Smith return 0; 1126d84be18SBarry Smith } 113