Ejemplo n.º 1
0
int main()
{
  printf("\n   Dla  równania  f(x) = 0,  gdzie  f(x) = 3x + 2 - e^x,  wczytać  a, b  należące");
  printf("\n   do zbioru liczb rzeczywistych takie, by a < b oraz f(a) * f(b) < 0. Następnie,");
  printf("\n   dopóki  \"użytownik się nie znudzi\",  wczytywać  wartości  0 < E < 1  i  metodą");
  printf("\n   połowienia  na  [a, b]  przybliżyc z dokładnością E rozwiązanie tego równania.");
  printf("\n   Rozwiązanie to przybliżyc również metodą Newtona  z  x_0 = a,  przy czym,  x_k");
  printf("\n   będzie  dobrym   przybliżeniem,  gdy  |x_k - x_(k - 1)| <= E.  Porównać  ilość");
  printf("\n   kroków wykonanych metodą połowienia i metodą Newtona.\n");

  double a, b;

  printf("\n   Podaj a należące do zbioru liczb rzeczywistych:\n   a = ");
  scanf("%lf", &a);

  b = a;

  while(a >= b || equation(a) * equation(b) >= 0)
  {
    printf("\n   Podaj b należące do zbioru liczb rzeczywistych,\n   takie by b > a oraz f(b) * f(a) < 0\n   b = ");
    scanf("%lf", &b);
  }

  double epsilon, rB, rN;
  int choice;

  while (1)
  {
    printf("\n   1 - Przybliż rozwiązanie równania z dokładnością E metodą połowienia i Newtona");
    printf("\n   2 - Zakoncz program");
    printf("\n   Twoj wybor: ");
    scanf("%i", &choice);

    switch (choice)
    {
      case 1:
        epsilon = 0;

        while (epsilon <= 0 || epsilon >= 1)
        {
          printf("\n   Podaj E, takie by 0 < E < 1:\n   E = ");
          scanf("%lf", &epsilon);
        }

        rB = bisection(a, b, epsilon);
        rN = newton(a, epsilon);

        printf("\n   Metoda połowienia:\n   %lf - przybliżone rozwiązanie\n   %i - ilość kroków\n", rB, iB);
        printf("\n   Metoda Newtona:\n   %lf - przybliżone rozwiązanie\n   %i - ilość kroków\n", rN, iN);

        iB = iN = 0;

        break;

      case 2:
        printf("\n");
        return 0;
    }
  }
}
Ejemplo n.º 2
0
void MorrisLecar::rungekutta(){
	 V0=V;
	n0=n;
	double CouplePlusNoise=Network::sm_gCouple[No]/C+Network::sm_gNoise[No];
	equation();
	DV1=(DV+CouplePlusNoise)*Network::sm_dt;
	Dn1=Dn*Network::sm_dt;
	V=V0+DV1/2.0;
	n=n0+Dn1/2.0;
	equation();
	DV2=(DV+CouplePlusNoise)*Network::sm_dt;
	Dn2=Dn*Network::sm_dt;
	V=V0+DV2/2.0;
	n=n0+Dn2/2.0;
	equation();
	DV3=(DV+CouplePlusNoise)*Network::sm_dt;
	Dn3=Dn*Network::sm_dt;
	V=V0+DV3;
	n=n0+Dn3;
	equation();
	DV4=(DV+CouplePlusNoise)*Network::sm_dt;
	Dn4=Dn*Network::sm_dt;
	V=V0+(DV1+2.0*DV2+2.0*DV3+DV4)/6.0;
	n=n0+(Dn1+2.0*Dn2+2.0*Dn3+Dn4)/6.0;
}
Ejemplo n.º 3
0
	void DRBPointLightData::updateRange(const glm::mat4 &transformation)
	{
		float errorRate = 0.01f;
		glm::vec3 lightRange;
		glm::vec3 equation(_range.z, _range.y, _range.x);

		// if the value of equation.z + equation.y * dist + equation.x * dist * dist == 256
		// then the pixels are not lighted anymore (pxlColor = final_color / attenuation)
		equation.z -= 256;
		if (equation.x == 0) // first degree
		{
			// infinite range if there is no distance attenuation
			AGE_ASSERT(equation.y != 0);
			lightRange = glm::vec3(-equation.z / equation.y);
		}
		else // second degree
		{
			float d = Mathematic::secondDegreeDiscriminant(equation);
			glm::vec2 res = Mathematic::resolveSecondDegree(equation, d);
			lightRange = glm::vec3(glm::max(res.x, res.y));
		}
		AGE_ASSERT(lightRange.x > 0);
		_sphereTransform = glm::scale(transformation, lightRange + lightRange * errorRate);
		_positionLightProperty->autoSet(glm::vec3(_sphereTransform[3]));
		setTransformation(_sphereTransform);
	}
Ejemplo n.º 4
0
  bool Mod2_System::add (I begin, I end, unsigned int rhs)
{
  if (not consistent())
    return false; // an already inconsistent system remains so
  bitmap::BitMap lhs(size(),begin,end); // convert iterators to bitmap

  // now new equation is given by |lhs| and |rhs|; reduce it w.r.t. old ones:
  for (unsigned int i=0; i<eqn.size(); ++i)
    if (lhs.isMember(eqn[i].lhs[0]))
      rhs += eliminate(i,lhs);

  if (lhs.empty() and rhs%2==0)
    return true; // we've ended up with a trivial equation, don't add it!

  unsigned long our_index = eqn.size(); // the number of the new equation
  eqn.push_back(equation(rhs)); // push equation with for now empty LHS
  equation& eq = eqn.back();
  if (eq.lhs.empty())
    return false; // we're done, and have just added an inconsistent equation

  eq.lhs.assign(lhs.begin(),lhs.end()); // else fill in LHS from the |BitMap|

  pivot_index[eq.lhs[0]] = our_index; // we are the pivot for column |lhs[0]|
  return true;
}
void CEquSystem::releaseVariables()
{
	for (auto i = equNumb(); i--;)
		equation(i)->releaseEquation();

	resetEquNumb();
}
bool CEquSystem::isSolved() const
{
	for (auto i = equNumb(); i--;)
		if (!equation(i)->solved())
			return false;

	return true;
}
Ejemplo n.º 7
0
double bisection(double left, double right, double epsilon)
{
  double point = (left + right) / 2;

  while(fabs(left - right) > epsilon)
  {
    iB++;

    point = (left + right) / 2;

    if (equation(point) == 0) break;
    else if (equation(point) * equation(left) < 0) right = point;
    else left = point;
  }

  return point;
}
Ejemplo n.º 8
0
std::string
Polynomial::toString() const
{
	std::ostringstream stream;
	stream << "[Polynomial][" << name() << "][" << equation() << "][" <<
		   parameterCount() << " parameters]";
	return stream.str();
}
Ejemplo n.º 9
0
void SplineFitting::CalcSpline(double *Xi, double *Yi, int n, int boundType, double b1, double b2)
{
    assert((boundType == 1) || (boundType == 2));

    double *matrixA = new double[n * n];
    if(matrixA == NULL)
    {
        return;
    }
    double *d = new double[n];
    if(d == NULL)
    {
        delete[] matrixA;
        return;
    }

    m_valN = n;
    m_valXi.assign(Xi, Xi + m_valN);
    m_valYi.assign(Yi, Yi + m_valN);
    m_valMi.resize(m_valN);
    memset(matrixA, 0, sizeof(double) * n * n);

    matrixA[ARR_INDEX(0, 0, m_valN)] = 2.0;
    matrixA[ARR_INDEX(m_valN - 1, m_valN - 1, m_valN)] = 2.0;
    if(boundType == 1) /*第一类边界条件*/
    {
        matrixA[ARR_INDEX(0, 1, m_valN)] = 1.0; //v0
        matrixA[ARR_INDEX(m_valN - 1, m_valN - 2, m_valN)] = 1.0; //un
        double h0 = Xi[1] - Xi[0];
        d[0] = 6 * ((Yi[1] - Yi[0]) / h0 - b1) / h0; //d0
        double hn_1 = Xi[m_valN - 1] - Xi[m_valN - 2];
        d[m_valN - 1] = 6 * (b2 - (Yi[m_valN - 1] - Yi[m_valN - 2]) / hn_1) / hn_1; //dn
    }
    else /*第二类边界条件*/
    {
        matrixA[ARR_INDEX(0, 1, m_valN)] = 0.0; //v0
        matrixA[ARR_INDEX(m_valN - 1, m_valN - 2, m_valN)] = 0.0; //un
        d[0] = 2 * b1; //d0
        d[m_valN - 1] = 2 * b2; //dn
    }
    /*计算ui,vi,di,i = 2,3,...,n-1*/
    for(int i = 1; i < (m_valN - 1); i++)
    {
        double hi_1 = Xi[i] - Xi[i - 1];
        double hi = Xi[i + 1] - Xi[i];
        matrixA[ARR_INDEX(i, i - 1, m_valN)] = hi_1 / (hi_1 + hi); //ui
        matrixA[ARR_INDEX(i, i, m_valN)] = 2.0; 
        matrixA[ARR_INDEX(i, i + 1, m_valN)] = 1 - matrixA[ARR_INDEX(i, i - 1, m_valN)]; //vi = 1 - ui
        d[i] = 6 * ((Yi[i + 1] - Yi[i]) / hi - (Yi[i] - Yi[i - 1]) / hi_1) / (hi_1 + hi); //di
    }

    ThomasEquation equation(m_valN, matrixA, d);
    equation.Resolve(m_valMi);
    m_bCalcCompleted = true;
    
    delete[] matrixA;
    delete[] d;
}
TEST_F(SquareEquationTest, deltaIsNonNegative)
{
	std::vector<double> polynom{-2,1,1};
	SquareEquation equation(polynom);
	std::vector<double> result;
	result.push_back(1.);
	result.push_back(-2.);
	EXPECT_EQ(equation.solveEquation(), result);
}
Ejemplo n.º 11
0
void MarchingCubes::insideOutsideTest(float (*equation) (int, int, int), float isoValue) {
   for (int x = 0; x < size.x + 1; x++) {
      for (int y = 0; y < size.y + 1; y++) {
         for (int z = 0; z < size.z + 1; z++) {
            points[index(x, y, z)] = equation(x - size.x/2, y - size.y/2,
             z - size.z/2) < isoValue;
         }
      }
   }
}
void render(image *im) {
	// set number of threads
	omp_set_num_threads(num_threads);
	int i, j;
	// division is used for scaling the picture to 100 logical pixels
	int division = Logic < resolution ? resolution/Logic : Logic/resolution;
	if(resolution > Logic){
		#pragma omp parallel for private(j)
		for(i = 0; i < resolution; i++)
			for(j = 0; j < resolution; j++)
				// calculate distance from each pixel and color it
				im -> map[i][j][0] = equation(i / division, j / division);
	}
	else {
		#pragma omp parallel for private(j)
		for(i = 0; i < resolution; i++)
			for(j = 0; j < resolution; j++)
				// calculate distance from each pixel and color it
				im -> map[i][j][0] = equation(i * division, j * division);
	}

}
Ejemplo n.º 13
0
void abstractor_provert::pred_abstract_block(
    goto_programt::const_targett target,
    const predicatest &predicates,
    abstract_transition_relationt &
    abstract_transition_relation)
{
#if 0
  symex_target_equationt equation(ns);

  std::vector<exprt> curr_predicates,
    next_predicates,
    predicates_wp;
  std::list<exprt> constraints;

  build_equation(
      ns,
      predicates,
      concrete_model,
      target,
      constraints,
      equation,
      curr_predicates,
      next_predicates,
      predicates_wp);

#if 0
  std::cout << equation;
#endif

  // make them all non-deterministic for now
  for(unsigned i=0; i<predicates.size(); i++)
    abstract_transition_relation.values[i].make_nil();

  // predicate image

#ifdef HAVE_PROVER
  predicate_image_prover(
      message_handler,
      curr_predicates,
      next_predicates,
      predicates_wp,
      constraints,
      equation,
      ns,
      abstract_transition_relation);
#else
  throw "no support for prover linked in";
#endif
#endif
}
CEquSystem::~CEquSystem()	
{ 
	delete[] varPntr();
	delete[] varDefinedPtr();
	delete[] getDefEquationPntr();

	releaseVariables();
	for (auto i = equNumbMax(); i--;)
		delete equation(i);

	delete[] equPntr();
	delete[] equIndx();
	delete equArray();
}
Ejemplo n.º 15
0
vector<double> equation(const T &f){
	vector<double> res;
	if(f.degree() == 1){
		if(sign(f.coef[1]))res.push_back(-f.coef[0] / f.coef[1]);
		return res;
	}
	vector<double> droot = equation(f.derivative());
	droot.insert(droot.begin(), -INF);
	droot.push_back(INF);
	for(int i=0;i<(int)droot.size()-1;i++){
		double tmp = find(f, droot[i], droot[i + 1]);
		if(tmp < INF) res.push_back(tmp);
	}
	return res;
}
Ejemplo n.º 16
0
void MorrisLecar::rungekutta_s(){
		V0=V;
		n0=n;
		equation();
		DV1=DV*single_dt;
		Dn1=Dn*single_dt;
		V=V0+DV1/2.0;
		n=n0+Dn1/2.0;
		equation();
		DV2=DV*single_dt;
		Dn2=Dn*single_dt;
		V=V0+DV2/2.0;
		n=n0+Dn2/2.0;
		equation();
		DV3=DV*single_dt;
		Dn3=Dn*single_dt;
		V=V0+DV3;
		n=n0+Dn3;
		equation();
		DV4=DV*single_dt;
		Dn4=Dn*single_dt;
		V=V0+(DV1+2.0*DV2+2.0*DV3+DV4)/6.0;
		n=n0+(Dn1+2.0*Dn2+2.0*Dn3+Dn4)/6.0;
}
Ejemplo n.º 17
0
double newton(double left, double epsilon)
{
  double pointPlus, point = left;

  while (1)
  {
    iN++;

    pointPlus = point - equation(point) / equationDerivative(point);

    if (fabs(point - pointPlus) <= epsilon) break;
    else point = pointPlus;
  }

  return pointPlus;
}
bool LeastSquare(const std::vector<double>& x_value, const std::vector<double>& y_value, 
                 int M, std::vector<double>& a_value)
{
    assert(x_value.size() == y_value.size());
    assert(a_value.size() == M);

    double *matrix = new double[M * M];
    double *b= new double[M];

    std::vector<double> x_m(x_value.size(), 1.0);
    std::vector<double> y_i(y_value.size(), 0.0);
    for(int i = 0; i < M; i++)
    {
        matrix[ARR_INDEX(0, i, M)] = std::accumulate(x_m.begin(), x_m.end(), 0.0);
        for(int j = 0; j < static_cast<int>(y_value.size()); j++)
        {
            y_i[j] = x_m[j] * y_value[j];
        }
        b[i] = std::accumulate(y_i.begin(), y_i.end(), 0.0);
        for(int k = 0; k < static_cast<int>(x_m.size()); k++)
        {
            x_m[k] *= x_value[k];
        }
    }
    for(int row = 1; row < M; row++)
    {
        for(int i = 0; i < M - 1; i++)
        {
            matrix[ARR_INDEX(row, i, M)] = matrix[ARR_INDEX(row - 1, i + 1, M)];
        }
        matrix[ARR_INDEX(row, M - 1, M)] = std::accumulate(x_m.begin(), x_m.end(), 0.0);
        for(int k = 0; k < static_cast<int>(x_m.size()); k++)
        {
            x_m[k] *= x_value[k];
        }
    }

    GuassEquation equation(M, matrix, b);
    delete[] matrix;
    delete[] b;

    return equation.Resolve(a_value);
}
Ejemplo n.º 19
0
	float		PointLightComponent::computePointLightRange(float minValue, glm::vec3 const &attenuation)
	{
		glm::vec3 equation(attenuation.z, attenuation.y, attenuation.x - minValue);
		float discriminant = Mathematic::secondDegreeDiscriminant(equation);
		if (discriminant == 0)
		{
			return (Mathematic::resolveSecondDegree(equation));
		}
		else if (discriminant > 0)
		{
			glm::vec2	results = Mathematic::resolveSecondDegree(equation, discriminant);
			return (glm::max(results.x, results.y));
		}
		else
		{
			assert(!"The impossible has happenned :/");
			return (0);
		}
	}
Ejemplo n.º 20
0
int main()
{
  int a, b, c, d;

  printf("(ax+b)(cx+d)\n");

  printf("a = ");
  scanf("%d", &a);

  printf("b = ");
  scanf("%d", &b);

  printf("c = ");
  scanf("%d", &c);

  printf("d = ");
  scanf("%d", &d);
  
  equation(a, b, c, d);

  return 0;  
}
Ejemplo n.º 21
0
int main()
{
	double a, b, c, x1, x2;
	
	printf("Input coefficient of x^2: ");
	scanf("%lf", &a);
	printf("Input coefficient of x: ");
	scanf("%lf", &b);
	printf("Input free member: ");
	scanf("%lf", &c);

	double x = equation(a, b, c, &x1, &x2);

	if (x == NO_ROOTS) {
		printf("Equation hasn't roots.");
	return 0;
	}
	if (x == SAME_ROOT) {
		printf("Equation has one root: x = %lf.\n", x1);
		return 0;
	}
	printf("x1 = %lf, x2 = %lf\n", x1, x2);
	return 0;
}
Ejemplo n.º 22
0
void srs_ui_but::CButProjection::cameraInfoCB(const sensor_msgs::CameraInfo::ConstPtr &cam_info)
{
	//   std::cerr << "Camera callback. Frame id: " << cam_info->header.frame_id << std::endl;

	boost::mutex::scoped_lock( m_cameraInfoLock );

    // Get camera info
    ROS_DEBUG("OctMapPlugin: Set camera info: %d x %d\n", cam_info->height, cam_info->width);
    if( !m_camera_model.fromCameraInfo(*cam_info) )
        return;

    m_camera_size = m_camera_model.fullResolution();

    Ogre::Vector3 position;
    Ogre::Quaternion orientation;
    vis_manager_->getFrameManager()->getTransform(cam_info->header, position, orientation);
  //  const cv::Mat_<double> pm( m_camera_model.projectionMatrix() );

    // convert vision (Z-forward) frame to ogre frame (Z-out)
    orientation = orientation * Ogre::Quaternion(Ogre::Degree(180), Ogre::Vector3::UNIT_X);

    // Get z-axis
    Ogre::Vector3 normal( orientation.zAxis() );
    normal.normalise();

    // Compute camera plane equation
    Ogre::Vector4 equation( normal.x, normal.y, normal.z, -normal.dotProduct( position) );


    float width = cam_info->width;
    float height = cam_info->height;

    // Possibly malformed camera info...
    if( width == 0.0 || height == 0.0 )
        return;

    double fx = cam_info->P[0];
    double fy = cam_info->P[5];
/*
     Ogre::Radian fovy( 2.0*atan(height / (2.0 * fy)) );

    if( fovy != fovy)
    	return; // NAN

    double aspect_ratio = width / height;

    if( aspect_ratio != aspect_ratio )
    	return; //NaN
*/
    // Add the camera's translation relative to the left camera (from P[3]);
    // Tx = -1*(P[3] / P[0])
    double tx = -1.0 * (cam_info->P[3] / fx);
    Ogre::Vector3 right = orientation * Ogre::Vector3::UNIT_X;
    position = position + (right * tx);

//    std::cerr << right * tx << std::endl;

    double ty = -1 * (cam_info->P[7] / fy);
    Ogre::Vector3 down = orientation * Ogre::Vector3::UNIT_Y;
    position = position + (down * ty);

    if( !rviz::validateFloats( position ))
    {
    	return;
    }


    if( m_projectionData != 0 )
    {
		m_projectionData->setProjectorPosition( position );
		m_projectionData->setProjectorOrientation( orientation );

		// f.setFOVy( fovX );
//		m_projectionData->setFOVy( fovy );
//		m_projectionData->setAspectRatio( aspect_ratio );

		m_projectionData->setCameraModel( *cam_info );
		m_projectionData->setProjectorEquation( equation );
		m_projectionData->updateMatrices();
    }
}
Ejemplo n.º 23
0
QString KstEquation::propertyString() const {
  return equation();
}
Ejemplo n.º 24
0
void Leaf::initData()
{
	if (pData)
	{
		delete[] pData;
		delete[] pIndices;
	}

	//example equation: y=+-sqrt(1 - x*x/100) (also is symmetric around x)
	unsigned int nInternalQuaterSteps = 5;
	
	//number of points
	dataCount = 3*(nInternalQuaterSteps*2+1) + 2; 
	//number of triangles
	unsigned int nTriangles = 2*4*nInternalQuaterSteps+4;
	//number of indices
	indicesCount = 3*nTriangles;

	pData = new VertexData [dataCount];
	pIndices = new unsigned int [indicesCount];
	
	//fill in pData array
	//right up
	unsigned int startIndex = 0;
	for (unsigned int i=0; i<nInternalQuaterSteps+1; i++)
	{
		float xPos = (1.0f +i)/(nInternalQuaterSteps+2);
		float yPos = equation(xPos);
			
		pData[startIndex+i].pos = glm::vec3(xPos/2, (yPos+1)/2,0);
		pData[startIndex+i].nor = glm::vec3(0,0,-1);
		pData[startIndex+i].tex = glm::vec2((xPos+1)/2, (yPos+1)/2);	
	}
	//right down
	startIndex += nInternalQuaterSteps+1;
	for (unsigned int i=0; i<nInternalQuaterSteps; i++)
	{
		float xPos = (1.0f + nInternalQuaterSteps-i)/(nInternalQuaterSteps+2);
		float yPos = -equation(xPos);
			
		pData[startIndex+i].pos = glm::vec3(xPos/2, (yPos+1)/2,0);
		pData[startIndex+i].nor = glm::vec3(0,0,-1);
		pData[startIndex+i].tex = glm::vec2((xPos+1)/2, (yPos+1)/2);		
	}
	startIndex += nInternalQuaterSteps;
	//left up
	for (unsigned int i=0; i<nInternalQuaterSteps+1; i++)
	{
		float xPos = -(1.0f+i)/(nInternalQuaterSteps+2);
		float yPos = equation(xPos);
			
		pData[startIndex+i].pos = glm::vec3(xPos/2, (yPos+1)/2,0);
		pData[startIndex+i].nor = glm::vec3(0,0,-1);
		pData[startIndex+i].tex = glm::vec2((xPos+1)/2, (yPos+1)/2);		
	}
	//left down
	startIndex += nInternalQuaterSteps+1;
	for (unsigned int i=0; i<nInternalQuaterSteps; i++)
	{
		float xPos = -(1.0f + nInternalQuaterSteps-i)/(nInternalQuaterSteps+2);
		float yPos = -equation(xPos);
			
		pData[startIndex+i].pos = glm::vec3(xPos/2, (yPos+1)/2,0);
		pData[startIndex+i].nor = glm::vec3(0,0,-1);
		pData[startIndex+i].tex = glm::vec2((xPos+1)/2, (yPos+1)/2);		
	}
	startIndex += nInternalQuaterSteps;

	//center up
	for (unsigned int i=0; i<nInternalQuaterSteps+1; i++)
	{
		float xPos = (1.0f + i)/(nInternalQuaterSteps+2);
		float yPos = equation(xPos);
			
		pData[startIndex+i].pos = glm::vec3(0, (yPos+1)/2,0);
		pData[startIndex+i].nor = glm::vec3(0,0,-1);
		pData[startIndex+i].tex = glm::vec2(0.5f,0.5f);		
	}
	//center down
	startIndex += nInternalQuaterSteps+1;
	for (unsigned int i=0; i<nInternalQuaterSteps; i++)
	{
		float xPos = (1.0f + nInternalQuaterSteps-i)/(nInternalQuaterSteps+2);
		float yPos = -equation(xPos);
			
		pData[startIndex+i].pos = glm::vec3(0, (yPos+1)/2,0);
		pData[startIndex+i].nor = glm::vec3(0,0,-1);
		pData[startIndex+i].tex = glm::vec2(0.5f,0.5f);		
	}
	startIndex += nInternalQuaterSteps;


	//generate north pole
	pData[startIndex].pos = glm::vec3(0,1,0);
	pData[startIndex].nor = glm::vec3(0,0,-1);
	pData[startIndex].tex = glm::vec2(0.5f, 1.0f);		
	
	//generate south pole
	pData[startIndex+1].pos = glm::vec3(0,0,0);
	pData[startIndex+1].nor = glm::vec3(0,0,-1);
	pData[startIndex+1].tex = glm::vec2(0.5f, 0.0f);		
	
	//fill in pIndices array

	//fill in side triangles (first 6*radialStep*(heightStep-1))
	
	//fill in pData array
	startIndex = 0;
	//center and right
	unsigned int startIndex1 = 0;//right
	unsigned int startIndex2 = 2*(2*nInternalQuaterSteps+1);//center
	for (unsigned int i=0; i<2*nInternalQuaterSteps; i++)
	{ 
		pIndices[startIndex+6*i+0] = startIndex2+i;
		pIndices[startIndex+6*i+1] = startIndex1+i;
		pIndices[startIndex+6*i+2] = startIndex1+i+1;

		pIndices[startIndex+6*i+3] = startIndex2+i;
		pIndices[startIndex+6*i+4] = startIndex1+i+1;
		pIndices[startIndex+6*i+5] = startIndex2+i+1;
	}
	startIndex += 3*4*nInternalQuaterSteps;
	//left and center
	startIndex1 = 2*(2*nInternalQuaterSteps+1);//center
	startIndex2 = 1*(2*nInternalQuaterSteps+1);//left
	for (unsigned int i=0; i<2*nInternalQuaterSteps; i++)
	{ 
		pIndices[startIndex+6*i+0] = startIndex2+i;
		pIndices[startIndex+6*i+1] = startIndex1+i;
		pIndices[startIndex+6*i+2] = startIndex1+i+1;

		pIndices[startIndex+6*i+3] = startIndex2+i;
		pIndices[startIndex+6*i+4] = startIndex1+i+1;
		pIndices[startIndex+6*i+5] = startIndex2+i+1;
	}
	startIndex += 3*4*nInternalQuaterSteps;

	//connect north pole
	unsigned int northPoleIndex = 3*(2*nInternalQuaterSteps+1);
	pIndices[startIndex+0] = 2*nInternalQuaterSteps+1;//left top
	pIndices[startIndex+1] = 2*(2*nInternalQuaterSteps+1);//center top
	pIndices[startIndex+2] = northPoleIndex;

	pIndices[startIndex+3] = 2*(2*nInternalQuaterSteps+1);//center top
	pIndices[startIndex+4] = 0;//right top
	pIndices[startIndex+5] = northPoleIndex;
	startIndex += 3*2;

	//connect south pole
	unsigned int southPoleIndex = 3*(2*nInternalQuaterSteps+1)+1;
	pIndices[startIndex+0] = 2*nInternalQuaterSteps+1+2*nInternalQuaterSteps;//left bottom
	pIndices[startIndex+1] = southPoleIndex;
	pIndices[startIndex+2] = 2*(2*nInternalQuaterSteps+1)+2*nInternalQuaterSteps;//center bottom

	pIndices[startIndex+3] = 2*(2*nInternalQuaterSteps+1)+2*nInternalQuaterSteps;//center bottom
	pIndices[startIndex+4] = southPoleIndex;
	pIndices[startIndex+5] = 2*nInternalQuaterSteps;//right bottom
	startIndex += 3*2;
}
Ejemplo n.º 25
0
bool simulator_loop_detectiont::check_prefix(
    const predicatest &predicates,
    const abstract_modelt &abstract_model,
    abstract_counterexamplet &abstract_counterexample,
    concrete_counterexamplet &concrete_counterexample,
    fail_infot &fail_info)
{
  assert(abstract_counterexample.steps.size()!=0);

  // no loop? Do normal stuff.
  if(!abstract_counterexample.has_loops())
    return simulator_symext::check_prefix(
        predicates,
        abstract_model,
        abstract_counterexample,
        concrete_counterexample,
        fail_info);

  // clean up
  concrete_counterexample.clear();
  loop_infos.clear();

  // phase 1: build parameterized equation

  status("Loop Simulation Phase I");

  concrete_counterexamplet phase_I_counterexample;

  {  
    // build equation
    symex_target_equationt equation(concrete_model.ns);
    goto_symex_statet state;
    prefixt::step_mapt step_map;

    build_parameterized_equation(abstract_counterexample, 
        equation, state,
        fail_info, step_map);

    // now we have all recurrence predicates in refinement_infos
#ifdef DEBUG
    std::list<fail_infot::induction_infot>::const_iterator it;
    for (it = fail_info.induction_infos.begin(); 
        it != fail_info.induction_infos.end(); it++)
    {
      const std::list<exprt> &predicates = it->predicates;

      std::cout << "Induction predicate(s): ";
      for (std::list<exprt>::const_iterator p_it = predicates.begin ();
          p_it != predicates.end (); p_it++)
      {
        const exprt &pred = *p_it;
        std::cout << from_expr(ns, "", pred);

        std::list<exprt>::const_iterator next = p_it; next++;
        if (next != predicates.end ())
          std::cout << ", ";
      }
      std::cout << std::endl;
    }

    std::cout << "LOOP_INFO size: " << loop_infos.size () << std::endl;
#endif

    // run decision procedure
    if(check_phase_I_equation(equation, state, 
          abstract_counterexample, 
          phase_I_counterexample, 
          step_map, fail_info))
    {
      fail_info.use_invariants = true;
      return true; // spurious
    }

    // it could still be spurious
    fail_info.use_invariants = false;
  }

  // phase 2: unwind counterexample

  status("Loop Simulation Phase II");

#ifdef DEBUG
  std::cout << abstract_counterexample;
#endif

  {  
    abstract_counterexamplet unwound_counterexample;

    unwind_counterexample(
        abstract_counterexample,
        phase_I_counterexample,
        unwound_counterexample);

    unwound_counterexample.swap(abstract_counterexample);

    return simulator_symext::check_prefix(
        predicates,
        abstract_model,
        abstract_counterexample,
        concrete_counterexample,
        fail_info);
  }
}
Ejemplo n.º 26
0
int main(int argc, char const *argv[])
{
	printf("Golden-Section Search v1\n");
	printf("Created by Ozan Yildiz\n\n");

	// Variables
	double xLow = 0, xHigh = 2;				// Max and min variables

	double x1 = 0, x2 = 0, xOpt = 0, d = 0, epsilon = 0;	// Function unknows variables

	double fLow, f1, f2, fHigh;				// Function

	// Golden Ratio
	double R = (sqrt(5) - 1) / 2;
	printf("Golden ratio is %.4lf\n\n", R);

	printf("x(low)\tf(flow)\tx2\tf2\tx1\tf1\tx(high)\tf(high)\td\tepsil\n");

	{ // Variables calculation
		d = dCalc(R, xHigh, xLow);
		x1 = x1Calc(xLow, d);
		x2 = x2Calc(xHigh, d);
	}

	{ // Functions calculation
		fLow = equation(xLow);
		f2 = equation(x2);
		f1 = equation(x1);
		fHigh = equation(xHigh);
	}

	for (int i = 0; i < ITERATION; i++)
	{
		printf("%.4lf\t%.4lf\t%.4lf\t%.4lf\t%.4lf\t%.4lf\t%.4lf\t%.4lf\t%.4lf\t%.2lf\n", xLow,fLow, x2, f2, x1, f1, xHigh, fHigh, d, epsilon);

		if (f2 > f1)
		{
			// New x High value and calculate
			xHigh = x1;
			fHigh = f1;

			// New x1 value and f1 function
			x1 = x2;
			f1 = f2;

			// Optimum x and Epsilon calc
			xOpt = x2;
			epsilon = epsilonCalc(R, xHigh, xLow, xOpt);

			// New d, x2, and f2 calculate
			{
				d = dCalc(R, xHigh, xLow);
				x2 = x2Calc(xHigh, d);
				f2 = equation(x2);
			}
		}
		else if(f2 < f1)
		{
			// New x Low value and calculate
			xLow = x2;
			fLow = f2;

			// x2 value and f2 function
			x2 = x1;
			f2 = f1;

			// Optimum x and Epsilon calc
			xOpt = x1;
			epsilon = epsilonCalc(R, xHigh, xLow, xOpt);

			// New d, x1 and f1 calculate
			{
				d = dCalc(R, xHigh, xLow);
				x1 = x1Calc(xLow, d);
				f1 = equation(x1);
			}
		}

	}

	system("PAUSE");
	return 0;
}
Ejemplo n.º 27
0
void MorrisLecar::euler_s(){
	equation();
	V+=DV*single_dt;
	n+=Dn*single_dt;
}
Ejemplo n.º 28
0
void MorrisLecar::euler(){
	equation();
	V+=(DV+Network::sm_gCouple[No]/C+Network::sm_gNoise[No])*Network::sm_dt;
	n+=Dn*Network::sm_dt;
}
Ejemplo n.º 29
0
bool termination_baset::bmc(
  concrete_modelt &model,
  fine_timet &modelchecker_time,
  fine_timet &unsafe_time,
  fine_timet &safe_time)
{
  bool res=false;

  #if 0
  std::ofstream out("model");
  model.goto_functions.output(ns, out);
  out.close();
  #endif

  fine_timet before=current_time();

  symex_target_equationt equation(ns);
  custom_symext symex(ns, shadow_context, equation);
  satcheckt satcheck;
  bv_pointerst bv_pointers(ns, satcheck);

  symex.options.set_option("assertions", true);
  satcheck.set_verbosity(2);
  satcheck.set_message_handler(get_message_handler());
  bv_pointers.set_verbosity(2);
  bv_pointers.set_message_handler(get_message_handler());

  try
  {
    symex(model.goto_functions);

    bv_pointerst::resultt satres;

    if(!symex.has_remaining_claims())
      satres=bv_pointerst::D_UNSATISFIABLE;
    else
    {
      equation.convert(bv_pointers);
      satres=bv_pointers.dec_solve();
    }
    modelchecker_time+=current_time()-before;

    switch(satres)
    {
      case bv_pointerst::D_TAUTOLOGY:
      case bv_pointerst::D_SATISFIABLE:
        unsafe_time+=current_time()-before;
        res=false;
        break;
      case bv_pointerst::D_UNSATISFIABLE:
        safe_time+=current_time()-before;
        res=true;
        break;
      default:
        throw("SAT Solver error.");
        break;
    }
  }
  catch (const std::bad_alloc &s)
  {
    status(std::string("BMC Exception: Memory exhausted"));
  }
  catch (const std::string &s)
  {
    status(std::string("BMC Exception: ") + s);
  }
  catch (const char *s)
  {
    status(std::string("BMC Exception: ") + s);
  }
  catch (unsigned u)
  {
    status(std::string("BMC Exception: ") + i2string(u));
  }

  return res;
}
Ejemplo n.º 30
0
void abstractor_satqet::pred_abstract_block(
    goto_programt::const_targett target,
    const predicatest &predicates,
    abstract_transition_relationt &
    abstract_transition_relation)
{
#if 0
  symex_target_equationt equation(ns);

  std::vector<exprt> curr_predicates,
    next_predicates,
    predicates_wp;
  std::list<exprt> constraints;

  build_equation(
      ns,
      predicates,
      concrete_model,
      target,
      constraints,
      equation,
      curr_predicates,
      next_predicates,
      predicates_wp);

#if 0
  std::cout << equation;
#endif

  // find unchanged predicates
  // find predicates with value

  for(unsigned i=0; i<predicates.size(); i++)
  {
    if(predicates_wp[i]==predicates[i])
    {
      abstract_transition_relation.values.erase(i);
      //std::cout << "UNCHANGED: P" << i << std::endl;
    }
    else
    {
#if 0
      std::cout << "DIFFERENT: P" << i << std::endl;
      std::cout << "WP: " << from_expr(ns, "", predicates_wp[i]) << std::endl;
      std::cout << "P:  " << from_expr(ns, "", predicates[i]) << std::endl;
#endif

      abstract_transition_relation.values[i]=
        get_value(i, predicates, predicates_wp[i]);

      // if it changes, it's output
      abstract_transition_relation.to_predicates.insert(i);
    }
  }

  // if there is no nondeterministic predicate, we are done
  if(!abstract_transition_relation.has_nondeterminism())
    return;

#ifdef HAVE_SATQE
  predicate_image_satqe(
      get_message_handler(),
      curr_predicates,
      next_predicates,
      constraints,
      equation,
      ns,
      abstract_transition_relation);
#else
  throw "no support for satqe linked in";
#endif
#endif
}