DotNetMath Library

The DotNetMath library provides a number of mathematical features for use in .NET applications:

  • A full mathematical solver which is capable of solving mathematical functions expressed in string format. For example, a user can enter "2*Sin(Degree(30)*Sqrt(64)" to obtain the answer 8. The mathematical solver has a very large library of mathematical functions, logical functions and operators. Support is also provided for assigning variables, so solving "a=2" followed by "b=a+1" will result in a variable b being defined with value 3.
  • A solver for linear simultaneous equations with any number of unknown variables.
  • Linear and piecewise-linear model representations.
  • A polynomial curve-fitting class which can produce a specified-order polynomial that is the best match for a set of given input data.


The library is available as a NuGet package and can be downloaded by entering DotNetMath in Nuget Package Manager.


Full API documentation for the library is provided here. The remainder of this page also provides documentation on the functionality of mathematical solver library and some examples of its use.

Mathematical Solver Library Functionality

Main Features

  • Solves single-line mathematical expressions that are specified in String format.
  • A "Context" feature is provided to allow the values of variables defined in mathematical expressions to be remembered between expressions. The default context provided allows for simple in-memory storage of variables, however the context class may be overridden to provide any form of backing storage for mathematical variables, for example a relational database.
  • Support for string variables and constants and basic string comparisons (equality and inequality).
  • Using the context feature, the library can be expanded into a full scripting engine for performing complex mathematical manipulations within an application without any need for recompilation of code - just specify the mathematical expressions in a configuration file or database.
  • Provides a large number of mathematical functions:
    • Logical: IF, AND, OR, NOT, XOR
    • Trigonometric: SIN, COS, TAN, SINH, COSH, TANH, ASIN, ACOS, ATAN
    • Rounding: FLOOR, CEILING, ABS, ROUND
    • Logarithmic: LN, LOG, EXP
    • Unit Conversion: RAD, DEG
    • General Purpose: SQRT, SIGN, MAX, MIN, POW
    • Variable deletion: CLEAR
  • Supports a large number of operators:
    • Logical:
      • ==
      • !=
      • >
      • <
      • >=
      • <=
      • ||
      • &&
    • Mathematical:
      • +
      • -
      • *
      • /
      • % (Modulo)
      • ^ (Power)
      • ! (Factorial)
    • Variable assignment:
      • =
      • $ (String variable identifier, e.g. $abcd)
      • # (Numeric variable identifier, e.g. #abcd)

Math Context

The MathContext class provided in the library is an abstract class which provides the notion of the storage of variables. The default implementation of this class, DefaultMathContext provides basic support for variables within mathematical expressions, storing variables in an in-memory hash table. Customised versions of the class can be provided to support more complex backing data stores.

If a MathContext object is not explicitly passed into the solver when it is called, then an instance of the default context will be created and used during the solution of the expression.


An online demonstration of the library is available here.

Code Examples

Example 1 - Simple Mathematical Expression Evaluation

// Writes 8 to console
Console.WriteLine(SymbolicMathSolver.Solve("2^3", new DefaultMathContext()));
// Writes 0.5 to console
Console.WriteLine(SymbolicMathSolver.Solve("Sin(Degree(30))", new DefaultMathContext()));
// Writes 10 to console
Console.WriteLine(SymbolicMathSolver.Solve("Sqrt(100)", new DefaultMathContext()));
// Writes 5 to console because conditional statement is false
Console.WriteLine(SymbolicMathSolver.Solve("If(2>3, 4, 5)", new DefaultMathContext()));
// Writes 7 to console
Console.WriteLine(SymbolicMathSolver.Solve("1+2*3", new DefaultMathContext()));
// Writes 0 to console (logical "False")
Console.WriteLine(SymbolicMathSolver.Solve("6 == 7", new DefaultMathContext()));

Example 2 - Using variables in expressions

DefaultMathContext context = new DefaultMathContext();
context.SetVariable("a", 10);
context.SetVariable("b", 2);
// Results of solutions are ignored here - we are more interested in the values of the variables
// after the solution of both expressions.
SymbolicMathSolver.Solve("c=b+2*a", context);
SymbolicMathSolver.Solve("d= c + 1", context);
// Writes 10 to console
// Writes 2 to console
// Writes 22 to console
// Writes 23 to console

Example 3 - Use of String Variables

DefaultMathContext context = new DefaultMathContext();
.SetStringVariable("a", "hello");
context.SetStringVariable("b", "world");
// Results of solutions are ignored here - we are more interested in the values of the variables
// after the solution of both expressions.

.Solve("$test=\"hello\"", context);
// Illustrates setting a numeric variable based on a string value
.Solve("result1= IF($test==$a, 10, 20)", context);
SymbolicMathSolver.Solve("result2 = IF($test!=$a, 50, 60)", context);
.Solve("result3 = ($b == \"world\")", context);
// Writes 10 to console

// Writes 60 to console
// Writes 1 to console