コード例 #1
0
        public static double CalculateExactRemainder(Expr func, FloatingPoint point, int order)
        {
            var pointVar = new Dictionary <string, FloatingPoint>
            {
                { "x", point }
            };

            var taylorExpr1 = GetTaylorExpression(order + 1, Expr.Variable("x"), 0, func);

            var trueValue   = func.Evaluate(pointVar);
            var taylorValue = taylorExpr1.Evaluate(pointVar);

            return(trueValue.RealValue - taylorValue.RealValue);
        }
コード例 #2
0
        public static double solve(Expr expr, string varName)
        {
            Expr v = Expr.Variable(varName);

            expr = expr.RationalSimplify(v).Numerator().Expand();

            Expr[] coeff = expr.Coefficients(v);
            Console.WriteLine(coeff.Length);
            switch (coeff.Length)
            {
            case 1: return(0.0);   //Expr.Zero.Equals(coeff[0]) ? v : Expr.Undefined;

            case 2: return((-coeff[0] / coeff[1]).Expand().RationalSimplify(v).RealNumberValue);

            default: return(0.0);   //Expr.Undefined.RealNumberValue;
            }
        }
コード例 #3
0
        static void Main(string[] args)
        {
            var x  = Expr.Variable("x");
            var y1 = Expr.Variable("y1");
            var y2 = Expr.Variable("y2");

            //Example 1
            Dictionary <string, FloatingPoint> initialVal1 = new Dictionary <string, FloatingPoint>()
            {
                { "x", 0 },
                { "y1", 3 },
                { "y2", 0 }
            };

            Expr func1     = (-2 * y1 + 4 * y2);
            Expr func2     = (-y1 + 3 * y2);
            var  functions = new List <Expr>();

            functions.Add(func1);
            functions.Add(func2);

            DifEquationSystem equation1 =
                new DifEquationSystem(functions);

            double[] section1 = new double[] { 0, 1 };


            RungeKutta rungeKutta1 =
                new RungeKutta(equation1, initialVal1, section1, 10);

            Func <double, double> answer1 = (4 * (-x).Exp() - (2 * x).Exp()).Compile("x");
            Func <double, double> answer2 = ((-x).Exp() - (2 * x).Exp()).Compile("x");
            var answers = new List <Func <double, double> >();

            answers.Add(answer1);
            answers.Add(answer2);

            rungeKutta1.Solve();

            PrintRealAnswer(answers, initialVal1, section1, 10);

            Console.ReadKey();
        }
コード例 #4
0
        static void Main(string[] args)
        {
            var    x    = Expr.Variable("x");
            var    a    = Expr.Variable("a");
            string poly = "5x^2+3x+5";
            int    start;
            int    count;
            int    end;
            int    posOfx;
            //int counting;
            List <int> coeff = new List <int>();

            end    = poly.Length;
            start  = 0;
            count  = 0;
            posOfx = 0;
            //counting = 0;

            while ((start <= end) && (posOfx > -1))
            {
                count  = end - start;
                posOfx = poly.IndexOf('x', start, count);
                if (posOfx == -1)
                {
                    break;
                }
                start = posOfx + 1;
                coeff.Add(posOfx);
            }
            Console.WriteLine(coeff[0]);
            foreach (int y in coeff)
            {
                Console.WriteLine(coeff[y]);
            }
            Console.ReadLine();



            //  menu();
        }
コード例 #5
0
        public MainWindow()
        {
            InitializeComponent();
            var a = Expr.Variable("a");
            var b = Expr.Variable("b");

            var diff = a.Differentiate(a);

            // string latex = Expr.Parse("a_1_2_3^a^3^b/b_1^3").ToLaTeX();
            var latex = Diff(a, a) + " = " + diff.ToLaTeX();


            string fileName = @"formula.png";


            var parser  = new TexFormulaParser();
            var formula = parser.Parse(latex);
            //var renderer = formula.GetRenderer(TexStyle.Display, 20.0, "Arial");
            //var bitmapSource = renderer.RenderToGeometry(0.0, 0.0);
            var pngBytes = formula.RenderToPng(20.0, 0.0, 0.0, "Arial");

            File.WriteAllBytes(fileName, pngBytes);
        }
コード例 #6
0
        public static double CalculateLagrangeRemainder(Expr function, string variableName, int n, FloatingPoint x, FloatingPoint x0)
        {
            var results = new List <double>();

            var    length           = GetLength(Math.Min(x.RealValue, x0.RealValue), Math.Max(x.RealValue, x0.RealValue));
            var    increment        = length / 10;
            double currentIncrement = Math.Min(x.RealValue, x0.RealValue);

            var factorial = (FloatingPoint)SpecialFunctions.Factorial(n + 1);

            while (currentIncrement < Math.Max(x.RealValue, x0.RealValue))
            {
                var variable = new Dictionary <string, FloatingPoint>
                {
                    { variableName, currentIncrement }
                };

                var derivativeResult = function.CalculateFunctionAtDerivative(variable, Expr.Variable(variableName), n + 1);
                var result           = derivativeResult.RealValue / factorial.RealValue * Math.Pow(Math.Abs(x.RealValue - x0.RealValue), n + 1);
                results.Add(Math.Abs(result));
                currentIncrement += increment;
            }

            var variableMax = new Dictionary <string, FloatingPoint>
            {
                { variableName, Math.Max(x.RealValue, x0.RealValue) }
            };

            var derivativeResultMax = function.CalculateFunctionAtDerivative(variableMax, Expr.Variable(variableName), n + 1);
            var resultMax           = derivativeResultMax.RealValue / factorial.RealValue * Math.Pow(Math.Abs(x.RealValue - x0.RealValue), n + 1);

            results.Add(Math.Abs(resultMax));

            return(results.Max(x => x));
        }
コード例 #7
0
        static void Main(string[] args)
        {
            //Используемые переменныые
            var x1 = Expr.Variable("x1");
            var x2 = Expr.Variable("x2");
            var x3 = Expr.Variable("x3");

            //Пример 1
            var values1 = new Dictionary <string, FloatingPoint>
            {
                { "x1", 0.9 },
                { "x2", 0.5 }
            };

            var f1 = (x1 * x1);
            var f2 = (x2 * x2 - 1);
            var f3 = (x1 * x1 * x1);
            var f4 = (-x2);

            Expr[,] functions1 = new Expr[, ] {
                { f1, f2 }, { f3, f4 }
            };

            var system1 = new EquationsSystem(functions1);

            system1.Print();

            var newton1 = new Newton(system1, values1, 0.0001);

            newton1.Solve();

            //Пример 2
            var values2 = new Dictionary <string, FloatingPoint>
            {
                { "x1", 0.5 },
                { "x2", 0.5 },
                { "x3", 0.5 }
            };

            f1 = (x1 * x1);
            f2 = (x2 * x2);
            f3 = (x3 * x3 - 1);
            f4 = (2 * x1 * x1);
            var f5 = f2;
            var f6 = (-4 * x3);
            var f7 = (3 * x1 * x1);
            var f8 = (-4 * x2);
            var f9 = (x3 * x3);

            Expr[,] functions2 = new Expr[, ] {
                { f1, f2, f3 }, { f4, f5, f6 }, { f7, f8, f9 }
            };

            var system2 = new EquationsSystem(functions2);

            system2.Print();

            var newton2 = new Newton(system2, values2, 0.0001);

            newton2.Solve();

            Console.ReadLine();
        }
コード例 #8
0
 public static Expr simplify(Expr expr, string varName)
 {
     return(expr.RationalSimplify(Expr.Variable(varName)));
 }
コード例 #9
0
        static void Main(string[] args)
        {
            //Используемые переменныые
            var x1  = Expr.Variable("x1");
            var x2  = Expr.Variable("x2");
            var x3  = Expr.Variable("x3");
            var x4  = Expr.Variable("x4");
            var x5  = Expr.Variable("x5");
            var x6  = Expr.Variable("x6");
            var x7  = Expr.Variable("x7");
            var x8  = Expr.Variable("x8");
            var x9  = Expr.Variable("x9");
            var x10 = Expr.Variable("x10");


            //Пример 1
            var values1 = new Dictionary <string, FloatingPoint>
            {
                { "x1", 0.9 },
                { "x2", 0.5 }
            };

            var f1 = (x1 * x1);
            var f2 = (x2 * x2 - 1);
            var f3 = (x1 * x1 * x1);
            var f4 = (-x2);

            Expr[,] functions1 = new Expr[, ] {
                { f1, f2 }, { f3, f4 }
            };

            var system1 = new EquationsSystem(functions1);

            system1.Print();

            var newton1 = new NewtonMethodSystem(system1, values1, 0.0001);

            newton1.Solve();

            //Пример 2
            var values2 = new Dictionary <string, FloatingPoint>
            {
                { "x1", 0.5 },
                { "x2", 0.5 },
                { "x3", 0.5 }
            };

            f1 = (x1 * x1);
            f2 = (x2 * x2);
            f3 = (x3 * x3 - 1);
            f4 = (2 * x1 * x1);
            var f5 = f2;
            var f6 = (-4 * x3);
            var f7 = (3 * x1 * x1);
            var f8 = (-4 * x2);
            var f9 = (x3 * x3);

            Expr[,] functions2 = new Expr[, ] {
                { f1, f2, f3 }, { f4, f5, f6 }, { f7, f8, f9 }
            };

            var system2 = new EquationsSystem(functions2);

            system2.Print();

            var newton2 = new NewtonMethodSystem(system2, values2, 0.0001);

            newton2.Solve();


            Console.WriteLine("Введите уравнения системы, =0 в конце вводить не нужно");
            Console.WriteLine("По окончанию ввода введите 0");
            List <string> equations = new List <string>();

            while (true)
            {
                string temp = Console.ReadLine();
                if (temp == "0")
                {
                    break;
                }
                equations.Add(temp);
            }
            Expr[,] functions3 = new Expr[equations.Count, equations.Count];
            int i = 0;

            foreach (string equation in equations)
            {
                Expr        temp       = Expr.Parse(equation);
                List <Expr> expression = new List <Expr>();
                for (int j = 0; j < temp.NumberOfOperands; j++)
                {
                    expression.Add(temp[j]);
                }
                if (temp.NumberOfOperands > equations.Count)
                {
                    expression[0] = temp[0] + temp[1];
                    expression.RemoveAt(1);
                }
                for (int j = 0; j < equations.Count; j++)
                {
                    functions3[i, j] = expression[j];
                }
                i++;
            }

            var system3 = new EquationsSystem(functions3);

            Console.WriteLine("Введите изначальное приближение");
            var values3 = new Dictionary <string, FloatingPoint>(equations.Count);

            for (int j = 0; j < equations.Count; j++)
            {
                Console.Write($"x{j+1}=");
                String[]      temp          = Console.ReadLine().Split('=');
                FloatingPoint floatingPoint = float.Parse(temp[0]);
                values3.Add($"x{j + 1}", floatingPoint);
            }

            var newton3 = new NewtonMethodSystem(system3, values3, 0.0001);

            newton3.Solve();

            Console.ReadLine();
        }