Example #1
0
        public static VariableContextExpression diff(VariableContextExpression expression, string variableName)
        {
            VariableContext c    = expression.VariableContext;
            Expression      diff = new Differ(expression.Expression, variableName).Calculate(c);

            TypeCheckingContext context = new MathyLanguageService().CreateTypeCheckingContext(c);

            foreach (VariableInfo variable in expression.Variables)
            {
                c.Set(variable.Name, context.CreateAutoCreatedVariableValue(variable.Type));
            }

            VariableContextExpression result = new VariableContextExpression(diff, expression.Variables, 0, 0)
            {
                VariableContext = expression.VariableContext
            };

            new MathyLanguageService().CreateTypeChecker().PerformTypeChecking(result, context);

            result = new VariableContextExpression(new Cherimoya.Reduction.ExpressionReductor(new MathyLanguageService()).Reduce(diff), expression.Variables, 0, 0)
            {
                VariableContext = expression.VariableContext
            };
            new MathyLanguageService().CreateTypeChecker().PerformTypeChecking(result, context);

            foreach (VariableInfo variable in expression.Variables)
            {
                context.VariableContext.Remove(variable.Name);
            }


            return(result);
        }
Example #2
0
        public static VariableContextExpression ucomb(VariableContextExpression func, Matrix r, Vector u)
        {
            int count = func.Variables.Length;

            VariableContextExpression[] diffs = func.Variables.Select(i => ExpressionFuncs.diff(func, i.Name)).ToArray();


            Expression e1 = null;

            for (int i = 0; i <= count - 1; i++)
            {
                Expression right = BinaryExpression.Create(BinaryOperator.Multiply,
                                                           ConstantExpression.create(u[i] * u[i], 0, 0),
                                                           new FunctionCallExpression("pow",
                                                                                      new Expression[]
                {
                    diffs[i].Expression,
                    ConstantExpression.create(2, 0, 0)
                },
                                                                                      0,
                                                                                      0));

                e1 = e1 == null ? right : BinaryExpression.Create(BinaryOperator.Add, e1, right);
            }


            Expression e2 = null;

            for (int i = 1; i <= count - 1; i++)
            {
                for (int j = i + 1; j <= count; j++)
                {
                    Expression right = BinaryExpression.Create(BinaryOperator.Multiply,
                                                               ConstantExpression.create(u[i - 1] * u[j - 1] * r[i - 1, j - 1], 0, 0),
                                                               BinaryExpression.Create(BinaryOperator.Multiply,
                                                                                       diffs[i - 1].Expression,
                                                                                       diffs[j - 1].Expression));

                    e2 = e2 == null ? right : BinaryExpression.Create(BinaryOperator.Add, e2, right);
                }
            }

            e2 = BinaryExpression.Create(BinaryOperator.Multiply,
                                         ConstantExpression.create(2, 0, 0),
                                         e2);


            Expression e = BinaryExpression.Create(BinaryOperator.Add,
                                                   e1,
                                                   e2);


            VariableContext     c       = func.VariableContext;
            TypeCheckingContext context = new MathyLanguageService().CreateTypeCheckingContext(c);

            foreach (VariableInfo variable in func.Variables)
            {
                c.Set(variable.Name, context.CreateAutoCreatedVariableValue(variable.Type));
            }

            VariableContextExpression result = new VariableContextExpression(e, func.Variables, 0, 0)
            {
                VariableContext = func.VariableContext
            };

            new MathyLanguageService().CreateTypeChecker().PerformTypeChecking(result, context);

            result = new VariableContextExpression(new Cherimoya.Reduction.ExpressionReductor(new MathyLanguageService()).Reduce(e), func.Variables, 0, 0)
            {
                VariableContext = func.VariableContext
            };
            new MathyLanguageService().CreateTypeChecker().PerformTypeChecking(result, context);

            foreach (VariableInfo variable in func.Variables)
            {
                context.VariableContext.Remove(variable.Name);
            }


            return(result);
        }