SMath 1.2.0

dotnet add package SMath --version 1.2.0
                    
NuGet\Install-Package SMath -Version 1.2.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SMath" Version="1.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SMath" Version="1.2.0" />
                    
Directory.Packages.props
<PackageReference Include="SMath" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add SMath --version 1.2.0
                    
#r "nuget: SMath, 1.2.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package SMath@1.2.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=SMath&version=1.2.0
                    
Install as a Cake Addin
#tool nuget:?package=SMath&version=1.2.0
                    
Install as a Cake Tool

<p align="center"> <img src="src/code/SMath/icon.png" alt="SMath" width="50"/> </p>

SMath

GitHub repo size GitHub code size Nuget
Build Code Analysis Code Lint

Geometry and statistics for .NET, written against generic math. Every formula is generic over the numeric type, so the same call site works for double, float, decimal or Half without overloads or casting.

Design

Generic over the number type. Constraints express the mathematics rather than a concrete type: a formula needing a square root asks for IRootFunctions<N>, one needing only addition asks for INumberBase<N>. Passing a type that cannot support the operation is a compile error.

Static and allocation free. There are no wrapper structs for points or vectors. Coordinates are plain tuples such as (N X, N Y), so values stay on the stack and interoperate with any other library. All entry points are static.

Names that read as the formula. Types are nested by the quantity being computed and the input it comes from, giving call sites like Circle.Region.Area.FromRadius(r) or Line.Segment.Length.FromTwoPoints(a, b).

Span overloads on the hot paths. Statistics and grid traversal accept ReadOnlySpan<T> next to IEnumerable<T>, avoiding enumerator allocation and interface dispatch per element. Coordinate generators additionally offer buffer filling overloads, so a caller can size a buffer with the matching count helper and reuse or stackalloc it.

Contents

Area Types
Geometry 2D Point2, Line (ray, segment, projection, intersection), Circle (arc, chord, sector, segment, tangents), Ellipse, Rectangle, GeometricVector2 (polar/cartesian, normals, reflection, dot and cross product), Function1Geometry (tangent and normal lines)
Geometry 3D Point3, Sphere
Statistics ArithmeticMean, Variance, StandardDeviation, Covariance, PearsonCorrelation (cross and auto correlation), Histogram
General Summation, Product, Determinant, PythagorasTheorem, single variable functions (Sine, Cosine, Tangent, Cotangent, Power2, Power3, Identity)

Distance metrics available on Point2 and Point3: Euclidean, Manhattan, Chebyshev and Minkowski.

Setup

<PackageReference Include="SMath" Version="X.X.X" />

Replace X.X.X with the current version from NuGet. The package targets net7.0 and runs on any newer runtime.

Usage

Geometry, with the numeric type inferred from the arguments:

using SMath.Geometry2D;

// double and float from the same generic method
var tangentD = Circle.TangentLine.FromAngle(radius: 5d, angle: double.Pi / 4d);
var tangentF = Circle.TangentLine.FromAngle(radius: 5f, angle: float.Pi / 4f);

// tangent points from an external point, then the secant line through them
var points = Circle.TangentPoint.FromPoint(radius: 2d, (4, 4));
var secant = Line.FromTwoPoints(points.Value.Point1, points.Value.Point2);

var area = Circle.Region.Area.FromRadius(2d);

Statistics, over a sequence or a span:

using SMath.Statistics;

double[] values = [1, 2, 3, 4, 5];

var mean = ArithmeticMean.Eval(values);
var variance = Variance.Sample.Eval(values);
var deviation = StandardDeviation.Sample.Eval(values);

double[] other = [2, 1, 4, 3, 5];
var correlation = PearsonCorrelation.Eval<double>(values, other);

Grid traversal without allocating, using a count helper to size the buffer:

using SMath.Geometry2D;

var center = (X: 0, Y: 0);
const int radius = 3;

Span<(int X, int Y)> buffer = stackalloc (int X, int Y)[Point2.ManhattanDiskCount(radius)];
var count = Point2.CoordinatesUpToManhattanDistance(center, radius, buffer);

foreach (var coordinate in buffer[..count])
{
    // visit each coordinate within the taxicab disk
}

Cross correlation over a range of lags, writing into a caller owned buffer:

using SMath.Statistics;

int[] lags = [-2, -1, 0, 1, 2];
var coefficients = new double[lags.Length];

PearsonCorrelation.Cross.Eval<double, int>(values, other, lags, coefficients);

Contributing

Ideas, bug reports and pull requests are welcome. Open an issue to propose a change, or send a pull request directly.

License

Project is under MIT license.

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net7.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on SMath:

Package Downloads
SPhysics

Physics library based on .NET generic math.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.2.0 53 8/5/2026
1.1.0 44 8/4/2026
1.0.0 119 8/3/2026
0.9.0 1,188 4/3/2025
0.8.0 510 3/3/2025
0.7.0 442 9/26/2023
0.6.0 327 8/22/2023
0.5.0 354 6/5/2023
0.4.0 362 6/4/2023
0.3.0 362 6/2/2023
0.2.0 358 6/1/2023
0.1.0 345 5/31/2023