Example #1
0
static void 
findpack(struct pam * const imgs,
         unsigned int const imgCt,
         Coord **     const coordsP,
         unsigned int const quality,
         unsigned int const qfactor) {

    Coord * coords;  /* malloc'ed array */
    unsigned int minarea;
    unsigned int i;
    unsigned int rdiv;
    unsigned int cdiv;
    Rectangle * current;  /* malloc'ed array */
    unsigned int z;
    Coord c;

    MALLOCARRAY(coords, imgCt);
  
    if (!coords)
        pm_error("Out of memory allocating %u-element coords array", imgCt);

    z = UINT_MAX;  /* initial value */
    c.x = 0; c.y = 0;  /* initial value */

    if (quality > 1) {
        unsigned int realMinarea;
        for (realMinarea = i = 0; i < imgCt; ++i)
            realMinarea += imgs[i].height * imgs[i].width;
        minarea = realMinarea * qfactor / 100;
    } else
        minarea = UINT_MAX - 1;

    /* It's relatively easy to show that, if all the images
     * are multiples of a particular size, then a best
     * packing will always align the images on a grid of
     * that size.
     *
     * This speeds computation immensely.
     */
    for (rdiv = imgs[0].height, i = 1; i < imgCt; ++i)
        rdiv = gcf(imgs[i].height, rdiv);

    for (cdiv = imgs[0].width, i = 1; i < imgCt; ++i)
        cdiv = gcf(imgs[i].width, cdiv);

    MALLOCARRAY(current, imgCt);

    for (i = 0; i < imgCt; ++i) {
        current[i].size.x = imgs[i].width;
        current[i].size.y = imgs[i].height;
    }
    recursefindpack(current, c, coords, minarea, &z, 0, imgCt, cdiv, rdiv,
                    quality, qfactor);

    free(current);

    *coordsP = coords;
}
Example #2
0
int main(int argc, char ** argv)
{
	int num, denom, n, d, NUM, DENOM;
	fraction_t * set, set_size, set_capacity;
	
	NUM = DENOM = 1;
	
	for(num = 11; num < 100; num++)
	{
		if(num % 10 == 0)
			continue;
		if(num / 10 == num % 10)
			continue;
		for(denom = num + 1; denom < 100; denom++)
		{
			if(denom % 10 == 0)
				continue;
			if(denom / 10 == denom % 10)
				continue;
			handle_nums(&NUM, &DENOM, num, denom, num / 10, denom / 10, 
				num % 10, denom % 10);
			handle_nums(&NUM, &DENOM, num, denom, num / 10, denom % 10, 
				num % 10, denom / 10);
			handle_nums(&NUM, &DENOM, num, denom, num % 10, denom / 10, 
				num / 10, denom % 10);
			handle_nums(&NUM, &DENOM, num, denom, num % 10, denom % 10, 
				num / 10, denom / 10);
		}
	}
	
	printf("Product of special denominators: %d\n", DENOM / gcf(NUM, DENOM));

	return 0;
}
Example #3
0
// **************************************************************
fdouble gammp(fdouble a, fdouble x)
/**
 * Returns the incomplete gamma function P (a, x).
 * See "Numerical Recipes in C", 2nd edition, page 218
 */
{
    fdouble gamser, gammcf, gln;

    if (x < 0.0 || a <= 0.0)
    {
        std_cout << "Invalid arguments in routine gammp\n";
        abort();
    }

    if (x < (a+fone))
    {
        //Use the series representation.
        gser(&gamser, a, x, &gln);
        return gamser;
    } else {
        // Use the continued fraction representation and take its complement.
        gcf(&gammcf, a, x, &gln);
        return fone - gammcf;
    }
}
Example #4
0
void alg2_decommon_n(Term a2)
	{
    List l1;
	int cnum,cden;
	Term t;

	l1=CompoundArgN(a2,5);
	if(is_empty_list(l1))
		{
		SetCompoundArg(a2,2,0);
		return;
		}

	t=ConsumeCompoundArg(a2,2);
	cnum=IntegerValue(CompoundArg1(t));
	cden=IntegerValue(CompoundArg2(t));
	FreeAtomic(t);

	while(!is_empty_list(l1))
		{
		Term t;
		int c1,c2,c3;
		c1=cnum*IntegerValue(CompoundArg1(ListFirst(l1)));
		c2=cden;
		c3=gcf(c1,c2);
		c1/=c3;
		c2/=c3;
		t=MakeCompound2(OPR_DIV,NewInteger(c1),NewInteger(c2));
		SetCompoundArg(ListFirst(l1),1,t);
		l1=ListTail(l1);
		}


	}
Foam::scalar Foam::incompleteGammaFunction::gammQ(const Foam::scalar a, const Foam::scalar x) 
{
	if (x < 0.0 || a <= 0.0) throw("bad args in gammQ");
	if (x == 0.0) return 1.0;
	else if (a >= aSwitch) return gammPapprox(a,x,0)*Foam::exp(gammln(a));
	else if (x < a + 1.0) return (1.0 - gSer(a,x))*Foam::exp(gammln(a));
	else return gcf(a,x)*Foam::exp(gammln(a));
}
Example #6
0
	bignum exponent(const bignum &base_value, const bignum &power, int precision)
	{
		if (base_value.getBase() != power.getBase())
			return exponent(base_value, power.getConverted(power.getBase()));

		bignum one(1);
		one.setBase(base_value.getBase());
		one.setPositive();

		if (power.isZero())
			return one;

		//if the power is negative, return 1/solution
		if (power.isNegative())
			return one / exponent(base_value, power.absolute());

		if (power.getDecimalCount() > 0)
		{
			if (power < 1)
			{
				bignum modified_power(power);
				modified_power.leftShift(power.getDecimalCount());
				bignum divisor(1);
				divisor.setBase(power.getBase());
				divisor.leftShift(power.getDecimalCount());

				bignum gcf(greatestCommonFactor(modified_power, divisor));
				modified_power /= gcf;
				divisor /= gcf;

				bignum divisor_root_of_base = jep::root(divisor, base_value, precision);
				return exponent(divisor_root_of_base, modified_power).getRoundedAllDigits(ONES_PLACE - precision);
			}

			else
			{
				bignum power_decimal = power % 1;
				bignum power_int = power.getRoundedDown(ONES_PLACE);

				return exponent(base_value, power_int, precision) * exponent(base_value, power_decimal, precision);
			}
		}

		bignum counter = power.absolute();
		bignum temp(base_value);

		//n^0 always returns 1
		if (power.isZero())
			return one;

		while (counter > one)
		{
			temp *= base_value;
			counter--;
		}

		return temp;
	}
Example #7
0
double 
gammq(double a, double x)
{
	if (x < a || a <= 0.0) {
		if (a < 0) fprintf(stderr, "gammq(%f, %f): invalid arguments\n", a, x);
		return 0.0;
	}
	if (x < a + 1.0) return 1.0 - gser(a, x);
	return gcf(a, x);
}
Example #8
0
int invert(int D,int nc,int dc,int wn)
{
    int GCF;
    numcoef=dc;
    dencoef=nc*(D-wn*wn);
    GCF=gcf(numcoef,dencoef);
    numcoef/=GCF;
    dencoef/=GCF;
    return 0;
}
Example #9
0
/**
   Represents incomplete error function, P(a,x)
**/
float gammp(float a, float x) {
  float gamser,gammcf,gln;
  if(x < 0.0 || a <= 0.0) printf("Invalid arguments in routine gammp");
  if (x < (a+1.0)) {
    gser(&gamser,a,x,&gln);
    return gamser;
  } else {
    gcf(&gammcf,a,x,&gln);
    return 1.0 - gammcf;
  }
}
Example #10
0
static int mlt_list1(List l)
{
  int ret=1;
  for(;l;l=ListTail(l))
    {
      int i=IntegerValue(ListFirst(l));
      i/=gcf(i,ret);
      ret*=i;
    }
  return ret;
}
Example #11
0
int gcf(int a, int b)
{
	int result = 0;

	if(b != 0)
		result = gcf(b, a % b);
	else
		result = a;

	return result;
}
Example #12
0
int gcf(int a, int b)
{
	if (a == 0)
		return b;
	if (b == 0)
		return a;
	if (a < 0)
		a = -a;
	if (b < 0)
   	b = -b;
	return gcf(b, a % b);
}
float gammp(float a, float x)
{
  if(x < 0.0 || a <= 0.0)
    printf("bad args in gammp\n");
  if(x == 0.0)
    return 0.0;
  else if((int) a >= ASWITCH)
    return gammpapprox(a, x, 1);
  else if(x < a + 1.0)
    return gser(a, x);
  else
    return 1.0 - gcf(a, x);
}
Example #14
0
double gammaq(double a, double x)
{
	double gamser,gammcf,gln;

	if (x < 0.0 || a <= 0.0) return 0;
	if (x < (a+1.0)) {
		gser(&gamser,a,x,&gln);
		return 1.0-gamser;
	} else {
		gcf(&gammcf,a,x,&gln);
		return gammcf;
	}
}
Example #15
0
double Statistics::gammq(double a, double x)
{
   double gamser, gammcf, gln, pval;
   if(x < (a + 1.0)){
      gser(&gamser, a, x, &gln);
      pval = 1.0 - gamser;
   }
   else{
      gcf(&gammcf, a, x, &gln);
      pval = gammcf;
   }
   return pval;
}
Example #16
0
double gammq ( double a, double x )
{
	double gamser,gammcf,gln;

	if (x < 0.0 || a <= 0.0) nrerror("Invalid arguments in routine GAMMQ");
	if (x < (a+1.0)) {
		gser(&gamser,a,x,&gln);
		return 1.0-gamser;
	} else {
		gcf(&gammcf,a,x,&gln);
		return gammcf;
	}
}
Example #17
0
double numrec_gammp(double a, double x)
{
	double gamser,gammcf,gln;

	if (x < 0.0 || a <= 0.0) my_error("Invalid arguments in routine gammp");
	if (x < (a+1.0)) {
		gser(&gamser,a,x,&gln);
		return gamser;
	} else {
		gcf(&gammcf,a,x,&gln);
		return 1.0-gammcf;
	}
}
double gammq(double a, double x)
{
  if(x < 0.0 || a <= 0.0)
    printf("bad args in gammq\n");
  if(x == 0.0)
    return 1.0;
  else if((int) a >= ASWITCH)
    return gammpapprox(a, x, 0);
  else if(x < a + 1.0)
    return 1.0 - gser(a, x);
  else
    return gcf(a, x);
}
Example #19
0
DP NR::gammp(const DP a, const DP x)
{
	DP gamser,gammcf,gln;

	if (x < 0.0 || a <= 0.0)
		nrerror("Invalid arguments in routine gammp");
	if (x < a+1.0) {
		gser(gamser,a,x,gln);
		return gamser;
	} else {
		gcf(gammcf,a,x,gln);
		return 1.0-gammcf;
	}
}
Example #20
0
// compute incomplete gamma function
double gammp(const double a, const double x)  
{
    if (x < 0 || a < 0)
		{
				return -1;
		}

		double chi;
    if (x < (a + 1.0))
        chi = 1.0 - gser(a, x);
    else
        chi =  gcf(a, x);
    return chi;
}
Example #21
0
Scalar gammq(Scalar a, Scalar x)
{
	Scalar gamser,gammcf,gln;
//	void gcf(),gser(),nrerror();

//	if (x < 0.0 || a <= 0.0) nrerror("Invalid arguments in routine GAMMQ");
	if (x < (a+1.0)) {
		gser(&gamser,a,x,&gln);
		return 1.0-gamser;
	} else {
		gcf(&gammcf,a,x,&gln);
		return gammcf;
	}
}
Example #22
0
void gammp (double *a, double *x, double *res)
{
	if (*x < 0.0 || *a <= 0.0)
	{	output("Invalid arguments in routine gammp\n");
		error=1; return;
	}
	if (*x < (*a+1.0)) {
		*res=gser(*a,*x);
		return;
	} else {
		*res=gamm(*a)-gcf(*a,*x);
		return;
	}
}
Example #23
0
double gammaCDF(double a, double x) {
	double gln, p;

	if (x <= 0.0 || a <= 0.0)
		return 0.0;
	else if (a > LARGE_A)
		return gnorm(a, x);
	else {
		gln = lngamma(a);
		if (x < (a + 1.0))
			return gser(a, x, gln);
		else
			return (1.0 - gcf(a, x, gln));
	}
}
Example #24
0
File: gamma.c Project: amnh/poy5
/** 
 * Returns the incomplete gamma function P(a,x)
 *
 * "Numerical Recipes in C", Section 6.2
 */
double gammap( const double x, const double a )
{
    double gam,gln;
    if (x < 0.0 || a <= 0.0){
        printf("Incomplete Gamma Calculated with: x:%f and a:%f\n",x,a);
        failwith("Invalid argument for incomplete gamma");
    }
    if ( x < (a+1.0)) {
        gser(&gam,a,x,&gln);
    } else {
        gcf (&gam,a,x,&gln); //inverse of continued fraction representation
        gam = 1.0 - gam;
    }
    return gam;
}
Example #25
0
double gammq(double a, double x)
{
	void gcf(double *gammcf, double a, double x, double *gln);
	void gser(double *gamser, double a, double x, double *gln);
	double gamser,gammcf,gln;

	if (x < 0.0 || a <= 0.0) my_error("Invalid arguments in routine gammq");
	if (x < (a+1.0)) {
		gser(&gamser,a,x,&gln);
		return 1.0-gamser;
	} else {
		gcf(&gammcf,a,x,&gln);
		return gammcf;
	}
}
int main()
{
    int t,n,a[50],i,g;
    t=readInt();
    while(t--)
    {
              n=readInt();
              
              a[0]=readInt();
              a[1]=readInt();
              g=gcf(a[0],a[1]);
              for(i=2;i<n;i++)
              {
                              a[i]=readInt();
                              g=gcf(g,a[i]);
              }
              for(i=0;i<n;i++)
              {
                              writeInt(a[i]/g);
              }
              PUTCHAR('\n');
    }
    return 0;
}
Example #27
0
void alg2_recommon_n(Term a2)
	{
    List m2l,l,l1,nl;
    int cnum,n,d;
	m2l=CompoundArgN(a2,5);
    nl=NewList();
	l=m2l;
	if(is_empty_list(l))
		return;


	while(!is_empty_list(l))
		{
        nl=AppendLast(nl,CompoundArg1(ListFirst(l)));
		l=ListTail(l);
		}

	cnum=gcf_list(nl);
	if(IntegerValue(ListFirst(nl))<0)
		cnum*=-1;
    if(cnum==1)
        {
        RemoveList(nl);
        return;
        }

    l1=m2l;
	l=nl;
	while(!is_empty_list(l))
		{
        SetCompoundArg(ListFirst(l1),1,
            NewInteger(IntegerValue(ListFirst(l))/cnum));
		l=ListTail(l);
		l1=ListTail(l1);
		}

	RemoveList(nl);
    n=IntegerValue(CompoundArg1(CompoundArg2(a2)));
    d=IntegerValue(CompoundArg2(CompoundArg2(a2)));
    n*=cnum;
    cnum=gcf(n,d);
    n/=cnum;
    d/=cnum;
    SetCompoundArg(CompoundArg2(a2),1,NewInteger(n));
    SetCompoundArg(CompoundArg2(a2),2,NewInteger(d));

	return ;
	}
Example #28
0
float gammq(float a, float x)
{
	void gcf(float *gammcf, float a, float x, float *gln);
	void gser(float *gamser, float a, float x, float *gln);
	void nrerror(char error_text[]);
	float gamser,gammcf,gln;

	if (x < 0.0 || a <= 0.0) nrerror("Invalid arguments in routine gammq");
	if (x < (a+1.0)) {
		gser(&gamser,a,x,&gln);
		return 1.0-gamser;
	} else {
		gcf(&gammcf,a,x,&gln);
		return gammcf;
	}
}
Example #29
0
double gammp(double a, double x)							// Returns normalised lower incomplete gamma function
{
    void gcf(double *gammcf,double a, double x, double *gln);
    void gser(double *gamser, double a, double x, double *gln);
    double gamser,gammcf,gln;
    if(x<0) DEBUG << "Error: Argument of incomplete gamma function <= 0" << endl;
    if(x< (a+1.0E0))
    {
        gser(&gamser,a,x,&gln);
        return gamser;
    }
    else{
        gcf(&gammcf, a, x, &gln);
        return (1.0E0-gammcf);
    }
}
Example #30
0
double gammq ( double a, double x )
   {
   double gammser, gammcf, gln;

   if (x < 0.0 || a <= 0.0) error("Invalid arguments in routine gammq");
   if (x < (a + 1.0))
      {
      gser (&gammser, a, x, &gln);     // use the series representation
      return (1.0 - gammser);          // and take its complement
      }
   else
      {
      gcf ( &gammcf, a, x, &gln);      // use the continued fraction representation
      return gammcf;
      }
   }