home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / mesch12a.zip / schur.c < prev    next >
C/C++ Source or Header  |  1994-01-13  |  17KB  |  600 lines

  1.  
  2. /**************************************************************************
  3. **
  4. ** Copyright (C) 1993 David E. Steward & Zbigniew Leyk, all rights reserved.
  5. **
  6. **                 Meschach Library
  7. ** 
  8. ** This Meschach Library is provided "as is" without any express 
  9. ** or implied warranty of any kind with respect to this software. 
  10. ** In particular the authors shall not be liable for any direct, 
  11. ** indirect, special, incidental or consequential damages arising 
  12. ** in any way from use of the software.
  13. ** 
  14. ** Everyone is granted permission to copy, modify and redistribute this
  15. ** Meschach Library, provided:
  16. **  1.  All copies contain this copyright notice.
  17. **  2.  All modified copies shall carry a notice stating who
  18. **      made the last modification and the date of such modification.
  19. **  3.  No charge is made for this software or works derived from it.  
  20. **      This clause shall not be construed as constraining other software
  21. **      distributed on the same medium as this software, nor is a
  22. **      distribution fee considered a charge.
  23. **
  24. ***************************************************************************/
  25.  
  26.  
  27. /*    
  28.     File containing routines for computing the Schur decomposition
  29.     of a real non-symmetric matrix
  30.     See also: hessen.c
  31. */
  32.  
  33. #include    <stdio.h>
  34. #include    <math.h>
  35. #include    "matrix.h"
  36. #include        "matrix2.h"
  37.  
  38.  
  39. static char rcsid[] = "$Id: schur.c,v 1.4 1994/01/13 05:35:52 des Exp $";
  40.  
  41.  
  42.  
  43. #ifndef ANSI_C
  44. static    void    hhldr3(x,y,z,nu1,beta,newval)
  45. double    x, y, z;
  46. Real    *nu1, *beta, *newval;
  47. #else
  48. static    void    hhldr3(double x, double y, double z,
  49.                Real *nu1, Real *beta, Real *newval)
  50. #endif
  51. {
  52.     Real    alpha;
  53.  
  54.     if ( x >= 0.0 )
  55.         alpha = sqrt(x*x+y*y+z*z);
  56.     else
  57.         alpha = -sqrt(x*x+y*y+z*z);
  58.     *nu1 = x + alpha;
  59.     *beta = 1.0/(alpha*(*nu1));
  60.     *newval = alpha;
  61. }
  62.  
  63. #ifndef ANSI_C
  64. static    void    hhldr3cols(A,k,j0,beta,nu1,nu2,nu3)
  65. MAT    *A;
  66. int    k, j0;
  67. double    beta, nu1, nu2, nu3;
  68. #else
  69. static    void    hhldr3cols(MAT *A, int k, int j0, double beta,
  70.                double nu1, double nu2, double nu3)
  71. #endif
  72. {
  73.     Real    **A_me, ip, prod;
  74.     int    j, n;
  75.  
  76.     if ( k < 0 || k+3 > A->m || j0 < 0 )
  77.         error(E_BOUNDS,"hhldr3cols");
  78.     A_me = A->me;        n = A->n;
  79.  
  80.     /* printf("hhldr3cols:(l.%d) j0 = %d, k = %d, A at 0x%lx, m = %d, n = %d\n",
  81.            __LINE__, j0, k, (long)A, A->m, A->n); */
  82.     /* printf("hhldr3cols: A (dumped) =\n");    m_dump(stdout,A); */
  83.  
  84.     for ( j = j0; j < n; j++ )
  85.     {
  86.         /*****        
  87.         ip = nu1*A_me[k][j] + nu2*A_me[k+1][j] + nu3*A_me[k+2][j];
  88.         prod = ip*beta;
  89.         A_me[k][j]   -= prod*nu1;
  90.         A_me[k+1][j] -= prod*nu2;
  91.         A_me[k+2][j] -= prod*nu3;
  92.         *****/
  93.         /* printf("hhldr3cols: j = %d\n", j); */
  94.  
  95.         ip = nu1*m_entry(A,k,j)+nu2*m_entry(A,k+1,j)+nu3*m_entry(A,k+2,j);
  96.         prod = ip*beta;
  97.         /*****
  98.         m_set_val(A,k  ,j,m_entry(A,k  ,j) - prod*nu1);
  99.         m_set_val(A,k+1,j,m_entry(A,k+1,j) - prod*nu2);
  100.         m_set_val(A,k+2,j,m_entry(A,k+2,j) - prod*nu3);
  101.         *****/
  102.         m_add_val(A,k  ,j,-prod*nu1);
  103.         m_add_val(A,k+1,j,-prod*nu2);
  104.         m_add_val(A,k+2,j,-prod*nu3);
  105.  
  106.     }
  107.     /* printf("hhldr3cols:(l.%d) j0 = %d, k = %d, m = %d, n = %d\n",
  108.            __LINE__, j0, k, A->m, A->n); */
  109.     /* putc('\n',stdout); */
  110. }
  111.  
  112. #ifndef ANSI_C
  113. static    void    hhldr3rows(A,k,i0,beta,nu1,nu2,nu3)
  114. MAT    *A;
  115. int    k, i0;
  116. double    beta, nu1, nu2, nu3;
  117. #else
  118. static    void    hhldr3rows(MAT *A, int k, int i0, double beta,
  119.                double nu1, double nu2, double nu3)
  120. #endif
  121. {
  122.     Real    **A_me, ip, prod;
  123.     int    i, m;
  124.  
  125.     /* printf("hhldr3rows:(l.%d) A at 0x%lx\n", __LINE__, (long)A); */
  126.     /* printf("hhldr3rows: k = %d\n", k); */
  127.     if ( k < 0 || k+3 > A->n )
  128.         error(E_BOUNDS,"hhldr3rows");
  129.     A_me = A->me;        m = A->m;
  130.     i0 = min(i0,m-1);
  131.  
  132.     for ( i = 0; i <= i0; i++ )
  133.     {
  134.         /****
  135.         ip = nu1*A_me[i][k] + nu2*A_me[i][k+1] + nu3*A_me[i][k+2];
  136.         prod = ip*beta;
  137.         A_me[i][k]   -= prod*nu1;
  138.         A_me[i][k+1] -= prod*nu2;
  139.         A_me[i][k+2] -= prod*nu3;
  140.         ****/
  141.  
  142.         ip = nu1*m_entry(A,i,k)+nu2*m_entry(A,i,k+1)+nu3*m_entry(A,i,k+2);
  143.         prod = ip*beta;
  144.         m_add_val(A,i,k  , - prod*nu1);
  145.         m_add_val(A,i,k+1, - prod*nu2);
  146.         m_add_val(A,i,k+2, - prod*nu3);
  147.  
  148.     }
  149. }
  150.  
  151. /* schur -- computes the Schur decomposition of the matrix A in situ
  152.     -- optionally, gives Q matrix such that Q^T.A.Q is upper triangular
  153.     -- returns upper triangular Schur matrix */
  154. MAT    *schur(A,Q)
  155. MAT    *A, *Q;
  156. {
  157.     int        i, j, iter, k, k_min, k_max, k_tmp, n, split;
  158.     Real    beta2, c, discrim, dummy, nu1, s, t, tmp, x, y, z;
  159.     Real    **A_me;
  160.     static    VEC    *diag=VNULL, *beta=VNULL;
  161.     
  162.     if ( ! A )
  163.     error(E_NULL,"schur");
  164.     if ( A->m != A->n || ( Q && Q->m != Q->n ) )
  165.     error(E_SQUARE,"schur");
  166.     if ( Q != MNULL && Q->m != A->m )
  167.     error(E_SIZES,"schur");
  168.     n = A->n;
  169.     diag = v_resize(diag,A->n);
  170.     beta = v_resize(beta,A->n);
  171.     MEM_STAT_REG(diag,TYPE_VEC);
  172.     MEM_STAT_REG(beta,TYPE_VEC);
  173.     /* compute Hessenberg form */
  174.     Hfactor(A,diag,beta);
  175.     
  176.     /* save Q if necessary */
  177.     if ( Q )
  178.     Q = makeHQ(A,diag,beta,Q);
  179.     makeH(A,A);
  180.  
  181.  
  182.     k_min = 0;    A_me = A->me;
  183.  
  184.     while ( k_min < n )
  185.     {
  186.     /* find k_max to suit:
  187.        submatrix k_min..k_max should be irreducible */
  188.     k_max = n-1;
  189.     for ( k = k_min; k < k_max; k++ )
  190.         /* if ( A_me[k+1][k] == 0.0 ) */
  191.         if ( m_entry(A,k+1,k) == 0.0 )
  192.         {    k_max = k;    break;    }
  193.  
  194.     if ( k_max <= k_min )
  195.     {
  196.         k_min = k_max + 1;
  197.         continue;        /* outer loop */
  198.     }
  199.  
  200.     /* check to see if we have a 2 x 2 block
  201.        with complex eigenvalues */
  202.     if ( k_max == k_min + 1 )
  203.     {
  204.         /* tmp = A_me[k_min][k_min] - A_me[k_max][k_max]; */
  205.         tmp = m_entry(A,k_min,k_min) - m_entry(A,k_max,k_max);
  206.         /* discrim = tmp*tmp +
  207.         4*A_me[k_min][k_max]*A_me[k_max][k_min]; */
  208.         discrim = tmp*tmp +
  209.         4*m_entry(A,k_min,k_max)*m_entry(A,k_max,k_min);
  210.         if ( discrim < 0.0 )
  211.         {    /* yes -- e-vals are complex -- leave 2 x 2 block */
  212.         k_min = k_max + 1;
  213.         continue;
  214.         }
  215.     }
  216.  
  217.     /* now have r x r block with r >= 2:
  218.        apply Francis QR step until block splits */
  219.     split = FALSE;        iter = 0;
  220.     while ( ! split )
  221.     {
  222.         Real    a00, a01, a10, a11;
  223.  
  224.         iter++;
  225.         
  226.         /* set up Wilkinson/Francis complex shift */
  227.         k_tmp = k_max - 1;
  228.  
  229.         a00 = m_entry(A,k_tmp,k_tmp);
  230.         a01 = m_entry(A,k_tmp,k_max);
  231.         a10 = m_entry(A,k_max,k_tmp);
  232.         a11 = m_entry(A,k_max,k_max);
  233.  
  234.         /********************
  235.         a00 = A->me[k_tmp][k_tmp];
  236.         a01 = A->me[k_tmp][k_max];
  237.         a10 = A->me[k_max][k_tmp];
  238.         a11 = A->me[k_max][k_max];
  239.         ********************/
  240.         /********************
  241.         s = A_me[k_tmp][k_tmp] + A_me[k_max][k_max];
  242.         t = A_me[k_tmp][k_tmp]*A_me[k_max][k_max] -
  243.         A_me[k_tmp][k_max]*A_me[k_max][k_tmp];
  244.         ********************/
  245.         s = a00 + a11;
  246.         t = a00*a11 - a01*a10;
  247.  
  248.         /* break loop if a 2 x 2 complex block */
  249.         if ( k_max == k_min + 1 && s*s < 4.0*t )
  250.         {
  251.         split = TRUE;
  252.         continue;
  253.         }
  254.  
  255.         /* perturb shift if convergence is slow */
  256.         if ( (iter % 10) == 0 )
  257.         {    s += iter*0.02;        t += iter*0.02;
  258.         }
  259.  
  260.         /* set up Householder transformations */
  261.         k_tmp = k_min + 1;
  262.         /********************
  263.         x = A_me[k_min][k_min]*A_me[k_min][k_min] +
  264.         A_me[k_min][k_tmp]*A_me[k_tmp][k_min] -
  265.             s*A_me[k_min][k_min] + t;
  266.         y = A_me[k_tmp][k_min]*
  267.         (A_me[k_min][k_min]+A_me[k_tmp][k_tmp]-s);
  268.         if ( k_min + 2 <= k_max )
  269.         z = A_me[k_tmp][k_min]*A_me[k_min+2][k_tmp];
  270.         else
  271.         z = 0.0;
  272.         ********************/
  273.  
  274.         a00 = m_entry(A,k_min,k_min);
  275.         a01 = m_entry(A,k_min,k_tmp);
  276.         a10 = m_entry(A,k_tmp,k_min);
  277.         a11 = m_entry(A,k_tmp,k_tmp);
  278.  
  279.         /********************
  280.         a00 = A->me[k_min][k_min];
  281.         a01 = A->me[k_min][k_tmp];
  282.         a10 = A->me[k_tmp][k_min];
  283.         a11 = A->me[k_tmp][k_tmp];
  284.         ********************/
  285.         x = a00*a00 + a01*a10 - s*a00 + t;
  286.         y = a10*(a00+a11-s);
  287.         if ( k_min + 2 <= k_max )
  288.         z = a10* /* m_entry(A,k_min+2,k_tmp) */ A->me[k_min+2][k_tmp];
  289.         else
  290.         z = 0.0;
  291.  
  292.         for ( k = k_min; k <= k_max-1; k++ )
  293.         {
  294.         if ( k < k_max - 1 )
  295.         {
  296.             hhldr3(x,y,z,&nu1,&beta2,&dummy);
  297.             tracecatch(hhldr3cols(A,k,max(k-1,0),  beta2,nu1,y,z),"schur");
  298.             tracecatch(hhldr3rows(A,k,min(n-1,k+3),beta2,nu1,y,z),"schur");
  299.             if ( Q != MNULL )
  300.             hhldr3rows(Q,k,n-1,beta2,nu1,y,z);
  301.         }
  302.         else
  303.         {
  304.             givens(x,y,&c,&s);
  305.             rot_cols(A,k,k+1,c,s,A);
  306.             rot_rows(A,k,k+1,c,s,A);
  307.             if ( Q )
  308.             rot_cols(Q,k,k+1,c,s,Q);
  309.         }
  310.         /* if ( k >= 2 )
  311.             m_set_val(A,k,k-2,0.0); */
  312.         /* x = A_me[k+1][k]; */
  313.         x = m_entry(A,k+1,k);
  314.         if ( k <= k_max - 2 )
  315.             /* y = A_me[k+2][k];*/
  316.             y = m_entry(A,k+2,k);
  317.         else
  318.             y = 0.0;
  319.         if ( k <= k_max - 3 )
  320.             /* z = A_me[k+3][k]; */
  321.             z = m_entry(A,k+3,k);
  322.         else
  323.             z = 0.0;
  324.         }
  325.         /* if ( k_min > 0 )
  326.         m_set_val(A,k_min,k_min-1,0.0);
  327.         if ( k_max < n - 1 )
  328.         m_set_val(A,k_max+1,k_max,0.0); */
  329.         for ( k = k_min; k <= k_max-2; k++ )
  330.         {
  331.         /* zero appropriate sub-diagonals */
  332.         m_set_val(A,k+2,k,0.0);
  333.         if ( k < k_max-2 )
  334.             m_set_val(A,k+3,k,0.0);
  335.         }
  336.  
  337.         /* test to see if matrix should split */
  338.         for ( k = k_min; k < k_max; k++ )
  339.         if ( fabs(A_me[k+1][k]) < MACHEPS*
  340.             (fabs(A_me[k][k])+fabs(A_me[k+1][k+1])) )
  341.         {    A_me[k+1][k] = 0.0;    split = TRUE;    }
  342.     }
  343.     }
  344.     
  345.     /* polish up A by zeroing strictly lower triangular elements
  346.        and small sub-diagonal elements */
  347.     for ( i = 0; i < A->m; i++ )
  348.     for ( j = 0; j < i-1; j++ )
  349.         A_me[i][j] = 0.0;
  350.     for ( i = 0; i < A->m - 1; i++ )
  351.     if ( fabs(A_me[i+1][i]) < MACHEPS*
  352.         (fabs(A_me[i][i])+fabs(A_me[i+1][i+1])) )
  353.         A_me[i+1][i] = 0.0;
  354.  
  355.     return A;
  356. }
  357.  
  358. /* schur_vals -- compute real & imaginary parts of eigenvalues
  359.     -- assumes T contains a block upper triangular matrix
  360.         as produced by schur()
  361.     -- real parts stored in real_pt, imaginary parts in imag_pt */
  362. void    schur_evals(T,real_pt,imag_pt)
  363. MAT    *T;
  364. VEC    *real_pt, *imag_pt;
  365. {
  366.     int    i, n;
  367.     Real    discrim, **T_me;
  368.     Real    diff, sum, tmp;
  369.  
  370.     if ( ! T || ! real_pt || ! imag_pt )
  371.         error(E_NULL,"schur_evals");
  372.     if ( T->m != T->n )
  373.         error(E_SQUARE,"schur_evals");
  374.     n = T->n;    T_me = T->me;
  375.     real_pt = v_resize(real_pt,(u_int)n);
  376.     imag_pt = v_resize(imag_pt,(u_int)n);
  377.  
  378.     i = 0;
  379.     while ( i < n )
  380.     {
  381.         if ( i < n-1 && T_me[i+1][i] != 0.0 )
  382.         {   /* should be a complex eigenvalue */
  383.             sum  = 0.5*(T_me[i][i]+T_me[i+1][i+1]);
  384.             diff = 0.5*(T_me[i][i]-T_me[i+1][i+1]);
  385.             discrim = diff*diff + T_me[i][i+1]*T_me[i+1][i];
  386.             if ( discrim < 0.0 )
  387.             {    /* yes -- complex e-vals */
  388.             real_pt->ve[i] = real_pt->ve[i+1] = sum;
  389.             imag_pt->ve[i] = sqrt(-discrim);
  390.             imag_pt->ve[i+1] = - imag_pt->ve[i];
  391.             }
  392.             else
  393.             {    /* no -- actually both real */
  394.             tmp = sqrt(discrim);
  395.             real_pt->ve[i]   = sum + tmp;
  396.             real_pt->ve[i+1] = sum - tmp;
  397.             imag_pt->ve[i]   = imag_pt->ve[i+1] = 0.0;
  398.             }
  399.             i += 2;
  400.         }
  401.         else
  402.         {   /* real eigenvalue */
  403.             real_pt->ve[i] = T_me[i][i];
  404.             imag_pt->ve[i] = 0.0;
  405.             i++;
  406.         }
  407.     }
  408. }
  409.  
  410. /* schur_vecs -- returns eigenvectors computed from the real Schur
  411.         decomposition of a matrix
  412.     -- T is the block upper triangular Schur matrix
  413.     -- Q is the orthognal matrix where A = Q.T.Q^T
  414.     -- if Q is null, the eigenvectors of T are returned
  415.     -- X_re is the real part of the matrix of eigenvectors,
  416.         and X_im is the imaginary part of the matrix.
  417.     -- X_re is returned */
  418. MAT    *schur_vecs(T,Q,X_re,X_im)
  419. MAT    *T, *Q, *X_re, *X_im;
  420. {
  421.     int    i, j, limit;
  422.     Real    t11_re, t11_im, t12, t21, t22_re, t22_im;
  423.     Real    l_re, l_im, det_re, det_im, invdet_re, invdet_im,
  424.         val1_re, val1_im, val2_re, val2_im,
  425.         tmp_val1_re, tmp_val1_im, tmp_val2_re, tmp_val2_im, **T_me;
  426.     Real    sum, diff, discrim, magdet, norm, scale;
  427.     static VEC    *tmp1_re=VNULL, *tmp1_im=VNULL,
  428.             *tmp2_re=VNULL, *tmp2_im=VNULL;
  429.  
  430.     if ( ! T || ! X_re )
  431.         error(E_NULL,"schur_vecs");
  432.     if ( T->m != T->n || X_re->m != X_re->n ||
  433.         ( Q != MNULL && Q->m != Q->n ) ||
  434.         ( X_im != MNULL && X_im->m != X_im->n ) )
  435.         error(E_SQUARE,"schur_vecs");
  436.     if ( T->m != X_re->m ||
  437.         ( Q != MNULL && T->m != Q->m ) ||
  438.         ( X_im != MNULL && T->m != X_im->m ) )
  439.         error(E_SIZES,"schur_vecs");
  440.  
  441.     tmp1_re = v_resize(tmp1_re,T->m);
  442.     tmp1_im = v_resize(tmp1_im,T->m);
  443.     tmp2_re = v_resize(tmp2_re,T->m);
  444.     tmp2_im = v_resize(tmp2_im,T->m);
  445.     MEM_STAT_REG(tmp1_re,TYPE_VEC);
  446.     MEM_STAT_REG(tmp1_im,TYPE_VEC);
  447.     MEM_STAT_REG(tmp2_re,TYPE_VEC);
  448.     MEM_STAT_REG(tmp2_im,TYPE_VEC);
  449.  
  450.     T_me = T->me;
  451.     i = 0;
  452.     while ( i < T->m )
  453.     {
  454.         if ( i+1 < T->m && T->me[i+1][i] != 0.0 )
  455.         {    /* complex eigenvalue */
  456.         sum  = 0.5*(T_me[i][i]+T_me[i+1][i+1]);
  457.         diff = 0.5*(T_me[i][i]-T_me[i+1][i+1]);
  458.         discrim = diff*diff + T_me[i][i+1]*T_me[i+1][i];
  459.         l_re = l_im = 0.0;
  460.         if ( discrim < 0.0 )
  461.         {    /* yes -- complex e-vals */
  462.             l_re = sum;
  463.             l_im = sqrt(-discrim);
  464.         }
  465.         else /* not correct Real Schur form */
  466.             error(E_RANGE,"schur_vecs");
  467.         }
  468.         else
  469.         {
  470.         l_re = T_me[i][i];
  471.         l_im = 0.0;
  472.         }
  473.  
  474.         v_zero(tmp1_im);
  475.         v_rand(tmp1_re);
  476.         sv_mlt(MACHEPS,tmp1_re,tmp1_re);
  477.  
  478.         /* solve (T-l.I)x = tmp1 */
  479.         limit = ( l_im != 0.0 ) ? i+1 : i;
  480.         /* printf("limit = %d\n",limit); */
  481.         for ( j = limit+1; j < T->m; j++ )
  482.         tmp1_re->ve[j] = 0.0;
  483.         j = limit;
  484.         while ( j >= 0 )
  485.         {
  486.         if ( j > 0 && T->me[j][j-1] != 0.0 )
  487.         {   /* 2 x 2 diagonal block */
  488.             /* printf("checkpoint A\n"); */
  489.             val1_re = tmp1_re->ve[j-1] -
  490.               __ip__(&(tmp1_re->ve[j+1]),&(T->me[j-1][j+1]),limit-j);
  491.             /* printf("checkpoint B\n"); */
  492.             val1_im = tmp1_im->ve[j-1] -
  493.               __ip__(&(tmp1_im->ve[j+1]),&(T->me[j-1][j+1]),limit-j);
  494.             /* printf("checkpoint C\n"); */
  495.             val2_re = tmp1_re->ve[j] -
  496.               __ip__(&(tmp1_re->ve[j+1]),&(T->me[j][j+1]),limit-j);
  497.             /* printf("checkpoint D\n"); */
  498.             val2_im = tmp1_im->ve[j] -
  499.               __ip__(&(tmp1_im->ve[j+1]),&(T->me[j][j+1]),limit-j);
  500.             /* printf("checkpoint E\n"); */
  501.             
  502.             t11_re = T_me[j-1][j-1] - l_re;
  503.             t11_im = - l_im;
  504.             t22_re = T_me[j][j] - l_re;
  505.             t22_im = - l_im;
  506.             t12 = T_me[j-1][j];
  507.             t21 = T_me[j][j-1];
  508.  
  509.             scale =  fabs(T_me[j-1][j-1]) + fabs(T_me[j][j]) +
  510.             fabs(t12) + fabs(t21) + fabs(l_re) + fabs(l_im);
  511.  
  512.             det_re = t11_re*t22_re - t11_im*t22_im - t12*t21;
  513.             det_im = t11_re*t22_im + t11_im*t22_re;
  514.             magdet = det_re*det_re+det_im*det_im;
  515.             if ( sqrt(magdet) < MACHEPS*scale )
  516.             {
  517.                 det_re = MACHEPS*scale;
  518.             magdet = det_re*det_re+det_im*det_im;
  519.             }
  520.             invdet_re =   det_re/magdet;
  521.             invdet_im = - det_im/magdet;
  522.             tmp_val1_re = t22_re*val1_re-t22_im*val1_im-t12*val2_re;
  523.             tmp_val1_im = t22_im*val1_re+t22_re*val1_im-t12*val2_im;
  524.             tmp_val2_re = t11_re*val2_re-t11_im*val2_im-t21*val1_re;
  525.             tmp_val2_im = t11_im*val2_re+t11_re*val2_im-t21*val1_im;
  526.             tmp1_re->ve[j-1] = invdet_re*tmp_val1_re -
  527.                     invdet_im*tmp_val1_im;
  528.             tmp1_im->ve[j-1] = invdet_im*tmp_val1_re +
  529.                     invdet_re*tmp_val1_im;
  530.             tmp1_re->ve[j]   = invdet_re*tmp_val2_re -
  531.                     invdet_im*tmp_val2_im;
  532.             tmp1_im->ve[j]   = invdet_im*tmp_val2_re +
  533.                     invdet_re*tmp_val2_im;
  534.             j -= 2;
  535.             }
  536.             else
  537.         {
  538.             t11_re = T_me[j][j] - l_re;
  539.             t11_im = - l_im;
  540.             magdet = t11_re*t11_re + t11_im*t11_im;
  541.             scale = fabs(T_me[j][j]) + fabs(l_re);
  542.             if ( sqrt(magdet) < MACHEPS*scale )
  543.             {
  544.                 t11_re = MACHEPS*scale;
  545.             magdet = t11_re*t11_re + t11_im*t11_im;
  546.             }
  547.             invdet_re =   t11_re/magdet;
  548.             invdet_im = - t11_im/magdet;
  549.             /* printf("checkpoint F\n"); */
  550.             val1_re = tmp1_re->ve[j] -
  551.               __ip__(&(tmp1_re->ve[j+1]),&(T->me[j][j+1]),limit-j);
  552.             /* printf("checkpoint G\n"); */
  553.             val1_im = tmp1_im->ve[j] -
  554.               __ip__(&(tmp1_im->ve[j+1]),&(T->me[j][j+1]),limit-j);
  555.             /* printf("checkpoint H\n"); */
  556.             tmp1_re->ve[j] = invdet_re*val1_re - invdet_im*val1_im;
  557.             tmp1_im->ve[j] = invdet_im*val1_re + invdet_re*val1_im;
  558.             j -= 1;
  559.         }
  560.         }
  561.  
  562.         norm = v_norm_inf(tmp1_re) + v_norm_inf(tmp1_im);
  563.         sv_mlt(1/norm,tmp1_re,tmp1_re);
  564.         if ( l_im != 0.0 )
  565.         sv_mlt(1/norm,tmp1_im,tmp1_im);
  566.         mv_mlt(Q,tmp1_re,tmp2_re);
  567.         if ( l_im != 0.0 )
  568.         mv_mlt(Q,tmp1_im,tmp2_im);
  569.         if ( l_im != 0.0 )
  570.         norm = sqrt(in_prod(tmp2_re,tmp2_re)+in_prod(tmp2_im,tmp2_im));
  571.         else
  572.         norm = v_norm2(tmp2_re);
  573.         sv_mlt(1/norm,tmp2_re,tmp2_re);
  574.         if ( l_im != 0.0 )
  575.         sv_mlt(1/norm,tmp2_im,tmp2_im);
  576.  
  577.         if ( l_im != 0.0 )
  578.         {
  579.         if ( ! X_im )
  580.         error(E_NULL,"schur_vecs");
  581.         set_col(X_re,i,tmp2_re);
  582.         set_col(X_im,i,tmp2_im);
  583.         sv_mlt(-1.0,tmp2_im,tmp2_im);
  584.         set_col(X_re,i+1,tmp2_re);
  585.         set_col(X_im,i+1,tmp2_im);
  586.         i += 2;
  587.         }
  588.         else
  589.         {
  590.         set_col(X_re,i,tmp2_re);
  591.         if ( X_im != MNULL )
  592.             set_col(X_im,i,tmp1_im);    /* zero vector */
  593.         i += 1;
  594.         }
  595.     }
  596.  
  597.     return X_re;
  598. }
  599.  
  600.