-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat: add method TanhFunction #563
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+160
−0
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b20c713
Add algorithm TanhFunction
khanhkhanhlele 1b7f7eb
Update Algorithms/Numeric/Tanh.cs
khanhkhanhlele 5fb4043
update
khanhkhanhlele ff811e0
add array test
khanhkhanhlele 22f2c4c
fix code stype
khanhkhanhlele 79711d3
Merge branch 'master' into TanhFunction
siriak 1e43aaa
Merge branch 'master' into TanhFunction
siriak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| using Algorithms.Numeric; | ||
| using NUnit.Framework; | ||
| using System; | ||
|
|
||
| namespace Algorithms.Tests.Numeric; | ||
|
|
||
| [TestFixture] | ||
siriak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public static class TanhTests | ||
| { | ||
| // Tolerance for floating-point comparisons | ||
| private const double Tolerance = 1e-9; | ||
|
|
||
| // --- SCALAR TESTS (Tanh.Compute(double)) --- | ||
|
|
||
| /// <summary> | ||
| /// Tests Tanh function for specific values, including zero and symmetric positive/negative inputs. | ||
| /// </summary> | ||
| [TestCase(0.0, 0.0)] | ||
| [TestCase(1.0, 0.7615941559557649)] | ||
| [TestCase(-1.0, -0.7615941559557649)] | ||
| [TestCase(5.0, 0.999909204262595)] | ||
| [TestCase(-5.0, -0.999909204262595)] | ||
| public static void TanhFunction_Scalar_ReturnsCorrectValue(double input, double expected) | ||
| { | ||
| var result = Tanh.Compute(input); | ||
| Assert.That(result, Is.EqualTo(expected).Within(Tolerance)); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Ensures the Tanh output approaches 1.0 for positive infinity and -1.0 for negative infinity. | ||
| /// </summary> | ||
| [Test] | ||
| public static void TanhFunction_Scalar_ApproachesLimits() | ||
| { | ||
| Assert.That(Tanh.Compute(double.PositiveInfinity), Is.EqualTo(1.0).Within(Tolerance)); | ||
| Assert.That(Tanh.Compute(double.NegativeInfinity), Is.EqualTo(-1.0).Within(Tolerance)); | ||
| Assert.That(Tanh.Compute(double.NaN), Is.NaN); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Checks that the Tanh result is always bounded between -1.0 and 1.0. | ||
| /// </summary> | ||
| [TestCase(100.0)] | ||
| [TestCase(-100.0)] | ||
| [TestCase(0.0001)] | ||
| public static void TanhFunction_Scalar_ResultIsBounded(double input) | ||
| { | ||
| var result = Tanh.Compute(input); | ||
| Assert.That(result, Is.GreaterThanOrEqualTo(-1.0)); | ||
| Assert.That(result, Is.LessThanOrEqualTo(1.0)); | ||
| } | ||
|
|
||
| // --- VECTOR TESTS (Tanh.Compute(double[])) --- | ||
|
|
||
| /// <summary> | ||
| /// Tests the element-wise computation for a vector input. | ||
| /// </summary> | ||
| [Test] | ||
| public static void TanhFunction_Vector_ReturnsCorrectValues() | ||
| { | ||
| // Input: [0.0, 1.0, -2.0] | ||
| var input = new[] { 0.0, 1.0, -2.0 }; | ||
| // Expected: [Tanh(0.0), Tanh(1.0), Tanh(-2.0)] | ||
| var expected = new[] { 0.0, 0.7615941559557649, -0.9640275800758169 }; | ||
|
|
||
| var result = Tanh.Compute(input); | ||
|
|
||
| // Assert deep equality within tolerance | ||
| Assert.That(result, Is.EqualTo(expected).Within(Tolerance)); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Tests vector handling of edge cases like infinity and NaN. | ||
| /// </summary> | ||
| [Test] | ||
| public static void TanhFunction_Vector_HandlesLimitsAndNaN() | ||
| { | ||
| var input = new[] { double.PositiveInfinity, 0.0, double.NaN }; | ||
| var expected = new[] { 1.0, 0.0, double.NaN }; | ||
|
|
||
| var result = Tanh.Compute(input); | ||
|
|
||
| Assert.That(result.Length, Is.EqualTo(expected.Length)); | ||
| Assert.That(result[0], Is.EqualTo(expected[0]).Within(Tolerance)); // Pos Inf -> 1.0 | ||
| Assert.That(result[2], Is.NaN); // NaN | ||
| } | ||
|
|
||
| // --- EXCEPTION TESTS --- | ||
|
|
||
| /// <summary> | ||
| /// Checks if the vector computation throws ArgumentNullException for null input. | ||
| /// </summary> | ||
| [Test] | ||
| public static void TanhFunction_Vector_ThrowsOnNullInput() | ||
| { | ||
| double[]? input = null; | ||
| Assert.Throws<ArgumentNullException>(() => Tanh.Compute(input!)); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Checks if the vector computation throws ArgumentException for an empty input array. | ||
| /// </summary> | ||
| [Test] | ||
| public static void TanhFunction_Vector_ThrowsOnEmptyInput() | ||
| { | ||
| var input = Array.Empty<double>(); | ||
| Assert.Throws<ArgumentException>(() => Tanh.Compute(input)); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| namespace Algorithms.Numeric; | ||
|
|
||
| /// <summary> | ||
| /// Implementation of the Hyperbolic Tangent (Tanh) function. | ||
| /// Tanh is an activation function that takes a real number as input and squashes | ||
| /// the output to a range between -1 and 1. | ||
| /// It is defined as: tanh(x) = (exp(x) - exp(-x)) / (exp(x) + exp(-x)). | ||
| /// https://en.wikipedia.org/wiki/Hyperbolic_function#Hyperbolic_tangent. | ||
| /// </summary> | ||
| public static class Tanh | ||
| { | ||
| /// <summary> | ||
| /// Compute the Hyperbolic Tangent (Tanh) function for a single value. | ||
| /// The Math.Tanh() method is used for efficient and accurate computation. | ||
| /// </summary> | ||
| /// <param name="input">The input real number.</param> | ||
| /// <returns>The output real number in the range [-1, 1].</returns> | ||
| public static double Compute(double input) | ||
| { | ||
| // For a single double, we can directly use the optimized Math.Tanh method. | ||
| return Math.Tanh(input); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Compute the Hyperbolic Tangent (Tanh) function element-wise for a vector. | ||
| /// </summary> | ||
| /// <param name="input">The input vector of real numbers.</param> | ||
| /// <returns>The output vector of real numbers, where each element is in the range [-1, 1].</returns> | ||
| public static double[] Compute(double[] input) | ||
| { | ||
khanhkhanhlele marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (input is null) | ||
| { | ||
| throw new ArgumentNullException(nameof(input)); | ||
| } | ||
siriak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
siriak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (input.Length == 0) | ||
| { | ||
| throw new ArgumentException("Array is empty."); | ||
| } | ||
|
|
||
| var outputVector = new double[input.Length]; | ||
|
|
||
| for (var index = 0; index < input.Length; index++) | ||
| { | ||
| // Apply Tanh to each element using the optimized Math.Tanh method. | ||
| outputVector[index] = Math.Tanh(input[index]); | ||
| } | ||
|
|
||
| return outputVector; | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.