Home

Awesome

MathSharp

Note: As of 2022 MathSharp is not recommended for general use

Since its inception, the .NET intrinsics and vectors have increased in performance and functionality massively, and they are recommended for any new projects

ko-fi Buy Me A Coffee

MIT license GitHub issues

ConfigurationWindows x86Windows x64Ubuntu 1604 x64Mac OS x64
DebugBuild StatusBuild StatusBuild StatusBuild Status
ReleaseBuild StatusBuild StatusBuild StatusBuild Status

MathSharp is a vector and matrix library written in C# using hardware intrinsics. Thanks to hardware acceleration, MathSharp is significantly faster than most mathematics libraries out there, but only supports .NET Core 3.0 and up.

MathSharp beats out all alternative for speed. Comparing it to the System.Numerics types and the OpenTk.Math types shows just how fast it is:

[All benchmarks were taken using BenchmarkDotNet on an i3-8350k at stock speeds with minimal background activity, with .NET Core 3.0.100]

Matrix Equality

Operation

Matrix4x4 == Matrix4x4

Results

<!-- ![Matrix Equality Benchmark](assets/Benchmarks/MatrixEqualityBenchmark-barplot.png =250x250)--> <img src="assets/Benchmarks/MatrixEqualityBenchmark-barplot.png" width="500" height="500">
MethodMeanErrorStdDev
OpenTk10.984 ns0.1087 ns0.1017 ns
SystemNumerics3.895 ns0.0185 ns0.0164 ns
MathSharp1.622 ns0.0109 ns0.0102 ns

Matrix Transposition

Operation

Transpose(Matrix4x4)

Results

<!--![Matrix Transpose Benchmark](assets/Benchmarks/MatrixTransposeBenchmark-barplot.png)--> <img src="assets/Benchmarks/MatrixTransposeBenchmark-barplot.png" width="500" height="500">
MethodMeanErrorStdDev
OpenTk10.984 ns0.1087 ns0.1017 ns
SystemNumerics3.895 ns0.0185 ns0.0164 ns
MathSharp1.622 ns0.0109 ns0.0102 ns

Sine Wave Generation

Operation

const int SampleRate = 44100;
const float Tau = MathF.PI * 2;
const float Frequency = 440;

for (var i = 0; i < waveInputs.Length; i++)
{
    waveInputs[i] = Sin(Tau * Frequency * ((float)i / SampleRate));
}

Results

<!--![Sine Wave Benchmark](assets/Benchmarks/SineWaveBenchmark-barplot.png)--> <img src="assets/Benchmarks/SineWaveBenchmark-barplot.png" width="500" height="500">
MethodMeanErrorStdDev
SystemMathF412.30 us1.6319 us1.5265 us
MathSharp80.37 us0.6465 us0.6048 us

Anchored Scale Operation

Operation

const Vector2 Translation = new Vector2(1.7f, 2.3f);
const Vector2 Anchor = new Vector2(1.0f, 0.0f);
const Vector2 Scale = new Vector2(7.0f, 3.6f);
const Vector2 Amount = new Vector2(0.5f, 0.25f);

Vector2 newScale = Scale * Amount;
Vector2 deltaT = Scale * (1 - Amount);
deltaT *= Anchor;
(Translation + deltaT) * newScale;

Results

<!--![Anchored Scale Benchmark](assets/Benchmarks/AnchoredScaleBenchmark-barplot.png)--> <img src="assets/Benchmarks/AnchoredScaleBenchmark-barplot.png" width="500" height="500">
MethodMeanErrorStdDevRank
MathSharp0.8542 ns0.0084 ns0.0079 ns1
SystemNumerics2.0281 ns0.0123 ns0.0115 ns2
OpenTk37.4250 ns0.1585 ns0.1483 ns3

Vector addition

Operation

Vector4 + Vector4;

Results (within margin of error between MathSharp and System.Numerics)

<!--![Vector Addition Benchmark](assets/Benchmarks/VectorAdditionBenchmark-barplot.png)--> <img src="assets/Benchmarks/VectorAdditionBenchmark-barplot.png" width="500" height="500">
MethodMeanErrorStdDev
OpenTk6.5341 ns0.0392 ns0.0367 ns
SystemNumerics0.0510 ns0.0080 ns0.0075 ns
MathSharp0.0426 ns0.0043 ns0.0040 ns

Authors

MathSharp is a library written, managed, and maintained by John Kelly (@john-h-k)

How to use

TODO

Contributing

MathSharp uses and encourages Early Pull Requests. Please don't wait until you're done to open a PR!

  1. Install Git and the .Net Core SDK
  2. Fork MathSharp
  3. Create a branch on your fork.
  4. Add an empty commit to start your work off (and let you open a PR): git commit --allow-empty -m "start of [thing you're working on]"
  5. Open a Pull request with [WIP] in the title. Do this before you actually start working.
  6. Make your commits in small, incremental steps with clear descriptions.
  7. Tag a maintainer when you're done and ask for a review!

Click here for good first issues.

Click here for everything we need help with.

License & Copyright

MathSharp is licensed under the MIT license. Full copyright belongs to @john-h-k.