public void GammaBorderTest() { MultiPrecision <Pow2.N8>[] borders = new MultiPrecision <Pow2.N8>[] { 0.5, 1 }; foreach (MultiPrecision <Pow2.N8> b in borders) { List <MultiPrecision <Pow2.N8> > ys = new(); foreach (MultiPrecision <Pow2.N8> x in TestTool.EnumerateNeighbor(b)) { MultiPrecision <Pow2.N8> y = MultiPrecision <Pow2.N8> .Gamma(x); Console.WriteLine(x); Console.WriteLine(x.ToHexcode()); Console.WriteLine(y); Console.WriteLine(y.ToHexcode()); Console.Write("\n"); TestTool.Tolerance(Approx.Gamma((double)x), y, ignore_sign: true); ys.Add(y); } TestTool.NearlyNeighbors(ys, 2); TestTool.SmoothnessSatisfied(ys, 1); TestTool.MonotonicitySatisfied(ys); Console.Write("\n"); } }
public void LogGammaApproxTest() { Assert.AreEqual(Math.Log(1), Approx.LogGamma(1), 1e-10); Assert.AreEqual(Math.Log(1), Approx.LogGamma(2), 1e-10); Assert.AreEqual(Math.Log(2), Approx.LogGamma(3), 1e-10); Assert.AreEqual(Math.Log(6), Approx.LogGamma(4), 1e-10); Assert.AreEqual(Math.Log(24), Approx.LogGamma(5), 1e-10); Assert.AreEqual(Math.Log(Math.Sqrt(Math.PI)), Approx.LogGamma(0.5), 1e-10); Assert.AreEqual(Math.Log(Math.Sqrt(Math.PI) / 2), Approx.LogGamma(1.5), 1e-10); Assert.AreEqual(Math.Log(Math.Sqrt(Math.PI) * 3 / 4), Approx.LogGamma(2.5), 1e-10); Assert.AreEqual(Math.Log(Math.Sqrt(Math.PI) * 15 / 8), Approx.LogGamma(3.5), 1e-10); }
public void GammaApproxTest() { Assert.AreEqual(1, Approx.Gamma(1), 1e-10); Assert.AreEqual(1, Approx.Gamma(2), 1e-10); Assert.AreEqual(2, Approx.Gamma(3), 1e-10); Assert.AreEqual(6, Approx.Gamma(4), 1e-10); Assert.AreEqual(24, Approx.Gamma(5), 1e-10); Assert.AreEqual(Math.Sqrt(Math.PI) * 4 / 3, Approx.Gamma(-1.5), 1e-10); Assert.AreEqual(Math.Sqrt(Math.PI) * -2, Approx.Gamma(-0.5), 1e-10); Assert.AreEqual(Math.Sqrt(Math.PI), Approx.Gamma(0.5), 1e-10); Assert.AreEqual(Math.Sqrt(Math.PI) / 2, Approx.Gamma(1.5), 1e-10); Assert.AreEqual(Math.Sqrt(Math.PI) * 3 / 4, Approx.Gamma(2.5), 1e-10); Assert.AreEqual(Math.Sqrt(Math.PI) * 15 / 8, Approx.Gamma(3.5), 1e-10); }
/// <summary> /// Generuje przebiegi i wypełnia tablice wartościami MSE /// </summary> /// <param name="typeOfApprox">Typ aproksymacji</param> /// <param name="LimitOfM">Maksymalna ilość harmonicznych</param> /// <param name="amplitude">Amplituda sygnałów</param> /// <param name="frequency">Częstotliwość sygnałów</param> /// <param name="samplingFrequency">Częstotliwość próbkowania sygnałów</param> /// <param name="lengthOfSignal">Długość sygnału</param> public void GenerateValues(Approx typeOfApprox, int LimitOfM, double amplitude, double frequency, double samplingFrequency, double lengthOfSignal) { this.limitOfM = LimitOfM; this.LengthOfBuffer = limitOfM; MSEValues = new double[LimitOfM]; switch (typeOfApprox) { case Approx.Square: model = new AnalogSignal(amplitude, frequency, samplingFrequency, lengthOfSignal).GenerateModelSquare(); for (int m = 0; m < LimitOfM; m++) { signal = new AnalogSignal(amplitude, frequency, samplingFrequency, lengthOfSignal).GenerateSquareSignal(m); MSEValues[m] = CalculateMSE(); } break; case Approx.Triangle: model = new AnalogSignal(amplitude, frequency, samplingFrequency, lengthOfSignal).GenerateModelTriangle(amplitude * 3.2); for (int m = 0; m < LimitOfM; m++) { signal = new AnalogSignal(amplitude, frequency, samplingFrequency, lengthOfSignal).GenerateTriangleSignal(m); MSEValues[m] = CalculateMSE(); } break; case Approx.Sawer: model = new AnalogSignal(amplitude, frequency, samplingFrequency, lengthOfSignal).GenerateModelSawer(amplitude); for (int m = 0; m < LimitOfM; m++) { signal = new AnalogSignal(amplitude, frequency, samplingFrequency, lengthOfSignal).GenerateSawSignal(m); MSEValues[m] = CalculateMSE(); } break; default: model = new AnalogSignal(amplitude, frequency, samplingFrequency, lengthOfSignal).GenerateModelSquare(); for (int m = 0; m < LimitOfM; m++) { signal = new AnalogSignal(amplitude, frequency, samplingFrequency, lengthOfSignal).GenerateSquareSignal(m); MSEValues[m] = CalculateMSE(); } break; } MaxFromBuffor = MSEValues.Max(); }
public Node <INode> GetNode(string nodeName) { switch (nodeName) { case Absolute.NAME: INode nodeAbsolute = new Absolute() as INode; return(new Node <INode>(nodeAbsolute)); case Approx.NAME: INode nodeAprox = new Approx() as INode; return(new Node <INode>(nodeAprox)); case ArcCos.NAME: INode nodeArcCos = new ArcCos() as INode; return(new Node <INode>(nodeArcCos)); case ArcSin.NAME: INode nodeArcSin = new ArcSin() as INode; return(new Node <INode>(nodeArcSin)); case ArcTan2.NAME: INode nodeArcTan2 = new ArcTan2() as INode; return(new Node <INode>(nodeArcTan2)); case Ceil.NAME: INode nodeCeil = new Ceil() as INode; return(new Node <INode>(nodeCeil)); case CeilToInt.NAME: INode nodeCeilToInt = new CeilToInt() as INode; return(new Node <INode>(nodeCeilToInt)); case Clamp.NAME: INode nodeClamp = new Clamp() as INode; return(new Node <INode>(nodeClamp)); case Clamp01.NAME: INode nodeClamp01 = new Clamp01() as INode; return(new Node <INode>(nodeClamp01)); case ClosestPowerOf2.NAME: INode nodeClosestPowerOf2 = new ClosestPowerOf2() as INode; return(new Node <INode>(nodeClosestPowerOf2)); case Cosinus.NAME: INode nodeCosinus = new Cosinus() as INode; return(new Node <INode>(nodeCosinus)); case DeltaAngle.NAME: INode nodeDeltaAngle = new DeltaAngle() as INode; return(new Node <INode>(nodeDeltaAngle)); case Exp.NAME: INode nodeExp = new Exp() as INode; return(new Node <INode>(nodeExp)); case Floor.NAME: INode nodeFloor = new Floor() as INode; return(new Node <INode>(nodeFloor)); case FloorToInt.NAME: INode nodeFloorToInt = new FloorToInt() as INode; return(new Node <INode>(nodeFloorToInt)); case Lerp.NAME: INode nodeLerp = new Lerp() as INode; return(new Node <INode>(nodeLerp)); case LerpAngle.NAME: INode nodeLerpAngle = new LerpAngle() as INode; return(new Node <INode>(nodeLerpAngle)); case Log10.NAME: INode nodeLog10 = new Log10() as INode; return(new Node <INode>(nodeLog10)); case Logarithm.NAME: INode nodeLogarithm = new Logarithm() as INode; return(new Node <INode>(nodeLogarithm)); case Sinus.NAME: INode nodeSinus_ = new Sinus() as INode; return(new Node <INode>(nodeSinus_)); case Max.NAME: INode nodeMax = new Max() as INode; return(new Node <INode>(nodeMax)); case Min.NAME: INode nodeMin = new Min() as INode; return(new Node <INode>(nodeMin)); case MoveTowards.NAME: INode nodeMoveTowards = new MoveTowards() as INode; return(new Node <INode>(nodeMoveTowards)); case MoveTowardsAngle.NAME: INode nodeMoveTowardsAngle = new MoveTowardsAngle() as INode; return(new Node <INode>(nodeMoveTowardsAngle)); case NextPowerOfTwo.NAME: INode nodeNextPowerOfTwo = new NextPowerOfTwo() as INode; return(new Node <INode>(nodeNextPowerOfTwo)); case PerlinNoise.NAME: INode nodePerlinNoise = new PerlinNoise() as INode; return(new Node <INode>(nodePerlinNoise)); case PingPong.NAME: INode nodePingPong = new PingPong() as INode; return(new Node <INode> (nodePingPong)); case Pow.NAME: INode nodePow = new Pow() as INode; return(new Node <INode>(nodePow)); case SquareRoot.NAME: INode nodeSqrt = new SquareRoot() as INode; return(new Node <INode>(nodeSqrt)); case Tan.NAME: INode nodeTan = new Tan() as INode; return(new Node <INode>(nodeTan)); case Random.NAME: INode nodeRandom = new Random() as INode; return(new Node <INode>(nodeRandom)); default: return(null); } }
static void Main() { // part A WriteLine("Part A:"); int i = 1; while (i + 1 > 1) { i++; } Write("My max int = {0}\n", i); Write("The int.MaxValue command says = {0}\n", int.MaxValue); i = 1; while (i - 1 < 1) { i--; } Write("My min int = {0}\n", i); Write("The int.MinValue command says = {0}\n", int.MinValue); // part B WriteLine("\nPart B:"); double x = 1; while (1 + x != 1) { x /= 2; } // When the loop breaks x is so small that the computer cannot see any difference // between 1 and 1+x. The smallest interval that the computer could distinguish is // thus the previous x-value. We therefore multiply it by two before printing it. x *= 2; Write("The machine epsilon with double is = {0}\n", x); float y = 1F; while ((float)(1F + y) != 1F) { y /= 2F; } // When the loop breaks y is so small that the computer cannot see any difference // between 1 and 1+y. The smallest interval that the computer could distinguish is // thus the previous y-value. We therefore multiply it by two before printing it. y *= 2F; Write("The machine epsilon with float is = {0}\n", y); // According to Dmitri the machine epsilon for a double precision machince should // be around System.Math.Pow(2, -52). double dmachineepsilon = Pow(2, -52); Write("Dmitri says the machine epsilon for doubles should be around = {0}\n", dmachineepsilon); double fmachineepsilon = Pow(2, -23); Write("Dmitri says the machine epsilon for floats should be around = {0}\n", fmachineepsilon); // part C /* Explanations: Small numbers added together gives smaller roundoff errors compared to large * numbers added to small numbers. Summing up lowest to highest should therefore be more precise. * Doubles have better precision than floats, and the result with doubles are therefore in * general more precise, since it is less vulnerable to roundoff errors. */ WriteLine("\nPart C:"); int max = int.MaxValue / 3; // I get the same value for 2 or 3 float float_sum_up = 1F; for (int j = 2; j < max; j++) { float_sum_up += 1F / j; } Write("The sum going up using floats becomes = {0}\n", float_sum_up); float float_sum_down = 1F / max; for (int j = max - 1; j > 0; j--) { float_sum_down += 1F / j; } Write("The sum going down using floats becomes = {0}\n\n", float_sum_down); WriteLine("Small numbers added to small numbers have smaller roundoff errors than large" + " numbers added to small numbers. The sum going from smallest to largest should thus be" + " the most precise one.\n"); /* These sums will appear to converge as a function of max, since at some point the numbers * that have to be added are smaller than the machine epsilon, and thus the computer can't * handle it properly. This is in contrast to mathematical theory in which this particular sum * actually diverges. */ WriteLine("The sums will converge as a function of max, since at some point we are adding" + " numbers smaller than the machine epsilon, which will just be zeros for the computer.\n"); // Let's try the sum with doubles now. double double_sum_up = 1D; for (int j = 2; j < max; j++) { double_sum_up += 1D / j; } Write("The sum going up using doubles becomes = {0}\n", double_sum_up); double double_sum_down = 1D / max; for (int j = max - 1; j > 0; j--) { double_sum_down += 1D / j; } Write("The sum going down using doubles becomes = {0}\n", double_sum_down); // part D WriteLine("\nPart D:"); WriteLine("If two values are within 1e-9 of each other, the approx function will" + " return true."); double a = 1.01; double b = 1; bool truth = Approx.approx(a, b); Write("The truth value for {0} ~ {1} is {2}\n", a, b, truth); double c = 1.999999999; double d = 2; truth = Approx.approx(c, d); Write("The truth value for {0} ~ {1} is {2}\n", c, d, truth); }
public void TestApproxFilter() { IFilter filter = new Approx(AttributeNames.CN, "pangxiaoliang"); Assert.AreEqual("(cn~=pangxiaoliang)", filter.BuildFilter()); }
public void LogGammaApproxBorderTest() { { List <MultiPrecision <Pow2.N4> > ys = new(); foreach (MultiPrecision <Pow2.N4> x in TestTool.EnumerateNeighbor((MultiPrecision <Pow2.N4>) 16, 4)) { MultiPrecision <Pow2.N4> y = MultiPrecision <Pow2.N4> .LogGamma(x); Console.WriteLine(x); Console.WriteLine(x.ToHexcode()); Console.WriteLine(y); Console.WriteLine(y.ToHexcode()); Console.Write("\n"); TestTool.Tolerance(Approx.LogGamma((double)x), y, ignore_sign: true); ys.Add(y); } TestTool.NearlyNeighbors(ys, 4); TestTool.SmoothnessSatisfied(ys, 1); TestTool.MonotonicitySatisfied(ys); } { List <MultiPrecision <Pow2.N8> > ys = new(); foreach (MultiPrecision <Pow2.N8> x in TestTool.EnumerateNeighbor((MultiPrecision <Pow2.N8>) 32, 4)) { MultiPrecision <Pow2.N8> y = MultiPrecision <Pow2.N8> .LogGamma(x); Console.WriteLine(x); Console.WriteLine(x.ToHexcode()); Console.WriteLine(y); Console.WriteLine(y.ToHexcode()); Console.Write("\n"); TestTool.Tolerance(Approx.LogGamma((double)x), y, ignore_sign: true); ys.Add(y); } TestTool.NearlyNeighbors(ys, 4); TestTool.SmoothnessSatisfied(ys, 1); TestTool.MonotonicitySatisfied(ys); } { List <MultiPrecision <Pow2.N16> > ys = new(); foreach (MultiPrecision <Pow2.N16> x in TestTool.EnumerateNeighbor((MultiPrecision <Pow2.N16>) 60, 4)) { MultiPrecision <Pow2.N16> y = MultiPrecision <Pow2.N16> .LogGamma(x); Console.WriteLine(x); Console.WriteLine(x.ToHexcode()); Console.WriteLine(y); Console.WriteLine(y.ToHexcode()); Console.Write("\n"); TestTool.Tolerance(Approx.LogGamma((double)x), y, ignore_sign: true); ys.Add(y); } TestTool.NearlyNeighbors(ys, 4); TestTool.SmoothnessSatisfied(ys, 1); TestTool.MonotonicitySatisfied(ys); } { List <MultiPrecision <Pow2.N32> > ys = new(); foreach (MultiPrecision <Pow2.N32> x in TestTool.EnumerateNeighbor((MultiPrecision <Pow2.N32>) 116, 4)) { MultiPrecision <Pow2.N32> y = MultiPrecision <Pow2.N32> .LogGamma(x); Console.WriteLine(x); Console.WriteLine(x.ToHexcode()); Console.WriteLine(y); Console.WriteLine(y.ToHexcode()); Console.Write("\n"); TestTool.Tolerance(Approx.LogGamma((double)x), y, ignore_sign: true); ys.Add(y); } TestTool.NearlyNeighbors(ys, 4); TestTool.SmoothnessSatisfied(ys, 1); TestTool.MonotonicitySatisfied(ys); } { List <MultiPrecision <Pow2.N64> > ys = new(); foreach (MultiPrecision <Pow2.N64> x in TestTool.EnumerateNeighbor((MultiPrecision <Pow2.N64>) 228, 4)) { MultiPrecision <Pow2.N64> y = MultiPrecision <Pow2.N64> .LogGamma(x); Console.WriteLine(x); Console.WriteLine(x.ToHexcode()); Console.WriteLine(y); Console.WriteLine(y.ToHexcode()); Console.Write("\n"); TestTool.Tolerance(Approx.LogGamma((double)x), y, ignore_sign: true); ys.Add(y); } TestTool.NearlyNeighbors(ys, 4); TestTool.SmoothnessSatisfied(ys, 1); TestTool.MonotonicitySatisfied(ys); } { List <MultiPrecision <Pow2.N128> > ys = new(); foreach (MultiPrecision <Pow2.N128> x in TestTool.EnumerateNeighbor((MultiPrecision <Pow2.N128>) 456, 4)) { MultiPrecision <Pow2.N128> y = MultiPrecision <Pow2.N128> .LogGamma(x); Console.WriteLine(x); Console.WriteLine(x.ToHexcode()); Console.WriteLine(y); Console.WriteLine(y.ToHexcode()); Console.Write("\n"); TestTool.Tolerance(Approx.LogGamma((double)x), y, ignore_sign: true); ys.Add(y); } TestTool.NearlyNeighbors(ys, 4); TestTool.SmoothnessSatisfied(ys, 1); TestTool.MonotonicitySatisfied(ys); } { List <MultiPrecision <Pow2.N256> > ys = new(); foreach (MultiPrecision <Pow2.N256> x in TestTool.EnumerateNeighbor((MultiPrecision <Pow2.N256>) 908, 4)) { MultiPrecision <Pow2.N256> y = MultiPrecision <Pow2.N256> .LogGamma(x); Console.WriteLine(x); Console.WriteLine(x.ToHexcode()); Console.WriteLine(y); Console.WriteLine(y.ToHexcode()); Console.Write("\n"); TestTool.Tolerance(Approx.LogGamma((double)x), y, ignore_sign: true); ys.Add(y); } TestTool.NearlyNeighbors(ys, 4); TestTool.SmoothnessSatisfied(ys, 1); TestTool.MonotonicitySatisfied(ys); } }
public void GammaTest() { for (int i = -200; i < 200; i++) { if (i <= 0 && i % 4 == 0) { continue; } MultiPrecision <Pow2.N8> x = MultiPrecision <Pow2.N8> .Ldexp(MultiPrecision <Pow2.N8> .One, -2) * i; MultiPrecision <Pow2.N8> y = MultiPrecision <Pow2.N8> .Gamma(x); Console.WriteLine(x); Console.WriteLine(y); TestTool.Tolerance(Approx.Gamma((double)x), y, rateerr: 1e-10, ignore_sign: true); } Assert.IsTrue(1 == MultiPrecision <Pow2.N8> .Gamma(1)); Assert.IsTrue(1 == MultiPrecision <Pow2.N8> .Gamma(2)); Assert.IsTrue(2 == MultiPrecision <Pow2.N8> .Gamma(3)); Assert.IsTrue(6 == MultiPrecision <Pow2.N8> .Gamma(4)); Assert.IsTrue(24 == MultiPrecision <Pow2.N8> .Gamma(5)); Assert.IsTrue( MultiPrecision <Pow2.N8> .NearlyEqualBits( MultiPrecision <Pow2.N8> .Sqrt(MultiPrecision <Pow2.N8> .PI) * 4 / 3, MultiPrecision <Pow2.N8> .Gamma(-1.5), 2 )); Assert.IsTrue( MultiPrecision <Pow2.N8> .NearlyEqualBits( MultiPrecision <Pow2.N8> .Sqrt(MultiPrecision <Pow2.N8> .PI) * -2, MultiPrecision <Pow2.N8> .Gamma(-0.5), 2 )); Assert.IsTrue( MultiPrecision <Pow2.N8> .NearlyEqualBits( MultiPrecision <Pow2.N8> .Sqrt(MultiPrecision <Pow2.N8> .PI), MultiPrecision <Pow2.N8> .Gamma(0.5), 2 )); Assert.IsTrue( MultiPrecision <Pow2.N8> .NearlyEqualBits( MultiPrecision <Pow2.N8> .Sqrt(MultiPrecision <Pow2.N8> .PI) / 2, MultiPrecision <Pow2.N8> .Gamma(1.5), 2 )); Assert.IsTrue( MultiPrecision <Pow2.N8> .NearlyEqualBits( MultiPrecision <Pow2.N8> .Sqrt(MultiPrecision <Pow2.N8> .PI) * 3 / 4, MultiPrecision <Pow2.N8> .Gamma(2.5), 2 )); Assert.IsTrue( MultiPrecision <Pow2.N8> .NearlyEqualBits( MultiPrecision <Pow2.N8> .Sqrt(MultiPrecision <Pow2.N8> .PI) * 15 / 8, MultiPrecision <Pow2.N8> .Gamma(3.5), 2 )); }
public void LogGammaTest() { for (int i = 1; i < 200; i++) { MultiPrecision <Pow2.N8> x = MultiPrecision <Pow2.N8> .Ldexp(MultiPrecision <Pow2.N8> .One, -2) * i; MultiPrecision <Pow2.N8> y = MultiPrecision <Pow2.N8> .LogGamma(x); Console.WriteLine(x); Console.WriteLine(y); TestTool.Tolerance(Approx.LogGamma((double)x), y, rateerr: 1e-10, ignore_sign: true); } Assert.IsTrue(0 == MultiPrecision <Pow2.N8> .LogGamma(1)); Assert.IsTrue(0 == MultiPrecision <Pow2.N8> .LogGamma(2)); Assert.IsTrue( MultiPrecision <Pow2.N8> .NearlyEqualBits( MultiPrecision <Pow2.N8> .Log(2), MultiPrecision <Pow2.N8> .LogGamma(3), 1 )); Assert.IsTrue( MultiPrecision <Pow2.N8> .NearlyEqualBits( MultiPrecision <Pow2.N8> .Log(6), MultiPrecision <Pow2.N8> .LogGamma(4), 1 )); Assert.IsTrue( MultiPrecision <Pow2.N8> .NearlyEqualBits( MultiPrecision <Pow2.N8> .Log(24), MultiPrecision <Pow2.N8> .LogGamma(5), 1 )); Assert.IsTrue( MultiPrecision <Pow2.N8> .NearlyEqualBits( MultiPrecision <Plus1 <Pow2.N8> > .Log(MultiPrecision <Plus1 <Pow2.N8> > .Sqrt(MultiPrecision <Plus1 <Pow2.N8> > .PI)).Convert <Pow2.N8>(), MultiPrecision <Pow2.N8> .LogGamma(0.5), 1 )); Assert.IsTrue( MultiPrecision <Pow2.N8> .NearlyEqualBits( MultiPrecision <Plus1 <Pow2.N8> > .Log(MultiPrecision <Plus1 <Pow2.N8> > .Sqrt(MultiPrecision <Plus1 <Pow2.N8> > .PI) / 2).Convert <Pow2.N8>(), MultiPrecision <Pow2.N8> .LogGamma(1.5), 1 )); Assert.IsTrue( MultiPrecision <Pow2.N8> .NearlyEqualBits( MultiPrecision <Plus1 <Pow2.N8> > .Log(MultiPrecision <Plus1 <Pow2.N8> > .Sqrt(MultiPrecision <Plus1 <Pow2.N8> > .PI) * 3 / 4).Convert <Pow2.N8>(), MultiPrecision <Pow2.N8> .LogGamma(2.5), 1 )); Assert.IsTrue( MultiPrecision <Pow2.N8> .NearlyEqualBits( MultiPrecision <Plus1 <Pow2.N8> > .Log(MultiPrecision <Plus1 <Pow2.N8> > .Sqrt(MultiPrecision <Plus1 <Pow2.N8> > .PI) * 15 / 8).Convert <Pow2.N8>(), MultiPrecision <Pow2.N8> .LogGamma(3.5), 1 )); }