Esempio n. 1
0
int main()
{
	int n;
	FILE *fin=fopen("sprime.in","r");
	fout=fopen("sprime.out","w");


	fscanf(fin, "%d", &n);
	
	sprime(2, n-1);
	sprime(3, n-1);
	sprime(5, n-1);
	sprime(7, n-1);
	return 0;
}
Esempio n. 2
0
int main() {
	freopen(INPUT, "r", stdin);
	freopen(OUTPUT, "w", stdout);
	int i, n;
	scanf("%d", &n);
	sprime(0, n, n);
	exit(0);
}
Esempio n. 3
0
void sprime(int n, int ndigit)
{
	if(ndigit == 0) {
		fprintf(fout, "%d\n", n);
		return;
	}
	
	n *= 10;
	if(isprime(n+1))
		sprime(n+1, ndigit-1);
	if(isprime(n+3))
		sprime(n+3, ndigit-1);
	if(isprime(n+7))
		sprime(n+7, ndigit-1);
	if(isprime(n+9))
		sprime(n+9, ndigit-1);
}
Esempio n. 4
0
int sprime(int n, int ndigit, int to) {
	if (ndigit == 0) {
		printf("%d\n", n);
		return;
	}

	n *= 10;
	int j = 1;
	if (ndigit == to) {
		j = 2;
	}
	for (; j <= 9; j++) {
		if (isprime(n + j))
			sprime(n + j, ndigit - 1, to);
	}
}
Esempio n. 5
0
/*!
 * \brief Calculate natural spline interpolation of current data
 */
void Data2D::interpolate(bool constrained)
{
	/* For a set of N points {x(i),y(i)} for i = 0 - (N-1), the i'th interval can be represented by a cubic spline
	 * 
	 * 	S(i)(x) = a(i) + b(i)(x - x(i)) + c(i)(x - x(i))**2 + d(i)h(x - x(i))**3.
	 * 
	 * As such, there are 4N unknown coefficients - {a(0),b(0),c(0),d(0),a(1),b(1),c(1),d(1),...,a(N-1),b(N-1),c(N-1),d(N-1)}
	 * 
	 * Conditions:
	 * 	At the extreme values of x in each interval, the original y values are retained
	 * 		S(i)(x = x(i)) = y(i)    and 	S(i)(x = x(i+1)) = y(i+1)		== 2N equations
	 * 	First derivatives are continuous across intervals (i.e. match at extreme x values of intervals)
	 * 		S'(i)(x = x(i+1)) = S'(i+1)(x = x(i+1))					== (N-1) equations
	 * 	Second derivatives are continuous across intervals (i.e. match at extreme x values of intervals)
	 * 		S"(i)(x = x(i+1)) = S"(i+1)(x = x(i+1))					== (N-1) equations
	 * 
	 * So, there are 4N-2 equations specified here - the remaining two come from specifying endpoint conditions of the spline fit. 
	 * No derivative matching is (can) be performed at the extreme points i=0 and i=N-1, so specify exact derivatives at these points:
	 * 	Natural splines:
	 * 		S'(0) = 0	S'(N-1) = 0
	 *
	 * We need expressions for the derivatives of S:
	 * 	Let h(i) = x - x(i)
	 * 	S(i)(x) = a(i) + b(i)h(i) + c(i)h(i)**2 + d(i)h(i)**3
	 * 	S'(i)(x) = b(i) + 2 c(i)h(i) + 3 d(i)h(i)**2
	 * 	S"(i)(x) = 2 c(i) + 6 d(i)h(i)
	 * 
	 * From the conditions outlined above:
	 * 	S(i)(x = x(i)) = y(i)	  ==>	a(i) = y(i)	since x = x(i) and so h(i) = x(i) - x(i) = 0
	 * 	S(i)(x = x(i+1)) = y(i+1) ==>	a(i) + b(i)h(i) + c(i)h(i)**2 + d(i)h(i)**3 = y(i+1)
	 * 	S'(i)(x = x(i+1)) = S'(i+1)(x = x(i+1))   ==>	b(i) + 2 c(i)h(i) + 3 d(i)h(i)**2 - b(i+1) = 0
	 * 							since at x = x(i+1), h(i+1) = 0 and so S'(i+1)(x = x(i+1)) = b(i+1)
	 * 	S"(i)(x = x(i+1)) = S"(i+1)(x = x(i+1))   ==>	2 c(i) + 6 d(i)h(i) - 2 c(i+1) = 0
	 * 							since at x = x(i+1), h(i+1) = 0 and so S"(i+1)(x = x(i+1)) = 2 c(i+1)
	 * To summarise:
	 * 	a(i) = y(i)						for i = 0,N-1	(N eqs)
	 * 	a(i) + b(i)h(i) + c(i)h(i)**2 + d(i)h(i)**3 = y(i+1)	for i = 0,N-1	(N eqs)
	 * 	b(i) + 2 c(i)h(i) + 3 d(i)h(i)**2 - b(i+1) = 0		for i = 0,N-2	(N-1 eqs)
	 * 	2 c(i) + 6 d(i)h(i) - 2 c(i+1) = 0			for i = 0,N-2	(N-1 eqs)
	 * 
	 * To simplify, consider value of second derivative in i'th interval at leftmost value:
	 * 	S"(i)(x = x(i)) = 2 c(i)	since at x = x(i+1), h(i+1) = 0 and so S"(i+1)(x = x(i+1)) = 2 c(i+1)
	 * We will call these coefficients m(i):
	 * 	2 c(i) + 6 d(i)h(i) - 2 c(i+1) = 0	==>	m(i) + 6 d(i)h(i) - m(i+1) = 0
	 * 						==>	d(i) = m(i+1) - m(i)
	 * 							       -------------
	 * 								   6 h(i)
	 * 											  m(i)		m(i+1) - m(i)
	 * 	y(i) + b(i)h(i) + c(i)h(i)**2 + d(i)h(i)**3 = y(i+1)	==>	y(i) + b(i)h(i) + --- h(i)**2 + ------------- h(i)**3 = y(i+1)
	 * 											   2		    6 h(i)
	 * 
	 * 									       y(i+1) - y(i)   m(i)	  m(i+1) - m(i)
	 * 								==>	b(i) = ------------- - --- h(i) - ------------- h(i)
	 * 										    h(i)	2	        6
	 * 
	 * 
	 * 	b(i) + 2 c(i)h(i) + 3 d(i)h(i)**2 = b(i+1)	==>	
	 * 	( y(i+1) - y(i)   m(i)	     m(i+1) - m(i)	)		 m(i+1) - m(i)		 ( y(i+2) - y(i+1)   m(i+1)	     m(i+2 - m(i+1)       )
	 * 	( ------------- - --- h(i) - ------------- h(i) ) + m(i)h(i) + 3 ------------- h(i)**2 = ( --------------- - ------ h(i+1) - ------------- h(i+1) )
	 * 	(      h(i)	   2	           6		)		     6 h(i)		 (      h(i+1)	        2	            6	          ) 
	 * 
	 * 								      ( y(i+2) - y(i+1)   y(i+1) - y(i) )
	 * 	==>	h(i)m(i) + 2 (h(i) + h(i+1))m(i+1) + h(i+1)m(i+2) = 6 ( --------------- - ------------- )
	 * 								      (      h(i+1)           h(i)	)
	 * 
	 * Written in matrix form, the above system of equations is:
	 * 
	 * 	[ 1		0		0		0	...	0 ] [  m(0)  ]     [			0			 ]
	 * 	[ h(0)		2(h(0) + h(1))	h(1)		0	...	0 ] [  m(1)  ]     [ (y(2) - y(1)) / h(1) - (y(1) - y(0)) / h(0) ]
	 * 	[ 0		h(1)		2(h(1) + h(2))	h(2)	...	0 ] [  m(2)  ] = 6 [ (y(3) - y(2)) / h(2) - (y(2) - y(1)) / h(1) ]
	 * 	[			...				...	  ] [  ...   ]     [
	 * 	[ 0		0		0		0	...	1 ] [ m(N-1) ]     [ 			0			 ]
	 * 
	 * Once solved, we have the set of m() and can thus determine a, b, c, and d as follows:
	 *
	 *			       y(i+1)-y(i)   h(i)        h(i)			       m(i)	       m(i+1)-m(i)
	 * 	a(i) = y(i)	b(i) = ----------- - ---- m(i) - ---- (m(i+1)-m(i))	c(i) = ----	d(i) = -----------
	 * 				  h(i)	      2	          6				2		  6 h(i)
	 * 
	 * The constrained spline fit removes the requirement that the second derivatives are equal at the boundaries of every interval, and instead specifies
	 * that an exact first derivative is obtained there instead. As such:
	 * 
	 * 	S"(i)(x = x(i+1)) = S"(i+1)(x = x(i+1))	  ==>	S'(i)(x = x(i+1)) = S'(i+1)(x = x(i+1)) = f'(x = x(i+1))
	 * 
	 * A suitable estimate of the slope comes from consideration of the slopes of the two adjacent intervals to x(i):
	 * 
	 * 		      ( x(i+1)-x(i)   x(i)-x(i-1) )
	 * 	f'(i) = 0.5 * ( ----------- + ----------- )	for i=1, N-2
	 * 		      ( y(i+1)-y(i)   y(i)-y(i-1) )
	 * 
	 * At the endpoints:
	 * 
	 * 		3( y(1)-y(0) )   f'(1)			  3( y(i-1) - y(n-2) )   f'(N-1)
	 * 	f'(0) = -------------- - -----		f'(N-1) = -------------------- - -------
	 * 		2( x(1)-x(0) )     2			  2( x(i-1) - x(n-2) )      2
	 * 
	 * Since the slope at each point is known, there is no longer a need to solve a system of equations, and each coefficient may be calculated as follows:
	 * 
	 * 		  2( f'(i)+2f'(i-1) )   6(y(i) - y(i-1)
	 * 	S"(i) =   ------------------- - ----------------	and  S"(i-1) = -S"(i)
	 * 		     x(i) - x(i-1)	(x(i)-x(i-1))**2
	 * 
	 * 	       S"(i) - S"(i-1)		       x(i)S"(i-1)-x(i-1)S"(i)
	 * 	d(i) = ---------------		c(i) = -----------------------
	 * 	       6(x(i) - x(i-1))			   2(x(i) - x(i-1))
	 * 
	 * 	       (y(i) - y(i-1)) - c(i)(x(i)**2 - x(i-1)**2) - d(i)(x(i)**3 - x(i-1)**3)
	 * 	b(i) =------------------------------------------------------------------------
	 * 					x(i) - x(i-1)
	 */
	
	int i, nPoints = x_.nItems();
	
	// Do a quick test on splineH_ and constrainedSpline_ to see if we need to recalculate the interpolation
// 	if ((splineH_.nItems() != 0) && (constrainedSpline_ == constrained)) return;

	// Calculate interval array 'h'
	splineH_.createEmpty(nPoints);
	for (i=0; i<nPoints-1; ++i) splineH_[i] = x_[i+1] - x_[i];

	// Initialise parameter arrays and working array
	splineA_.createEmpty(nPoints);
	splineB_.createEmpty(nPoints);
	splineC_.createEmpty(nPoints);
	splineD_.createEmpty(nPoints);

	constrainedSpline_ = constrained;
	
	if (!constrainedSpline_)
	{
		/*
		 * Unconstrained Spline Fit
		 */
		// Determine 'm' values (== C) now by solving the tridiagonal matrix above. Use Thomas algorithm...
		// -- First stage
		double p, q, r, s;
		Array<double> rprime(nPoints), sprime(nPoints);
		rprime[0] = 0.0;	// Would be 0.0 / 1.0 in the case of Natural spline
		sprime[0] = 0.0;

		for (i=1; i<nPoints-1; ++i)
		{
			// For a given i, p(i) = h(i-1), q(i) = 2(h(i-1)+h(i)), r(i) = h(i), s(i) = 6 ((y(n+1) - y(n)) / h(n) - (y(n) - y(n-1)) / h(n-1)
			p = splineH_[i-1];
			q = 2.0*(splineH_[i-1]+splineH_[i]);
			r = splineH_[i];
			s = 6.0 * ((y_[i+1] - y_[i]) / splineH_[i] - (y_[i] - y_[i-1]) / splineH_[i-1]);

			// -- Calculate r'(i) = r(i) / ( q(i) - r'(i-1)p(i) )
			rprime[i] = r / (q - rprime[i-1]*p);
			// -- Calculate s'(i) = (s(i) - s'(i-1)p(i) ) / ( q(i) - r'(i-1)p(i))
			sprime[i] = (s - sprime[i-1]*p) / (q - rprime[i-1]*p);
		}
		rprime[nPoints-1] = 0.0;
		sprime[nPoints-1] = (0.0 - sprime[nPoints-2]/splineH_[i-1]) / (2.0*splineH_[i-1]);

		// -- Second stage - backsubstitution
		splineC_[nPoints-1] = 0.0;
		for (i=nPoints-2; i>=0; --i)
		{
			// For a given i, m(i) = s'(i) - r'(i)m(i+1)
			splineC_[i] = sprime[i] - rprime[i]*splineC_[i+1];
		}
		
		// splineC_ array now contains m(i)...
		for (i=0; i<nPoints-1; ++i)
		{
			splineB_[i] = (y_[i+1] - y_[i]) / splineH_[i] - 0.5 * splineH_[i] * splineC_[i] - (splineH_[i] * (splineC_[i+1]-splineC_[i]))/6.0;
			splineD_[i] = (splineC_[i+1] - splineC_[i]) / (6.0 * splineH_[i]);
			splineC_[i] *= 0.5;
			splineA_[i] = y_[i];
		}
	}
	else
	{
		/*
		 * Constrained Spline fit
		 */
		
		// Calculate first derivatives at each point
		Array<double> fp(nPoints);
		double gradA, gradB;
		for (i=1; i<nPoints-1; ++i)
		{
			gradA = (x_[i+1] - x_[i])/(y_[i+1] - y_[i]);
			gradB = (x_[i] - x_[i-1])/(y_[i] - y_[i-1]);
			if (UChromaMath::sgn(gradA) != UChromaMath::sgn(gradB)) fp[i] = 0.0;
			else fp[i] = 2.0 / (gradA + gradB);
			
		}
// 		fp[0] = (3.0*(y_[1] - y_[0])) / (2.0*x_[1]-x_[0]) - 0.5*fp[1];
// 		fp[nPoints-1] = (3.0*(y_[nPoints-1] - y_[nPoints-2])) / (2.0*x_[nPoints-1]-x_[nPoints-2]) - 0.5*fp[nPoints-2];
		fp[0] = 0.0;
		fp[nPoints-1] = 0.0;

		// Calculate coefficients
		double fppi, fppim1, dx, dy;
		for (i=1; i<nPoints; ++i)
		{
			dx = x_[i] - x_[i-1];
			dy = y_[i] - y_[i-1];
			fppim1 = -2.0*(fp[i]+2.0*fp[i-1]) / dx + 6.0*dy/(dx*dx);
			fppi = 2.0*(2.0*fp[i]+fp[i-1]) / dx - 6.0*dy/(dx*dx);
			splineD_[i-1] = (fppi - fppim1) / (6.0*dx);
			splineC_[i-1] = (x_[i]*fppim1 - x_[i-1]*fppi) / (2.0*dx);
			splineB_[i-1] = (dy - splineC_[i-1]*(x_[i]*x_[i] - x_[i-1]*x_[i-1]) - splineD_[i-1]*(x_[i]*x_[i]*x_[i] - x_[i-1]*x_[i-1]*x_[i-1])) / dx;
			splineA_[i-1] = y_[i-1] - splineB_[i-1]*x_[i-1] - splineC_[i-1]*x_[i-1]*x_[i-1] - splineD_[i-1]*x_[i-1]*x_[i-1]*x_[i-1];
		}
	}

	splineInterval_ = 0;
}
Esempio n. 6
0
void QpGenData::datarandom( OoqpVector & x, OoqpVector & y,
			    OoqpVector & z, OoqpVector & s )
{
  double drand( double * );
  double ix = 3074.20374;
  
  OoqpVectorHandle xdual(la->newVector( nx ));
  this->randomlyChooseBoundedVariables( x, *xdual,
  					*blx, *ixlow, *bux, *ixupp,
  					&ix, .25, .25, .25 );

  {
//      ofstream x_vec( "x" );
//      x->writeToStream( x_vec );
//      ofstream eta_vec( "xdual" );
//      eta_vec.precision(16);
//      xdual->writeToStream( eta_vec );
//      ofstream blx_vec( "blx" );
//      blx->writeToStream( blx_vec );
//      ofstream bux_vec( "bux" );
//      bux->writeToStream( bux_vec );
  } 

  OoqpVectorHandle sprime(la->newVector( mz ));
  this->randomlyChooseBoundedVariables( *sprime, z,
  					*bl, *iclow, *bu, *icupp,
  					&ix, .25, .25, .5 );
  
  {
    //      	ofstream z_vec( "z" );
    //      	z_vec << z << endl;
  }
  {
    Q->randomizePSD( &ix );
//      ofstream Q_mat( "Q" );
//      Q_mat << Q << endl;
  }
  { 
    A->randomize( -10.0, 10.0, &ix );
    //  	ofstream A_mat( "A" );
    //  	A_mat << A << endl;
  }
  {
    C->randomize( -10.0, 10.0, &ix );
    //  	ofstream C_mat( "C" );
    //  	C_mat << C << endl;
  }

  y.randomize( -10.0, 10.0, &ix );
  {
    //  	ofstream y_vec( "y" );
    //  	y_vec << y << endl;
  }
  // g = - Q x + A\T y + C\T z + xdual 
  g->copyFrom( *xdual );
  Q->mult( 1.0, *g, -1.0, x );
  A->transMult( 1.0, *g, 1.0, y );
  C->transMult( 1.0, *g, 1.0, z );
  // bA = A x
  A->mult( 0.0, *bA, 1.0, x );
  {
    //  	ofstream bA_vec( "bA" );
    //  	bA_vec << bA << endl;
  }
  // Have a randomly generated sprime.
  // C x - s = 0. Let q + sprime = s, i.e. q = s - sprime.
  // Compute s and temporarily store in q
  C->mult( 0.0, s, 1.0, x );
  // Now compute the real q = s - sprime
  OoqpVectorHandle q(la->newVector( mz ));
  q->copyFrom( s );
  q->axpy( -1.0, *sprime );
  // Adjust bl and bu appropriately
  bl->axpy( 1.0, *q );
  bu->axpy( 1.0, *q );
  
  bl->selectNonZeros( *iclow );
  bu->selectNonZeros( *icupp );
  
  {
    //   	ofstream bl_vec( "bl" );
    //      	bl_vec << bl << endl;
    //      	ofstream bu_vec( "bu" );
    //      	bu_vec << bu << endl;
  }
}