示例#1
0
文件: mie.c 项目: JiapengHuang/SPP
void small_conducting_Mie(double x,struct c_complex m,double*mu,
long nangles,struct c_complex*s1,
struct c_complex*s2,double*qext,double*qsca,
double*qback,double*g)

/*:31*/
#line 448 "./mie.w"


{
struct c_complex ahat1,ahat2,bhat1,bhat2;
struct c_complex ss1;
double x2,x3,x4,muj,angle;
long j;

if((s1==NULL)||(s2==NULL))nangles= 0;

m.re+= 0.0;
x2= x*x;
x3= x2*x;
x4= x2*x2;

ahat1= c_div(c_set(0.0,2.0/3.0*(1.0-0.2*x2)),c_set(1.0-0.5*x2,2.0/3.0*x3));
bhat1= c_div(c_set(0.0,(x2-10.0)/30.0),c_set(1+0.5*x2,-x3/3.0));
ahat2= c_set(0.0,x2/30.);
bhat2= c_set(0.0,-x2/45.);

*qsca= 6.0*x4*(c_norm(ahat1)+c_norm(bhat1)+
(5.0/3.0)*(c_norm(ahat2)+c_norm(bhat2)));
*qext= *qsca;
*g= 6.0*x4*(ahat1.im*(ahat2.im+bhat1.im)+
bhat2.im*(5.0/9.0*ahat2.im+bhat1.im)+
ahat1.re*bhat1.re)/(*qsca);

ss1.re= 1.5*x2*(ahat1.re-bhat1.re);
ss1.im= 1.5*x2*(ahat1.im-bhat1.im-(5.0/3.0)*(ahat2.im+bhat2.im));
*qback= 4*c_norm(ss1);

x3*= 1.5;
ahat1.re*= x3;
ahat1.im*= x3;
bhat1.re*= x3;
bhat1.im*= x3;
ahat2.im*= x3*(5.0/3.0);
bhat2.im*= x3*(5.0/3.0);
for(j= 0;j<nangles;j++){
muj= mu[j];
angle= 2.0*muj*muj-1.0;
s1[j].re= ahat1.re+(bhat1.re)*muj;
s1[j].im= ahat1.im+(bhat1.im+ahat2.im)*muj+bhat2.im*angle;;
s2[j].re= bhat1.re+(ahat1.re)*muj;
s2[j].im= bhat1.im+(ahat1.im+bhat2.im)*muj+ahat2.im*angle;
}
}
示例#2
0
/*--------------------------------------------------------------------*/
complex c_div(complex x, complex y){
  complex z;
  double n = c_norm(y); 
  n = n*n;
  z.re = (x.re * y.re + x.im * y.im) / n;
  z.im = (x.im * y.re - x.re * y.im) / n; 
  return z;
}
示例#3
0
void filterer::sortGB(vector <scan*> &sec)
{
	for (unsigned int i = 0; i < sec.size(); ++i){
		double z_score = (sec.at(i)->discrim-final_p_avg)/final_p_std;
		sec.at(i)->PP = c_norm(z_score);
	//	if (sec.at(i)->PP > cutoff)
	//		sec.at(i)->good = true;
	//	else
	//		sec.at(i)->good = false;
		evalCharge(sec.at(i));
	}
}
void schurFactorization(long n, complex **A, complex **T, complex **U)
{

  /* Schur factorization: A = U*T*U', T = upper triangular, U = unitary */
            
  long i,j,iter,maxIter;
  double tol, diff1,diff2; 
  complex T11, T12, T21, T22; 
  complex sigma1, sigma2, sigma; 
  complex z, z1, z2; 
  complex **P, **Q, **R;


  /* Allocate auxiliary matrices */

  P     = (complex **)Mem(MEM_ALLOC,n, sizeof(complex *)); 
  Q     = (complex **)Mem(MEM_ALLOC,n, sizeof(complex *)); 
  R     = (complex **)Mem(MEM_ALLOC,n, sizeof(complex *)); 

  for (i=0; i<n; i++){
    P[i]     = (complex *)Mem(MEM_ALLOC,n, sizeof(complex)); 
    Q[i]     = (complex *)Mem(MEM_ALLOC,n, sizeof(complex)); 
    R[i]     = (complex *)Mem(MEM_ALLOC,n, sizeof(complex)); 
  }

  /* ------------------------------------------------------------*/

  /* Parameters for iteration */

   maxIter = 500;
   tol     = 1E-30; 

  /* ------------------------------------------------------------*/

  /* Init U = eye(n) (identity matrix) */

  for (i=0; i<n; i++){
    U[i][i].re = 1.0; 
    U[i][i].im = 0.0; 
  }  
  
  /* ------------------------------------------------------------*/

  /* Reduce A to Hessenberg form */

  hessFactorization(n,A,P,T); 

  /* ------------------------------------------------------------*/

  /* Compute Schur factorization of Hessenberg matrix T */


   for (j=n-1; j>0; j--){ /* Main loop */

     for (iter=0; iter<maxIter; iter++){ /* Iteration loop */

       sigma.re = T[j][j].re;
       sigma.im = T[j][j].im; 


       /* -- Use Wilkinson shift -- */

       /* submatrix considered in the shift */

       T11 = T[j-1][j-1];
       T12 = T[j-1][j];
       T21 = T[j][j-1];
       T22 = T[j][j];

       /* Compute eigenvalues of submatrix */

       z.re  = 0.0;
       z.im  = 0.0;
       z2.re = 0.0;
       z2.im = 0.0;

       /* z = T11*T11 + T22*T22 - 2*T11*T22 + 4*T12*T21 */

       z1 = c_mul(T11,T11);

       z  = c_add(z ,z1);
       z2 = c_add(z2,z1);
       
       z1 = c_mul(T22,T22);

       z  = c_add(z ,z1);
       z2 = c_add(z2,z1);

       z1 = c_mul(T11,T22);

       z1.re = -2.0 * z1.re;
       z1.im = -2.0 * z1.im;

       z  = c_add(z,z1);

       z1 = c_mul(T12,T21);
       z1.re = 4.0 * z1.re;
       z1.im = 4.0 * z1.im;
       z = c_add(z,z1);

       /* Square root*/

       z = c_sqrt(z);

       /* Eigenvalues */
       
       sigma1 = c_add(z2,z);
       sigma2 = c_sub(z2,z);

/*        printf("sigma1 = %e %e\n", sigma1.re, sigma1.im); */
/*        printf("sigma2 = %e %e\n", sigma2.re, sigma2.im); */

       /* Select eigenvalue for shift*/

       diff1 = c_norm( c_sub(T[j][j], sigma1) );
       diff2 = c_norm( c_sub(T[j][j], sigma2) );

       if (diff1 < diff2){
	 sigma.re = sigma1.re;
	 sigma.im = sigma1.im;
       }else{
	 sigma.re = sigma2.re;
	 sigma.im = sigma2.im;
       }

       /* --- QR step with Wilkinson shift --- */

       /* Shift: T(1:j,1:j) = T(1:j,1:j) - sigma * eye(j) */

       for (i=0; i<j+1; i++){

	 CheckValue(FUNCTION_NAME, "T[i][i].re","", T[i][i].re, -INFTY, INFTY);
	 CheckValue(FUNCTION_NAME, "T[i][i].im","", T[i][i].im, -INFTY, INFTY);	 

	 T[i][i].re = T[i][i].re - sigma.re;   
	 T[i][i].im = T[i][i].im - sigma.im;   
	 
       }

       /* Compute QR factorization of shifted Hessenberg matrix */

       for (i=0; i<n; i++){
	 memset(Q[i], 0, n*sizeof(complex));
	 memset(R[i], 0, n*sizeof(complex));
       }

       QRfactorization(n,T,Q,R); 

       /* T = T_new = R * Q  */

       for (i=0; i<n; i++){
	 memset(T[i], 0, n*sizeof(complex));
       }
       matProduct(n, n, n, R, Q, T);

       /* T(1:j,1:j) = T(1:j,1:j) + sigma * eye(j) */
       for (i=0; i<j+1; i++){
	 T[i][i].re = T[i][i].re + sigma.re;   
	 T[i][i].im = T[i][i].im + sigma.im;   
       }


       /* R =  U_new = U * Q */

       for (i=0; i<n; i++){
	 memset(R[i], 0, n*sizeof(complex));
       }       
       matProduct(n,n,n,U,Q,R); 

       /* U = R */

       for (i=0; i<n; i++){
	 memcpy(U[i],R[i], n*sizeof(complex));
       } 

       /* Check convergence */

       if (c_norm( T[j][j-1] ) <= tol * (c_norm(T[j-1][j-1]) + c_norm(T[j][j]))){
	 T[j][j-1].re = 0.0;
	 T[j][j-1].im = 0.0;
	 break; 
       }
       
   
     }	/* end of iter loop */  
    
   } /* end of main loop */


  /* -------------------------------------------------------------*/

   /* U = P*U */

   for (i=0; i<n; i++){
     memset(U[i], 0, n*sizeof(complex));
   }
   matProduct(n,n,n,P,R,U);
   

  /* -------------------------------------------------------------*/
  /* Free auxiliary variables */

   for (i=0; i<n; i++){
     Mem(MEM_FREE,P[i]); 
     Mem(MEM_FREE,Q[i]); 
     Mem(MEM_FREE,R[i]); 
   }

   Mem(MEM_FREE,P); 
   Mem(MEM_FREE,Q); 
   Mem(MEM_FREE,R); 

  /* Return */

  return;   
    
  
}
void QRfactorization(long n, complex **A, complex **Q, complex **R)
{

  /* QR factorization based on Householder transformations */
            
  long i,j,k,m;
  double nrm; 
  complex z,z1,z2;  
  complex *vj;

 /* Init. Q = eye(n) (identity matrix) */

  for (i=0; i<n; i++){
    Q[i][i].re = 1.0; 
    Q[i][i].im = 0.0; 
  }


  /* Init. R = A  */

  for(j=0; j<n; j++){
    for (i=0; i<n; i++){
      R[i][j].re = A[i][j].re;
      R[i][j].im = A[i][j].im;

      CheckValue(FUNCTION_NAME, "A[i][j].re","", A[i][j].re, -INFTY, INFTY);
      CheckValue(FUNCTION_NAME, "A[i][j].im","", A[i][j].im, -INFTY, INFTY);

    }
  }

  /* Allocate auxiliary variables*/

  vj = (complex *)Mem(MEM_ALLOC, n, sizeof(complex));

 /*  printf("begin calc, n = %d\n", n);  */

    /* ------------------------------------------------------------*/

  for (j=0; j<n; j++){ /* Main loop */

    /* R(j:end, j)  */

    for (i=j; i<n; i++){
      vj[i-j].re = R[i][j].re;
      vj[i-j].im = R[i][j].im;
    }
    
    nrm = vectorNorm(n-j, vj); 

    /* v(1) = v(1) + sign(R(j,j)) * norm(v) */

    vj[0].re = vj[0].re + R[j][j].re / c_norm(R[j][j]) * nrm; 
    vj[0].im = vj[0].im + R[j][j].im / c_norm(R[j][j]) * nrm;


    /* Update norm */

    nrm = vectorNorm(n-j, vj);  

    /* v = v./norm(v) */

    for (i=0; i<n-j; i++){
      vj[i].re = vj[i].re / nrm; 
      vj[i].im = vj[i].im / nrm; 
    }

    /* Update */

    /* R(j:end, :) = R(j:end,:) - 2 * vj * vj' * R(j:end,:), : */

    /* Q(:,j:end)  = Q(:,j:end) - 2 * Q(:,j:end) * vj * vj^T */

    for (k=0; k<n; k++){

	/* (v * v' * A)_ik = v_i SUM_m Conj(v_m) A_mk */

	z.re = 0.0; 
	z.im = 0.0; 

	for (m=j; m<n; m++){

	  z1 = c_con(vj[m-j]); 
	  z1 = c_mul(z1, R[m][k]); 	  
	  z  = c_add(z,z1); 
	}

	for (i=j; i<n; i++){ 

	  z2    = c_mul(vj[i-j],z);

	  /* Update R(i,k) */
	  
	  R[i][k].re = R[i][k].re - 2.0 * z2.re; 
	  R[i][k].im = R[i][k].im - 2.0 * z2.im; 

	  CheckValue(FUNCTION_NAME, "R[i][k].re","", R[i][k].re, -INFTY, INFTY);
	  CheckValue(FUNCTION_NAME, "R[i][k].im","", R[i][k].im, -INFTY, INFTY);

	} 

	/* (A * v * v^')_ki = v_i * SUM_m Conj(v_m) A_km */

	z.re = 0.0;
	z.im = 0.0;

	for (m=j; m<n; m++){

	  z1 = vj[m-j]; 
	  z1 = c_mul(z1, Q[k][m]);
	  z  = c_add(z,z1);
	}

	for (i=j; i<n; i++){

	  z1 = c_con(vj[i-j]); 
	  z2 = c_mul(z1,z);

	  /* Update Q(k,i)*/

	  Q[k][i].re = Q[k][i].re - 2.0 * z2.re; 
	  Q[k][i].im = Q[k][i].im - 2.0 * z2.im; 

	  CheckValue(FUNCTION_NAME, "Q[k][i].re","", Q[k][i].re, -INFTY, INFTY);
	  CheckValue(FUNCTION_NAME, "Q[k][i].im","", Q[k][i].im, -INFTY, INFTY);
	}	      
    }
  } /* End of main loop (j) */

  /* -------------------------------------------------------------*/

  /* Free auxiliary variables */

  Mem(MEM_FREE, vj); 
  return;   

    
  
}
示例#6
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);
}
void LUdecomposition(long n, complex **A, complex *b, complex *x)
{

  long i,j,k,*p;
  complex akk, bk, z, sum; 
  complex *Ak; 


  /* Allocate auxiliary variables */

  p = (long *)Mem(MEM_ALLOC, n, sizeof(long)); 


  /* Gaussian elimination with partial pivoting */

  for (k=0; k<n-1; k++){ /* columns of A */


    /* --- Pivoting --- */

    akk.re = 0.0; 
    akk.im = 0.0; 
    j = k; 
    for (i=k; i<n; i++){
      if ( c_norm(A[i][k]) > c_norm(akk)){
	akk = A[i][k]; 

	CheckValue(FUNCTION_NAME, "A[i][k].re","", A[i][k].re, -INFTY, INFTY);
	CheckValue(FUNCTION_NAME, "A[i][k].im","", A[i][k].im, -INFTY, INFTY);

	j = i; 
      }
    }

    if (j != k){ /* swap rows j and k*/


      Ak   = A[k];  
      A[k] = A[j]; /* swap pointers*/
      A[j] = Ak; 

      bk   = b[k]; 
      b[k] = b[j]; 
      b[j] = bk; 

    }

    p[k] = j;  /* Keep list of permutations */

    /* Sanity check */

    if (akk.re != A[k][k].re && akk.im != A[k][k].im){
      Die(FUNCTION_NAME, "Something went wrong with pivoting?"); 
      return; 
    }

    if (akk.re == 0.0 && akk.im == 0.0){
      Die(FUNCTION_NAME, "Matrix singular?"); 
      return; 
    }

    /* Store L part */

    for (i=k+1;i<n; i++){
      A[i][k] = c_div(A[i][k],A[k][k]); 

      CheckValue(FUNCTION_NAME, "A[i][k].re","", A[i][k].re, -INFTY, INFTY);
      CheckValue(FUNCTION_NAME, "A[i][k].im","", A[i][k].im, -INFTY, INFTY);

    }

    /* Update */

    for (i=k+1; i<n; i++){

      /* update b */

      z = c_mul(A[i][k], b[k]);  
      b[i] = c_sub(b[i], z); 

      CheckValue(FUNCTION_NAME, "b[i].re","", b[i].re, -INFTY, INFTY);
      CheckValue(FUNCTION_NAME, "b[i].im","", b[i].im, -INFTY, INFTY);

      for (j=k+1;  j<n; j++){
	
	/* Update U part of A */
	
	z = c_mul( A[i][k], A[k][j]);
	A[i][j] = c_sub(A[i][j],z);  

	CheckValue(FUNCTION_NAME, "A[i][j].re","", A[i][j].re, -INFTY, INFTY);
	CheckValue(FUNCTION_NAME, "A[i][j].im","", A[i][j].im, -INFTY, INFTY);
      }
    }        
  }


  /* Solve x */

  x[n-1] = c_div(b[n-1],A[n-1][n-1]);

  for (i=n-2; i>=0; i--){
    sum.re = 0.0;
    sum.im = 0.0;
    for (j=i+1; j<n; j++){
      z   = c_mul(A[i][j], x[j]);
      sum = c_add(sum, z);
    }
    z    = c_sub(b[i], sum);
    x[i] = c_div(z, A[i][i]);
  }

  Mem(MEM_FREE, p); 

  /* Return */

  return;   
    
  
}
示例#8
0
文件: mie.c 项目: JiapengHuang/SPP
void Mie(double x,struct c_complex m,double*mu,long nangles,struct c_complex*s1,
struct c_complex*s2,double*qext,double*qsca,double*qback,double*g)

/*:34*/
#line 519 "./mie.w"


{
/*36:*/
#line 546 "./mie.w"

struct c_complex*D;
struct c_complex z1,an,bn,bnm1,anm1,qbcalc;
double*pi0,*pi1,*tau;
struct c_complex xi,xi0,xi1;
double psi,psi0,psi1;
double alpha,beta,factor;
long n,k,nstop,sign;
*qext= -1;
*qsca= -1;
*qback= -1;
*g= -1;

/*:36*/
#line 522 "./mie.w"


/*37:*/
#line 559 "./mie.w"

if(m.im> 0.0){
mie_error("This program requires m.im>=0",1);
return;
}
if(x<=0.0){
mie_error("This program requires positive sphere sizes",2);
return;
}
if(nangles<0){
mie_error("This program requires non-negative angle sizes",3);
return;
}
if(nangles<0){
mie_error("This program requires non-negative angle sizes",4);
return;
}
if((nangles> 0)&&(s1==NULL)){
mie_error("Space must be allocated for s1 if nangles!=0",5);
return;
}
if((nangles> 0)&&(s2==NULL)){
mie_error("Space must be allocated for s2if nangles!=0",6);
return;
}
if(x> 20000){
mie_error("Program not validated for spheres with x>20000",7);
return;
}

/*:37*/
#line 524 "./mie.w"

/*38:*/
#line 589 "./mie.w"

if((m.re==0)&&(x<0.1)){
small_conducting_Mie(x,m,mu,nangles,s1,s2,qext,qsca,qback,g);
return;
}

if((m.re> 0.0)&&(c_abs(m)*x<0.1)){
small_Mie(x,m,mu,nangles,s1,s2,qext,qsca,qback,g);
return;
}

/*:38*/
#line 525 "./mie.w"


/*40:*/
#line 616 "./mie.w"

nstop= floor(x+4.05*pow(x,0.33333)+2.0);

/*:40*/
#line 527 "./mie.w"


/*39:*/
#line 600 "./mie.w"

if(nangles> 0){
set_carray(s1,nangles,c_set(0.0,0.0));
set_carray(s2,nangles,c_set(0.0,0.0));

pi0= new_darray(nangles);
pi1= new_darray(nangles);
tau= new_darray(nangles);

set_darray(pi0,nangles,0.0);
set_darray(tau,nangles,0.0);
set_darray(pi1,nangles,1.0);
}

/*:39*/
#line 529 "./mie.w"

if(m.re> 0)
/*41:*/
#line 634 "./mie.w"

{
struct c_complex z;

z= c_smul(x,m);

D= new_carray(nstop+1);
if(D==NULL){
mie_error("Cannot allocate log array",8);
return;
}

if(fabs(m.im*x)<((13.78*m.re-10.8)*m.re+3.9))
Dn_up(z,nstop,D);
else
Dn_down(z,nstop,D);
}

/*:41*/
#line 531 "./mie.w"


/*42:*/
#line 671 "./mie.w"

psi0= sin(x);
psi1= psi0/x-cos(x);
xi0= c_set(psi0,cos(x));
xi1= c_set(psi1,cos(x)/x+sin(x));
*qsca= 0.0;
*g= 0.0;
*qext= 0.0;
sign= 1;
qbcalc= c_set(0.0,0.0);
anm1= c_set(0.0,0.0);
bnm1= c_set(0.0,0.0);

/*:42*/
#line 533 "./mie.w"


for(n= 1;n<=nstop;n++){
/*43:*/
#line 696 "./mie.w"

if(m.re==0.0){
an= c_sdiv(n*psi1/x-psi0,c_sub(c_smul(n/x,xi1),xi0));
bn= c_sdiv(psi1,xi1);
}else if(m.im==0.0){
z1.re= D[n].re/m.re+n/x;
an= c_sdiv(z1.re*psi1-psi0,c_sub(c_smul(z1.re,xi1),xi0));

z1.re= D[n].re*m.re+n/x;
bn= c_sdiv(z1.re*psi1-psi0,c_sub(c_smul(z1.re,xi1),xi0));
}else{
z1= c_div(D[n],m);
z1.re+= n/x;
an= c_div(c_set(z1.re*psi1-psi0,z1.im*psi1),c_sub(c_mul(z1,xi1),xi0));

z1= c_mul(D[n],m);
z1.re+= n/x;
bn= c_div(c_set(z1.re*psi1-psi0,z1.im*psi1),c_sub(c_mul(z1,xi1),xi0));
}

/*:43*/
#line 536 "./mie.w"

/*44:*/
#line 734 "./mie.w"

for(k= 0;k<nangles;k++){
factor= (2.0*n+1.0)/(n+1.0)/n;
tau[k]= n*mu[k]*pi1[k]-(n+1)*pi0[k];
alpha= factor*pi1[k];
beta= factor*tau[k];
s1[k].re+= alpha*an.re+beta*bn.re;
s1[k].im+= alpha*an.im+beta*bn.im;
s2[k].re+= alpha*bn.re+beta*an.re;
s2[k].im+= alpha*bn.im+beta*an.im;
}

for(k= 0;k<nangles;k++){
factor= pi1[k];
pi1[k]= ((2.0*n+1.0)*mu[k]*pi1[k]-(n+1.0)*pi0[k])/n;
pi0[k]= factor;
}

/*:44*/
#line 537 "./mie.w"

/*45:*/
#line 780 "./mie.w"

factor= 2.0*n+1.0;
*g+= (n-1.0/n)*(anm1.re*an.re+anm1.im*an.im+bnm1.re*bn.re+bnm1.im*bn.im);
*g+= factor/n/(n+1.0)*(an.re*bn.re+an.im*bn.im);
*qsca+= factor*(c_norm(an)+c_norm(bn));
*qext+= factor*(an.re+bn.re);
sign*= -1;
qbcalc.re+= sign*factor*(an.re-bn.re);
qbcalc.im+= sign*factor*(an.im-bn.im);

/*:45*/
#line 538 "./mie.w"

/*46:*/
#line 804 "./mie.w"

factor= (2.0*n+1.0)/x;
xi= c_sub(c_smul(factor,xi1),xi0);
xi0= xi1;
xi1= xi;

psi= factor*psi1-psi0;
psi0= psi1;
psi1= xi1.re;

anm1= an;
bnm1= bn;

/*:46*/
#line 539 "./mie.w"

}

/*47:*/
#line 817 "./mie.w"

*qsca*= 2/(x*x);
*qext*= 2/(x*x);
*g*= 4/(*qsca)/(x*x);
*qback= c_norm(qbcalc)/(x*x);

/*:47*/
#line 542 "./mie.w"

/*48:*/
#line 823 "./mie.w"

if(m.re> 0)free_carray(D);

if(nangles> 0){
free_darray(pi0);
free_darray(pi1);
free_darray(tau);
}

/*:48*/
#line 543 "./mie.w"

}
示例#9
0
文件: mie.c 项目: JiapengHuang/SPP
void small_Mie(double x,struct c_complex m,double*mu,
long nangles,struct c_complex*s1,
struct c_complex*s2,double*qext,double*qsca,
double*qback,double*g)

/*:22*/
#line 285 "./mie.w"


{
struct c_complex ahat1,ahat2,bhat1;
struct c_complex z0,m2,m4;
double x2,x3,x4;

if((s1==NULL)||(s2==NULL))nangles= 0;

m2= c_sqr(m);
m4= c_sqr(m2);
x2= x*x;
x3= x2*x;
x4= x2*x2;
z0.re= -m2.im;
z0.im= m2.re-1;

/*24:*/
#line 319 "./mie.w"

{struct c_complex z1,z2,z3,z4,D;

z1= c_smul(2.0/3.0,z0);
z2.re= 1.0-0.1*x2+(4.0*m2.re+5.0)*x4/1400.0;
z2.im= 4.0*x4*m2.im/1400.0;
z3= c_mul(z1,z2);

z4= c_smul(x3*(1.0-0.1*x2),z1);
D.re= 2.0+m2.re+(1-0.7*m2.re)*x2-(8.0*m4.re-385.0*m2.re+350.0)/1400.0*x4+z4.re;
D.im= m2.im+(-0.7*m2.im)*x2-(8.0*m4.im-385.0*m2.im)/1400.0*x4+z4.im;

ahat1= c_div(z3,D);

}

/*:24*/
#line 302 "./mie.w"

/*25:*/
#line 339 "./mie.w"

{
struct c_complex z2,z6,z7;
z2= c_smul(x2/45.0,z0);
z6.re= 1.0+(2.0*m2.re-5.0)*x2/70.0;
z6.im= m2.im*x2/35.0;
z7.re= 1.0-(2.0*m2.re-5.0)*x2/30.0;
z7.im= -m2.im*x2/15.0;
bhat1= c_mul(z2,c_div(z6,z7));
}

/*:25*/
#line 303 "./mie.w"

/*26:*/
#line 354 "./mie.w"

{struct c_complex z3,z8;

z3= c_smul((1.0-x2/14.0)*x2/15.0,z0);
z8.re= 2.0*m2.re+3.0-(m2.re/7.0-0.5)*x2;
z8.im= 2.0*m2.im-m2.im/7.0*x2;
ahat2= c_div(z3,z8);

}

/*:26*/
#line 304 "./mie.w"

/*28:*/
#line 396 "./mie.w"

{struct c_complex ss1;
double T;

T= c_norm(ahat1)+c_norm(bhat1)+(5.0/3.0)*c_norm(ahat2);
*qsca= 6.0*x4*T;
*qext= 6.0*x*(ahat1.re+bhat1.re+(5.0/3.0)*ahat2.re);
*g= (ahat1.re*(ahat2.re+bhat1.re)+ahat1.im*(ahat2.im+bhat1.im))/T;
ss1.re= 1.5*x2*(ahat1.re-bhat1.re-(5.0/3.0)*ahat2.re);
ss1.im= 1.5*x2*(ahat1.im-bhat1.im-(5.0/3.0)*ahat2.im);
*qback= 4*c_norm(ss1);
}

/*:28*/
#line 305 "./mie.w"

/*29:*/
#line 418 "./mie.w"

{
double muj,angle;
long j;
x3*= 1.5;
ahat1.re*= x3;
ahat1.im*= x3;
bhat1.re*= x3;
bhat1.im*= x3;
ahat2.re*= x3*(5.0/3.0);
ahat2.im*= x3*(5.0/3.0);
for(j= 0;j<nangles;j++){
muj= mu[j];
angle= 2.0*muj*muj-1.0;
s1[j].re= ahat1.re+(bhat1.re+ahat2.re)*muj;
s1[j].im= ahat1.im+(bhat1.im+ahat2.im)*muj;
s2[j].re= bhat1.re+ahat1.re*muj+ahat2.re*angle;
s2[j].im= bhat1.im+ahat1.im*muj+ahat2.im*angle;
}
}

/*:29*/
#line 306 "./mie.w"

}
void prepare_fasta_output_distribution(ifstream& fp_input, ofstream& fp_fasta, ofstream& fp_detail, 
					string& reference, meta_data& meta_details)
{
  	ofstream condense_log;
  	condense_log.open("condense_log.tsv", ofstream::out);

        vector<map<char,int>> mutationStats;
	//baslragsh;

        string read_name, cigar, alignment, quality, len;
	string alignment_string, quality_score, detail;
	int total_score, average_score, opt, reflength;
	int i, k, count, refindex, readindex;
	int fprimer_length, except_primer;
        unordered_map<string, int> umap;
	vector< pair<string, string>> sequences; 
	double exponent;

	int raw_sequence_count = 0;//debug purposes only

	fp_detail << "###############################################Sequence Anaysis Starts##############################################" << endl;
	
	distribution error_correction[reference.length()];
	for(i = 0; i < reference.length(); i++)
	{
	  	mutationStats.push_back(map<char,int>());
		mutationStats[i]['A'] = 0;
		mutationStats[i]['C'] = 0;
		mutationStats[i]['G'] = 0;
		mutationStats[i]['T'] = 0;
		mutationStats[i]['N'] = 0;
		for(k = 0; k < ILLUMINA_SCORE; k++)
			error_correction[i].qscore[k] = 0;

		error_correction[i].matching_base = 0;
		error_correction[i].mismatch = 0;
		error_correction[i].expected_mismatch = 0.0;
		error_correction[i].standard_deviation = 0.0;
	}
	

	condense_log << "Observed,Expected,zscore,correct,raw pval,A,C,G,T,N,%mutated" << endl;
	fprimer_length = meta_details.fprimer.length();
	except_primer = reference.length() - meta_details.fprimer.length();

	while(getline(fp_input, read_name))
        {
	  	raw_sequence_count++;
                getline(fp_input, cigar);
                getline(fp_input, alignment_string);
		getline(fp_input, quality_score);
		
		total_score = 0;
		for(i = 0; i < quality_score.length(); i++)
		{
			total_score += quality_score.at(i) - '!';
		}

		average_score = total_score / quality_score.length();
		fp_detail << endl << "Global Average Score = " << average_score << endl;

		if(average_score < GLOBAL_QSCORE)
			continue;
		
		refindex = fprimer_length;
		i = readindex = 0;
		alignment = "";
		quality = "";
		len = "";

		fp_detail << "CIGAR = " << cigar << endl;
		while(i < cigar.length())
		{
			if(cigar.at(i) >= '0' && cigar.at(i) <= '9')
				len += cigar.at(i);
			else
			{
				opt = cigar.at(i);
				count = atoi(len.c_str());
				fp_detail << "Count = " << count << ", and Option = " << opt << endl;
				len = "";			

				//fp_detail << "Reference = " << reference << endl;
				//fp_detail << "Alignment = " << alignment_string << endl;

				if(opt == 'M' || opt == 'X')
				{
					for(k = 0; k < count; k++)
					{
						if(alignment_string.at(readindex) == reference.at(refindex))
						{
							error_correction[refindex].matching_base += 1;
						}
						else 
						{
							error_correction[refindex].mismatch += 1;
							mutationStats[refindex][alignment_string.at(readindex)] += 1;
							char blah = alignment_string.at(readindex);
							//if(!(alignment_string.at(readindex) == 'A') && (alignment_string.at(readindex) != 'C') && (alignment_string.at(readindex) != 'G') && (alignment_string.at(readindex) != 'T')){
							//if((blah != 'A') && (blah != 'C') && (blah != 'G') && (blah != 'T')){
							fp_detail << "BASE Mismatch is Found at = " << refindex << endl;
						}

						error_correction[refindex].qscore[quality_score.at(readindex) - '!'] += 1;

						//fp_detail << k << "= (" << refindex << ", " << readindex << ") = " 
						//	"(" << reference.at(refindex) << ", " << alignment_string.at(readindex) << ")" << endl;

						refindex += 1;
						readindex += 1;
					}	
				}
				else if(opt == 'I')
				{
					readindex += count;
				}
				else if(opt == 'D')
				{
					refindex += count;
				}
				else
				{
					assert(false);
				}
			} 
			
			i += 1;
		}

	}

	fp_detail << endl;
	for(i = 0; i < reference.length(); i++)
	{
		fp_detail << "Showing Analysis for Index = " << i << endl;
		if(error_correction[i].mismatch + error_correction[i].matching_base == 0)
			continue;

		for(k = 0; k < ILLUMINA_SCORE; k++)
		{
			if(error_correction[i].qscore[k] == 0)
				continue;

			exponent = pow(10, -1 * k *  0.1);
			fp_detail << "QSCORE = " << k << ", and COUNT = " << error_correction[i].qscore[k] << ", and Exponent = " << exponent << endl;
			error_correction[i].expected_mismatch += (error_correction[i].qscore[k] * exponent);
			error_correction[i].standard_deviation += (error_correction[i].qscore[k] * exponent * (1 - exponent));
			
			fp_detail << "Expected Number of Mismatch = " << error_correction[i].expected_mismatch;
	                fp_detail << ", and Standard Deviation = " << error_correction[i].standard_deviation << endl;

		}

		error_correction[i].standard_deviation = sqrt(error_correction[i].standard_deviation);
		error_correction[i].zscore = (error_correction[i].mismatch + 0.5 - 
						error_correction[i].expected_mismatch) / 
						error_correction[i].standard_deviation;
		
		error_correction[i].pvalue = 1-c_norm(error_correction[i].zscore);
		cout << "A pvalue is: " << error_correction[i].pvalue << endl;

		fp_detail << "Error Correction At Position = " << i - fprimer_length << endl;
		fp_detail << "Mismatch = " << error_correction[i].mismatch << ", and Matching BASE = " 
				<< error_correction[i].matching_base << endl;

		fp_detail << "Expected Number of Mismatch = " << error_correction[i].expected_mismatch;
		fp_detail << ", and Standard Deviation = " << error_correction[i].standard_deviation << endl;
		fp_detail << "Finally calculated ZSCORE = " << error_correction[i].zscore << endl;
		fp_detail << "Finally p-value is = " << error_correction[i].pvalue << endl;
		fp_detail << "Fail to reject null hypothesis is = " << (error_correction[i].reject_by_bh_threshold ? "0" : "1")<< endl;
		
		fp_detail << endl;

		//if(error_correction[i].mismatch > 0)
		//	assert(false);
	}
	set_reject_flags(error_correction, reference.length());

	for(i = 0; i < reference.length(); i++){
		if(error_correction[i].mismatch + error_correction[i].matching_base == 0)
		  continue;
		condense_log << error_correction[i].mismatch << "," << error_correction[i].expected_mismatch << "," << error_correction[i].zscore;
		condense_log << "," << (error_correction[i].reject_by_bh_threshold ? "0" : "1");
		condense_log << "," << (error_correction[i].pvalue) << ",";

		condense_log << mutationStats[i]['A'] << ",";
		condense_log << mutationStats[i]['C'] << ",";
		condense_log << mutationStats[i]['G'] << ",";
		condense_log << mutationStats[i]['T'] << ",";
		condense_log << mutationStats[i]['N'] << ",";
		condense_log << "," << double((double)error_correction[i].mismatch / (double)raw_sequence_count) << endl;
		
          if(error_correction[i].reject_by_bh_threshold){
	    fp_detail << "No Correcting at position " << i << endl;
	  } else {
	    fp_detail << "Correcting at position " << i << endl;

	  }
	}
	//condense_log.close();
	fp_input.clear();
	fp_input.seekg(0, fp_input.beg);

	while(getline(fp_input, read_name))
        {
                getline(fp_input, cigar);
                getline(fp_input, alignment_string);
		getline(fp_input, quality_score);
		
		total_score = 0;
		for(i = 0; i < quality_score.length(); i++)
		{
			total_score += quality_score.at(i) - '!';
		}

		average_score = total_score / quality_score.length();
		fp_detail << endl << "Global Average Score = " << average_score << endl;

		if(average_score < GLOBAL_QSCORE)
			continue;
		
		refindex = meta_details.fprimer.length();
		i = readindex = 0;
		alignment = "";
		quality = "";
		len = "";

		fp_detail << "CIGAR = " << cigar << endl;
		while(i < cigar.length())
		{
			if(cigar.at(i) >= '0' && cigar.at(i) <= '9')
				len += cigar.at(i);
			else
			{
				opt = cigar.at(i);
				count = atoi(len.c_str());
				fp_detail << "Count = " << count << ", and Option = " << opt << endl;
				len = "";			

				fp_detail << "Reference = " << reference << endl;
				fp_detail << "Alignment = " << alignment_string << endl;

				if(opt == 'M' || opt == 'X')
				{
					for(k = 0; k < count; k++)
					{
					  	/*if(error_correction[refindex].reject_by_bh_threshold){
						  cout << "Rejectin null hypothesis whooo at " << refindex << endl;
						} else {
						  cout << "Acceptin null hypothesis =( " << refindex << endl;
						}*/
						if(alignment_string.at(readindex) != reference.at(refindex) &&
							//error_correction[refindex].zscore < ZSCORE &&
							!error_correction[refindex].reject_by_bh_threshold)
						{
							fp_detail << endl << "Error CORRECTION Point at " << readindex << endl;
							fp_detail << "ZSCORE at this point = " << error_correction[refindex].zscore << endl;
							fp_detail << "Changin " << alignment_string.at(readindex) << " To " <<
									reference.at(readindex) << endl;
						
							alignment += reference.at(refindex);
							quality += quality_score.at(readindex);

						}
						else
						{
						        fp_detail << endl << "No correction at " << refindex;
							alignment += alignment_string.at(readindex);
							quality += quality_score.at(readindex);
						}

						//fp_detail << k << "= (" << refindex << ", " << readindex << ")" << endl;

						refindex += 1;
						readindex += 1;
					}	
				}
				else if(opt == 'I')
				{
					for(k = 0; k < count; k++)
					{
						alignment += alignment_string.at(readindex);
						quality += quality_score.at(readindex);

						readindex += 1;
					}
				}
				else if(opt == 'D')
				{
					refindex += count;
				}
				else
				{
					assert(false);
				}
			} 
			
			i += 1;
		}

		detail = cigar + "\n" + alignment;
		if(umap.find(detail) == umap.end())
                {
                        //umap[alignment] = 1;
                	sequences.push_back(make_pair(read_name, detail));
			umap[detail] = 1;
                }
                else
                {
                        umap[detail] += 1;
                }

		/*
                fp_fasta << ">" << read_name;

                fp_fasta << "|DUPCOUNT=1";
                fp_fasta << "|CIGAR=" << cigar << endl;
                fp_fasta << alignment << endl;
		*/
        }



	for(i = 0; i < sequences.size(); i++)
	{
	        fp_fasta << ">" << sequences[i].first;

                fp_fasta << "|DUPCOUNT=" << umap[sequences[i].second];
                fp_fasta << "|CIGAR=" << sequences[i].second << endl;
                //fp_fasta << alignment << endl;

	}

        fp_input.close();
        fp_fasta.close();
	condense_log.close();

	return;
}