Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
11
Добавлен:
03.06.2014
Размер:
2.52 Кб
Скачать
class Function
{
	public:

	Matrix GetHk(Matrix X, Function F);
	Matrix Newton(Matrix x1, Function F);
	Matrix Zautendijk( Matrix x0, Function F, double eps, int* _k );
	Matrix Powell( Matrix x0, Function F, double eps, int* _k );

	inline double Val( Matrix x ) {
		return f( x[0][0], x[1][0] );
	}

	/*
	junk:

	inline double df_dx1 (Matrix X) { double x1 = X.element[0][0], x2 = X.element[1][0]; return 2*x1+2; }
	inline double df_dx2 (Matrix X) { double x1 = X.element[0][0], x2 = X.element[1][0]; return 2*x2-2; }
	inline double df_dx1dx1 (Matrix X) { return 2; }
	inline double df_dx1dx2 (Matrix X) { return 0; }
	inline double df_dx2dx1 (Matrix X) { return 0; }
	inline double df_dx2dx2 (Matrix X) { return 2; }
	*/

	// a new differential listed below:
	double df( const Matrix idx, const Matrix& x0, const double eps = 0.0001, int _ord = 0 ) {
		Matrix x1 = x0, x2 = x0;
		x1[ (int)idx[_ord][0] ][0] += eps;
		x2[ (int)idx[_ord][0] ][0] -= eps;
		if( idx.h == _ord+1 )
			return ( this->Val( x1 ) - this->Val( x2 ) ) / (2 * eps);
		else
			return ( this->df( idx, x1, eps, _ord+1 ) - this->df( idx, x2, eps, _ord+1 ) ) / (2 * eps);
	}

	// shorthand form
	double df( const int idx, const Matrix& x0, const double eps = 0.0001 ) {
		Matrix x1 = x0, x2 = x0;
		x1[idx][0] += eps;
		x2[idx][0] -= eps;
		return ( this->Val( x1 ) - this->Val( x2 ) ) / (2 * eps);
	}

	inline double df_dp (Matrix X, Matrix P)
	{
		Matrix g = this->AntiGrad(X);
		return g.element[0][0]*P.element[0][0] + g.element[1][0]*P.element[1][0];
	}

	inline Matrix AntiGrad (Matrix X)
	{
		Matrix g (2, 1);
		Matrix _idx( 1, 1 );
		_idx[0][0] = 0;
		g[0][0] = -this->df( _idx, X ); // dx1
		_idx[0][0] = 1;
		g[1][0] = -this->df( _idx, X ); // dx2
		//printf( "g( %f, %f ) = { %f ; %f }\n", X[0][0], X[1][0], g[0][0], g[1][0] );
		return g;
	}

	double GetAlpha (Matrix X, Matrix P)
	{
		double Alpha=0, h=0.00001, a, b, c, d, dc, dd;
		Matrix X1 = X;
		if (this->Val(X) < this->Val(X+P*h))
    	h *= -1;

		while(this->Val(X + P*h) > this->Val(X + P*(2*h)))
			{
			h *= 2;
    	}
		if (h/2 > h*2)
    	{
    	a = h*2;
    	b = h/2;
    	}
		else
    	{
    	a = h/2;
    	b = h*2;
    	}
		while ((b-a) > 0.0001)
			{
			dc = this->Val(X + P*(a+(b-a)/4));
			dd = this->Val(X + P*(b-(b-a)/4));
    	if (dc > dd)
				{
					a = a + (b-a)/4;
				}
				else
				{
					b = b - (b-a)/4;
				}
			}

			Alpha = (b+a) / 2;
			return Alpha;
  }
};
Соседние файлы в папке Пакет прироста генетичек 2