|
| 1 | +// <copyright file="Expm1.cs" company="Math.NET"> |
| 2 | +// Math.NET Numerics, part of the Math.NET Project |
| 3 | +// http://numerics.mathdotnet.com |
| 4 | +// http://github.com/mathnet/mathnet-numerics |
| 5 | +// |
| 6 | +// Copyright (c) 2009-2022 Math.NET |
| 7 | +// |
| 8 | +// Permission is hereby granted, free of charge, to any person |
| 9 | +// obtaining a copy of this software and associated documentation |
| 10 | +// files (the "Software"), to deal in the Software without |
| 11 | +// restriction, including without limitation the rights to use, |
| 12 | +// copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | +// copies of the Software, and to permit persons to whom the |
| 14 | +// Software is furnished to do so, subject to the following |
| 15 | +// conditions: |
| 16 | +// |
| 17 | +// The above copyright notice and this permission notice shall be |
| 18 | +// included in all copies or substantial portions of the Software. |
| 19 | +// |
| 20 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 21 | +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 22 | +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 23 | +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 24 | +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 25 | +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 26 | +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 27 | +// OTHER DEALINGS IN THE SOFTWARE. |
| 28 | +// </copyright> |
| 29 | + |
| 30 | +using System; |
| 31 | + |
| 32 | +// ReSharper disable once CheckNamespace |
| 33 | +namespace MathNet.Numerics |
| 34 | +{ |
| 35 | + public partial class SpecialFunctions |
| 36 | + { |
| 37 | + /// <summary> |
| 38 | + /// Numerically stable exponential minus one, i.e. <code>x -> exp(x)-1</code> |
| 39 | + /// </summary> |
| 40 | + /// <param name="power">A number specifying a power.</param> |
| 41 | + /// <returns>Returns <code>exp(power)-1</code>.</returns> |
| 42 | + public static double Expm1(double power) |
| 43 | + { |
| 44 | + double x = Math.Abs(power); |
| 45 | + if (x > 0.1) |
| 46 | + { |
| 47 | + return Math.Exp(power) - 1.0; |
| 48 | + } |
| 49 | + |
| 50 | + if (x < x.PositiveEpsilonOf()) |
| 51 | + { |
| 52 | + return x; |
| 53 | + } |
| 54 | + |
| 55 | + // Series Expansion to x^k / k! |
| 56 | + int k = 0; |
| 57 | + double term = 1.0; |
| 58 | + return Series.Evaluate( |
| 59 | + () => |
| 60 | + { |
| 61 | + k++; |
| 62 | + term *= power; |
| 63 | + term /= k; |
| 64 | + return term; |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + /// <summary> |
| 69 | + /// Numerically stable exponential minus one, i.e. <code>x -> exp(x)-1</code> |
| 70 | + /// </summary> |
| 71 | + /// <param name="power">A number specifying a power.</param> |
| 72 | + /// <returns>Returns <code>exp(power)-1</code>.</returns> |
| 73 | + [Obsolete("Use Expm1 instead")] |
| 74 | + public static double ExponentialMinusOne(double power) |
| 75 | + { |
| 76 | + return Expm1(power); |
| 77 | + } |
| 78 | + } |
| 79 | +} |
0 commit comments