private static Expression Split(Expression P) { int child = 0; if (P is Sum) { return(ComputerAlgebra.Sum.New(((ComputerAlgebra.Sum)P).Terms.Select(i => ChildPattern(ref child)))); } if (P is Product) { return(Product.New(((Product)P).Terms.Select(i => ChildPattern(ref child)))); } if (P is Binary) { return(Binary.New(((Binary)P).Operator, ChildPattern(ref child), ChildPattern(ref child))); } if (P is Unary) { return(Unary.New(((Unary)P).Operator, ChildPattern(ref child))); } if (P is Call) { return(Call.New(((Call)P).Target, ((Call)P).Arguments.Select(i => ChildPattern(ref child)))); } return(null); }
protected override Expression VisitCall(Call F) { IEnumerable <Expression> arguments = VisitList(F.Arguments); if (ReferenceEquals(arguments, null)) { return(null); } return(ReferenceEquals(arguments, F.Arguments) ? F : Call.New(F.Target, arguments)); }
protected override Expression VisitCall(Call F) { F = (Call)base.VisitCall(F); if (F.Target is UnknownFunction) { IEnumerable <Function> lookup = ns.LookupFunction(F.Target.Name, F.Arguments); switch (lookup.Count()) { case 0: return(F); case 1: return(Call.New(lookup.Single(), F.Arguments)); default: throw new UnresolvedName(F.Target.Name); } } return(F); }
//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)); } } }
// Resolve a call to a function Name in the global namespace. private static Call CallGlobal(string Name, params Expression[] Args) { return(Call.New(Namespace.Global.Resolve(Name, Args), Args)); }
public static Expression Delta(Expression x) { return(Call.New(d, x)); }