Skip to content

HamletTanyavong/Mathematics.NET

Repository files navigation

Mathematics.NET Logo

Mathematics.NET

Mathematics.NET is a C# class library that provides tools for solving advanced mathematical problems.

GitHub GitHub Repo Stars NuGet Package NuGet Downloads Unit Tests Discord

About

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.

Features

The examples below highlight some of the features of Mathematics.NET.

Rational Numbers

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();

Automatic Differentiation

Support for automatic differentiation (autodiff) is provided by gradient tapes, Hessian tapes, dual numbers, and hyper-dual numbers.

First-Order, Reverse-Mode AutoDiff

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 $x=1.23$, $y=0.66$, and $z=2.34$, write

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

$$ f(x,y,z) = \frac{\cos x}{(x+y)\sin z} $$

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.2382974299363868

Second-Order, Reverse-Mode AutoDiff

We 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 $x=1.23$, $y=0.66$, and $z=2.34$. Now suppose we want to find the Laplacian of the function

$$ f(r,\theta,\phi) = \frac{\cos r}{(r+\theta)\sin\phi}. $$

We can use the formula

$$ \begin{align*} \nabla^2f &= \frac{1}{r^2}\frac{\partial}{\partial r}\left(r^2\frac{\partial f}{\partial r}\right)+\frac{1}{r^2\sin\theta}\frac{\partial}{\partial\theta}\left(\sin\theta\frac{\partial f}{\partial\theta}\right)+\frac{1}{r^2\sin^2\theta}\frac{\partial^2f}{\partial\phi^2} \\ &=\frac{2}{r}\frac{\partial f}{\partial r}+\frac{\partial^2f}{\partial r^2}+\frac{1}{r^2\sin\theta}\left(\cos\theta\frac{\partial f}{\partial\theta}+\sin\theta\frac{\partial^2f}{\partial\theta^2}\right)+\frac{1}{r^2\sin^2\theta}\frac{\partial^2f}{\partial\phi^2} \end{align*} $$

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.80966092022821

Samples

For more examples of how to use this library, please check out the samples folder.

Contributing

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.

License

Mathematics.NET falls under the MIT license.

Footnotes

  1. Please visit the documentation site for detailed information.

Sponsor this project

  •  

Packages

No packages published

Contributors 2

  •  
  •