1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
5 // Simultaneous equations by Newton's method adapted from Conte and De Boor
6 // to solve F(X,Y)=0 and G(X,Y)=0
8 using Microsoft.Xunit.Performance;
10 using System.Runtime.CompilerServices;
13 [assembly: OptimizeForBenchmarks]
15 namespace Benchstone.BenchF
17 public static class NewtE
20 public const int Iterations = 1;
22 public const int Iterations = 1000000;
25 [MethodImpl(MethodImplOptions.NoInlining)]
26 private static bool Bench()
28 double idgb, a, b, x, y, deltaX, deltaY;
37 System.Console.WriteLine("{0}, {1}, F(x,y) , G(x,y) \n", x, y);
40 for (int j = 1; j <= Iterations; j++)
48 System.Console.WriteLine(" {0}, {1}, {2}, {3}\n", x, y, a, b);
51 for (int i = 1; i <= 20; i++)
53 deltaX = (-F(x, y) * GY(x, y) + G(x, y) * FY(x, y)) / (FX(x, y) * GY(x, y) - FY(x, y) * GX(x, y));
54 deltaY = (-G(x, y) * FX(x, y) + F(x, y) * GX(x, y)) / (FX(x, y) * GY(x, y) - FY(x, y) * GX(x, y));
61 System.Console.WriteLine("{0}, {1}, {2}, {3}, {4}\n", i, x, y, a, b);
64 if ((System.Math.Abs(deltaX) < 0.000001) && (System.Math.Abs(deltaY) < 0.000001) &&
65 (System.Math.Abs(a) < 0.000001) && (System.Math.Abs(b) < 0.000001))
72 System.Console.WriteLine("FAILED TO CONVERGE IN 20 ITERATIONS\n");
83 private static double F(double x, double y)
85 return ((x) + 3 * System.Math.Log(x) / System.Math.Log(10.0) - (y) * (y));
88 private static double G(double x, double y)
90 return (2 * (x) * (x) - (x) * (y) - 5 * (x) + 1);
93 private static double FX(double x, double y)
95 return (1 + 3 / (System.Math.Log(10.0) * (x)));
98 private static double FY(double x, double y)
103 private static double GX(double x, double y)
105 return (4 * (x) - (y) - 5);
108 private static double GY(double x, double y)
114 public static void Test()
116 foreach (var iteration in Benchmark.Iterations)
118 using (iteration.StartMeasurement())
125 private static bool TestBase()
127 bool result = Bench();
131 public static int Main()
133 bool result = TestBase();
134 return (result ? 100 : -1);