Beispiel #1
0
        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
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            var x = new Complex(1, -2);
            var y = Complex.Cos(x);

            (Complex.Sh(x) + y).Show();
        }
Beispiel #3
0
        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
        }
Beispiel #4
0
        //Метод возведения в степень
        private Complex Power(string s)
        {
            Complex element;
            string  el = "";

            foreach (char ch in s)
            {
                if (ch == '^')
                {
                    break;
                }
                el += ch;
            }
            if (Char.IsLetter(el[0]))
            {
                element = arg;
            }
            else
            {
                element = Complex.ToComplex(el);
            }
            if (s.Length - el.Length > 0)
            {
                element = Complex.Pow(element, Element(s.Substring(el.Length + 1)).Re);
            }

            return(element);
        }
        public void OneRootInDegree(double rootx, double rooty, double degree)
        {
            Complex     root = new Complex(rootx, rooty);
            ComplexFunc f    = (Complex z) => Complex.Pow(z - root, degree);
            Complex     val  = Muller(f, new Complex(0, 1), new Complex(3, 3), new Complex(9, 5), 1e-16);

            Assert.IsTrue((root - val).Abs < 1e-2, message: $"Expected {root}, but was {val} (distance = {(root - val).Abs})");
        }
Beispiel #6
0
 /// <summary>
 /// Конструктор для выражений с переменными (переменная x)
 /// </summary>
 /// <param name="z">Значение переменной</param>
 /// <param name="str">Строка формулы</param>
 public ParserComplex(Complex x, string str)
 {
     Clean(str);
     arg = x;
     ShowT();
     Nam = Product(term);
     ShowT();
 }
Beispiel #7
0
        public static void IntegExamples()
        {
            Func <double, double> freal = x => (x - 4 + Math.Sin(x * 10)) / (1 + x * x);

            double integral = FuncMethods.DefInteg.GaussKronrod.GaussKronrodSum(freal, a: -3, b: 3, n: 61, count: 5);

            integral.Show(); // -9,992366179186035

            Complex integ = FuncMethods.DefInteg.GaussKronrod.GaussKronrodSum(
                z => (Math.Exp(-z.Abs) + Complex.Sin(z + Complex.I)) / (1 + z * z / 5),
                a: new Complex(-1, -4.3), b: 3 + Complex.I * 2, n: 61, count: 10);

            integ.Show(); // -3,325142834912312 + 10,22008333462534i
        }
Beispiel #8
0
        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}");
            }
        }
Beispiel #9
0
        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}");
            }
        }
Beispiel #10
0
        //Расчет умножения/деления
        private Complex Element(string s)
        {
            Complex element;
            string  el = "";

            foreach (char ch in s)
            {
                if (ch == '*' || ch == '/')
                {
                    break;
                }
                el += ch;
            }
            if (Char.IsLetter(el[0]) && el.IndexOf('^') == -1)
            {
                element = Func(el);
            }
            else
            {
                if (el.IndexOf('^') == -1)
                {
                    element = Complex.ToComplex(el);
                }
                else
                {
                    element = Power(el);
                }
            }
            if (el.Length < s.Length - 1)
            {
                if (s[el.Length] == '*')
                {
                    element *= Element(s.Substring(el.Length + 1));
                }
                else if (s[el.Length] == '/')
                {
                    element /= Element(s.Substring(el.Length + 1));
                }
            }
            return(element);
        }
        public void RootofShTryMany(double coef, double xmin, double xmax, double ymin, double ymax, int count)
        {
            bool val = MullerTryMany((Complex z) => Complex.Sh(coef * z), xmin, xmax, ymin, ymax, out List <Complex> res, maxcount: count);

            Assert.IsTrue(val, message: $"|Sh(root)| was {res.ToArray().Select(s=>Complex.Sh(coef*s).Abs).ToArray().ToStringMas().Aggregate((s,r)=>$"{s} {r}")}");
        }
        public void RootofSinTrying(double coef, double xmin, double xmax, double ymin, double ymax, int count)
        {
            bool val = MullerTrying((Complex z) => Complex.Sin(coef * z), xmin, xmax, ymin, ymax, out Complex res, maxcount: count);

            Assert.IsTrue(val, message: $"|Sin(root)| was {Complex.Sin(coef * res).Abs}");
        }
        public void RootofSin(double coef, double x1, double y1, double x2, double y2, double x3, double y3)
        {
            Complex val = Muller((Complex z) => Complex.Sin(coef * z), new Complex(x1, y1), new Complex(x2, y2), new Complex(x3, y3));

            Assert.IsTrue(Complex.Sin(coef * val).Abs < 1e-12, message: $"|Sin| was {Complex.Sin(coef * val).Abs}");
        }
Beispiel #14
0
        public void ConvertTest(double x, double y)
        {
            var c = new Complex(x, y);

            Assert.IsTrue(c == Complex.ToComplex(c.ToString()), $"Was {Complex.ToComplex(c.ToString())} from {c.ToString()}");
        }
Beispiel #15
0
        public void ToComplexTest(string s, double x, double y)
        {
            var v = Complex.ToComplex(s);

            Assert.IsTrue(v.Re == x && v.Im == y, $"Expected {new Complex(x, y)}, but was {v}");
        }
Beispiel #16
0
        //Метод обработки функций и присваивания значения переменной
        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);
        }
Beispiel #17
0
 /// <summary>
 /// Конструктор для выражений с переменными (переменная x)
 /// </summary>
 /// <param name="z">Значение переменной</param>
 /// <param name="str">Строка формулы</param>
 public ParserComplex(string s, Complex x) : this(x, s)
 {
 }