/// <example> /// <code lang="CS"> /// using System; /// using Genetibase.MathX.Core; /// /// namespace Genetibase.MathX.Core.Tests /// { /// public class ParametricSurfaceSample /// { /// [STAThread] /// static void Main(string[] args) /// { /// // create function /// ParametricSurface function = new ParametricSurface("u+v","u-v","u*v"); /// /// // calculate function _Function; /// for (int u = 0; u < 100; u++) /// for (int v = 0; v < 100; v++) /// { /// Console.WriteLine("f({0},{1}) = ({2},{3},{4})", /// u,v,function.ValueAt(u,v).X,function.ValueAt(u,v).Y, /// function.ValueAt(u,v).Z); /// } /// /// } /// } /// } /// </code> /// </example> public ParametricSurface(string expressionX, string expressionY, string expressionZ) { if (expressionX == null) { throw new ArgumentNullException("expressionX"); } if (expressionY == null) { throw new ArgumentNullException("expressionY"); } if (expressionZ == null) { throw new ArgumentNullException("expressionZ"); } _expressionTreeX = new ExpressionTree(expressionX); _expressionTreeY = new ExpressionTree(expressionY); _expressionTreeZ = new ExpressionTree(expressionZ); if (_expressionTreeX.Variables.Length > 2) { throw new ArgumentException("expressionX"); } if (_expressionTreeY.Variables.Length > 2) { throw new ArgumentException("expressionY"); } if (_expressionTreeZ.Variables.Length > 2) { throw new ArgumentException("expressionZ"); } _expressionX = expressionX; _expressionY = expressionY; _expressionZ = expressionZ; _expression = string.Format("{0};{1};{2}", _expressionX, _expressionY, _expressionZ); ExpressionCompiler compiler = new ExpressionCompiler(new ExpressionTree[] { _expressionTreeX, _expressionTreeY, _expressionTreeZ }); _functionX = FunctionFactory.CreateRealFunction(_expressionTreeX); _functionY = FunctionFactory.CreateRealFunction(_expressionTreeY); _functionZ = FunctionFactory.CreateRealFunction(_expressionTreeZ); _function = (ParametricSurfaceDelegate)compiler.CreateDelegate(typeof(ParametricSurfaceDelegate), "return new Point3D({0},{1},{2});"); base._delegate = _function; base._definitionType = DefinitionType.Analytic; }
/// <example> /// <code lang="CS"> /// using System; /// using Genetibase.MathX.Core; /// /// namespace Genetibase.MathX.Core.Tests /// { /// public class Parameter2DFunctionSample /// { /// [STAThread] /// static void Main(string[] args) /// { /// // create function /// Parameter2DFunction function = new Parameter2DFunction("10*sin(t)","10*cos(t)"); /// /// // calculate function _Function; /// for (int i = 0; i < 100; i++) /// { /// Console.WriteLine("f({0}) = ({1},{2})",i,function.ValueAt(i).X,function.ValueAt(i).Y); /// } /// /// } /// } /// } /// </code> /// </example> /// <summary>Initializes a function that defined by delegate.</summary> public Parameter2DFunction(string expressionX, string expressionY) { if (expressionX == null) { throw new ArgumentNullException("expressionX"); } if (expressionY == null) { throw new ArgumentNullException("expressionY"); } _expressionTreeX = new ExpressionTree(expressionX); _expressionTreeY = new ExpressionTree(expressionY); if (_expressionTreeX.Variables.Length > 1) { throw new ArgumentException("expressionX"); } if (_expressionTreeY.Variables.Length > 1) { throw new ArgumentException("expressionY"); } _expressionX = expressionX; _expressionY = expressionY; _expression = string.Format("{0};{1}", _expressionX, _expressionY); ExpressionCompiler compilerX = new ExpressionCompiler(_expressionTreeX); ExpressionCompiler compilerY = new ExpressionCompiler(_expressionTreeY); if (_expressionTreeX.Variables.Length == 0) { _functionX = new Constant(_expressionTreeX.ToString()); } else { _functionX = new Explicit2DFunction(_expressionTreeX.ToString()); } if (_expressionTreeY.Variables.Length == 0) { _functionY = new Constant(_expressionTreeY.ToString()); } else { _functionY = new Explicit2DFunction(_expressionTreeY.ToString()); } _function = DelegateFactory.CreateParameter2DFunctionDelegate( _functionX.ValueAt, _functionY.ValueAt); base._delegate = _function; base._definitionType = DefinitionType.Analytic; }
/// <summary>Differentiates symbolic expression by specified variable.</summary> /// <returns>bynary tree that represents derivative of source tree</returns> /// <param name="tree"><strong>ExpressionTree</strong> binary tree of expression.</param> /// <param name="diffVar">variable of differentiation</param> public static ExpressionTree Differentiate(ExpressionTree tree, string diffVar) { if (tree == null) throw new ArgumentNullException("tree"); if (diffVar == null) throw new ArgumentNullException("diffVar"); ExpressionTree diffTree = new ExpressionTree(df(tree.RootNode,diffVar)); diffTree.Simplify(); return new ExpressionTree(diffTree.ToString()); }
private Explicit3DFunction(ExpressionTree tree) { _expressionTree = tree; _expression = _expressionTree.ToString(); ExpressionCompiler compiler = new ExpressionCompiler(_expressionTree); _function = (BivariateRealFunction)compiler.CreateDelegate(typeof(BivariateRealFunction)); base._delegate = _function; base._definitionType = DefinitionType.Analytic; }
private Implicit2DFunction (ExpressionTree tree) { _expressionTree = tree; _expression = _expressionTree.ToString(); ExpressionCompiler compiler = new ExpressionCompiler(_expressionTree); _function = (BivariateRealFunction) compiler.CreateDelegate(typeof(BivariateRealFunction)); base._delegate = _function; base._definitionType = DefinitionType.Analytic; }
/// <example> /// <code lang="CS"> /// using System; /// using Genetibase.MathX.Core; /// /// namespace Genetibase.MathX.Core.Tests /// { /// public class Parameter3DFunctionSample /// { /// [STAThread] /// static void Main(string[] args) /// { /// // create function /// Parameter3DFunction function = new Parameter3DFunction("10*sin(t)","10*cos(t)", "10*tan(t)"); /// /// // calculate function _Function; /// for (int i = 0; i < 100; i++) /// { /// Console.WriteLine("f({0}) = ({1},{2},{3})",i,function.ValueAt(i).X,function.ValueAt(i).Y, function.ValueAt(i).Z); /// } /// /// } /// } /// } /// </code> /// </example> /// <summary><para>Initializes a function that defined by expression.</para></summary> public Parameter3DFunction(string expressionX, string expressionY, string expressionZ) { if (expressionX == null) { throw new ArgumentNullException("expressionX"); } if (expressionY == null) { throw new ArgumentNullException("expressionY"); } if (expressionZ == null) { throw new ArgumentNullException("expressionZ"); } _expressionTreeX = new ExpressionTree(expressionX); _expressionTreeY = new ExpressionTree(expressionY); _expressionTreeZ = new ExpressionTree(expressionZ); if (_expressionTreeX.Variables.Length > 1) { throw new ArgumentException("expressionX"); } if (_expressionTreeY.Variables.Length > 1) { throw new ArgumentException("expressionY"); } if (_expressionTreeZ.Variables.Length > 1) { throw new ArgumentException("expressionZ"); } _expressionX = expressionX; _expressionY = expressionY; _expressionZ = expressionZ; _expression = string.Format("{0};{1};{2}", _expressionX, _expressionY, _expressionZ); _functionX = FunctionFactory.CreateRealFunction(_expressionTreeX); _functionY = FunctionFactory.CreateRealFunction(_expressionTreeY); _functionZ = FunctionFactory.CreateRealFunction(_expressionTreeZ); _function = DelegateFactory.CreateParameter3DFunctionDelegate( _functionX.ValueAt, _functionY.ValueAt, _functionZ.ValueAt); base._delegate = _function; base._definitionType = DefinitionType.Analytic; }
public static Function CreateRealFunction(ExpressionTree tree) { switch (tree.Variables.Length) { case 0: return new Constant(tree.ToString()); case 1: return new Explicit2DFunction(tree.ToString()); case 2: return new Explicit3DFunction(tree.ToString()); default: return null; } }
private Function CalculateAnalyticDerivative() { ExpressionTree diffTree = AnalyticDifferentiator.Differentiate(_expressionTree, _expressionTree.Variables[0]); switch (diffTree.Variables.Length) { case 0: return(new Constant(diffTree.ToString())); case 1: return(new Explicit2DFunction(diffTree)); default: throw new InvalidOperationException(); } }
/// <summary>Differentiates symbolic expression by specified variable.</summary> /// <returns>bynary tree that represents derivative of source tree</returns> /// <param name="tree"><strong>ExpressionTree</strong> binary tree of expression.</param> /// <param name="diffVar">variable of differentiation</param> public static ExpressionTree Differentiate(ExpressionTree tree, string diffVar) { if (tree == null) { throw new ArgumentNullException("tree"); } if (diffVar == null) { throw new ArgumentNullException("diffVar"); } ExpressionTree diffTree = new ExpressionTree(df(tree.RootNode, diffVar)); diffTree.Simplify(); return(new ExpressionTree(diffTree.ToString())); }
public static Function CreateRealFunction(ExpressionTree tree) { switch (tree.Variables.Length) { case 0: return(new Constant(tree.ToString())); case 1: return(new Explicit2DFunction(tree.ToString())); case 2: return(new Explicit3DFunction(tree.ToString())); default: return(null); } }
public Constant(string expression) { if (expression == null) throw new ArgumentNullException("expression"); _expressionTree = new ExpressionTree(_expression); if (_expressionTree.Variables.Length != 0) throw new ArgumentException("Constant function cant have any variable in expression","expression"); _expression = expression; ExpressionCompiler compiler = new ExpressionCompiler(_expressionTree); _function = (ConstantFunction) compiler.CreateDelegate(typeof(ConstantFunction)); base._delegate = _function; base._definitionType = DefinitionType.Analytic; }
/// <summary>Initializes a function that defined by expression.</summary> /// <exception cref="System.ArgumentException" caption=""></exception> /// <exception cref="System.ArgumentNullException" caption=""></exception> /// <exception cref="ExpressionSyntaxException" caption="ExpressionSyntaxException"></exception> public Implicit2DFunction(string expression) { if (expression == null) throw new ArgumentNullException("expression"); _expressionTree = new ExpressionTree(expression); if (_expressionTree.Variables.Length != 2) throw new ArgumentException("Implicit 2D function must have two variables in expression","expression"); _expression = expression; ExpressionCompiler compiler = new ExpressionCompiler(_expressionTree); _function = (BivariateRealFunction) compiler.CreateDelegate(typeof(BivariateRealFunction)); base._delegate = _function; base._definitionType = DefinitionType.Analytic; }
/// <example> /// <code lang="CS"> /// using System; /// using System.Drawing; /// /// using Genetibase.MathX.Core; /// using Genetibase.MathX.Core.Plotters; /// /// namespace Genetibase.MathX.Core.Tests /// { /// public class Implicit3DFunctionSample /// { /// [STAThread] /// static void Main(string[] args) /// { /// // create function /// Implicit3DFunction function = new Implicit3DFunction("x^2 + y^2 + z^2 - 10^2"); /// /// // create function plotter to plot function in specified range /// Implicit3DFunctionPlotter plotter = new Implicit3DFunctionPlotter(function); /// /// // write function roots in [0,0 - 10,10] range /// foreach (Point3D point in plotter.Plot(new Point3D(-20,-20,-20), new Point3D(20,20,20), 1,1,1)) /// { /// Console.WriteLine("Function root at point ({0},{1},{2})", point.X, point.Y, point.Z); /// } /// } /// } /// } /// </code> /// </example> /// <exception cref="System.ArgumentException" caption=""></exception> /// <exception cref="System.ArgumentNullException" caption=""></exception> /// <exception cref="ExpressionSyntaxException" caption="ExpressionSyntaxException"></exception> /// <summary>Initializes a function that defined by expression.</summary> public Implicit3DFunction(string expression) { if (expression == null) { throw new ArgumentNullException("expression"); } _expressionTree = new ExpressionTree(expression); if (_expressionTree.Variables.Length != 3) { throw new ArgumentException("Implicit 3D function must have three variables in expression", "expression"); } _expression = expression; ExpressionCompiler compiler = new ExpressionCompiler(_expressionTree); _function = (TrivariateRealFunction)compiler.CreateDelegate(typeof(TrivariateRealFunction)); base._delegate = _function; base._definitionType = DefinitionType.Analytic; }
public Constant(string expression) { if (expression == null) { throw new ArgumentNullException("expression"); } _expressionTree = new ExpressionTree(_expression); if (_expressionTree.Variables.Length != 0) { throw new ArgumentException("Constant function cant have any variable in expression", "expression"); } _expression = expression; ExpressionCompiler compiler = new ExpressionCompiler(_expressionTree); _function = (ConstantFunction)compiler.CreateDelegate(typeof(ConstantFunction)); base._delegate = _function; base._definitionType = DefinitionType.Analytic; }
private Function CalculateAnalyticDerivative() { string formula = String.Format("-(({0})/({1}))", AnalyticDifferentiator.Differentiate(_expressionTree, _expressionTree.Variables[0]), AnalyticDifferentiator.Differentiate(_expressionTree, _expressionTree.Variables[1])); ExpressionTree diffTree = new ExpressionTree(formula); switch (diffTree.Variables.Length) { case 0: return(new Constant(diffTree.ToString())); case 1: return(new Explicit2DFunction(diffTree.ToString())); case 2: return(new Implicit2DFunction(diffTree.ToString())); default: throw new InvalidOperationException(); } }
private Function CalculateAnalyticDerivative() { string formula = String.Format("-(({0})/({1}))", AnalyticDifferentiator.Differentiate(_expressionTree,_expressionTree.Variables[0]), AnalyticDifferentiator.Differentiate(_expressionTree,_expressionTree.Variables[1])); ExpressionTree diffTree = new ExpressionTree(formula); switch (diffTree.Variables.Length) { case 0: return new Constant(diffTree.ToString()); case 1: return new Explicit2DFunction(diffTree.ToString()); case 2: return new Implicit2DFunction(diffTree.ToString()); default: throw new InvalidOperationException(); } }
/// <example> /// <code lang="CS"> /// using System; /// using Genetibase.MathX.Core; /// /// namespace Genetibase.MathX.Core.Tests /// { /// public class Parameter2DFunctionSample /// { /// [STAThread] /// static void Main(string[] args) /// { /// // create function /// Parameter2DFunction function = new Parameter2DFunction("10*sin(t)","10*cos(t)"); /// /// // calculate function _Function; /// for (int i = 0; i < 100; i++) /// { /// Console.WriteLine("f({0}) = ({1},{2})",i,function.ValueAt(i).X,function.ValueAt(i).Y); /// } /// /// } /// } /// } /// </code> /// </example> /// <summary>Initializes a function that defined by delegate.</summary> public Parameter2DFunction(string expressionX,string expressionY) { if (expressionX == null) throw new ArgumentNullException("expressionX"); if (expressionY == null) throw new ArgumentNullException("expressionY"); _expressionTreeX = new ExpressionTree(expressionX); _expressionTreeY = new ExpressionTree(expressionY); if (_expressionTreeX.Variables.Length > 1) throw new ArgumentException("expressionX"); if (_expressionTreeY.Variables.Length > 1) throw new ArgumentException("expressionY"); _expressionX = expressionX; _expressionY = expressionY; _expression = string.Format("{0};{1}",_expressionX,_expressionY); ExpressionCompiler compilerX = new ExpressionCompiler(_expressionTreeX); ExpressionCompiler compilerY = new ExpressionCompiler(_expressionTreeY); if (_expressionTreeX.Variables.Length == 0) _functionX = new Constant(_expressionTreeX.ToString()); else _functionX = new Explicit2DFunction(_expressionTreeX.ToString()); if (_expressionTreeY.Variables.Length == 0) _functionY = new Constant(_expressionTreeY.ToString()); else _functionY = new Explicit2DFunction(_expressionTreeY.ToString()); _function = DelegateFactory.CreateParameter2DFunctionDelegate( _functionX.ValueAt,_functionY.ValueAt); base._delegate = _function; base._definitionType = DefinitionType.Analytic; }
public ExpressionCompiler(ExpressionTree tree) { _trees = new ExpressionTree[]{ tree }; }
public ExpressionCompiler(ExpressionTree[] trees) { _trees = trees; }
/// <example> /// <code lang="CS"> /// using System; /// using Genetibase.MathX.Core; /// /// namespace Genetibase.MathX.Core.Tests /// { /// public class ParametricSurfaceSample /// { /// [STAThread] /// static void Main(string[] args) /// { /// // create function /// ParametricSurface function = new ParametricSurface("u+v","u-v","u*v"); /// /// // calculate function _Function; /// for (int u = 0; u < 100; u++) /// for (int v = 0; v < 100; v++) /// { /// Console.WriteLine("f({0},{1}) = ({2},{3},{4})", /// u,v,function.ValueAt(u,v).X,function.ValueAt(u,v).Y, /// function.ValueAt(u,v).Z); /// } /// /// } /// } /// } /// </code> /// </example> public ParametricSurface(string expressionX,string expressionY,string expressionZ) { if (expressionX == null) throw new ArgumentNullException("expressionX"); if (expressionY == null) throw new ArgumentNullException("expressionY"); if (expressionZ == null) throw new ArgumentNullException("expressionZ"); _expressionTreeX = new ExpressionTree(expressionX); _expressionTreeY = new ExpressionTree(expressionY); _expressionTreeZ = new ExpressionTree(expressionZ); if (_expressionTreeX.Variables.Length > 2) throw new ArgumentException("expressionX"); if (_expressionTreeY.Variables.Length > 2) throw new ArgumentException("expressionY"); if (_expressionTreeZ.Variables.Length > 2) throw new ArgumentException("expressionZ"); _expressionX = expressionX; _expressionY = expressionY; _expressionZ = expressionZ; _expression = string.Format("{0};{1};{2}", _expressionX,_expressionY,_expressionZ); ExpressionCompiler compiler = new ExpressionCompiler(new ExpressionTree[] { _expressionTreeX, _expressionTreeY, _expressionTreeZ }); _functionX = FunctionFactory.CreateRealFunction(_expressionTreeX); _functionY = FunctionFactory.CreateRealFunction(_expressionTreeY); _functionZ = FunctionFactory.CreateRealFunction(_expressionTreeZ); _function = (ParametricSurfaceDelegate)compiler.CreateDelegate(typeof(ParametricSurfaceDelegate), "return new Point3D({0},{1},{2});"); base._delegate = _function; base._definitionType = DefinitionType.Analytic; }
public ExpressionCompiler(ExpressionTree tree) { _trees = new ExpressionTree[] { tree }; }
/// <example> /// <code lang="CS"> /// using System; /// using Genetibase.MathX.Core; /// /// namespace Genetibase.MathX.Core.Tests /// { /// public class Parameter3DFunctionSample /// { /// [STAThread] /// static void Main(string[] args) /// { /// // create function /// Parameter3DFunction function = new Parameter3DFunction("10*sin(t)","10*cos(t)", "10*tan(t)"); /// /// // calculate function _Function; /// for (int i = 0; i < 100; i++) /// { /// Console.WriteLine("f({0}) = ({1},{2},{3})",i,function.ValueAt(i).X,function.ValueAt(i).Y, function.ValueAt(i).Z); /// } /// /// } /// } /// } /// </code> /// </example> /// <summary><para>Initializes a function that defined by expression.</para></summary> public Parameter3DFunction(string expressionX,string expressionY,string expressionZ) { if (expressionX == null) throw new ArgumentNullException("expressionX"); if (expressionY == null) throw new ArgumentNullException("expressionY"); if (expressionZ == null) throw new ArgumentNullException("expressionZ"); _expressionTreeX = new ExpressionTree(expressionX); _expressionTreeY = new ExpressionTree(expressionY); _expressionTreeZ = new ExpressionTree(expressionZ); if (_expressionTreeX.Variables.Length > 1) throw new ArgumentException("expressionX"); if (_expressionTreeY.Variables.Length > 1) throw new ArgumentException("expressionY"); if (_expressionTreeZ.Variables.Length > 1) throw new ArgumentException("expressionZ"); _expressionX = expressionX; _expressionY = expressionY; _expressionZ = expressionZ; _expression = string.Format("{0};{1};{2}", _expressionX,_expressionY,_expressionZ); _functionX = FunctionFactory.CreateRealFunction(_expressionTreeX); _functionY = FunctionFactory.CreateRealFunction(_expressionTreeY); _functionZ = FunctionFactory.CreateRealFunction(_expressionTreeZ); _function = DelegateFactory.CreateParameter3DFunctionDelegate( _functionX.ValueAt,_functionY.ValueAt,_functionZ.ValueAt); base._delegate = _function; base._definitionType = DefinitionType.Analytic; }