Repository: mathnet/mathnet-numerics (csharp)

Issue:
Bug in SpecialFunctions.Hypotenuse
Hello, the implementation of Hypotenuse is buggy. The following test fails:

```
[Fact]
void TestHypotenuse()
{
  Assert.True(double.IsNaN(SpecialFunctions.Hypotenuse(double.NaN, 0)));
}
```

This has severe consequences, because e.g. the following test also fails:

```
 [Fact]
 void TestL2Norm()
 {
   var v = Vector<double>.Build.Dense([double.NaN, 0]);
   Assert.True(double.IsNaN(v.L2Norm()));
 }
```

Best regards, Dirk

Pull request description:
Fixes #1071

Patch:
diff --git a/src/Numerics/SpecialFunctions/Stability.cs b/src/Numerics/SpecialFunctions/Stability.cs
index 1bca52240..6692f5c99 100644
--- a/src/Numerics/SpecialFunctions/Stability.cs
+++ b/src/Numerics/SpecialFunctions/Stability.cs
@@ -91,6 +91,11 @@ public static Complex32 Hypotenuse(Complex32 a, Complex32 b)
         /// <returns>Returns <code>sqrt(a<sup>2</sup> + b<sup>2</sup>)</code> without underflow/overflow.</returns>
         public static double Hypotenuse(double a, double b)
         {
+            if (double.IsNaN(a) || double.IsNaN(b))
+            {
+                return double.NaN;
+            }
+
             if (Math.Abs(a) > Math.Abs(b))
             {
                 double r = b/a;


Test patch:
diff --git a/src/Numerics.Tests/SpecialFunctionsTests/SpecialFunctionsTests.cs b/src/Numerics.Tests/SpecialFunctionsTests/SpecialFunctionsTests.cs
index 01f5fbffd..4a2761250 100644
--- a/src/Numerics.Tests/SpecialFunctionsTests/SpecialFunctionsTests.cs
+++ b/src/Numerics.Tests/SpecialFunctionsTests/SpecialFunctionsTests.cs
@@ -332,5 +332,18 @@ public void Log1p(double x, double y)
         {
             AssertHelpers.AlmostEqualRelative(y, SpecialFunctions.Log1p(x), 14);
         }
+
+        [Test]
+        public void HypotenuseWhenArgumentIsNanReturnesNan()
+        {
+            Assert.True(double.IsNaN(SpecialFunctions.Hypotenuse(double.NaN, 0)));
+        }
+
+        [Test]
+        public void L2NormOfVectorWhenElementsNanReturnsNan()
+        {
+            var v = LinearAlgebra.Vector<double>.Build.Dense(new[] { double.NaN, 0 });
+            Assert.True(double.IsNaN(v.L2Norm()));
+        }
     }
 }