feat: add MathUtility.Sigmoid (#60)

This commit is contained in:
Oliver Booth 2023-04-03 16:38:58 +01:00
parent 654d5b5b08
commit c7370c39fd
No known key found for this signature in database
GPG Key ID: 20BEB9DC87961025
3 changed files with 121 additions and 4 deletions

View File

@ -15,13 +15,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- X10D: Added `IntrinsicExtensions` and `IntrinsicUtility` which offer methods for vectors in `System.Runtime.Instrinsics`. (#70)
- X10D: Added `MathUtility.Bias(float, float)` and `MathUtility.Bias(double, double)`.
- X10D: Added `MathUtility.ExponentialDecay(float, float, float)` and `MathUtility.ExponentialDecay(double, double, double)`.
- X10D: Added `MathUtility.InverseLerp(float, float, float)` and `MathUtility.InverseLerp(double, double, double)`
- X10D: Added `MathUtility.InverseLerp(float, float, float)` and `MathUtility.InverseLerp(double, double, double)`.
- X10D: Added `MathUtility.ScaleRange(float, float, float, float, float)`
and `MathUtility.ScaleRange(double, double, double, double, double)`
- X10D:
- X10D: Added `MathUtility.Sigmoid(float)` and `MathUtility.Sigmoid(double)`.
- X10D: Added `MathUtility.SmoothStep(float, float, float)` and `MathUtility.SmoothStep(double, double, double)`.
Added `Circle`, `CircleF`, `Cuboid`, `Ellipse`, `EllipseF`, `Line3D`, `Line`, `LineF`, `Polygon`, `PolygonF`, `Polyhedron`,
and `Sphere`, to complement System.Drawing structs such as `Point` and `Rectangle`
- X10D: Added `Circle`, `CircleF`, `Cuboid`, `Ellipse`, `EllipseF`, `Line3D`, `Line`, `LineF`, `Polygon`, `PolygonF`, `Polyhedron`,
and `Sphere`, to complement System.Drawing structs such as `Point` and `Rectangle`.
- X10D: Added `Color.Deconstruct()` - with optional alpha parameter.
- X10D: Added `Color.GetClosestConsoleColor()`.
- X10D: Added `DateTime.GetIso8601WeekOfYear()` and `DateTimeOffset.GetIso8601WeekOfYear()`.

View File

@ -177,6 +177,95 @@ public class MathUtilityTests
Assert.AreEqual(7.5f, result);
}
[TestMethod]
public void Sigmoid_ReturnsExpectedValue_UsingDouble()
{
const double input = 0.5f;
const double expected = 0.622459331f;
double actual = MathUtility.Sigmoid(input);
Assert.AreEqual(expected, actual, 1e-6);
}
[TestMethod]
public void Sigmoid_ReturnsExpectedValue_UsingSingle()
{
const float input = 0.5f;
const float expected = 0.622459331f;
float actual = MathUtility.Sigmoid(input);
Assert.AreEqual(expected, actual, 1e-6f);
}
[TestMethod]
public void Sigmoid_ReturnsZeroWhenInputIsNegativeInfinity_UsingDouble()
{
const double input = double.NegativeInfinity;
const double expected = 0f;
double actual = MathUtility.Sigmoid(input);
Assert.AreEqual(expected, actual, 1e-6);
}
[TestMethod]
public void Sigmoid_ReturnsZeroWhenInputIsNegativeInfinity_UsingSingle()
{
const float input = float.NegativeInfinity;
const float expected = 0f;
float actual = MathUtility.Sigmoid(input);
Assert.AreEqual(expected, actual, 1e-6f);
}
[TestMethod]
public void Sigmoid_ReturnsOneWhenInputIsPositiveInfinity_UsingDouble()
{
const double input = double.PositiveInfinity;
const double expected = 1f;
double actual = MathUtility.Sigmoid(input);
Assert.AreEqual(expected, actual, 1e-6);
}
[TestMethod]
public void Sigmoid_ReturnsOneWhenInputIsPositiveInfinity_UsingSingle()
{
const float input = float.PositiveInfinity;
const float expected = 1f;
float actual = MathUtility.Sigmoid(input);
Assert.AreEqual(expected, actual, 1e-6f);
}
[TestMethod]
public void Sigmoid_ReturnsZeroPointFiveWhenInputIsZero_UsingDouble()
{
const double input = 0f;
const double expected = 0.5f;
double actual = MathUtility.Sigmoid(input);
Assert.AreEqual(expected, actual, 1e-6);
}
[TestMethod]
public void Sigmoid_ReturnsZeroPointFiveWhenInputIsZero_UsingSingle()
{
const float input = 0f;
const float expected = 0.5f;
float actual = MathUtility.Sigmoid(input);
Assert.AreEqual(expected, actual, 1e-6f);
}
[TestMethod]
public void SmoothStep_ShouldReturnHigher_GivenAlpha1()
{

View File

@ -333,6 +333,34 @@ public static class MathUtility
return (alpha * newRange) + newMin;
}
/// <summary>
/// Calculates the sigmoid function for the given input value.
/// </summary>
/// <param name="value">The input value for which to calculate the sigmoid function.</param>
/// <returns>The result of applying the sigmoid function to the input value.</returns>
/// <remarks>
/// The sigmoid function is a commonly used activation function in artificial neural networks and logistic regression. It
/// maps any real-valued number to a value between 0 and 1.
/// </remarks>
public static float Sigmoid(float value)
{
return 1.0f / (1.0f + MathF.Exp(-value));
}
/// <summary>
/// Calculates the sigmoid function for the given input value.
/// </summary>
/// <param name="value">The input value for which to calculate the sigmoid function.</param>
/// <returns>The result of applying the sigmoid function to the input value.</returns>
/// <remarks>
/// The sigmoid function is a commonly used activation function in artificial neural networks and logistic regression. It
/// maps any real-valued number to a value between 0 and 1.
/// </remarks>
public static double Sigmoid(double value)
{
return 1.0f / (1.0f + System.Math.Exp(-value));
}
/// <summary>
/// Performs smooth Hermite interpolation from one value to a target using a specified alpha.
/// </summary>