Mathematics.NET is a C# class library that provides tools for solving advanced mathematical problems.
Mathematics.NET provides custom types for complex, real, and rational numbers as well as other mathematical objects such as vectors, matrices, and tensors.1 Mathematics.NET also supports first and second-order, forward and reverse-mode automatic differentiation.
The examples below highlight some of the features of Mathematics.NET.
Rational numbers can be created using
Rational<int> number = new(4, 6);Besides the basic arithmetic operations, operations on rational numbers include, but are not limited to, the following:
var reduced = number.Reduce();
var reciprocal = number.Reciprocate();Support for automatic differentiation (autodiff) is provided by gradient tapes, Hessian tapes, dual numbers, and hyper-dual numbers.
Reverse-mode automatic differentiation can be performed using gradient tapes. To create a gradient tape, use
GradientTape<Real> tape = new();Variables can be created using the .CreateVariable() method. To work with functions of three variables with intial values
var x = tape.CreateVariable(1.23);
var y = tape.CreateVariable(0.66);
var z = tape.CreateVariable(2.34);Now suppose we want to find the gradient of the function
at the specified points. We write
var result = tape.Divide(
tape.Cos(x),
tape.Multiply(
tape.Add(x, y),
tape.Sin(z)));and use the .ReverseAccumulate() method to compute the gradient.
tape.ReverseAccumulate(out var gradient);
Console.WriteLine("At the points x = 1.23, y = 0.66, and z = 2.34:");
Console.WriteLine($"f = {result.Value}");
Console.WriteLine($"df/dx = {gradient[0]}");
Console.WriteLine($"df/dy = {gradient[1]}");
Console.WriteLine($"df/dz = {gradient[2]}");
// At the points x = 1.23, y = 0.66, and z = 2.34:
// f = 0.24614338791952137
// df/dx = -0.8243135949243512
// df/dy = -0.13023459678281554
// df/dz = 0.2382974299363868We can use Hessian tapes to perform second-order, reverse-mode automatic differentiation. To create a Hessian tape, use
HessianTape<Real> tape = new();Then, we can use the .CreateAutoDiffVector() method to create a vector with initial values. Write
var x = tape.CreateAutoDiffVector(1.23, 0.66, 2.34);to create a vector with the initial values
We can use the formula
and write
_ = tape.Divide(
tape.Cos(x.X1),
tape.Multiply(
tape.Add(x.X1, x.X2),
tape.Sin(x.X3)));Use the .ReverseAccumulate() method to get our gradient and Hessian.
tape.ReverseAccumulate(out var gradient, our var hessian);Finally, use those values to compute our Laplacian.
var u = Real.One / (x.X1.Value * Real.Sin(x.X2.Value)); // 1 / (r * Sin(θ))
var laplacian =
2.0 * gradient[0] / x.X1.Value +
hessian[0, 0] +
u * Real.Cos(x.X2.Value) * gradient[1] / x.X1.Value +
hessian[1, 1] / (x.X1.Value * x.X1.Value) +
u * u * hessian[2, 2];
Console.Writeline(laplacian); // 48.80966092022821For more examples of how to use this library, please check out the samples folder.
Contributions are welcome and appreciated; filing issues is a good way to do so. However, please start a discussion before starting any work as not all implementations or features will be accepted.
Mathematics.NET falls under the MIT license.
Footnotes
-
Please visit the documentation site for detailed information. ↩