public static void ParserExamples() { var gen = Expendator.ThreadSafeRandomGen(1); Func <double, double> f1 = Parser.GetDelegate("cos(x)+sin(x/2+4,6)*exp(-sqr(x))+abs(x)^0,05"); Func <double, double> f2 = x => Math.Cos(x) + Math.Sin(x / 2 + 4.6) * Math.Exp(-x * x) + Math.Pow(Math.Abs(x), 0.05); for (int i = 0; i < 5; i++) { var d = gen.NextDouble() * 50; $"{f1(d)} == {f2(d)}".Show(); } //2,1254805141764708 == 2,1254805141764708 //1,8237614071831993 == 1,8237614071831991 //0,9607774340933849 == 0,9607774340933849 //1,8366859282256982 == 1,8366859282256982 //1,3013656833866554 == 1,3013656833866554 Func <Complex, Complex> c1 = ParserComplex.GetDelegate("Re(z)*Im(z) + sh(z)*I + sin(z)/100"); Func <Complex, Complex> c2 = z => z.Re * z.Im + Complex.Sh(z) * Complex.I + Complex.Sin(z) / 100; for (int i = 0; i < 5; i++) { var d = new Complex(gen.NextDouble() * 50, gen.NextDouble() * 10); $"{c1(d)} == {c2(d)}".Show(); } // 485696399,00749403 - 1151202339,537752i == 485696398,8506223 - 1151202339,7349265i //- 1,328008130324937E+20 + 8,291419281824573E+19i == -1,328008130324937E+20 + 8,291419281824573E+19i // - 12609203585138,465 + 42821192159972,99i == -12609203585138,78 + 42821192159972,99i //7270,488386388151 - 121362,61308893963i == 7270,344179063162 - 121362,52416901031i //- 7,964336745137357E+20 + 1,3345594600169975E+21i == -7,964336745137357E+20 + 1,3345594600169975E+21i }
public void ParsingTest2() { const string formula = "2+3*I+Re(z+Im(z))"; Func <Complex, Complex> expected = (Complex z) => new Complex(2, 3) + (z + z.Im).Re; Func <Complex, Complex> result = ParserComplex.GetDelegate(formula); Random rnd = new Random(22); Complex tmp, v1, v2; for (int i = 0; i < 50; i++) { tmp = new Complex(rnd.NextDouble() * 10 - 5, rnd.NextDouble() * 10 - 5); v1 = expected(tmp); v2 = result(tmp); Assert.IsTrue((v1 - v2).Abs < 1e-10, $"Expected {v1} but was {v2} on arg = {tmp}; iter = {i + 1}"); } }
public void ParsingTest1() { const string formula = "sh(z)+cos(z)*7,6+z*z/3+exp(z+sqrt(z))"; Func <Complex, Complex> expected = (Complex z) => Complex.Sh(z) + Complex.Cos(z) * 7.6 + z * z / 3 + Complex.Exp(z + Complex.Sqrt(z)); Func <Complex, Complex> result = ParserComplex.GetDelegate(formula); Random rnd = new Random(22); Complex tmp, v1, v2; for (int i = 0; i < 50; i++) { tmp = new Complex(rnd.NextDouble() * 10 - 5, rnd.NextDouble() * 10 - 5); v1 = expected(tmp); v2 = result(tmp); Assert.IsTrue((v1 - v2).Abs < 1e-10, $"Expected {v1} but was {v2} on arg = {tmp}; iter = {i+1}"); } }