Actual source code: ex2.c
1: /*$Id: ex2.c,v 1.25 2001/08/07 21:30:08 bsmith Exp $*/
3: static char help[] = "Tests MatTranspose(), MatNorm(), MatValid(), and MatAXPY().nn";
5: #include petscmat.h
7: #undef __FUNCT__
9: int main(int argc,char **argv)
10: {
11: Mat mat,tmat = 0;
12: int m = 7,n,i,j,ierr,size,rank,rstart,rend,rect = 0;
13: PetscTruth flg;
14: PetscScalar v;
15: PetscReal normf,normi,norm1;
17: PetscInitialize(&argc,&argv,(char*)0,help);
18: PetscViewerSetFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_COMMON);
19: PetscOptionsGetInt(PETSC_NULL,"-m",&m,PETSC_NULL);
20: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
21: MPI_Comm_size(PETSC_COMM_WORLD,&size);
22: n = m;
23: PetscOptionsHasName(PETSC_NULL,"-rectA",&flg);
24: if (flg) {n += 2; rect = 1;}
25: PetscOptionsHasName(PETSC_NULL,"-rectB",&flg);
26: if (flg) {n -= 2; rect = 1;}
28: /* ------- Assemble matrix, test MatValid() --------- */
30: MatCreate(PETSC_COMM_WORLD,PETSC_DECIDE,PETSC_DECIDE,m,n,&mat);
31: MatSetFromOptions(mat);
32: MatGetOwnershipRange(mat,&rstart,&rend);
33: for (i=rstart; i<rend; i++) {
34: for (j=0; j<n; j++) {
35: v=10*i+j;
36: MatSetValues(mat,1,&i,1,&j,&v,INSERT_VALUES);
37: }
38: }
39: MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);
40: MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);
42: /* Test whether matrix has been corrupted (just to demonstrate this
43: routine) not needed in most application codes. */
44: MatValid(mat,(PetscTruth*)&flg);
45: if (!flg) SETERRQ(1,"Corrupted matrix.");
47: /* ----------------- Test MatNorm() ----------------- */
49: MatNorm(mat,NORM_FROBENIUS,&normf);
50: MatNorm(mat,NORM_1,&norm1);
51: MatNorm(mat,NORM_INFINITY,&normi);
52: PetscPrintf(PETSC_COMM_WORLD,"original: Frobenious norm = %g, one norm = %g, infinity norm = %gn",
53: normf,norm1,normi);
54: MatView(mat,PETSC_VIEWER_STDOUT_WORLD);
56: /* --------------- Test MatTranspose() -------------- */
58: PetscOptionsHasName(PETSC_NULL,"-in_place",&flg);
59: if (!rect && flg) {
60: MatTranspose(mat,0); /* in-place transpose */
61: tmat = mat; mat = 0;
62: } else { /* out-of-place transpose */
63: MatTranspose(mat,&tmat);
64: }
66: /* ----------------- Test MatNorm() ----------------- */
68: /* Print info about transpose matrix */
69: MatNorm(tmat,NORM_FROBENIUS,&normf);
70: MatNorm(tmat,NORM_1,&norm1);
71: MatNorm(tmat,NORM_INFINITY,&normi);
72: PetscPrintf(PETSC_COMM_WORLD,"transpose: Frobenious norm = %g, one norm = %g, infinity norm = %gn",
73: normf,norm1,normi);
74: MatView(tmat,PETSC_VIEWER_STDOUT_WORLD);
76: /* ----------------- Test MatAXPY() ----------------- */
78: if (mat && !rect) {
79: PetscScalar alpha = 1.0;
80: PetscOptionsGetScalar(PETSC_NULL,"-alpha",&alpha,PETSC_NULL);
81: PetscPrintf(PETSC_COMM_WORLD,"matrix addition: B = B + alpha * An");
82: MatAXPY(&alpha,mat,tmat,DIFFERENT_NONZERO_PATTERN);
83: MatView(tmat,PETSC_VIEWER_STDOUT_WORLD);
84: }
86: /* Free data structures */
87: MatDestroy(tmat);
88: if (mat) {MatDestroy(mat);}
90: PetscFinalize();
91: return 0;
92: }
93: