-
Notifications
You must be signed in to change notification settings - Fork 0
Home
TMath is an open source C# Math library that has function implementations for any number or custom type that implements INumber<T>
. It was originally made with the intent of easily being able to create custom types and have access to very commonly used functions, besides being able to use most, if not all, built in types on them.
The inspiration from the project came from seeing the generic math additions to C# 7, and wanting to do something with it.
TMath is licensed under the MIT License and targets .NET 8.
TMath has a couple of classes containing functions in specific areas, such as TStatistics, TGeneration, TEasings, etc.
For example, say we want to get info such as the mean and standard deviation for a dataset, we can do it in 2 ways:
short[] data = {1, 3, 9, 5, 9, 2, 1, 10}; // Using short as an example, but can be any type
short mean = TStatistics.Mean(data); // Because we used short, the mean will also be returned in a short, so it will be a bit innacurate.
short stddev = TStatistics.StandardDeviation(data); // Same as above
Console.WriteLine($"Mean: {mean}");
Console.WriteLine($"Standard Deviation: {stddev}");
// OUTPUT:
// Mean: 5
// Standard Deviation: 3
// OR you can use DescriptiveStatistics type
var data2 = TGeneration.Default.RandomArray(10, 0m, 10);
DescriptiveStatistics<decimal> stats = new(data);
Console.WriteLine(stats);
// OUTPUT:
// Data: 7,2624326996796,8,17325359590969,7,68022689394663,5,58161191436537,2,06033154021033,5,58884794618415,9,06027066011926,4,42177873310716,9,7754975314138,2,7370445768987
// Mean: 6,234129609183469
// Median: 6,425640322931875
// Variance: 6,8015715497188031362875683539
// Standard Deviation: 2,60798227557604
// Mode: 7,2624326996796
Or maybe we need to calculate the following:
Func<long, long> fac = x => TFunctions.Factorial<long>(x)
var sum = TFunctions.Sum(fac, 10);
Console.WriteLine(sum); // OUTPUT: 4037913