void
_acb_poly_revert_series_lagrange_fast(acb_ptr Qinv, acb_srcptr Q, slong Qlen, slong n, slong prec)
{
    slong i, j, k, m;
    acb_ptr R, S, T, tmp;
    acb_t t;

    if (n <= 2)
    {
        if (n >= 1)
            acb_zero(Qinv);
        if (n == 2)
            acb_inv(Qinv + 1, Q + 1, prec);
        return;
    }

    m = n_sqrt(n);

    acb_init(t);
    R = _acb_vec_init((n - 1) * m);
    S = _acb_vec_init(n - 1);
    T = _acb_vec_init(n - 1);

    acb_zero(Qinv);
    acb_inv(Qinv + 1, Q + 1, prec);

    _acb_poly_inv_series(Ri(1), Q + 1, FLINT_MIN(Qlen, n) - 1, n - 1, prec);
    for (i = 2; i <= m; i++)
        _acb_poly_mullow(Ri(i), Ri((i + 1) / 2), n - 1, Ri(i / 2), n - 1, n - 1, prec);

    for (i = 2; i < m; i++)
        acb_div_ui(Qinv + i, Ri(i) + i - 1, i, prec);

    _acb_vec_set(S, Ri(m), n - 1);

    for (i = m; i < n; i += m)
    {
        acb_div_ui(Qinv + i, S + i - 1, i, prec);

        for (j = 1; j < m && i + j < n; j++)
        {
            acb_mul(t, S + 0, Ri(j) + i + j - 1, prec);
            for (k = 1; k <= i + j - 1; k++)
                acb_addmul(t, S + k, Ri(j) + i + j - 1 - k, prec);
            acb_div_ui(Qinv + i + j, t, i + j, prec);
        }

        if (i + 1 < n)
        {
            _acb_poly_mullow(T, S, n - 1, Ri(m), n - 1, n - 1, prec);
            tmp = S; S = T; T = tmp;
        }
    }

    acb_clear(t);
    _acb_vec_clear(R, (n - 1) * m);
    _acb_vec_clear(S, n - 1);
    _acb_vec_clear(T, n - 1);
}
void Rubik::turnCubeUp() { //White to orange
	Ri(false);
	L(false);
	this->shift(this->middle, 0, 1, 5, 2);
	this->shift(this->edges, 4, 17, 14, 0);
	this->shift(this->edges, 12, 16, 5, 2);

	std::map<std::string, std::string> tempMap(movesDictionary);
	//Dictionary
	/*movesDictionary[movesDictionary[_R]] = movesDictionary[_R];
	movesDictionary[movesDictionary[_Ri]] = movesDictionary[_Ri];
	movesDictionary[movesDictionary[_R2]] = movesDictionary[_R2];
	movesDictionary[movesDictionary[_L]] = movesDictionary[_L];
	movesDictionary[movesDictionary[_Li]] = movesDictionary[_Li];
	movesDictionary[movesDictionary[_L2]] = movesDictionary[_L2];*/
	movesDictionary[_U] = tempMap[_B];
	movesDictionary[_Ui] = tempMap[_Bi];
	movesDictionary[_U2] = tempMap[_B2];
	movesDictionary[_B] = tempMap[_D];
	movesDictionary[_Bi] = tempMap[_Di];
	movesDictionary[_B2] = tempMap[_D2];
	movesDictionary[_F] = tempMap[_U];
	movesDictionary[_Fi] = tempMap[_Ui];
	movesDictionary[_F2] = tempMap[_U2];
	movesDictionary[_D] = tempMap[_F];
	movesDictionary[_Di] = tempMap[_Fi];
	movesDictionary[_D2] = tempMap[_F2];
}
Beispiel #3
0
maxint_t S2_hard(maxint_t x, int threads)
{
  if (x < 1)
    return 0;

  double alpha = get_alpha_deleglise_rivat(x);
  string limit = get_max_x(alpha);

  if (x > to_maxint(limit))
    throw primecount_error("S2_hard(x): x must be <= " + limit);

  if (is_print())
    set_print_variables(true);

  int64_t y = (int64_t) (iroot<3>(x) * alpha);
  int64_t z = (int64_t) (x / y);
  int64_t c = PhiTiny::get_c(y);

  if (x <= numeric_limits<int64_t>::max())
    return S2_hard((int64_t) x, y, z, c, (int64_t) Ri(x), threads);
  else
    return S2_hard(x, y, z, c, Ri(x), threads);
}
Beispiel #4
0
void Cube::RotateDown()
{
    L();
    SaveState();
    subCubes[1][0][0] = ROTATE_DOWN(oldSubCubes[1][0][2]);
    subCubes[1][0][1] = ROTATE_DOWN(oldSubCubes[1][1][2]);
    subCubes[1][0][2] = ROTATE_DOWN(oldSubCubes[1][2][2]);
    subCubes[1][1][0] = ROTATE_DOWN(oldSubCubes[1][0][1]);
    subCubes[1][1][1] = ROTATE_DOWN(oldSubCubes[1][1][1]);
    subCubes[1][1][2] = ROTATE_DOWN(oldSubCubes[1][2][1]);
    subCubes[1][2][0] = ROTATE_DOWN(oldSubCubes[1][0][0]);
    subCubes[1][2][1] = ROTATE_DOWN(oldSubCubes[1][1][0]);
    subCubes[1][2][2] = ROTATE_DOWN(oldSubCubes[1][2][0]);
    Ri();
}
void
_fmpq_poly_revert_series_lagrange_fast(fmpz * Qinv, fmpz_t den,
                                    const fmpz * Q, const fmpz_t Qden, slong n)
{
    slong i, j, k, m;
    fmpz *R, *Rden, *S, *T, *dens, *tmp;
    fmpz_t Sden, Tden, t;

    if (fmpz_is_one(Qden) && (n > 1) && fmpz_is_pm1(Q + 1))
    {
        _fmpz_poly_revert_series(Qinv, Q, n);
        fmpz_one(den);
        return;
    }

    if (n <= 2)
    {
        fmpz_zero(Qinv);
        if (n == 2)
        {
            fmpz_set(Qinv + 1, Qden);
            fmpz_set(den, Q + 1);
            _fmpq_poly_canonicalise(Qinv, den, 2);
        }
        return;
    }

    m = n_sqrt(n);

    fmpz_init(t);
    dens = _fmpz_vec_init(n);
    R = _fmpz_vec_init((n - 1) * m);
    S = _fmpz_vec_init(n - 1);
    T = _fmpz_vec_init(n - 1);
    Rden = _fmpz_vec_init(m);
    fmpz_init(Sden);
    fmpz_init(Tden);

    fmpz_zero(Qinv);
    fmpz_one(dens);

    _fmpq_poly_inv_series(Ri(1), Rdeni(1), Q + 1, Qden, n - 1);
    _fmpq_poly_canonicalise(Ri(1), Rdeni(1), n - 1);

    for (i = 2; i <= m; i++)
    {
        _fmpq_poly_mullow(Ri(i), Rdeni(i), Ri(i-1), Rdeni(i-1), n - 1,
                Ri(1), Rdeni(1), n - 1, n - 1);
        _fmpq_poly_canonicalise(Ri(i), Rdeni(i), n - 1);
    }

    for (i = 1; i < m; i++)
    {
        fmpz_set(Qinv + i, Ri(i) + i - 1);
        fmpz_mul_ui(dens + i, Rdeni(i), i);
    }

    _fmpz_vec_set(S, Ri(m), n - 1);
    fmpz_set(Sden, Rdeni(m));

    for (i = m; i < n; i += m)
    {
        fmpz_set(Qinv + i, S + i - 1);
        fmpz_mul_ui(dens + i, Sden, i);

        for (j = 1; j < m && i + j < n; j++)
        {
            fmpz_mul(t, S + 0, Ri(j) + i + j - 1);

            for (k = 1; k <= i + j - 1; k++)
                fmpz_addmul(t, S + k, Ri(j) + i + j - 1 - k);

            fmpz_set(Qinv + i + j, t);
            fmpz_mul(dens + i + j, Sden, Rdeni(j));
            fmpz_mul_ui(dens + i + j, dens + i + j, i + j);
        }

        if (i + 1 < n)
        {
            _fmpq_poly_mullow(T, Tden, S, Sden, n - 1,
                Ri(m), Rdeni(m), n - 1, n - 1);
            _fmpq_poly_canonicalise(T, Tden, n - 1);
            fmpz_swap(Tden, Sden);
            tmp = S; S = T; T = tmp;
        }
    }

    _set_vec(Qinv, den, Qinv, dens, n);
    _fmpq_poly_canonicalise(Qinv, den, n);

    fmpz_clear(t);
    _fmpz_vec_clear(dens, n);
    _fmpz_vec_clear(R, (n - 1) * m);
    _fmpz_vec_clear(S, n - 1);
    _fmpz_vec_clear(T, n - 1);
    _fmpz_vec_clear(Rden, m);
    fmpz_clear(Sden);
    fmpz_clear(Tden);
}
double AnalyticIntegral(
	SpkModel                    &model           ,
	const std::valarray<int>    &N               , 
	const std::valarray<double> &y               ,
	const std::valarray<double> &alpha           ,
	const std::valarray<double> &L               ,
	const std::valarray<double> &U               ,
	size_t                       individual      )
{
	double Pi   = 4. * atan(1.);

	// set the fixed effects
	model.setPopPar(alpha);

	// set the individual
	model.selectIndividual(individual);

	// index in y where measurements for this individual starts
	size_t start = 0;
	size_t i;
	for(i = 0; i < individual; i++)
	{	assert( N[i] >= 0 );
		start += N[i];
	}

	// number of measurements for this individual
	assert( N[individual] >= 0 );
	size_t Ni = N[individual];

	// data for this individual
	std::valarray<double> yi = y[ std::slice( start, Ni, 1 ) ];

	// set the random effects = 0
	std::valarray<double> b(1);
	b[0] = 0.;
	model.setIndPar(b);

	std::valarray<double> Fi(Ni);
	model.dataMean(Fi);

	// model for the variance of random effects
	std::valarray<double> D(1);
	model.indParVariance(D);

	// model for variance of the measurements given random effects
	std::valarray<double> Ri( Ni * Ni );
	model.dataVariance(Ri);

	// determine bHat
	double residual = 0;
	size_t j;
	for(j = 0; j < Ni; j++)
	{	assert( Ri[j * Ni + j] == Ri[0] );
		residual += (yi[j] - Fi[j]);
	}
	std::valarray<double> bHat(1);
	bHat[0] = residual * D[0] / (Ni * D[0] + Ri[0]);

	// now evaluate the Map Bayesian objective at its optimal value
	model.setIndPar(bHat);
	model.dataMean(Fi);
	double sum = bHat[0] * bHat[0] / D[0] + log( 2. * Pi * D[0] );
	for(j = 0; j < Ni; j++)
	{	sum += (yi[j] - Fi[j]) * (yi[j] - Fi[j]) / Ri[0];
		sum += log( 2. * Pi * Ri[0] );
	}
	double GHat = .5 * sum;

	// square root of Hessian of the Map Bayesian objective
	double rootHessian = sqrt( 1. / D[0] + Ni / Ri[0]);

	// determine the values of beta that correspond to the limits
	double Gamma_L = (L[0] - bHat[0]) * rootHessian;
	double Gamma_U = (U[0] - bHat[0]) * rootHessian;

	// compute the upper tails corresponding to the standard normal
	double Q_L    = gsl_sf_erf_Q(Gamma_L);
	double Q_U    = gsl_sf_erf_Q(Gamma_U);

	// analytic value of the integral
	double factor   = exp(-GHat) * sqrt(2. * Pi) / rootHessian;
	return  factor * (Q_L - Q_U);
}
Beispiel #7
0
void Cube::DoMethod(CubeRotateMethod method)
{
    switch (method)
    {
    case ROTATE_NONE:
    case ROTATE_NONEi:
        break;
    case ROTATE_FRONT:
        F();
        break;
    case ROTATE_BACK:
        B();
        break;
    case ROTATE_LEFT:
        L();
        break;
    case ROTATE_RIGHT:
        R();
        break;
    case ROTATE_UP:
        U();
        break;
    case ROTATE_DOWN:
        D();
        break;
    case ROTATE_FRONTi:
        Fi();
        break;
    case ROTATE_BACKi:
        Bi();
        break;
    case ROTATE_LEFTi:
        Li();
        break;
    case ROTATE_RIGHTi:
        Ri();
        break;
    case ROTATE_UPi:
        Ui();
        break;
    case ROTATE_DOWNi:
        Di();
        break;
    case ROTATE_WHOLEX:
        RotateUp();
        break;
    case ROTATE_WHOLEY:
        RotateLeft();
        break;
    case ROTATE_WHOLEZ:
        RotateClockwise();
        break;
    case ROTATE_WHOLEXi:
        RotateDown();
        break;
    case ROTATE_WHOLEYi:
        RotateRight();
        break;
    case ROTATE_WHOLEZi:
        RotateCounterClockwise();
        break;
    default:
        break;
    }
}
Beispiel #8
0
/*!
  Method which enables to compute a NURBS curve approximating a set of data points.
  
  The data points are approximated thanks to a least square method.
  
  The result of the method is composed by a knot vector, a set of control points and a set of associated weights.
  
  \param l_crossingPoints : The list of data points which have to be interpolated.
  \param l_p : Degree of the NURBS basis functions.
  \param l_n : The desired number of control points. l_n must be under or equal to the number of data points.
  \param l_knots : The knot vector.
  \param l_controlPoints : the list of control points.
  \param l_weights : the list of weights.
*/
void vpNurbs::globalCurveApprox(std::vector<vpImagePoint> &l_crossingPoints, unsigned int l_p, unsigned int l_n, std::vector<double> &l_knots, std::vector<vpImagePoint> &l_controlPoints, std::vector<double> &l_weights)
{
  l_knots.clear();
  l_controlPoints.clear();
  l_weights.clear();
  unsigned int m = (unsigned int)l_crossingPoints.size()-1;

  double d = 0;
  for(unsigned int k=1; k<=m; k++)
    d = d + distance(l_crossingPoints[k],1,l_crossingPoints[k-1],1);
  
  //Compute ubar
  std::vector<double> ubar;
  ubar.push_back(0.0);
  for(unsigned int k=1; k<m; k++)
    ubar.push_back(ubar[k-1]+distance(l_crossingPoints[k],1,l_crossingPoints[k-1],1)/d);
   ubar.push_back(1.0);

  //Compute the knot vector
  for(unsigned int k = 0; k <= l_p; k++)
    l_knots.push_back(0.0);
  
  d = (double)(m+1)/(double)(l_n-l_p+1);
  
  for(unsigned int j = 1; j <= l_n-l_p; j++)
  {
    double i = floor(j*d);
    double alpha = j*d-i;
    l_knots.push_back((1.0-alpha)*ubar[(unsigned int)i-1]+alpha*ubar[(unsigned int)i]);
  }

  for(unsigned int k = 0; k <= l_p ; k++)
    l_knots.push_back(1.0);

  //Compute Rk
  std::vector<vpImagePoint> Rk;
  vpBasisFunction* N;
  for(unsigned int k = 1; k <= m-1; k++)
  {
    unsigned int span = findSpan(ubar[k], l_p, l_knots);
    if (span == l_p && span == l_n) 
    {
      N = computeBasisFuns(ubar[k], span, l_p, l_knots);
      vpImagePoint pt(l_crossingPoints[k].get_i()-N[0].value*l_crossingPoints[0].get_i()-N[l_p].value*l_crossingPoints[m].get_i(),
		      l_crossingPoints[k].get_j()-N[0].value*l_crossingPoints[0].get_j()-N[l_p].value*l_crossingPoints[m].get_j());
      Rk.push_back(pt);
      delete[] N;
    }
    else if (span == l_p) 
    {
      N = computeBasisFuns(ubar[k], span, l_p, l_knots);
      vpImagePoint pt(l_crossingPoints[k].get_i()-N[0].value*l_crossingPoints[0].get_i(),
		      l_crossingPoints[k].get_j()-N[0].value*l_crossingPoints[0].get_j());
      Rk.push_back(pt);
      delete[] N;
    }
    else if (span == l_n)
    {
      N = computeBasisFuns(ubar[k], span, l_p, l_knots);
      vpImagePoint pt(l_crossingPoints[k].get_i()-N[l_p].value*l_crossingPoints[m].get_i(),
		      l_crossingPoints[k].get_j()-N[l_p].value*l_crossingPoints[m].get_j());
      Rk.push_back(pt);
      delete[] N;
    }
    else
    {
      Rk.push_back(l_crossingPoints[k]);
    }
  }

  vpMatrix A(m-1,l_n-1);
  //Compute A
  for(unsigned int i = 1; i <= m-1; i++)
  {
    unsigned int span = findSpan(ubar[i], l_p, l_knots);
    N = computeBasisFuns(ubar[i], span, l_p, l_knots);
    for (unsigned int k = 0; k <= l_p; k++)
    {
      if (N[k].i > 0 && N[k].i < l_n)
        A[i-1][N[k].i-1] = N[k].value;
    }
    delete[] N;
  }
  
  vpColVector Ri(l_n-1);
  vpColVector Rj(l_n-1);
  vpColVector Rw(l_n-1);
  for (unsigned int i = 0; i < l_n-1; i++)
  {
    double sum =0;
    for (unsigned int k = 0; k < m-1; k++) sum = sum + A[k][i]*Rk[k].get_i();
    Ri[i] = sum;
    sum = 0;
    for (unsigned int k = 0; k < m-1; k++) sum = sum + A[k][i]*Rk[k].get_j();
    Rj[i] = sum;
    sum = 0;
    for (unsigned int k = 0; k < m-1; k++) sum = sum + A[k][i]; //The crossing points weigths are equal to 1.
    Rw[i] = sum;
  }
  
  vpMatrix AtA = A.AtA();
  vpMatrix AtAinv;
  AtA.pseudoInverse(AtAinv);
  
  vpColVector Pi = AtAinv*Ri;
  vpColVector Pj = AtAinv*Rj;
  vpColVector Pw = AtAinv*Rw;
  
  vpImagePoint pt;
  l_controlPoints.push_back(l_crossingPoints[0]);
  l_weights.push_back(1.0);
  for (unsigned int k = 0; k < l_n-1; k++)
  {
    pt.set_ij(Pi[k],Pj[k]);
    l_controlPoints.push_back(pt);
    l_weights.push_back(Pw[k]);
  }
  l_controlPoints.push_back(l_crossingPoints[m]);
  l_weights.push_back(1.0);
}