项目作者: 65001

项目描述 :
Math Library
高级语言: C#
项目地址: git://github.com/65001/AbMath.git
创建时间: 2018-03-31T00:44:57Z
项目社区:https://github.com/65001/AbMath

开源协议:MIT License

下载


Build Status

AbMath

AbMath is a library that lets you compute an arbitrary mathematical expression and is designed to be
efficient enough to be used in graphing calculators.

It implements algorithims constructs such as Reverse Polish Notation, the shunting yard alogrithim, and apportionment.

Usage

Using AbMath is as simple as adding it as a library and importing it into your codebase!
Computing any math can be done in around four lines of code.

  1. RPN test = new RPN("3 + 2 + 7");
  2. test.Compute();
  3. PostFix math = new PostFix(test)
  4. math.Compute(); //numeric result is returned here

For runtime user defined variables:

  1. RPN test = new RPN("3 + 2 + x");
  2. test.Compute();
  3. PostFix math = new PostFix(test);
  4. //Set all the unknown variables
  5. if (test.ContainsVariables) {
  6. for (int i = 0; i < test.Data.Variables.Count; i++)
  7. {
  8. //...
  9. //get a numerical constant from the user
  10. math.SetVariable(test.Data.Variables[i], a double value);
  11. }
  12. }
  13. math.Compute(); //numeric result is returned here

For generating a table for any given function with the dependent variable being x in the example:

  1. //Code adapted from https://github.com/65001/RPN/blob/master/RPN/CLI.cs#L288
  2. double start = 0; //Start point of our table or graph
  3. double end = 10; //End point of our table or graph
  4. double freq = 1; //How often we should sample the function
  5. double DeltaX = end - start;
  6. double n = DeltaX / freq; //The number of itterations we need given the above information
  7. int max = (int)Math.Ceiling(n);
  8. RPN test = new RPN("x^2 + 3");
  9. test.Compute();
  10. PostFix math = new PostFix(test)
  11. for (int i = 0; i <= max; i++) {
  12. double RealX = start + i * DeltaX / n; //We do this to minimize floating point drift.
  13. math.SetVariable("x", RealX);
  14. double answer = math.Compute();
  15. math.Reset(); //Unsets all variables
  16. }

Apportionment

Apportionment is a way to distribute a fixed integer amount of resources based on population or other data.
AbMath implements the Hamilton, Hunnington-Hill, Jefferson, and Webster methods of apportionment.

Reverse Polish Notation / Shunting Yard

Reverse Polish Notation and the Shunting Yard Algorithim are ways to take user mathematical input and return an answer.
AbMath’s implemetation of our Tokenizer and Shunting Yard Alogrithim currentley support the following:

  • Unary negative and positive operands
  • Primitive composite function support
  • Operators such as +,-,*,/,^,and % (modulo).
  • Comparison operators such as >,<,=,and !=.
  • Logic operators such as || (or) and “&&” (and).
  • All the basic trig functions, max,min, sqrt, round, ln, and log functions.
  • Implicit multiplication support
  • Variadic function support (functions with a variable number of arguments)
  • All of this alongside with the ability to easily add your own operators and functions at runtime!

Contributors

Thanks to Josh Hayward for his assistance in fixing the Arity system which is the basis of
variadic functions.