public override Expression On(SymbolFunction e) { return((bool)(On(e as Function) as Logical) && e.Derivatives .Zip((Source as SymbolFunction).Derivatives, (ed, sd) => ed <= sd) .All(same => same)); }
public static Expression On(SymbolFunction e, Operation op) { return(new SymbolFunction(e, e.Arguments .Select(a => a.Perform(op)) .ToArray() )); }
static void Main(string[] args) { var x = new Symbol("x"); var f = new SymbolFunction("f"); var res1 = D(Sin(f[2 * x])); Console.WriteLine(res1.ToString()); var res2 = res1.Replace(f[~x] >> Pow(x, 2)); Console.WriteLine(res2.ToString()); var res3 = res1.Replace(f[x] >> Pow(x, 2)); Console.WriteLine(res3.ToString()); var res4 = res1.Replace(f[2 * x] >> Pow(x, 3)); Console.WriteLine(res4.ToString()); var res5 = res1.Replace(f[2 * ~x] >> Pow(x, 3)); Console.WriteLine(res5.ToString()); var res6 = (x + 2 * x - Pow(x, 2) + Sin(x)).Replace(x >> -4); Console.WriteLine(res6.ToString()); var y = new Symbol("y"); var res7 = -x + 2 - 4 + 3 * x + y - 5 - (-x) + 1 - 1; Console.WriteLine(res7.Canonical().ToString()); var res8 = res7.Simplify(); Console.WriteLine(res8.Canonical().ToString()); var res9 = ((x + 1) + 2).Replace((Expression)1 + 2 >> 5); Console.WriteLine(res9.ToString()); }
public Task <string> SymbolQueryAsync(SymbolFunction symbolFunction = null, BlockParameter blockParameter = null, CancellationToken cancellationToken = default(CancellationToken)) { return(ContractHandler.QueryRawAsync <SymbolFunction, StringBytes32Decoder, string>(symbolFunction, blockParameter, cancellationToken)); }
public void ReplaceFunction() { var x = new Symbol("x"); var f = new SymbolFunction("f"); Assert.IsTrue(_.Same(1 + 2 * (x + 2), (1 + f._(x + 2)).Replace( new Rule(f._(~x), 2 * x)))); }
public void SymbolFunctionDerivative() { var x = new Symbol("x"); var f = new SymbolFunction("f", x).D(); var g = new SymbolFunction("g", x, x).D(0).D(1).D(1); Assert.AreEqual("f'[x]", f.ToString()); Assert.AreEqual("g<1,2>[x,x]", g.ToString()); }
public static void ResetSymbolAsFunction(ISymbols symscope, string varname, LObject lobj) { // 1. Define the function in global symbol scope var lambda = lobj as LFunction; var funcExpr = lambda.Value as FunctionExpr; var symbol = new SymbolFunction(funcExpr.Meta); symbol.Name = varname; symbol.FuncExpr = funcExpr; symscope.Define(symbol); }
public void SymbolFunctionString() { var x = new Symbol("x"); var f = new SymbolFunction("f"); var g = new SymbolFunction("g", x); Assert.AreEqual("f[]", f.ToString()); Assert.AreEqual("g[x]", g.ToString()); Assert.AreEqual("f[x,x+1]", f._(x, x + 1).ToString()); }
public void ReplaceFunctionDerivative() { var x = new Symbol("x"); var f = new SymbolFunction("f", x); Assert.IsTrue(_.Same(1 + 2 * x, (1 + f.D()._(x)).Replace( new Rule(f._(~x), _.Pow(x, 2))))); Assert.IsTrue(_.Same(4 * x, (f.D()._(2 * x)).Replace( new Rule(f._(~x), _.Pow(x, 2))))); }
public void SameSymbolFunction() { var x = new Symbol("x"); var f = new SymbolFunction("f", x).D(); var g = new SymbolFunction("f", x).D(); var h = new SymbolFunction("h", x); Assert.IsTrue(_.Same(f, g)); Assert.IsFalse(_.Same(f, h)); }
public void DerivativeFormula() { var x = new Symbol("x"); var f = new SymbolFunction("f", x); var g = new SymbolFunction("g", x); Assert.AreEqual("f'[x]*g[x]+f[x]*g'[x]", _.D(f * g).ToString()); Assert.AreEqual("(f'[x]*g[x]+f[x]*g'[x]*ln[f[x]])*pow[f[x],g[x]-1]", _.D(_.Pow(f, g)).ToString()); }
public void SymbolFunctionCreate() { var x = new Symbol("x"); var f = new SymbolFunction("f"); var g = new SymbolFunction("g", x); Assert.AreEqual("f", f.Name); Assert.AreEqual(0, f.Arguments.Length); Assert.AreEqual("g", g.Name); Assert.AreEqual(1, g.Arguments.Length); var f1 = f._(x); Assert.AreEqual("f", f1.Name); Assert.AreEqual(1, f1.Arguments.Length); }
public void DerivativeFunctionVariable() { var x = new Symbol("x"); var y = new Symbol("y"); var f = new SymbolFunction("f", x, 2 * x, y, _.Pow(x, 2)); Assert.AreEqual( "f<1,0,0,0>[x,2*x,y,pow[x,2]]+2*f<0,1,0,0>[x,2*x,y,pow[x,2]]+2*x*f<0,0,0,1>[x,2*x,y,pow[x,2]]", _.D(f, x).ToString() ); // needs canonical form (not the same tree, yet the same expression) //Assert.IsTrue(_.Same( // f.D(0) + f.D(1)*2 + f.D(3)*2*x, // _.D(f, x) //)); }
public void OnParseLambdaBlock(Expr expr) { var fs = expr as FunctionExpr; // 1. Define the function in global symbol scope var funcSymbol = new SymbolFunction(fs.Meta); funcSymbol.FuncExpr = expr; // 2. Push the current scope. expr.SymScope = this._parser.Context.Symbols.Current; this._parser.Context.Symbols.Push(new SymbolsFunction(string.Empty), true); // 3. Parse the function block this._parser.ParseBlock(fs); // 4. Pop the symbols scope. this._parser.Context.Symbols.Pop(); }
/// <summary> /// Parses a block by first pushing symbol scope and then popping after completion. /// </summary> public override void ParseBlock(IBlockExpr stmt) { var fs = stmt as FunctionExpr; // 1. Define the function in global symbol scope var funcSymbol = new SymbolFunction(fs.Meta); funcSymbol.FuncExpr = stmt; // 2. Push the current scope. stmt.SymScope = this.Ctx.Symbols.Current; this.Ctx.Symbols.Push(new SymbolsFunction(string.Empty), true); // 3. Parse the function block _parser.ParseBlock(stmt); // 4. Pop the symbols scope. this.Ctx.Symbols.Pop(); }
/// <summary> /// Parses a block by first pushing symbol scope and then popping after completion. /// </summary> public override void ParseBlock(IBlockExpr stmt) { var fs = stmt as FunctionExpr; var funcName = fs.Meta.Name; // 1. Define the function in global symbol scope var funcSymbol = new SymbolFunction(fs.Meta) { FuncExpr = stmt }; Ctx.Symbols.Define(funcSymbol); // 2. Define the aliases. if (fs.Meta.Aliases?.Count > 0) { foreach (var alias in fs.Meta.Aliases) { Ctx.Symbols.DefineAlias(alias, fs.Meta.Name); } } // 3. Push the current scope. stmt.SymScope = Ctx.Symbols.Current; Ctx.Symbols.Push(new SymbolsFunction(fs.Meta.Name), true); // 4. Register the parameter names in the symbol scope. if (fs.Meta.Arguments?.Count > 0) { foreach (var arg in fs.Meta.Arguments) { Ctx.Symbols.DefineVariable(arg.Name, LTypes.Object); } } _parser.ParseBlock(stmt); Ctx.Symbols.Pop(); }
/// <summary> /// Parses a block by first pushing symbol scope and then popping after completion. /// </summary> public void OnParseFunctionDeclareBlock(BlockExpr expr) { var fs = expr as FunctionExpr; var funcName = fs.Meta.Name; // 1. Define the function in global symbol scope var funcSymbol = new SymbolFunction(fs.Meta); funcSymbol.FuncExpr = expr; this._parser.Context.Symbols.Define(funcSymbol); // 2. Define the aliases. if (fs.Meta.Aliases != null && fs.Meta.Aliases.Count > 0) { foreach (var alias in fs.Meta.Aliases) { this._parser.Context.Symbols.DefineAlias(alias, fs.Meta.Name); } } // 3. Push the current scope. expr.SymScope = this._parser.Context.Symbols.Current; this._parser.Context.Symbols.Push(new SymbolsFunction(fs.Meta.Name), true); // 4. Register the parameter names in the symbol scope. if (fs.Meta.Arguments != null && fs.Meta.Arguments.Count > 0) { foreach (var arg in fs.Meta.Arguments) { this._parser.Context.Symbols.DefineVariable(arg.Name, LTypes.Object); } } this._parser.ParseBlock(expr); this._parser.Context.Symbols.Pop(); }
/// <summary> /// Parses a block by first pushing symbol scope and then popping after completion. /// </summary> public override void ParseBlock(BlockExpr stmt) { var fs = stmt as FunctionExpr; var funcName = fs.Name; // 1. Define the function in global symbol scope var funcSymbol = new SymbolFunction(fs.Meta); funcSymbol.FuncExpr = stmt; this.Ctx.Symbols.Define(funcSymbol); // 2. Define the aliases. if (!fs.Meta.Aliases.IsNullOrEmpty()) { foreach (var alias in fs.Meta.Aliases) { this.Ctx.Symbols.DefineAlias(alias, fs.Meta.Name); } } // 3. Push the current scope. this.Ctx.Symbols.Push(new SymbolsFunction(fs.Name), true); // 4. Register the parameter names in the symbol scope. if (!fs.Meta.Arguments.IsNullOrEmpty()) { foreach (var arg in fs.Meta.Arguments) { this.Ctx.Symbols.DefineVariable(arg.Name, LTypes.Object); } } stmt.SymScope = this.Ctx.Symbols.Current; _parser.ParseBlock(stmt); this.Ctx.Symbols.Pop(); }
public override Expression On(SymbolFunction e) { foreach (var rule in Rules) { Rule[] patternRules; if (rule.Left.Match(e, out patternRules)) { var left = rule.Left as SymbolFunction; var tmpRules = patternRules .Select(r => new Rule(r.Left, tmpSymbol())) .ToArray(); var invTmpRules = patternRules .Zip(tmpRules, (pr, tr) => new Rule(tr.Right, pr.Right)) .ToArray(); var res = rule.Right.Replace(tmpRules); var f = left.Replace(tmpRules) as SymbolFunction; for (int i = 0; i < e.Derivatives.Length; ++i) { var vars = f.Symbols().Where(s => isTemp(s)); if (e.Derivatives[i] != left.Derivatives[i]) { if (vars.Count() == 0 || vars.Count() > 1) { return(Clone.On(e, new Replace(Rules))); } for (int d = left.Derivatives[i]; d < e.Derivatives[i]; ++d) { res = _.D(res, vars.Single()) / _.D(f.Arguments[i], vars.Single()); } } } return(res.Replace(invTmpRules)); } } return(Clone.On(e, new Replace(Rules))); }
public Task <string> SymbolQueryAsync(SymbolFunction symbolFunction, BlockParameter blockParameter = null) { return(ContractHandler.QueryAsync <SymbolFunction, string>(symbolFunction, blockParameter)); }
public Task <string> SymbolQueryAsync(SymbolFunction symbolFunction = null, BlockParameter blockParameter = null) { return(ContractHandler.QueryRawAsync <SymbolFunction, StringBytes32Decoder, string>(symbolFunction, blockParameter)); }
public override Expression On(SymbolFunction e) { return(On(e, Activator.CreateInstance(this.GetType()) as Operation)); }
public override Expression On(SymbolFunction e) { return(e.Arguments .Select((arg, i) => e.D(i) * _.D(arg, x)) .Aggregate((a, b) => a + b)); }
public override Expression On(SymbolFunction e) { return((bool)(On(e as Function) as Logical) && e.Derivatives.SequenceEqual((Source as SymbolFunction).Derivatives)); }
/// <summary> /// Parses a block by first pushing symbol scope and then popping after completion. /// </summary> public override void ParseBlock(BlockExpr stmt) { var fs = stmt as FunctionExpr; // 1. Define the function in global symbol scope var funcSymbol = new SymbolFunction(fs.Meta); funcSymbol.FuncExpr = stmt; // 2. Push the current scope. stmt.SymScope = this.Ctx.Symbols.Current; this.Ctx.Symbols.Push(new SymbolsFunction(string.Empty), true); // 3. Parse the function block _parser.ParseBlock(stmt); // 4. Pop the symbols scope. this.Ctx.Symbols.Pop(); }
public virtual Expression On(SymbolFunction e) => On(e as Function);
/// <summary> /// Parses a block by first pushing symbol scope and then popping after completion. /// </summary> public override void ParseBlock(BlockExpr stmt) { var fs = stmt as FunctionExpr; var funcName = fs.Name; // 1. Define the function in global symbol scope var funcSymbol = new SymbolFunction(fs.Meta); funcSymbol.FuncExpr = stmt; this.Ctx.Symbols.Define(funcSymbol); // 2. Define the aliases. if (fs.Meta.Aliases != null && fs.Meta.Aliases.Count > 0) foreach (var alias in fs.Meta.Aliases) this.Ctx.Symbols.DefineAlias(alias, fs.Meta.Name); // 3. Push the current scope. stmt.SymScope = this.Ctx.Symbols.Current; this.Ctx.Symbols.Push(new SymbolsFunction(fs.Name), true); // 4. Register the parameter names in the symbol scope. if( fs.Meta.Arguments != null && fs.Meta.Arguments.Count > 0) foreach(var arg in fs.Meta.Arguments) this.Ctx.Symbols.DefineVariable(arg.Name, LTypes.Object); _parser.ParseBlock(stmt); this.Ctx.Symbols.Pop(); }