Exemplo n.º 1
0
        protected override Expression VisitSet(Set S)
        {
            IEnumerable <Expression> members = VisitList(S.Members);

            if (ReferenceEquals(members, null))
            {
                return(null);
            }
            return(ReferenceEquals(members, S.Members) ? S : Set.New(members));
        }
Exemplo n.º 2
0
        public override Expression Substitute(Call C, IDictionary <Expression, Expression> x0, bool IsTransform)
        {
            if (IsTransform)
            {
                return(base.Substitute(C, x0, IsTransform));
            }

            Dictionary <Expression, Expression> now = new Dictionary <Expression, Expression>(x0);
            List <Arrow> late = new List <Arrow>();

            foreach (var i in method.GetParameters().Zip(C.Arguments, (p, a) => new { p, a }))
            {
                if (i.p.CustomAttribute <NoSubstitute>() != null)
                {
                    if (now.ContainsKey(i.a))
                    {
                        late.Add(Arrow.New(i.a, now[i.a]));
                        now.Remove(i.a);
                    }
                }
            }

            if (!now.Empty())
            {
                C = ComputerAlgebra.Call.New(C.Target, C.Arguments.Select(i => i.Substitute(now)));
            }

            if (late.Empty())
            {
                return(C);
            }
            else
            {
                return(ComputerAlgebra.Substitute.New(C, late.Count > 1 ? (Expression)Set.New(late) : late.Single()));
            }
        }
Exemplo n.º 3
0
        //P is
        //    if next is a unary operator
        //         const op := unary(next)
        //         consume
        //         q := prec( op )
        //         const t := Exp( q )
        //         return mkNode( op, t )
        //    else if next = "("
        //         consume
        //         const t := Exp( 0 )
        //         expect ")"
        //         return t
        //    else if next is a v
        //         const t := mkLeaf( next )
        //         consume
        //         return t
        //    else
        //         error
        private Expression P()
        {
            Operator op = new Operator();

            if (tokens.Tok == "+")
            {
                // Skip unary +.
                tokens.Consume();
                return(P());
            }
            else if (IsUnaryPreOperator(tokens.Tok, ref op))
            {
                // Unary operator.
                tokens.Consume();
                Expression t = Exp(Precedence(op));
                return(Unary.New(op, t));
            }
            else if (tokens.Tok == "(")
            {
                // Group.
                tokens.Consume();
                Expression t = Exp(0);
                tokens.Expect(")");
                return(t);
            }
            else if (tokens.Tok == "{")
            {
                // Set.
                tokens.Consume();
                return(Set.New(L(",", "}")));
            }
            else if (tokens.Tok == "[")
            {
                // Matrix.
                tokens.Consume();
                List <List <Expression> > entries = new List <List <Expression> >();
                while (tokens.Tok == "[")
                {
                    tokens.Consume();
                    entries.Add(L(",", "]"));
                }
                tokens.Expect("]");

                return(Matrix.New(entries));
            }
            else
            {
                string tok = tokens.Consume();

                decimal dec = 0;
                double  dbl = 0.0;
                if (decimal.TryParse(tok, NumberStyles.Float, culture, out dec))
                {
                    return(Constant.New(dec));
                }
                if (double.TryParse(tok, NumberStyles.Float, culture, out dbl))
                {
                    return(Constant.New(dbl));
                }
                else if (tok == "True")
                {
                    return(Constant.New(true));
                }
                else if (tok == "False")
                {
                    return(Constant.New(false));
                }
                else if (tok == "\u221E" || tok == "oo")
                {
                    return(Constant.New(Real.Infinity));
                }
                else if (tokens.Tok == "[")
                {
                    // Bracket function call.
                    tokens.Consume();
                    List <Expression> args = L(",", "]");
                    return(Call.New(Resolve(tok, args), args));
                }
                else if (tokens.Tok == "(")
                {
                    // Paren function call.
                    tokens.Consume();
                    List <Expression> args = L(",", ")");
                    return(Call.New(Resolve(tok, args), args));
                }
                else
                {
                    return(Resolve(tok));
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Solve a differential equation or system of differential equations f for functions y[t], with initial conditions y^(n)[0] = y0.
        /// </summary>
        /// <param name="f">Equation or set of equations to solve.</param>
        /// <param name="y">Functions to solve for.</param>
        /// <param name="y0">Values for y^(n)[0].</param>
        /// <param name="t">Independent variable.</param>
        /// <returns>Expression or set of expressions for y.</returns>
        public static Expression DSolve(Expression f, Expression y, Expression y0, Expression t)
        {
            IEnumerable <Expression> result = Set.MembersOf(f).Cast <Equal>().DSolve(Set.MembersOf(y), Set.MembersOf(y0).Cast <Arrow>(), t);

            return((f is Set || result.Count() != 1) ? Set.New(result) : result.Single());
        }
Exemplo n.º 5
0
        public static Expression NSolve(Expression f, Expression x)
        {
            IEnumerable <Expression> result = Set.MembersOf(f).Cast <Equal>().NSolve(Set.MembersOf(x).Cast <Arrow>());

            return((f is Set || result.Count() != 1) ? Set.New(result) : result.Single());
        }