A random assembly of static methods for matrix and vector operations, system of ODE solver, Newton Raphson, and minor calculus methods.
$ dotnet add package StapletonMathPackageStapletonMathPackage is a C# numerical methods library providing tools for linear algebra, optimization, root-finding, interpolation, and ODE solving. It is pretty old, and I mostly started it as a wrapper around other math packages because I wanted to stick with double arrays in my program rather than specialized matrix classes. This version wraps around Math.Numerics for many of the linear algebra operations.
Licensed under the MIT License.
MatrixMath, VectorMath)ThreadLocal<Random>Install via NuGet:
dotnet add package StapletonMathPackage
Or in Visual Studio:
PM> Install-Package StapletonMathPackage
using RandomMath;
double[,] A = { { 1, 2 }, { 3, 4 } };
double[,] B = { { 5, 6 }, { 7, 8 } };
double[,] C = MatrixMath.Multiply(A, B);
double detA = MatrixMath.Determinant(A);
using RandomMath.CubicSplineFit;
var xs = new double[] { 0, 1, 2, 3 };
var ys = new double[] { 0, 1, 0, 1 };
var spline = new CubicSplineFit(xs, ys);
double dy = 0, d2y = 0;
double yInterp = spline.Interpolate(1.5, ref dy, ref d2y);
using RandomMath.GeneticAlgorithm;
public class SphereFunction : IOptimize {
public int nX => 3;
public double Eval(double[] X) => X.Sum(x => x*x);
public IOptimize DeepCopy() => new SphereFunction();
}
var ga = new GeneticAlgorithm(new SphereFunction());
ga.Run();
Console.WriteLine($"Best fitness: {ga.Best.Fitness}");
using RandomMath.NewtonRaphson;
public class SimpleFunc : IMatrixFunctionAndDerivative {
public double[] Eval(double[] X) => new[] { X[0]*X[0] - 2 };
public double[,] DEval(double[] X) => new double[,] { { 2*X[0] } };
}
var solver = new NewtonRaphsonJacobian(
new double[] { 0 }, // target y
new double[] { 1.0 }, // initial guess
new SimpleFunc(),
1e-10, 50
);
solver.Solve();
Console.WriteLine($"Root ≈ {solver.X[0]}");
using RandomMath.ODESystemSolver;
double[,] A = { { 0, 1 }, { -1, 0 } };
var mexp = new MatrixExponential(A, 1.0);
var expAx = mexp.Solve(1.0); // exp(A*1.0)
RandomMath.* namespaces.Contributions are welcome!
Please open an issue or submit a PR with improvements, bug fixes, or new numerical routines.
Guidelines:
This project is licensed under the MIT License — see the LICENSE file for details.