Code Monkey home page Code Monkey logo

sys27 / xfunc Goto Github PK

View Code? Open in Web Editor NEW
54.0 8.0 6.0 6.92 MB

xFunc provides a powerful parser and analyzer for mathematical expressions. It excels at calculating derivatives, simplifying expressions, and achieving high performance.

Home Page: https://sys27.github.io/xFunc/

License: MIT License

C# 100.00%
lexer parse c-sharp math mathematics logic analyzer derivative differentiation complex-numbers parser simplification expression dotnet units units-of-measure math-parser evaluate-expressions

xfunc's Introduction

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

xfunc's People

Contributors

dependabot[bot] avatar sys27 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

xfunc's Issues

Issue with Differential(IMathExpression..)

Hello,

i found an Issue in the Differential-Method.
The Function don't uses the current AngleMeasurement-Value of the MathParser.
Instead it creates a new MathExpression with the AngleMeasurement.Degree - Value.

In my Project, i only calculate with Radians and I solved the Issue by replacing the AngleMeasurement to Radians in the Default MathParser-Constructor. But this is only a solution for me, its not clean and should be fixed.

bye

Original: https://xfunc.codeplex.com/workitem/10500

Add matrices.

Syntax:
{1, 2, 3, 4, 5, 6}
or
{ {1, 1}, {2, 2}, {3, 3} }
or
{1+1, 2^6, sqrt(3), ln(21)}

Differentation bug.

Input: deriv(((x + 3)^2) / 16, x)

Output (wrong): 8 * (x + 3)

Output (right): (x + 3) / 8

Numbers with dynamic size.

Create a separate project that allows use large numbers (integer and real). Implement +, -, *, /, % and math functions, casting from base types.

Bug in the Equals method.

var sine = new Sin(new Number(2));
var ln = new Ln(new Number(2));

sine.Equals(ln) == true

And

var add = new Add(new Number(2), new Number(3));
var sub = new Sub(new Number(2), new Number(3));

add.Equals(sub) == true

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.