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

Actual source code: stsolve.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: */
     10: /*
     11:    ST interface routines, callable by users
     12: */

     14: #include <slepc/private/stimpl.h>

     16: PetscErrorCode STApply_Generic(ST st,Vec x,Vec y)
     17: {

     21:   if (st->M && st->P) {
     22:     MatMult(st->M,x,st->work[0]);
     23:     STMatSolve(st,st->work[0],y);
     24:   } else if (st->M) {
     25:     MatMult(st->M,x,y);
     26:   } else {
     27:     STMatSolve(st,x,y);
     28:   }
     29:   return(0);
     30: }

     32: /*@
     33:    STApply - Applies the spectral transformation operator to a vector, for
     34:    instance (A - sB)^-1 B in the case of the shift-and-invert transformation
     35:    and generalized eigenproblem.

     37:    Collective on st

     39:    Input Parameters:
     40: +  st - the spectral transformation context
     41: -  x  - input vector

     43:    Output Parameter:
     44: .  y - output vector

     46:    Level: developer

     48: .seealso: STApplyTranspose(), STApplyHermitianTranspose()
     49: @*/
     50: PetscErrorCode STApply(ST st,Vec x,Vec y)
     51: {
     53:   Mat            Op;

     60:   STCheckMatrices(st,1);
     61:   if (x == y) SETERRQ(PetscObjectComm((PetscObject)st),PETSC_ERR_ARG_IDN,"x and y must be different vectors");
     62:   VecSetErrorIfLocked(y,3);
     63:   if (!st->ops->apply) SETERRQ(PetscObjectComm((PetscObject)st),PETSC_ERR_SUP,"ST does not have apply");
     64:   STGetOperator_Private(st,&Op);
     65:   MatMult(Op,x,y);
     66:   return(0);
     67: }

     69: PetscErrorCode STApplyMat_Generic(ST st,Mat B,Mat C)
     70: {
     72:   Mat            work;

     75:   if (st->M && st->P) {
     76:     MatMatMult(st->M,B,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&work);
     77:     STMatMatSolve(st,work,C);
     78:     MatDestroy(&work);
     79:   } else if (st->M) {
     80:     MatMatMult(st->M,B,MAT_REUSE_MATRIX,PETSC_DEFAULT,&C);
     81:   } else {
     82:     STMatMatSolve(st,B,C);
     83:   }
     84:   return(0);
     85: }

     87: /*@
     88:    STApplyMat - Applies the spectral transformation operator to a matrix, for
     89:    instance (A - sB)^-1 B in the case of the shift-and-invert transformation
     90:    and generalized eigenproblem.

     92:    Collective on st

     94:    Input Parameters:
     95: +  st - the spectral transformation context
     96: -  X  - input matrix

     98:    Output Parameter:
     99: .  y - output matrix

    101:    Level: developer

    103: .seealso: STApply()
    104: @*/
    105: PetscErrorCode STApplyMat(ST st,Mat X,Mat Y)
    106: {

    114:   STCheckMatrices(st,1);
    115:   if (X == Y) SETERRQ(PetscObjectComm((PetscObject)st),PETSC_ERR_ARG_IDN,"X and Y must be different matrices");
    116:   if (!st->ops->applymat) SETERRQ(PetscObjectComm((PetscObject)st),PETSC_ERR_SUP,"ST does not have applymat");
    117:   (*st->ops->applymat)(st,X,Y);
    118:   return(0);
    119: }

    121: PetscErrorCode STApplyTranspose_Generic(ST st,Vec x,Vec y)
    122: {

    126:   if (st->M && st->P) {
    127:     STMatSolveTranspose(st,x,st->work[0]);
    128:     MatMultTranspose(st->M,st->work[0],y);
    129:   } else if (st->M) {
    130:     MatMultTranspose(st->M,x,y);
    131:   } else {
    132:     STMatSolveTranspose(st,x,y);
    133:   }
    134:   return(0);
    135: }

    137: /*@
    138:    STApplyTranspose - Applies the transpose of the operator to a vector, for
    139:    instance B^T(A - sB)^-T in the case of the shift-and-invert transformation
    140:    and generalized eigenproblem.

    142:    Collective on st

    144:    Input Parameters:
    145: +  st - the spectral transformation context
    146: -  x  - input vector

    148:    Output Parameter:
    149: .  y - output vector

    151:    Level: developer

    153: .seealso: STApply(), STApplyHermitianTranspose()
    154: @*/
    155: PetscErrorCode STApplyTranspose(ST st,Vec x,Vec y)
    156: {
    158:   Mat            Op;

    165:   STCheckMatrices(st,1);
    166:   if (x == y) SETERRQ(PetscObjectComm((PetscObject)st),PETSC_ERR_ARG_IDN,"x and y must be different vectors");
    167:   VecSetErrorIfLocked(y,3);
    168:   if (!st->ops->applytrans) SETERRQ(PetscObjectComm((PetscObject)st),PETSC_ERR_SUP,"ST does not have applytrans");
    169:   STGetOperator_Private(st,&Op);
    170:   MatMultTranspose(Op,x,y);
    171:   return(0);
    172: }

    174: /*@
    175:    STApplyHermitianTranspose - Applies the hermitian-transpose of the operator
    176:    to a vector, for instance B^H(A - sB)^-H in the case of the shift-and-invert
    177:    transformation and generalized eigenproblem.

    179:    Collective on st

    181:    Input Parameters:
    182: +  st - the spectral transformation context
    183: -  x  - input vector

    185:    Output Parameter:
    186: .  y - output vector

    188:    Note:
    189:    Currently implemented via STApplyTranspose() with appropriate conjugation.

    191:    Level: developer

    193: .seealso: STApply(), STApplyTranspose()
    194: @*/
    195: PetscErrorCode STApplyHermitianTranspose(ST st,Vec x,Vec y)
    196: {
    198:   Mat            Op;

    205:   STCheckMatrices(st,1);
    206:   if (x == y) SETERRQ(PetscObjectComm((PetscObject)st),PETSC_ERR_ARG_IDN,"x and y must be different vectors");
    207:   VecSetErrorIfLocked(y,3);
    208:   if (!st->ops->applytrans) SETERRQ(PetscObjectComm((PetscObject)st),PETSC_ERR_SUP,"ST does not have applytrans");
    209:   STGetOperator_Private(st,&Op);
    210:   MatMultHermitianTranspose(Op,x,y);
    211:   return(0);
    212: }

    214: /*@
    215:    STGetBilinearForm - Returns the matrix used in the bilinear form with a
    216:    generalized problem with semi-definite B.

    218:    Not collective, though a parallel Mat may be returned

    220:    Input Parameters:
    221: .  st - the spectral transformation context

    223:    Output Parameter:
    224: .  B - output matrix

    226:    Notes:
    227:    The output matrix B must be destroyed after use. It will be NULL in
    228:    case of standard eigenproblems.

    230:    Level: developer
    231: @*/
    232: PetscErrorCode STGetBilinearForm(ST st,Mat *B)
    233: {

    240:   STCheckMatrices(st,1);
    241:   (*st->ops->getbilinearform)(st,B);
    242:   return(0);
    243: }

    245: PetscErrorCode STGetBilinearForm_Default(ST st,Mat *B)
    246: {

    250:   if (st->nmat==1) *B = NULL;
    251:   else {
    252:     *B = st->A[1];
    253:     PetscObjectReference((PetscObject)*B);
    254:   }
    255:   return(0);
    256: }

    258: static PetscErrorCode MatMult_STOperator(Mat Op,Vec x,Vec y)
    259: {
    261:   ST             st;

    264:   MatShellGetContext(Op,(void**)&st);
    265:   STSetUp(st);
    266:   PetscLogEventBegin(ST_Apply,st,x,y,0);
    267:   if (st->D) { /* with balancing */
    268:     VecPointwiseDivide(st->wb,x,st->D);
    269:     (*st->ops->apply)(st,st->wb,y);
    270:     VecPointwiseMult(y,y,st->D);
    271:   } else {
    272:     (*st->ops->apply)(st,x,y);
    273:   }
    274:   PetscLogEventEnd(ST_Apply,st,x,y,0);
    275:   return(0);
    276: }

    278: static PetscErrorCode MatMultTranspose_STOperator(Mat Op,Vec x,Vec y)
    279: {
    281:   ST             st;

    284:   MatShellGetContext(Op,(void**)&st);
    285:   STSetUp(st);
    286:   PetscLogEventBegin(ST_ApplyTranspose,st,x,y,0);
    287:   if (st->D) { /* with balancing */
    288:     VecPointwiseMult(st->wb,x,st->D);
    289:     (*st->ops->applytrans)(st,st->wb,y);
    290:     VecPointwiseDivide(y,y,st->D);
    291:   } else {
    292:     (*st->ops->applytrans)(st,x,y);
    293:   }
    294:   PetscLogEventEnd(ST_ApplyTranspose,st,x,y,0);
    295:   return(0);
    296: }

    298: #if defined(PETSC_USE_COMPLEX)
    299: static PetscErrorCode MatMultHermitianTranspose_STOperator(Mat Op,Vec x,Vec y)
    300: {
    302:   ST             st;

    305:   MatShellGetContext(Op,(void**)&st);
    306:   STSetUp(st);
    307:   PetscLogEventBegin(ST_ApplyTranspose,st,x,y,0);
    308:   if (!st->wht) {
    309:     MatCreateVecs(st->A[0],&st->wht,NULL);
    310:     PetscLogObjectParent((PetscObject)st,(PetscObject)st->wht);
    311:   }
    312:   VecCopy(x,st->wht);
    313:   VecConjugate(st->wht);
    314:   if (st->D) { /* with balancing */
    315:     VecPointwiseMult(st->wb,st->wht,st->D);
    316:     (*st->ops->applytrans)(st,st->wb,y);
    317:     VecPointwiseDivide(y,y,st->D);
    318:   } else {
    319:     (*st->ops->applytrans)(st,st->wht,y);
    320:   }
    321:   VecConjugate(y);
    322:   PetscLogEventEnd(ST_ApplyTranspose,st,x,y,0);
    323:   return(0);
    324: }
    325: #endif

    327: static PetscErrorCode MatMatMult_STOperator(Mat Op,Mat B,Mat C,void *ctx)
    328: {
    330:   ST             st;

    333:   MatShellGetContext(Op,(void**)&st);
    334:   STSetUp(st);
    335:   PetscLogEventBegin(ST_Apply,st,B,C,0);
    336:   STApplyMat_Generic(st,B,C);
    337:   PetscLogEventEnd(ST_Apply,st,B,C,0);
    338:   return(0);
    339: }

    341: PetscErrorCode STGetOperator_Private(ST st,Mat *Op)
    342: {
    344:   PetscInt       m,n,M,N;
    345:   Vec            v;
    346:   VecType        vtype;

    349:   if (!st->Op) {
    350:     if (Op) *Op = NULL;
    351:     /* create the shell matrix */
    352:     MatGetLocalSize(st->A[0],&m,&n);
    353:     MatGetSize(st->A[0],&M,&N);
    354:     MatCreateShell(PetscObjectComm((PetscObject)st),m,n,M,N,st,&st->Op);
    355:     MatShellSetOperation(st->Op,MATOP_MULT,(void(*)(void))MatMult_STOperator);
    356:     MatShellSetOperation(st->Op,MATOP_MULT_TRANSPOSE,(void(*)(void))MatMultTranspose_STOperator);
    357: #if defined(PETSC_USE_COMPLEX)
    358:     MatShellSetOperation(st->Op,MATOP_MULT_HERMITIAN_TRANSPOSE,(void(*)(void))MatMultHermitianTranspose_STOperator);
    359: #else
    360:     MatShellSetOperation(st->Op,MATOP_MULT_HERMITIAN_TRANSPOSE,(void(*)(void))MatMultTranspose_STOperator);
    361: #endif
    362:     if (!st->D && st->ops->apply==STApply_Generic) {
    363:       MatShellSetMatProductOperation(st->Op,MATPRODUCT_AB,NULL,MatMatMult_STOperator,NULL,MATDENSE,MATDENSE);
    364:     }
    365:     /* make sure the shell matrix generates a vector of the same type as the problem matrices */
    366:     MatCreateVecs(st->A[0],&v,NULL);
    367:     VecGetType(v,&vtype);
    368:     MatShellSetVecType(st->Op,vtype);
    369:     VecDestroy(&v);
    370:     /* build the operator matrices */
    371:     STComputeOperator(st);
    372:   }
    373:   if (Op) *Op = st->Op;
    374:   return(0);
    375: }

    377: /*@
    378:    STGetOperator - Returns a shell matrix that represents the operator of the
    379:    spectral transformation.

    381:    Collective on st

    383:    Input Parameter:
    384: .  st - the spectral transformation context

    386:    Output Parameter:
    387: .  Op - operator matrix

    389:    Notes:
    390:    The operator is defined in linear eigenproblems only, not in polynomial ones,
    391:    so the call will fail if more than 2 matrices were passed in STSetMatrices().

    393:    The returned shell matrix is essentially a wrapper to the STApply() and
    394:    STApplyTranspose() operations. The operator can often be expressed as

    396: .vb
    397:       Op = D*inv(K)*M*inv(D)
    398: .ve

    400:    where D is the balancing matrix, and M and K are two matrices corresponding
    401:    to the numerator and denominator for spectral transformations that represent
    402:    a rational matrix function. In the case of STSHELL, the inner part inv(K)*M
    403:    is replaced by the user-provided operation from STShellSetApply().

    405:    The preconditioner matrix K typically depends on the value of the shift, and
    406:    its inverse is handled via an internal KSP object. Normal usage does not
    407:    require explicitly calling STGetOperator(), but it can be used to force the
    408:    creation of K and M, and then K is passed to the KSP. This is useful for
    409:    setting options associated with the PCFactor (to set MUMPS options, for instance).

    411:    The returned matrix must NOT be destroyed by the user. Instead, when no
    412:    longer needed it must be returned with STRestoreOperator(). In particular,
    413:    this is required before modifying the ST matrices or the shift.

    415:    A NULL pointer can be passed in Op in case the matrix is not required but we
    416:    want to force its creation. In this case, STRestoreOperator() should not be
    417:    called.

    419:    Level: advanced

    421: .seealso: STApply(), STApplyTranspose(), STSetBalanceMatrix(), STShellSetApply(),
    422:           STGetKSP(), STSetShift(), STRestoreOperator(), STSetMatrices()
    423: @*/
    424: PetscErrorCode STGetOperator(ST st,Mat *Op)
    425: {

    431:   STCheckMatrices(st,1);
    432:   STCheckNotSeized(st,1);
    433:   if (st->nmat>2) SETERRQ(PetscObjectComm((PetscObject)st),PETSC_ERR_ARG_WRONGSTATE,"The operator is not defined in polynomial eigenproblems");
    434:   STGetOperator_Private(st,Op);
    435:   if (Op) st->opseized = PETSC_TRUE;
    436:   return(0);
    437: }

    439: /*@
    440:    STRestoreOperator - Restore the previously seized operator matrix.

    442:    Collective on st

    444:    Input Parameters:
    445: +  st - the spectral transformation context
    446: -  Op - operator matrix

    448:    Notes:
    449:    The arguments must match the corresponding call to STGetOperator().

    451:    Level: advanced

    453: .seealso: STGetOperator()
    454: @*/
    455: PetscErrorCode STRestoreOperator(ST st,Mat *Op)
    456: {
    461:   if (!st->opseized) SETERRQ(PetscObjectComm((PetscObject)st),PETSC_ERR_ARG_WRONGSTATE,"Must be called after STGetOperator()");
    462:   *Op = NULL;
    463:   st->opseized = PETSC_FALSE;
    464:   return(0);
    465: }

    467: /*
    468:    STComputeOperator - Computes the matrices that constitute the operator

    470:       Op = D*inv(K)*M*inv(D).

    472:    K and M are computed here (D is user-provided) from the system matrices
    473:    and the shift sigma (whenever these are changed, this function recomputes
    474:    K and M). This is used only in linear eigenproblems (nmat<3).

    476:    K is the "preconditioner matrix": it is the denominator in rational operators,
    477:    e.g. (A-sigma*B) in shift-and-invert. In non-rational transformations such
    478:    as STFILTER, K=NULL which means identity. After computing K, it is passed to
    479:    the internal KSP object via KSPSetOperators.

    481:    M is the numerator in rational operators. If unused it is set to NULL (e.g.
    482:    in STPRECOND).

    484:    STSHELL does not compute anything here, but sets the flag as if it was ready.
    485: */
    486: PetscErrorCode STComputeOperator(ST st)
    487: {
    489:   PC             pc;

    494:   if (!st->opready && st->ops->computeoperator) {
    495:     PetscInfo(st,"Building the operator matrices\n");
    496:     STCheckMatrices(st,1);
    497:     if (!st->T) {
    498:       PetscCalloc1(PetscMax(2,st->nmat),&st->T);
    499:       PetscLogObjectMemory((PetscObject)st,PetscMax(2,st->nmat)*sizeof(Mat));
    500:     }
    501:     PetscLogEventBegin(ST_ComputeOperator,st,0,0,0);
    502:     (*st->ops->computeoperator)(st);
    503:     PetscLogEventEnd(ST_ComputeOperator,st,0,0,0);
    504:     if (st->usesksp) {
    505:       if (!st->ksp) { STGetKSP(st,&st->ksp); }
    506:       if (st->P) {
    507:         STSetDefaultKSP(st);
    508:         STKSPSetOperators(st,st->P,st->Pmat?st->Pmat:st->P);
    509:       } else {
    510:         /* STPRECOND defaults to PCNONE if st->P is empty */
    511:         KSPGetPC(st->ksp,&pc);
    512:         PCSetType(pc,PCNONE);
    513:       }
    514:     }
    515:   }
    516:   st->opready = PETSC_TRUE;
    517:   return(0);
    518: }

    520: /*@
    521:    STSetUp - Prepares for the use of a spectral transformation.

    523:    Collective on st

    525:    Input Parameter:
    526: .  st - the spectral transformation context

    528:    Level: advanced

    530: .seealso: STCreate(), STApply(), STDestroy()
    531: @*/
    532: PetscErrorCode STSetUp(ST st)
    533: {
    534:   PetscInt       i,n,k;

    540:   STCheckMatrices(st,1);
    541:   switch (st->state) {
    542:     case ST_STATE_INITIAL:
    543:       PetscInfo(st,"Setting up new ST\n");
    544:       if (!((PetscObject)st)->type_name) {
    545:         STSetType(st,STSHIFT);
    546:       }
    547:       break;
    548:     case ST_STATE_SETUP:
    549:       return(0);
    550:     case ST_STATE_UPDATED:
    551:       PetscInfo(st,"Setting up updated ST\n");
    552:       break;
    553:   }
    554:   PetscLogEventBegin(ST_SetUp,st,0,0,0);
    555:   if (st->state!=ST_STATE_UPDATED) {
    556:     if (!(st->nmat<3 && st->opready)) {
    557:       if (st->T) {
    558:         for (i=0;i<PetscMax(2,st->nmat);i++) {
    559:           MatDestroy(&st->T[i]);
    560:         }
    561:       }
    562:       MatDestroy(&st->P);
    563:     }
    564:   }
    565:   if (st->D) {
    566:     MatGetLocalSize(st->A[0],NULL,&n);
    567:     VecGetLocalSize(st->D,&k);
    568:     if (n != k) SETERRQ2(PetscObjectComm((PetscObject)st),PETSC_ERR_ARG_SIZ,"Balance matrix has wrong dimension %D (should be %D)",k,n);
    569:     if (!st->wb) {
    570:       VecDuplicate(st->D,&st->wb);
    571:       PetscLogObjectParent((PetscObject)st,(PetscObject)st->wb);
    572:     }
    573:   }
    574:   if (st->nmat<3 && st->transform) {
    575:     STComputeOperator(st);
    576:   } else {
    577:     if (!st->T) {
    578:       PetscCalloc1(PetscMax(2,st->nmat),&st->T);
    579:       PetscLogObjectMemory((PetscObject)st,PetscMax(2,st->nmat)*sizeof(Mat));
    580:     }
    581:   }
    582:   if (st->ops->setup) { (*st->ops->setup)(st); }
    583:   st->state = ST_STATE_SETUP;
    584:   PetscLogEventEnd(ST_SetUp,st,0,0,0);
    585:   return(0);
    586: }

    588: /*
    589:    Computes coefficients for the transformed polynomial,
    590:    and stores the result in argument S.

    592:    alpha - value of the parameter of the transformed polynomial
    593:    beta - value of the previous shift (only used in inplace mode)
    594:    k - index of first matrix included in the computation
    595:    coeffs - coefficients of the expansion
    596:    initial - true if this is the first time (only relevant for shell mode)
    597: */
    598: PetscErrorCode STMatMAXPY_Private(ST st,PetscScalar alpha,PetscScalar beta,PetscInt k,PetscScalar *coeffs,PetscBool initial,Mat *S)
    599: {
    601:   PetscInt       *matIdx=NULL,nmat,i,ini=-1;
    602:   PetscScalar    t=1.0,ta,gamma;
    603:   PetscBool      nz=PETSC_FALSE;

    606:   nmat = st->nmat-k;
    607:   switch (st->matmode) {
    608:   case ST_MATMODE_INPLACE:
    609:     if (st->nmat>2) SETERRQ(PetscObjectComm((PetscObject)st),PETSC_ERR_SUP,"ST_MATMODE_INPLACE not supported for polynomial eigenproblems");
    610:     if (initial) {
    611:       PetscObjectReference((PetscObject)st->A[0]);
    612:       *S = st->A[0];
    613:       gamma = alpha;
    614:     } else gamma = alpha-beta;
    615:     if (gamma != 0.0) {
    616:       if (st->nmat>1) {
    617:         MatAXPY(*S,gamma,st->A[1],st->str);
    618:       } else {
    619:         MatShift(*S,gamma);
    620:       }
    621:     }
    622:     break;
    623:   case ST_MATMODE_SHELL:
    624:     if (initial) {
    625:       if (st->nmat>2) {
    626:         PetscMalloc1(nmat,&matIdx);
    627:         for (i=0;i<nmat;i++) matIdx[i] = k+i;
    628:       }
    629:       STMatShellCreate(st,alpha,nmat,matIdx,coeffs,S);
    630:       PetscLogObjectParent((PetscObject)st,(PetscObject)*S);
    631:       if (st->nmat>2) { PetscFree(matIdx); }
    632:     } else {
    633:       STMatShellShift(*S,alpha);
    634:     }
    635:     break;
    636:   case ST_MATMODE_COPY:
    637:     if (coeffs) {
    638:       for (i=0;i<nmat && ini==-1;i++) {
    639:         if (coeffs[i]!=0.0) ini = i;
    640:         else t *= alpha;
    641:       }
    642:       if (coeffs[ini] != 1.0) nz = PETSC_TRUE;
    643:       for (i=ini+1;i<nmat&&!nz;i++) if (coeffs[i]!=0.0) nz = PETSC_TRUE;
    644:     } else { nz = PETSC_TRUE; ini = 0; }
    645:     if ((alpha == 0.0 || !nz) && t==1.0) {
    646:       PetscObjectReference((PetscObject)st->A[k+ini]);
    647:       MatDestroy(S);
    648:       *S = st->A[k+ini];
    649:     } else {
    650:       if (*S && *S!=st->A[k+ini]) {
    651:         MatSetOption(*S,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE);
    652:         MatCopy(st->A[k+ini],*S,DIFFERENT_NONZERO_PATTERN);
    653:       } else {
    654:         MatDestroy(S);
    655:         MatDuplicate(st->A[k+ini],MAT_COPY_VALUES,S);
    656:         MatSetOption(*S,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_FALSE);
    657:         PetscLogObjectParent((PetscObject)st,(PetscObject)*S);
    658:       }
    659:       if (coeffs && coeffs[ini]!=1.0) {
    660:         MatScale(*S,coeffs[ini]);
    661:       }
    662:       for (i=ini+k+1;i<PetscMax(2,st->nmat);i++) {
    663:         t *= alpha;
    664:         ta = t;
    665:         if (coeffs) ta *= coeffs[i-k];
    666:         if (ta!=0.0) {
    667:           if (st->nmat>1) {
    668:             MatAXPY(*S,ta,st->A[i],st->str);
    669:           } else {
    670:             MatShift(*S,ta);
    671:           }
    672:         }
    673:       }
    674:     }
    675:   }
    676:   MatSetOption(*S,MAT_SYMMETRIC,st->asymm);
    677:   MatSetOption(*S,MAT_HERMITIAN,(PetscImaginaryPart(st->sigma)==0.0)?st->aherm:PETSC_FALSE);
    678:   return(0);
    679: }

    681: /*
    682:    Computes the values of the coefficients required by STMatMAXPY_Private
    683:    for the case of monomial basis.
    684: */
    685: PetscErrorCode STCoeffs_Monomial(ST st, PetscScalar *coeffs)
    686: {
    687:   PetscInt  k,i,ini,inip;

    690:   /* Compute binomial coefficients */
    691:   ini = (st->nmat*(st->nmat-1))/2;
    692:   for (i=0;i<st->nmat;i++) coeffs[ini+i]=1.0;
    693:   for (k=st->nmat-1;k>=1;k--) {
    694:     inip = ini+1;
    695:     ini = (k*(k-1))/2;
    696:     coeffs[ini] = 1.0;
    697:     for (i=1;i<k;i++) coeffs[ini+i] = coeffs[ini+i-1]+coeffs[inip+i-1];
    698:   }
    699:   return(0);
    700: }

    702: /*@
    703:    STPostSolve - Optional post-solve phase, intended for any actions that must
    704:    be performed on the ST object after the eigensolver has finished.

    706:    Collective on st

    708:    Input Parameters:
    709: .  st  - the spectral transformation context

    711:    Level: developer

    713: .seealso: EPSSolve()
    714: @*/
    715: PetscErrorCode STPostSolve(ST st)
    716: {

    722:   if (st->ops->postsolve) {
    723:     (*st->ops->postsolve)(st);
    724:   }
    725:   return(0);
    726: }

    728: /*@
    729:    STBackTransform - Back-transformation phase, intended for
    730:    spectral transformations which require to transform the computed
    731:    eigenvalues back to the original eigenvalue problem.

    733:    Not Collective

    735:    Input Parameters:
    736: +  st   - the spectral transformation context
    737:    eigr - real part of a computed eigenvalue
    738: -  eigi - imaginary part of a computed eigenvalue

    740:    Level: developer

    742: .seealso: STIsInjective()
    743: @*/
    744: PetscErrorCode STBackTransform(ST st,PetscInt n,PetscScalar* eigr,PetscScalar* eigi)
    745: {

    751:   if (st->ops->backtransform) {
    752:     (*st->ops->backtransform)(st,n,eigr,eigi);
    753:   }
    754:   return(0);
    755: }

    757: /*@
    758:    STIsInjective - Ask if this spectral transformation is injective or not
    759:    (that is, if it corresponds to a one-to-one mapping). If not, then it
    760:    does not make sense to call STBackTransform().

    762:    Not collective

    764:    Input Parameter:
    765: .  st   - the spectral transformation context

    767:    Output Parameter:
    768: .  is - the answer

    770:    Level: developer

    772: .seealso: STBackTransform()
    773: @*/
    774: PetscErrorCode STIsInjective(ST st,PetscBool* is)
    775: {
    777:   PetscBool      shell;

    784:   PetscObjectTypeCompare((PetscObject)st,STSHELL,&shell);
    785:   if (shell) {
    786:     STIsInjective_Shell(st,is);
    787:   } else *is = st->ops->backtransform? PETSC_TRUE: PETSC_FALSE;
    788:   return(0);
    789: }

    791: /*@
    792:    STMatSetUp - Build the preconditioner matrix used in STMatSolve().

    794:    Collective on st

    796:    Input Parameters:
    797: +  st     - the spectral transformation context
    798: .  sigma  - the shift
    799: -  coeffs - the coefficients (may be NULL)

    801:    Note:
    802:    This function is not intended to be called by end users, but by SLEPc
    803:    solvers that use ST. It builds matrix st->P as follows, then calls KSPSetUp().
    804: .vb
    805:     If (coeffs):  st->P = Sum_{i=0:nmat-1} coeffs[i]*sigma^i*A_i.
    806:     else          st->P = Sum_{i=0:nmat-1} sigma^i*A_i
    807: .ve

    809:    Level: developer

    811: .seealso: STMatSolve()
    812: @*/
    813: PetscErrorCode STMatSetUp(ST st,PetscScalar sigma,PetscScalar *coeffs)
    814: {

    820:   STCheckMatrices(st,1);

    822:   PetscLogEventBegin(ST_MatSetUp,st,0,0,0);
    823:   STMatMAXPY_Private(st,sigma,0.0,0,coeffs,PETSC_TRUE,&st->P);
    824:   STKSPSetOperators(st,st->P,st->Pmat?st->Pmat:st->P);
    825:   KSPSetUp(st->ksp);
    826:   PetscLogEventEnd(ST_MatSetUp,st,0,0,0);
    827:   return(0);
    828: }

    830: /*@
    831:    STSetWorkVecs - Sets a number of work vectors into the ST object.

    833:    Collective on st

    835:    Input Parameters:
    836: +  st - the spectral transformation context
    837: -  nw - number of work vectors to allocate

    839:    Developers Note:
    840:    This is SLEPC_EXTERN because it may be required by shell STs.

    842:    Level: developer
    843: @*/
    844: PetscErrorCode STSetWorkVecs(ST st,PetscInt nw)
    845: {
    847:   PetscInt       i;

    852:   if (nw <= 0) SETERRQ1(PetscObjectComm((PetscObject)st),PETSC_ERR_ARG_OUTOFRANGE,"nw must be > 0: nw = %D",nw);
    853:   if (st->nwork < nw) {
    854:     VecDestroyVecs(st->nwork,&st->work);
    855:     st->nwork = nw;
    856:     PetscMalloc1(nw,&st->work);
    857:     for (i=0;i<nw;i++) { STMatCreateVecs(st,&st->work[i],NULL); }
    858:     PetscLogObjectParents(st,nw,st->work);
    859:   }
    860:   return(0);
    861: }