Actual source code: dsnhep.c

slepc-3.16.0 2021-09-30
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_V:
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);
378:   if (!rank) {
379:     MPI_Pack(ds->mat[DS_MAT_A]+l*ld,ldn,MPIU_SCALAR,ds->work,size,&off,PetscObjectComm((PetscObject)ds));
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));
382:     }
383:     if (eigr) {
384:       MPI_Pack(eigr+l,n,MPIU_SCALAR,ds->work,size,&off,PetscObjectComm((PetscObject)ds));
385:     }
386: #if !defined(PETSC_USE_COMPLEX)
387:     if (eigi) {
388:       MPI_Pack(eigi+l,n,MPIU_SCALAR,ds->work,size,&off,PetscObjectComm((PetscObject)ds));
389:     }
390: #endif
391:   }
392:   MPI_Bcast(ds->work,size,MPI_BYTE,0,PetscObjectComm((PetscObject)ds));
393:   if (rank) {
394:     MPI_Unpack(ds->work,size,&off,ds->mat[DS_MAT_A]+l*ld,ldn,MPIU_SCALAR,PetscObjectComm((PetscObject)ds));
395:     if (ds->state>DS_STATE_RAW) {
396:       MPI_Unpack(ds->work,size,&off,ds->mat[DS_MAT_Q]+l*ld,ldn,MPIU_SCALAR,PetscObjectComm((PetscObject)ds));
397:     }
398:     if (eigr) {
399:       MPI_Unpack(ds->work,size,&off,eigr+l,n,MPIU_SCALAR,PetscObjectComm((PetscObject)ds));
400:     }
401: #if !defined(PETSC_USE_COMPLEX)
402:     if (eigi) {
403:       MPI_Unpack(ds->work,size,&off,eigi+l,n,MPIU_SCALAR,PetscObjectComm((PetscObject)ds));
404:     }
405: #endif
406:   }
407:   return(0);
408: }

410: PetscErrorCode DSTruncate_NHEP(DS ds,PetscInt n,PetscBool trim)
411: {
412:   PetscInt    i,ld=ds->ld,l=ds->l;
413:   PetscScalar *A = ds->mat[DS_MAT_A];

416: #if defined(PETSC_USE_DEBUG)
417:   /* make sure diagonal 2x2 block is not broken */
418:   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");
419: #endif
420:   if (trim) {
421:     if (ds->extrarow) {   /* clean extra row */
422:       for (i=l;i<ds->n;i++) A[ds->n+i*ld] = 0.0;
423:     }
424:     ds->l = 0;
425:     ds->k = 0;
426:     ds->n = n;
427:     ds->t = ds->n;   /* truncated length equal to the new dimension */
428:   } else {
429:     if (ds->extrarow && ds->k==ds->n) {
430:       /* copy entries of extra row to the new position, then clean last row */
431:       for (i=l;i<n;i++) A[n+i*ld] = A[ds->n+i*ld];
432:       for (i=l;i<ds->n;i++) A[ds->n+i*ld] = 0.0;
433:     }
434:     ds->k = (ds->extrarow)? n: 0;
435:     ds->t = ds->n;   /* truncated length equal to previous dimension */
436:     ds->n = n;
437:   }
438:   return(0);
439: }

441: PetscErrorCode DSCond_NHEP(DS ds,PetscReal *cond)
442: {
444:   PetscScalar    *work;
445:   PetscReal      *rwork;
446:   PetscBLASInt   *ipiv;
447:   PetscBLASInt   lwork,info,n,ld;
448:   PetscReal      hn,hin;
449:   PetscScalar    *A;

452:   PetscBLASIntCast(ds->n,&n);
453:   PetscBLASIntCast(ds->ld,&ld);
454:   lwork = 8*ld;
455:   DSAllocateWork_Private(ds,lwork,ld,ld);
456:   work  = ds->work;
457:   rwork = ds->rwork;
458:   ipiv  = ds->iwork;

460:   /* use workspace matrix W to avoid overwriting A */
461:   DSAllocateMat_Private(ds,DS_MAT_W);
462:   A = ds->mat[DS_MAT_W];
463:   PetscArraycpy(A,ds->mat[DS_MAT_A],ds->ld*ds->ld);

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

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

476:   *cond = hn*hin;
477:   return(0);
478: }

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

490:   PetscBLASIntCast(ds->n,&n);
491:   PetscBLASIntCast(ds->ld,&ld);
492:   A  = ds->mat[DS_MAT_A];

494:   if (!recover) {

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

507:     /* Vector g initialy stores b = beta*e_n^T */
508:     PetscArrayzero(g,n);
509:     g[n-1] = beta;

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

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

523:   } else { /* recover */

525:     DSAllocateWork_Private(ds,ld,0,0);
526:     ghat = ds->work;
527:     Q    = ds->mat[DS_MAT_Q];

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

533:     /* A = A + g^*b' */
534:     for (i=0;i<ds->l+ds->k;i++)
535:       for (j=ds->l;j<ds->l+ds->k;j++)
536:         A[i+j*ld] += ghat[i]*Q[n-1+j*ld]*beta;

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

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

551: /*MC
552:    DSNHEP - Dense Non-Hermitian Eigenvalue Problem.

554:    Level: beginner

556:    Notes:
557:    The problem is expressed as A*X = X*Lambda, where A is the input matrix.
558:    Lambda is a diagonal matrix whose diagonal elements are the arguments of
559:    DSSolve(). After solve, A is overwritten with the upper quasi-triangular
560:    matrix T of the (real) Schur form, A*Q = Q*T.

562:    In the intermediate state A is reduced to upper Hessenberg form.

564:    Computation of left eigenvectors is supported, but two-sided Krylov solvers
565:    usually rely on the related DSNHEPTS.

567:    Used DS matrices:
568: +  DS_MAT_A - problem matrix
569: -  DS_MAT_Q - orthogonal/unitary transformation that reduces to Hessenberg form
570:    (intermediate step) or matrix of orthogonal Schur vectors

572:    Implemented methods:
573: .  0 - Implicit QR (_hseqr)

575: .seealso: DSCreate(), DSSetType(), DSType
576: M*/
577: SLEPC_EXTERN PetscErrorCode DSCreate_NHEP(DS ds)
578: {
580:   ds->ops->allocate        = DSAllocate_NHEP;
581:   ds->ops->view            = DSView_NHEP;
582:   ds->ops->vectors         = DSVectors_NHEP;
583:   ds->ops->solve[0]        = DSSolve_NHEP;
584:   ds->ops->sort            = DSSort_NHEP;
585:   ds->ops->sortperm        = DSSortWithPermutation_NHEP;
586:   ds->ops->synchronize     = DSSynchronize_NHEP;
587:   ds->ops->gettruncatesize = DSGetTruncateSize_Default;
588:   ds->ops->truncate        = DSTruncate_NHEP;
589:   ds->ops->update          = DSUpdateExtraRow_NHEP;
590:   ds->ops->cond            = DSCond_NHEP;
591:   ds->ops->transharm       = DSTranslateHarmonic_NHEP;
592:   return(0);
593: }