dotnet add package xFunc.Maths
var processor = new Processor();
var exp = processor.Parse("x ^ 2");
Note: You can use Parser
instead of Processor
.
The exp
variable will contain the expression tree for x ^ 2
and will be equal to:
var exp = new Pow(
Variable.X,
new Number(2)
);
Let's assume you have exp
from the previous step. Now you can evaluate it but because the expression has the x
variable you need to provide a value for it.
var parameters = new ExpressionParameters
{
new Parameter("x", 10.0),
};
var result = exp.Execute(parameters);
The result
will be equal to 100.0. All expressions have another overload of the Execute
method, which doesn't accept any parameters, it is useful when your expression doesn't require any parameter, for example, 2 + 2
. But be careful with this method because it could throw the NotSupportedException
exception if the expression has any parameter or user-defined function.
If you don't need the expression tree at all, you can use Processor.Solve
method. It will parse the string expression into the tree and immediately evaluate it for you:
var processor = new Processor();
var result = processor.Solve("10 ^ 2");
The xFunc library has the implementation of Visitor Pattern. It is called IAnalyzer<TResult>
. You can implement your own analyzers but xFunc provides several built-in analyzers: Simplifier
, Differentiator
, TypeAnalyzer
. I'm going to demonstrate analyzer based on Simplifier
.
var simplifier = new Simplifier();
var exp = new Ln(new Variable("e")); // ln(e)
var result = exp.Analyze(simplifier);
The Simplifier
allows you to simplify the expression, in the code shown above, the ln(e)
expression will be simplified just to 1
.
xFunc has a CLI tool, to install it use the following command:
dotnet tool install -g xFunc.Cli
after that, you will be able to use the xfunc
command in your terminal. It supports the following commands:
This command parses the expression and outputs the string representation of it. Right now, its main purpose is testing (to check the expression was parsed correctly).
This command evaluates the expression. For example:
xfunc solve "sin(90 'deg')"
This command launches the CLI tool in the interactive mode where you can enter each expression one by one, like:
> sin(90 'deg')
1
> cos(90 'deg')
0
To exit use Cmd+C or Cmd+D on Mac OS.
Also, you are able to save the current progress to a file (by using #save <path>
command) and run it later with xfunc run
.
Let's assume you saved your progress to the test.xf
file with the following content:
sin(90 'deg')
cos(90 'deg')
So, you can execute it by:
xfunc run test.xf