ll findAnswer(ll n,ll k,ll counter)
{
	ll sum=0,i,m;
	m=n<k?n:k;
	if(counter==0)
	{
		if(k>n)
			return p[2,n-1];
		if(k&1)
		{
			for(i=1;i<=m;i+=2)
				sum= (sum+ncr(n,i))%MOD;
		}
		else
		{
			for(i=0;i<=m;i+=2)
				sum= (sum+ncr(n,i))%MOD;		
		}
	}
	else
	{
		if(k>n)
			return p[2,n];
		for(i=0;i<=m;i++)
			sum= (sum+ncr(n,i))%MOD;
	}
	return sum;
}
Beispiel #2
0
void printBinomialExpansion(int a, int b, unsigned int n)
{
    int r;
    long int result = 0;
    
    printf("\n = ");
    for (r = 0; r <= n; r++)
    {
        printf("%dC%d * %d^%d * %d^%d", n, r, a, n - r, r);
        if (r != n) printf(" + ");
    }
    
    printf("\n = ");
    for (r = 0; r <= n; r++)
    {
        printf("%d * %d * %d", ncr(n, r), power(a, n - r), power(b, r));
        if (r != n) printf(" + ");
    }
        
    printf("\n = ");
    for (r = 0; r <= n; r++)
    {
        result += ncr(n, r) * power(a, n - r) * power(b, r);
        printf("%d", ncr(n, r) * power(a, n - r) * power(b, r));
        if (r != n) printf(" + ");
    }
    
    printf("\n = %d", result);
}
Beispiel #3
0
// table with lowest probability can be constructed by sorting columns by column sums and packing the 
double pvalue_contable_iter(int** ctable,int size,int iter,double** NCRtable,int** newtable,int fast, struct acounts* atable,int st)
{
	int ones = 0; int reads = 0; int  i=0; double p=0;
	double cl =0;
	for (i=0;i<size;i++) cl += ncr(ctable[i][0+st],ctable[i][1+st]);
	int maxcol = 0;
	for (i=0;i<size;i++) 
	{  
		ones += ctable[i][1+st]; reads += ctable[i][0+st]; 
		if (ctable[i][0+st] > maxcol) maxcol = ctable[i][0+st];
	}

	if (ones < 70 && maxcol < MAXN && size >= 2 && size < 8)
	{
		//	printf("computing p-value using exact method %d %d \n",reads,ones);
		p = compute_pvalue_exact(ctable,size,0,reads,ones,cl,NCRtable,st);
		//printf("%f %d %d %f %f \n",cl,reads,ones,p,ncr(reads,ones));  
		return pow(10,p-ncr(reads,ones));
	}
	else
	{
		//	printf("computing p-value using permutation method %d %d \n",reads,ones);
		p= compute_pvalue_permutation(ctable,size,cl,iter,newtable,fast,atable,st);
		return p;
	}
}
int main()
{
	long long int t,i,j,a[4],n;
	scanf("%lld",&t);
	while(t--)
	{
		for(i=0;i<4;i++)
			scanf("%lld",&a[i]);
		scanf("%lld",&n);
		dp[0][0]=0;
		for(i=0;i<=n;i++)
		{
			for(j=0;j<=n;j++)
			{
				if(j>i)
					dp[i][j]=dp[i][j-1];
				else if(i%j==0)
					dp[i][j]=dp[i-j][j-1]*f(j,a)+ncr(i+f(i,a)-1,f(i,a));
				else
					dp[i][j]=dp[i-j][j-1]*f(j,a);
			}
		}
		printf("%lld",dp[n][n]%1000000007);
	}	
	return 0;
}
Beispiel #5
0
u64 ncr(u64 n, u64 r)
{
    if (r == 0)
    {
        return 1;
    }
    return n * ncr(n - 1, r - 1) / r;
}
int main(){
  int a,b,c,d,output_ncr,output_npr;
  scanf("%d %d %d %d",&a,&b,&c,&d);
  output_ncr=ncr(a,b);
  output_npr=npr(c,d);
  printf("%d",(output_ncr-output_npr));
   return 0;
}
int main()
{	int t;
	long int n,k,x,i;
	long long int ans,temp;
	scanf("%d",&t);
	while(t--)
	{	ans=temp=1;
		scanf("%ld %ld",&n,&k);
		x=n-k;
		printf("%Ld\n",ncr(n-1,x));
	}
	return 0;
}
void AsciiComponentClass::createRange(unichar to) {
    assert(range_start <= 0xff);
    unsigned char from = (u8)range_start;
    if (from > to) {
        throw LocatedParseError("Range out of order in character class");
    }

    in_cand_range = false;
    CharReach ncr(from, to);
    if (mode.caseless) {
        make_caseless(&ncr);
    }
    cr |= ncr;
    range_start = INVALID_UNICODE;
}
/* ****************************************************************************
*
* Constructor -
*/
TEST(NotifyContextResponse, Constructor)
{
  StatusCode sc(SccOk, "2");
  NotifyContextResponse ncr(sc);

  utInit();

  EXPECT_EQ(SccOk, ncr.responseCode.code);
  ncr.release();

  StatusCode ec(SccOk, "4");
  NotifyContextResponse ncr2(ec);
  EXPECT_EQ(SccOk, ncr2.responseCode.code);

  utExit();
}
Beispiel #10
0
void printsolution(float **sols,int m,int n,int *type,float *optim,int max)
{
  int i =0,j,maxc=0,num=0;
  float zz=0;
  int comb = ncr(n,n-m);
  printf("\n");
  for(;i<n;i++)
    printf("x%d\t",i+1);
  printf(" Z      Type \n");
  for(i=0;i<comb;i++)
    {
      int flag=0;
      if(type[i] == 1)
	{
	  num++;
	  for(j=0;j<n;j++)
	    {
	      if(sols[i][j] < 0)
		flag =1;
	      printf("%.2f\t",sols[i][j]);
	    }
	  float z = arraymult(sols[i],optim,n);
	  if(z > zz)
	    {
	      zz= z;
	      maxc =i;
	    }
	  printf("%.2f\t",z);
	  if(flag == 1)
	    printf("Not feasible\n");
	  else
	    printf("Feasible\n");
	}
    }
  if(num != 0)
    {
      printf("The optimal solution is \n");
      for(i=0;i<n;i++)
	printf("x%d=%f\n",i+1,sols[maxc][i]);
      printf("Z=%.2f\n",zz);
    }
  else
    {
      printf("Given system is either inconsistent or has infinite solutions\n");
    }
}
void AsciiComponentClass::add(unichar c) {
    DEBUG_PRINTF("adding \\x%02x\n", c);
    if (c > 0xff) { // too big!
        throw LocatedParseError("Hexadecimal value is greater than \\xFF");
    }

    if (in_cand_range) {
        createRange(c);
        return;
    }

    CharReach ncr(c, c);
    if (mode.caseless) {
        make_caseless(&ncr);
    }

    cr |= ncr;
    range_start = c;
}
Beispiel #12
0
// calculate permutation based p-value for variant allele counts
// general permutation that works for multiple bins and multiple alleles...
// conditional test where we permute the third allele keeping second allele constant and vice versa....
// bins should be based on strand (+/-), first/2nd read (1/2) and quality value (30+ and remaining)
// for indels, we shouldn't do quality values based bins but mapping based bins
// to speed up permutation based p-value:  stop as soon as clranew exceeds clra
int FETpvalue_variant(struct VARIANT* variant,int maxiter,double* permutationpvalue,int allele1,int allele2,int allele3,int allele4)
{
	int i=0,j=0,k=0,r=0,b=0,pvalue=0,offset=0,iter=0,R=0;
//	fprintf(stdout,"calling new permutation test alleles %d:%d:%d:%d\n",allele1,allele2,allele3,allele4);
	double clra =0, clranew=0;
	int** table_index = (int**)malloc(sizeof(int*)*12);
	// we have 12 bins (2 for each strand x (3 for QV + 3 for R12_Single)) within which we permute 
	int** newtable = calloc(variant->samples,sizeof(int*)); 
	static int allelecounts[12][4]; // for total, allele2, allele3, allele4
	double* TLL = calloc(maxiter,sizeof(double)); // random table likelihoods
	for (i=0;i<12;i++) 
	{
		for (j=0;j<4;j++) allelecounts[i][j] = 0;
	}

	for (i=0;i<variant->samples;i++) 
	{
		for (j=0;j<6;j++) 
		{
			allelecounts[j][0] += variant->indcounts_binned[i][allele1][j] + variant->indcounts_binned[i][allele2][j] + variant->indcounts_binned[i][allele3][j];
			allelecounts[j][1] += variant->indcounts_binned[i][allele2][j];
			allelecounts[j][2] += variant->indcounts_binned[i][allele3][j];
			allelecounts[j+6][0] += variant->indcounts_binned[i][allele1+maxalleles][j] + variant->indcounts_binned[i][allele2+maxalleles][j] + variant->indcounts_binned[i][allele3+maxalleles][j];
			allelecounts[j+6][1] += variant->indcounts_binned[i][allele2+maxalleles][j];
			allelecounts[j+6][2] += variant->indcounts_binned[i][allele3+maxalleles][j];
		}
		R = variant->indcounts[i][allele1]+variant->indcounts[i][allele1+maxalleles] + variant->indcounts[i][allele2]+variant->indcounts[i][allele2+maxalleles] + variant->indcounts[i][allele3]+variant->indcounts[i][allele3+maxalleles]; 
		newtable[i] = calloc(4,sizeof(int)); newtable[i][0] = R; 
		//clra += multinomial(R,variant->indcounts[i][allele2]+variant->indcounts[i][allele2+maxalleles],variant->indcounts[i][allele3]+variant->indcounts[i][allele3+maxalleles]);
		clra += ncr(R,variant->indcounts[i][allele2]+variant->indcounts[i][allele2+maxalleles]);
	}

	for (j=0;j<12;j++) table_index[j] = (int*)malloc(sizeof(int)*(allelecounts[j][0]+1));
	for (j=0;j<12;j++)
	{
		offset =0; 
		for (i=0;i<variant->samples;i++) 
		{
			if (j < 6) R = variant->indcounts_binned[i][allele1][j] + variant->indcounts_binned[i][allele2][j] + variant->indcounts_binned[i][allele3][j]; 
			else R = variant->indcounts_binned[i][allele1+maxalleles][j-6] + variant->indcounts_binned[i][allele2+maxalleles][j-6] + variant->indcounts_binned[i][allele3+maxalleles][j-6];
			for (k=0;k<R;k++) table_index[j][offset+k] =i; offset += R;
		}

	}
	for (iter=1;iter<=maxiter;iter++)
	{
		for (i=0;i<variant->samples;i++) { newtable[i][1] = 0; newtable[i][2] = 0; newtable[i][3] = 0; }
		clranew=0;
		// do the bins for quality values
		for (j=0;j<3;j++)
		{
			for (i=0;i<allelecounts[j][1];i++) 
			{
				// freedom to permute among all alleles rather than just allele1/2 // check this 
				r = (int)(drand48()*(allelecounts[j][0]-i))+i; 
				b = table_index[j][(int)r]; table_index[j][(int)r] = table_index[j][i]; table_index[j][i]=b;
				//fprintf(stdout,"clra %f %d:%d\n",clranew,newtable[i][0],newtable[b][1]);
				clranew += log10(newtable[b][0]-newtable[b][1])- log10(newtable[b][1]+1); newtable[b][1]++;
			}
			for (i=0;i<allelecounts[j+6][1];i++) 
			{
				r = (int)(drand48()*(allelecounts[j+6][0]-i))+i; 
				b = table_index[j+6][(int)r]; table_index[j+6][(int)r] = table_index[j+6][i]; table_index[j+6][i]=b;
				clranew += log10(newtable[b][0]-newtable[b][1])- log10(newtable[b][1]+1); newtable[b][1]++;
			}
		}
		if (clranew <= clra+epsilon) 	pvalue +=1;  
		TLL[iter-1] = clranew;
		if (pvalue >= 10 || (iter <= 1000 && pvalue >= 5)) break; 
	}

	//qsort(TLL,iter-1,sizeof(double),doublecomp);
	//fprintf(stdout,"\nTLL %d iter %d clra %f | ",variant->position,iter,clra);
	//for (i=0;i<iter-1;i++) fprintf(stdout,"%f ",TLL[i]); fprintf(stdout,"\n");
	// sort the TLL array, find peak of PDF, take left half of it, it is approximated as N(0,1) with mean at maxima PDF

	free(TLL);	
	for (i=0;i<12;i++) free(table_index[i]); free(table_index);
	for (i=0;i<variant->samples;i++) free(newtable[i]); free(newtable);
	//fprintf(stdout,"clra %f mean %f sq %f %d\n",clra,clrasum,clrasumsq,k+1);
	fprintf(stdout,"table %d NEWTEST:%d/%d/%f\t",variant->position,pvalue,iter,clra);
	*permutationpvalue = log10(pvalue+1)-log10(iter+1); 
	return 1;
}
Beispiel #13
0
// this works for only two strands (indels)
int FETpvalue_indels(struct VARIANT* variant,int maxiter,double* permutationpvalue,int allele1,int allele2,int allele3)
{
	int i=0,j=0,k=0,r=0,b=0,pvalue=0,offset=0,iter=0,R=0;
	double clra =0, clranew=0;
	int** table_index = calloc(2,sizeof(int*));
	int** newtable = calloc(variant->samples,sizeof(int*)); 
	static int allelecounts[2][4]; // for total, allele2, allele3, allele4
	for (i=0;i<2;i++) 
	{
		for (j=0;j<4;j++) allelecounts[i][j] = 0;
	}

	for (i=0;i<variant->samples;i++) 
	{
		allelecounts[0][0] += variant->indcounts[i][allele1] + variant->indcounts[i][allele2] + variant->indcounts[i][allele3];
		allelecounts[0][1] += variant->indcounts[i][allele2];
		allelecounts[0][2] += variant->indcounts[i][allele3];
		allelecounts[1][0] += variant->indcounts[i][allele1+maxalleles] + variant->indcounts[i][allele2+maxalleles] + variant->indcounts[i][allele3+maxalleles];
		allelecounts[1][1] += variant->indcounts[i][allele2+maxalleles];
		allelecounts[1][2] += variant->indcounts[i][allele3+maxalleles];
		R = variant->indcounts[i][allele1]+variant->indcounts[i][allele1+maxalleles] + variant->indcounts[i][allele2]+variant->indcounts[i][allele2+maxalleles] + variant->indcounts[i][allele3]+variant->indcounts[i][allele3+maxalleles]; 
		newtable[i] = calloc(4,sizeof(int)); newtable[i][0] = R; 
		//clra += multinomial(R,variant->indcounts[i][allele2]+variant->indcounts[i][allele2+maxalleles],variant->indcounts[i][allele3]+variant->indcounts[i][allele3+maxalleles]);
		clra += ncr(R,variant->indcounts[i][allele2]+variant->indcounts[i][allele2+maxalleles]);
	}

	for (j=0;j<2;j++)
	{
		table_index[j] = (int*)malloc(sizeof(int)*(allelecounts[j][0]+1));
		offset =0; 
		for (i=0;i<variant->samples;i++) 
		{
			if (j ==0) R = variant->indcounts[i][allele1] + variant->indcounts[i][allele2] + variant->indcounts_binned[i][allele3]; 
			else R = variant->indcounts[i][allele1+maxalleles] + variant->indcounts[i][allele2+maxalleles] + variant->indcounts[i][allele3+maxalleles];
			for (k=0;k<R;k++) table_index[j][offset+k] =i; offset += R;
		}

	}
	for (iter=1;iter<=maxiter;iter++)
	{
		for (i=0;i<variant->samples;i++) { newtable[i][1] = 0; newtable[i][2] = 0; newtable[i][3] = 0; }
		clranew=0;
		for (j=0;j<2;j++)
		{
			for (i=0;i<allelecounts[j][1];i++) 
			{
				// freedom to permute among all alleles rather than just allele1/2 // check this 
				r = (int)(drand48()*(allelecounts[j][0]-i))+i; 
				b = table_index[j][(int)r]; table_index[j][(int)r] = table_index[j][i]; table_index[j][i]=b;
				//fprintf(stdout,"clra %f %d:%d\n",clranew,newtable[i][0],newtable[b][1]);
				clranew += log10(newtable[b][0]-newtable[b][1])- log10(newtable[b][1]+1); newtable[b][1]++;
			}
		}
		if (clranew <= clra+epsilon) 	pvalue +=1;  
		if (pvalue >= 10 || (iter <= 1000 && pvalue >= 5)) break; 
	}

	for (i=0;i<2;i++) free(table_index[i]); free(table_index);
	for (i=0;i<variant->samples;i++) free(newtable[i]); free(newtable);
	//fprintf(stdout,"clra %f mean %f sq %f %d\n",clra,clrasum,clrasumsq,k+1);
	fprintf(stdout,"table %d NEWTEST:%d/%d/%f\t",variant->position,pvalue,iter,clra);
	*permutationpvalue = log10(pvalue+1)-log10(iter+1); 
	return 1;
}
Beispiel #14
0
int main()
{
SPList<int> il, il1, il2, il3;
SPStringList x, y, z;
int n;

    if(x) cout << "x is not empty" << endl;
    else cout << "x is empty" << endl;

    if(x.isempty()) cout << "x.isempty() is true" << endl;
    else cout << "x.isempty() is false" << endl;

    n= x.split("a b c d e f");

    if(x) cout << "x is not empty" << endl;
    else cout << "x is empty" << endl;

    if(x.isempty()) cout << "x.isempty() is true" << endl;
    else cout << "x.isempty() is false" << endl;

    cout << "x.split(a b c d e f)= " << n << ": " << x << endl;
    cout << "x[0] = " << x[0] << endl;
    z= x; z[0]= "x";
    cout << "z= x; z[0]=\"x\" " << "z: " << z << endl;

    SPString ss("1.2.3.4.5.6.7.8.9.0");
    y= ss.split("\\.");
    cout << "ss= " << ss << ", y= ss.split(\"\\.\"), y=" << endl << y << endl;
    cout << "y.join(\" \")" << y.join(" ") << endl;       
    {
        SPString xx= "a b c\nd e\tf   g";
        cout << xx << endl << "xx.split()= " << xx.split() << endl;
        xx= "a b c d e f g";
        cout << xx << endl << "xx.split(\",\")= " << xx.split(",") << endl;
        xx= "  a b c d e f g hi  ";
        cout << xx << endl << "xx.split(\"\")= " << xx.split("") << endl;
        xx= "a,b,c,d,,e,f,g,,,,";
        cout << xx << endl << "xx.split(\",\")= " << xx.split(",") << endl;
        xx= "a,b,c,d,,e,f,g,,";
        cout << xx << endl << "xx.split(\",\", 5)= " << xx.split(",", 5) << endl;
        xx= " a b c d e f g  ";
        cout << xx << endl << "xx.split(\" \")= " << xx.split(" ") << endl;
        xx= "a b c d,e,f g";
        cout << xx << endl << "xx.split(\"([ ,])+\")= " << xx.split("([ ,])+") << endl;
        xx= ",,,,";
        cout << xx << endl << "xx.split(\",\")= " << xx.split(",") << endl;
        xx= "";
        cout << xx << endl << "xx.split(\",\")= " << xx.split(",") << endl;
        xx= "   a b c\td    e\nf  g   ";
        cout << xx << endl << "xx.split(\"' '\")= " << xx.split("' '") << endl;
   }

    cout << "x = " << x << endl;

    cout << "x.pop() : " << x.pop() << ", " ;
    cout << x.pop() << endl << "x= " << x << endl;;
    cout << "x.shift() : " << x.shift() << ", ";
    cout << x.shift() << endl<< "x= " << x << endl;
    
    x.unshift(y);
    cout << "x.unshift(y): " << x << endl;
    
    if(il) cout << "il is not empty" << endl;
    else cout << "il is empty" << endl;
    
    il.push(1); il.push(2); il.push(3); il.push(4);

    if(il) cout << "il is not empty" << endl;
    else cout << "il is empty" << endl;
    
    cout << "il(1, 2, 3, 4) : " << il << endl;
    il3= il; il3[0]= 9999;
    cout << "il3= il; il3[0]= 9999; il3 = " << il3 << endl << "il= " << il << endl;

    il1= il.reverse();
    cout << "il.reverse: " << il1 << endl;

    cout << "il1.sort(): " << il1.sort() << endl;

    y.reset();
    y.push("one"); y.push("two"); y.push("three"); y.push("four");
    cout << "y = " << endl << y;
    cout << "y.reverse() " << y.reverse() << endl;
    cout << "y.sort() " << y.sort() << endl;
    cout << "y.sort().reverse() " << y.sort().reverse() << endl;
        
    il2.push(3); il2.push(4);
    cout << "il2.push(3, 4) : " << il2 << endl;

    il.push(il2);    
    cout << "il.push(il2) : " << il << endl;
    
    cout << "il.pop() : " << il.pop() << ", ";
    cout << il.pop() << endl;
    
    il.unshift(il2);    
    cout << "il.unshift(il2) : " << il << endl;
    
    cout << "il.shift() : " << il.shift() << ", ";
    cout << il.shift() << endl;

    il.reset();
    il.push(1); il.push(2);
    if(il.shift() != 1) cout << "FIFO1 error" << endl;
    if(il.shift() != 2) cout << "FIFO2 error" << endl;

    int i;
    for(i=0;i<100;i++) il.push(i);
    i= 0;
    while(il){
        if(il.shift() != i)  cout << "FIFO3 error" << endl;
        i++;
    }
    if(i != 100) cout << "FIFO over/under run" << endl;
    
    cout << "testing splice:" << endl;
    x.reset();
    x.split("a b c d e f g h i");
    cout << "x = " << x << endl;
    z= x.splice(2, 3);
    cout << "z= x.splice(2, 3): z= " << z << endl << "x = " << x << endl;
    cout << "x.splice(2, 0, z): " << x.splice(2, 0, z);
    cout << "x= " << x << endl;
    cout << "z.splice(1, 1, x): " << z.splice(1, 1, x);
    cout << "z= " << z << endl;
    cout << "x= " << x << endl;
    cout << "z.splice(20, 1, x): " << z.splice(20, 1, x);
    cout << "z= " << z << endl;
    
 // test auto expand
    SPList<int> ile;
    ile[3]= 3;
    cout << ile.scalar() << ", " << ile[3] << endl;
    ile[100]= 1234;
    ile[0]= 5678;
    cout << ile.scalar() << ", " << ile[0] << ", " << ile[100] << endl;
    SPList<int> ile2;
    for(i=0;i<=100;i++) ile2[i]= i;
    for(i=200;i>100;i--) ile2[i]= i;
    for(i=0;i<=200;i++) if(ile2[i] != i) cout << "error at index " << i << endl;
    cout << "Index check done" << endl;
    cout << ile2.scalar() << ", " << ile2[0] << ", " << ile2[200] << endl;

// test Regexp stuff
    cout << endl << "testing regexp stuff:" << endl;
    x.reset();
    cout << "x.m(\".*X((...)...(...))\", \"12345Xabcxyzdef\") returns " <<
         x.m(".*X((...)...(...))", "12345Xabcxyzdef") << endl;
    cout << "subs matched = " << x << endl;

    Regexp rexp("abc");
    SPString rst("12345Xabcxyzdef");
    cout << "rst.m(rexp) returns " << rst.m(rexp) << endl;
    
    cout << endl << "testing grep:" << endl;
    x.reset();
    x.push("abcd"); x.push("a2345"); x.push("X2345"); x.push("Xaaaaa"); x.push("aaaaa");
    
    y= x.grep("^a.*");
    cout << "x: " << endl << x << endl << "grep(^a.*)" << endl;
    cout << "Expect 3 matches:" << endl << y << endl;
    {
        SPString s1("abcdef");
        cout << "s1= " << s1 << ", s1.m(\"^cde\") : " << s1.m("^cde") << endl;
        cout << "s1= " << s1 << ", s1.m(\"^..cde\") : " << s1.m("^..cde") << endl;
    }
    {
    SPStringList sl;
    SPString str= "ab cd ef";
        sl = m("(..) (..)", str);
        cout << "sl = m(\"(..) (..)\", \"ab cd ef\"); sl = " << endl <<
                 sl << endl;
    }

    {
    Regexp ncr("abc", Regexp::nocase);
    Regexp cr("abc");
    SPString s= "ABC";
    cout << "s= " << s << ": s.m(ncr)= " << s.m(ncr) << endl;
    cout << "s= " << s << ": s.m(cr)= " << s.m(cr) << endl;
    cout << "s.m(\"abc\", \"i\")= " << s.m("abc", "i") << endl;
    cout << "s.m(\"abc\")= " << s.m("abc") << endl;
    }
    
// Test strings
    cout << "test string stuff:" << endl;

    SPString s1("string1"), s2, s3;
    const char *s= s1;

    cout << "Empty string: " << s2 << " length= " << s2.length()
         << ",  strlen(s2) = " << strlen(s2) << endl;
    
    cout << "s1:" << s1 << endl;
    cout << "s[0]= " << s[0] << ", s[5]= " << s[5] << endl;
//    s[2]= 'X';
//    cout << "s[2]= 'X', s= " << s << endl;
//    s[2]= 'r';
    
    cout << "const char *s= s1: s= " << s << endl;
    s2= s1;
    cout << "s2=s1,  s2:" << s2 << endl;
    s1.chop();
    cout << "s1.chop()" << s1 << endl;
    s3= s;
    cout << "s3= s: s3 = " << s3 << endl;
    cout << "index(\"ri\") in " << s1 << ": " << s1.index("ri") << endl;
    s3= "1";
    cout << "index(" << s3 << ") in " << s1 << ": " << s1.index(s3) << endl;
    s3= "abcabcabc";
    cout << "rindex(abc) in" << s3 << ": " << s3.rindex("abc") << endl;
    cout << "rindex(abc, 5) in" << s3 << ": " << s3.rindex("abc", 5) << endl;

// test substrings
    cout << "substr(5, 3) in " << s3 << ": " << s3.substr(5, 3) << endl;
    s3.substr(5, 3)= "XXX";
    cout << "s3.substr(5, 3) = \"XXX\"" << s3 << endl;
    s3.substr(5, 3)= s1;
    cout << "s3.substr(5, 3) = s1" << s3 << endl;
    s3.substr(5, 3)= s1.substr(1, 3);
    cout << "s3.substr(5, 3) = s1.substr(1, 3)" << s3 << endl;
    s3.substr(0, 6)= s1.substr(0, 3);
    cout << "s3.substr(0, 6) = s1.substr(0, 3)" << s3 << endl;
    s3.substr(-3, 2)= s1.substr(0, 2);
    cout << "s3.substr(-3, 2) = s1.substr(0, 2)" << s3 << endl;

// test overlapping substrings
    s1= "1234567890";
    cout << "s1 = " << s1 << endl;
    s1.substr(0, 10)= s1.substr(1, 9);
    cout << "s1.substr(0, 10)= s1.substr(1, 9) " << s1 << endl;
    s1= "1234567890";
    cout << "s1 = " << s1 << endl;
    s1.substr(1, 9)= s1.substr(0, 10);
    cout << "s1.substr(1, 9)= s1.substr(0, 10) " << s1 << endl;

    // test over-large substrings
    s1= "1234567890"; s1.substr(7, 10)= "abcdefghij";
    cout << "s1.substr(7, 10)= \"abcdefghij\" " << s1 << endl;
    s1= "1234567890"; s1.substr(10, 5)= "abcdefghij";
    cout << "s1.substr(10, 5)= \"abcdefghij\" " << s1 << endl;
    s1= "1234567890"; s1.substr(20, 1)= "abcdefghij";
    cout << "s1.substr(20, 1)= \"abcdefghij\" " << s1 << endl;

    s1= "abcdef"; s2= "123456";
     
    cout << s1 << " + " << s2 << ": " << s1 + s2 << endl;
    cout << s1 << " + " << "\"hello\"= " << s1 + "hello" << endl;
    cout << "\"hello\"" << " + " << s1 << "= " << "hello" + s1 << endl;
    cout << s1 << " + \'x\' = " << s1 + 'x' << endl;
    
    s1= "abc"; s2= "def"; s3= "abc";
    cout << s1 << " == " << s2 << ": " << (s1 == s2) << endl; 
    cout << s1 << " != " << s2 << ": " << (s1 != s2) << endl;
    cout << s1 << " == " << s3 << ": " << (s1 == s3) << endl; 
    cout << s1 << " != " << s3 << ": " << (s1 != s3) << endl;
    cout << s1 << " < " << s2 << ": " << (s1 < s2) << endl; 
    cout << s1 << " > " << s2 << ": " << (s1 > s2) << endl; 
    cout << s1 << " <= " << s2 << ": " << (s1 <= s2) << endl; 
    cout << s1 << " >= " << s3 << ": " << (s1 >= s3) << endl; 

// Test the tr() functions
    s1= "abcdefghi";
    cout << "s1 = " << s1;
    cout << ", s1.tr(\"ceg\", \"123\") = " << s1.tr("ceg", "123");
    cout << ", s1 = " << s1 << endl;
    s1= "abcdefghi";
    cout << "s1.tr(\"a-z\", \"A-Z\") = " << s1.tr("a-z", "A-Z");
    cout << ", s1 = " << s1 << endl;
    s1= "abcdefghi";
    cout << "s1.tr(\"efg\", \"\") = " << s1.tr("efg", "");
    cout << ", s1 = " << s1 << endl;
    s1= "abcdefghi";
    cout << "s1.tr(\"ac-e\", \"X\") = " << s1.tr("ac-e", "X");
    cout << ", s1 = " << s1 << endl;
    s1= "abcdefghiiii";
    cout << "s1 = " << s1;
    cout << ", s1.tr(\"ac-e\", \"X\", \"s\") = " << s1.tr("ac-e", "X", "s");
    cout << ", s1 = " << s1 << endl;
    s1= "abcdefghi";
    cout << "s1.tr(\"ac-e\", \"\", \"d\") = " << s1.tr("ac-e", "", "d");
    cout << ", s1 = " << s1 << endl;
    s1= "abcdefghi";
    cout << "s1.tr(\"ac-e\", \"d\", \"d\") = " << s1.tr("ac-e", "d", "d");
    cout << ", s1 = " << s1 << endl;
    s1= "abcdefghi";
    cout << "s1.tr(\"ac-e\", \"\", \"cd\") = " << s1.tr("ac-e", "", "cd");
    cout << ", s1 = " << s1 << endl;
    s1= "bookkeeper";
    cout << s1;
    cout << ": s1.tr(\"a-zA-Z\", \"\", \"s\") = " << s1.tr("a-zA-Z", "", "s");
    cout << ", s1 = " << s1 << endl;
    s1= "abc123def456ghi";
    cout << s1;
    cout << ": s1.tr(\"a-zA-Z\", \" \", \"c\") = " << s1.tr("a-zA-Z", " ", "c");
    cout << ", s1 = " << s1 << endl;
    s1= "abc123def456ghi789aaa";
    cout << s1;
    cout << ": s1.tr(\"a-zA-Z\", \" \", \"cs\") = " << s1.tr("a-zA-Z", " ", "cs");
    cout << ", s1 = " << s1 << endl;
    s1= "abcdddaaaxxx";
    cout << s1;
    cout << ": s1.tr(\"a\", \"d\", \"s\") = " << s1.tr("a", "d", "s");
    cout << ", s1 = " << s1 << endl;
    
// Test substitute command
    s1= "abcdefghi";
    cout << s1;
    cout <<" s1.s(\"def\", \"FED\") = " << s1.s("def", "FED");
    cout << ", s1= " << s1 << endl;
    s1= "abcDEFghi";
    cout << s1;
    cout <<" s1.s(\"def\", \"FED\") = " << s1.s("def", "FED");
    cout << ", s1= " << s1 << endl;
    s1= "abcDEFghi";
    cout << s1;
    cout <<" s1.s(\"def\", \"FED\", \"i\") = " << s1.s("def", "FED", "i");
    cout << ", s1= " << s1 << endl;
    s1= "abcdefghi";
    cout << s1;
    cout <<" s1.s(\"(...)(...)\", \"\\$,$&,$2 $1\") = " <<
             s1.s("(...)(...)", "\\$,$&,$2 $1");
    cout << ", s1= " << s1 << endl;
    s1= "abcdefabcghiabc";
    cout << s1;
    cout <<" s1.s(\"abc\", \"XabcX\", \"g\") = " << s1.s("abc", "XabcX", "g");
    cout << ", s1= " << s1 << endl;
    s1= "abcdefabcghiabc";
    cout << s1;
    cout <<" s1.s(\"abc\", \"X\", \"g\") = " << s1.s("abc", "X", "g");
    cout << ", s1= " << s1 << endl;
    s1= "abcdefabcghiabc";
    cout << s1;
    cout <<" s1.s(\"abc(.)\", \"X$1abcX$1\", \"g\") = " <<
             s1.s("abc(.)", "X$1abcX$1", "g");
    cout << ", s1= " << s1 << endl;
    s1= "abcdefabcghiabc";
    cout << s1;
    cout <<" s1.s(\"(.)abc\", \"$1X$1abcX\", \"g\") = " <<
             s1.s("(.)abc", "$1X$1abcX", "g");
    cout << ", s1= " << s1 << endl;
    s1= "1234567890";
    cout << s1;
    cout <<" s1.s(\"(.)(.)\", \"$2$1\", \"g\") = " <<
             s1.s("(.)(.)", "$2$1", "g");
    cout << ", s1= " << s1 << endl;
    return 0;
}
Beispiel #15
0
TGraphErrors* newCorrCoefGraph(const T* const dat1,
                               const U* const dat2,
                               const Int_t    nd,
                               const Int_t    minbin=-1, // -1 => 0
                               const Int_t    maxbin=-1  // -1 => nd-1
                               ) {
   const Int_t minb = (minbin<0) ?    0 : minbin;
   const Int_t maxb = (maxbin<0) ? nd-1 : maxbin;
   if ( (maxb<=minb) || (maxb>=nd) ) {
      Fatal("newCorrCoefGraph",
            "Invalid maxb=%d. (minb=%d, nd=%d)",
            maxb, minb, nd);
   }

   // find correlation coef for different shifts
   //const Int_t ndh = nd/2;
   const T* c1 = dat1+minb;
   const U* c2 = dat2+minb;
   const Int_t ndh = (maxb-minb+1)/2;
   Int_t pos, j(0);
   TGraphErrors* gc = new TGraphErrors;
   for (Int_t sh=1-ndh; sh<ndh; ++sh) {
      Double_t a1(0), a2(0), r1(0), r2(0);
      Double_t cor(0), ncr(0);
      c1 = dat1+minb;
      c2 = dat2+minb-sh;
      for (Int_t i=minb; i<=maxb; ++i, ++c1, ++c2) {
         pos = c2 - dat2;
#ifdef SHIFT_INTO_WINDOW
         if (pos<nd) { // CHECK: can c1 go above nd?
            if (pos>=0) {
#else
         if (pos<=maxb) {
            if (pos>=minb) {
#endif
               a1  += *c1;
               a2  += *c2;
               r1  += (*c1)*(*c1);
               r2  += (*c2)*(*c2);
               cor += (*c1)*(*c2);
               ncr += 1.0;
            }
         } else {
            break;
         }
      } // sample
      if (ncr>1) {
         // rms
         r1  -= (a1*a1)/ncr;
         r2  -= (a2*a2)/ncr;
         r1  /= ncr-1.0;
         r2  /= ncr-1.0;
         r1   = TMath::Sqrt(r1);
         r2   = TMath::Sqrt(r2);
         // ave
         a1  /= ncr;
         a2  /= ncr;
         // cor
         cor -= ncr*a1*a2;
         cor /= (ncr-1.0)*r1*r2;
         gc->SetPoint(j, static_cast<Double_t>(sh), cor);
         gc->SetPointError(j, 0, 
            (1.0-(cor*cor))/TMath::Sqrt(ncr-1.0));
         ++j;
      }
   } // shift

   return gc;
}
Beispiel #16
0
int main()
{
    int n = 20;
    std::cout << ncr(2 * n, n) << std::endl;
}
Beispiel #17
0
int main()
{
  int  m,n,p,i,j,comb,max;
  printf("Enter number of equations and variables\n");
  scanf("%d %d",&m,&n);
  p=n+1;
  float *optim=(float *)malloc(n*sizeof(float));
  float *blank=(float *)malloc(n*sizeof(float));
  float **mat = allocate(m,p);
  printf("Enter the coefficients of the equations\n");
  scanf("%*c");
  for(i=0;i<m;i++)
    {
      printf("Enter equation %d \n",i+1);
      for(j=0;j<p;j++)
	{
	  scanf("%f",(j+*(mat+i)));
	}
    }
  printf("The coefficient matrix is \n");
  printmatrix(mat,m,p);
  printf("\n");
  printf("Enter the function to be optimized \n");
  for(j=0;j<n;j++)
    {
      scanf("%f",optim+j);
    }
  printf("Enter 1 to maximize, 0 to minimize \n");
  scanf("%d",&max);
  
  comb =ncr(n,n-m);
  float **sols = allocate(comb,n);
  float **store = allocate(comb,n);
  float **temp = allocate(m,m+1);
  int *type=(int *)malloc(comb*sizeof(int));
  fill(blank,0,m,n,store);
  for( i=0;i<comb;i++)
    {
      int k=0,l;
      for(j=0;j<n;j++)
	{
	  if(fabs(store[i][j] - 1) < 0.001)
	    {
	     
	      for(l=0;l<m;l++)
		{
		  temp[l][k]=mat[l][j];
		}
	      k++;
	    }
	}
      for(l=0;l<m;l++)
	{
	  temp[l][k]=mat[l][n];
	}
      //printmatrix(temp,m,m+1);
      //  printf("\n");
      float * fs = (float *)malloc(m*sizeof(float));
     type[i]= gauss(temp,m,fs);
      k =0;
      for(j=0;j<n;j++)
	{
	  if(store[i][j] < 0.001)
	    {
	      sols[i][j]=0;
	    }
	  else
	    {
	      sols[i][j]=fs[k];
	      k++;
	    }
	}
    }
  //  printmatrix(sols,comb,n);
  printsolution(sols,m,n,type,optim,max);
  return 0;
}
Beispiel #18
0
/* ****************************************************************************
*
* restErrorReplyGet - 
*
* This function renders an error reply depending on the 'request' type.
* Many responses have different syntax and especially the tag in the reply
* differs (registerContextResponse, discoverContextAvailabilityResponse etc).
*
* Also, the function is called from more than one place, especially from 
* restErrorReply, but also from where the payload type is matched against the request URL.
* Where the payload type is matched against the request URL, the incoming 'request' is a
* request and not a response.
*/
std::string restErrorReplyGet(ConnectionInfo* ciP, Format format, std::string indent, std::string request, HttpStatusCode code, std::string details)
{
   std::string   tag = tagGet(request);
   StatusCode    errorCode(code, details, "errorCode");
   std::string   reply;

   ciP->httpStatusCode = SccOk;

   if (tag == "registerContextResponse")
   {
      RegisterContextResponse rcr("000000000000000000000000", errorCode);
      reply =  rcr.render(RegisterContext, format, indent);
   }
   else if (tag == "discoverContextAvailabilityResponse")
   {
      DiscoverContextAvailabilityResponse dcar(errorCode);
      reply =  dcar.render(DiscoverContextAvailability, format, indent);
   }
   else if (tag == "subscribeContextAvailabilityResponse")
   {
      SubscribeContextAvailabilityResponse scar("000000000000000000000000", errorCode);
      reply =  scar.render(SubscribeContextAvailability, format, indent);
   }
   else if (tag == "updateContextAvailabilitySubscriptionResponse")
   {
      UpdateContextAvailabilitySubscriptionResponse ucas(errorCode);
      reply =  ucas.render(UpdateContextAvailabilitySubscription, format, indent, 0);
   }
   else if (tag == "unsubscribeContextAvailabilityResponse")
   {
      UnsubscribeContextAvailabilityResponse ucar(errorCode);
      reply =  ucar.render(UnsubscribeContextAvailability, format, indent);
   }
   else if (tag == "notifyContextAvailabilityResponse")
   {
      NotifyContextAvailabilityResponse ncar(errorCode);
      reply =  ncar.render(NotifyContextAvailability, format, indent);
   }

   else if (tag == "queryContextResponse")
   {
      QueryContextResponse qcr(errorCode);
      reply =  qcr.render(QueryContext, format, indent);
   }
   else if (tag == "subscribeContextResponse")
   {
      SubscribeContextResponse scr(errorCode);
      reply =  scr.render(SubscribeContext, format, indent);
   }
   else if (tag == "updateContextSubscriptionResponse")
   {
      UpdateContextSubscriptionResponse ucsr(errorCode);
      reply =  ucsr.render(UpdateContextSubscription, format, indent);
   }
   else if (tag == "unsubscribeContextResponse")
   {
      UnsubscribeContextResponse uncr(errorCode);
      reply =  uncr.render(UnsubscribeContext, format, indent);
   }
   else if (tag == "updateContextResponse")
   {
      UpdateContextResponse ucr(errorCode);
      reply = ucr.render(UpdateContext, format, indent);
   }
   else if (tag == "notifyContextResponse")
   {
      NotifyContextResponse ncr(errorCode);
      reply =  ncr.render(NotifyContext, format, indent);
   }
   else if (tag == "StatusCode")
   {
     StatusCode sc(code, details);
     reply = sc.render(format, indent);
   }
   else
   {
      OrionError orionError(errorCode);

      LM_E(("Unknown tag: '%s', request == '%s'", tag.c_str(), request.c_str()));
      
      reply = orionError.render(format, indent);
   }

   return reply;
}
Beispiel #19
0
	static int catalan(int n)
	{
		return divide(ncr(2*n,n), n+1);
	}