Exemple #1
0
float d_cos_dist(int len, double *list1, double *list2){
	double nor1 = 0;
	double nor2 = 0;
	double dist = 0;
	nor1 = d_norm(len,list1);
	nor2 = d_norm(len,list2);
	dist = d_inner(len,list1,list2);
	dist = 1 - (dist / nor1 / nor2);
	return(dist);
}
Exemple #2
0
bool TestRectStdDev::process()
{
    NCVStatus ncvStat;
    bool rcode = false;

    Ncv32s _normWidth = (Ncv32s)this->width - this->rect.x - this->rect.width + 1;
    Ncv32s _normHeight = (Ncv32s)this->height - this->rect.y - this->rect.height + 1;
    if (_normWidth <= 0 || _normHeight <= 0)
    {
        return true;
    }
    Ncv32u normWidth = (Ncv32u)_normWidth;
    Ncv32u normHeight = (Ncv32u)_normHeight;
    NcvSize32u szNormRoi(normWidth, normHeight);

    Ncv32u widthII = this->width + 1;
    Ncv32u heightII = this->height + 1;
    Ncv32u widthSII = this->width + 1;
    Ncv32u heightSII = this->height + 1;

    NCVMatrixAlloc<Ncv8u> d_img(*this->allocatorGPU.get(), this->width, this->height);
    ncvAssertReturn(d_img.isMemAllocated(), false);
    NCVMatrixAlloc<Ncv8u> h_img(*this->allocatorCPU.get(), this->width, this->height);
    ncvAssertReturn(h_img.isMemAllocated(), false);

    NCVMatrixAlloc<Ncv32u> d_imgII(*this->allocatorGPU.get(), widthII, heightII);
    ncvAssertReturn(d_imgII.isMemAllocated(), false);
    NCVMatrixAlloc<Ncv32u> h_imgII(*this->allocatorCPU.get(), widthII, heightII);
    ncvAssertReturn(h_imgII.isMemAllocated(), false);

    NCVMatrixAlloc<Ncv64u> d_imgSII(*this->allocatorGPU.get(), widthSII, heightSII);
    ncvAssertReturn(d_imgSII.isMemAllocated(), false);
    NCVMatrixAlloc<Ncv64u> h_imgSII(*this->allocatorCPU.get(), widthSII, heightSII);
    ncvAssertReturn(h_imgSII.isMemAllocated(), false);

    NCVMatrixAlloc<Ncv32f> d_norm(*this->allocatorGPU.get(), normWidth, normHeight);
    ncvAssertReturn(d_norm.isMemAllocated(), false);
    NCVMatrixAlloc<Ncv32f> h_norm(*this->allocatorCPU.get(), normWidth, normHeight);
    ncvAssertReturn(h_norm.isMemAllocated(), false);
    NCVMatrixAlloc<Ncv32f> h_norm_d(*this->allocatorCPU.get(), normWidth, normHeight);
    ncvAssertReturn(h_norm_d.isMemAllocated(), false);

    Ncv32u bufSizeII, bufSizeSII;
    ncvStat = nppiStIntegralGetSize_8u32u(NcvSize32u(this->width, this->height), &bufSizeII, this->devProp);
    ncvAssertReturn(NPPST_SUCCESS == ncvStat, false);
    ncvStat = nppiStSqrIntegralGetSize_8u64u(NcvSize32u(this->width, this->height), &bufSizeSII, this->devProp);
    ncvAssertReturn(NPPST_SUCCESS == ncvStat, false);
    Ncv32u bufSize = bufSizeII > bufSizeSII ? bufSizeII : bufSizeSII;
    NCVVectorAlloc<Ncv8u> d_tmpBuf(*this->allocatorGPU.get(), bufSize);
    ncvAssertReturn(d_tmpBuf.isMemAllocated(), false);

    NCV_SET_SKIP_COND(this->allocatorGPU.get()->isCounting());
    NCV_SKIP_COND_BEGIN
    ncvAssertReturn(this->src.fill(h_img), false);

    ncvStat = h_img.copySolid(d_img, 0);
    ncvAssertReturn(ncvStat == NPPST_SUCCESS, false);

    ncvStat = nppiStIntegral_8u32u_C1R(d_img.ptr(), d_img.pitch(),
                                       d_imgII.ptr(), d_imgII.pitch(),
                                       NcvSize32u(this->width, this->height),
                                       d_tmpBuf.ptr(), bufSize, this->devProp);
    ncvAssertReturn(ncvStat == NPPST_SUCCESS, false);

    ncvStat = nppiStSqrIntegral_8u64u_C1R(d_img.ptr(), d_img.pitch(),
                                          d_imgSII.ptr(), d_imgSII.pitch(),
                                          NcvSize32u(this->width, this->height),
                                          d_tmpBuf.ptr(), bufSize, this->devProp);
    ncvAssertReturn(ncvStat == NPPST_SUCCESS, false);

    ncvStat = nppiStRectStdDev_32f_C1R(d_imgII.ptr(), d_imgII.pitch(),
                                       d_imgSII.ptr(), d_imgSII.pitch(),
                                       d_norm.ptr(), d_norm.pitch(),
                                       szNormRoi, this->rect,
                                       this->scaleFactor,
                                       this->bTextureCache);
    ncvAssertReturn(ncvStat == NPPST_SUCCESS, false);

    ncvStat = d_norm.copySolid(h_norm_d, 0);
    ncvAssertReturn(ncvStat == NPPST_SUCCESS, false);

    ncvStat = nppiStIntegral_8u32u_C1R_host(h_img.ptr(), h_img.pitch(),
                                          h_imgII.ptr(), h_imgII.pitch(),
                                          NcvSize32u(this->width, this->height));
    ncvAssertReturn(ncvStat == NPPST_SUCCESS, false);

    ncvStat = nppiStSqrIntegral_8u64u_C1R_host(h_img.ptr(), h_img.pitch(),
                                             h_imgSII.ptr(), h_imgSII.pitch(),
                                             NcvSize32u(this->width, this->height));
    ncvAssertReturn(ncvStat == NPPST_SUCCESS, false);

    ncvStat = nppiStRectStdDev_32f_C1R_host(h_imgII.ptr(), h_imgII.pitch(),
                                          h_imgSII.ptr(), h_imgSII.pitch(),
                                          h_norm.ptr(), h_norm.pitch(),
                                          szNormRoi, this->rect,
                                          this->scaleFactor);
    ncvAssertReturn(ncvStat == NPPST_SUCCESS, false);
    NCV_SKIP_COND_END

    //bit-to-bit check
    bool bLoopVirgin = true;

    NCV_SKIP_COND_BEGIN
    const Ncv64f relEPS = 0.005;
    for (Ncv32u i=0; bLoopVirgin && i < h_norm.height(); i++)
    {
        for (Ncv32u j=0; bLoopVirgin && j < h_norm.width(); j++)
        {
            Ncv64f absErr = fabs(h_norm.ptr()[h_norm.stride()*i+j] - h_norm_d.ptr()[h_norm_d.stride()*i+j]);
            Ncv64f relErr = absErr / h_norm.ptr()[h_norm.stride()*i+j];

            if (relErr > relEPS)
            {
                bLoopVirgin = false;
            }
        }
    }
    NCV_SKIP_COND_END

    if (bLoopVirgin)
    {
        rcode = true;
    }

    return rcode;
}
Exemple #3
0
void sqrtm(const emxArray_real_T *A, emxArray_creal_T *X, real_T *arg2)
{
  emxArray_creal_T *T;
  emxArray_creal_T *y;
  boolean_T b0;
  const mxArray *b_y;
  static const int32_T iv40[2] = { 1, 19 };

  const mxArray *m2;
  char_T cv12[19];
  int32_T i;
  static const char_T cv13[19] = { 'C', 'o', 'd', 'e', 'r', ':', 'M', 'A', 'T',
    'L', 'A', 'B', ':', 's', 'q', 'u', 'a', 'r', 'e' };

  emxArray_creal_T *Q;
  emxArray_creal_T *r25;
  int32_T loop_ub;
  int16_T iv41[2];
  emxArray_creal_T *R;
  real_T s_re;
  real_T s_im;
  int32_T k;
  real_T R_re;
  real_T R_im;
  real_T T_re;
  real_T T_im;
  real_T brm;
  int32_T i1;
  int32_T b_loop_ub;
  int32_T i2;
  emxArray_creal_T *b_T;
  emxArray_real_T *b_X;
  emlrtHeapReferenceStackEnterFcnR2012b(emlrtRootTLSGlobal);
  emxInit_creal_T(&T, 2, &k_emlrtRTEI, TRUE);
  emxInit_creal_T(&y, 2, &k_emlrtRTEI, TRUE);
  b0 = (A->size[0] == A->size[1]);
  if (b0) {
  } else {
    emlrtPushRtStackR2012b(&hb_emlrtRSI, emlrtRootTLSGlobal);
    emlrt_synchGlobalsToML();
    b_y = NULL;
    m2 = mxCreateCharArray(2, iv40);
    for (i = 0; i < 19; i++) {
      cv12[i] = cv13[i];
    }

    emlrtInitCharArrayR2013a(emlrtRootTLSGlobal, 19, m2, cv12);
    emlrtAssign(&b_y, m2);
    error(message(b_y, &f_emlrtMCI), &g_emlrtMCI);
    emlrt_synchGlobalsFromML();
    emlrtPopRtStackR2012b(&hb_emlrtRSI, emlrtRootTLSGlobal);
  }

  emxInit_creal_T(&Q, 2, &k_emlrtRTEI, TRUE);
  emxInit_creal_T(&r25, 2, &k_emlrtRTEI, TRUE);
  emlrtPushRtStackR2012b(&ib_emlrtRSI, emlrtRootTLSGlobal);
  schur(A, Q, r25);
  i = T->size[0] * T->size[1];
  T->size[0] = r25->size[0];
  T->size[1] = r25->size[1];
  emxEnsureCapacity((emxArray__common *)T, i, (int32_T)sizeof(creal_T),
                    &k_emlrtRTEI);
  loop_ub = r25->size[0] * r25->size[1];
  for (i = 0; i < loop_ub; i++) {
    T->data[i] = r25->data[i];
  }

  emxFree_creal_T(&r25);
  emlrtPopRtStackR2012b(&ib_emlrtRSI, emlrtRootTLSGlobal);
  for (i = 0; i < 2; i++) {
    iv41[i] = (int16_T)T->size[i];
  }

  emxInit_creal_T(&R, 2, &l_emlrtRTEI, TRUE);
  i = R->size[0] * R->size[1];
  R->size[0] = iv41[0];
  emxEnsureCapacity((emxArray__common *)R, i, (int32_T)sizeof(creal_T),
                    &k_emlrtRTEI);
  i = R->size[0] * R->size[1];
  R->size[1] = iv41[1];
  emxEnsureCapacity((emxArray__common *)R, i, (int32_T)sizeof(creal_T),
                    &k_emlrtRTEI);
  loop_ub = iv41[0] * iv41[1];
  for (i = 0; i < loop_ub; i++) {
    R->data[i].re = 0.0;
    R->data[i].im = 0.0;
  }

  emlrtPushRtStackR2012b(&jb_emlrtRSI, emlrtRootTLSGlobal);
  b0 = isUTmatD(T);
  emlrtPopRtStackR2012b(&jb_emlrtRSI, emlrtRootTLSGlobal);
  if (b0) {
    emlrtPushRtStackR2012b(&kb_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPopRtStackR2012b(&kb_emlrtRSI, emlrtRootTLSGlobal);
    for (loop_ub = 0; loop_ub + 1 <= A->size[0]; loop_ub++) {
      R->data[loop_ub + R->size[0] * loop_ub] = T->data[loop_ub + T->size[0] *
        loop_ub];
      c_sqrt(&R->data[loop_ub + R->size[0] * loop_ub]);
    }
  } else {
    emlrtPushRtStackR2012b(&lb_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPopRtStackR2012b(&lb_emlrtRSI, emlrtRootTLSGlobal);
    for (loop_ub = 0; loop_ub + 1 <= A->size[0]; loop_ub++) {
      R->data[loop_ub + R->size[0] * loop_ub] = T->data[loop_ub + T->size[0] *
        loop_ub];
      c_sqrt(&R->data[loop_ub + R->size[0] * loop_ub]);
      for (i = loop_ub - 1; i + 1 > 0; i--) {
        s_re = 0.0;
        s_im = 0.0;
        emlrtPushRtStackR2012b(&mb_emlrtRSI, emlrtRootTLSGlobal);
        emlrtPopRtStackR2012b(&mb_emlrtRSI, emlrtRootTLSGlobal);
        for (k = i + 1; k + 1 <= loop_ub; k++) {
          R_re = R->data[i + R->size[0] * k].re * R->data[k + R->size[0] *
            loop_ub].re - R->data[i + R->size[0] * k].im * R->data[k + R->size[0]
            * loop_ub].im;
          R_im = R->data[i + R->size[0] * k].re * R->data[k + R->size[0] *
            loop_ub].im + R->data[i + R->size[0] * k].im * R->data[k + R->size[0]
            * loop_ub].re;
          s_re += R_re;
          s_im += R_im;
        }

        T_re = T->data[i + T->size[0] * loop_ub].re - s_re;
        T_im = T->data[i + T->size[0] * loop_ub].im - s_im;
        R_re = R->data[i + R->size[0] * i].re + R->data[loop_ub + R->size[0] *
          loop_ub].re;
        R_im = R->data[i + R->size[0] * i].im + R->data[loop_ub + R->size[0] *
          loop_ub].im;
        if (R_im == 0.0) {
          if (T_im == 0.0) {
            R->data[i + R->size[0] * loop_ub].re = T_re / R_re;
            R->data[i + R->size[0] * loop_ub].im = 0.0;
          } else if (T_re == 0.0) {
            R->data[i + R->size[0] * loop_ub].re = 0.0;
            R->data[i + R->size[0] * loop_ub].im = T_im / R_re;
          } else {
            R->data[i + R->size[0] * loop_ub].re = T_re / R_re;
            R->data[i + R->size[0] * loop_ub].im = T_im / R_re;
          }
        } else if (R_re == 0.0) {
          if (T_re == 0.0) {
            R->data[i + R->size[0] * loop_ub].re = T_im / R_im;
            R->data[i + R->size[0] * loop_ub].im = 0.0;
          } else if (T_im == 0.0) {
            R->data[i + R->size[0] * loop_ub].re = 0.0;
            R->data[i + R->size[0] * loop_ub].im = -(T_re / R_im);
          } else {
            R->data[i + R->size[0] * loop_ub].re = T_im / R_im;
            R->data[i + R->size[0] * loop_ub].im = -(T_re / R_im);
          }
        } else {
          brm = muDoubleScalarAbs(R_re);
          s_re = muDoubleScalarAbs(R_im);
          if (brm > s_re) {
            s_re = R_im / R_re;
            s_im = R_re + s_re * R_im;
            R->data[i + R->size[0] * loop_ub].re = (T_re + s_re * T_im) / s_im;
            R->data[i + R->size[0] * loop_ub].im = (T_im - s_re * T_re) / s_im;
          } else if (s_re == brm) {
            if (R_re > 0.0) {
              s_im = 0.5;
            } else {
              s_im = -0.5;
            }

            if (R_im > 0.0) {
              s_re = 0.5;
            } else {
              s_re = -0.5;
            }

            R->data[i + R->size[0] * loop_ub].re = (T_re * s_im + T_im * s_re) /
              brm;
            R->data[i + R->size[0] * loop_ub].im = (T_im * s_im - T_re * s_re) /
              brm;
          } else {
            s_re = R_re / R_im;
            s_im = R_im + s_re * R_re;
            R->data[i + R->size[0] * loop_ub].re = (s_re * T_re + T_im) / s_im;
            R->data[i + R->size[0] * loop_ub].im = (s_re * T_im - T_re) / s_im;
          }
        }
      }
    }
  }

  emlrtPushRtStackR2012b(&nb_emlrtRSI, emlrtRootTLSGlobal);
  emlrtPushRtStackR2012b(&kh_emlrtRSI, emlrtRootTLSGlobal);
  dynamic_size_checks(Q, R);
  emlrtPopRtStackR2012b(&kh_emlrtRSI, emlrtRootTLSGlobal);
  if ((Q->size[1] == 1) || (R->size[0] == 1)) {
    i = y->size[0] * y->size[1];
    y->size[0] = Q->size[0];
    y->size[1] = R->size[1];
    emxEnsureCapacity((emxArray__common *)y, i, (int32_T)sizeof(creal_T),
                      &k_emlrtRTEI);
    loop_ub = Q->size[0];
    for (i = 0; i < loop_ub; i++) {
      k = R->size[1];
      for (i1 = 0; i1 < k; i1++) {
        y->data[i + y->size[0] * i1].re = 0.0;
        y->data[i + y->size[0] * i1].im = 0.0;
        b_loop_ub = Q->size[1];
        for (i2 = 0; i2 < b_loop_ub; i2++) {
          s_re = Q->data[i + Q->size[0] * i2].re * R->data[i2 + R->size[0] * i1]
            .re - Q->data[i + Q->size[0] * i2].im * R->data[i2 + R->size[0] * i1]
            .im;
          s_im = Q->data[i + Q->size[0] * i2].re * R->data[i2 + R->size[0] * i1]
            .im + Q->data[i + Q->size[0] * i2].im * R->data[i2 + R->size[0] * i1]
            .re;
          y->data[i + y->size[0] * i1].re += s_re;
          y->data[i + y->size[0] * i1].im += s_im;
        }
      }
    }
  } else {
    iv41[0] = (int16_T)Q->size[0];
    iv41[1] = (int16_T)R->size[1];
    emlrtPushRtStackR2012b(&jh_emlrtRSI, emlrtRootTLSGlobal);
    i = y->size[0] * y->size[1];
    y->size[0] = iv41[0];
    emxEnsureCapacity((emxArray__common *)y, i, (int32_T)sizeof(creal_T),
                      &k_emlrtRTEI);
    i = y->size[0] * y->size[1];
    y->size[1] = iv41[1];
    emxEnsureCapacity((emxArray__common *)y, i, (int32_T)sizeof(creal_T),
                      &k_emlrtRTEI);
    loop_ub = iv41[0] * iv41[1];
    for (i = 0; i < loop_ub; i++) {
      y->data[i].re = 0.0;
      y->data[i].im = 0.0;
    }

    eml_xgemm(Q->size[0], R->size[1], Q->size[1], Q, Q->size[0], R, Q->size[1],
              y, Q->size[0]);
    emlrtPopRtStackR2012b(&jh_emlrtRSI, emlrtRootTLSGlobal);
  }

  emxFree_creal_T(&R);
  i = T->size[0] * T->size[1];
  T->size[0] = Q->size[1];
  T->size[1] = Q->size[0];
  emxEnsureCapacity((emxArray__common *)T, i, (int32_T)sizeof(creal_T),
                    &k_emlrtRTEI);
  loop_ub = Q->size[0];
  for (i = 0; i < loop_ub; i++) {
    k = Q->size[1];
    for (i1 = 0; i1 < k; i1++) {
      T->data[i1 + T->size[0] * i].re = Q->data[i + Q->size[0] * i1].re;
      T->data[i1 + T->size[0] * i].im = -Q->data[i + Q->size[0] * i1].im;
    }
  }

  emxFree_creal_T(&Q);
  emlrtPushRtStackR2012b(&kh_emlrtRSI, emlrtRootTLSGlobal);
  dynamic_size_checks(y, T);
  emlrtPopRtStackR2012b(&kh_emlrtRSI, emlrtRootTLSGlobal);
  if ((y->size[1] == 1) || (T->size[0] == 1)) {
    i = X->size[0] * X->size[1];
    X->size[0] = y->size[0];
    X->size[1] = T->size[1];
    emxEnsureCapacity((emxArray__common *)X, i, (int32_T)sizeof(creal_T),
                      &k_emlrtRTEI);
    loop_ub = y->size[0];
    for (i = 0; i < loop_ub; i++) {
      k = T->size[1];
      for (i1 = 0; i1 < k; i1++) {
        X->data[i + X->size[0] * i1].re = 0.0;
        X->data[i + X->size[0] * i1].im = 0.0;
        b_loop_ub = y->size[1];
        for (i2 = 0; i2 < b_loop_ub; i2++) {
          s_re = y->data[i + y->size[0] * i2].re * T->data[i2 + T->size[0] * i1]
            .re - y->data[i + y->size[0] * i2].im * T->data[i2 + T->size[0] * i1]
            .im;
          s_im = y->data[i + y->size[0] * i2].re * T->data[i2 + T->size[0] * i1]
            .im + y->data[i + y->size[0] * i2].im * T->data[i2 + T->size[0] * i1]
            .re;
          X->data[i + X->size[0] * i1].re += s_re;
          X->data[i + X->size[0] * i1].im += s_im;
        }
      }
    }
  } else {
    iv41[0] = (int16_T)y->size[0];
    iv41[1] = (int16_T)T->size[1];
    emlrtPushRtStackR2012b(&jh_emlrtRSI, emlrtRootTLSGlobal);
    i = X->size[0] * X->size[1];
    X->size[0] = iv41[0];
    emxEnsureCapacity((emxArray__common *)X, i, (int32_T)sizeof(creal_T),
                      &k_emlrtRTEI);
    i = X->size[0] * X->size[1];
    X->size[1] = iv41[1];
    emxEnsureCapacity((emxArray__common *)X, i, (int32_T)sizeof(creal_T),
                      &k_emlrtRTEI);
    loop_ub = iv41[0] * iv41[1];
    for (i = 0; i < loop_ub; i++) {
      X->data[i].re = 0.0;
      X->data[i].im = 0.0;
    }

    eml_xgemm(y->size[0], T->size[1], y->size[1], y, y->size[0], T, y->size[1],
              X, y->size[0]);
    emlrtPopRtStackR2012b(&jh_emlrtRSI, emlrtRootTLSGlobal);
  }

  emlrtPopRtStackR2012b(&nb_emlrtRSI, emlrtRootTLSGlobal);
  emlrtPushRtStackR2012b(&ob_emlrtRSI, emlrtRootTLSGlobal);
  emlrtPushRtStackR2012b(&kh_emlrtRSI, emlrtRootTLSGlobal);
  dynamic_size_checks(X, X);
  emlrtPopRtStackR2012b(&kh_emlrtRSI, emlrtRootTLSGlobal);
  if ((X->size[1] == 1) || (X->size[0] == 1)) {
    i = T->size[0] * T->size[1];
    T->size[0] = X->size[0];
    T->size[1] = X->size[1];
    emxEnsureCapacity((emxArray__common *)T, i, (int32_T)sizeof(creal_T),
                      &k_emlrtRTEI);
    loop_ub = X->size[0];
    for (i = 0; i < loop_ub; i++) {
      k = X->size[1];
      for (i1 = 0; i1 < k; i1++) {
        T->data[i + T->size[0] * i1].re = 0.0;
        T->data[i + T->size[0] * i1].im = 0.0;
        b_loop_ub = X->size[1];
        for (i2 = 0; i2 < b_loop_ub; i2++) {
          s_re = X->data[i + X->size[0] * i2].re * X->data[i2 + X->size[0] * i1]
            .re - X->data[i + X->size[0] * i2].im * X->data[i2 + X->size[0] * i1]
            .im;
          s_im = X->data[i + X->size[0] * i2].re * X->data[i2 + X->size[0] * i1]
            .im + X->data[i + X->size[0] * i2].im * X->data[i2 + X->size[0] * i1]
            .re;
          T->data[i + T->size[0] * i1].re += s_re;
          T->data[i + T->size[0] * i1].im += s_im;
        }
      }
    }
  } else {
    iv41[0] = (int16_T)X->size[0];
    iv41[1] = (int16_T)X->size[1];
    emlrtPushRtStackR2012b(&jh_emlrtRSI, emlrtRootTLSGlobal);
    i = T->size[0] * T->size[1];
    T->size[0] = iv41[0];
    emxEnsureCapacity((emxArray__common *)T, i, (int32_T)sizeof(creal_T),
                      &k_emlrtRTEI);
    i = T->size[0] * T->size[1];
    T->size[1] = iv41[1];
    emxEnsureCapacity((emxArray__common *)T, i, (int32_T)sizeof(creal_T),
                      &k_emlrtRTEI);
    loop_ub = iv41[0] * iv41[1];
    for (i = 0; i < loop_ub; i++) {
      T->data[i].re = 0.0;
      T->data[i].im = 0.0;
    }

    eml_xgemm(X->size[0], X->size[1], X->size[1], X, X->size[0], X, X->size[1],
              T, X->size[0]);
    emlrtPopRtStackR2012b(&jh_emlrtRSI, emlrtRootTLSGlobal);
  }

  emxInit_creal_T(&b_T, 2, &k_emlrtRTEI, TRUE);
  emlrtPopRtStackR2012b(&ob_emlrtRSI, emlrtRootTLSGlobal);
  emlrtPushRtStackR2012b(&ob_emlrtRSI, emlrtRootTLSGlobal);
  i = b_T->size[0] * b_T->size[1];
  b_T->size[0] = T->size[0];
  b_T->size[1] = T->size[1];
  emxEnsureCapacity((emxArray__common *)b_T, i, (int32_T)sizeof(creal_T),
                    &k_emlrtRTEI);
  loop_ub = T->size[0] * T->size[1];
  for (i = 0; i < loop_ub; i++) {
    b_T->data[i].re = T->data[i].re - A->data[i];
    b_T->data[i].im = T->data[i].im;
  }

  emxInit_real_T(&b_X, 2, &k_emlrtRTEI, TRUE);
  s_re = norm(b_T);
  s_im = b_norm(A);
  *arg2 = s_re / s_im;
  emlrtPopRtStackR2012b(&ob_emlrtRSI, emlrtRootTLSGlobal);
  i = b_X->size[0] * b_X->size[1];
  b_X->size[0] = X->size[0];
  b_X->size[1] = X->size[1];
  emxEnsureCapacity((emxArray__common *)b_X, i, (int32_T)sizeof(real_T),
                    &k_emlrtRTEI);
  loop_ub = X->size[0] * X->size[1];
  emxFree_creal_T(&b_T);
  for (i = 0; i < loop_ub; i++) {
    b_X->data[i] = X->data[i].im;
  }

  if (c_norm(b_X) <= 10.0 * (real_T)A->size[0] * 2.2204460492503131E-16 * d_norm
      (X)) {
    emlrtPushRtStackR2012b(&pb_emlrtRSI, emlrtRootTLSGlobal);
    emlrtPopRtStackR2012b(&pb_emlrtRSI, emlrtRootTLSGlobal);
    for (loop_ub = 0; loop_ub + 1 <= A->size[0]; loop_ub++) {
      emlrtPushRtStackR2012b(&qb_emlrtRSI, emlrtRootTLSGlobal);
      emlrtPopRtStackR2012b(&qb_emlrtRSI, emlrtRootTLSGlobal);
      for (i = 0; i + 1 <= A->size[0]; i++) {
        s_re = X->data[i + X->size[0] * loop_ub].re;
        X->data[i + X->size[0] * loop_ub].re = s_re;
        X->data[i + X->size[0] * loop_ub].im = 0.0;
      }
    }
  }

  emxFree_real_T(&b_X);
  emxFree_creal_T(&y);
  emxFree_creal_T(&T);
  emlrtHeapReferenceStackLeaveFcnR2012b(emlrtRootTLSGlobal);
}