LCOV - code coverage report
Current view: top level - svd/tutorials - ex8.c (source / functions) Hit Total Coverage
Test: SLEPc Lines: 38 40 95.0 %
Date: 2024-12-18 00:42:09 Functions: 1 1 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
       3             :    SLEPc - Scalable Library for Eigenvalue Problem Computations
       4             :    Copyright (c) 2002-, Universitat Politecnica de Valencia, Spain
       5             : 
       6             :    This file is part of SLEPc.
       7             :    SLEPc is distributed under a 2-clause BSD license (see LICENSE).
       8             :    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
       9             : */
      10             : 
      11             : static char help[] = "Estimates the 2-norm condition number of a matrix A, that is, the ratio of the largest to the smallest singular values of A. "
      12             :   "The matrix is a Grcar matrix.\n\n"
      13             :   "The command line options are:\n"
      14             :   "  -n <n>, where <n> = matrix dimension.\n\n";
      15             : 
      16             : #include <slepcsvd.h>
      17             : 
      18             : /*
      19             :    This example computes the singular values of an nxn Grcar matrix,
      20             :    which is a nonsymmetric Toeplitz matrix:
      21             : 
      22             :               |  1  1  1  1               |
      23             :               | -1  1  1  1  1            |
      24             :               |    -1  1  1  1  1         |
      25             :               |       .  .  .  .  .       |
      26             :           A = |          .  .  .  .  .    |
      27             :               |            -1  1  1  1  1 |
      28             :               |               -1  1  1  1 |
      29             :               |                  -1  1  1 |
      30             :               |                     -1  1 |
      31             : 
      32             :  */
      33             : 
      34           1 : int main(int argc,char **argv)
      35             : {
      36           1 :   Mat            A;               /* Grcar matrix */
      37           1 :   SVD            svd;             /* singular value solver context */
      38           1 :   PetscInt       N=30,Istart,Iend,i,col[5],nconv1,nconv2;
      39           1 :   PetscScalar    value[] = { -1, 1, 1, 1, 1 };
      40           1 :   PetscReal      sigma_1,sigma_n;
      41             : 
      42           1 :   PetscFunctionBeginUser;
      43           1 :   PetscCall(SlepcInitialize(&argc,&argv,NULL,help));
      44             : 
      45           1 :   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&N,NULL));
      46           1 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\nEstimate the condition number of a Grcar matrix, n=%" PetscInt_FMT "\n\n",N));
      47             : 
      48             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      49             :         Generate the matrix
      50             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      51             : 
      52           1 :   PetscCall(MatCreate(PETSC_COMM_WORLD,&A));
      53           1 :   PetscCall(MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,N,N));
      54           1 :   PetscCall(MatSetFromOptions(A));
      55             : 
      56           1 :   PetscCall(MatGetOwnershipRange(A,&Istart,&Iend));
      57          31 :   for (i=Istart;i<Iend;i++) {
      58          30 :     col[0]=i-1; col[1]=i; col[2]=i+1; col[3]=i+2; col[4]=i+3;
      59          30 :     if (i==0) PetscCall(MatSetValues(A,1,&i,PetscMin(4,N-i),col+1,value+1,INSERT_VALUES));
      60          30 :     else PetscCall(MatSetValues(A,1,&i,PetscMin(5,N-i+1),col,value,INSERT_VALUES));
      61             :   }
      62             : 
      63           1 :   PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY));
      64           1 :   PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY));
      65             : 
      66             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      67             :              Create the singular value solver and set the solution method
      68             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      69             : 
      70             :   /*
      71             :      Create singular value context
      72             :   */
      73           1 :   PetscCall(SVDCreate(PETSC_COMM_WORLD,&svd));
      74             : 
      75             :   /*
      76             :      Set operator
      77             :   */
      78           1 :   PetscCall(SVDSetOperators(svd,A,NULL));
      79             : 
      80             :   /*
      81             :      Set solver parameters at runtime
      82             :   */
      83           1 :   PetscCall(SVDSetFromOptions(svd));
      84           1 :   PetscCall(SVDSetDimensions(svd,1,PETSC_DETERMINE,PETSC_DETERMINE));
      85             : 
      86             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      87             :                       Solve the singular value problem
      88             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
      89             : 
      90             :   /*
      91             :      First request a singular value from one end of the spectrum
      92             :   */
      93           1 :   PetscCall(SVDSetWhichSingularTriplets(svd,SVD_LARGEST));
      94           1 :   PetscCall(SVDSolve(svd));
      95             :   /*
      96             :      Get number of converged singular values
      97             :   */
      98           1 :   PetscCall(SVDGetConverged(svd,&nconv1));
      99             :   /*
     100             :      Get converged singular values: largest singular value is stored in sigma_1.
     101             :      In this example, we are not interested in the singular vectors
     102             :   */
     103           1 :   if (nconv1 > 0) PetscCall(SVDGetSingularTriplet(svd,0,&sigma_1,NULL,NULL));
     104           0 :   else PetscCall(PetscPrintf(PETSC_COMM_WORLD," Unable to compute large singular value!\n\n"));
     105             : 
     106             :   /*
     107             :      Request a singular value from the other end of the spectrum
     108             :   */
     109           1 :   PetscCall(SVDSetWhichSingularTriplets(svd,SVD_SMALLEST));
     110           1 :   PetscCall(SVDSolve(svd));
     111             :   /*
     112             :      Get number of converged singular triplets
     113             :   */
     114           1 :   PetscCall(SVDGetConverged(svd,&nconv2));
     115             :   /*
     116             :      Get converged singular values: smallest singular value is stored in sigma_n.
     117             :      As before, we are not interested in the singular vectors
     118             :   */
     119           1 :   if (nconv2 > 0) PetscCall(SVDGetSingularTriplet(svd,0,&sigma_n,NULL,NULL));
     120           0 :   else PetscCall(PetscPrintf(PETSC_COMM_WORLD," Unable to compute small singular value!\n\n"));
     121             : 
     122             :   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     123             :                     Display solution and clean up
     124             :      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     125           1 :   if (nconv1 > 0 && nconv2 > 0) {
     126           1 :     PetscCall(PetscPrintf(PETSC_COMM_WORLD," Computed singular values: sigma_1=%.4f, sigma_n=%.4f\n",(double)sigma_1,(double)sigma_n));
     127           1 :     PetscCall(PetscPrintf(PETSC_COMM_WORLD," Estimated condition number: sigma_1/sigma_n=%.4f\n\n",(double)(sigma_1/sigma_n)));
     128             :   }
     129             : 
     130             :   /*
     131             :      Free work space
     132             :   */
     133           1 :   PetscCall(SVDDestroy(&svd));
     134           1 :   PetscCall(MatDestroy(&A));
     135           1 :   PetscCall(SlepcFinalize());
     136             :   return 0;
     137             : }
     138             : 
     139             : /*TEST
     140             : 
     141             :    test:
     142             :       suffix: 1
     143             : 
     144             : TEST*/

Generated by: LCOV version 1.14