From 6aecba2bf438de87b2d0d8186821f909689cf50b Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Sat, 29 Mar 2014 14:24:29 +0100 Subject: [PATCH] Modulus, Remainder and Factorial for BigInteger --- RELEASENOTES.md | 12 ++++---- src/Numerics/Euclid.cs | 22 +++++++++++++- src/Numerics/SpecialFunctions/Factorial.cs | 35 ++++++++++++++++++---- 3 files changed, 58 insertions(+), 11 deletions(-) diff --git a/RELEASENOTES.md b/RELEASENOTES.md index 11c0afde..27b97b8b 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -156,11 +156,12 @@ Changes as of now: ### Functions - Trig functions: common short names instead of very long names. Add sinc function. -- Excel Functions: TDIST, TINV, BETADIST, BETAINV, GAMMADIST, GAMMAINV, NORMDIST, NORMINV, NORMSDIST, NORMSINV QUARTILE, PERCENTILE, PERCENTRANK. -- Special Functions: BetaRegularized more robust for large arguments. -- Special Functions: new `GammaLowerRegularizedInv`. +- Excel functions: TDIST, TINV, BETADIST, BETAINV, GAMMADIST, GAMMAINV, NORMDIST, NORMINV, NORMSDIST, NORMSINV QUARTILE, PERCENTILE, PERCENTRANK. +- Special functions: BetaRegularized more robust for large arguments. +- Special functions: new `GammaLowerRegularizedInv`. - New distance functions in `Distance`: euclidean, manhattan, chebychev distance of arrays or generic vectors. SAD, MAE, SSD, MSE metrics. Pearson's, Canberra and Minkowski distance. Hamming distance. - Windows: ported windowing functions from Neodym (Hamming, Hann, Cosine, Lanczos, Gauss, Blackmann, Bartlett, ...) +- BigInteger factorial ### Build & Packages @@ -180,14 +181,15 @@ Changes as of now: - Integration: simplification of the double-exponential transformation api design. - FFT: converted to static class design and shorter names for simpler usage. Drop now redundant `Transform` class. - Generate: ported synthetic data generation and sampling routines from Neodym (includes all from old Signals namespace). F# module for higher order functions. -- Euclid: modulus vs remainder, integer theory (includes all from old NumberTheory namespace). +- Euclid: modulus vs remainder (also BigInteger), integer theory (includes all from old NumberTheory namespace). - Complex: common short names for Exp, Ln, Log10, Log. - Complex: fix issue where a *negative zero* may flip the sign in special cases (like `Atanh(2)`, where incidentally MATLAB and Mathematica do not agree on the sign either). - Complex: routines to return all two square and three cubic roots of a complex number. - Complex: More robust complex Asin/Acos for large real numbers. - Evaluate: routine to evaluate complex polynomials, or real polynomials at a complex point. - CommonParallel now also supported in .Net 3.5 and portable profiles; TaskScheduler can be replaced with custom implementation *~Thomas Ibel* -- F# code originally imported from F# PowerPack cleaned up *~Jack Pappas* +- F# BigRational type cleaned up and optimized *~Jack Pappas* +- F# BigRational IsZero, IsOne, IsInteger, Reciprocal, Power operator support (**), create from fraction. - F# functions now use the clearer `Func` suffix instead of just `F` if they return a function. - Precision: reworked, now much more consistent. **If you use `AlmostEqual` with numbers-between/ULP semantics, please do review your code to make sure you're still using the expected variant!**. If you use the decimal-places semantics, you may need to decrement the digits argument to get the same behavior as before. - Much less null checks, our code generally only throws `ArgumentNullException` if an unexpected null argument would *not* have caused an immediate `NullReferenceException`. diff --git a/src/Numerics/Euclid.cs b/src/Numerics/Euclid.cs index 05bc2fce..9323c94e 100644 --- a/src/Numerics/Euclid.cs +++ b/src/Numerics/Euclid.cs @@ -4,7 +4,7 @@ // http://github.com/mathnet/mathnet-numerics // http://mathnetnumerics.codeplex.com // -// Copyright (c) 2009-2013 Math.NET +// Copyright (c) 2009-2014 Math.NET // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation @@ -74,6 +74,16 @@ namespace MathNet.Numerics return ((dividend%divisor) + divisor)%divisor; } +#if !NOSYSNUMERICS + /// + /// Canonical Modulus. The result has the sign of the divisor. + /// + public static BigInteger Modulus(BigInteger dividend, BigInteger divisor) + { + return ((dividend%divisor) + divisor)%divisor; + } +#endif + /// /// Remainder (% operator). The result has the sign of the dividend. /// @@ -106,6 +116,16 @@ namespace MathNet.Numerics return dividend%divisor; } +#if !NOSYSNUMERICS + /// + /// Remainder (% operator). The result has the sign of the dividend. + /// + public static BigInteger Remainder(BigInteger dividend, BigInteger divisor) + { + return dividend%divisor; + } +#endif + /// /// Find out whether the provided 32 bit integer is an even number. /// diff --git a/src/Numerics/SpecialFunctions/Factorial.cs b/src/Numerics/SpecialFunctions/Factorial.cs index be858c46..ece7ea0d 100644 --- a/src/Numerics/SpecialFunctions/Factorial.cs +++ b/src/Numerics/SpecialFunctions/Factorial.cs @@ -28,13 +28,17 @@ // OTHER DEALINGS IN THE SOFTWARE. // +using System; +using MathNet.Numerics.Properties; + +#if !NOSYSNUMERICS +using System.Numerics; +#endif + // ReSharper disable CheckNamespace namespace MathNet.Numerics // ReSharper restore CheckNamespace { - using System; - using Properties; - public partial class SpecialFunctions { private const int FactorialMaxArgument = 170; @@ -64,7 +68,7 @@ namespace MathNet.Numerics /// /// A value value! for value > 0 /// - /// If you need to multiply or divide various such factorials, consider using the logarithmic version + /// If you need to multiply or divide various such factorials, consider using the logarithmic version /// instead so you can add instead of multiply and subtract instead of divide, and /// then exponentiate the result using . This will also circumvent the problem that /// factorials become very large even for small parameters. @@ -85,6 +89,27 @@ namespace MathNet.Numerics return Double.PositiveInfinity; } +#if !NOSYSNUMERICS + /// + /// Computes the factorial of an integer. + /// + public static BigInteger Factorial(BigInteger x) + { + if (x < 0) + { + throw new ArgumentOutOfRangeException("x", Resources.ArgumentPositive); + } + if (x == 0) + { + return BigInteger.One; + } + + BigInteger r = x; + while (--x > 1) r *= x; + return r; + } +#endif + /// /// Computes the logarithmic factorial function x -> ln(x!) of an integer number > 0. /// @@ -147,7 +172,7 @@ namespace MathNet.Numerics /// A nonnegative value n. /// An array of nonnegative values that sum to . /// The multinomial coefficient. - /// if is . + /// if is . /// If or any of the are negative. /// If the sum of all is not equal to . public static double Multinomial(int n, int[] ni)