gmath.h

来自「一个由Mike Gashler完成的机器学习方面的includes neural」· C头文件 代码 · 共 97 行

H
97
字号
/*	Copyright (C) 2006, Mike Gashler	This library is free software; you can redistribute it and/or	modify it under the terms of the GNU Lesser General Public	License as published by the Free Software Foundation; either	version 2.1 of the License, or (at your option) any later version.	see http://www.gnu.org/copyleft/lesser.html*/#ifndef __GMATH_H__#define __GMATH_H__#include <math.h>typedef double (*MathFunc)(double x);class GMath{public:	// Converts an analog value in the range 0-1 to a digital value	inline static int analogToDigital(double dVal, int nValues)	{		return (int)(dVal * nValues);	}	// Converts a digital value to analog.  Typically the digital	// value will be discreet, but for applications of interpolation	// it may be non-discreet, so it's a double instead of an int.	inline static double digitalToAnalog(double nVal, int nValues)	{		return (.5 + nVal) / nValues;	}	// The sigmoid function.  It goes through the points	// (-inf, 0) and (inf, 1) with a slope of 0 and through	// (0, .5) with a slope related to steepness	inline static double sigmoid(double x, double steepness)	{		return (double)1 / (exp(-steepness * x) + 1);	}	// This evaluates the derivative of the sigmoid function	inline static double sigmoidDerivative(double x, double steepness)	{		double d = sigmoid(x, steepness);		return steepness * d * ((double)1 - d);	}	// Calculates a function that always goes through (0, 0)	// and (1, 1), and goes through (0.5, 0.5) with a slope	// of "steepness". If steepness is > 1, then it	// will have a slope of 0 at (0, 0) and (1, 1). If steepness	// is < 1, it will have a slope of infinity at those points.	// If steepness is exactly 1, it will have a slope of 1 at	// those points.	inline static double smoothedIdentity(double x, double steepness)	{		x *= 2.0;		if(x >= 1)		{			x = 2.0 - x;			return 1.0 - pow(x, steepness) / 2.0;		}		else			return pow(x, steepness) / 2.0;	}	// The gamma function	static double gamma(double x);	// The gaussian function	inline static double gaussian(double x)	{		return exp((x * x) / (-2.0));	}	// This implements Newton's method for determining a	// polynomial f(t) that goes through all the control points	// pFuncValues at pTValues.  (You could then convert to a	// Bezier curve to get a Bezier curve that goes through those	// points.)  The polynomial coefficients are put in pFuncValues	// in the form c0 + c1*t + c2*t*t + c3*t*t*t + ...	static void NewtonPolynomial(const double* pTValues, double* pFuncValues, int nPoints);	// Integrates the specified function from dStart to dEnd	static double Integrate(MathFunc pFunc, double dStart, double dEnd, int nSteps);#ifndef NO_TEST_CODE	static void Test();#endif // !NO_TEST_CODE};#endif // __GMATH_H__

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?