Date: 2021-09-20T09:43:40+00:00

Actual source code: dsnhep.c
slepc-3.15.2 2021-09-20
Report Typos and Errors

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

      6:    This file is part of SLEPc.
      7:    SLEPc is distributed under a 2-clause BSD license (see LICENSE).
      8:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      9: */

     11: #include <slepc/private/dsimpl.h>
     12: #include <slepcblaslapack.h>

     14: PetscErrorCode DSAllocate_NHEP(DS ds,PetscInt ld)
     15: {

     19:   DSAllocateMat_Private(ds,DS_MAT_A);
     20:   DSAllocateMat_Private(ds,DS_MAT_Q);
     21:   PetscFree(ds->perm);
     22:   PetscMalloc1(ld,&ds->perm);
     23:   PetscLogObjectMemory((PetscObject)ds,ld*sizeof(PetscInt));
     24:   return(0);
     25: }

     27: PetscErrorCode DSView_NHEP(DS ds,PetscViewer viewer)
     28: {
     29:   PetscErrorCode    ierr;
     30:   PetscViewerFormat format;

     33:   PetscViewerGetFormat(viewer,&format);
     34:   if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) return(0);
     35:   DSViewMat(ds,viewer,DS_MAT_A);
     36:   if (ds->state>DS_STATE_INTERMEDIATE) { DSViewMat(ds,viewer,DS_MAT_Q); }
     37:   if (ds->mat[DS_MAT_X]) { DSViewMat(ds,viewer,DS_MAT_X); }
     38:   if (ds->mat[DS_MAT_Y]) { DSViewMat(ds,viewer,DS_MAT_Y); }
     39:   return(0);
     40: }

     42: static PetscErrorCode DSVectors_NHEP_Refined_Some(DS ds,PetscInt *k,PetscReal *rnorm,PetscBool left)
     43: {
     45:   PetscInt       i,j;
     46:   PetscBLASInt   info,ld,n,n1,lwork,inc=1;
     47:   PetscScalar    sdummy,done=1.0,zero=0.0;
     48:   PetscReal      *sigma;
     49:   PetscBool      iscomplex = PETSC_FALSE;
     50:   PetscScalar    *A = ds->mat[DS_MAT_A];
     51:   PetscScalar    *Q = ds->mat[DS_MAT_Q];
     52:   PetscScalar    *X = ds->mat[left?DS_MAT_Y:DS_MAT_X];
     53:   PetscScalar    *W;

     56:   if (left) SETERRQ(PetscObjectComm((PetscObject)ds),PETSC_ERR_SUP,"Not implemented for left vectors");
     57:   PetscBLASIntCast(ds->n,&n);
     58:   PetscBLASIntCast(ds->ld,&ld);
     59:   n1 = n+1;
     60:   if ((*k)<n-1 && A[(*k)+1+(*k)*ld]!=0.0) iscomplex = PETSC_TRUE;
     61:   if (iscomplex) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not implemented for complex eigenvalues yet");
     62:   DSAllocateWork_Private(ds,5*ld,6*ld,0);
     63:   DSAllocateMat_Private(ds,DS_MAT_W);
     64:   W = ds->mat[DS_MAT_W];
     65:   lwork = 5*ld;
     66:   sigma = ds->rwork+5*ld;

     68:   /* build A-w*I in W */
     69:   for (j=0;j<n;j++)
     70:     for (i=0;i<=n;i++)
     71:       W[i+j*ld] = A[i+j*ld];
     72:   for (i=0;i<n;i++)
     73:     W[i+i*ld] -= A[(*k)+(*k)*ld];

     75:   /* compute SVD of W */
     76: #if !defined(PETSC_USE_COMPLEX)
     77:   PetscStackCallBLAS("LAPACKgesvd",LAPACKgesvd_("N","O",&n1,&n,W,&ld,sigma,&sdummy,&ld,&sdummy,&ld,ds->work,&lwork,&info));
     78: #else
     79:   PetscStackCallBLAS("LAPACKgesvd",LAPACKgesvd_("N","O",&n1,&n,W,&ld,sigma,&sdummy,&ld,&sdummy,&ld,ds->work,&lwork,ds->rwork,&info));
     80: #endif
     81:   SlepcCheckLapackInfo("gesvd",info);

     83:   /* the smallest singular value is the new error estimate */
     84:   if (rnorm) *rnorm = sigma[n-1];

     86:   /* update vector with right singular vector associated to smallest singular value,
     87:      accumulating the transformation matrix Q */
     88:   PetscStackCallBLAS("BLASgemv",BLASgemv_("N",&n,&n,&done,Q,&ld,W+n-1,&ld,&zero,X+(*k)*ld,&inc));
     89:   return(0);
     90: }

     92: static PetscErrorCode DSVectors_NHEP_Refined_All(DS ds,PetscBool left)
     93: {
     95:   PetscInt       i;

     98:   for (i=0;i<ds->n;i++) {
     99:     DSVectors_NHEP_Refined_Some(ds,&i,NULL,left);
    100:   }
    101:   return(0);
    102: }

    104: static PetscErrorCode DSVectors_NHEP_Eigen_Some(DS ds,PetscInt *k,PetscReal *rnorm,PetscBool left)
    105: {
    107:   PetscInt       i;
    108:   PetscBLASInt   mm=1,mout,info,ld,n,*select,inc=1,cols=1,zero=0;
    109:   PetscScalar    sone=1.0,szero=0.0;
    110:   PetscReal      norm,done=1.0;
    111:   PetscBool      iscomplex = PETSC_FALSE;
    112:   PetscScalar    *A = ds->mat[DS_MAT_A];
    113:   PetscScalar    *Q = ds->mat[DS_MAT_Q];
    114:   PetscScalar    *X = ds->mat[left?DS_MAT_Y:DS_MAT_X];
    115:   PetscScalar    *Y;

    118:   PetscBLASIntCast(ds->n,&n);
    119:   PetscBLASIntCast(ds->ld,&ld);
    120:   DSAllocateWork_Private(ds,0,0,ld);
    121:   select = ds->iwork;
    122:   for (i=0;i<n;i++) select[i] = (PetscBLASInt)PETSC_FALSE;

    124:   /* compute k-th eigenvector Y of A */
    125:   Y = X+(*k)*ld;
    126:   select[*k] = (PetscBLASInt)PETSC_TRUE;
    127: #if !defined(PETSC_USE_COMPLEX)
    128:   if ((*k)<n-1 && A[(*k)+1+(*k)*ld]!=0.0) iscomplex = PETSC_TRUE;
    129:   mm = iscomplex? 2: 1;
    130:   if (iscomplex) select[(*k)+1] = (PetscBLASInt)PETSC_TRUE;
    131:   DSAllocateWork_Private(ds,3*ld,0,0);
    132:   PetscStackCallBLAS("LAPACKtrevc",LAPACKtrevc_(left?"L":"R","S",select,&n,A,&ld,Y,&ld,Y,&ld,&mm,&mout,ds->work,&info));
    133: #else
    134:   DSAllocateWork_Private(ds,2*ld,ld,0);
    135:   PetscStackCallBLAS("LAPACKtrevc",LAPACKtrevc_(left?"L":"R","S",select,&n,A,&ld,Y,&ld,Y,&ld,&mm,&mout,ds->work,ds->rwork,&info));
    136: #endif
    137:   SlepcCheckLapackInfo("trevc",info);
    138:   if (mout != mm) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Inconsistent arguments");

    140:   /* accumulate and normalize eigenvectors */
    141:   if (ds->state>=DS_STATE_CONDENSED) {
    142:     PetscArraycpy(ds->work,Y,mout*ld);
    143:     PetscStackCallBLAS("BLASgemv",BLASgemv_("N",&n,&n,&sone,Q,&ld,ds->work,&inc,&szero,Y,&inc));
    144: #if !defined(PETSC_USE_COMPLEX)
    145:     if (iscomplex) PetscStackCallBLAS("BLASgemv",BLASgemv_("N",&n,&n,&sone,Q,&ld,ds->work+ld,&inc,&szero,Y+ld,&inc));
    146: #endif
    147:     cols = 1;
    148:     norm = BLASnrm2_(&n,Y,&inc);
    149: #if !defined(PETSC_USE_COMPLEX)
    150:     if (iscomplex) {
    151:       norm = SlepcAbsEigenvalue(norm,BLASnrm2_(&n,Y+ld,&inc));
    152:       cols = 2;
    153:     }
    154: #endif
    155:     PetscStackCallBLAS("LAPACKlascl",LAPACKlascl_("G",&zero,&zero,&norm,&done,&n,&cols,Y,&ld,&info));
    156:     SlepcCheckLapackInfo("lascl",info);
    157:   }

    159:   /* set output arguments */
    160:   if (iscomplex) (*k)++;
    161:   if (rnorm) {
    162:     if (iscomplex) *rnorm = SlepcAbsEigenvalue(Y[n-1],Y[n-1+ld]);
    163:     else *rnorm = PetscAbsScalar(Y[n-1]);
    164:   }
    165:   return(0);
    166: }

    168: static PetscErrorCode DSVectors_NHEP_Eigen_All(DS ds,PetscBool left)
    169: {
    171:   PetscInt       i;
    172:   PetscBLASInt   n,ld,mout,info,inc=1,cols,zero=0;
    173:   PetscBool      iscomplex;
    174:   PetscScalar    *X,*Y,*Z,*A = ds->mat[DS_MAT_A];
    175:   PetscReal      norm,done=1.0;
    176:   const char     *side,*back;

    179:   PetscBLASIntCast(ds->n,&n);
    180:   PetscBLASIntCast(ds->ld,&ld);
    181:   if (left) {
    182:     X = NULL;
    183:     Y = ds->mat[DS_MAT_Y];
    184:     side = "L";
    185:   } else {
    186:     X = ds->mat[DS_MAT_X];
    187:     Y = NULL;
    188:     side = "R";
    189:   }
    190:   Z = left? Y: X;
    191:   if (ds->state>=DS_STATE_CONDENSED) {
    192:     /* DSSolve() has been called, backtransform with matrix Q */
    193:     back = "B";
    194:     PetscArraycpy(Z,ds->mat[DS_MAT_Q],ld*ld);
    195:   } else back = "A";
    196: #if !defined(PETSC_USE_COMPLEX)
    197:   DSAllocateWork_Private(ds,3*ld,0,0);
    198:   PetscStackCallBLAS("LAPACKtrevc",LAPACKtrevc_(side,back,NULL,&n,A,&ld,Y,&ld,X,&ld,&n,&mout,ds->work,&info));
    199: #else
    200:   DSAllocateWork_Private(ds,2*ld,ld,0);
    201:   PetscStackCallBLAS("LAPACKtrevc",LAPACKtrevc_(side,back,NULL,&n,A,&ld,Y,&ld,X,&ld,&n,&mout,ds->work,ds->rwork,&info));
    202: #endif
    203:   SlepcCheckLapackInfo("trevc",info);

    205:   /* normalize eigenvectors */
    206:   for (i=0;i<n;i++) {
    207:     iscomplex = (i<n-1 && A[i+1+i*ld]!=0.0)? PETSC_TRUE: PETSC_FALSE;
    208:     cols = 1;
    209:     norm = BLASnrm2_(&n,Z+i*ld,&inc);
    210: #if !defined(PETSC_USE_COMPLEX)
    211:     if (iscomplex) {
    212:       norm = SlepcAbsEigenvalue(norm,BLASnrm2_(&n,Z+(i+1)*ld,&inc));
    213:       cols = 2;
    214:     }
    215: #endif
    216:     PetscStackCallBLAS("LAPACKlascl",LAPACKlascl_("G",&zero,&zero,&norm,&done,&n,&cols,Z+i*ld,&ld,&info));
    217:     SlepcCheckLapackInfo("lascl",info);
    218:     if (iscomplex) i++;
    219:   }
    220:   return(0);
    221: }

    223: PetscErrorCode DSVectors_NHEP(DS ds,DSMatType mat,PetscInt *j,PetscReal *rnorm)
    224: {

    228:   switch (mat) {
    229:     case DS_MAT_X:
    230:       if (ds->refined) {
    231:         if (!ds->extrarow) SETERRQ(PetscObjectComm((PetscObject)ds),PETSC_ERR_SUP,"Refined vectors require activating the extra row");
    232:         if (j) {
    233:           DSVectors_NHEP_Refined_Some(ds,j,rnorm,PETSC_FALSE);
    234:         } else {
    235:           DSVectors_NHEP_Refined_All(ds,PETSC_FALSE);
    236:         }
    237:       } else {
    238:         if (j) {
    239:           DSVectors_NHEP_Eigen_Some(ds,j,rnorm,PETSC_FALSE);
    240:         } else {
    241:           DSVectors_NHEP_Eigen_All(ds,PETSC_FALSE);
    242:         }
    243:       }
    244:       break;
    245:     case DS_MAT_Y:
    246:       if (ds->refined) SETERRQ(PetscObjectComm((PetscObject)ds),PETSC_ERR_SUP,"Not implemented yet");
    247:       if (j) {
    248:         DSVectors_NHEP_Eigen_Some(ds,j,rnorm,PETSC_TRUE);
    249:       } else {
    250:         DSVectors_NHEP_Eigen_All(ds,PETSC_TRUE);
    251:       }
    252:       break;
    253:     case DS_MAT_U:
    254:     case DS_MAT_VT:
    255:       SETERRQ(PetscObjectComm((PetscObject)ds),PETSC_ERR_SUP,"Not implemented yet");
    256:     default:
    257:       SETERRQ(PetscObjectComm((PetscObject)ds),PETSC_ERR_ARG_OUTOFRANGE,"Invalid mat parameter");
    258:   }
    259:   return(0);
    260: }

    262: static PetscErrorCode DSSort_NHEP_Arbitrary(DS ds,PetscScalar *wr,PetscScalar *wi,PetscScalar *rr,PetscScalar *ri,PetscInt *k)
    263: {
    265:   PetscInt       i;
    266:   PetscBLASInt   info,n,ld,mout,lwork,*selection;
    267:   PetscScalar    *T = ds->mat[DS_MAT_A],*Q = ds->mat[DS_MAT_Q],*work;
    268:   PetscReal      dummy;
    269: #if !defined(PETSC_USE_COMPLEX)
    270:   PetscBLASInt   *iwork,liwork;
    271: #endif

    274:   if (!k) SETERRQ(PetscObjectComm((PetscObject)ds),PETSC_ERR_ARG_WRONG,"Must supply argument k");
    275:   PetscBLASIntCast(ds->n,&n);
    276:   PetscBLASIntCast(ds->ld,&ld);
    277: #if !defined(PETSC_USE_COMPLEX)
    278:   lwork = n;
    279:   liwork = 1;
    280:   DSAllocateWork_Private(ds,lwork,0,liwork+n);
    281:   work = ds->work;
    282:   lwork = ds->lwork;
    283:   selection = ds->iwork;
    284:   iwork = ds->iwork + n;
    285:   liwork = ds->liwork - n;
    286: #else
    287:   lwork = 1;
    288:   DSAllocateWork_Private(ds,lwork,0,n);
    289:   work = ds->work;
    290:   selection = ds->iwork;
    291: #endif
    292:   /* Compute the selected eigenvalue to be in the leading position */
    293:   DSSortEigenvalues_Private(ds,rr,ri,ds->perm,PETSC_FALSE);
    294:   PetscArrayzero(selection,n);
    295:   for (i=0;i<*k;i++) selection[ds->perm[i]] = 1;
    296: #if !defined(PETSC_USE_COMPLEX)
    297:   PetscStackCallBLAS("LAPACKtrsen",LAPACKtrsen_("N","V",selection,&n,T,&ld,Q,&ld,wr,wi,&mout,&dummy,&dummy,work,&lwork,iwork,&liwork,&info));
    298: #else
    299:   PetscStackCallBLAS("LAPACKtrsen",LAPACKtrsen_("N","V",selection,&n,T,&ld,Q,&ld,wr,&mout,&dummy,&dummy,work,&lwork,&info));
    300: #endif
    301:   SlepcCheckLapackInfo("trsen",info);
    302:   *k = mout;
    303:   return(0);
    304: }

    306: PetscErrorCode DSSort_NHEP(DS ds,PetscScalar *wr,PetscScalar *wi,PetscScalar *rr,PetscScalar *ri,PetscInt *k)
    307: {

    311:   if (!rr || wr == rr) {
    312:     DSSort_NHEP_Total(ds,ds->mat[DS_MAT_A],ds->mat[DS_MAT_Q],wr,wi);
    313:   } else {
    314:     DSSort_NHEP_Arbitrary(ds,wr,wi,rr,ri,k);
    315:   }
    316:   return(0);
    317: }

    319: static PetscErrorCode DSSortWithPermutation_NHEP(DS ds,PetscInt *perm,PetscScalar *wr,PetscScalar *wi)
    320: {

    324:   DSSortWithPermutation_NHEP_Private(ds,perm,ds->mat[DS_MAT_A],ds->mat[DS_MAT_Q],wr,wi);
    325:   return(0);
    326: }

    328: PetscErrorCode DSUpdateExtraRow_NHEP(DS ds)
    329: {
    331:   PetscInt       i;
    332:   PetscBLASInt   n,ld,incx=1;
    333:   PetscScalar    *A,*Q,*x,*y,one=1.0,zero=0.0;

    336:   PetscBLASIntCast(ds->n,&n);
    337:   PetscBLASIntCast(ds->ld,&ld);
    338:   A  = ds->mat[DS_MAT_A];
    339:   Q  = ds->mat[DS_MAT_Q];
    340:   DSAllocateWork_Private(ds,2*ld,0,0);
    341:   x = ds->work;
    342:   y = ds->work+ld;
    343:   for (i=0;i<n;i++) x[i] = PetscConj(A[n+i*ld]);
    344:   PetscStackCallBLAS("BLASgemv",BLASgemv_("C",&n,&n,&one,Q,&ld,x,&incx,&zero,y,&incx));
    345:   for (i=0;i<n;i++) A[n+i*ld] = PetscConj(y[i]);
    346:   ds->k = n;
    347:   return(0);
    348: }

    350: PetscErrorCode DSSolve_NHEP(DS ds,PetscScalar *wr,PetscScalar *wi)
    351: {

    355: #if !defined(PETSC_USE_COMPLEX)
    357: #endif
    358:   DSSolve_NHEP_Private(ds,ds->mat[DS_MAT_A],ds->mat[DS_MAT_Q],wr,wi);
    359:   return(0);
    360: }

    362: PetscErrorCode DSSynchronize_NHEP(DS ds,PetscScalar eigr[],PetscScalar eigi[])
    363: {
    365:   PetscInt       ld=ds->ld,l=ds->l,k;
    366:   PetscMPIInt    n,rank,off=0,size,ldn;

    369:   k = (ds->n-l)*ld;
    370:   if (ds->state>DS_STATE_RAW) k += (ds->n-l)*ld;
    371:   if (eigr) k += ds->n-l;
    372:   if (eigi) k += ds->n-l;
    373:   DSAllocateWork_Private(ds,k,0,0);
    374:   PetscMPIIntCast(k*sizeof(PetscScalar),&size);
    375:   PetscMPIIntCast(ds->n-l,&n);
    376:   PetscMPIIntCast(ld*(ds->n-l),&ldn);
    377:   MPI_Comm_rank(PetscObjectComm((PetscObject)ds),&rank);CHKERRMPI(ierr);
    378:   if (!rank) {
    379:     MPI_Pack(ds->mat[DS_MAT_A]+l*ld,ldn,MPIU_SCALAR,ds->work,size,&off,PetscObjectComm((PetscObject)ds));CHKERRMPI(ierr);
    380:     if (ds->state>DS_STATE_RAW) {
    381:       MPI_Pack(ds->mat[DS_MAT_Q]+l*ld,ldn,MPIU_SCALAR,ds->work,size,&off,PetscObjectComm((PetscObject)ds));CHKERRMPI(ierr);
    382:     }
    383:     if (eigr) {
    384:       MPI_Pack(eigr+l,n,MPIU_SCALAR,ds->work,size,&off,PetscObjectComm((PetscObject)ds));CHKERRMPI(ierr);
    385:     }
    386:     if (eigi) {
    387:       MPI_Pack(eigi+l,n,MPIU_SCALAR,ds->work,size,&off,PetscObjectComm((PetscObject)ds));CHKERRMPI(ierr);
    388:     }
    389:   }
    390:   MPI_Bcast(ds->work,size,MPI_BYTE,0,PetscObjectComm((PetscObject)ds));CHKERRMPI(ierr);
    391:   if (rank) {
    392:     MPI_Unpack(ds->work,size,&off,ds->mat[DS_MAT_A]+l*ld,ldn,MPIU_SCALAR,PetscObjectComm((PetscObject)ds));CHKERRMPI(ierr);
    393:     if (ds->state>DS_STATE_RAW) {
    394:       MPI_Unpack(ds->work,size,&off,ds->mat[DS_MAT_Q]+l*ld,ldn,MPIU_SCALAR,PetscObjectComm((PetscObject)ds));CHKERRMPI(ierr);
    395:     }
    396:     if (eigr) {
    397:       MPI_Unpack(ds->work,size,&off,eigr+l,n,MPIU_SCALAR,PetscObjectComm((PetscObject)ds));CHKERRMPI(ierr);
    398:     }
    399:     if (eigi) {
    400:       MPI_Unpack(ds->work,size,&off,eigi+l,n,MPIU_SCALAR,PetscObjectComm((PetscObject)ds));CHKERRMPI(ierr);
    401:     }
    402:   }
    403:   return(0);
    404: }

    406: PetscErrorCode DSTruncate_NHEP(DS ds,PetscInt n,PetscBool trim)
    407: {
    408:   PetscInt    i,ld=ds->ld,l=ds->l;
    409:   PetscScalar *A = ds->mat[DS_MAT_A];

    412: #if defined(PETSC_USE_DEBUG)
    413:   /* make sure diagonal 2x2 block is not broken */
    414:   if (ds->state>=DS_STATE_CONDENSED && n>0 && n<ds->n && A[n+(n-1)*ld]!=0.0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"The given size would break a 2x2 block, call DSGetTruncateSize() first");
    415: #endif
    416:   if (trim) {
    417:     if (ds->extrarow) {   /* clean extra row */
    418:       for (i=l;i<ds->n;i++) A[ds->n+i*ld] = 0.0;
    419:     }
    420:     ds->l = 0;
    421:     ds->k = 0;
    422:     ds->n = n;
    423:     ds->t = ds->n;   /* truncated length equal to the new dimension */
    424:   } else {
    425:     if (ds->extrarow && ds->k==ds->n) {
    426:       /* copy entries of extra row to the new position, then clean last row */
    427:       for (i=l;i<n;i++) A[n+i*ld] = A[ds->n+i*ld];
    428:       for (i=l;i<ds->n;i++) A[ds->n+i*ld] = 0.0;
    429:     }
    430:     ds->k = (ds->extrarow)? n: 0;
    431:     ds->t = ds->n;   /* truncated length equal to previous dimension */
    432:     ds->n = n;
    433:   }
    434:   return(0);
    435: }

    437: PetscErrorCode DSCond_NHEP(DS ds,PetscReal *cond)
    438: {
    440:   PetscScalar    *work;
    441:   PetscReal      *rwork;
    442:   PetscBLASInt   *ipiv;
    443:   PetscBLASInt   lwork,info,n,ld;
    444:   PetscReal      hn,hin;
    445:   PetscScalar    *A;

    448:   PetscBLASIntCast(ds->n,&n);
    449:   PetscBLASIntCast(ds->ld,&ld);
    450:   lwork = 8*ld;
    451:   DSAllocateWork_Private(ds,lwork,ld,ld);
    452:   work  = ds->work;
    453:   rwork = ds->rwork;
    454:   ipiv  = ds->iwork;

    456:   /* use workspace matrix W to avoid overwriting A */
    457:   DSAllocateMat_Private(ds,DS_MAT_W);
    458:   A = ds->mat[DS_MAT_W];
    459:   PetscArraycpy(A,ds->mat[DS_MAT_A],ds->ld*ds->ld);

    461:   /* norm of A */
    462:   if (ds->state<DS_STATE_INTERMEDIATE) hn = LAPACKlange_("I",&n,&n,A,&ld,rwork);
    463:   else hn = LAPACKlanhs_("I",&n,A,&ld,rwork);

    465:   /* norm of inv(A) */
    466:   PetscStackCallBLAS("LAPACKgetrf",LAPACKgetrf_(&n,&n,A,&ld,ipiv,&info));
    467:   SlepcCheckLapackInfo("getrf",info);
    468:   PetscStackCallBLAS("LAPACKgetri",LAPACKgetri_(&n,A,&ld,ipiv,work,&lwork,&info));
    469:   SlepcCheckLapackInfo("getri",info);
    470:   hin = LAPACKlange_("I",&n,&n,A,&ld,rwork);

    472:   *cond = hn*hin;
    473:   return(0);
    474: }

    476: PetscErrorCode DSTranslateHarmonic_NHEP(DS ds,PetscScalar tau,PetscReal beta,PetscBool recover,PetscScalar *gin,PetscReal *gammaout)
    477: {
    479:   PetscInt       i,j;
    480:   PetscBLASInt   *ipiv,info,n,ld,one=1,ncol;
    481:   PetscScalar    *A,*B,*Q,*g=gin,*ghat;
    482:   PetscScalar    done=1.0,dmone=-1.0,dzero=0.0;
    483:   PetscReal      gamma=1.0;

    486:   PetscBLASIntCast(ds->n,&n);
    487:   PetscBLASIntCast(ds->ld,&ld);
    488:   A  = ds->mat[DS_MAT_A];

    490:   if (!recover) {

    492:     DSAllocateWork_Private(ds,0,0,ld);
    493:     ipiv = ds->iwork;
    494:     if (!g) {
    495:       DSAllocateWork_Private(ds,ld,0,0);
    496:       g = ds->work;
    497:     }
    498:     /* use workspace matrix W to factor A-tau*eye(n) */
    499:     DSAllocateMat_Private(ds,DS_MAT_W);
    500:     B = ds->mat[DS_MAT_W];
    501:     PetscArraycpy(B,A,ld*ld);

    503:     /* Vector g initialy stores b = beta*e_n^T */
    504:     PetscArrayzero(g,n);
    505:     g[n-1] = beta;

    507:     /* g = (A-tau*eye(n))'\b */
    508:     for (i=0;i<n;i++) B[i+i*ld] -= tau;
    509:     PetscStackCallBLAS("LAPACKgetrf",LAPACKgetrf_(&n,&n,B,&ld,ipiv,&info));
    510:     SlepcCheckLapackInfo("getrf",info);
    511:     PetscLogFlops(2.0*n*n*n/3.0);
    512:     PetscStackCallBLAS("LAPACKgetrs",LAPACKgetrs_("C",&n,&one,B,&ld,ipiv,g,&ld,&info));
    513:     SlepcCheckLapackInfo("getrs",info);
    514:     PetscLogFlops(2.0*n*n-n);

    516:     /* A = A + g*b' */
    517:     for (i=0;i<n;i++) A[i+(n-1)*ld] += g[i]*beta;

    519:   } else { /* recover */

    521:     DSAllocateWork_Private(ds,ld,0,0);
    522:     ghat = ds->work;
    523:     Q    = ds->mat[DS_MAT_Q];

    525:     /* g^ = -Q(:,idx)'*g */
    526:     PetscBLASIntCast(ds->l+ds->k,&ncol);
    527:     PetscStackCallBLAS("BLASgemv",BLASgemv_("C",&n,&ncol,&dmone,Q,&ld,g,&one,&dzero,ghat,&one));

    529:     /* A = A + g^*b' */
    530:     for (i=0;i<ds->l+ds->k;i++)
    531:       for (j=ds->l;j<ds->l+ds->k;j++)
    532:         A[i+j*ld] += ghat[i]*Q[n-1+j*ld]*beta;

    534:     /* g~ = (I-Q(:,idx)*Q(:,idx)')*g = g+Q(:,idx)*g^ */
    535:     PetscStackCallBLAS("BLASgemv",BLASgemv_("N",&n,&ncol,&done,Q,&ld,ghat,&one,&done,g,&one));
    536:   }

    538:   /* Compute gamma factor */
    539:   if (gammaout || (recover && ds->extrarow)) gamma = SlepcAbs(1.0,BLASnrm2_(&n,g,&one));
    540:   if (gammaout) *gammaout = gamma;
    541:   if (recover && ds->extrarow) {
    542:     for (j=ds->l;j<ds->l+ds->k;j++) A[ds->n+j*ld] *= gamma;
    543:   }
    544:   return(0);
    545: }

    547: SLEPC_EXTERN PetscErrorCode DSCreate_NHEP(DS ds)
    548: {
    550:   ds->ops->allocate        = DSAllocate_NHEP;
    551:   ds->ops->view            = DSView_NHEP;
    552:   ds->ops->vectors         = DSVectors_NHEP;
    553:   ds->ops->solve[0]        = DSSolve_NHEP;
    554:   ds->ops->sort            = DSSort_NHEP;
    555:   ds->ops->sortperm        = DSSortWithPermutation_NHEP;
    556:   ds->ops->synchronize     = DSSynchronize_NHEP;
    557:   ds->ops->gettruncatesize = DSGetTruncateSize_Default;
    558:   ds->ops->truncate        = DSTruncate_NHEP;
    559:   ds->ops->update          = DSUpdateExtraRow_NHEP;
    560:   ds->ops->cond            = DSCond_NHEP;
    561:   ds->ops->transharm       = DSTranslateHarmonic_NHEP;
    562:   return(0);
    563: }