public static void ComplexNumberExamples() { var a = new Complex(1); a.Show(); // 1 var b = new Complex(2.5, 1.5); b.Show(); // 2,5 + 1,5i b.Conjugate.Show(); // 2,5 - 1,5i b.Re.Show(); // 2,5 b.Im.Show(); // 1,5 b.Abs.Show(); // 2,9154759474226504 b.Arg.Show(); // 0,5404195002705842 var c = a / b + 3 + 5.6 + a * b + Complex.Cos(b) + Complex.Sin(a + b) + Complex.Ch(a * b * b) + Complex.Ln(b) + Complex.Exp(a) + Complex.Expi(10); c.Show(); // 21,099555214728547 + 23,649577072514752i c.Round(4).Show(); // 21,0996 + 23,6496i c.Swap.Show(); // 23,649577072514752 + 21,099555214728547i c.Pow(3.5).Show(); // -175884,94745749474 + 34459,051603806576i new CVectors(Complex.Radical(a + b + b * b, 7)).Show(); // (1,4101632808375018 + 0,1774107754735756i 0,7405170949635271 + 1,2131238576267886i -0,4867535672138724 + 1,3353299317700824i -1,3474888653159458 + 0,4520053315239409i -1,1935375640715047 - 0,771688502588176i -0,14082813335184957 - 1,4142851546746704i 1,0179277541521443 - 0,9918962391315406i) Complex.ToComplex("1+2i").Show(); // 1 + 2i Complex.ToComplex("-1 + 2,346e-5i").Show(); // -1 + 2,346e-5i }
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}"); } }
//Метод обработки функций и присваивания значения переменной private Complex Func(string s) { Complex element = 0.0; string el = ""; foreach (char ch in s) { if (!Char.IsLetter(ch) || ch == 'i' /*|| ch!='x'*/) { break; } el += ch; } if (!IsFunc(el)) { element = arg; } else { if (el == "I") { element = Complex.I; } else if (el == "pi") { element = Math.PI; } else { var val = Complex.ToComplex(s.Substring(el.Length)); if (el == "sin") { element = Complex.Sin(val); } if (el == "cos") { element = Complex.Cos(val); } if (el == "exp") { element = Complex.Exp(val); } if (el == "ln") { element = Complex.Ln(val); } if (el == "abs") { element = val.Abs; } if (el == "sqrt") { element = Complex.Sqrt(val); } if (el == "sqr") { element = Complex.Pow(val, 2); } if (el == "cube") { element = Complex.Pow(val, 3); } if (el == "sh") { element = Complex.Sh(val); } if (el == "ch") { element = Complex.Ch(val); } if (el == "Re") { element = val.Re; } if (el == "Im") { element = val.Im; } } } return(element); }