Example #1
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));
}
Example #2
0
static void b_eml_xgesvd(const real32_T A[16], real32_T U[16], real32_T S[4],
  real32_T V[16])
{
  real32_T b_A[16];
  real32_T s[4];
  real32_T e[4];
  real32_T work[4];
  int32_T i;
  real32_T Vf[16];
  int32_T q;
  int32_T qs;
  real32_T ztest0;
  int32_T ii;
  int32_T m;
  real32_T rt;
  real32_T ztest;
  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;
  memcpy(&b_A[0], &A[0], sizeof(real32_T) << 4);
  for (i = 0; i < 4; i++) {
    s[i] = 0.0F;
    e[i] = 0.0F;
    work[i] = 0.0F;
  }

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

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

      c_eml_xscal(4 - 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 < 5; ii++) {
      i = q + (ii << 2);
      if (s[q] != 0.0F) {
        ztest0 = -eml_div(b_eml_xdotc(4 - q, b_A, qs + 1, b_A, i + 1), b_A[q +
                          (q << 2)]);
        d_eml_xaxpy(4 - q, ztest0, qs + 1, b_A, i + 1);
      }

      e[ii] = b_A[i];
    }

    for (ii = q; ii + 1 < 5; ii++) {
      U[ii + (q << 2)] = b_A[ii + (q << 2)];
    }

    if (q + 1 <= 2) {
      ztest0 = d_eml_xnrm2(3 - 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 = b_eml_div(1.0, e[q]);
        d_eml_xscal(3 - q, ztest0, e, q + 2);
        e[q + 1]++;
      }

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

        for (ii = q + 1; ii + 1 < 5; ii++) {
          e_eml_xaxpy(3 - q, e[ii], b_A, (q + (ii << 2)) + 2, work, q + 2);
        }

        for (ii = q + 1; ii + 1 < 5; ii++) {
          f_eml_xaxpy(3 - q, eml_div(-e[ii], e[q + 1]), work, q + 2, b_A, (q +
            (ii << 2)) + 2);
        }
      }

      for (ii = q + 1; ii + 1 < 5; ii++) {
        Vf[ii + (q << 2)] = e[ii];
      }
    }
  }

  m = 2;
  s[3] = b_A[15];
  e[2] = b_A[14];
  e[3] = 0.0F;
  for (ii = 0; ii < 4; ii++) {
    U[12 + ii] = 0.0F;
  }

  U[15] = 1.0F;
  for (q = 2; q > -1; q += -1) {
    qs = q + (q << 2);
    if (s[q] != 0.0F) {
      for (ii = q + 1; ii + 1 < 5; ii++) {
        i = (q + (ii << 2)) + 1;
        ztest0 = -eml_div(b_eml_xdotc(4 - q, U, qs + 1, U, i), U[qs]);
        d_eml_xaxpy(4 - q, ztest0, qs + 1, U, i);
      }

      for (ii = q; ii + 1 < 5; ii++) {
        U[ii + (q << 2)] = -U[ii + (q << 2)];
      }

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

      U[qs] = 1.0F;
    }
  }

  for (q = 3; q > -1; q += -1) {
    if ((q + 1 <= 2) && (e[q] != 0.0F)) {
      i = (q + (q << 2)) + 2;
      for (ii = q + 1; ii + 1 < 5; ii++) {
        qs = (q + (ii << 2)) + 2;
        ztest0 = -eml_div(b_eml_xdotc(3 - q, Vf, i, Vf, qs), Vf[i - 1]);
        d_eml_xaxpy(3 - q, ztest0, i, Vf, qs);
      }
    }

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

    Vf[q + (q << 2)] = 1.0F;
  }

  for (q = 0; q < 4; 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 < 4) {
        ztest0 = eml_div(e[q], ztest);
      }

      c_eml_xscal(4, ztest, U, (q << 2) + 1);
    }

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

    e[q] = ztest0;
  }

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

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

  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 (qs = m; qs + 1 >= q + 1; qs--) {
        ztest0 = s[qs];
        eml_xrotg(&ztest0, &ztest, &rt, &sn);
        s[qs] = ztest0;
        if (qs + 1 > q + 1) {
          ztest = -sn * e[qs - 1];
          e[qs - 1] *= rt;
        }

        b_eml_xrot(Vf, (qs << 2) + 1, ((m + 1) << 2) + 1, rt, sn);
      }
      break;

     case 2:
      ztest = e[q - 1];
      e[q - 1] = 0.0F;
      for (qs = q; qs + 1 <= m + 2; qs++) {
        eml_xrotg(&s[qs], &ztest, &rt, &sn);
        ztest = -sn * e[qs];
        e[qs] *= rt;
        b_eml_xrot(U, (qs << 2) + 1, ((q - 1) << 2) + 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])) {
        qs = 2;
        exitg1 = FALSE;
        while ((exitg1 == FALSE) && (qs < 6)) {
          i = qs;
          if (!rtIsNaNF(varargin_1[qs - 1])) {
            sn = varargin_1[qs - 1];
            exitg1 = TRUE;
          } else {
            qs++;
          }
        }
      }

      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 (qs = q + 1; qs <= m + 1; qs++) {
        eml_xrotg(&ztest, &ztest0, &rt, &sn);
        if (qs > q + 1) {
          e[qs - 2] = ztest;
        }

        ztest0 = rt * s[qs - 1];
        ztest = sn * e[qs - 1];
        e[qs - 1] = rt * e[qs - 1] - sn * s[qs - 1];
        b = s[qs];
        s[qs] *= rt;
        b_eml_xrot(Vf, ((qs - 1) << 2) + 1, (qs << 2) + 1, rt, sn);
        s[qs - 1] = ztest0 + ztest;
        ztest0 = sn * b;
        eml_xrotg(&s[qs - 1], &ztest0, &rt, &sn);
        ztest = rt * e[qs - 1] + sn * s[qs];
        s[qs] = -sn * e[qs - 1] + rt * s[qs];
        ztest0 = sn * e[qs];
        e[qs] *= rt;
        b_eml_xrot(U, ((qs - 1) << 2) + 1, (qs << 2) + 1, rt, sn);
      }

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

     default:
      if (s[q] < 0.0F) {
        s[q] = -s[q];
        c_eml_xscal(4, -1.0F, Vf, (q << 2) + 1);
      }

      i = q + 1;
      while ((q + 1 < 4) && (s[q] < s[i])) {
        rt = s[q];
        s[q] = s[i];
        s[i] = rt;
        b_eml_xswap(Vf, (q << 2) + 1, ((q + 1) << 2) + 1);
        b_eml_xswap(U, (q << 2) + 1, ((q + 1) << 2) + 1);
        q = i;
        i++;
      }

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

  for (qs = 0; qs < 4; qs++) {
    S[qs] = s[qs];
    for (i = 0; i < 4; i++) {
      V[i + (qs << 2)] = Vf[i + (qs << 2)];
    }
  }
}
Example #3
0
static void eml_xgesvd(const emxArray_real_T *A, real_T S_data[10], int32_T
  S_size[1])
{
  emxArray_real_T *b_A;
  int32_T mm;
  int32_T kase;
  int32_T n;
  int32_T minnp;
  real_T s_data[10];
  real_T e[10];
  emxArray_real_T *work;
  int32_T nrt;
  int32_T nct;
  int32_T q;
  int32_T qp1;
  int32_T iter;
  real_T ztest0;
  int32_T qs;
  int32_T mm1;
  int32_T m;
  real_T ztest;
  real_T tiny;
  real_T snorm;
  boolean_T exitg3;
  boolean_T exitg2;
  real_T sn;
  real_T sm;
  real_T varargin_1[5];
  boolean_T exitg1;
  real_T sqds;
  real_T b;
  emxInit_real_T(&b_A, 2);
  mm = b_A->size[0] * b_A->size[1];
  b_A->size[0] = A->size[0];
  b_A->size[1] = 10;
  emxEnsureCapacity((emxArray__common *)b_A, mm, (int32_T)sizeof(real_T));
  kase = A->size[0] * A->size[1] - 1;
  for (mm = 0; mm <= kase; mm++) {
    b_A->data[mm] = A->data[mm];
  }

  n = A->size[0];
  if (n <= 10) {
    minnp = n;
  } else {
    minnp = 10;
  }

  kase = n + 1;
  if (kase <= 10) {
  } else {
    kase = 10;
  }

  kase--;
  for (mm = 0; mm <= kase; mm++) {
    s_data[mm] = 0.0;
  }

  memset(&e[0], 0, 10U * sizeof(real_T));
  b_emxInit_real_T(&work, 1);
  mm = work->size[0];
  work->size[0] = n;
  emxEnsureCapacity((emxArray__common *)work, mm, (int32_T)sizeof(real_T));
  kase = n - 1;
  for (mm = 0; mm <= kase; mm++) {
    work->data[mm] = 0.0;
  }

  if (A->size[0] == 0) {
  } else {
    if (8 <= n) {
      nrt = 8;
    } else {
      nrt = n;
    }

    if (n < 1) {
      kase = 1;
    } else {
      kase = n;
    }

    nct = kase - 1;
    if (nct <= 10) {
    } else {
      nct = 10;
    }

    if (nct >= nrt) {
      mm = nct;
    } else {
      mm = nrt;
    }

    for (q = 0; q + 1 <= mm; q++) {
      qp1 = q + 2;
      kase = q + n * q;
      iter = n - q;
      if (q + 1 <= nct) {
        ztest0 = c_eml_xnrm2(iter, b_A, kase + 1);
        if (ztest0 > 0.0) {
          if (b_A->data[kase] < 0.0) {
            ztest0 = -ztest0;
          }

          s_data[q] = ztest0;
          eml_xscal(iter, eml_div(1.0, s_data[q]), b_A, kase + 1);
          b_A->data[kase]++;
          s_data[q] = -s_data[q];
        } else {
          s_data[q] = 0.0;
        }
      }

      for (qs = qp1; qs < 11; qs++) {
        mm1 = q + n * (qs - 1);
        if ((q + 1 <= nct) && (s_data[q] != 0.0)) {
          ztest0 = eml_xdotc(iter, b_A, kase + 1, b_A, mm1 + 1);
          ztest0 = -eml_div(ztest0, b_A->data[q + b_A->size[0] * q]);
          eml_xaxpy(iter, ztest0, kase + 1, b_A, mm1 + 1);
        }

        e[qs - 1] = b_A->data[mm1];
      }

      if (q + 1 <= nrt) {
        ztest0 = d_eml_xnrm2(9 - q, e, qp1);
        if (ztest0 == 0.0) {
          e[q] = 0.0;
        } else {
          if (e[qp1 - 1] < 0.0) {
            ztest0 = -ztest0;
          }

          e[q] = ztest0;
          b_eml_xscal(9 - q, eml_div(1.0, e[q]), e, qp1);
          e[qp1 - 1]++;
        }

        e[q] = -e[q];
        if ((qp1 <= n) && (e[q] != 0.0)) {
          for (kase = qp1; kase <= n; kase++) {
            work->data[kase - 1] = 0.0;
          }

          for (qs = qp1; qs < 11; qs++) {
            b_eml_xaxpy(iter - 1, e[qs - 1], b_A, qp1 + n * (qs - 1), work, qp1);
          }

          for (qs = qp1; qs < 11; qs++) {
            c_eml_xaxpy(iter - 1, eml_div(-e[qs - 1], e[qp1 - 1]), work, qp1,
                        b_A, qp1 + n * (qs - 1));
          }
        }
      }
    }

    m = n + 1;
    if (10 <= m) {
      m = 10;
    }

    if (nct < 10) {
      s_data[nct] = b_A->data[nct + b_A->size[0] * nct];
    }

    if (n < m) {
      s_data[m - 1] = 0.0;
    }

    if (nrt + 1 < m) {
      e[nrt] = b_A->data[nrt + b_A->size[0] * (m - 1)];
    }

    e[m - 1] = 0.0;
    for (q = 0; q + 1 <= m; q++) {
      if (s_data[q] != 0.0) {
        ztest = fabs(s_data[q]);
        ztest0 = eml_div(s_data[q], ztest);
        s_data[q] = ztest;
        if (q + 1 < m) {
          e[q] = eml_div(e[q], ztest0);
        }
      }

      if ((q + 1 < m) && (e[q] != 0.0)) {
        ztest = fabs(e[q]);
        ztest0 = eml_div(ztest, e[q]);
        e[q] = ztest;
        s_data[q + 1] *= ztest0;
      }
    }

    mm = m;
    iter = 0;
    tiny = eml_div(2.2250738585072014E-308, 2.2204460492503131E-16);
    snorm = 0.0;
    for (kase = 0; kase + 1 <= m; kase++) {
      ztest0 = fabs(s_data[kase]);
      ztest = fabs(e[kase]);
      if ((ztest0 >= ztest) || rtIsNaN(ztest)) {
        ztest = ztest0;
      }

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

    while ((m > 0) && (!(iter >= 75))) {
      q = m - 1;
      exitg3 = FALSE;
      while (!((exitg3 == 1U) || (q == 0))) {
        ztest0 = fabs(e[q - 1]);
        if ((ztest0 <= 2.2204460492503131E-16 * (fabs(s_data[q - 1]) + fabs
              (s_data[q]))) || (ztest0 <= tiny) || ((iter > 20) && (ztest0 <=
              2.2204460492503131E-16 * snorm))) {
          e[q - 1] = 0.0;
          exitg3 = TRUE;
        } else {
          q--;
        }
      }

      if (q == m - 1) {
        kase = 4;
      } else {
        qs = m;
        kase = m;
        exitg2 = FALSE;
        while ((exitg2 == 0U) && (kase >= q)) {
          qs = kase;
          if (kase == q) {
            exitg2 = TRUE;
          } else {
            ztest0 = 0.0;
            if (kase < m) {
              ztest0 = fabs(e[kase - 1]);
            }

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

            ztest = fabs(s_data[kase - 1]);
            if ((ztest <= 2.2204460492503131E-16 * ztest0) || (ztest <= tiny)) {
              s_data[kase - 1] = 0.0;
              exitg2 = TRUE;
            } else {
              kase--;
            }
          }
        }

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

      switch (kase) {
       case 1:
        ztest = e[m - 2];
        e[m - 2] = 0.0;
        for (qs = m - 1; qs >= q + 1; qs--) {
          ztest0 = s_data[qs - 1];
          eml_xrotg(&ztest0, &ztest, &sm, &sn);
          s_data[qs - 1] = ztest0;
          if (qs > q + 1) {
            kase = qs - 2;
            ztest = -sn * e[kase];
            e[kase] *= sm;
          }
        }
        break;

       case 2:
        kase = q - 1;
        ztest = e[kase];
        e[kase] = 0.0;
        while (q + 1 <= m) {
          eml_xrotg(&s_data[q], &ztest, &sm, &sn);
          ztest = -sn * e[q];
          e[q] *= sm;
          q++;
        }
        break;

       case 3:
        mm1 = m - 2;
        varargin_1[0] = fabs(s_data[m - 1]);
        varargin_1[1] = fabs(s_data[mm1]);
        varargin_1[2] = fabs(e[mm1]);
        varargin_1[3] = fabs(s_data[q]);
        varargin_1[4] = fabs(e[q]);
        kase = 1;
        sn = varargin_1[0];
        if (rtIsNaN(varargin_1[0])) {
          qs = 2;
          exitg1 = FALSE;
          while ((exitg1 == 0U) && (qs < 6)) {
            kase = qs;
            if (!rtIsNaN(varargin_1[qs - 1])) {
              sn = varargin_1[qs - 1];
              exitg1 = TRUE;
            } else {
              qs++;
            }
          }
        }

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

            kase++;
          }
        }

        sm = eml_div(s_data[m - 1], sn);
        ztest0 = eml_div(s_data[mm1], sn);
        ztest = eml_div(e[mm1], sn);
        sqds = eml_div(s_data[q], sn);
        b = eml_div((ztest0 + sm) * (ztest0 - sm) + ztest * ztest, 2.0);
        ztest0 = sm * ztest;
        ztest0 *= ztest0;
        ztest = 0.0;
        if ((b != 0.0) || (ztest0 != 0.0)) {
          ztest = sqrt(b * b + ztest0);
          if (b < 0.0) {
            ztest = -ztest;
          }

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

        ztest += (sqds + sm) * (sqds - sm);
        ztest0 = sqds * eml_div(e[q], sn);
        for (qs = q; qs + 1 <= mm1 + 1; qs++) {
          kase = qs + 1;
          eml_xrotg(&ztest, &ztest0, &sm, &sn);
          if (qs + 1 > q + 1) {
            e[qs - 1] = ztest;
          }

          ztest0 = sm * s_data[qs];
          ztest = sn * e[qs];
          e[qs] = sm * e[qs] - sn * s_data[qs];
          b = s_data[kase];
          s_data[kase] *= sm;
          s_data[qs] = ztest0 + ztest;
          ztest0 = sn * b;
          eml_xrotg(&s_data[qs], &ztest0, &sm, &sn);
          ztest = sm * e[qs] + sn * s_data[kase];
          s_data[kase] = -sn * e[qs] + sm * s_data[kase];
          ztest0 = sn * e[kase];
          e[kase] *= sm;
        }

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

       default:
        if (s_data[q] < 0.0) {
          s_data[q] = -s_data[q];
        }

        qp1 = q + 1;
        while ((q + 1 < mm) && (s_data[q] < s_data[qp1])) {
          ztest = s_data[q];
          s_data[q] = s_data[qp1];
          s_data[qp1] = ztest;
          q = qp1;
          qp1++;
        }

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

  emxFree_real_T(&work);
  emxFree_real_T(&b_A);
  S_size[0] = minnp;
  for (qs = 0; qs + 1 <= minnp; qs++) {
    S_data[qs] = s_data[qs];
  }
}
Example #4
0
/* Function Definitions */
static void b_eml_qrsolve(const emxArray_real_T *A, emxArray_real_T *B, double
  Y[3], double *rankR)
{
  emxArray_real_T *b_A;
  int m;
  int mn;
  int i23;
  int itemp;
  int b_m;
  int b_mn;
  double tau_data[3];
  signed char jpvt[3];
  double work[3];
  int i;
  int k;
  double vn1[3];
  double vn2[3];
  int iy;
  double smax;
  int i_i;
  int mmi;
  int ix;
  double temp2;
  int pvt;
  double atmp;
  int i_ip1;
  int lastv;
  int lastc;
  boolean_T exitg2;
  int32_T exitg1;
  double t;
  unsigned int b_i;
  emxInit_real_T(&b_A, 2);
  m = A->size[0] - 2;
  mn = (int)fmin(A->size[0], 3.0) - 1;
  i23 = b_A->size[0] * b_A->size[1];
  b_A->size[0] = A->size[0];
  b_A->size[1] = 3;
  emxEnsureCapacity((emxArray__common *)b_A, i23, (int)sizeof(double));
  itemp = A->size[0] * A->size[1];
  for (i23 = 0; i23 < itemp; i23++) {
    b_A->data[i23] = A->data[i23];
  }

  b_m = b_A->size[0];
  if (b_A->size[0] <= 3) {
    b_mn = b_A->size[0];
  } else {
    b_mn = 3;
  }

  for (i23 = 0; i23 < 3; i23++) {
    jpvt[i23] = (signed char)(1 + i23);
  }

  if (b_A->size[0] == 0) {
  } else {
    for (i = 0; i < 3; i++) {
      work[i] = 0.0;
    }

    k = 1;
    for (iy = 0; iy < 3; iy++) {
      smax = c_eml_xnrm2(b_m, b_A, k);
      vn2[iy] = smax;
      k += b_m;
      vn1[iy] = smax;
    }

    for (i = 0; i + 1 <= b_mn; i++) {
      i_i = i + i * b_m;
      mmi = (b_m - i) - 1;
      itemp = 1;
      if (3 - i > 1) {
        ix = i;
        smax = fabs(vn1[i]);
        for (k = 2; k <= 3 - i; k++) {
          ix++;
          temp2 = fabs(vn1[ix]);
          if (temp2 > smax) {
            itemp = k;
            smax = temp2;
          }
        }
      }

      pvt = (i + itemp) - 1;
      if (pvt + 1 != i + 1) {
        ix = b_m * pvt;
        iy = b_m * i;
        for (k = 1; k <= b_m; k++) {
          smax = b_A->data[ix];
          b_A->data[ix] = b_A->data[iy];
          b_A->data[iy] = smax;
          ix++;
          iy++;
        }

        itemp = jpvt[pvt];
        jpvt[pvt] = jpvt[i];
        jpvt[i] = (signed char)itemp;
        vn1[pvt] = vn1[i];
        vn2[pvt] = vn2[i];
      }

      if (i + 1 < b_m) {
        atmp = b_A->data[i_i];
        temp2 = 0.0;
        if (1 + mmi <= 0) {
        } else {
          smax = d_eml_xnrm2(mmi, b_A, i_i + 2);
          if (smax != 0.0) {
            smax = hypot(b_A->data[i_i], smax);
            if (b_A->data[i_i] >= 0.0) {
              smax = -smax;
            }

            if (fabs(smax) < 1.0020841800044864E-292) {
              itemp = 0;
              do {
                itemp++;
                b_eml_xscal(mmi, 9.9792015476736E+291, b_A, i_i + 2);
                smax *= 9.9792015476736E+291;
                atmp *= 9.9792015476736E+291;
              } while (!(fabs(smax) >= 1.0020841800044864E-292));

              smax = d_eml_xnrm2(mmi, b_A, i_i + 2);
              smax = hypot(atmp, smax);
              if (atmp >= 0.0) {
                smax = -smax;
              }

              temp2 = (smax - atmp) / smax;
              b_eml_xscal(mmi, 1.0 / (atmp - smax), b_A, i_i + 2);
              for (k = 1; k <= itemp; k++) {
                smax *= 1.0020841800044864E-292;
              }

              atmp = smax;
            } else {
              temp2 = (smax - b_A->data[i_i]) / smax;
              b_eml_xscal(mmi, 1.0 / (b_A->data[i_i] - smax), b_A, i_i + 2);
              atmp = smax;
            }
          }
        }

        tau_data[i] = temp2;
      } else {
        atmp = b_A->data[i_i];
        tau_data[i] = eml_matlab_zlarfg();
      }

      b_A->data[i_i] = atmp;
      if (i + 1 < 3) {
        atmp = b_A->data[i_i];
        b_A->data[i_i] = 1.0;
        i_ip1 = (i + (i + 1) * b_m) + 1;
        if (tau_data[i] != 0.0) {
          lastv = mmi;
          itemp = i_i + mmi;
          while ((lastv + 1 > 0) && (b_A->data[itemp] == 0.0)) {
            lastv--;
            itemp--;
          }

          lastc = 2 - i;
          exitg2 = false;
          while ((!exitg2) && (lastc > 0)) {
            itemp = i_ip1 + (lastc - 1) * b_m;
            k = itemp;
            do {
              exitg1 = 0;
              if (k <= itemp + lastv) {
                if (b_A->data[k - 1] != 0.0) {
                  exitg1 = 1;
                } else {
                  k++;
                }
              } else {
                lastc--;
                exitg1 = 2;
              }
            } while (exitg1 == 0);

            if (exitg1 == 1) {
              exitg2 = true;
            }
          }
        } else {
          lastv = -1;
          lastc = 0;
        }

        if (lastv + 1 > 0) {
          if (lastc == 0) {
          } else {
            for (iy = 1; iy <= lastc; iy++) {
              work[iy - 1] = 0.0;
            }

            iy = 0;
            i23 = i_ip1 + b_m * (lastc - 1);
            itemp = i_ip1;
            while ((b_m > 0) && (itemp <= i23)) {
              ix = i_i;
              smax = 0.0;
              pvt = itemp + lastv;
              for (k = itemp; k <= pvt; k++) {
                smax += b_A->data[k - 1] * b_A->data[ix];
                ix++;
              }

              work[iy] += smax;
              iy++;
              itemp += b_m;
            }
          }

          if (-tau_data[i] == 0.0) {
          } else {
            itemp = 0;
            for (iy = 1; iy <= lastc; iy++) {
              if (work[itemp] != 0.0) {
                smax = work[itemp] * -tau_data[i];
                ix = i_i;
                i23 = lastv + i_ip1;
                for (pvt = i_ip1; pvt <= i23; pvt++) {
                  b_A->data[pvt - 1] += b_A->data[ix] * smax;
                  ix++;
                }
              }

              itemp++;
              i_ip1 += b_m;
            }
          }
        }

        b_A->data[i_i] = atmp;
      }

      for (iy = i + 1; iy + 1 < 4; iy++) {
        itemp = (i + b_m * iy) + 1;
        if (vn1[iy] != 0.0) {
          smax = fabs(b_A->data[i + b_A->size[0] * iy]) / vn1[iy];
          smax = 1.0 - smax * smax;
          if (smax < 0.0) {
            smax = 0.0;
          }

          temp2 = vn1[iy] / vn2[iy];
          temp2 = smax * (temp2 * temp2);
          if (temp2 <= 1.4901161193847656E-8) {
            if (i + 1 < b_m) {
              smax = 0.0;
              if (mmi < 1) {
              } else if (mmi == 1) {
                smax = fabs(b_A->data[itemp]);
              } else {
                temp2 = 2.2250738585072014E-308;
                pvt = itemp + mmi;
                while (itemp + 1 <= pvt) {
                  atmp = fabs(b_A->data[itemp]);
                  if (atmp > temp2) {
                    t = temp2 / atmp;
                    smax = 1.0 + smax * t * t;
                    temp2 = atmp;
                  } else {
                    t = atmp / temp2;
                    smax += t * t;
                  }

                  itemp++;
                }

                smax = temp2 * sqrt(smax);
              }

              vn1[iy] = smax;
              vn2[iy] = vn1[iy];
            } else {
              vn1[iy] = 0.0;
              vn2[iy] = 0.0;
            }
          } else {
            vn1[iy] *= sqrt(smax);
          }
        }
      }
    }
  }

  *rankR = 0.0;
  if (mn + 1 > 0) {
    smax = fmax(A->size[0], 3.0) * fabs(b_A->data[0]) * 2.2204460492503131E-16;
    k = 0;
    while ((k <= mn) && (!(fabs(b_A->data[k + b_A->size[0] * k]) <= smax))) {
      (*rankR)++;
      k++;
    }
  }

  for (i = 0; i < 3; i++) {
    Y[i] = 0.0;
  }

  for (iy = 0; iy <= mn; iy++) {
    if (tau_data[iy] != 0.0) {
      smax = B->data[iy];
      i23 = m - iy;
      for (i = 0; i <= i23; i++) {
        b_i = ((unsigned int)iy + i) + 2U;
        smax += b_A->data[((int)b_i + b_A->size[0] * iy) - 1] * B->data[(int)b_i
          - 1];
      }

      smax *= tau_data[iy];
      if (smax != 0.0) {
        B->data[iy] -= smax;
        i23 = m - iy;
        for (i = 0; i <= i23; i++) {
          b_i = ((unsigned int)iy + i) + 2U;
          B->data[(int)b_i - 1] -= b_A->data[((int)b_i + b_A->size[0] * iy) - 1]
            * smax;
        }
      }
    }
  }

  for (i = 0; i <= mn; i++) {
    Y[jpvt[i] - 1] = B->data[i];
  }

  for (iy = 0; iy <= mn; iy++) {
    itemp = mn - iy;
    Y[jpvt[itemp] - 1] /= b_A->data[itemp + b_A->size[0] * itemp];
    for (i = 0; i < itemp; i++) {
      Y[jpvt[i] - 1] -= Y[jpvt[itemp] - 1] * b_A->data[i + b_A->size[0] * itemp];
    }
  }

  emxFree_real_T(&b_A);
}