Actual source code: test2.c

  1: /*
  2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
  4:    Copyright (c) 2002-2012, Universitat Politecnica de Valencia, Spain

  6:    This file is part of SLEPc.
  7:       
  8:    SLEPc is free software: you can redistribute it and/or modify it under  the
  9:    terms of version 3 of the GNU Lesser General Public License as published by
 10:    the Free Software Foundation.

 12:    SLEPc  is  distributed in the hope that it will be useful, but WITHOUT  ANY 
 13:    WARRANTY;  without even the implied warranty of MERCHANTABILITY or  FITNESS 
 14:    FOR  A  PARTICULAR PURPOSE. See the GNU Lesser General Public  License  for 
 15:    more details.

 17:    You  should have received a copy of the GNU Lesser General  Public  License
 18:    along with SLEPc. If not, see <http://www.gnu.org/licenses/>.
 19:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 20: */

 22: static char help[] = "Test DSHEP.\n\n";

 24:  #include slepcds.h

 28: int main( int argc, char **argv )
 29: {
 31:   DS             ds;
 32:   PetscScalar    *A,*eig;
 33:   PetscInt       i,j,n=10,ld;
 34:   PetscViewer    viewer;
 35:   PetscBool      verbose,extrarow;

 37:   SlepcInitialize(&argc,&argv,(char*)0,help);
 38:   PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);
 39:   PetscPrintf(PETSC_COMM_WORLD,"Solve a Dense System of type HEP - dimension %D.\n",n);
 40:   PetscOptionsHasName(PETSC_NULL,"-verbose",&verbose);
 41:   PetscOptionsHasName(PETSC_NULL,"-extrarow",&extrarow);

 43:   /* Create DS object */
 44:   DSCreate(PETSC_COMM_WORLD,&ds);
 45:   DSSetType(ds,DSHEP);
 46:   DSSetFromOptions(ds);
 47:   ld = n+2;  /* test leading dimension larger than n */
 48:   DSAllocate(ds,ld);
 49:   DSSetDimensions(ds,n,PETSC_IGNORE,0,0);
 50:   DSSetExtraRow(ds,extrarow);

 52:   /* Set up viewer */
 53:   PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&viewer);
 54:   PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_INFO_DETAIL);
 55:   DSView(ds,viewer);
 56:   PetscViewerPopFormat(viewer);
 57:   if (verbose) {
 58:     PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_MATLAB);
 59:   }

 61:   /* Fill with a symmetric Toeplitz matrix */
 62:   DSGetArray(ds,DS_MAT_A,&A);
 63:   for (i=0;i<n;i++) A[i+i*ld]=2.0;
 64:   for (j=1;j<3;j++) {
 65:     for (i=0;i<n-j;i++) { A[i+(i+j)*ld]=1.0; A[(i+j)+i*ld]=1.0; }
 66:   }
 67:   if (extrarow) { A[n+(n-2)*ld]=1.0; A[n+(n-1)*ld]=1.0; }
 68:   DSRestoreArray(ds,DS_MAT_A,&A);
 69:   DSSetState(ds,DS_STATE_RAW);
 70:   if (verbose) {
 71:     PetscPrintf(PETSC_COMM_WORLD,"Initial - - - - - - - - -\n");
 72:     DSView(ds,viewer);
 73:   }

 75:   /* Solve */
 76:   PetscMalloc(n*sizeof(PetscScalar),&eig);
 77:   DSSetEigenvalueComparison(ds,SlepcCompareLargestMagnitude,PETSC_NULL);
 78:   DSSolve(ds,eig,PETSC_NULL);
 79:   DSSort(ds,eig,PETSC_NULL,PETSC_NULL,PETSC_NULL,PETSC_NULL);
 80:   if (extrarow) { DSUpdateExtraRow(ds); }
 81:   if (verbose) {
 82:     PetscPrintf(PETSC_COMM_WORLD,"After solve - - - - - - - - -\n");
 83:     DSView(ds,viewer);
 84:   }

 86:   /* Print eigenvalues */
 87:   PetscPrintf(PETSC_COMM_WORLD,"Computed eigenvalues =\n",n);
 88:   for (i=0;i<n;i++) {
 89:     PetscViewerASCIIPrintf(viewer,"  %.5F\n",PetscRealPart(eig[i]));
 90:   }

 92:   PetscFree(eig);
 93:   DSDestroy(&ds);
 94:   SlepcFinalize();
 95:   return 0;
 96: }