Browse Source

Modulus, Remainder and Factorial for BigInteger

pull/202/merge v3.0.0-alpha9
Christoph Ruegg 13 years ago
parent
commit
6aecba2bf4
  1. 12
      RELEASENOTES.md
  2. 22
      src/Numerics/Euclid.cs
  3. 35
      src/Numerics/SpecialFunctions/Factorial.cs

12
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`.

22
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
/// <summary>
/// Canonical Modulus. The result has the sign of the divisor.
/// </summary>
public static BigInteger Modulus(BigInteger dividend, BigInteger divisor)
{
return ((dividend%divisor) + divisor)%divisor;
}
#endif
/// <summary>
/// Remainder (% operator). The result has the sign of the dividend.
/// </summary>
@ -106,6 +116,16 @@ namespace MathNet.Numerics
return dividend%divisor;
}
#if !NOSYSNUMERICS
/// <summary>
/// Remainder (% operator). The result has the sign of the dividend.
/// </summary>
public static BigInteger Remainder(BigInteger dividend, BigInteger divisor)
{
return dividend%divisor;
}
#endif
/// <summary>
/// Find out whether the provided 32 bit integer is an even number.
/// </summary>

35
src/Numerics/SpecialFunctions/Factorial.cs

@ -28,13 +28,17 @@
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
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
/// </summary>
/// <returns>A value value! for value > 0</returns>
/// <remarks>
/// 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
/// <see cref="FactorialLn"/> instead so you can add instead of multiply and subtract instead of divide, and
/// then exponentiate the result using <see cref="System.Math.Exp"/>. 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
/// <summary>
/// Computes the factorial of an integer.
/// </summary>
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
/// <summary>
/// Computes the logarithmic factorial function x -> ln(x!) of an integer number > 0.
/// </summary>
@ -147,7 +172,7 @@ namespace MathNet.Numerics
/// <param name="n">A nonnegative value n.</param>
/// <param name="ni">An array of nonnegative values that sum to <paramref name="n"/>.</param>
/// <returns>The multinomial coefficient.</returns>
/// <exception cref="ArgumentNullException">if <paramref name="ni"/> is <see langword="null" />.</exception>
/// <exception cref="ArgumentNullException">if <paramref name="ni"/> is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If <paramref name="n"/> or any of the <paramref name="ni"/> are negative.</exception>
/// <exception cref="ArgumentException">If the sum of all <paramref name="ni"/> is not equal to <paramref name="n"/>.</exception>
public static double Multinomial(int n, int[] ni)

Loading…
Cancel
Save