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: int main(int argc,char **argv)
8: {
9: Mat mat,tmat = 0;
10: int m = 7,n,i,j,ierr,size,rank,rstart,rend,rect = 0;
11: PetscTruth flg;
12: PetscScalar v;
13: PetscReal normf,normi,norm1;
15: PetscInitialize(&argc,&argv,(char*)0,help);
16: PetscViewerSetFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_COMMON);
17: PetscOptionsGetInt(PETSC_NULL,"-m",&m,PETSC_NULL);
18: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
19: MPI_Comm_size(PETSC_COMM_WORLD,&size);
20: n = m;
21: PetscOptionsHasName(PETSC_NULL,"-rectA",&flg);
22: if (flg) {n += 2; rect = 1;}
23: PetscOptionsHasName(PETSC_NULL,"-rectB",&flg);
24: if (flg) {n -= 2; rect = 1;}
26: /* ------- Assemble matrix, test MatValid() --------- */
28: MatCreate(PETSC_COMM_WORLD,m,PETSC_DECIDE,PETSC_DECIDE,n,&mat);
29: MatSetFromOptions(mat);
30: MatGetOwnershipRange(mat,&rstart,&rend);
31: for (i=rstart; i<rend; i++) {
32: for (j=0; j<n; j++) {
33: v=10*i+j;
34: MatSetValues(mat,1,&i,1,&j,&v,INSERT_VALUES);
35: }
36: }
37: MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);
38: MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);
40: /* Test whether matrix has been corrupted (just to demonstrate this
41: routine) not needed in most application codes. */
42: MatValid(mat,(PetscTruth*)&flg);
43: if (!flg) SETERRQ(1,"Corrupted matrix.");
45: /* ----------------- Test MatNorm() ----------------- */
47: MatNorm(mat,NORM_FROBENIUS,&normf);
48: MatNorm(mat,NORM_1,&norm1);
49: MatNorm(mat,NORM_INFINITY,&normi);
50: PetscPrintf(PETSC_COMM_WORLD,"original: Frobenious norm = %g, one norm = %g, infinity norm = %gn",
51: normf,norm1,normi);
52: MatView(mat,PETSC_VIEWER_STDOUT_WORLD);
54: /* --------------- Test MatTranspose() -------------- */
56: PetscOptionsHasName(PETSC_NULL,"-in_place",&flg);
57: if (!rect && flg) {
58: MatTranspose(mat,0); /* in-place transpose */
59: tmat = mat; mat = 0;
60: } else { /* out-of-place transpose */
61: MatTranspose(mat,&tmat);
62: }
64: /* ----------------- Test MatNorm() ----------------- */
66: /* Print info about transpose matrix */
67: MatNorm(tmat,NORM_FROBENIUS,&normf);
68: MatNorm(tmat,NORM_1,&norm1);
69: MatNorm(tmat,NORM_INFINITY,&normi);
70: PetscPrintf(PETSC_COMM_WORLD,"transpose: Frobenious norm = %g, one norm = %g, infinity norm = %gn",
71: normf,norm1,normi);
72: MatView(tmat,PETSC_VIEWER_STDOUT_WORLD);
74: /* ----------------- Test MatAXPY() ----------------- */
76: if (mat && !rect) {
77: PetscScalar alpha = 1.0;
78: PetscOptionsGetScalar(PETSC_NULL,"-alpha",&alpha,PETSC_NULL);
79: PetscPrintf(PETSC_COMM_WORLD,"matrix addition: B = B + alpha * An");
80: MatAXPY(&alpha,mat,tmat,DIFFERENT_NONZERO_PATTERN);
81: MatView(tmat,PETSC_VIEWER_STDOUT_WORLD);
82: }
84: /* Free data structures */
85: MatDestroy(tmat);
86: if (mat) {MatDestroy(mat);}
88: PetscFinalize();
89: return 0;
90: }
91: