Exemple #1
0
void leds_set_RGB(uint8_t id, uint8_t r, uint8_t g, uint8_t b) {
  if (id==1) {
    eye1col=r;
    eye1col=(eye1col<<8)|(g<<4)|b;
    led_r1=calcR(r);
    led_g1=calcGB(g);
    led_b1=calcGB(b);
  } else if (id==2) {
    eye2col=r;
    eye2col=(eye2col<<8)|(g<<4)|b;
    led_r2=calcR(r);
    led_g2=calcGB(g);
    led_b2=calcGB(b);
  }
}
Exemple #2
0
double AnosimCommand::runANOSIM(ofstream& ANOSIMFile, vector<vector<double> > dMatrix, map<string, vector<int> > groupSampleMap, double alpha) {
	try {

		
		vector<vector<double> > rankMatrix = convertToRanks(dMatrix);
		double RValue = calcR(rankMatrix, groupSampleMap);
		
		int pCount = 0;
		for(int i=0;i<iters;i++){
			map<string, vector<int> > randGroupSampleMap = getRandomizedGroups(groupSampleMap);
			double RValueRand = calcR(rankMatrix, randGroupSampleMap);
			if(RValue <= RValueRand){	pCount++;	}
		}

		double pValue = (double)pCount / (double) iters;
		string pString = "";
		if(pValue < 1/(double)iters){	pString = '<' + toString(1/(double)iters);	}
		else						{	pString = toString(pValue);					}
		
		
		map<string, vector<int> >::iterator it=groupSampleMap.begin();
		m->mothurOut(it->first);
		ANOSIMFile << it->first;
		it++;
		for(it;it!=groupSampleMap.end();it++){
			m->mothurOut('-' + it->first);
			ANOSIMFile << '-' << it->first;
		
		}
		m->mothurOut('\t' + toString(RValue) + '\t' + pString);
		ANOSIMFile << '\t' << RValue << '\t' << pString;

		if(pValue < alpha){
			ANOSIMFile << "*";
			m->mothurOut("*");
		}
		ANOSIMFile << endl;
		m->mothurOutEndLine();
		
		return pValue;
	}
	catch(exception& e) {
		m->errorOut(e, "AnosimCommand", "calcAnisom");
		exit(1);
	}
}
Exemple #3
0
static void viewport_init_ra()
{
  static gboolean done_before = FALSE;
  if ( !done_before )
  {
    gint i;
    for ( i = -90; i <= 90; i++)
      Radius[i+90] = calcR ( DEG2RAD((double)i) );
    done_before = TRUE;
  }
}
Exemple #4
0
QVariant QgsNodeEditorModel::data( const QModelIndex& index, int role ) const
{
  if ( !index.isValid() ||
       ( role != Qt::DisplayRole && role != Qt::EditRole && role != MinRadiusRole && role != Qt::FontRole ) )
    return QVariant();

  if ( index.row() >= mSelectedFeature->vertexMap().count() )
    return QVariant();

  if ( index.column() >= columnCount() )
    return QVariant();

  //get QgsVertexEntry for row
  const QgsVertexEntry* vertex = mSelectedFeature->vertexMap().at( index.row() );
  if ( !vertex )
  {
    return QVariant();
  }

  if ( role == Qt::FontRole )
  {
    double r = 0;
    double minRadius = 0;
    if ( calcR( index.row(), r, minRadius ) )
    {
      QFont curvePointFont = mWidgetFont;
      curvePointFont.setItalic( true );
      return curvePointFont;
    }
    else
    {
      return QVariant();
    }
  }

  if ( role == MinRadiusRole )
  {
    if ( index.column() == mRCol )
    {
      double r = 0;
      double minRadius = 0;
      if ( calcR( index.row(), r, minRadius ) )
      {
        return minRadius;
      }
    }
    return QVariant();
  }

  if ( index.column() == 0 )
    return vertex->point().x();
  else if ( index.column() == 1 )
    return vertex->point().y();
  else if ( index.column() == mZCol )
    return vertex->point().z();
  else if ( index.column() ==  mMCol )
    return vertex->point().m();
  else if ( index.column() == mRCol )
  {
    double r = 0;
    double minRadius = 0;
    if ( calcR( index.row(), r, minRadius ) )
    {
      return r;
    }
    return QVariant();
  }
  else
  {
    return QVariant();
  }

}
RootNodeT *
buildTree(int N, MatrixT Dorig, char **taxon)
{
  
  /* 
     \section{Initialize} 
     Initialize main variables 
  */
  
  int i;
  RootNodeT *root;
  int Nleft, Nnext; /* number of Nodes left to be joined 
		       and the next index to be used */
  
  
  MatrixT b=matrix(N);      /* the $b_{i;j}$ matrix (eq 0.7) */ 
  
  /* $q(i)$ array: value which minimizes $R(i,q(i),j)\,\forall j\ne i,q(i)$ */
  int *q;
  
  int *q2;                  /* Second best value */ 
  VectorT R=vector(N);      /* $R(i,q(i))$ (eq 0.10) */
  VectorT LLR=vector(N);    /* $R(i,q(i),q2(i))$ */
  VectorT Zscore=vector(N); /* $z(i,q(i))$ */
  
  /*
    
    This auxilary matrices are globally defined in \|weighbor.h| we do
    this to make it simplier so we do not always have to pass these
    around. Note that the need to be visible here as we will be
    calling \|calcR| later in this function and \|calcR| needs these
    values
    
  */
  
  s       = matrix(N);      /* $s_{ij}$ eq 0.9 */
  deltaB  = matrix(N);      /* $\Delta b_{ij}$ eq 0.8 */
  delta2B = matrix(N);      /* $\Delta^2 b_{ij}$ */
  if(recalcB)
    oldDeltaB = matrix(N);
  
  /* 
     
     This will hold this orignal $N$ distances plus any distances from
     the $N-3$ internal nodes. Note we do not care about the root node
     so $N-3$ and not $N-2$
     
  */
  
  mD=matrix(2*N-3); 
  
  /*
    
    This is the renormalization vector $c_i$ (eq 0.39) and matrix
    $c_{i;j}$ (eq 0.43 ver0.2.5); again it must
    be large enough to hold both the original and the new joined taxa
    
    N.B. \|vector| sets all elements to zero.
    
  */
  
  vC=vector(2*N-3);
  
  
  /*
    
    This matrices hold the previous iterations values of $s_{ij}$,
    $\Delta b_{ij}$, etc. They are used to speed up the next
    iterations calcultions of these quantities.
    
  */
  
  mS     = matrix(2*N-3);
  mDelB  = matrix(2*N-3);
  mDel2B = matrix(2*N-3);
  
  /*
    
    Init \|mS| to -1 to keep track of which entries have not yet been
    computed.  */
  
  for(i=0;i<2*N-3;++i) {
    int j;
    for(j=0;j<2*N-3;++j)
      mS[i][j] = -1.0;
  }
  
  /*
    
    Make a copy of the original distance matrix; embed it in the
    larger matrix which will hold the new distance from the added
    internal nodes of the tree.
    
  */
  
  setMM(N, Dorig, mD);
  
  /*
    
    Allocate and initialize the \|q|, \|q2| and \|nodes| array. $2N-3$
    nodes to hold both the original and the added nodes.
    
  */
  
  q = (int *)malloc(N*sizeof(int));
  if(!q) printError("build::buildTree:out of memory-q\n");
  q2 = (int *)malloc(N*sizeof(int));
  if(!q2) printError("build::buildTree:out of memory-q2\n");
  
  nodes = (NodeT **)malloc( (2*N-3)*sizeof(NodeT *));
  if(!nodes) printError("build::buildTree:out of memory-nodes");
  
  for(i=0;i<N;++i) {
    nodes[i] = createNode();
    nodes[i]->name = taxon[i];
    nodes[i]->ind  = i;
  }
  
  
  Nleft = N;
  Nnext = N;
  
  /*
    
    \section{Loop until 3 taxa left}
    
    While we have more than 3 nodes left do the neighbor joining algorithm. 
    Each pass of the algorithm will join 2 nodes replacing them with one.
    
  */
  
  while(Nleft>3) {
    
    int j, k, ip, ip2;
    double minR=0.0, min2R=0.0;
    NodeT *newNode, *tmpNode;
    double sigma_inf_i, sigma_inf_ip, sigma_inf_rat;
    double sig_r, sig_l;
    int jj, jjmin;
    double LLRp=0, tR, tmp;
    
    /* \subsection{Calculate Residual} */
    
    calc_q(Nleft, q, R, b, q2, LLR, Zscore);
    
    if(printLevel>2)
      for(k=0;k<Nleft;++k)
	fprintf(outfile, "q[%d]=%d R(%d,%d)=%g\n",
		k, q[k], k, q[k], R[k]); 
    
    /*
      
      Find $i$ than minimizes $R(i,q(i))$. With the constraint that
      $q(q(i))=i$ first if no pair found then find the best $i$
      without this constraint.
      
      Note: the \|checkQQI| flag determines if we will use the
      $q(q(i))=i$ constraint.
      
      Note: j will hold the next best pair
      
    */
    
    i = -1;
    j = -1;
    
    if(checkQQI) { 
      for(k=0;k<Nleft;++k)
	if(q[q[k]]==k) {
	  if(R[k]<minR || i==-1) {
	    if(printLevel>3)
	      fprintf(outfile, 
		      "ij=%d,%d k=%d q[k]=%d minR = %.16g R[k] = %.16g\n",
		      i,j,k, q[k], minR, R[k]);
	    j = i;
	    min2R = minR;
	    i = k;
	    minR = R[k];
	  }
	  else if(R[k]>minR && (R[k]<min2R || j==-1) ) {
	    j = k;
	    min2R = R[k];
	  }
	}
    }
    
    if(i==-1) { /* No pair had $q(q(i))=i$ */
      
      if(R[0]<R[1]) {
	i = 0;
	minR = R[0];
	j = 1;
	min2R = R[1];
      } else {
	i = 1;
	minR = R[1];
	j = 0;
	min2R = R[0];
      }	    
      for(k=1;k<Nleft;++k)
	if(R[k]<minR) {
	  j = i;
	  min2R = minR;
	  i = k;
	  minR = R[k];
	}
	else if(R[k] < min2R && R[k] > minR) {
	  j = k;
	  min2R = R[k];
	}
      
      if(checkQQI && printLevel>1)
	fprintf(outfile, "No pair with q[q[i]]==i ");
      else
	if(q[q[i]]!=i && printLevel>1)
	  fprintf(outfile, 
		  "The pair does not satisfy q[q[i]]==i (checking is off)"
		  );
    }
    
    ip = q[i];
    ip2 = j;
    
    /*
      
      If the extended tournament option is set (-e) then run two 
      more tournaments for (i,q[i]) to see who really wins. 
      
    */
    
    if(extendedTourn) {
      
      double minR1=0, minR2=0, tmpR, oldR=R[i];
      int jmin=-1, jpmin=-1;
      
      /* 
	 First fine the j the minimizes R(i,j)
      */
      
      for(j=0;j<Nleft;++j) 
	if(j!=i && j!=q[i]) {
	  if(j!=q2[i])
	    tmpR = calcR2(Nleft, i, j, q2[i], b);
	  else
	    tmpR = calcR2(Nleft, i, j, q[i], b);
	  
	  if(tmpR<minR1 || jmin==-1)
	    {
	      minR1=tmpR;
	      jmin = j;
	    }
	}
      
      /* 
	 and now the $j'$ that minimizes $R(j',q[i])$
      */
      
      for(j=0;j<Nleft;++j) 
	if(j!=i && j!=q[i]) {
	  if(j!=q2[i])
	    tmpR = calcR2(Nleft, j, q[i], q2[i], b);
	  else
	    tmpR = calcR2(Nleft, j, q[i], i, b);
	  
	  if(tmpR<minR2 || jpmin==-1) {
	    minR2=tmpR;
	    jpmin = j;
	  }
	}
      
      /*
	Now fnd which of the three is the smallest
      */
      
      if(minR1<minR2 && minR1<R[i]) {
	ip = jmin;
	if(printLevel>1)
	  fprintf(outfile, 
		  "Extended Tournament New Winner(A): (%d, %d) R=%g\n",
		  i, ip, minR1);
      }
      else if(minR2<minR1 && minR2<R[i]) {
	i = jpmin;
	if(printLevel>1)
	  fprintf(outfile, 
		  "Extended Tournament New Winner(B): (%d, %d) R=%g\n",
		  i, ip, minR2);
      }	    
      
      if(printLevel>3)
	fprintf(outfile, "R=%g, R1=%g, R2=%g\n", oldR, minR1, minR2);
      
    }
    
    /*
      
      Find the $jj$ that minimizes $R(q(i),i,jj)$ and then print out 
      the LLR and LLR' values.
      
    */
    
    jjmin=-1;
    
    for(jj=0;jj<Nleft;++jj)
      if(jj!=i && jj!=ip 
	 && (((tR=calcR(Nleft, ip, jj, i))<LLRp) || jjmin==-1)) {
	jjmin = jj;
	LLRp = tR;
      }
    
    LLRp *= 0.5;
    
    if( (LLR[i]<1e-6) && (LLRp<1e-6) ) {
      if(!warnFlag) {
	fprintf(stderr, 
		"warning: tie scores encountered; topology may depend on sequence order!\n");
	warnFlag = True;
      }
      if(printLevel>1) {
	fprintf(outfile, 
		"warning: tie scores encountered; topology may depend on sequence order!\n");
	fprintf(outfile, "taxon %s and taxon %s\n\n",
		nodes[i]->name, nodes[ip]->name);
	
      }
    }
    
    if(printLevel>0) {
      fprintf(outfile, 
	      "\nJoin taxon %s to taxon %s (%s next best choice)\n",
	      nodes[i]->name, nodes[ip]->name, nodes[q2[i]]->name);
      
      
      fprintf(outfile, "     p-value = %g\n", 
	      DMAX(1.0/(exp(LLR[i])+1.0), 1.0/(exp(LLRp)+1.0)));
      
      if(printLevel>1) {
	fprintf(outfile,"\nJoin taxon %s to taxon %s; R=%g\n", 
		nodes[i]->name, nodes[ip]->name, minR);
	
	if(ip2!=-1 && ip2!=i && ip2!=ip)
	  fprintf(outfile, "Second best pair (%s, %s); R=%g\n",
		  nodes[ip2]->name, nodes[q[ip2]]->name, min2R);
	else
	  fprintf(outfile, "No second best pair\n");
      }
    }
    
    
    /* 
       
       Note due to the way we shuffle around nodes after joining:
       i->Nnext, New->i, ip<->Nleft-1, if ip is less than i and
       i=Nleft-1 then the new node will be in position ip not i!!
       But tc (the global that is suppose to point to the position
       of the new node for calcb) is set to i so this will screw us
       up. The simpliest solution is to make sure i<ip; swap if they
       are not.
       
    */
    
    
    if(ip<i) {
      int tt;
      tt=i;
      i=ip;
      ip=tt;
    }
    
    /*
      
      Need to calculate the new branch lengths $\bar b_{i;i'}$ and
      $\bar b_{i';i}$, eq. 0.19.
      
      Note if the z-score is negative then we calculate $\phi$ eq
      (0.26) and use it to renormalize $d_{i,i'}$ and recompute 
      $b_{i;i'}$ and $b_{i';i}$.
      
    */
    
    if(Zscore[i]<0.0) {
      
      double phi_iip, dBar_iip;
      
      phi_iip = calcPhi(Nleft, i, ip);
      if(printLevel>2)
	fprintf(outfile, "Renormalizing z[%d,%d] = %g\n", i, ip, Zscore[i]);
      if(phi_iip>0) {
	dBar_iip = D(i,ip)-phi_iip;
	if(printLevel>2)
	  fprintf(outfile, "phi=%g dBar_iip=%g\n", phi_iip, dBar_iip);
	
	/* renormalize the b's */
	
	if( dBar_iip >= fabs(deltaB[i][ip]) )
	  b[i][ip] = (deltaB[i][ip] + dBar_iip)/2.0;
	else if( dBar_iip < -deltaB[i][ip] )
	  b[i][ip] = 0.0;
	else
	  b[i][ip] = dBar_iip;
	
	
	if( dBar_iip >= fabs(deltaB[ip][i]) )
	  b[ip][i] = (deltaB[ip][i] + dBar_iip)/2.0;
	else if( dBar_iip < -deltaB[ip][i] )
	  b[ip][i] = 0.0;
	else
	  b[ip][i] = dBar_iip;
      }
    }
    
    nodes[i ]->rho = b[i][ip];
    nodes[ip]->rho = b[ip][i];
    
    if(nodes[i ]->rho < 0.0)  {
      if(printLevel>0)
	fprintf(outfile, 
		"WARNING: Negative branch length %g set to zero\n", 
		nodes[i ]->rho);
      nodes[i ]->rho = 0.0;
      nodes[ip]->rho = D(i,ip);
    }
    else if(nodes[ip]->rho < 0.0) {
      if(printLevel>0)
	fprintf(outfile, 
		"WARNING: Negative branch length %g set to zero\n", 
		nodes[ip]->rho);
      nodes[ip]->rho = 0.0;
      nodes[i ]->rho = D(i,ip);
    }
    
    if(printLevel>3) {
      fprintf(outfile, "\\bar b_[%d%d] = %g b_[%d%d]=%g\n",
	      i, ip, nodes[i]->rho, i, ip, b[i][ip]);
      fprintf(outfile, "\\bar b_[%d%d] = %g b_[%d%d]=%g\n\n",
	      ip, i, nodes[ip]->rho, ip, i, b[ip][i]);
    }
    
    newNode = createNode();
    
    newNode->ind = Nnext;
    newNode->child_r = nodes[i];
    newNode->child_l = nodes[ip];
    newNode->name = nodes[i]->name;
    nodes[Nnext] = newNode;
    
    /*
      
      Calculate $\sigma^2_\infty(i\bar\imath)$ (eq. 0.27) for each
      of the joined taxa.
      
    */
    
    sigma_inf_i  = 0.0;
    sigma_inf_ip = 0.0;
    
    for(j=0;j<Nleft;++j)  {
      if(j!=i && j!=ip) {
	sigma_inf_i  
	  += sigma_na(DMAX(b[i][ip],MINB)+C(i), 
		      DMAX(D(i,j)-b[i][ip],MINB)+C(j) );
	sigma_inf_ip 
	  += sigma_na(DMAX(b[ip][i],MINB)+C(ip), 
		      DMAX(D(ip,j)-b[ip][i],MINB)+C(j) );
      }
    }
    
    /*
      
      Add \|EPSILON| here to make the following formulae a bit simplier
      
    */
    
    sigma_inf_i  += EPSILON;
    sigma_inf_ip += EPSILON;
    
    
    /*
      
      Calculate the new distances from eq. 0.24
      $$
      d_{\bar\imath k} = {{(d_{ik}-b_{i;i'}+\phi_i)/\sigma^2_\infty(i\bar\imath)+
      (d_{i'j}-b_{i';i}+\phi_{i'})/\sigma^2_\infty(i'\bar\imath)}
      \over{
      {1\over\sigma^2_\infty(i'\bar\imath)} +
      {1\over\sigma^2_\infty(i'\bar\imath)}}}
      $$
      where\hfill\break
      $i=$ \|newNode->child_r->ind|,\hfill\break
      $i'=$ \|newNode->child_l->ind|,\hfill\break
      $b_{i;i'}=$ \|newNode->child_r->rho|,\hfill\break
      $b_{i';i}=$ \|newNode->child_l->rho|
      
      
      Also calcuate the renormalization terms $c_{i;j}$ (eq 0.43 ver0.2.5)
      and $c_i$
      
    */
    
    for(j=0;j<Nleft;++j)  {
      if(j!=i && j!=ip) {
	
	/* $1/\sigma^2_\infty(i\bar\imath)+1/\sigma^2_\infty(i'\bar\imath)$ */
	double norm = 
	  1.0/( 1.0/sigma_inf_i + 1.0/sigma_inf_ip);
	
	/*
	  First calcuate the new distances
	*/
	
	D(Nnext,j) = D(j,Nnext) = 
	  norm *
	  (
	   (D(i,j)-RHO(newNode->child_r))/(sigma_inf_i)
	   + 
	   (D(ip,j)-RHO(newNode->child_l))/(sigma_inf_ip)
	   );
	
	if(D(Nnext,j)<0.0)
	  D(Nnext,j) = D(j,Nnext) = 0.0;
	
      }
    }
    
    D(Nnext,Nnext) = 0.0;
    
    /*
      
      And now the new renormalization quantity $c_{\bar\imath}$
      
      N.B. eq 0.30 has been rewritten from
      $$
      {1\over{{1\over X}+{1\over Y}}}
      $$
      to
      $$
      {XY\over X+Y}
      $$
      which is better behaved numerically when $X$ or $Y$ is
      small (and cheeper since it only has one division).
      
    */
    
    sig_r = sigma2t(C(i)+DMAX(RHO(newNode->child_r), MINB));
    sig_l = sigma2t(C(ip)+DMAX(RHO(newNode->child_l), MINB));
    
    if(sigma_inf_i+sigma_inf_ip>0.0) {
      
      
      if(sigma_inf_i+sigma_inf_ip < .9*sqrt(DBL_MAX) && /* no overflow */
         sigma_inf_i+sigma_inf_ip > .9*sqrt(DBL_MIN))   /* no underflow */
	{
	  tmp=
	    (sig_r*SQR(sigma_inf_ip)+ sig_l*SQR(sigma_inf_i))
	    /
	    SQR(sigma_inf_i+sigma_inf_ip);
	}
      else if(sigma_inf_ip > sigma_inf_i)       /* to avoid over/underflow */
	{
	  sigma_inf_rat = sigma_inf_i / sigma_inf_ip;
	  tmp = 
	    sig_r*1.0+sig_l*SQR(sigma_inf_rat) 
	    /
	    SQR(1.0+sigma_inf_rat);
	}
      else
	{
	  sigma_inf_rat = sigma_inf_ip / sigma_inf_i;
	  tmp = 
	    sig_r*SQR(sigma_inf_rat)+sig_l*1.0 
	    /
	    SQR(1.0+sigma_inf_rat);
	}
      
      C(Nnext) = sigma2tinv( tmp  );
    }
    else
      C(Nnext) = sigma2tinv(0.0);
    
    /*      if(!
	    (C(Nnext)<=DMAX(DMAX(RHO(newNode->child_r),MINB)+C(i)+1e-14,
	    DMAX(RHO(newNode->child_l),MINB)+C(ip)+1e-14)))
	    {
	    printf("C(Nnext=%d)=%g\n"
	    "RHO_R=%g C(i=%d)=%g sig_r=%g\nRHO_L=%g C(ip=%d)=%g sig_l=%g -- %g\n",
	    Nnext, C(Nnext),
	    RHO(newNode->child_r), i, C(i), sig_r,
	    RHO(newNode->child_l), ip, C(ip), sig_l,
	    sig_r*sig_l/(sig_r+sig_l));
	    fflush(stdout);
	    } 
	    
	    assert((C(Nnext)<=DMAX(DMAX(RHO(newNode->child_r),MINB)+C(i)+1e-14,
	    DMAX(RHO(newNode->child_l),MINB)+C(ip)+1e-14)));
	    
    */
    
    /*
      Swap $i$ node to the empty node at the end of the list and
      place the new node in position $i$ */
    
    nodes[Nnext] = nodes[i];
    nodes[i] = newNode;
    
    /*
      Swap the $ip$ node and the last node on the list this moves
      $ip$ to the end. When we decrease \|Nleft| by one there will be
      on less node and the two joined nodes $i$ and $ip$ will now be
      after then end (\|Nleft|) of the list
    */
    
    tmpNode = nodes[ip];
    nodes[ip] = nodes[Nleft-1];
    nodes[Nleft-1] = tmpNode;
    
    /*
      In the new node set the child indecies to the
      new indexes of the the joined nodes. This info
      will be used by \|sigma2_3| in the renormalization
      step
    */
    
    newNode->cind_r=Nnext;
    newNode->cind_l=Nleft-1;
    
    /*
      Set up the \|ta|, \|tb| and \|tc| node array indices.  \|ta|
      and \|tb| point to the two taxa that where just joined, and
      \|tc| points to the newly created taxon.
      
      These globals will be used in the next call to \|calcb|.
    */
    
    ta = Nnext;
    tb = Nleft - 1;
    tc = i;
    
    --Nleft;
    ++Nnext;
    
    /* 
       Print out the values of the various variables
    */
    
    if(printLevel>2) {
      int a, b;
      fprintf(outfile, "\nReduced d_ij=\n");
      for(a=0;a<Nleft;++a)
	{
	  for(b=0;b<Nleft;++b)
	    fprintf(outfile,"%7.4g ", D(a,b));
	  fprintf(outfile,"\n");
	}
      fprintf(outfile,"\n");
    }
    
    
    if(printLevel>3) {
      int a, b;
      
      for(a=0;a<Nnext;++a) {
	for(b=0;b<Nnext;++b)
	  fprintf(outfile,"%7.4g ", mD[a][b]);
	fprintf(outfile,"\n");
      }
      fprintf(outfile,"\n");
      
      fprintf(outfile, "c_i = ");
      for(a=0;a<Nleft;++a) {
	fprintf(outfile,"%7.4g ", C(a));
      }
      fprintf(outfile,"\n");
      
      for(a=0;a<Nnext;++a) {
	fprintf(outfile,"%7.4g ", vC[a]);
      }
      fprintf(outfile,"\n");
      
      
      fprintf(outfile, "\n");
    }
  }	    
  
  /*
    
    \section{Final three taxa}
    
    Now there are just three taxa left. They will join to the root
    node of our tree. Find their branch lengths (which we can do
    exactly) and set up the root node to be passed back on return from
    this functin.
    
  */
  
  root = createRootNode();
  if(!root) printError("build::buildTree:out of memory-root");
  
  root->child_l = nodes[0];
  root->child_m = nodes[1];
  root->child_r = nodes[2];
  
  /*
    
    Now get the root branch lengths. We can solve this exactly since
    we have three equations and three unknows. The equations to solve
    are:
    $$
    \rho_0+\rho_1 = d_{01},
    \rho_0+\rho_2 = d_{02},
    \rho_1+\rho_2 = d_{12}
    $$
    And the solution is:
    $$
    \rho_0={1 \over 2}\left(d_{01}+d_{02}-d_{12}\right),
    \rho_1={1 \over 2}\left(d_{01}-d_{02}+d_{12}\right),
    \rho_2={1 \over 2}\left(-d_{01}+d_{02}+d_{12}\right)
    $$
    
  */
  
  root->child_l->rho = 0.5*( D(0,1)+D(0,2)-D(1,2));
  root->child_m->rho = 0.5*( D(0,1)-D(0,2)+D(1,2));
  root->child_r->rho = 0.5*(-D(0,1)+D(0,2)+D(1,2));
  
  /* check for negative lengths and set to zero if found and decrease
     the other each by half the the negative length (note + a neg
     number is a decrease) */
  
  if(root->child_l->rho < 0.0) {
    root->child_m->rho += 0.5*root->child_l->rho;
    root->child_r->rho += 0.5*root->child_l->rho;
    root->child_l->rho=0.0;
  }
  
  if(root->child_m->rho < 0.0) { 
    root->child_l->rho += 0.5*root->child_m->rho;
    root->child_r->rho += 0.5*root->child_m->rho;
    root->child_m->rho=0.0;
  }
  if(root->child_r->rho < 0.0) {
    root->child_l->rho += 0.5*root->child_r->rho;
    root->child_m->rho += 0.5*root->child_r->rho;
    root->child_r->rho=0.0;
  }
  
  /*
    Clean up
  */
  
  freeMatrix(mD);
  
  freeMatrix(b);
  freeMatrix(delta2B);
  freeMatrix(deltaB);
  if(recalcB)
    freeMatrix(oldDeltaB);
  freeMatrix(s);
  
  freeVector(R);
  freeVector(LLR);
  freeVector(Zscore);
  
  freeVector(vC);
  
  freeMatrix(mS);
  freeMatrix(mDelB);
  freeMatrix(mDel2B);
  
  free(nodes);
  free(q);
  free(q2);
  
  return(root);
  
}
Exemple #6
0
QVariant QgsVertexEditorModel::data( const QModelIndex &index, int role ) const
{
  if ( !index.isValid() || !mLockedFeature ||
       ( role != Qt::DisplayRole && role != Qt::EditRole && role != MIN_RADIUS_ROLE && role != Qt::FontRole ) )
    return QVariant();

  if ( index.row() >= mLockedFeature->vertexMap().count() )
    return QVariant();

  if ( index.column() >= columnCount() )
    return QVariant();

  //get QgsVertexEntry for row
  const QgsVertexEntry *vertex = mLockedFeature->vertexMap().at( index.row() );
  if ( !vertex )
  {
    return QVariant();
  }

  if ( role == Qt::FontRole )
  {
    double r = 0;
    double minRadius = 0;
    QFont font = mWidgetFont;
    bool fontChanged = false;
    if ( vertex->isSelected() )
    {
      font.setBold( true );
      fontChanged = true;
    }
    if ( calcR( index.row(), r, minRadius ) )
    {
      font.setItalic( true );
      fontChanged = true;;
    }
    if ( fontChanged )
    {
      return font;
    }
    else
    {
      return QVariant();
    }
  }

  if ( role == MIN_RADIUS_ROLE )
  {
    if ( index.column() == mRCol )
    {
      double r = 0;
      double minRadius = 0;
      if ( calcR( index.row(), r, minRadius ) )
      {
        return minRadius;
      }
    }
    return QVariant();
  }

  if ( index.column() == 0 )
    return vertex->point().x();
  else if ( index.column() == 1 )
    return vertex->point().y();
  else if ( index.column() == mZCol )
    return vertex->point().z();
  else if ( index.column() == mMCol )
    return vertex->point().m();
  else if ( index.column() == mRCol )
  {
    double r = 0;
    double minRadius = 0;
    if ( calcR( index.row(), r, minRadius ) )
    {
      return r;
    }
    return QVariant();
  }
  else
  {
    return QVariant();
  }

}
Exemple #7
0
int main(void)
{
    // C MUST BE PRIME to ensure that it is co-prime with other values
	const BYTE C = 0x07;
    
    // D MUST be assigned the same value as Y
	BYTE D = 0x00;

    // Variable for the X, Y, etc. values calculated by the recipient
    BYTE calculated_X = 0x00;
    BYTE calculated_Y = 0x00;
    BYTE calculated_Q = 0x00;
    BYTE calculated_R = 0x00;
    BYTE calculated_Z = 0x00;
    BYTE calculated_Zprime = 0x00;
	
    bool root_found = false;
    
    BYTE Q = 0x00;
    BYTE R = 0x00;
    
    BYTE Z1 = 0x00;
    BYTE Z2 = 0x00;
    
    BYTE sender_Z1prime = 0x00;
    BYTE sender_Z2prime = 0x00;
    
    BYTE recipient_Zprime = 0x00;
    
    // Iterate through every possible combination of the X and Y byte values to verify
    // that it is possible to recover X and Y from Z given ANY combinations of values
    for (BYTE X = 0x00; X < 0x10; ++X)
    {
        // For each value of X iterate for each value of Y incremented by 0x01
        for (BYTE Y = 0x00; Y < 0x10; ++Y)
        {
            /**
             * Simulate the sender compressing the 2 bytes into a single byte, Z and
             * then sending Z to the recipient
             */
            D = Y;  // D MUST be same value as Y, D is not known to recipient
            Q = calcQ(&X, &Y, &C);
            R = calcR(&X, &Y, &D);
            
            // Calculate Z using both equivalent methods, Z = R XOR C = Q XOR D
            Z1 = calcZ(&R, &C);
            Z2 = calcZ(&Q, &D);
            
            // Calcualte Z' using both equivalent methods, Z' = R XNOR C = Q XNOR D
            sender_Z1prime = calcZprime(&R, &C);
            sender_Z2prime = calcZprime(&Q, &D);
            
            // Verify that Z using both methods is equivalent (This is an AXIOM OF THE
            // COMPRESSION ALGORITHM) so if it's wrong we have a major flaw
            if (Z1 != Z2)
            {
                printf("Z1 NOT EQUIVALENT TO Z2!\n");
                printf("SENDER X, Y:\t"BYTETOBINARYPATTERN", "BYTETOBINARYPATTERN"\t: %d, %d\n", BYTETOBINARY(X), BYTETOBINARY(Y), X, Y);
                printf("Q:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ Q)), 0xF0 ^ Q); 
                printf("R:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ R)), 0xF0 ^ R);
                printf("Z1:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ Z1)), 0xF0 ^ Z1);
                printf("Z2:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ Z2)), 0xF0 ^ Z2);
            }
            
            // Verify that Z' using both methods is equivalent (This is an AXIOM OF THE
            // COMPRESSION ALGORITHM) so if it's wrong we have a major flaw           
            if (sender_Z1prime != sender_Z2prime)
            {
                printf("Z1 PRIME NOT EQUIVALENT TO Z2 PRIME!\n");
                printf("SENDER X, Y:\t"BYTETOBINARYPATTERN", "BYTETOBINARYPATTERN"\t: %d, %d\n", BYTETOBINARY(X), BYTETOBINARY(Y), X, Y);
                printf("Q:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ Q)), 0xF0 ^ Q); 
                printf("R:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ R)), 0xF0 ^ R);
                printf("Z1 PRIME:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ sender_Z1prime)), 0xF0 ^ sender_Z1prime);
                printf("Z2 PRIME:\t\t"BYTETOBINARYPATTERN", %d\n\n", BYTETOBINARY((0xF0 ^ sender_Z2prime)), 0xF0 ^ sender_Z2prime);            
            }
            
               
            /**
             * Simulate the recipient receiving a single byte, Z and getting the original
             * bytes X and Y from Z, the recipient knows the constant value C, which is prime
             */
             // The user can get Z' which is ~Z, this is the UNIQUE property that makes it 
             // possible to recover X and Y by using the equations of for Z and Z' given the
             // two values Z and Z' = ~Z
             recipient_Zprime = ~(Z1);
             
             // Verify that the Zprime the recipient gets from ~Z is ACTUALLY, the same Zprime
             // that the sender calculated using the equations (This is an AXIOM OF THE
             // COMPRESSION ALGORITHM), so if it's wrong we have a major flaw
            if (recipient_Zprime != sender_Z1prime)
            {
                printf("RECIPIENT'S Z PRIME NOT EQUIVALENT TO SENDER'S Z PRIME!!!\n");
                printf("SENDER X, Y:\t"BYTETOBINARYPATTERN", "BYTETOBINARYPATTERN"\t: %d, %d\n", BYTETOBINARY(X), BYTETOBINARY(Y), X, Y);
                printf("Q:\t\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ Q)), 0xF0 ^ Q); 
                printf("R:\t\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ R)), 0xF0 ^ R);
                printf("SENDER Z PRIME:\t\t"BYTETOBINARYPATTERN", %d\n\n", BYTETOBINARY(sender_Z1prime), sender_Z1prime);
                printf("RECIPIENT Z PRIME:\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY(recipient_Zprime), recipient_Zprime);
            }

            // Now the user can calculate X very easily given a unique property when C is prime and D == Y
            // The user has RECEIVED Z and BOTH recipient and sender KNOW C, UNKNOWN is Y == D
            calculated_X = calcX(&Z1, &C);
            
            // Verify that the CALCULATED X IS ACTUALLY EQUAL to the Sender's X value the recipients' 
            // calculated X should ALWAYS be equivalent to the sender's X when Y == D (This is an AXIOM OF THE
            // COMPRESSION ALGORITHM), so if it's wrong we have a major flaw
            if (calculated_X != X)
            {
                printf("RECIPIENT'S CALCULATED X NOT EQUIVALENT TO SENDER'S X!!!\n");
                printf("Q:\t\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ Q)), 0xF0 ^ Q); 
                printf("R:\t\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ R)), 0xF0 ^ R);
                printf("SENDER X, Y:\t"BYTETOBINARYPATTERN", "BYTETOBINARYPATTERN"\t: %d, %d\n", BYTETOBINARY(X), BYTETOBINARY(Y), X, Y);
                printf("RECIPIENT X:\t"BYTETOBINARYPATTERN"\t\t: %d\n\n", BYTETOBINARY(calculated_X), calculated_X);
            }
            
            /**
            calculated_Y = 0x00;
            calculated_Q = 0x00;
            calculated_R = 0x00;
            calculated_Z = 0x00;
            calculated_Zprime = 0x00;
            root_found = false;
            
            calculated_Q = calcQ(&calculated_X, &Y, &C);
            calculated_R = calcR(&calculated_X, &Y, &Y);

            calculated_Z = calcZ(&calculated_R, &C);
            calculated_Zprime = calcZprime(&calculated_Q, &Y);
            
            calculated_Y = calcY(&calculated_Zprime, &calculated_Q);

            printf("SENDER Q:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ Q)), 0xF0 ^ Q); 
            printf("CALCULATED Q:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ calculated_Q)), 0xF0 ^ calculated_Q);
            printf("SENDER R:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ R)), 0xF0 ^ R);
            printf("CALCULATED R:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ calculated_R)), 0xF0 ^ calculated_R);
            printf("SENDER Z:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ Z1)), 0xF0 ^ Z1);
            printf("CALCULATED Z:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ calculated_Z)), 0xF0 ^ calculated_Z);
            printf("SENDER Z PRIME:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY(sender_Z1prime), sender_Z1prime);
            printf("RECIPIENT Z PRIME:\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY(recipient_Zprime), recipient_Zprime);
            printf("CALCULATED Z PRIME:\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY(calculated_Zprime), calculated_Zprime);
            printf("SENDER X, Y:\t"BYTETOBINARYPATTERN", "BYTETOBINARYPATTERN"\t: %d, %d\n", BYTETOBINARY(X), BYTETOBINARY(Y), X, Y);
            printf("RECIPIENT X, Y:\t"BYTETOBINARYPATTERN", "BYTETOBINARYPATTERN"\t: %d, %d\n\n\n", BYTETOBINARY(calculated_X), 
                    BYTETOBINARY(calculated_Y), calculated_X, calculated_Y);
            */
            // Now solve for the unknown value Y, given the Z' value, X, and C, the only way to feasibly solve
            // for Y is to perform a bruteforce search given the known values and the equations for
            // Z' and Q. THERE MUST ONLY BE ONE ROOT, Y, GIVEN THE ROOT X , Z' and C (This is an AXIOM OF THE
            // COMPRESSION ALGORITHM), so if it's wrong we have a major flaw, in fact its IMPOSSIBLE to
            // reverse the compression unless Y == D is UNIQUE!!!!
            for (BYTE Y_TEST = 0x00; Y_TEST < 0x10; ++Y_TEST)
            {
                calculated_Q = calcQ(&calculated_X, &Y_TEST, &C);
                calculated_R = calcR(&calculated_X, &Y_TEST, &Y_TEST);
                
                calculated_Z = calcZ(&calculated_R, &C);
                calculated_Zprime = calcZprime(&calculated_Q, &Y_TEST);
                
                //printf("Calculated X: %d\n", calculated_X);
                //printf("Calculated Y: %d\n", Y_TEST);
                //printf("Calculated Q: %d\n", 0xF0 ^ calculated_Q);
                //printf("Calculated R: %d\n", 0xF0 ^ calculated_R);
                //printf("Calculated Z: %d\n",  0xF0 ^ calculated_Z);
                //printf("Calculated Z PRIME: %d\n\n", calculated_Zprime);
                
                /**
                printf("SENDER Q:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ Q)), 0xF0 ^ Q); 
                printf("CALCULATED Q:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ calculated_Q)), 0xF0 ^ calculated_Q);
                printf("SENDER R:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ R)), 0xF0 ^ R);
                printf("CALCULATED R:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ calculated_R)), 0xF0 ^ calculated_R);
                printf("SENDER Z:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ Z1)), 0xF0 ^ Z1);
                printf("CALCULATED Z:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ calculated_Z)), 0xF0 ^ calculated_Z);
                printf("SENDER Z PRIME:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY(sender_Z1prime), sender_Z1prime);
                printf("RECIPIENT Z PRIME:\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY(recipient_Zprime), recipient_Zprime);
                printf("CALCULATED Z PRIME:\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY(calculated_Zprime), calculated_Zprime);
                printf("SENDER X, Y:\t"BYTETOBINARYPATTERN", "BYTETOBINARYPATTERN"\t: %d, %d\n", BYTETOBINARY(X), BYTETOBINARY(Y), X, Y);
                printf("RECIPIENT X, Y:\t"BYTETOBINARYPATTERN", "BYTETOBINARYPATTERN"\t: %d, %d\n\n\n", BYTETOBINARY(calculated_X), 
                    BYTETOBINARY(Y_TEST), calculated_X, Y_TEST);
                */
                
                // If the calculated Z' given the calculated root Y matches the known Z' which is 
                // Z' = ~Z, then the root Y satisfies the equations and IS THE ROOT. Remember that
                // for the sender as well as the recipient Y == D, this is the unique property that
                // makes it possible to avoid the 0000/1111 cancellations of XOR              
                if (calculated_X == calcX(&calculated_Z, &C))
                {
                    if (Y_TEST == calcY(&calculated_Zprime, &calculated_Q))
                    {
                        if (Z1 == calculated_Z && recipient_Zprime == calculated_Zprime)
                        {
                            // X XOR Y SHOULD NOT BE EQUIVALENT TO Z, ONLY THE UNIQUE ROOT IS NOT A COMBINATION
                            // OF X XOR Y = Z
                            //if (Y_TEST == Y)
                            if ((calculated_X ^ Y_TEST) != calculated_Z)
                            {
                                calculated_Y = Y_TEST;
                                root_found = true;
                                break;
                            }
                        }
                    }
                }
            }
            // If the correct root is found I will be DELIGHTED, the calculated Y MUST
            // be UNIQUE and equivalent to the initial Y value of the sender, if it does
            // not match we have a major flaw,
            if (calculated_Y == Y)
            {
                printf("RECIPIENT'S CALCULATED Y EQUIVALENT TO SENDER'S Y!!!!!!!!!!!!!!!!!!!!\n");
                printf("SENDER Q:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ Q)), 0xF0 ^ Q); 
                printf("CALCULATED Q:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ calculated_Q)), 0xF0 ^ calculated_Q);
                printf("SENDER R:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ R)), 0xF0 ^ R);
                printf("CALCULATED R:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ calculated_R)), 0xF0 ^ calculated_R);
                printf("SENDER Z:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ Z1)), 0xF0 ^ Z1);
                printf("CALCULATED Z:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ calculated_Z)), 0xF0 ^ calculated_Z);
                printf("SENDER Z PRIME:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY(sender_Z1prime), sender_Z1prime);
                printf("RECIPIENT Z PRIME:\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY(recipient_Zprime), recipient_Zprime);
                printf("CALCULATED Z PRIME:\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY(calculated_Zprime), calculated_Zprime);
                printf("SENDER X, Y:\t"BYTETOBINARYPATTERN", "BYTETOBINARYPATTERN"\t: %d, %d\n", BYTETOBINARY(X), BYTETOBINARY(Y), X, Y);
                printf("RECIPIENT X, Y:\t"BYTETOBINARYPATTERN", "BYTETOBINARYPATTERN"\t: %d, %d\n\n", BYTETOBINARY(calculated_X), 
                    BYTETOBINARY(calculated_Y), calculated_X, calculated_Y);
            }
            // major flaw, back to the drawing board!
            else
            {
                printf("RECIPIENT'S CALCULATED Y NOT EQUIVALENT TO SENDER'S Y!!!!!!!!!!!!!!!!!!!!\n");
                printf("SENDER Q:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ Q)), 0xF0 ^ Q); 
                printf("CALCULATED Q:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ calculated_Q)), 0xF0 ^ calculated_Q);
                printf("SENDER R:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ R)), 0xF0 ^ R);
                printf("CALCULATED R:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ calculated_R)), 0xF0 ^ calculated_R);
                printf("SENDER Z:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ Z1)), 0xF0 ^ Z1);
                printf("CALCULATED Z:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ calculated_Z)), 0xF0 ^ calculated_Z);
                printf("SENDER Z PRIME:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY(sender_Z1prime), sender_Z1prime);
                printf("RECIPIENT Z PRIME:\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY(recipient_Zprime), recipient_Zprime);
                printf("CALCULATED Z PRIME:\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY(calculated_Zprime), calculated_Zprime);
                printf("SENDER X, Y:\t"BYTETOBINARYPATTERN", "BYTETOBINARYPATTERN"\t: %d, %d\n", BYTETOBINARY(X), BYTETOBINARY(Y), X, Y);
                printf("RECIPIENT X, Y:\t"BYTETOBINARYPATTERN", "BYTETOBINARYPATTERN"\t: %d, %d\n\n", BYTETOBINARY(calculated_X), 
                    BYTETOBINARY(calculated_Y), calculated_X, calculated_Y);
            }
        }
            /**
            Zprime = calcZprime(&Q, &D);
            
            calculated_Y = calcY(&Zprime, &Q);

            if ( X == calculated_X && Y == calculated_Y)
            {
            printf("Q:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ Q)), 0xF0 ^ Q); 
            printf("R:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ R)), 0xF0 ^ R);
            printf("Z1:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ Z1)), 0xF0 ^ Z1);
            printf("Z2:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY((0xF0 ^ Z2)), 0xF0 ^ Z2);

            );
            //printf("Z':\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY(Zprime), Zprime);

            printf("Q:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY(Q), Q); 
            printf("R:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY(R), R);
            printf("Z:\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY(Z), Z);
            printf("Z':\t\t"BYTETOBINARYPATTERN", %d\n", BYTETOBINARY(Zprime), Zprime);

            printf("INITIAL X, Y:\t"BYTETOBINARYPATTERN", "BYTETOBINARYPATTERN"\t: %d, %d\n", BYTETOBINARY(X), BYTETOBINARY(Y), X, Y);
            printf("SOLVED X, Y:\t"BYTETOBINARYPATTERN", "BYTETOBINARYPATTERN"\t: %d, %d\n\n", 
            BYTETOBINARY(calculated_X), BYTETOBINARY(calculated_Y), calculated_X, calculated_Y);
            }*/
    }
	return 0;
}
void
calc_q(int N, int *q, VectorT R, MatrixT b, 
       int* q2, VectorT LLR, VectorT Zscore)
{
  
  int i, j, jwin;
  double Rmin = -1.0; /* best R */ 
  
  /* for best speed I think we could omit the following (test it first!)
     2 variables.  Without them different second best values are
     printed with -v, but who cares? */
  double R2ndmin = -1.0; /* 2ndbest R */
  double R3rdmin = -1.0; /* 3rdbest R ; note first 2 might be the same */
  
  VectorT z       = vector(N); /* $z(i,q(i))$ */
  
  /*
    First compute $b_{i;j}$, $\Delta b_{ij}$, $\Delta^2b_{ij}$,
    $s_{ij}$ */
  
  calc_b(N, b, deltaB, delta2B, s);
  
  if(recalcB)
    {
      /* 
	 
	 This is the original $\Delta b_{ij}$. The one before the
	 recalculation using the new sigma's. We need to save this for the
	 proper calculation of $s''_{ij}$ which also uses the new sigma's.
	 
      */
      
      /*
	Save the original value of \|deltaB| for use in calculating $s''$
	and $\Delta b''$
      */
      
      setMM(N,deltaB,oldDeltaB);
      
      /* 
	 Now recompute $b_{i;j}$, $\Delta b_{ij}$, $\Delta^2b_{ij}$,
	 $s_{ij}$ using the previously calculted values of $\Delta b_{ij}$
	 and the new noise function.
      */
      
      recalc_b(N, b, deltaB, delta2B, s);
    }
  
  /* 
     
     First calculate the $q(i)$ array using equation 0.11 to run a
     single elmination tournament among the $j$'s 
     
  */
  
  
  if(printLevel>3)
    fprintf(outfile,"args:\n");
  
  for(i=0;i<N;++i)
    {
      
      double minR=0, tmpR;
      int j2win=-1;
      
      if(i!=0)
	jwin = 0;
      else
	jwin = 1;
      for(j=0;j<N;++j)
	if(j!=i && j!=jwin)
	  {
	    if(calcR(N,i,j,jwin)<0.0)
	      jwin = j;
	  }
      
      if(printLevel>3) {
	fprintf(outfile,"basics:\n");
	fprintf(outfile,"%f %f \n",calcR(N,0,2,1),calcR(N,0,2,3));
      }
      
      /*
	Now make sure the \|jwin| is really the winner
      */
      
      
      for(j=0;j<N;++j) 
	if(j!=i && j!=jwin && (((tmpR=calcR(N,i,j,jwin))<minR) || j2win==-1))
	  {
	    minR = tmpR;
	    j2win = j;
	  }
      
      if(minR<0)
	{
	  if(printLevel>1) {
	    fprintf(outfile, "WARNING: non-transitive R(i,j,j'); tree could depend on input order\n");	    
	    fprintf(outfile, "New winner %d (orig winner was %d)\n\n", 
		    j2win, jwin);
	  }
	  
	  if(!warnFlag) {
	    fprintf(stderr, "WARNING: non-transitive R(i,j,j'); tree could depend on input order\n");
	    warnFlag=True;
	  }
	  
	  /* New winner swap, jwin and j2win */
	  
	  q[i] = j2win;
	  q2[i] = jwin;
	}
      else
	{
	  
	  q[i] = jwin;
	  q2[i] = j2win;
	  
	  if(minR==0.0) { 
	    if(printLevel>1) {
	      fprintf(outfile, "WARNING: Tie in R(i,j,j'); tree likely to depend on input order\n");
	      fprintf(outfile, "Tie %d==%d\n\n", j2win, jwin);
	    }
	    
	    if(!warnFlag) {
	      fprintf(stderr, "WARNING: Tie in R(i,j,j'); tree likely to depend on input order\n");
	      warnFlag=True;
	    }
	  }
	  
	  
	}
      
      if(printLevel>2)
	fprintf(outfile, "i=%d q[i]=%d q2[i]=%d minR=%f\n",
		i, q[i], q2[i], minR);
      
      
      /*
	
	The \|LLR| array holds the 3 index R values for the winning
	pair and the second best taxon.
	
      */
      
      
      LLR[i] = -0.5*calcR(N,i,q[i],q2[i]);
      
    }
  
  
  /* 
     
     now calculate $z(i,q(i))$ and then the full residule (eq 0.10)
     
     Two different methods for calculating $z(i,j)$ choose the old one 
     if the \|oldZflag| is set else use the newer ($N^2$) method.
     
  */
  
  
  for(i=0;i<N;++i)
    {
      double arg;
      double lnerfc;
      
      if(!x_Flag) {
	R[i] = 
	  s[i][q[i]]*(delta2B[i][q[i]]-SQR(deltaB[i][q[i]]))/(((double)N)-3.0) ;
	
      } else {
	R[i] = 
	  2.0*s[i][q[i]]*(delta2B[i][q[i]]-SQR(deltaB[i][q[i]]))
	  /(((double)N)-2.0) ;
      }
      
      /* only do z if R looks ok so far */
      if(R[i] < R3rdmin || R3rdmin==-1.0)
	{
	  if(!oldZflag)
	    calc_z2(N, z, q, q2, b, i);
	  else
	    calc_z1(N, z, q, i);
	  /*
	    
	    Finally calculate $R(i,q(i))$ eq 0.10
	    
	  */
	  
	  arg = -z[i]*M_SQRT1_2;
	  
	  
	  /*
	    Save \|Zscore| for use in \|build.c|
	  */
	  Zscore[i] = z[i];
	  
	  if(arg>0.0)
	    lnerfc = log(0.5*derfcx(arg)) - arg*arg;
	  else
	    lnerfc = log(0.5*derfc(arg));
	  
	  R[i] -= 
	    2.0*lnerfc;
	  
	  
	  if(R[i] < R3rdmin || R3rdmin==-1.0) R3rdmin = R[i];
	  if(R[i] < R2ndmin || R2ndmin==-1.0) {R3rdmin = R2ndmin; R2ndmin = R[i];}
	  if(R[i] < Rmin || Rmin==-1.0) {R2ndmin = Rmin; Rmin=R[i];}
	  
	  if(printLevel>2)
	    fprintf(outfile, "z(%d,%d)=%g\n", i, q[i], z[i]); 
	}	  
      
      
    }
  
  if(printLevel>0)
    fprintf(outfile, "\n");
  
  freeVector(z);
  
}