Master: Build Status codecov
Dev: Build Status codecov
xFunc.Maths: NuGet Downloads
xFunc.Cli: NuGet

xFunc

xFunc is a user-friendly C# library for constructing and manipulating mathematical and logical expressions. This lightweight library empowers developers to effortlessly parse strings into expression trees, analyze expressions (including derivatives and simplifications), and perform various mathematical operations.

xFunc is a versatile tool suitable for both educators and students, allowing the creation of complex mathematical expressions.

Note: The WPF application (xFunc UI) was migrated to a separate repository xFunc.UI.

Features:

  • Evaluate expressions (see all supported functions and operators):
    • basic arithmetic operators: +, -, *, /, ^, etc.
    • trigonometric functions: sin(x), cos(x), tan(x), etc.
    • inverse trigonometric functions: arcsin(x), arccos(x), arctan(x), etc.
    • hyperbolic functions:sinh(x), cosh(x), tanh(x), etc.
    • inverse hyperbolic functions: arsinh(x), arcosh(x), artanh(x), etc.
    • complex numbers: 1 + 2i, im(x), re(x), etc.
    • vector/matrix: {1, 2, 3}, {{1, 2}, {3, 4}}, etc.
    • statistical: sum(a, b, c), avg(a, b, c), etc.
    • bitwise operators: x or y, x and y, etc.
    • units: 90 'deg' - angles, 10 'm' - length, 10 'min' - time, etc.
    • lambdas: f := (x) => x ^ 2, ((x) => x ^ 2)(3), curry(f), etc.
  • Derivative calculation;
  • Simplify expressions (simplification rules);
  • Supported Framework: .NET 6+;

Usage

The main class of xFunc library is Processor. Detailed documentation is located on GitHub Pages.

Processor

It allows you to:

Parse:

var processor = new Processor();
var exp = processor.Parse("2 + x"); 

// 'exp' will contain the expression tree for later use
// you can calculate it or process it by analyzers (Differentiator, Simplifier, etc.)

// 'exp' has a parameter
// we should provide a value for variable 'x'
var parameters = new ExpressionParameters
{
    { "x", 10 }
};
var result = exp.Execute(parameters);

// result will be equal to 12

Note: The Parse method won't simplify the expression automatically, it will return the complete representation of provided string expression.

Solve:

This method parses string expression (like the Parse method) and then calculates it (returns object which implements the Result abstract class).

var processor = new Processor();
var result = processor.Solve("2 + 2");

Console.WriteLine(result); // 4.0

The result variable will contain 4 (as NumberResult which is the implementation of the Result class). It is a hand-made implementation of the discriminated union. The Result class provides the abstraction (root class) for DU, whereas implementation for each possible return type is dedicated to the appropriate nested result class. Check documentation for more examples: Result, Processor.

If your expression has any parameter, you need to assign a value to it (otherwise xFunc will throw an exception), because Processor has a build-in collection of parameters and user functions, you don't need to use ExpressionParameters directly:

processor.Solve("x := 10");

// or explicitly through Parameters property

processor.Parameters.Variables.Add("x", 10);

Simplify:

var processor = new Processor();

processor.Solve("simplify((x) => arcsin(sin(x)))");
// or
processor.Simplify("arcsin(sin(x))");
// will return simplified expression = "x"

Differentiate:

var processor = new Processor();

processor.Solve("deriv((x) => 2x)");
// or
processor.Differentiate("2x");
// will return "2"

You can specify variable (default is "x") of differentiation:

var processor = new Processor();
processor.Differentiate("2y", Variable.Y); // will return "2"
processor.Differentiate("2x + sin(y)", new Variable("x")); // will return "2"

Performance

Processor

Version Method Mean Allocated
3.7.3 Parse 39,567.9 ns 63736 B
4.3.0 Parse 10,434.04 ns 4848 B
3.7.3 Solve 55,260.0 ns 96920 B
4.3.0 Solve 15,683.42 ns 9552 B

More details

License

xFunc is released under MIT License.

Thanks

Azure Pipelines
Coverlet
ReportGenerator
NUnit
NSubstitute
docfx
BenchmarkDotNet