From 79f29c450a68ed2d8e1fd52e48c346e17cb35087 Mon Sep 17 00:00:00 2001
From: Febin <25066330+febkor@users.noreply.github.com>
Date: Fri, 5 Mar 2021 09:46:48 +0200
Subject: [PATCH] Distributions: minor simplification to Cauchy for performance
---
src/Numerics/Distributions/Cauchy.cs | 13 ++++++++-----
1 file changed, 8 insertions(+), 5 deletions(-)
diff --git a/src/Numerics/Distributions/Cauchy.cs b/src/Numerics/Distributions/Cauchy.cs
index c8af2860..f6d9b1bd 100644
--- a/src/Numerics/Distributions/Cauchy.cs
+++ b/src/Numerics/Distributions/Cauchy.cs
@@ -179,7 +179,8 @@ namespace MathNet.Numerics.Distributions
///
public double Density(double x)
{
- return 1.0/(Constants.Pi*_scale*(1.0 + (((x - _location)/_scale)*((x - _location)/_scale))));
+ var z = (x - _location)/_scale;
+ return 1.0/(Constants.Pi*_scale*(1.0 + z * z));
}
///
@@ -190,7 +191,8 @@ namespace MathNet.Numerics.Distributions
///
public double DensityLn(double x)
{
- return -Math.Log(Constants.Pi*_scale*(1.0 + (((x - _location)/_scale)*((x - _location)/_scale))));
+ var z = (x - _location)/_scale;
+ return -Math.Log(Constants.Pi*_scale*(1.0 + z * z));
}
///
@@ -283,9 +285,9 @@ namespace MathNet.Numerics.Distributions
throw new ArgumentException("Invalid parametrization for the distribution.");
}
- return 1.0/(Constants.Pi*scale*(1.0 + (((x - location)/scale)*((x - location)/scale))));
+ var z = (x - location)/scale;
+ return 1.0/(Constants.Pi*scale*(1.0 + z * z));
}
-
///
/// Computes the log probability density of the distribution (lnPDF) at x, i.e. ln(∂P(X ≤ x)/∂x).
///
@@ -301,7 +303,8 @@ namespace MathNet.Numerics.Distributions
throw new ArgumentException("Invalid parametrization for the distribution.");
}
- return -Math.Log(Constants.Pi*scale*(1.0 + (((x - location)/scale)*((x - location)/scale))));
+ var z = (x - location)/scale;
+ return -Math.Log(Constants.Pi*scale*(1.0 + z * z ));
}
///