Beispiel #1
0
// eulers gcd algorithm without modulus
void slow_gcd(bigInt num1[], bigInt num2[]) {
   int compare;
	while ((compare = cmp(num1, num2)) != EQ) {
	   if (compare == GT)
		   Subtract(num1, num2);
		else
		   Subtract(num2, num1);
	}
}
Beispiel #2
0
Integer Dixon::factor(Integer x){
    auto sqrt_x = sqrt(x);
    if (sqrt_x * sqrt_x == x) return sqrt_x;
    
    Integer one("1", BASE_INTEGER), sqrt4_x = sqrt(sqrt_x), cur = sqrt4_x, cur_pow2 = cur * cur;

    while (cur < sqrt_x){

        auto d = gcd(cur_pow2, x);
        if (one < d) return d;
        auto xxx = (cur_pow2 * cur_pow2) % x;
    
        if (isSqrt(xxx)){
            auto yyy = sqrt(xxx);
            auto delta = xxx;
            delta.Subtract(yyy);
            delta = delta % x;
            auto d = gcd(delta, x);
    
            if (one < d) return d;
        }

        cur_pow2.Add(cur * 2);
        cur_pow2.Add(one);
        cur.Add(one);
    }
    
    return x;
}
Beispiel #3
0
// Binary GCD algorithm
// requires num1 > 0 and num2 > 0
// sets either num1 or num2 to the 1 if gcd == 1 or some number >1 if gcd >1 and
// returns the pointer to whichever num was set
bigInt* gcd(bigInt *num1, bigInt *num2) {
   int shift, compare;
	
	for (shift = 0; ((num1[0] | num2[0]) & LOWBIT) == 0; ++shift) {
	   shiftR1(num1);
		shiftR1(num2);
	}
	
	while ((num1[0] & 1) == 0)
	   shiftR1(num1);
	
	do {
	   while ((num2[0] & 1) == 0)
		   shiftR1(num2);
		
		compare = cmp(num1, num2);
		if (compare == EQ)
		   break;
		else if (compare == GT) {
		   bigInt *t = num1;
			num1 = num2;
			num2 = t;
		}
		Subtract(num2, num1);
	} while (1);
	
	if (shift)
	   shiftL1(num1);
	
	return num1;
}
Beispiel #4
0
 void Divide(const JntArrayVel& src,const doubleVel& factor,JntArrayVel& dest)
 {
     Multiply(src.q,(factor.grad/factor.t/factor.t),dest.q);
     Divide(src.qdot,factor.t,dest.qdot);
     Subtract(dest.qdot,dest.q,dest.qdot);
     Divide(src.q,factor.t,dest.q);
 }
Beispiel #5
0
const float Distance(	const TVector2f& _krA,
							const TVector2f& _krB)
{
	const float kfDistance = Magnitude(Subtract(TVector2f(), _krA, _krB));

	return(kfDistance);
}
Beispiel #6
0
const double Distance(const TVector2d& _krA,
							const TVector2d& _krB)
{
	const double kdDistance = Magnitude(Subtract(TVector2d(), _krA, _krB));

	return(kdDistance);
}
Beispiel #7
0
void Terrain::ReInit(
   Vector3              *v0P,
   Vector3              *v1P,
   Vector3              *v2P,
   Vector3              *v3P
) {
   int i, j, k;

   v0 = *v0P;
   v1 = *v1P;
   v2 = *v2P;
   v3 = *v3P;
   dv = Subtract(v2, v0);
   dv.x /= float(widthVertices - 1);
   dv.y /= 1;
   dv.z /= float(heightVertices - 1);

   for (j=0, k=0; j<heightVertices; j++) {
      for (i=0; i<widthVertices; i++, k++) {
         GetTerrainPoint(&(vertices[k]), float(v0.x + i * dv.x), float(v0.z + j * dv.z));
         GetTerrainNormal(&(normals[k]), &(vertices[k]));
         //vertices[k].u = float(i);   
         //vertices[k].v = float(j);   
         //vertices[k].color = D3DCOLOR_ARGB(0,255,255,255);
      }
   }

}
Beispiel #8
0
bool SDraw::ExcludeClipOp(const Rect& r)
{
	Cloff& c = cloff.Top();
	bool dummy;
	c.clip = Subtract(c.clip, r + c.offset, dummy);
	return c.clip.GetCount();
}
int main()
{
	int x;
	int start=0,end=100;
	int y=(start+end)/2;
	printf("*****************************************************************\n");
	printf("Enter the number corresponding to the desired pay rate or action:\n");
	printf("a) add\t\t\ts) subtract\n");
	printf("m) multiply\t\td) divide\n");
	printf("q) quit\n");
	printf("*****************************************************************\n");
	x=getchar();
	switch(x)
	{
		case 'a':
			Add();
			break;
		case 's':
			Subtract();
			break;
		case 'm':
			Mutliply();
			break;
		case 'd':
			Divide();
			break;
		case 'q':
			printf("Bye~~~~~~\n");
			break;
	}
}
Beispiel #10
0
Vector<Rect> Ctrl::GetPaintRects()
{
	Vector<Rect> r;
	r.Add(GetScreenRect());
	for(int i = max(FindTopCtrl() + 1, 0); i < topctrl.GetCount(); i++)
		Subtract(r, topctrl[i]->GetScreenRect());
	return r;
}
Beispiel #11
0
// Solution taken from: http://mathworld.wolfram.com/QuarticEquation.html
// and http://www.csit.fsu.edu/~burkardt/f_src/subpak/subpak.f90
int Factor(double a4,double a3,double a2,double a1,double a0,double roots[4][2],const double& EPS){
	double R[2],D[2],E[2],R2[2];

	if(fabs(a4)<EPS){return Factor(a3,a2,a1,a0,roots,EPS);}
	a3/=a4;
	a2/=a4;
	a1/=a4;
	a0/=a4;

	Factor(1.0,-a2,a3*a1-4.0*a0,-a3*a3*a0+4.0*a2*a0-a1*a1,roots,EPS);

	R2[0]=a3*a3/4.0-a2+roots[0][0];
	R2[1]=0;
	Sqrt(R2,R);
	if(fabs(R[0])>10e-8){
		double temp1[2],temp2[2];
		double p1[2],p2[2];

		p1[0]=a3*a3*0.75-2.0*a2-R2[0];
		p1[1]=0;

		temp2[0]=((4.0*a3*a2-8.0*a1-a3*a3*a3)/4.0);
		temp2[1]=0;
		Divide(temp2,R,p2);

		Add     (p1,p2,temp1);
		Subtract(p1,p2,temp2);

		Sqrt(temp1,D);
		Sqrt(temp2,E);
	}
	else{
		R[0]=R[1]=0;
		double temp1[2],temp2[2];
		temp1[0]=roots[0][0]*roots[0][0]-4.0*a0;
		temp1[1]=0;
		Sqrt(temp1,temp2);
		temp1[0]=a3*a3*0.75-2.0*a2+2.0*temp2[0];
		temp1[1]=                  2.0*temp2[1];
		Sqrt(temp1,D);
		temp1[0]=a3*a3*0.75-2.0*a2-2.0*temp2[0];
		temp1[1]=                 -2.0*temp2[1];
		Sqrt(temp1,E);
	}

	roots[0][0]=-a3/4.0+R[0]/2.0+D[0]/2.0;
	roots[0][1]=        R[1]/2.0+D[1]/2.0;

	roots[1][0]=-a3/4.0+R[0]/2.0-D[0]/2.0;
	roots[1][1]=        R[1]/2.0-D[1]/2.0;

	roots[2][0]=-a3/4.0-R[0]/2.0+E[0]/2.0;
	roots[2][1]=       -R[1]/2.0+E[1]/2.0;

	roots[3][0]=-a3/4.0-R[0]/2.0-E[0]/2.0;
	roots[3][1]=       -R[1]/2.0-E[1]/2.0;
	return 4;
}
Beispiel #12
0
 NekMatrix<typename NekMatrix<LhsDataType, LhsMatrixType>::NumberType, StandardMatrixTag> 
 Subtract(const NekMatrix<LhsDataType, LhsMatrixType>& lhs,
          const NekMatrix<RhsDataType, RhsMatrixType>& rhs)
 {
     typedef typename NekMatrix<LhsDataType, LhsMatrixType>::NumberType NumberType;
     NekMatrix<NumberType, StandardMatrixTag> result(lhs.GetRows(), lhs.GetColumns());
     Subtract(result, lhs, rhs);
     return result;
 }
Beispiel #13
0
	// product = xy
	// memory space for product may not overlap with x,y
    void Multiply(
    	int limbs,		// Number of limbs in x,y
    	u32 *product,	// Product; buffer size = limbs*2
    	const u32 *x,	// Large number; buffer size = limbs
    	const u32 *y)	// Large number; buffer size = limbs
	{
		// Stop recursing under 640 bits or odd limb count
		if (limbs < 30 || (limbs & 1))
		{
			SimpleMultiply(limbs, product, x, y);
			return;
		}

		// Compute high and low products
		Multiply(limbs/2, product, x, y);
		Multiply(limbs/2, product + limbs, x + limbs/2, y + limbs/2);

		// Compute (x1 + x2), xc = carry out
		u32 *xsum = (u32*)alloca((limbs/2)*4);
		u32 xcarry = Add(xsum, x, limbs/2, x + limbs/2, limbs/2);

		// Compute (y1 + y2), yc = carry out
		u32 *ysum = (u32*)alloca((limbs/2)*4);
		u32 ycarry = Add(ysum, y, limbs/2, y + limbs/2, limbs/2);

		// Compute (x1 + x2) * (y1 + y2)
		u32 *cross_product = (u32*)alloca(limbs*4);
		Multiply(limbs/2, cross_product, xsum, ysum);

		// Subtract out the high and low products
		s32 cross_carry = Subtract(cross_product, limbs, product, limbs);
		cross_carry += Subtract(cross_product, limbs, product + limbs, limbs);

		// Fix the extra high carry bits of the result
		if (ycarry) cross_carry += Add(cross_product + limbs/2, limbs/2, xsum, limbs/2);
		if (xcarry) cross_carry += Add(cross_product + limbs/2, limbs/2, ysum, limbs/2);
		cross_carry += (xcarry & ycarry);

		// Add the cross product into the result
		cross_carry += Add(product + limbs/2, limbs*3/2, cross_product, limbs);

		// Add in the fixed high carry bits
		if (cross_carry) Add32(product + limbs*3/2, limbs/2, cross_carry);
	}
void Expression() {
  Term();
  while(IsAddop(Look)) {
    EmitLn("push rax");
    switch(Look) {
      case '+': Add(); break;
      case '-': Subtract(); break;
    }
  }
}
Beispiel #15
0
// returns the normalized direction from 2 points (p1, p2) 
// projected on the surface
// in direction from p1 -> p2
Vector3 Terrain::GetDirection(Vector3 p1, Vector3 p2) {
	Vector3 dir;

	GetTerrainPoint(&p1, p1.x, p1.z);
	GetTerrainPoint(&p2, p2.x, p2.z);

	dir = Subtract(p2, p1);
	dir.normalize();

	return dir;
}
Beispiel #16
0
void Terrain::GetTerrainNormal( Vector3   *n, Vector3   *pt) 
{
	Vector3 a, b;
	Vector3 temp;
	float dx, dz;

	// make deltas 1% of each cell
	dx = (v2.x - v0.x) / maxWidthVertices / 100;
	dz = (v2.z - v0.z) / maxHeightVertices / 100;

	GetTerrainPoint(&temp, pt->x,      pt->z);
	GetTerrainPoint(&a, pt->x + dx,      pt->z);
	a = Subtract(a, temp);
	GetTerrainPoint(&b,       pt->x,      pt->z + dz);
	b = Subtract(b, temp);

	*n = CrossProduct(a, b);

	n->normalize();
}
void Expression() {
  FirstTerm();
  NewLine();
  while(IsAddop(Look)) {
    Push();
    switch(Look) {
      case '+': Add(); break;
      case '-': Subtract(); break;
    }
    NewLine();
  }
}
Beispiel #18
0
void GRegion::Union(GRect *b)
{
	if (b && b->Valid())
	{
		Subtract(b);
		GRect *n = NewOne();
		if (n)
		{
			*n = *b;
		}
	}
}
int CheckHit( XMFLOAT3 *pv3Point )							// 多角形(制限付き)と点の当たり判定
{
	int				i;
	float			fAngle;
	int				nAngleIndex;
	XMFLOAT3		v3LineVec;
	XMFLOAT3		v3HitVec;
	float			fCross;
	float			fDot;							// 内積
	int				nLastIndex = -1;
	float			fLen;

	for ( i = 0; i < 2; i++ ) {
		fAngle = atan2f( pv3Point->z - g_HitPolygonCenter.z,
						 pv3Point->x - g_HitPolygonCenter.x );
		if ( fAngle < 0.0f ) fAngle += 2.0f * PI;
		nAngleIndex = ( int )( fAngle / ( 2.0f * PI ) * CHECK_POLYGON_ANGLES );
		if ( nAngleIndex == nLastIndex ) break;

		// サイクルベクトル
		v3LineVec = Subtract( &g_HitPolygonVertices[nAngleIndex + 1], &g_HitPolygonVertices[nAngleIndex] );
		v3HitVec = Subtract( pv3Point, &g_HitPolygonVertices[nAngleIndex] );
		fCross = v3LineVec.z * v3HitVec.x - v3LineVec.x * v3HitVec.z;
		if ( fCross < 0.0f ) {
			fLen = sqrtf( v3LineVec.x * v3LineVec.x + v3LineVec.y * v3LineVec.y + v3LineVec.z * v3LineVec.z );
			v3LineVec = XMFLOAT3( v3LineVec.x / fLen, v3LineVec.y / fLen, v3LineVec.z / fLen );
			fDot = Dot( &v3LineVec, &v3HitVec );
			if ( i == 1 ) {
				if ( fDot < 0.0f ) fDot = 0.0f;
				if ( fDot > fLen ) fDot = fLen;
			}
			pv3Point->x = v3LineVec.x * fDot + g_HitPolygonVertices[nAngleIndex].x;
			pv3Point->z = v3LineVec.z * fDot + g_HitPolygonVertices[nAngleIndex].z;
			if ( ( fDot >= 0.0f ) && ( fDot <= fLen ) ) break;
			nLastIndex = nAngleIndex;
		}
	}

	return nAngleIndex;
}
Beispiel #20
0
void Ctrl::WndUpdate0r(const Rect& r)
{
	GuiLock __;
	Rect rr = r + GetRect().TopLeft();
	bool dummy;
	Vector<Rect> h;
	h <<= invalid;
	invalid = Intersect(invalid, rr, dummy);
	FBInitUpdate();
	DoPaint();
	invalid <<= h;
	Subtract(invalid, rr);
	FBFlush();
}
Beispiel #21
0
		float FindLaplacianValue(
			const MatrixF& img,
			const Remapping& remapping,
			float gvalue,
			int level,
			int row,
			int col )
		{
			//find the total size we need
			int size = 3 * ( pow(2.0f,level+2) - 1 );
			int halfSize = size / 2;

			//find the corresponding point from that level to original image
			int rowTemp = row*pow(2.0f,level);
			int colTemp = col*pow(2.0f,level);

			//copy the whole block (replicating when necessary)
			MatrixF mat[2];
			MatrixF* dest0 = &(mat[0]);
			MatrixF* dest1 = &(mat[1]);

			Point topLeft( colTemp - halfSize, rowTemp - halfSize );
			Point bottomRight( colTemp + halfSize, rowTemp + halfSize );
			Copy( img, topLeft, bottomRight, *dest0 );
			RemapImage(*dest0, gvalue, remapping, *dest0);

			Pyramid::Gaussian( *dest0, *dest1, 0.4f );
			Copy( *dest1, Point(2,2), Point(dest1->Cols()-3,dest1->Rows()-3), *dest1);
			Decimate(*dest1,*dest1);

			for(int i = 0; i < level; ++i)
			{
				std::swap( dest0, dest1 );
				Pyramid::Gaussian( *dest0, *dest1, 0.4f );
				Copy( *dest1, Point(2,2), Point(dest1->Cols()-3,dest1->Rows()-3), *dest1);
				Decimate(*dest1,*dest1);
			}

			//expand
			assert( dest1->Rows() == 3 );
			assert( dest1->Cols() == 3 );
			ExpandOdd( *dest1, *dest1, 0.4f ); //now 5X5

			//we need to also reduce dest0
			Copy( *dest0, Point(2,2), Point(dest0->Cols()-3,dest0->Rows()-3), *dest0);
			MatrixF laplacian;
			Subtract(*dest0, *dest1, laplacian);
			return laplacian[laplacian.Rows()/2][laplacian.Cols()/2];
		}
		static bool WouldBeTrappedInElement(const WebCore::IntRect& rect, const WebCore::IntPoint& point, EA::WebKit::JumpDirection direction)
		{
			// If we're not inside, don't worry
			if (rect.contains(point))
			{
				typedef WebCore::IntPoint Vector2D;

				const WebCore::IntPoint centre = Average( rect.minXMaxYCorner(), rect.maxXMinYCorner() );

				Vector2D pointToCentre = Subtract(centre,point);
				Vector2D forward;

				switch (direction)
				{
				// note these are 'backward
				case EA::WebKit::JumpUp:		forward = Vector2D(0,-100); break;
				case EA::WebKit::JumpDown:		forward = Vector2D(0,100); break;
				case EA::WebKit::JumpLeft:		forward = Vector2D(-100,0); break;
				case EA::WebKit::JumpRight:		forward = Vector2D(100,0); break;
				}

				// Basically, if the centre is behind us, don't jump there
				if (DotProduct(forward,pointToCentre) < 0)
				{
					return false;
				}
				else
				{
					/*printf("(%d,%d) Trapped Inside Element (%d,%d)->(%d,%d): forward=(%d,%d) pointToCentre=(%d,%d) dot=%d\n",
						point.x(),
						point.y(),
						rect.bottomLeft().x(),
						rect.bottomLeft().y(),
						rect.topRight().x(),
						rect.topRight().y(),
						forward.x(),
						forward.y(),
						pointToCentre.x(),
						pointToCentre.y(),
						DotProduct(forward,pointToCentre)
						);*/
					return true;
				}
			}
			else
			{
				return false;
			}
		}
Beispiel #23
0
void GuideCamera::SubtractDark(usImage& img)
{
    // dark subtraction is done in the camera worker thread, so we need to acquire the
    // DarkFrameLock to protect against the dark frame disappearing when the main
    // thread does "Load Darks" or "Clear Darks"

    wxCriticalSectionLocker lck(DarkFrameLock);

    if (CurrentDefectMap)
    {
        RemoveDefects(img, *CurrentDefectMap);
    }
    else if (CurrentDarkFrame)
    {
        Subtract(img, *CurrentDarkFrame);
    }
}
Beispiel #24
0
//
//#############################################################################
//#############################################################################
//
UnitQuaternion&
	UnitQuaternion::Subtract(
		const Vector3D &end,
		const Vector3D &start
	)
{
	Check_Pointer(this);
	Check_Object(&start);
	Check_Object(&end);

	UnitVector3D
		s,e;

	s = start;
	e = end;
	return Subtract(e, s);
}
Beispiel #25
0
TEST(SubtractionTest, CanSubtructIfEquals)
{
	BigInt left, right;
	left.size = 3;
	right.size = 3;

	int leftDigits[] = {123, 45, 46};
	int rightDigits[] = {123, 45, 46};
	SetDigits(&left, leftDigits);
	SetDigits(&right, rightDigits);

	BigInt* result = Subtract(&left, &right);
	int digits[] = {0};

	ASSERT_EQ(1, result->size);
	UnitTestsHelper::AssertDigits(digits, result);
}
Beispiel #26
0
TEST(SubtractionTest, SubtructIfLeftDigitGreaterAndHasRemainder)
{
	BigInt left, right;
	left.size = 3;
	right.size = 2;

	int leftDigits[] = {1998, 0, 10};
	int rightDigits[] = {4567, 123};
	SetDigits(&left, leftDigits);
	SetDigits(&right, rightDigits);

	BigInt* result = Subtract(&left, &right);
	int digits[] = {7431, 9876, 9};

	ASSERT_EQ(3, result->size);
	UnitTestsHelper::AssertDigits(digits, result);
}
Beispiel #27
0
TEST(SubtractionTest, SubtructIfResultSizeIsLess)
{
	BigInt left, right;
	left.size = 3;
	right.size = 2;

	int leftDigits[] = {198, 0, 1};
	int rightDigits[] = {4567, 123};
	SetDigits(&left, leftDigits);
	SetDigits(&right, rightDigits);

	BigInt* result = Subtract(&left, &right);
	int digits[] = {5631, 9876};

	ASSERT_EQ(2, result->size);
	UnitTestsHelper::AssertDigits(digits, result);
}
Beispiel #28
0
ViewDraw::ViewDraw(Ctrl *ctrl)
{
	if(Ctrl::invalid.GetCount())
		Ctrl::DoPaint();
	Ctrl::invalid.Clear();
	Ctrl::RemoveCursor();
	Ctrl::RemoveCaret();
	Rect r = ctrl->GetScreenView();
	Ctrl::invalid.Add(r);
	Ctrl::AddUpdate(r);
	for(int i = max(ctrl->GetTopCtrl()->FindTopCtrl() + 1, 0); i < Ctrl::topctrl.GetCount(); i++) {
		Rect rr = Ctrl::topctrl[i]->GetScreenRect();
		ExcludeClip(rr);
		Subtract(Ctrl::invalid, rr);
	}
	Offset(r.TopLeft());
}
Beispiel #29
0
void Expression()
{
    FirstTerm();
    while(IsAddop(Look)) {
        EmitLn("pushl %eax");
        switch (Look) {
            case '+':
                Add();
                break;
            case '-':
                Subtract();
                break;
            default:
                Expected("Addop");
        }
    }
}
Beispiel #30
0
void Expression(){
   if (IsAddop(Look[0]))
      EmitLn("CLR D0");
   else
      Term();
   EmitLn("MOVE D0,D1");
   while (IsAddop(Look[0])){
      switch (Look[0]){
         case '+': Add();
	    break;
         case '-': Subtract();
	    break;
         default:
            Expected("Addop");
      }
   }
}