예제 #1
0
void eml_xgesvd(const float A[25], float U[25], float S[5], float V[25])
{
  float b_A[25];
  float s[5];
  float e[5];
  float work[5];
  int kase;
  int q;
  int iter;
  boolean_T apply_transform;
  float ztest0;
  int qp1jj;
  int qs;
  int m;
  float rt;
  float ztest;
  float snorm;
  int32_T exitg3;
  boolean_T exitg2;
  float f;
  float varargin_1[5];
  float mtmp;
  boolean_T exitg1;
  float sqds;
  float mtmp_inv;

  for (int k=0;k<25;k++)
    b_A[k] = A[k];
  //mem_fp_cpy(&b_A[0], &A[0], 25);
  //memcpy(&b_A[0], &A[0], 25U * sizeof(float));
  for (kase = 0; kase < 5; kase++) {
    s[kase] = 0.0F;
    e[kase] = 0.0F;
    work[kase] = 0.0F;
  }

  for (kase = 0; kase < 25; kase++) {
    U[kase] = 0.0F;
    V[kase] = 0.0F;
  }

  for (q = 0; q < 4; q++) {
    iter = q + 5 * q;
    apply_transform = false;
    ztest0 = eml_xnrm2(5 - q, b_A, iter + 1);
    if (ztest0 > 0.0F) {
      apply_transform = true;
      if (b_A[iter] < 0.0F) {
        s[q] = -ztest0;
      } else {
        s[q] = ztest0;
      }

      if (fAbs(s[q]) >= 9.86076132E-32F) {
        ztest0 = fDiv(1.0F,  s[q]);
        kase = (iter - q) + 5;
        for (qp1jj = iter; qp1jj + 1 <= kase; qp1jj++) {
          b_A[qp1jj] *= ztest0;
        }
      } else {
        kase = (iter - q) + 5;
        for (qp1jj = iter; qp1jj + 1 <= kase; qp1jj++) {
          b_A[qp1jj] = fDiv(b_A[qp1jj],s[q]);
        }
      }

      b_A[iter]++;
      s[q] = -s[q];
    } else {
      s[q] = 0.0F;
    }

    for (qs = q + 1; qs + 1 < 6; qs++) {
      kase = q + 5 * qs;
      if (apply_transform) {
        eml_xaxpy(5 - q, -(fDiv(eml_xdotc(5 - q, b_A, iter + 1, b_A, kase + 1), b_A[q + 5 * q])), iter + 1, b_A, kase + 1);
      }

      e[qs] = b_A[kase];
    }

    for (qp1jj = q; qp1jj + 1 < 6; qp1jj++) {
      U[qp1jj + 5 * q] = b_A[qp1jj + 5 * q];
    }

    if (q + 1 <= 3) {
      ztest0 = b_eml_xnrm2(4 - q, e, q + 2);
      if (ztest0 == 0.0F) {
        e[q] = 0.0F;
      } else {
        if (e[q + 1] < 0.0F) {
          e[q] = -ztest0;
        } else {
          e[q] = ztest0;
        }

        ztest0 = e[q];
        if (fAbs(e[q]) >= 9.86076132E-32F) {
          ztest0 = fDiv(1.0F, e[q]);
          for (qp1jj = q + 1; qp1jj + 1 < 6; qp1jj++) {
            e[qp1jj] *= ztest0;
          }
        } else {
          for (qp1jj = q + 1; qp1jj + 1 < 6; qp1jj++) {
            e[qp1jj] = fDiv(e[qp1jj],ztest0);
          }
        }

        e[q + 1]++;
        e[q] = -e[q];
        for (qp1jj = q + 1; qp1jj + 1 < 6; qp1jj++) {
          work[qp1jj] = 0.0F;
        }

        for (qs = q + 1; qs + 1 < 6; qs++) {
          b_eml_xaxpy(4 - q, e[qs], b_A, (q + 5 * qs) + 2, work, q + 2);
        }

        for (qs = q + 1; qs + 1 < 6; qs++) {
          c_eml_xaxpy(4 - q, -fDiv(e[qs], e[q + 1]), work, q + 2, b_A, (q + 5 * qs) +
                      2);
        }
      }

      for (qp1jj = q + 1; qp1jj + 1 < 6; qp1jj++) {
        V[qp1jj + 5 * q] = e[qp1jj];
      }
    }
  }

  m = 3;
  s[4] = b_A[24];
  e[3] = b_A[23];
  e[4] = 0.0F;
  for (qp1jj = 0; qp1jj < 5; qp1jj++) {
    U[20 + qp1jj] = 0.0F;
  }

  U[24] = 1.0F;
  for (q = 3; q > -1; q += -1) {
    iter = q + 5 * q;
    if (s[q] != 0.0F) {
      for (qs = q + 1; qs + 1 < 6; qs++) {
        kase = (q + 5 * qs) + 1;
        eml_xaxpy(5 - q, -(fDiv(eml_xdotc(5 - q, U, iter + 1, U, kase), U[iter])),
                  iter + 1, U, kase);
      }

      for (qp1jj = q; qp1jj + 1 < 6; qp1jj++) {
        U[qp1jj + 5 * q] = -U[qp1jj + 5 * q];
      }

      U[iter]++;
      for (qp1jj = 1; qp1jj <= q; qp1jj++) {
        U[(qp1jj + 5 * q) - 1] = 0.0F;
      }
    } else {
      for (qp1jj = 0; qp1jj < 5; qp1jj++) {
        U[qp1jj + 5 * q] = 0.0F;
      }

      U[iter] = 1.0F;
    }
  }

  for (q = 4; q > -1; q += -1) {
    if ((q + 1 <= 3) && (e[q] != 0.0F)) {
      kase = (q + 5 * q) + 2;
      for (qs = q + 1; qs + 1 < 6; qs++) {
        qp1jj = (q + 5 * qs) + 2;
        eml_xaxpy(4 - q, -(fDiv(eml_xdotc(4 - q, V, kase, V, qp1jj), V[kase - 1])),
                  kase, V, qp1jj);
      }
    }

    for (qp1jj = 0; qp1jj < 5; qp1jj++) {
      V[qp1jj + 5 * q] = 0.0F;
    }

    V[q + 5 * q] = 1.0F;
  }

  for (q = 0; q < 5; q++) {
    ztest0 = e[q];
    if (s[q] != 0.0F) {
      rt = fAbs(s[q]);
      ztest = fDiv(s[q], rt);
      s[q] = rt;
      if (q + 1 < 5) {
        ztest0 = fDiv(e[q], ztest);
      }

      eml_xscal(ztest, U, 1 + 5 * q);
    }

    if ((q + 1 < 5) && (ztest0 != 0.0F)) {
      rt = fAbs(ztest0);
      ztest = fDiv(rt, ztest0);
      ztest0 = rt;
      s[q + 1] *= ztest;
      eml_xscal(ztest, V, 1 + 5 * (q + 1));
    }

    e[q] = ztest0;
  }

  iter = 0;
  snorm = 0.0F;
  for (qp1jj = 0; qp1jj < 5; qp1jj++) {
    ztest0 = fAbs(s[qp1jj]);
    ztest = fAbs(e[qp1jj]);
    if ((ztest0 >= ztest) || fIsNan(ztest)) {
    } else {
      ztest0 = ztest;
    }

    if ((snorm >= ztest0) || fIsNan(ztest0)) {
    } else {
      snorm = ztest0;
    }
  }

  while ((m + 2 > 0) && (!(iter >= 75))) {
    qp1jj = m;
    do {
      exitg3 = 0;
      q = qp1jj + 1;
      if (qp1jj + 1 == 0) {
        exitg3 = 1;
      } else {
        ztest0 = fAbs(e[qp1jj]);
        if ((ztest0 <= 1.1920929E-7F * (fAbs(s[qp1jj]) + fAbs(s[qp1jj + 1]))) || (ztest0 <= 9.86076132E-32F) || ((iter > 20)
             && (ztest0 <= 1.1920929E-7F * snorm))) {
          e[qp1jj] = 0.0F;
          exitg3 = 1;
        } else {
          qp1jj--;
        }
      }
    } while (exitg3 == 0);

    if (qp1jj + 1 == m + 1) {
      kase = 4;
    } else {
      qs = m + 2;
      kase = m + 2;
      exitg2 = false;
      while ((!exitg2) && (kase >= qp1jj + 1)) {
        qs = kase;
        if (kase == qp1jj + 1) {
          exitg2 = true;
        } else {
          ztest0 = 0.0F;
          if (kase < m + 2) {
            ztest0 = fAbs(e[kase - 1]);
          }

          if (kase > qp1jj + 2) {
            ztest0 += fAbs(e[kase - 2]);
          }

          ztest = fAbs(s[kase - 1]);
          if ((ztest <= 1.1920929E-7F * ztest0) || (ztest <= 9.86076132E-32F)) {
            s[kase - 1] = 0.0F;
            exitg2 = true;
          } else {
            kase--;
          }
        }
      }

      if (qs == qp1jj + 1) {
        kase = 3;
      } else if (qs == m + 2) {
        kase = 1;
      } else {
        kase = 2;
        q = qs;
      }
    }

    switch (kase) {
     case 1:
      f = e[m];
      e[m] = 0.0F;
      for (qp1jj = m; qp1jj + 1 >= q + 1; qp1jj--) {
        ztest0 = s[qp1jj];
        eml_xrotg(&ztest0, &f, &ztest, &rt);
        s[qp1jj] = ztest0;
        if (qp1jj + 1 > q + 1) {
          f = -rt * e[qp1jj - 1];
          e[qp1jj - 1] *= ztest;
        }

        eml_xrot(V, 1 + 5 * qp1jj, 1 + 5 * (m + 1), ztest, rt);
      }
      break;

     case 2:
      f = e[q - 1];
      e[q - 1] = 0.0F;
      for (qp1jj = q; qp1jj + 1 <= m + 2; qp1jj++) {
        eml_xrotg(&s[qp1jj], &f, &ztest, &rt);
        f = -rt * e[qp1jj];
        e[qp1jj] *= ztest;
        eml_xrot(U, 1 + 5 * qp1jj, 1 + 5 * (q - 1), ztest, rt);
      }
      break;

     case 3:
      varargin_1[0] = fAbs(s[m + 1]);
      varargin_1[1] = fAbs(s[m]);
      varargin_1[2] = fAbs(e[m]);
      varargin_1[3] = fAbs(s[q]);
      varargin_1[4] = fAbs(e[q]);
      kase = 1;
      mtmp = varargin_1[0];
      if (fIsNan(varargin_1[0])) {
        qp1jj = 2;
        exitg1 = false;
        while ((!exitg1) && (qp1jj < 6)) {
          kase = qp1jj;
          if (!fIsNan(varargin_1[qp1jj - 1])) {
            mtmp = varargin_1[qp1jj - 1];
            exitg1 = true;
          } else {
            qp1jj++;
          }
        }
      }

      if (kase < 5) {
        while (kase + 1 < 6) {
          if (varargin_1[kase] > mtmp) {
            mtmp = varargin_1[kase];
          }

          kase++;
        }
      }

      mtmp_inv = fDiv(1.0F,mtmp);
      
      f = s[m + 1] * mtmp_inv;
      ztest0 = s[m] * mtmp_inv;
      ztest = e[m] * mtmp_inv;
      sqds = s[q] * mtmp_inv;
      rt = ((ztest0 + f) * (ztest0 - f) + ztest * ztest) * 0.5F;
      ztest0 = f * ztest;
      ztest0 *= ztest0;
      if ((rt != 0.0F) || (ztest0 != 0.0F)) {
        ztest = fSqrt(rt * rt + ztest0);
        if (rt < 0.0F) {
          ztest = -ztest;
        }

        ztest = fDiv(ztest0, (rt + ztest));
      } else {
        ztest = 0.0F;
      }

      f = (sqds + f) * (sqds - f) + ztest;
      ztest0 = sqds * fDiv(e[q], mtmp);
      for (qp1jj = q + 1; qp1jj <= m + 1; qp1jj++) {
        eml_xrotg(&f, &ztest0, &ztest, &rt);
        if (qp1jj > q + 1) {
          e[qp1jj - 2] = f;
        }

        f = ztest * s[qp1jj - 1] + rt * e[qp1jj - 1];
        e[qp1jj - 1] = ztest * e[qp1jj - 1] - rt * s[qp1jj - 1];
        ztest0 = rt * s[qp1jj];
        s[qp1jj] *= ztest;
        eml_xrot(V, 1 + 5 * (qp1jj - 1), 1 + 5 * qp1jj, ztest, rt);
        s[qp1jj - 1] = f;
        eml_xrotg(&s[qp1jj - 1], &ztest0, &ztest, &rt);
        f = ztest * e[qp1jj - 1] + rt * s[qp1jj];
        s[qp1jj] = -rt * e[qp1jj - 1] + ztest * s[qp1jj];
        ztest0 = rt * e[qp1jj];
        e[qp1jj] *= ztest;
        eml_xrot(U, 1 + 5 * (qp1jj - 1), 1 + 5 * qp1jj, ztest, rt);
      }

      e[m] = f;
      iter++;
      break;

     default:
      if (s[q] < 0.0F) {
        s[q] = -s[q];
        eml_xscal(-1.0F, V, 1 + 5 * q);
      }

      kase = q + 1;
      while ((q + 1 < 5) && (s[q] < s[kase])) {
        rt = s[q];
        s[q] = s[kase];
        s[kase] = rt;
        eml_xswap(V, 1 + 5 * q, 1 + 5 * (q + 1));
        eml_xswap(U, 1 + 5 * q, 1 + 5 * (q + 1));
        q = kase;
        kase++;
      }

      iter = 0;
      m--;
      break;
    }
  }

  for (qp1jj = 0; qp1jj < 5; qp1jj++) {
    S[qp1jj] = s[qp1jj];
  }
}
예제 #2
0
//
// Arguments    : const double A[476]
//                double U[476]
//                double S[17]
//                double V[289]
// Return Type  : void
//
static void eml_xgesvd(const double A[476], double U[476], double S[17], double
  V[289])
{
  double b_A[476];
  double s[17];
  double e[17];
  int kase;
  double work[28];
  int q;
  int iter;
  boolean_T apply_transform;
  double ztest0;
  int qp1jj;
  int qs;
  int m;
  double rt;
  double ztest;
  double snorm;
  int32_T exitg3;
  boolean_T exitg2;
  double f;
  double varargin_1[5];
  double mtmp;
  boolean_T exitg1;
  double sqds;
  memcpy(&b_A[0], &A[0], 476U * sizeof(double));
  for (kase = 0; kase < 17; kase++) {
    s[kase] = 0.0;
    e[kase] = 0.0;
  }

  memset(&work[0], 0, 28U * sizeof(double));
  memset(&U[0], 0, 476U * sizeof(double));
  memset(&V[0], 0, 289U * sizeof(double));
  for (q = 0; q < 17; q++) {
    iter = q + 28 * q;
    apply_transform = false;
    ztest0 = c_eml_xnrm2(28 - q, b_A, iter + 1);
    if (ztest0 > 0.0) {
      apply_transform = true;
      if (b_A[iter] < 0.0) {
        s[q] = -ztest0;
      } else {
        s[q] = ztest0;
      }

      if (fabs(s[q]) >= 1.0020841800044864E-292) {
        ztest0 = 1.0 / s[q];
        kase = (iter - q) + 28;
        for (qp1jj = iter; qp1jj + 1 <= kase; qp1jj++) {
          b_A[qp1jj] *= ztest0;
        }
      } else {
        kase = (iter - q) + 28;
        for (qp1jj = iter; qp1jj + 1 <= kase; qp1jj++) {
          b_A[qp1jj] /= s[q];
        }
      }

      b_A[iter]++;
      s[q] = -s[q];
    } else {
      s[q] = 0.0;
    }

    for (qs = q + 1; qs + 1 < 18; qs++) {
      kase = q + 28 * qs;
      if (apply_transform) {
        eml_xaxpy(28 - q, -(eml_xdotc(28 - q, b_A, iter + 1, b_A, kase + 1) /
                            b_A[q + 28 * q]), iter + 1, b_A, kase + 1);
      }

      e[qs] = b_A[kase];
    }

    for (qp1jj = q; qp1jj + 1 < 29; qp1jj++) {
      U[qp1jj + 28 * q] = b_A[qp1jj + 28 * q];
    }

    if (q + 1 <= 15) {
      ztest0 = d_eml_xnrm2(16 - q, e, q + 2);
      if (ztest0 == 0.0) {
        e[q] = 0.0;
      } else {
        if (e[q + 1] < 0.0) {
          e[q] = -ztest0;
        } else {
          e[q] = ztest0;
        }

        ztest0 = e[q];
        if (fabs(e[q]) >= 1.0020841800044864E-292) {
          ztest0 = 1.0 / e[q];
          for (qp1jj = q + 1; qp1jj + 1 < 18; qp1jj++) {
            e[qp1jj] *= ztest0;
          }
        } else {
          for (qp1jj = q + 1; qp1jj + 1 < 18; qp1jj++) {
            e[qp1jj] /= ztest0;
          }
        }

        e[q + 1]++;
        e[q] = -e[q];
        for (qp1jj = q + 1; qp1jj + 1 < 29; qp1jj++) {
          work[qp1jj] = 0.0;
        }

        for (qs = q + 1; qs + 1 < 18; qs++) {
          b_eml_xaxpy(27 - q, e[qs], b_A, (q + 28 * qs) + 2, work, q + 2);
        }

        for (qs = q + 1; qs + 1 < 18; qs++) {
          c_eml_xaxpy(27 - q, -e[qs] / e[q + 1], work, q + 2, b_A, (q + 28 * qs)
                      + 2);
        }
      }

      for (qp1jj = q + 1; qp1jj + 1 < 18; qp1jj++) {
        V[qp1jj + 17 * q] = e[qp1jj];
      }
    }
  }

  m = 15;
  e[15] = b_A[463];
  e[16] = 0.0;
  for (q = 16; q > -1; q += -1) {
    iter = q + 28 * q;
    if (s[q] != 0.0) {
      for (qs = q + 1; qs + 1 < 18; qs++) {
        kase = (q + 28 * qs) + 1;
        eml_xaxpy(28 - q, -(eml_xdotc(28 - q, U, iter + 1, U, kase) / U[iter]),
                  iter + 1, U, kase);
      }

      for (qp1jj = q; qp1jj + 1 < 29; qp1jj++) {
        U[qp1jj + 28 * q] = -U[qp1jj + 28 * q];
      }

      U[iter]++;
      for (qp1jj = 1; qp1jj <= q; qp1jj++) {
        U[(qp1jj + 28 * q) - 1] = 0.0;
      }
    } else {
      memset(&U[28 * q], 0, 28U * sizeof(double));
      U[iter] = 1.0;
    }
  }

  for (q = 16; q > -1; q += -1) {
    if ((q + 1 <= 15) && (e[q] != 0.0)) {
      kase = (q + 17 * q) + 2;
      for (qs = q + 1; qs + 1 < 18; qs++) {
        qp1jj = (q + 17 * qs) + 2;
        d_eml_xaxpy(16 - q, -(b_eml_xdotc(16 - q, V, kase, V, qp1jj) / V[kase -
                              1]), kase, V, qp1jj);
      }
    }

    memset(&V[17 * q], 0, 17U * sizeof(double));
    V[q + 17 * q] = 1.0;
  }

  for (q = 0; q < 17; q++) {
    ztest0 = e[q];
    if (s[q] != 0.0) {
      rt = fabs(s[q]);
      ztest = s[q] / rt;
      s[q] = rt;
      if (q + 1 < 17) {
        ztest0 = e[q] / ztest;
      }

      b_eml_xscal(ztest, U, 1 + 28 * q);
    }

    if ((q + 1 < 17) && (ztest0 != 0.0)) {
      rt = fabs(ztest0);
      ztest = rt / ztest0;
      ztest0 = rt;
      s[q + 1] *= ztest;
      c_eml_xscal(ztest, V, 1 + 17 * (q + 1));
    }

    e[q] = ztest0;
  }

  iter = 0;
  snorm = 0.0;
  for (qp1jj = 0; qp1jj < 17; qp1jj++) {
    ztest0 = fabs(s[qp1jj]);
    ztest = fabs(e[qp1jj]);
    if ((ztest0 >= ztest) || rtIsNaN(ztest)) {
    } else {
      ztest0 = ztest;
    }

    if ((snorm >= ztest0) || rtIsNaN(ztest0)) {
    } else {
      snorm = ztest0;
    }
  }

  while ((m + 2 > 0) && (!(iter >= 75))) {
    qp1jj = m;
    do {
      exitg3 = 0;
      q = qp1jj + 1;
      if (qp1jj + 1 == 0) {
        exitg3 = 1;
      } else {
        ztest0 = fabs(e[qp1jj]);
        if ((ztest0 <= 2.2204460492503131E-16 * (fabs(s[qp1jj]) + fabs(s[qp1jj +
               1]))) || (ztest0 <= 1.0020841800044864E-292) || ((iter > 20) &&
             (ztest0 <= 2.2204460492503131E-16 * snorm))) {
          e[qp1jj] = 0.0;
          exitg3 = 1;
        } else {
          qp1jj--;
        }
      }
    } while (exitg3 == 0);

    if (qp1jj + 1 == m + 1) {
      kase = 4;
    } else {
      qs = m + 2;
      kase = m + 2;
      exitg2 = false;
      while ((!exitg2) && (kase >= qp1jj + 1)) {
        qs = kase;
        if (kase == qp1jj + 1) {
          exitg2 = true;
        } else {
          ztest0 = 0.0;
          if (kase < m + 2) {
            ztest0 = fabs(e[kase - 1]);
          }

          if (kase > qp1jj + 2) {
            ztest0 += fabs(e[kase - 2]);
          }

          ztest = fabs(s[kase - 1]);
          if ((ztest <= 2.2204460492503131E-16 * ztest0) || (ztest <=
               1.0020841800044864E-292)) {
            s[kase - 1] = 0.0;
            exitg2 = true;
          } else {
            kase--;
          }
        }
      }

      if (qs == qp1jj + 1) {
        kase = 3;
      } else if (qs == m + 2) {
        kase = 1;
      } else {
        kase = 2;
        q = qs;
      }
    }

    switch (kase) {
     case 1:
      f = e[m];
      e[m] = 0.0;
      for (qp1jj = m; qp1jj + 1 >= q + 1; qp1jj--) {
        ztest0 = s[qp1jj];
        eml_xrotg(&ztest0, &f, &ztest, &rt);
        s[qp1jj] = ztest0;
        if (qp1jj + 1 > q + 1) {
          f = -rt * e[qp1jj - 1];
          e[qp1jj - 1] *= ztest;
        }

        eml_xrot(V, 1 + 17 * qp1jj, 1 + 17 * (m + 1), ztest, rt);
      }
      break;

     case 2:
      f = e[q - 1];
      e[q - 1] = 0.0;
      for (qp1jj = q; qp1jj + 1 <= m + 2; qp1jj++) {
        eml_xrotg(&s[qp1jj], &f, &ztest, &rt);
        f = -rt * e[qp1jj];
        e[qp1jj] *= ztest;
        b_eml_xrot(U, 1 + 28 * qp1jj, 1 + 28 * (q - 1), ztest, rt);
      }
      break;

     case 3:
      varargin_1[0] = fabs(s[m + 1]);
      varargin_1[1] = fabs(s[m]);
      varargin_1[2] = fabs(e[m]);
      varargin_1[3] = fabs(s[q]);
      varargin_1[4] = fabs(e[q]);
      kase = 1;
      mtmp = varargin_1[0];
      if (rtIsNaN(varargin_1[0])) {
        qp1jj = 2;
        exitg1 = false;
        while ((!exitg1) && (qp1jj < 6)) {
          kase = qp1jj;
          if (!rtIsNaN(varargin_1[qp1jj - 1])) {
            mtmp = varargin_1[qp1jj - 1];
            exitg1 = true;
          } else {
            qp1jj++;
          }
        }
      }

      if (kase < 5) {
        while (kase + 1 < 6) {
          if (varargin_1[kase] > mtmp) {
            mtmp = varargin_1[kase];
          }

          kase++;
        }
      }

      f = s[m + 1] / mtmp;
      ztest0 = s[m] / mtmp;
      ztest = e[m] / mtmp;
      sqds = s[q] / mtmp;
      rt = ((ztest0 + f) * (ztest0 - f) + ztest * ztest) / 2.0;
      ztest0 = f * ztest;
      ztest0 *= ztest0;
      if ((rt != 0.0) || (ztest0 != 0.0)) {
        ztest = sqrt(rt * rt + ztest0);
        if (rt < 0.0) {
          ztest = -ztest;
        }

        ztest = ztest0 / (rt + ztest);
      } else {
        ztest = 0.0;
      }

      f = (sqds + f) * (sqds - f) + ztest;
      ztest0 = sqds * (e[q] / mtmp);
      for (qp1jj = q + 1; qp1jj <= m + 1; qp1jj++) {
        eml_xrotg(&f, &ztest0, &ztest, &rt);
        if (qp1jj > q + 1) {
          e[qp1jj - 2] = f;
        }

        f = ztest * s[qp1jj - 1] + rt * e[qp1jj - 1];
        e[qp1jj - 1] = ztest * e[qp1jj - 1] - rt * s[qp1jj - 1];
        ztest0 = rt * s[qp1jj];
        s[qp1jj] *= ztest;
        eml_xrot(V, 1 + 17 * (qp1jj - 1), 1 + 17 * qp1jj, ztest, rt);
        s[qp1jj - 1] = f;
        eml_xrotg(&s[qp1jj - 1], &ztest0, &ztest, &rt);
        f = ztest * e[qp1jj - 1] + rt * s[qp1jj];
        s[qp1jj] = -rt * e[qp1jj - 1] + ztest * s[qp1jj];
        ztest0 = rt * e[qp1jj];
        e[qp1jj] *= ztest;
        b_eml_xrot(U, 1 + 28 * (qp1jj - 1), 1 + 28 * qp1jj, ztest, rt);
      }

      e[m] = f;
      iter++;
      break;

     default:
      if (s[q] < 0.0) {
        s[q] = -s[q];
        c_eml_xscal(-1.0, V, 1 + 17 * q);
      }

      kase = q + 1;
      while ((q + 1 < 17) && (s[q] < s[kase])) {
        rt = s[q];
        s[q] = s[kase];
        s[kase] = rt;
        b_eml_xswap(V, 1 + 17 * q, 1 + 17 * (q + 1));
        c_eml_xswap(U, 1 + 28 * q, 1 + 28 * (q + 1));
        q = kase;
        kase++;
      }

      iter = 0;
      m--;
      break;
    }
  }

  memcpy(&S[0], &s[0], 17U * sizeof(double));
}
예제 #3
0
static void eml_xgesvd(const real32_T A[9], real32_T U[9], real32_T S[3],
  real32_T V[9])
{
  real32_T b_A[9];
  int32_T i;
  real32_T s[3];
  real32_T e[3];
  real32_T work[3];
  real32_T Vf[9];
  int32_T q;
  int32_T qs;
  real32_T ztest0;
  int32_T ii;
  real32_T ztest;
  int32_T m;
  real32_T rt;
  int32_T iter;
  real32_T tiny;
  real32_T snorm;
  int32_T exitg3;
  boolean_T exitg2;
  real32_T sn;
  real32_T varargin_1[5];
  boolean_T exitg1;
  real32_T sqds;
  real32_T b;
  for (i = 0; i < 9; i++) {
    b_A[i] = A[i];
  }

  for (i = 0; i < 3; i++) {
    s[i] = 0.0F;
    e[i] = 0.0F;
    work[i] = 0.0F;
  }

  for (i = 0; i < 9; i++) {
    U[i] = 0.0F;
    Vf[i] = 0.0F;
  }

  for (q = 0; q < 2; q++) {
    qs = q + 3 * q;
    ztest0 = eml_xnrm2(3 - q, b_A, qs + 1);
    if (ztest0 > 0.0F) {
      if (b_A[qs] < 0.0F) {
        s[q] = -ztest0;
      } else {
        s[q] = ztest0;
      }

      eml_xscal(3 - q, b_eml_div(1.0, s[q]), b_A, qs + 1);
      b_A[qs]++;
      s[q] = -s[q];
    } else {
      s[q] = 0.0F;
    }

    for (ii = q + 1; ii + 1 < 4; ii++) {
      i = q + 3 * ii;
      if (s[q] != 0.0F) {
        ztest0 = -eml_div(eml_xdotc(3 - q, b_A, qs + 1, b_A, i + 1), b_A[q + 3 *
                          q]);
        eml_xaxpy(3 - q, ztest0, qs + 1, b_A, i + 1);
      }

      e[ii] = b_A[i];
    }

    for (ii = q; ii + 1 < 4; ii++) {
      U[ii + 3 * q] = b_A[ii + 3 * q];
    }

    if (q + 1 <= 1) {
      ztest0 = b_eml_xnrm2(2, e, 2);
      if (ztest0 == 0.0F) {
        e[0] = 0.0F;
      } else {
        if (e[1] < 0.0F) {
          ztest = -ztest0;
        } else {
          ztest = ztest0;
        }

        if (e[1] < 0.0F) {
          e[0] = -ztest0;
        } else {
          e[0] = ztest0;
        }

        b_eml_xscal(2, b_eml_div(1.0, ztest), e, 2);
        e[1]++;
      }

      e[0] = -e[0];
      if (e[0] != 0.0F) {
        for (ii = 2; ii < 4; ii++) {
          work[ii - 1] = 0.0F;
        }

        for (ii = 1; ii + 1 < 4; ii++) {
          b_eml_xaxpy(2, e[ii], b_A, 2 + 3 * ii, work, 2);
        }

        for (ii = 1; ii + 1 < 4; ii++) {
          c_eml_xaxpy(2, eml_div(-e[ii], e[1]), work, 2, b_A, 2 + 3 * ii);
        }
      }

      for (ii = 1; ii + 1 < 4; ii++) {
        Vf[ii] = e[ii];
      }
    }
  }

  m = 1;
  s[2] = b_A[8];
  e[1] = b_A[7];
  e[2] = 0.0F;
  for (ii = 0; ii < 3; ii++) {
    U[6 + ii] = 0.0F;
  }

  U[8] = 1.0F;
  for (q = 1; q > -1; q += -1) {
    qs = q + 3 * q;
    if (s[q] != 0.0F) {
      for (ii = q + 1; ii + 1 < 4; ii++) {
        i = (q + 3 * ii) + 1;
        ztest0 = -eml_div(eml_xdotc(3 - q, U, qs + 1, U, i), U[qs]);
        eml_xaxpy(3 - q, ztest0, qs + 1, U, i);
      }

      for (ii = q; ii + 1 < 4; ii++) {
        U[ii + 3 * q] = -U[ii + 3 * q];
      }

      U[qs]++;
      ii = 1;
      while (ii <= q) {
        U[3] = 0.0F;
        ii = 2;
      }
    } else {
      for (ii = 0; ii < 3; ii++) {
        U[ii + 3 * q] = 0.0F;
      }

      U[qs] = 1.0F;
    }
  }

  for (q = 2; q > -1; q += -1) {
    if ((q + 1 <= 1) && (e[0] != 0.0F)) {
      for (ii = 2; ii < 4; ii++) {
        i = 2 + 3 * (ii - 1);
        ztest0 = -eml_div(eml_xdotc(2, Vf, 2, Vf, i), Vf[1]);
        eml_xaxpy(2, ztest0, 2, Vf, i);
      }
    }

    for (ii = 0; ii < 3; ii++) {
      Vf[ii + 3 * q] = 0.0F;
    }

    Vf[q + 3 * q] = 1.0F;
  }

  for (q = 0; q < 3; q++) {
    ztest0 = e[q];
    if (s[q] != 0.0F) {
      rt = (real32_T)fabs(s[q]);
      ztest = eml_div(s[q], rt);
      s[q] = rt;
      if (q + 1 < 3) {
        ztest0 = eml_div(e[q], ztest);
      }

      eml_xscal(3, ztest, U, 3 * q + 1);
    }

    if ((q + 1 < 3) && (ztest0 != 0.0F)) {
      rt = (real32_T)fabs(ztest0);
      ztest = eml_div(rt, ztest0);
      ztest0 = rt;
      s[q + 1] *= ztest;
      eml_xscal(3, ztest, Vf, 3 * (q + 1) + 1);
    }

    e[q] = ztest0;
  }

  iter = 0;
  tiny = eml_div(1.17549435E-38F, 1.1920929E-7F);
  snorm = 0.0F;
  for (ii = 0; ii < 3; ii++) {
    ztest0 = (real32_T)fabs(s[ii]);
    ztest = (real32_T)fabs(e[ii]);
    if ((ztest0 >= ztest) || rtIsNaNF(ztest)) {
      ztest = ztest0;
    }

    if ((snorm >= ztest) || rtIsNaNF(ztest)) {
    } else {
      snorm = ztest;
    }
  }

  while ((m + 2 > 0) && (!(iter >= 75))) {
    ii = m;
    do {
      exitg3 = 0;
      q = ii + 1;
      if (ii + 1 == 0) {
        exitg3 = 1;
      } else {
        ztest0 = (real32_T)fabs(e[ii]);
        if ((ztest0 <= 1.1920929E-7F * ((real32_T)fabs(s[ii]) + (real32_T)fabs
              (s[ii + 1]))) || (ztest0 <= tiny) || ((iter > 20) && (ztest0 <=
              1.1920929E-7F * snorm))) {
          e[ii] = 0.0F;
          exitg3 = 1;
        } else {
          ii--;
        }
      }
    } while (exitg3 == 0);

    if (ii + 1 == m + 1) {
      i = 4;
    } else {
      qs = m + 2;
      i = m + 2;
      exitg2 = FALSE;
      while ((exitg2 == FALSE) && (i >= ii + 1)) {
        qs = i;
        if (i == ii + 1) {
          exitg2 = TRUE;
        } else {
          ztest0 = 0.0F;
          if (i < m + 2) {
            ztest0 = (real32_T)fabs(e[i - 1]);
          }

          if (i > ii + 2) {
            ztest0 += (real32_T)fabs(e[i - 2]);
          }

          ztest = (real32_T)fabs(s[i - 1]);
          if ((ztest <= 1.1920929E-7F * ztest0) || (ztest <= tiny)) {
            s[i - 1] = 0.0F;
            exitg2 = TRUE;
          } else {
            i--;
          }
        }
      }

      if (qs == ii + 1) {
        i = 3;
      } else if (qs == m + 2) {
        i = 1;
      } else {
        i = 2;
        q = qs;
      }
    }

    switch (i) {
     case 1:
      ztest = e[m];
      e[m] = 0.0F;
      for (ii = m; ii + 1 >= q + 1; ii--) {
        ztest0 = s[ii];
        eml_xrotg(&ztest0, &ztest, &rt, &sn);
        s[ii] = ztest0;
        if (ii + 1 > q + 1) {
          ztest = -sn * e[0];
          e[0] *= rt;
        }

        eml_xrot(Vf, 3 * ii + 1, 3 * (m + 1) + 1, rt, sn);
      }
      break;

     case 2:
      ztest = e[q - 1];
      e[q - 1] = 0.0F;
      for (ii = q; ii + 1 <= m + 2; ii++) {
        eml_xrotg(&s[ii], &ztest, &rt, &sn);
        ztest = -sn * e[ii];
        e[ii] *= rt;
        eml_xrot(U, 3 * ii + 1, 3 * (q - 1) + 1, rt, sn);
      }
      break;

     case 3:
      varargin_1[0] = (real32_T)fabs(s[m + 1]);
      varargin_1[1] = (real32_T)fabs(s[m]);
      varargin_1[2] = (real32_T)fabs(e[m]);
      varargin_1[3] = (real32_T)fabs(s[q]);
      varargin_1[4] = (real32_T)fabs(e[q]);
      i = 1;
      sn = varargin_1[0];
      if (rtIsNaNF(varargin_1[0])) {
        ii = 2;
        exitg1 = FALSE;
        while ((exitg1 == FALSE) && (ii < 6)) {
          i = ii;
          if (!rtIsNaNF(varargin_1[ii - 1])) {
            sn = varargin_1[ii - 1];
            exitg1 = TRUE;
          } else {
            ii++;
          }
        }
      }

      if (i < 5) {
        while (i + 1 < 6) {
          if (varargin_1[i] > sn) {
            sn = varargin_1[i];
          }

          i++;
        }
      }

      rt = eml_div(s[m + 1], sn);
      ztest0 = eml_div(s[m], sn);
      ztest = eml_div(e[m], sn);
      sqds = eml_div(s[q], sn);
      b = c_eml_div((ztest0 + rt) * (ztest0 - rt) + ztest * ztest, 2.0);
      ztest0 = rt * ztest;
      ztest0 *= ztest0;
      ztest = 0.0F;
      if ((b != 0.0F) || (ztest0 != 0.0F)) {
        ztest = (real32_T)sqrt(b * b + ztest0);
        if (b < 0.0F) {
          ztest = -ztest;
        }

        ztest = eml_div(ztest0, b + ztest);
      }

      ztest += (sqds + rt) * (sqds - rt);
      ztest0 = sqds * eml_div(e[q], sn);
      for (ii = q + 1; ii <= m + 1; ii++) {
        eml_xrotg(&ztest, &ztest0, &rt, &sn);
        if (ii > q + 1) {
          e[0] = ztest;
        }

        ztest = rt * s[ii - 1];
        ztest0 = sn * e[ii - 1];
        e[ii - 1] = rt * e[ii - 1] - sn * s[ii - 1];
        b = s[ii];
        s[ii] *= rt;
        eml_xrot(Vf, 3 * (ii - 1) + 1, 3 * ii + 1, rt, sn);
        s[ii - 1] = ztest + ztest0;
        ztest0 = sn * b;
        eml_xrotg(&s[ii - 1], &ztest0, &rt, &sn);
        ztest = rt * e[ii - 1] + sn * s[ii];
        s[ii] = -sn * e[ii - 1] + rt * s[ii];
        ztest0 = sn * e[ii];
        e[ii] *= rt;
        eml_xrot(U, 3 * (ii - 1) + 1, 3 * ii + 1, rt, sn);
      }

      e[m] = ztest;
      iter++;
      break;

     default:
      if (s[q] < 0.0F) {
        s[q] = -s[q];
        eml_xscal(3, -1.0F, Vf, 3 * q + 1);
      }

      i = q + 1;
      while ((q + 1 < 3) && (s[q] < s[i])) {
        rt = s[q];
        s[q] = s[i];
        s[i] = rt;
        eml_xswap(Vf, 3 * q + 1, 3 * (q + 1) + 1);
        eml_xswap(U, 3 * q + 1, 3 * (q + 1) + 1);
        q = i;
        i++;
      }

      iter = 0;
      m--;
      break;
    }
  }

  for (ii = 0; ii < 3; ii++) {
    S[ii] = s[ii];
    for (i = 0; i < 3; i++) {
      V[i + 3 * ii] = Vf[i + 3 * ii];
    }
  }
}