Browse Source

Euclid: new Euclid class with mod/rem and all that used to be in integer theory #175

pull/184/head
Christoph Ruegg 13 years ago
parent
commit
59e375ac01
  1. 18
      src/Examples/NumberTheory.cs
  2. 8
      src/Examples/Signals/Chebyshev.cs
  3. 611
      src/Numerics/Euclid.cs
  4. 21
      src/Numerics/Generate.cs
  5. 8
      src/Numerics/IntegralTransforms/Algorithms/DiscreteFourierTransform.Bluestein.cs
  6. 10
      src/Numerics/IntegralTransforms/Algorithms/DiscreteFourierTransform.RadixN.cs
  7. 1
      src/Numerics/Integration/SimpsonRule.cs
  8. 197
      src/Numerics/NumberTheory/IntegerTheory.Euclid.Big.cs
  9. 203
      src/Numerics/NumberTheory/IntegerTheory.Euclid.cs
  10. 245
      src/Numerics/NumberTheory/IntegerTheory.cs
  11. 4
      src/Numerics/Numerics.csproj
  12. 201
      src/UnitTests/EuclidTests/GcdRelatedTest.cs
  13. 222
      src/UnitTests/EuclidTests/GcdRelatedTestBigInteger.cs
  14. 104
      src/UnitTests/EuclidTests/IntegerTheoryTest.cs
  15. 202
      src/UnitTests/NumberTheoryTests/GcdRelatedTest.cs
  16. 222
      src/UnitTests/NumberTheoryTests/GcdRelatedTestBigInteger.cs
  17. 6
      src/UnitTests/UnitTests.csproj

18
src/Examples/NumberTheory.cs

@ -25,7 +25,7 @@
// </copyright>
using System;
using MathNet.Numerics.NumberTheory;
using MathNet.Numerics;
namespace Examples
{
@ -63,17 +63,17 @@ namespace Examples
{
// 1. Find out whether the provided number is an even number
Console.WriteLine(@"1. Find out whether the provided number is an even number");
Console.WriteLine(@"{0} is even = {1}. {2} is even = {3}", 1, IntegerTheory.IsEven(1), 2, 2.IsEven());
Console.WriteLine(@"{0} is even = {1}. {2} is even = {3}", 1, Euclid.IsEven(1), 2, 2.IsEven());
Console.WriteLine();
// 2. Find out whether the provided number is an odd number
Console.WriteLine(@"2. Find out whether the provided number is an odd number");
Console.WriteLine(@"{0} is odd = {1}. {2} is odd = {3}", 1, 1.IsOdd(), 2, IntegerTheory.IsOdd(2));
Console.WriteLine(@"{0} is odd = {1}. {2} is odd = {3}", 1, 1.IsOdd(), 2, Euclid.IsOdd(2));
Console.WriteLine();
// 3. Find out whether the provided number is a perfect power of two
Console.WriteLine(@"2. Find out whether the provided number is a perfect power of two");
Console.WriteLine(@"{0} is power of two = {1}. {2} is power of two = {3}", 5, 5.IsPowerOfTwo(), 16, IntegerTheory.IsPowerOfTwo(16));
Console.WriteLine(@"{0} is power of two = {1}. {2} is power of two = {3}", 5, 5.IsPowerOfTwo(), 16, Euclid.IsPowerOfTwo(16));
Console.WriteLine();
// 4. Find the closest perfect power of two that is larger or equal to 97
@ -88,29 +88,29 @@ namespace Examples
// 6. Find out whether the number is a perfect square
Console.WriteLine(@"6. Find out whether the number is a perfect square");
Console.WriteLine(@"{0} is perfect square = {1}. {2} is perfect square = {3}", 37, 37.IsPerfectSquare(), 81, IntegerTheory.IsPerfectSquare(81));
Console.WriteLine(@"{0} is perfect square = {1}. {2} is perfect square = {3}", 37, 37.IsPerfectSquare(), 81, Euclid.IsPerfectSquare(81));
Console.WriteLine();
// 7. Compute the greatest common divisor of 32 and 36
Console.WriteLine(@"7. Returns the greatest common divisor of 32 and 36");
Console.WriteLine(IntegerTheory.GreatestCommonDivisor(32, 36));
Console.WriteLine(Euclid.GreatestCommonDivisor(32, 36));
Console.WriteLine();
// 8. Compute the greatest common divisor of 492, -984, 123, 246
Console.WriteLine(@"8. Returns the greatest common divisor of 492, -984, 123, 246");
Console.WriteLine(IntegerTheory.GreatestCommonDivisor(492, -984, 123, 246));
Console.WriteLine(Euclid.GreatestCommonDivisor(492, -984, 123, 246));
Console.WriteLine();
// 9. Compute the extended greatest common divisor "z", such that 45*x + 18*y = z
Console.WriteLine(@"9. Compute the extended greatest common divisor Z, such that 45*x + 18*y = Z");
long x, y;
var z = IntegerTheory.ExtendedGreatestCommonDivisor(45, 18, out x, out y);
var z = Euclid.ExtendedGreatestCommonDivisor(45, 18, out x, out y);
Console.WriteLine(@"z = {0}, x = {1}, y = {2}. 45*{1} + 18*{2} = {0}", z, x, y);
Console.WriteLine();
// 10. Compute the least common multiple of 16 and 12
Console.WriteLine(@"10. Compute the least common multiple of 16 and 12");
Console.WriteLine(IntegerTheory.LeastCommonMultiple(16, 12));
Console.WriteLine(Euclid.LeastCommonMultiple(16, 12));
Console.WriteLine();
}
}

8
src/Examples/Signals/Chebyshev.cs

@ -25,7 +25,7 @@
// </copyright>
using System;
using MathNet.Numerics.Signals;
using MathNet.Numerics;
namespace Examples.SignalsExamples
{
@ -62,7 +62,8 @@ namespace Examples.SignalsExamples
public void Run()
{
// 1. Get 20 samples of f(x) = (x * x) / 2 at the roots of the Chebyshev polynomial of the first kind within interval [0, 10]
var result = SignalGenerator.ChebyshevNodesFirstKind(Function, 0, 10, 20);
var roots = FindRoots.ChebychevPolynomialFirstKind(20, 0, 10);
var result = Generate.Map(roots, Function);
Console.WriteLine(@"1. Get 20 samples of f(x) = (x * x) / 2 at the roots of the Chebyshev polynomial of the first kind within interval [0, 10]");
for (var i = 0; i < result.Length; i++)
{
@ -73,7 +74,8 @@ namespace Examples.SignalsExamples
Console.WriteLine();
// 2. Get 20 samples of f(x) = (x * x) / 2 at the roots of the Chebyshev polynomial of the second kind within interval [0, 10]
result = SignalGenerator.ChebyshevNodesSecondKind(Function, 0, 10, 20);
roots = FindRoots.ChebychevPolynomialSecondKind(20, 0, 10);
result = Generate.Map(roots, Function);
Console.WriteLine(@"2. Get 20 samples of f(x) = (x * x) / 2 at the roots of the Chebyshev polynomial of the second kind within interval [0, 10]");
for (var i = 0; i < result.Length; i++)
{

611
src/Numerics/Euclid.cs

@ -0,0 +1,611 @@
// <copyright file="Euclid.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com
//
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
using System;
using System.Collections.Generic;
#if !NOSYSNUMERICS
using System.Numerics;
#endif
namespace MathNet.Numerics
{
/// <summary>
/// Integer number theory functions.
/// </summary>
public static class Euclid
{
/// <summary>
/// Canonical Modulus. The result has the sign of the divisor.
/// </summary>
public static double Modulus(double dividend, double divisor)
{
return ((dividend%divisor) + divisor)%divisor;
}
/// <summary>
/// Canonical Modulus. The result has the sign of the divisor.
/// </summary>
public static int Modulus(int dividend, int divisor)
{
return ((dividend%divisor) + divisor)%divisor;
}
/// <summary>
/// Canonical Modulus. The result has the sign of the divisor.
/// </summary>
public static long Modulus(long dividend, long divisor)
{
return ((dividend%divisor) + divisor)%divisor;
}
/// <summary>
/// Remainder (% operator). The result has the sign of the dividend.
/// </summary>
public static double Remainder(double dividend, double divisor)
{
return dividend%divisor;
}
/// <summary>
/// Remainder (% operator). The result has the sign of the dividend.
/// </summary>
public static int Remainder(int dividend, int divisor)
{
return dividend%divisor;
}
/// <summary>
/// Remainder (% operator). The result has the sign of the dividend.
/// </summary>
public static long Remainder(long dividend, long divisor)
{
return dividend%divisor;
}
/// <summary>
/// Find out whether the provided 32 bit integer is an even number.
/// </summary>
/// <param name="number">The number to very whether it's even.</param>
/// <returns>True if and only if it is an even number.</returns>
public static bool IsEven(this int number)
{
return (number & 0x1) == 0x0;
}
/// <summary>
/// Find out whether the provided 64 bit integer is an even number.
/// </summary>
/// <param name="number">The number to very whether it's even.</param>
/// <returns>True if and only if it is an even number.</returns>
public static bool IsEven(this long number)
{
return (number & 0x1) == 0x0;
}
/// <summary>
/// Find out whether the provided 32 bit integer is an odd number.
/// </summary>
/// <param name="number">The number to very whether it's odd.</param>
/// <returns>True if and only if it is an odd number.</returns>
public static bool IsOdd(this int number)
{
return (number & 0x1) == 0x1;
}
/// <summary>
/// Find out whether the provided 64 bit integer is an odd number.
/// </summary>
/// <param name="number">The number to very whether it's odd.</param>
/// <returns>True if and only if it is an odd number.</returns>
public static bool IsOdd(this long number)
{
return (number & 0x1) == 0x1;
}
/// <summary>
/// Find out whether the provided 32 bit integer is a perfect power of two.
/// </summary>
/// <param name="number">The number to very whether it's a power of two.</param>
/// <returns>True if and only if it is a power of two.</returns>
public static bool IsPowerOfTwo(this int number)
{
return number > 0 && (number & (number - 1)) == 0x0;
}
/// <summary>
/// Find out whether the provided 64 bit integer is a perfect power of two.
/// </summary>
/// <param name="number">The number to very whether it's a power of two.</param>
/// <returns>True if and only if it is a power of two.</returns>
public static bool IsPowerOfTwo(this long number)
{
return number > 0 && (number & (number - 1)) == 0x0;
}
/// <summary>
/// Find out whether the provided 32 bit integer is a perfect square, i.e. a square of an integer.
/// </summary>
/// <param name="number">The number to very whether it's a perfect square.</param>
/// <returns>True if and only if it is a perfect square.</returns>
public static bool IsPerfectSquare(this int number)
{
if (number < 0)
{
return false;
}
int lastHexDigit = number & 0xF;
if (lastHexDigit > 9)
{
return false; // return immediately in 6 cases out of 16.
}
if (lastHexDigit == 0 || lastHexDigit == 1 || lastHexDigit == 4 || lastHexDigit == 9)
{
int t = (int)Math.Floor(Math.Sqrt(number) + 0.5);
return (t * t) == number;
}
return false;
}
/// <summary>
/// Find out whether the provided 64 bit integer is a perfect square, i.e. a square of an integer.
/// </summary>
/// <param name="number">The number to very whether it's a perfect square.</param>
/// <returns>True if and only if it is a perfect square.</returns>
public static bool IsPerfectSquare(this long number)
{
if (number < 0)
{
return false;
}
int lastHexDigit = (int)(number & 0xF);
if (lastHexDigit > 9)
{
return false; // return immediately in 6 cases out of 16.
}
if (lastHexDigit == 0 || lastHexDigit == 1 || lastHexDigit == 4 || lastHexDigit == 9)
{
long t = (long)Math.Floor(Math.Sqrt(number) + 0.5);
return (t * t) == number;
}
return false;
}
/// <summary>
/// Raises 2 to the provided integer exponent (0 &lt;= exponent &lt; 31).
/// </summary>
/// <param name="exponent">The exponent to raise 2 up to.</param>
/// <returns>2 ^ exponent.</returns>
/// <exception cref="ArgumentOutOfRangeException"/>
public static int PowerOfTwo(this int exponent)
{
if (exponent < 0 || exponent >= 31)
{
throw new ArgumentOutOfRangeException("exponent");
}
return 1 << exponent;
}
/// <summary>
/// Raises 2 to the provided integer exponent (0 &lt;= exponent &lt; 63).
/// </summary>
/// <param name="exponent">The exponent to raise 2 up to.</param>
/// <returns>2 ^ exponent.</returns>
/// <exception cref="ArgumentOutOfRangeException"/>
public static long PowerOfTwo(this long exponent)
{
if (exponent < 0 || exponent >= 63)
{
throw new ArgumentOutOfRangeException("exponent");
}
return ((long)1) << (int)exponent;
}
/// <summary>
/// Find the closest perfect power of two that is larger or equal to the provided
/// 32 bit integer.
/// </summary>
/// <param name="number">The number of which to find the closest upper power of two.</param>
/// <returns>A power of two.</returns>
/// <exception cref="ArgumentOutOfRangeException"/>
public static int CeilingToPowerOfTwo(this int number)
{
if (number == Int32.MinValue)
{
return 0;
}
const int maxPowerOfTwo = 0x40000000;
if (number > maxPowerOfTwo)
{
throw new ArgumentOutOfRangeException("number");
}
number--;
number |= number >> 1;
number |= number >> 2;
number |= number >> 4;
number |= number >> 8;
number |= number >> 16;
return number + 1;
}
/// <summary>
/// Find the closest perfect power of two that is larger or equal to the provided
/// 64 bit integer.
/// </summary>
/// <param name="number">The number of which to find the closest upper power of two.</param>
/// <returns>A power of two.</returns>
/// <exception cref="ArgumentOutOfRangeException"/>
public static long CeilingToPowerOfTwo(this long number)
{
if (number == Int64.MinValue)
{
return 0;
}
const long maxPowerOfTwo = 0x4000000000000000;
if (number > maxPowerOfTwo)
{
throw new ArgumentOutOfRangeException("number");
}
number--;
number |= number >> 1;
number |= number >> 2;
number |= number >> 4;
number |= number >> 8;
number |= number >> 16;
number |= number >> 32;
return number + 1;
}
/// <summary>
/// Returns the greatest common divisor (<c>gcd</c>) of two integers using Euclid's algorithm.
/// </summary>
/// <param name="a">First Integer: a.</param>
/// <param name="b">Second Integer: b.</param>
/// <returns>Greatest common divisor <c>gcd</c>(a,b)</returns>
public static long GreatestCommonDivisor(long a, long b)
{
while (b != 0)
{
var remainder = a%b;
a = b;
b = remainder;
}
return Math.Abs(a);
}
/// <summary>
/// Returns the greatest common divisor (<c>gcd</c>) of a set of integers using Euclid's
/// algorithm.
/// </summary>
/// <param name="integers">List of Integers.</param>
/// <returns>Greatest common divisor <c>gcd</c>(list of integers)</returns>
public static long GreatestCommonDivisor(IList<long> integers)
{
if (null == integers)
{
throw new ArgumentNullException("integers");
}
if (integers.Count == 0)
{
return 0;
}
var gcd = Math.Abs(integers[0]);
for (var i = 1; (i < integers.Count) && (gcd > 1); i++)
{
gcd = GreatestCommonDivisor(gcd, integers[i]);
}
return gcd;
}
/// <summary>
/// Returns the greatest common divisor (<c>gcd</c>) of a set of integers using Euclid's algorithm.
/// </summary>
/// <param name="integers">List of Integers.</param>
/// <returns>Greatest common divisor <c>gcd</c>(list of integers)</returns>
public static long GreatestCommonDivisor(params long[] integers)
{
return GreatestCommonDivisor((IList<long>)integers);
}
/// <summary>
/// Computes the extended greatest common divisor, such that a*x + b*y = <c>gcd</c>(a,b).
/// </summary>
/// <param name="a">First Integer: a.</param>
/// <param name="b">Second Integer: b.</param>
/// <param name="x">Resulting x, such that a*x + b*y = <c>gcd</c>(a,b).</param>
/// <param name="y">Resulting y, such that a*x + b*y = <c>gcd</c>(a,b)</param>
/// <returns>Greatest common divisor <c>gcd</c>(a,b)</returns>
/// <example>
/// <code>
/// long x,y,d;
/// d = Fn.GreatestCommonDivisor(45,18,out x, out y);
/// -> d == 9 &amp;&amp; x == 1 &amp;&amp; y == -2
/// </code>
/// The <c>gcd</c> of 45 and 18 is 9: 18 = 2*9, 45 = 5*9. 9 = 1*45 -2*18, therefore x=1 and y=-2.
/// </example>
public static long ExtendedGreatestCommonDivisor(long a, long b, out long x, out long y)
{
long mp = 1, np = 0, m = 0, n = 1;
while (b != 0)
{
long rem;
#if PORTABLE
rem = a % b;
var quot = a / b;
#else
long quot = Math.DivRem(a, b, out rem);
#endif
a = b;
b = rem;
var tmp = m;
m = mp - (quot*m);
mp = tmp;
tmp = n;
n = np - (quot*n);
np = tmp;
}
if (a >= 0)
{
x = mp;
y = np;
return a;
}
x = -mp;
y = -np;
return -a;
}
/// <summary>
/// Returns the least common multiple (<c>lcm</c>) of two integers using Euclid's algorithm.
/// </summary>
/// <param name="a">First Integer: a.</param>
/// <param name="b">Second Integer: b.</param>
/// <returns>Least common multiple <c>lcm</c>(a,b)</returns>
public static long LeastCommonMultiple(long a, long b)
{
if ((a == 0) || (b == 0))
{
return 0;
}
return Math.Abs((a/GreatestCommonDivisor(a, b))*b);
}
/// <summary>
/// Returns the least common multiple (<c>lcm</c>) of a set of integers using Euclid's algorithm.
/// </summary>
/// <param name="integers">List of Integers.</param>
/// <returns>Least common multiple <c>lcm</c>(list of integers)</returns>
public static long LeastCommonMultiple(IList<long> integers)
{
if (null == integers)
{
throw new ArgumentNullException("integers");
}
if (integers.Count == 0)
{
return 1;
}
var lcm = Math.Abs(integers[0]);
for (var i = 1; i < integers.Count; i++)
{
lcm = LeastCommonMultiple(lcm, integers[i]);
}
return lcm;
}
/// <summary>
/// Returns the least common multiple (<c>lcm</c>) of a set of integers using Euclid's algorithm.
/// </summary>
/// <param name="integers">List of Integers.</param>
/// <returns>Least common multiple <c>lcm</c>(list of integers)</returns>
public static long LeastCommonMultiple(params long[] integers)
{
return LeastCommonMultiple((IList<long>)integers);
}
#if !NOSYSNUMERICS
/// <summary>
/// Returns the greatest common divisor (<c>gcd</c>) of two big integers.
/// </summary>
/// <param name="a">First Integer: a.</param>
/// <param name="b">Second Integer: b.</param>
/// <returns>Greatest common divisor <c>gcd</c>(a,b)</returns>
public static BigInteger GreatestCommonDivisor(BigInteger a, BigInteger b)
{
return BigInteger.GreatestCommonDivisor(a, b);
}
/// <summary>
/// Returns the greatest common divisor (<c>gcd</c>) of a set of big integers.
/// </summary>
/// <param name="integers">List of Integers.</param>
/// <returns>Greatest common divisor <c>gcd</c>(list of integers)</returns>
public static BigInteger GreatestCommonDivisor(IList<BigInteger> integers)
{
if (null == integers)
{
throw new ArgumentNullException("integers");
}
if (integers.Count == 0)
{
return 0;
}
var gcd = BigInteger.Abs(integers[0]);
for (int i = 1; (i < integers.Count) && (gcd > BigInteger.One); i++)
{
gcd = GreatestCommonDivisor(gcd, integers[i]);
}
return gcd;
}
/// <summary>
/// Returns the greatest common divisor (<c>gcd</c>) of a set of big integers.
/// </summary>
/// <param name="integers">List of Integers.</param>
/// <returns>Greatest common divisor <c>gcd</c>(list of integers)</returns>
public static BigInteger GreatestCommonDivisor(params BigInteger[] integers)
{
return GreatestCommonDivisor((IList<BigInteger>)integers);
}
/// <summary>
/// Computes the extended greatest common divisor, such that a*x + b*y = <c>gcd</c>(a,b).
/// </summary>
/// <param name="a">First Integer: a.</param>
/// <param name="b">Second Integer: b.</param>
/// <param name="x">Resulting x, such that a*x + b*y = <c>gcd</c>(a,b).</param>
/// <param name="y">Resulting y, such that a*x + b*y = <c>gcd</c>(a,b)</param>
/// <returns>Greatest common divisor <c>gcd</c>(a,b)</returns>
/// <example>
/// <code>
/// long x,y,d;
/// d = Fn.GreatestCommonDivisor(45,18,out x, out y);
/// -> d == 9 &amp;&amp; x == 1 &amp;&amp; y == -2
/// </code>
/// The <c>gcd</c> of 45 and 18 is 9: 18 = 2*9, 45 = 5*9. 9 = 1*45 -2*18, therefore x=1 and y=-2.
/// </example>
public static BigInteger ExtendedGreatestCommonDivisor(BigInteger a, BigInteger b, out BigInteger x, out BigInteger y)
{
BigInteger mp = BigInteger.One, np = BigInteger.Zero, m = BigInteger.Zero, n = BigInteger.One;
while (!b.IsZero)
{
BigInteger rem;
BigInteger quot = BigInteger.DivRem(a, b, out rem);
a = b;
b = rem;
BigInteger tmp = m;
m = mp - (quot*m);
mp = tmp;
tmp = n;
n = np - (quot*n);
np = tmp;
}
if (a >= BigInteger.Zero)
{
x = mp;
y = np;
return a;
}
x = -mp;
y = -np;
return -a;
}
/// <summary>
/// Returns the least common multiple (<c>lcm</c>) of two big integers.
/// </summary>
/// <param name="a">First Integer: a.</param>
/// <param name="b">Second Integer: b.</param>
/// <returns>Least common multiple <c>lcm</c>(a,b)</returns>
public static BigInteger LeastCommonMultiple(BigInteger a, BigInteger b)
{
if (a.IsZero || b.IsZero)
{
return BigInteger.Zero;
}
return BigInteger.Abs((a/BigInteger.GreatestCommonDivisor(a, b))*b);
}
/// <summary>
/// Returns the least common multiple (<c>lcm</c>) of a set of big integers.
/// </summary>
/// <param name="integers">List of Integers.</param>
/// <returns>Least common multiple <c>lcm</c>(list of integers)</returns>
public static BigInteger LeastCommonMultiple(IList<BigInteger> integers)
{
if (null == integers)
{
throw new ArgumentNullException("integers");
}
if (integers.Count == 0)
{
return 1;
}
var lcm = BigInteger.Abs(integers[0]);
for (int i = 1; i < integers.Count; i++)
{
lcm = LeastCommonMultiple(lcm, integers[i]);
}
return lcm;
}
/// <summary>
/// Returns the least common multiple (<c>lcm</c>) of a set of big integers.
/// </summary>
/// <param name="integers">List of Integers.</param>
/// <returns>Least common multiple <c>lcm</c>(list of integers)</returns>
public static BigInteger LeastCommonMultiple(params BigInteger[] integers)
{
return LeastCommonMultiple((IList<BigInteger>)integers);
}
#endif
}
}

21
src/Numerics/Generate.cs

@ -357,5 +357,26 @@ namespace MathNet.Numerics
{
return Stable.Samples(MersenneTwister.Default, alpha, beta, scale, location);
}
/// <summary>
/// Generate sample by sampling a function at the provided points.
/// </summary>
public static TV[] Map<TU, TV>(TU[] points, Func<TU, TV> map)
{
var res = new TV[points.Length];
for (int i = 0; i < points.Length; i++)
{
res[i] = map(points[i]);
}
return res;
}
/// <summary>
/// Generate a sample sequence by sampling a function at the provided point sequence.
/// </summary>
public static IEnumerable<TV> MapSequence<TU, TV>(IEnumerable<TU> points, Func<TU, TV> map)
{
return points.Select(map);
}
}
}

8
src/Numerics/IntegralTransforms/Algorithms/DiscreteFourierTransform.Bluestein.cs

@ -28,14 +28,14 @@
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
using System;
using MathNet.Numerics.Threading;
namespace MathNet.Numerics.IntegralTransforms.Algorithms
{
using System;
using NumberTheory;
using Threading;
#if !NOSYSNUMERICS
using Complex = System.Numerics.Complex;
using System.Numerics;
#endif
/// <summary>

10
src/Numerics/IntegralTransforms/Algorithms/DiscreteFourierTransform.RadixN.cs

@ -28,15 +28,15 @@
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
using System;
using MathNet.Numerics.Properties;
using MathNet.Numerics.Threading;
namespace MathNet.Numerics.IntegralTransforms.Algorithms
{
using System;
using NumberTheory;
using Properties;
using Threading;
#if !NOSYSNUMERICS
using Complex = System.Numerics.Complex;
using System.Numerics;
#endif
/// <summary>

1
src/Numerics/Integration/SimpsonRule.cs

@ -29,7 +29,6 @@
// </copyright>
using System;
using MathNet.Numerics.NumberTheory;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.Integration

197
src/Numerics/NumberTheory/IntegerTheory.Euclid.Big.cs

@ -1,197 +0,0 @@
// <copyright file="IntegerTheory.Euclid.Big.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com
//
// Copyright (c) 2009-2010 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
#if !NOSYSNUMERICS
namespace MathNet.Numerics.NumberTheory
{
using System;
using System.Collections.Generic;
using System.Numerics;
/// <summary>
/// Number theory utility functions for integers.
/// </summary>
public static partial class IntegerTheory
{
/// <summary>
/// Returns the greatest common divisor (<c>gcd</c>) of two big integers.
/// </summary>
/// <param name="a">First Integer: a.</param>
/// <param name="b">Second Integer: b.</param>
/// <returns>Greatest common divisor <c>gcd</c>(a,b)</returns>
public static BigInteger GreatestCommonDivisor(BigInteger a, BigInteger b)
{
return BigInteger.GreatestCommonDivisor(a, b);
}
/// <summary>
/// Returns the greatest common divisor (<c>gcd</c>) of a set of big integers.
/// </summary>
/// <param name="integers">List of Integers.</param>
/// <returns>Greatest common divisor <c>gcd</c>(list of integers)</returns>
public static BigInteger GreatestCommonDivisor(IList<BigInteger> integers)
{
if (null == integers)
{
throw new ArgumentNullException("integers");
}
if (integers.Count == 0)
{
return 0;
}
var gcd = BigInteger.Abs(integers[0]);
for (int i = 1; (i < integers.Count) && (gcd > BigInteger.One); i++)
{
gcd = GreatestCommonDivisor(gcd, integers[i]);
}
return gcd;
}
/// <summary>
/// Returns the greatest common divisor (<c>gcd</c>) of a set of big integers.
/// </summary>
/// <param name="integers">List of Integers.</param>
/// <returns>Greatest common divisor <c>gcd</c>(list of integers)</returns>
public static BigInteger GreatestCommonDivisor(params BigInteger[] integers)
{
return GreatestCommonDivisor((IList<BigInteger>)integers);
}
/// <summary>
/// Computes the extended greatest common divisor, such that a*x + b*y = <c>gcd</c>(a,b).
/// </summary>
/// <param name="a">First Integer: a.</param>
/// <param name="b">Second Integer: b.</param>
/// <param name="x">Resulting x, such that a*x + b*y = <c>gcd</c>(a,b).</param>
/// <param name="y">Resulting y, such that a*x + b*y = <c>gcd</c>(a,b)</param>
/// <returns>Greatest common divisor <c>gcd</c>(a,b)</returns>
/// <example>
/// <code>
/// long x,y,d;
/// d = Fn.GreatestCommonDivisor(45,18,out x, out y);
/// -> d == 9 &amp;&amp; x == 1 &amp;&amp; y == -2
/// </code>
/// The <c>gcd</c> of 45 and 18 is 9: 18 = 2*9, 45 = 5*9. 9 = 1*45 -2*18, therefore x=1 and y=-2.
/// </example>
public static BigInteger ExtendedGreatestCommonDivisor(
BigInteger a,
BigInteger b,
out BigInteger x,
out BigInteger y)
{
BigInteger mp = BigInteger.One, np = BigInteger.Zero, m = BigInteger.Zero, n = BigInteger.One;
while (!b.IsZero)
{
BigInteger rem;
BigInteger quot = BigInteger.DivRem(a, b, out rem);
a = b;
b = rem;
BigInteger tmp = m;
m = mp - (quot * m);
mp = tmp;
tmp = n;
n = np - (quot * n);
np = tmp;
}
if (a >= BigInteger.Zero)
{
x = mp;
y = np;
return a;
}
x = -mp;
y = -np;
return -a;
}
/// <summary>
/// Returns the least common multiple (<c>lcm</c>) of two big integers.
/// </summary>
/// <param name="a">First Integer: a.</param>
/// <param name="b">Second Integer: b.</param>
/// <returns>Least common multiple <c>lcm</c>(a,b)</returns>
public static BigInteger LeastCommonMultiple(BigInteger a, BigInteger b)
{
if (a.IsZero || b.IsZero)
{
return BigInteger.Zero;
}
return BigInteger.Abs((a / BigInteger.GreatestCommonDivisor(a, b)) * b);
}
/// <summary>
/// Returns the least common multiple (<c>lcm</c>) of a set of big integers.
/// </summary>
/// <param name="integers">List of Integers.</param>
/// <returns>Least common multiple <c>lcm</c>(list of integers)</returns>
public static BigInteger LeastCommonMultiple(IList<BigInteger> integers)
{
if (null == integers)
{
throw new ArgumentNullException("integers");
}
if (integers.Count == 0)
{
return 1;
}
var lcm = BigInteger.Abs(integers[0]);
for (int i = 1; i < integers.Count; i++)
{
lcm = LeastCommonMultiple(lcm, integers[i]);
}
return lcm;
}
/// <summary>
/// Returns the least common multiple (<c>lcm</c>) of a set of big integers.
/// </summary>
/// <param name="integers">List of Integers.</param>
/// <returns>Least common multiple <c>lcm</c>(list of integers)</returns>
public static BigInteger LeastCommonMultiple(params BigInteger[] integers)
{
return LeastCommonMultiple((IList<BigInteger>)integers);
}
}
}
#endif

203
src/Numerics/NumberTheory/IntegerTheory.Euclid.cs

@ -1,203 +0,0 @@
// <copyright file="IntegerTheory.Euclid.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com
// Copyright (c) 2009-2010 Math.NET
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
namespace MathNet.Numerics.NumberTheory
{
using System;
using System.Collections.Generic;
/// <summary>
/// Number theory utility functions for integers.
/// </summary>
public static partial class IntegerTheory
{
/// <summary>
/// Returns the greatest common divisor (<c>gcd</c>) of two integers using Euclid's algorithm.
/// </summary>
/// <param name="a">First Integer: a.</param>
/// <param name="b">Second Integer: b.</param>
/// <returns>Greatest common divisor <c>gcd</c>(a,b)</returns>
public static long GreatestCommonDivisor(long a, long b)
{
while (b != 0)
{
var remainder = a % b;
a = b;
b = remainder;
}
return Math.Abs(a);
}
/// <summary>
/// Returns the greatest common divisor (<c>gcd</c>) of a set of integers using Euclid's
/// algorithm.
/// </summary>
/// <param name="integers">List of Integers.</param>
/// <returns>Greatest common divisor <c>gcd</c>(list of integers)</returns>
public static long GreatestCommonDivisor(IList<long> integers)
{
if (null == integers)
{
throw new ArgumentNullException("integers");
}
if (integers.Count == 0)
{
return 0;
}
var gcd = Math.Abs(integers[0]);
for (var i = 1; (i < integers.Count) && (gcd > 1); i++)
{
gcd = GreatestCommonDivisor(gcd, integers[i]);
}
return gcd;
}
/// <summary>
/// Returns the greatest common divisor (<c>gcd</c>) of a set of integers using Euclid's algorithm.
/// </summary>
/// <param name="integers">List of Integers.</param>
/// <returns>Greatest common divisor <c>gcd</c>(list of integers)</returns>
public static long GreatestCommonDivisor(params long[] integers)
{
return GreatestCommonDivisor((IList<long>)integers);
}
/// <summary>
/// Computes the extended greatest common divisor, such that a*x + b*y = <c>gcd</c>(a,b).
/// </summary>
/// <param name="a">First Integer: a.</param>
/// <param name="b">Second Integer: b.</param>
/// <param name="x">Resulting x, such that a*x + b*y = <c>gcd</c>(a,b).</param>
/// <param name="y">Resulting y, such that a*x + b*y = <c>gcd</c>(a,b)</param>
/// <returns>Greatest common divisor <c>gcd</c>(a,b)</returns>
/// <example>
/// <code>
/// long x,y,d;
/// d = Fn.GreatestCommonDivisor(45,18,out x, out y);
/// -> d == 9 &amp;&amp; x == 1 &amp;&amp; y == -2
/// </code>
/// The <c>gcd</c> of 45 and 18 is 9: 18 = 2*9, 45 = 5*9. 9 = 1*45 -2*18, therefore x=1 and y=-2.
/// </example>
public static long ExtendedGreatestCommonDivisor(
long a,
long b,
out long x,
out long y)
{
long mp = 1, np = 0, m = 0, n = 1;
while (b != 0)
{
long rem;
#if PORTABLE
rem = a % b;
var quot = a / b;
#else
long quot = Math.DivRem(a, b, out rem);
#endif
a = b;
b = rem;
var tmp = m;
m = mp - (quot * m);
mp = tmp;
tmp = n;
n = np - (quot * n);
np = tmp;
}
if (a >= 0)
{
x = mp;
y = np;
return a;
}
x = -mp;
y = -np;
return -a;
}
/// <summary>
/// Returns the least common multiple (<c>lcm</c>) of two integers using Euclid's algorithm.
/// </summary>
/// <param name="a">First Integer: a.</param>
/// <param name="b">Second Integer: b.</param>
/// <returns>Least common multiple <c>lcm</c>(a,b)</returns>
public static long LeastCommonMultiple(long a, long b)
{
if ((a == 0) || (b == 0))
{
return 0;
}
return Math.Abs((a / GreatestCommonDivisor(a, b)) * b);
}
/// <summary>
/// Returns the least common multiple (<c>lcm</c>) of a set of integers using Euclid's algorithm.
/// </summary>
/// <param name="integers">List of Integers.</param>
/// <returns>Least common multiple <c>lcm</c>(list of integers)</returns>
public static long LeastCommonMultiple(IList<long> integers)
{
if (null == integers)
{
throw new ArgumentNullException("integers");
}
if (integers.Count == 0)
{
return 1;
}
var lcm = Math.Abs(integers[0]);
for (var i = 1; i < integers.Count; i++)
{
lcm = LeastCommonMultiple(lcm, integers[i]);
}
return lcm;
}
/// <summary>
/// Returns the least common multiple (<c>lcm</c>) of a set of integers using Euclid's algorithm.
/// </summary>
/// <param name="integers">List of Integers.</param>
/// <returns>Least common multiple <c>lcm</c>(list of integers)</returns>
public static long LeastCommonMultiple(params long[] integers)
{
return LeastCommonMultiple((IList<long>)integers);
}
}
}

245
src/Numerics/NumberTheory/IntegerTheory.cs

@ -1,245 +0,0 @@
// <copyright file="IntegerTheory.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com
//
// Copyright (c) 2009-2010 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
namespace MathNet.Numerics.NumberTheory
{
using System;
/// <summary>
/// Number theory utility functions for integers.
/// </summary>
public static partial class IntegerTheory
{
/// <summary>
/// Find out whether the provided 32 bit integer is an even number.
/// </summary>
/// <param name="number">The number to very whether it's even.</param>
/// <returns>True if and only if it is an even number.</returns>
public static bool IsEven(this int number)
{
return (number & 0x1) == 0x0;
}
/// <summary>
/// Find out whether the provided 64 bit integer is an even number.
/// </summary>
/// <param name="number">The number to very whether it's even.</param>
/// <returns>True if and only if it is an even number.</returns>
public static bool IsEven(this long number)
{
return (number & 0x1) == 0x0;
}
/// <summary>
/// Find out whether the provided 32 bit integer is an odd number.
/// </summary>
/// <param name="number">The number to very whether it's odd.</param>
/// <returns>True if and only if it is an odd number.</returns>
public static bool IsOdd(this int number)
{
return (number & 0x1) == 0x1;
}
/// <summary>
/// Find out whether the provided 64 bit integer is an odd number.
/// </summary>
/// <param name="number">The number to very whether it's odd.</param>
/// <returns>True if and only if it is an odd number.</returns>
public static bool IsOdd(this long number)
{
return (number & 0x1) == 0x1;
}
/// <summary>
/// Find out whether the provided 32 bit integer is a perfect power of two.
/// </summary>
/// <param name="number">The number to very whether it's a power of two.</param>
/// <returns>True if and only if it is a power of two.</returns>
public static bool IsPowerOfTwo(this int number)
{
return number > 0 && (number & (number - 1)) == 0x0;
}
/// <summary>
/// Find out whether the provided 64 bit integer is a perfect power of two.
/// </summary>
/// <param name="number">The number to very whether it's a power of two.</param>
/// <returns>True if and only if it is a power of two.</returns>
public static bool IsPowerOfTwo(this long number)
{
return number > 0 && (number & (number - 1)) == 0x0;
}
/// <summary>
/// Find the closest perfect power of two that is larger or equal to the provided
/// 32 bit integer.
/// </summary>
/// <param name="number">The number of which to find the closest upper power of two.</param>
/// <returns>A power of two.</returns>
/// <exception cref="ArgumentOutOfRangeException"/>
public static int CeilingToPowerOfTwo(this int number)
{
if (number == Int32.MinValue)
{
return 0;
}
const int maxPowerOfTwo = 0x40000000;
if (number > maxPowerOfTwo)
{
throw new ArgumentOutOfRangeException("number");
}
number--;
number |= number >> 1;
number |= number >> 2;
number |= number >> 4;
number |= number >> 8;
number |= number >> 16;
return number + 1;
}
/// <summary>
/// Find the closest perfect power of two that is larger or equal to the provided
/// 64 bit integer.
/// </summary>
/// <param name="number">The number of which to find the closest upper power of two.</param>
/// <returns>A power of two.</returns>
/// <exception cref="ArgumentOutOfRangeException"/>
public static long CeilingToPowerOfTwo(this long number)
{
if (number == Int64.MinValue)
{
return 0;
}
const long maxPowerOfTwo = 0x4000000000000000;
if (number > maxPowerOfTwo)
{
throw new ArgumentOutOfRangeException("number");
}
number--;
number |= number >> 1;
number |= number >> 2;
number |= number >> 4;
number |= number >> 8;
number |= number >> 16;
number |= number >> 32;
return number + 1;
}
/// <summary>
/// Raises 2 to the provided integer exponent (0 &lt;= exponent &lt; 31).
/// </summary>
/// <param name="exponent">The exponent to raise 2 up to.</param>
/// <returns>2 ^ exponent.</returns>
/// <exception cref="ArgumentOutOfRangeException"/>
public static int PowerOfTwo(this int exponent)
{
if (exponent < 0 || exponent >= 31)
{
throw new ArgumentOutOfRangeException("exponent");
}
return 1 << exponent;
}
/// <summary>
/// Raises 2 to the provided integer exponent (0 &lt;= exponent &lt; 63).
/// </summary>
/// <param name="exponent">The exponent to raise 2 up to.</param>
/// <returns>2 ^ exponent.</returns>
/// <exception cref="ArgumentOutOfRangeException"/>
public static long PowerOfTwo(this long exponent)
{
if (exponent < 0 || exponent >= 63)
{
throw new ArgumentOutOfRangeException("exponent");
}
return ((long)1) << (int)exponent;
}
/// <summary>
/// Find out whether the provided 32 bit integer is a perfect square, i.e. a square of an integer.
/// </summary>
/// <param name="number">The number to very whether it's a perfect square.</param>
/// <returns>True if and only if it is a perfect square.</returns>
public static bool IsPerfectSquare(this int number)
{
if (number < 0)
{
return false;
}
int lastHexDigit = number & 0xF;
if (lastHexDigit > 9)
{
return false; // return immediately in 6 cases out of 16.
}
if (lastHexDigit == 0 || lastHexDigit == 1 || lastHexDigit == 4 || lastHexDigit == 9)
{
int t = (int)Math.Floor(Math.Sqrt(number) + 0.5);
return (t * t) == number;
}
return false;
}
/// <summary>
/// Find out whether the provided 64 bit integer is a perfect square, i.e. a square of an integer.
/// </summary>
/// <param name="number">The number to very whether it's a perfect square.</param>
/// <returns>True if and only if it is a perfect square.</returns>
public static bool IsPerfectSquare(this long number)
{
if (number < 0)
{
return false;
}
int lastHexDigit = (int)(number & 0xF);
if (lastHexDigit > 9)
{
return false; // return immediately in 6 cases out of 16.
}
if (lastHexDigit == 0 || lastHexDigit == 1 || lastHexDigit == 4 || lastHexDigit == 9)
{
long t = (long)Math.Floor(Math.Sqrt(number) + 0.5);
return (t * t) == number;
}
return false;
}
}
}

4
src/Numerics/Numerics.csproj

@ -87,6 +87,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Euclid.cs" />
<Compile Include="Generate.cs" />
<Compile Include="GoodnessOfFit.cs" />
<Compile Include="Precision.Comparison.cs" />
@ -387,9 +388,6 @@
<Compile Include="Interpolation\IInterpolation.cs" />
<Compile Include="Interpolate.cs" />
<Compile Include="Interpolation\SplineBoundaryCondition.cs" />
<Compile Include="NumberTheory\IntegerTheory.Euclid.Big.cs" />
<Compile Include="NumberTheory\IntegerTheory.cs" />
<Compile Include="NumberTheory\IntegerTheory.Euclid.cs" />
<Compile Include="Precision.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\Resources.Designer.cs">

201
src/UnitTests/EuclidTests/GcdRelatedTest.cs

@ -0,0 +1,201 @@
// <copyright file="GcdRelatedTest.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com
// Copyright (c) 2009-2010 Math.NET
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
using System;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.EuclidTests
{
/// <summary>
/// GreatestCommonDivisor related test.
/// </summary>
[TestFixture, Category("Functions")]
public class GcdRelatedTest
{
/// <summary>
/// GreatestCommonDivisor handles normal input correctly.
/// </summary>
[Test]
public void GcdHandlesNormalInputCorrectly()
{
Assert.AreEqual(0, Euclid.GreatestCommonDivisor(0, 0), "Gcd(0,0)");
Assert.AreEqual(6, Euclid.GreatestCommonDivisor(0, 6), "Gcd(0,6)");
Assert.AreEqual(1, Euclid.GreatestCommonDivisor(7, 13), "Gcd(7,13)");
Assert.AreEqual(7, Euclid.GreatestCommonDivisor(7, 14), "Gcd(7,14)");
Assert.AreEqual(1, Euclid.GreatestCommonDivisor(7, 15), "Gcd(7,15)");
Assert.AreEqual(3, Euclid.GreatestCommonDivisor(6, 15), "Gcd(6,15)");
}
/// <summary>
/// GreatestCommonDivisor handles negative input correctly.
/// </summary>
[Test]
public void GcdHandlesNegativeInputCorrectly()
{
Assert.AreEqual(5, Euclid.GreatestCommonDivisor(-5, 0), "Gcd(-5,0)");
Assert.AreEqual(5, Euclid.GreatestCommonDivisor(0, -5), "Gcd(0, -5)");
Assert.AreEqual(1, Euclid.GreatestCommonDivisor(-7, 15), "Gcd(-7,15)");
Assert.AreEqual(1, Euclid.GreatestCommonDivisor(-7, -15), "Gcd(-7,-15)");
}
/// <summary>
/// GreatestCommonDivisor supports large input.
/// </summary>
[Test]
public void GcdSupportsLargeInput()
{
Assert.AreEqual(Int32.MaxValue, Euclid.GreatestCommonDivisor(0, Int32.MaxValue), "Gcd(0,Int32Max)");
Assert.AreEqual(Int64.MaxValue, Euclid.GreatestCommonDivisor(0, Int64.MaxValue), "Gcd(0,Int64Max)");
Assert.AreEqual(1, Euclid.GreatestCommonDivisor(Int32.MaxValue, Int64.MaxValue), "Gcd(Int32Max,Int64Max)");
Assert.AreEqual(1 << 18, Euclid.GreatestCommonDivisor(1 << 18, 1 << 20), "Gcd(1>>18,1<<20)");
}
/// <summary>
/// Extended GreatestCommonDivisor handles normal input correctly
/// </summary>
[Test]
public void ExtendedGcdHandlesNormalInputCorrectly()
{
long x, y;
Assert.AreEqual(3, Euclid.ExtendedGreatestCommonDivisor(6, 15, out x, out y), "Egcd(6,15)");
Assert.AreEqual(3, (6*x) + (15*y), "Egcd(6,15) -> a*x+b*y");
Assert.AreEqual(3, Euclid.ExtendedGreatestCommonDivisor(-6, 15, out x, out y), "Egcd(-6,15)");
Assert.AreEqual(3, (-6*x) + (15*y), "Egcd(-6,15) -> a*x+b*y");
Assert.AreEqual(3, Euclid.ExtendedGreatestCommonDivisor(-6, -15, out x, out y), "Egcd(-6,-15)");
Assert.AreEqual(3, (-6*x) + (-15*y), "Egcd(-6,-15) -> a*x+b*y");
}
/// <summary>
/// List GreatestCommonDivisor handles normal input Correctly
/// </summary>
[Test]
public void ListGcdHandlesNormalInputCorrectly()
{
Assert.AreEqual(2, Euclid.GreatestCommonDivisor(-10, 6, -8), "Gcd(-10,6,-8)");
Assert.AreEqual(1, Euclid.GreatestCommonDivisor(-10, 6, -8, 5, 9, 13), "Gcd(-10,6,-8,5,9,13)");
Assert.AreEqual(5, Euclid.GreatestCommonDivisor(-10, 20, 120, 60, -15, 1000), "Gcd(-10,20,120,60,-15,1000)");
Assert.AreEqual(3, Euclid.GreatestCommonDivisor(Int64.MaxValue - 1, Int64.MaxValue - 4, Int64.MaxValue - 7), "Gcd(Int64Max-1,Int64Max-4,Int64Max-7)");
Assert.AreEqual(123, Euclid.GreatestCommonDivisor(492, -2*492, 492/4), "Gcd(492, -984, 123)");
}
/// <summary>
/// List GreatestCommonDivisor handles special input correctly.
/// </summary>
[Test]
public void ListGcdHandlesSpecialInputCorrectly()
{
Assert.AreEqual(0, Euclid.GreatestCommonDivisor(new long[0]), "Gcd()");
Assert.AreEqual(100, Euclid.GreatestCommonDivisor(-100), "Gcd(-100)");
}
/// <summary>
/// List GreatestCommonDivisor checks for <c>null</c> all arguments.
/// </summary>
[Test]
public void ListGcdChecksForNullArguments()
{
Assert.Throws(
typeof (ArgumentNullException),
() => Euclid.GreatestCommonDivisor((long[])null));
}
/// <summary>
/// LeastCommonMultiple handles normal input correctly.
/// </summary>
[Test]
public void LcmHandlesNormalInputCorrectly()
{
Assert.AreEqual(10, Euclid.LeastCommonMultiple(10, 10), "Lcm(10,10)");
Assert.AreEqual(0, Euclid.LeastCommonMultiple(0, 10), "Lcm(0,10)");
Assert.AreEqual(0, Euclid.LeastCommonMultiple(10, 0), "Lcm(10,0)");
Assert.AreEqual(77, Euclid.LeastCommonMultiple(11, 7), "Lcm(11,7)");
Assert.AreEqual(33, Euclid.LeastCommonMultiple(11, 33), "Lcm(11,33)");
Assert.AreEqual(374, Euclid.LeastCommonMultiple(11, 34), "Lcm(11,34)");
}
/// <summary>
/// LeastCommonMultiple handles negative input correctly.
/// </summary>
[Test]
public void LcmHandlesNegativeInputCorrectly()
{
Assert.AreEqual(352, Euclid.LeastCommonMultiple(11, -32), "Lcm(11,-32)");
Assert.AreEqual(352, Euclid.LeastCommonMultiple(-11, 32), "Lcm(-11,32)");
Assert.AreEqual(352, Euclid.LeastCommonMultiple(-11, -32), "Lcm(-11,-32)");
}
/// <summary>
/// LeastCommonMultiple supports large input.
/// </summary>
[Test]
public void LcmSupportsLargeInput()
{
Assert.AreEqual(Int32.MaxValue, Euclid.LeastCommonMultiple(Int32.MaxValue, Int32.MaxValue), "Lcm(Int32Max,Int32Max)");
Assert.AreEqual(Int64.MaxValue, Euclid.LeastCommonMultiple(Int64.MaxValue, Int64.MaxValue), "Lcm(Int64Max,Int64Max)");
Assert.AreEqual(Int64.MaxValue, Euclid.LeastCommonMultiple(-Int64.MaxValue, -Int64.MaxValue), "Lcm(-Int64Max,-Int64Max)");
Assert.AreEqual(Int64.MaxValue, Euclid.LeastCommonMultiple(-Int64.MaxValue, Int64.MaxValue), "Lcm(-Int64Max,Int64Max)");
}
/// <summary>
/// List LeastCommonMultiple handles normal input correctly.
/// </summary>
[Test]
public void ListLcmHandlesNormalInputCorrectly()
{
Assert.AreEqual(120, Euclid.LeastCommonMultiple(-10, 6, -8), "Lcm(-10,6,-8)");
Assert.AreEqual(4680, Euclid.LeastCommonMultiple(-10, 6, -8, 5, 9, 13), "Lcm(-10,6,-8,5,9,13)");
Assert.AreEqual(3000, Euclid.LeastCommonMultiple(-10, 20, 120, 60, -15, 1000), "Lcm(-10,20,120,60,-15,1000)");
Assert.AreEqual(984, Euclid.LeastCommonMultiple(492, -2*492, 492/4), "Lcm(492, -984, 123)");
Assert.AreEqual(2016, Euclid.LeastCommonMultiple(32, 42, 36, 18), "Lcm(32,42,36,18)");
}
/// <summary>
/// List LeastCommonMultiple handles special input correctly.
/// </summary>
[Test]
public void ListLcmHandlesSpecialInputCorrectly()
{
Assert.AreEqual(1, Euclid.LeastCommonMultiple(new long[0]), "Lcm()");
Assert.AreEqual(100, Euclid.LeastCommonMultiple(-100), "Lcm(-100)");
}
/// <summary>
/// List LeastCommonMultiple checks for <c>null</c> arguments.
/// </summary>
[Test]
public void ListLcmChecksForNullArguments()
{
Assert.Throws(
typeof (ArgumentNullException),
() => Euclid.LeastCommonMultiple((long[])null));
}
}
}

222
src/UnitTests/EuclidTests/GcdRelatedTestBigInteger.cs

@ -0,0 +1,222 @@
// <copyright file="GcdRelatedTestBigInteger.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com
// Copyright (c) 2009-2010 Math.NET
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
#if !NOSYSNUMERICS
using System;
using System.Numerics;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.EuclidTests
{
/// <summary>
/// GreatestCommonDivisor related test for <c>BigInteger</c>.
/// </summary>
[TestFixture, Category("Functions")]
public class GcdRelatedTestBigInteger
{
/// <summary>
/// GreatestCommonDivisor handles normal input correctly.
/// </summary>
[Test]
public void GcdHandlesNormalInputCorrectly()
{
Assert.AreEqual((BigInteger)0, Euclid.GreatestCommonDivisor(BigInteger.Zero, BigInteger.Zero), "Gcd(0,0)");
Assert.AreEqual((BigInteger)6, Euclid.GreatestCommonDivisor(BigInteger.Zero, 6), "Gcd(0,6)");
Assert.AreEqual((BigInteger)1, Euclid.GreatestCommonDivisor((BigInteger)7, 13), "Gcd(7,13)");
Assert.AreEqual((BigInteger)7, Euclid.GreatestCommonDivisor((BigInteger)7, 14), "Gcd(7,14)");
Assert.AreEqual((BigInteger)1, Euclid.GreatestCommonDivisor((BigInteger)7, 15), "Gcd(7,15)");
Assert.AreEqual((BigInteger)3, Euclid.GreatestCommonDivisor((BigInteger)6, 15), "Gcd(6,15)");
}
/// <summary>
/// GreatestCommonDivisor handles negative input correctly.
/// </summary>
[Test]
public void GcdHandlesNegativeInputCorrectly()
{
Assert.AreEqual((BigInteger)5, Euclid.GreatestCommonDivisor((BigInteger)(-5), 0), "Gcd(-5,0)");
Assert.AreEqual((BigInteger)5, Euclid.GreatestCommonDivisor(BigInteger.Zero, -5), "Gcd(0, -5)");
Assert.AreEqual((BigInteger)1, Euclid.GreatestCommonDivisor((BigInteger)(-7), 15), "Gcd(-7,15)");
Assert.AreEqual((BigInteger)1, Euclid.GreatestCommonDivisor((BigInteger)(-7), -15), "Gcd(-7,-15)");
}
/// <summary>
/// GreatestCommonDivisor supports large input.
/// </summary>
[Test]
public void GcdSupportsLargeInput()
{
Assert.AreEqual((BigInteger)Int32.MaxValue, Euclid.GreatestCommonDivisor(BigInteger.Zero, Int32.MaxValue), "Gcd(0,Int32Max)");
Assert.AreEqual((BigInteger)Int64.MaxValue, Euclid.GreatestCommonDivisor(BigInteger.Zero, Int64.MaxValue), "Gcd(0,Int64Max)");
Assert.AreEqual((BigInteger)1, Euclid.GreatestCommonDivisor((BigInteger)Int32.MaxValue, Int64.MaxValue), "Gcd(Int32Max,Int64Max)");
Assert.AreEqual((BigInteger)(1 << 18), Euclid.GreatestCommonDivisor((BigInteger)(1 << 18), 1 << 20), "Gcd(1>>18,1<<20)");
Assert.AreEqual((BigInteger)(1 << 18), Euclid.GreatestCommonDivisor((BigInteger)(1 << 18), 1 << 20), "Gcd(1>>18,1<<20)");
#if !PORTABLE
Assert.AreEqual((BigInteger)4569031055798, Euclid.GreatestCommonDivisor(BigInteger.Parse("7305316061155559483748611586449542122662"), BigInteger.Parse("57377277362010117405715236427413896")), "Gcd(large)");
#endif
}
/// <summary>
/// Extended GreatestCommonDivisor handles normal input correctly.
/// </summary>
[Test]
public void ExtendedGcdHandlesNormalInputCorrectly()
{
BigInteger x, y;
Assert.AreEqual((BigInteger)3, Euclid.ExtendedGreatestCommonDivisor(6, 15, out x, out y), "Egcd(6,15)");
Assert.AreEqual((BigInteger)3, (6*x) + (15*y), "Egcd(6,15) -> a*x+b*y");
Assert.AreEqual((BigInteger)3, Euclid.ExtendedGreatestCommonDivisor(-6, 15, out x, out y), "Egcd(-6,15)");
Assert.AreEqual((BigInteger)3, (-6*x) + (15*y), "Egcd(-6,15) -> a*x+b*y");
Assert.AreEqual((BigInteger)3, Euclid.ExtendedGreatestCommonDivisor(-6, -15, out x, out y), "Egcd(-6,-15)");
Assert.AreEqual((BigInteger)3, (-6*x) + (-15*y), "Egcd(-6,-15) -> a*x+b*y");
#if !PORTABLE
var a = BigInteger.Parse("7305316061155559483748611586449542122662");
var b = BigInteger.Parse("57377277362010117405715236427413896");
Assert.AreEqual((BigInteger)4569031055798, Euclid.ExtendedGreatestCommonDivisor(a, b, out x, out y), "Egcd(large)");
Assert.AreEqual((BigInteger)4569031055798, (a*x) + (b*y), "Egcd(large) -> a*x+b*y");
Assert.AreEqual((BigInteger)4569031055798, Euclid.ExtendedGreatestCommonDivisor(-a, b, out x, out y), "Egcd(-large)");
Assert.AreEqual((BigInteger)4569031055798, (-a*x) + (b*y), "Egcd(-large) -> a*x+b*y");
#endif
}
/// <summary>
/// List GreatestCommonDivisor handles normal input Correctly
/// </summary>
[Test]
public void ListGcdHandlesNormalInputCorrectly()
{
Assert.AreEqual((BigInteger)2, Euclid.GreatestCommonDivisor((BigInteger)(-10), 6, -8), "Gcd(-10,6,-8)");
Assert.AreEqual((BigInteger)1, Euclid.GreatestCommonDivisor((BigInteger)(-10), 6, -8, 5, 9, 13), "Gcd(-10,6,-8,5,9,13)");
Assert.AreEqual((BigInteger)5, Euclid.GreatestCommonDivisor((BigInteger)(-10), 20, 120, 60, -15, 1000), "Gcd(-10,20,120,60,-15,1000)");
Assert.AreEqual((BigInteger)3, Euclid.GreatestCommonDivisor((BigInteger)(Int64.MaxValue - 1), Int64.MaxValue - 4, Int64.MaxValue - 7), "Gcd(Int64Max-1,Int64Max-4,Int64Max-7)");
Assert.AreEqual((BigInteger)123, Euclid.GreatestCommonDivisor((BigInteger)492, -2*492, 492/4), "Gcd(492, -984, 123)");
}
/// <summary>
/// List GreatestCommonDivisor handles special input correctly.
/// </summary>
[Test]
public void ListGcdHandlesSpecialInputCorrectly()
{
Assert.AreEqual((BigInteger)0, Euclid.GreatestCommonDivisor(new BigInteger[0]), "Gcd()");
Assert.AreEqual((BigInteger)100, Euclid.GreatestCommonDivisor((BigInteger)(-100)), "Gcd(-100)");
}
/// <summary>
/// List GreatestCommonDivisor checks for <c>null</c> all arguments.
/// </summary>
[Test]
public void ListGcdChecksForNullArguments()
{
Assert.Throws(
typeof (ArgumentNullException),
() => Euclid.GreatestCommonDivisor((BigInteger[])null));
}
/// <summary>
/// LeastCommonMultiple handles normal input correctly.
/// </summary>
[Test]
public void LcmHandlesNormalInputCorrectly()
{
Assert.AreEqual((BigInteger)10, Euclid.LeastCommonMultiple((BigInteger)10, 10), "Lcm(10,10)");
Assert.AreEqual((BigInteger)0, Euclid.LeastCommonMultiple(BigInteger.Zero, 10), "Lcm(0,10)");
Assert.AreEqual((BigInteger)0, Euclid.LeastCommonMultiple((BigInteger)10, 0), "Lcm(10,0)");
Assert.AreEqual((BigInteger)77, Euclid.LeastCommonMultiple((BigInteger)11, 7), "Lcm(11,7)");
Assert.AreEqual((BigInteger)33, Euclid.LeastCommonMultiple((BigInteger)11, 33), "Lcm(11,33)");
Assert.AreEqual((BigInteger)374, Euclid.LeastCommonMultiple((BigInteger)11, 34), "Lcm(11,34)");
}
/// <summary>
/// LeastCommonMultiple handles negative input correctly.
/// </summary>
[Test]
public void LcmHandlesNegativeInputCorrectly()
{
Assert.AreEqual((BigInteger)352, Euclid.LeastCommonMultiple((BigInteger)11, -32), "Lcm(11,-32)");
Assert.AreEqual((BigInteger)352, Euclid.LeastCommonMultiple((BigInteger)(-11), 32), "Lcm(-11,32)");
Assert.AreEqual((BigInteger)352, Euclid.LeastCommonMultiple((BigInteger)(-11), -32), "Lcm(-11,-32)");
}
/// <summary>
/// LeastCommonMultiple supports large input.
/// </summary>
[Test]
public void LcmSupportsLargeInput()
{
Assert.AreEqual((BigInteger)Int32.MaxValue, Euclid.LeastCommonMultiple((BigInteger)Int32.MaxValue, Int32.MaxValue), "Lcm(Int32Max,Int32Max)");
Assert.AreEqual((BigInteger)Int64.MaxValue, Euclid.LeastCommonMultiple((BigInteger)Int64.MaxValue, Int64.MaxValue), "Lcm(Int64Max,Int64Max)");
Assert.AreEqual((BigInteger)Int64.MaxValue, Euclid.LeastCommonMultiple((BigInteger)(-Int64.MaxValue), -Int64.MaxValue), "Lcm(-Int64Max,-Int64Max)");
Assert.AreEqual((BigInteger)Int64.MaxValue, Euclid.LeastCommonMultiple((BigInteger)(-Int64.MaxValue), Int64.MaxValue), "Lcm(-Int64Max,Int64Max)");
#if !PORTABLE
Assert.AreEqual(BigInteger.Parse("91739176367857263082719902034485224119528064014300888465614024"), Euclid.LeastCommonMultiple(BigInteger.Parse("7305316061155559483748611586449542122662"), BigInteger.Parse("57377277362010117405715236427413896")), "Lcm(large)");
#endif
}
/// <summary>
/// List LeastCommonMultiple handles normal input correctly.
/// </summary>
[Test]
public void ListLcmHandlesNormalInputCorrectly()
{
Assert.AreEqual((BigInteger)120, Euclid.LeastCommonMultiple((BigInteger)(-10), 6, -8), "Lcm(-10,6,-8)");
Assert.AreEqual((BigInteger)4680, Euclid.LeastCommonMultiple((BigInteger)(-10), 6, -8, 5, 9, 13), "Lcm(-10,6,-8,5,9,13)");
Assert.AreEqual((BigInteger)3000, Euclid.LeastCommonMultiple((BigInteger)(-10), 20, 120, 60, -15, 1000), "Lcm(-10,20,120,60,-15,1000)");
Assert.AreEqual((BigInteger)984, Euclid.LeastCommonMultiple((BigInteger)492, -2*492, 492/4), "Lcm(492, -984, 123)");
Assert.AreEqual((BigInteger)2016, Euclid.LeastCommonMultiple((BigInteger)32, 42, 36, 18), "Lcm(32,42,36,18)");
}
/// <summary>
/// List LeastCommonMultiple handles special input correctly.
/// </summary>
[Test]
public void ListLcmHandlesSpecialInputCorrectly()
{
Assert.AreEqual((BigInteger)1, Euclid.LeastCommonMultiple(new BigInteger[0]), "Lcm()");
Assert.AreEqual((BigInteger)100, Euclid.LeastCommonMultiple((BigInteger)(-100)), "Lcm(-100)");
}
/// <summary>
/// List LeastCommonMultiple checks for <c>null</c> arguments.
/// </summary>
[Test]
public void ListLcmChecksForNullArguments()
{
Assert.Throws(
typeof (ArgumentNullException),
() => Euclid.LeastCommonMultiple((BigInteger[])null));
}
}
}
#endif

104
src/UnitTests/NumberTheoryTests/IntegerTheoryTest.cs → src/UnitTests/EuclidTests/IntegerTheoryTest.cs

@ -24,18 +24,112 @@
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
namespace MathNet.Numerics.UnitTests.NumberTheoryTests
{
using System;
using NumberTheory;
using NUnit.Framework;
using System;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.EuclidTests
{
/// <summary>
/// Integer theory tests.
/// </summary>
[TestFixture, Category("Functions")]
public class IntegerTheoryTest
{
[Test]
public void TestModulus()
{
Assert.That(Euclid.Modulus(0, 3), Is.EqualTo(0));
Assert.That(Euclid.Modulus(2, 3), Is.EqualTo(2));
Assert.That(Euclid.Modulus(3, 3), Is.EqualTo(0));
Assert.That(Euclid.Modulus(4, 3), Is.EqualTo(1));
Assert.That(Euclid.Modulus(6, 3), Is.EqualTo(0));
Assert.That(Euclid.Modulus(-1, 3), Is.EqualTo(2));
Assert.That(Euclid.Modulus(-2, 3), Is.EqualTo(1));
Assert.That(Euclid.Modulus(-3, 3), Is.EqualTo(0));
Assert.That(Euclid.Modulus(-4, 3), Is.EqualTo(2));
Assert.That(Euclid.Modulus(0, -3), Is.EqualTo(0));
Assert.That(Euclid.Modulus(2, -3), Is.EqualTo(-1));
Assert.That(Euclid.Modulus(3, -3), Is.EqualTo(0));
Assert.That(Euclid.Modulus(4, -3), Is.EqualTo(-2));
Assert.That(Euclid.Modulus(6, -3), Is.EqualTo(0));
Assert.That(Euclid.Modulus(-1, -3), Is.EqualTo(-1));
Assert.That(Euclid.Modulus(-2, -3), Is.EqualTo(-2));
Assert.That(Euclid.Modulus(-3, -3), Is.EqualTo(0));
Assert.That(Euclid.Modulus(-4, -3), Is.EqualTo(-1));
}
[Test]
public void TestModulusFloatingPoint()
{
Assert.That(Euclid.Modulus(0.2, 3), Is.EqualTo(0.2).Within(1e-12));
Assert.That(Euclid.Modulus(2.2, 3), Is.EqualTo(2.2).Within(1e-12));
Assert.That(Euclid.Modulus(3.2, 3), Is.EqualTo(0.2).Within(1e-12));
Assert.That(Euclid.Modulus(4.2, 3), Is.EqualTo(1.2).Within(1e-12));
Assert.That(Euclid.Modulus(6.2, 3), Is.EqualTo(0.2).Within(1e-12));
Assert.That(Euclid.Modulus(-1.2, 3), Is.EqualTo(1.8).Within(1e-12));
Assert.That(Euclid.Modulus(-2.2, 3), Is.EqualTo(0.8).Within(1e-12));
Assert.That(Euclid.Modulus(-3.2, 3), Is.EqualTo(2.8).Within(1e-12));
Assert.That(Euclid.Modulus(-4.2, 3), Is.EqualTo(1.8).Within(1e-12));
Assert.That(Euclid.Modulus(0.2, -3), Is.EqualTo(-2.8).Within(1e-12));
Assert.That(Euclid.Modulus(2.2, -3), Is.EqualTo(-0.8).Within(1e-12));
Assert.That(Euclid.Modulus(3.2, -3), Is.EqualTo(-2.8).Within(1e-12));
Assert.That(Euclid.Modulus(4.2, -3), Is.EqualTo(-1.8).Within(1e-12));
Assert.That(Euclid.Modulus(6.2, -3), Is.EqualTo(-2.8).Within(1e-12));
Assert.That(Euclid.Modulus(-1.2, -3), Is.EqualTo(-1.2).Within(1e-12));
Assert.That(Euclid.Modulus(-2.2, -3), Is.EqualTo(-2.2).Within(1e-12));
Assert.That(Euclid.Modulus(-3.2, -3), Is.EqualTo(-0.2).Within(1e-12));
Assert.That(Euclid.Modulus(-4.2, -3), Is.EqualTo(-1.2).Within(1e-12));
}
[Test]
public void TestRemainder()
{
Assert.That(Euclid.Remainder(0, 3), Is.EqualTo(0));
Assert.That(Euclid.Remainder(2, 3), Is.EqualTo(2));
Assert.That(Euclid.Remainder(3, 3), Is.EqualTo(0));
Assert.That(Euclid.Remainder(4, 3), Is.EqualTo(1));
Assert.That(Euclid.Remainder(6, 3), Is.EqualTo(0));
Assert.That(Euclid.Remainder(-1, 3), Is.EqualTo(-1));
Assert.That(Euclid.Remainder(-2, 3), Is.EqualTo(-2));
Assert.That(Euclid.Remainder(-3, 3), Is.EqualTo(0));
Assert.That(Euclid.Remainder(-4, 3), Is.EqualTo(-1));
Assert.That(Euclid.Remainder(0, -3), Is.EqualTo(0));
Assert.That(Euclid.Remainder(2, -3), Is.EqualTo(2));
Assert.That(Euclid.Remainder(3, -3), Is.EqualTo(0));
Assert.That(Euclid.Remainder(4, -3), Is.EqualTo(1));
Assert.That(Euclid.Remainder(6, -3), Is.EqualTo(0));
Assert.That(Euclid.Remainder(-1, -3), Is.EqualTo(-1));
Assert.That(Euclid.Remainder(-2, -3), Is.EqualTo(-2));
Assert.That(Euclid.Remainder(-3, -3), Is.EqualTo(0));
Assert.That(Euclid.Remainder(-4, -3), Is.EqualTo(-1));
}
[Test]
public void TestRemainderFloatingPoint()
{
Assert.That(Euclid.Remainder(0.2, 3), Is.EqualTo(0.2).Within(1e-12));
Assert.That(Euclid.Remainder(2.2, 3), Is.EqualTo(2.2).Within(1e-12));
Assert.That(Euclid.Remainder(3.2, 3), Is.EqualTo(0.2).Within(1e-12));
Assert.That(Euclid.Remainder(4.2, 3), Is.EqualTo(1.2).Within(1e-12));
Assert.That(Euclid.Remainder(6.2, 3), Is.EqualTo(0.2).Within(1e-12));
Assert.That(Euclid.Remainder(-1.2, 3), Is.EqualTo(-1.2).Within(1e-12));
Assert.That(Euclid.Remainder(-2.2, 3), Is.EqualTo(-2.2).Within(1e-12));
Assert.That(Euclid.Remainder(-3.2, 3), Is.EqualTo(-0.2).Within(1e-12));
Assert.That(Euclid.Remainder(-4.2, 3), Is.EqualTo(-1.2).Within(1e-12));
Assert.That(Euclid.Remainder(0.2, -3), Is.EqualTo(0.2).Within(1e-12));
Assert.That(Euclid.Remainder(2.2, -3), Is.EqualTo(2.2).Within(1e-12));
Assert.That(Euclid.Remainder(3.2, -3), Is.EqualTo(0.2).Within(1e-12));
Assert.That(Euclid.Remainder(4.2, -3), Is.EqualTo(1.2).Within(1e-12));
Assert.That(Euclid.Remainder(6.2, -3), Is.EqualTo(0.2).Within(1e-12));
Assert.That(Euclid.Remainder(-1.2, -3), Is.EqualTo(-1.2).Within(1e-12));
Assert.That(Euclid.Remainder(-2.2, -3), Is.EqualTo(-2.2).Within(1e-12));
Assert.That(Euclid.Remainder(-3.2, -3), Is.EqualTo(-0.2).Within(1e-12));
Assert.That(Euclid.Remainder(-4.2, -3), Is.EqualTo(-1.2).Within(1e-12));
}
/// <summary>
/// Test even/odd int32.
/// </summary>

202
src/UnitTests/NumberTheoryTests/GcdRelatedTest.cs

@ -1,202 +0,0 @@
// <copyright file="GcdRelatedTest.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com
// Copyright (c) 2009-2010 Math.NET
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
namespace MathNet.Numerics.UnitTests.NumberTheoryTests
{
using System;
using NumberTheory;
using NUnit.Framework;
/// <summary>
/// GreatestCommonDivisor related test.
/// </summary>
[TestFixture, Category("Functions")]
public class GcdRelatedTest
{
/// <summary>
/// GreatestCommonDivisor handles normal input correctly.
/// </summary>
[Test]
public void GcdHandlesNormalInputCorrectly()
{
Assert.AreEqual(0, IntegerTheory.GreatestCommonDivisor(0, 0), "Gcd(0,0)");
Assert.AreEqual(6, IntegerTheory.GreatestCommonDivisor(0, 6), "Gcd(0,6)");
Assert.AreEqual(1, IntegerTheory.GreatestCommonDivisor(7, 13), "Gcd(7,13)");
Assert.AreEqual(7, IntegerTheory.GreatestCommonDivisor(7, 14), "Gcd(7,14)");
Assert.AreEqual(1, IntegerTheory.GreatestCommonDivisor(7, 15), "Gcd(7,15)");
Assert.AreEqual(3, IntegerTheory.GreatestCommonDivisor(6, 15), "Gcd(6,15)");
}
/// <summary>
/// GreatestCommonDivisor handles negative input correctly.
/// </summary>
[Test]
public void GcdHandlesNegativeInputCorrectly()
{
Assert.AreEqual(5, IntegerTheory.GreatestCommonDivisor(-5, 0), "Gcd(-5,0)");
Assert.AreEqual(5, IntegerTheory.GreatestCommonDivisor(0, -5), "Gcd(0, -5)");
Assert.AreEqual(1, IntegerTheory.GreatestCommonDivisor(-7, 15), "Gcd(-7,15)");
Assert.AreEqual(1, IntegerTheory.GreatestCommonDivisor(-7, -15), "Gcd(-7,-15)");
}
/// <summary>
/// GreatestCommonDivisor supports large input.
/// </summary>
[Test]
public void GcdSupportsLargeInput()
{
Assert.AreEqual(Int32.MaxValue, IntegerTheory.GreatestCommonDivisor(0, Int32.MaxValue), "Gcd(0,Int32Max)");
Assert.AreEqual(Int64.MaxValue, IntegerTheory.GreatestCommonDivisor(0, Int64.MaxValue), "Gcd(0,Int64Max)");
Assert.AreEqual(1, IntegerTheory.GreatestCommonDivisor(Int32.MaxValue, Int64.MaxValue), "Gcd(Int32Max,Int64Max)");
Assert.AreEqual(1 << 18, IntegerTheory.GreatestCommonDivisor(1 << 18, 1 << 20), "Gcd(1>>18,1<<20)");
}
/// <summary>
/// Extended GreatestCommonDivisor handles normal input correctly
/// </summary>
[Test]
public void ExtendedGcdHandlesNormalInputCorrectly()
{
long x, y;
Assert.AreEqual(3, IntegerTheory.ExtendedGreatestCommonDivisor(6, 15, out x, out y), "Egcd(6,15)");
Assert.AreEqual(3, (6 * x) + (15 * y), "Egcd(6,15) -> a*x+b*y");
Assert.AreEqual(3, IntegerTheory.ExtendedGreatestCommonDivisor(-6, 15, out x, out y), "Egcd(-6,15)");
Assert.AreEqual(3, (-6 * x) + (15 * y), "Egcd(-6,15) -> a*x+b*y");
Assert.AreEqual(3, IntegerTheory.ExtendedGreatestCommonDivisor(-6, -15, out x, out y), "Egcd(-6,-15)");
Assert.AreEqual(3, (-6 * x) + (-15 * y), "Egcd(-6,-15) -> a*x+b*y");
}
/// <summary>
/// List GreatestCommonDivisor handles normal input Correctly
/// </summary>
[Test]
public void ListGcdHandlesNormalInputCorrectly()
{
Assert.AreEqual(2, IntegerTheory.GreatestCommonDivisor(-10, 6, -8), "Gcd(-10,6,-8)");
Assert.AreEqual(1, IntegerTheory.GreatestCommonDivisor(-10, 6, -8, 5, 9, 13), "Gcd(-10,6,-8,5,9,13)");
Assert.AreEqual(5, IntegerTheory.GreatestCommonDivisor(-10, 20, 120, 60, -15, 1000), "Gcd(-10,20,120,60,-15,1000)");
Assert.AreEqual(3, IntegerTheory.GreatestCommonDivisor(Int64.MaxValue - 1, Int64.MaxValue - 4, Int64.MaxValue - 7), "Gcd(Int64Max-1,Int64Max-4,Int64Max-7)");
Assert.AreEqual(123, IntegerTheory.GreatestCommonDivisor(492, -2 * 492, 492 / 4), "Gcd(492, -984, 123)");
}
/// <summary>
/// List GreatestCommonDivisor handles special input correctly.
/// </summary>
[Test]
public void ListGcdHandlesSpecialInputCorrectly()
{
Assert.AreEqual(0, IntegerTheory.GreatestCommonDivisor(new long[0]), "Gcd()");
Assert.AreEqual(100, IntegerTheory.GreatestCommonDivisor(-100), "Gcd(-100)");
}
/// <summary>
/// List GreatestCommonDivisor checks for <c>null</c> all arguments.
/// </summary>
[Test]
public void ListGcdChecksForNullArguments()
{
Assert.Throws(
typeof(ArgumentNullException),
() => IntegerTheory.GreatestCommonDivisor((long[])null));
}
/// <summary>
/// LeastCommonMultiple handles normal input correctly.
/// </summary>
[Test]
public void LcmHandlesNormalInputCorrectly()
{
Assert.AreEqual(10, IntegerTheory.LeastCommonMultiple(10, 10), "Lcm(10,10)");
Assert.AreEqual(0, IntegerTheory.LeastCommonMultiple(0, 10), "Lcm(0,10)");
Assert.AreEqual(0, IntegerTheory.LeastCommonMultiple(10, 0), "Lcm(10,0)");
Assert.AreEqual(77, IntegerTheory.LeastCommonMultiple(11, 7), "Lcm(11,7)");
Assert.AreEqual(33, IntegerTheory.LeastCommonMultiple(11, 33), "Lcm(11,33)");
Assert.AreEqual(374, IntegerTheory.LeastCommonMultiple(11, 34), "Lcm(11,34)");
}
/// <summary>
/// LeastCommonMultiple handles negative input correctly.
/// </summary>
[Test]
public void LcmHandlesNegativeInputCorrectly()
{
Assert.AreEqual(352, IntegerTheory.LeastCommonMultiple(11, -32), "Lcm(11,-32)");
Assert.AreEqual(352, IntegerTheory.LeastCommonMultiple(-11, 32), "Lcm(-11,32)");
Assert.AreEqual(352, IntegerTheory.LeastCommonMultiple(-11, -32), "Lcm(-11,-32)");
}
/// <summary>
/// LeastCommonMultiple supports large input.
/// </summary>
[Test]
public void LcmSupportsLargeInput()
{
Assert.AreEqual(Int32.MaxValue, IntegerTheory.LeastCommonMultiple(Int32.MaxValue, Int32.MaxValue), "Lcm(Int32Max,Int32Max)");
Assert.AreEqual(Int64.MaxValue, IntegerTheory.LeastCommonMultiple(Int64.MaxValue, Int64.MaxValue), "Lcm(Int64Max,Int64Max)");
Assert.AreEqual(Int64.MaxValue, IntegerTheory.LeastCommonMultiple(-Int64.MaxValue, -Int64.MaxValue), "Lcm(-Int64Max,-Int64Max)");
Assert.AreEqual(Int64.MaxValue, IntegerTheory.LeastCommonMultiple(-Int64.MaxValue, Int64.MaxValue), "Lcm(-Int64Max,Int64Max)");
}
/// <summary>
/// List LeastCommonMultiple handles normal input correctly.
/// </summary>
[Test]
public void ListLcmHandlesNormalInputCorrectly()
{
Assert.AreEqual(120, IntegerTheory.LeastCommonMultiple(-10, 6, -8), "Lcm(-10,6,-8)");
Assert.AreEqual(4680, IntegerTheory.LeastCommonMultiple(-10, 6, -8, 5, 9, 13), "Lcm(-10,6,-8,5,9,13)");
Assert.AreEqual(3000, IntegerTheory.LeastCommonMultiple(-10, 20, 120, 60, -15, 1000), "Lcm(-10,20,120,60,-15,1000)");
Assert.AreEqual(984, IntegerTheory.LeastCommonMultiple(492, -2 * 492, 492 / 4), "Lcm(492, -984, 123)");
Assert.AreEqual(2016, IntegerTheory.LeastCommonMultiple(32, 42, 36, 18), "Lcm(32,42,36,18)");
}
/// <summary>
/// List LeastCommonMultiple handles special input correctly.
/// </summary>
[Test]
public void ListLcmHandlesSpecialInputCorrectly()
{
Assert.AreEqual(1, IntegerTheory.LeastCommonMultiple(new long[0]), "Lcm()");
Assert.AreEqual(100, IntegerTheory.LeastCommonMultiple(-100), "Lcm(-100)");
}
/// <summary>
/// List LeastCommonMultiple checks for <c>null</c> arguments.
/// </summary>
[Test]
public void ListLcmChecksForNullArguments()
{
Assert.Throws(
typeof(ArgumentNullException),
() => IntegerTheory.LeastCommonMultiple((long[])null));
}
}
}

222
src/UnitTests/NumberTheoryTests/GcdRelatedTestBigInteger.cs

@ -1,222 +0,0 @@
// <copyright file="GcdRelatedTestBigInteger.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com
// Copyright (c) 2009-2010 Math.NET
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
#if !NOSYSNUMERICS
using System;
using System.Numerics;
using MathNet.Numerics.NumberTheory;
using NUnit.Framework;
namespace MathNet.Numerics.UnitTests.NumberTheoryTests
{
/// <summary>
/// GreatestCommonDivisor related test for <c>BigInteger</c>.
/// </summary>
[TestFixture, Category("Functions")]
public class GcdRelatedTestBigInteger
{
/// <summary>
/// GreatestCommonDivisor handles normal input correctly.
/// </summary>
[Test]
public void GcdHandlesNormalInputCorrectly()
{
Assert.AreEqual((BigInteger)0, IntegerTheory.GreatestCommonDivisor(BigInteger.Zero, BigInteger.Zero), "Gcd(0,0)");
Assert.AreEqual((BigInteger)6, IntegerTheory.GreatestCommonDivisor(BigInteger.Zero, 6), "Gcd(0,6)");
Assert.AreEqual((BigInteger)1, IntegerTheory.GreatestCommonDivisor((BigInteger)7, 13), "Gcd(7,13)");
Assert.AreEqual((BigInteger)7, IntegerTheory.GreatestCommonDivisor((BigInteger)7, 14), "Gcd(7,14)");
Assert.AreEqual((BigInteger)1, IntegerTheory.GreatestCommonDivisor((BigInteger)7, 15), "Gcd(7,15)");
Assert.AreEqual((BigInteger)3, IntegerTheory.GreatestCommonDivisor((BigInteger)6, 15), "Gcd(6,15)");
}
/// <summary>
/// GreatestCommonDivisor handles negative input correctly.
/// </summary>
[Test]
public void GcdHandlesNegativeInputCorrectly()
{
Assert.AreEqual((BigInteger)5, IntegerTheory.GreatestCommonDivisor((BigInteger)(-5), 0), "Gcd(-5,0)");
Assert.AreEqual((BigInteger)5, IntegerTheory.GreatestCommonDivisor(BigInteger.Zero, -5), "Gcd(0, -5)");
Assert.AreEqual((BigInteger)1, IntegerTheory.GreatestCommonDivisor((BigInteger)(-7), 15), "Gcd(-7,15)");
Assert.AreEqual((BigInteger)1, IntegerTheory.GreatestCommonDivisor((BigInteger)(-7), -15), "Gcd(-7,-15)");
}
/// <summary>
/// GreatestCommonDivisor supports large input.
/// </summary>
[Test]
public void GcdSupportsLargeInput()
{
Assert.AreEqual((BigInteger)Int32.MaxValue, IntegerTheory.GreatestCommonDivisor(BigInteger.Zero, Int32.MaxValue), "Gcd(0,Int32Max)");
Assert.AreEqual((BigInteger)Int64.MaxValue, IntegerTheory.GreatestCommonDivisor(BigInteger.Zero, Int64.MaxValue), "Gcd(0,Int64Max)");
Assert.AreEqual((BigInteger)1, IntegerTheory.GreatestCommonDivisor((BigInteger)Int32.MaxValue, Int64.MaxValue), "Gcd(Int32Max,Int64Max)");
Assert.AreEqual((BigInteger)(1 << 18), IntegerTheory.GreatestCommonDivisor((BigInteger)(1 << 18), 1 << 20), "Gcd(1>>18,1<<20)");
Assert.AreEqual((BigInteger)(1 << 18), IntegerTheory.GreatestCommonDivisor((BigInteger)(1 << 18), 1 << 20), "Gcd(1>>18,1<<20)");
#if !PORTABLE
Assert.AreEqual((BigInteger)4569031055798, IntegerTheory.GreatestCommonDivisor(BigInteger.Parse("7305316061155559483748611586449542122662"), BigInteger.Parse("57377277362010117405715236427413896")), "Gcd(large)");
#endif
}
/// <summary>
/// Extended GreatestCommonDivisor handles normal input correctly.
/// </summary>
[Test]
public void ExtendedGcdHandlesNormalInputCorrectly()
{
BigInteger x, y;
Assert.AreEqual((BigInteger)3, IntegerTheory.ExtendedGreatestCommonDivisor(6, 15, out x, out y), "Egcd(6,15)");
Assert.AreEqual((BigInteger)3, (6 * x) + (15 * y), "Egcd(6,15) -> a*x+b*y");
Assert.AreEqual((BigInteger)3, IntegerTheory.ExtendedGreatestCommonDivisor(-6, 15, out x, out y), "Egcd(-6,15)");
Assert.AreEqual((BigInteger)3, (-6 * x) + (15 * y), "Egcd(-6,15) -> a*x+b*y");
Assert.AreEqual((BigInteger)3, IntegerTheory.ExtendedGreatestCommonDivisor(-6, -15, out x, out y), "Egcd(-6,-15)");
Assert.AreEqual((BigInteger)3, (-6 * x) + (-15 * y), "Egcd(-6,-15) -> a*x+b*y");
#if !PORTABLE
var a = BigInteger.Parse("7305316061155559483748611586449542122662");
var b = BigInteger.Parse("57377277362010117405715236427413896");
Assert.AreEqual((BigInteger)4569031055798, IntegerTheory.ExtendedGreatestCommonDivisor(a, b, out x, out y), "Egcd(large)");
Assert.AreEqual((BigInteger)4569031055798, (a * x) + (b * y), "Egcd(large) -> a*x+b*y");
Assert.AreEqual((BigInteger)4569031055798, IntegerTheory.ExtendedGreatestCommonDivisor(-a, b, out x, out y), "Egcd(-large)");
Assert.AreEqual((BigInteger)4569031055798, (-a * x) + (b * y), "Egcd(-large) -> a*x+b*y");
#endif
}
/// <summary>
/// List GreatestCommonDivisor handles normal input Correctly
/// </summary>
[Test]
public void ListGcdHandlesNormalInputCorrectly()
{
Assert.AreEqual((BigInteger)2, IntegerTheory.GreatestCommonDivisor((BigInteger)(-10), 6, -8), "Gcd(-10,6,-8)");
Assert.AreEqual((BigInteger)1, IntegerTheory.GreatestCommonDivisor((BigInteger)(-10), 6, -8, 5, 9, 13), "Gcd(-10,6,-8,5,9,13)");
Assert.AreEqual((BigInteger)5, IntegerTheory.GreatestCommonDivisor((BigInteger)(-10), 20, 120, 60, -15, 1000), "Gcd(-10,20,120,60,-15,1000)");
Assert.AreEqual((BigInteger)3, IntegerTheory.GreatestCommonDivisor((BigInteger)(Int64.MaxValue - 1), Int64.MaxValue - 4, Int64.MaxValue - 7), "Gcd(Int64Max-1,Int64Max-4,Int64Max-7)");
Assert.AreEqual((BigInteger)123, IntegerTheory.GreatestCommonDivisor((BigInteger)492, -2 * 492, 492 / 4), "Gcd(492, -984, 123)");
}
/// <summary>
/// List GreatestCommonDivisor handles special input correctly.
/// </summary>
[Test]
public void ListGcdHandlesSpecialInputCorrectly()
{
Assert.AreEqual((BigInteger)0, IntegerTheory.GreatestCommonDivisor(new BigInteger[0]), "Gcd()");
Assert.AreEqual((BigInteger)100, IntegerTheory.GreatestCommonDivisor((BigInteger)(-100)), "Gcd(-100)");
}
/// <summary>
/// List GreatestCommonDivisor checks for <c>null</c> all arguments.
/// </summary>
[Test]
public void ListGcdChecksForNullArguments()
{
Assert.Throws(
typeof(ArgumentNullException),
() => IntegerTheory.GreatestCommonDivisor((BigInteger[])null));
}
/// <summary>
/// LeastCommonMultiple handles normal input correctly.
/// </summary>
[Test]
public void LcmHandlesNormalInputCorrectly()
{
Assert.AreEqual((BigInteger)10, IntegerTheory.LeastCommonMultiple((BigInteger)10, 10), "Lcm(10,10)");
Assert.AreEqual((BigInteger)0, IntegerTheory.LeastCommonMultiple(BigInteger.Zero, 10), "Lcm(0,10)");
Assert.AreEqual((BigInteger)0, IntegerTheory.LeastCommonMultiple((BigInteger)10, 0), "Lcm(10,0)");
Assert.AreEqual((BigInteger)77, IntegerTheory.LeastCommonMultiple((BigInteger)11, 7), "Lcm(11,7)");
Assert.AreEqual((BigInteger)33, IntegerTheory.LeastCommonMultiple((BigInteger)11, 33), "Lcm(11,33)");
Assert.AreEqual((BigInteger)374, IntegerTheory.LeastCommonMultiple((BigInteger)11, 34), "Lcm(11,34)");
}
/// <summary>
/// LeastCommonMultiple handles negative input correctly.
/// </summary>
[Test]
public void LcmHandlesNegativeInputCorrectly()
{
Assert.AreEqual((BigInteger)352, IntegerTheory.LeastCommonMultiple((BigInteger)11, -32), "Lcm(11,-32)");
Assert.AreEqual((BigInteger)352, IntegerTheory.LeastCommonMultiple((BigInteger)(-11), 32), "Lcm(-11,32)");
Assert.AreEqual((BigInteger)352, IntegerTheory.LeastCommonMultiple((BigInteger)(-11), -32), "Lcm(-11,-32)");
}
/// <summary>
/// LeastCommonMultiple supports large input.
/// </summary>
[Test]
public void LcmSupportsLargeInput()
{
Assert.AreEqual((BigInteger)Int32.MaxValue, IntegerTheory.LeastCommonMultiple((BigInteger)Int32.MaxValue, Int32.MaxValue), "Lcm(Int32Max,Int32Max)");
Assert.AreEqual((BigInteger)Int64.MaxValue, IntegerTheory.LeastCommonMultiple((BigInteger)Int64.MaxValue, Int64.MaxValue), "Lcm(Int64Max,Int64Max)");
Assert.AreEqual((BigInteger)Int64.MaxValue, IntegerTheory.LeastCommonMultiple((BigInteger)(-Int64.MaxValue), -Int64.MaxValue), "Lcm(-Int64Max,-Int64Max)");
Assert.AreEqual((BigInteger)Int64.MaxValue, IntegerTheory.LeastCommonMultiple((BigInteger)(-Int64.MaxValue), Int64.MaxValue), "Lcm(-Int64Max,Int64Max)");
#if !PORTABLE
Assert.AreEqual(BigInteger.Parse("91739176367857263082719902034485224119528064014300888465614024"), IntegerTheory.LeastCommonMultiple(BigInteger.Parse("7305316061155559483748611586449542122662"), BigInteger.Parse("57377277362010117405715236427413896")), "Lcm(large)");
#endif
}
/// <summary>
/// List LeastCommonMultiple handles normal input correctly.
/// </summary>
[Test]
public void ListLcmHandlesNormalInputCorrectly()
{
Assert.AreEqual((BigInteger)120, IntegerTheory.LeastCommonMultiple((BigInteger)(-10), 6, -8), "Lcm(-10,6,-8)");
Assert.AreEqual((BigInteger)4680, IntegerTheory.LeastCommonMultiple((BigInteger)(-10), 6, -8, 5, 9, 13), "Lcm(-10,6,-8,5,9,13)");
Assert.AreEqual((BigInteger)3000, IntegerTheory.LeastCommonMultiple((BigInteger)(-10), 20, 120, 60, -15, 1000), "Lcm(-10,20,120,60,-15,1000)");
Assert.AreEqual((BigInteger)984, IntegerTheory.LeastCommonMultiple((BigInteger)492, -2 * 492, 492 / 4), "Lcm(492, -984, 123)");
Assert.AreEqual((BigInteger)2016, IntegerTheory.LeastCommonMultiple((BigInteger)32, 42, 36, 18), "Lcm(32,42,36,18)");
}
/// <summary>
/// List LeastCommonMultiple handles special input correctly.
/// </summary>
[Test]
public void ListLcmHandlesSpecialInputCorrectly()
{
Assert.AreEqual((BigInteger)1, IntegerTheory.LeastCommonMultiple(new BigInteger[0]), "Lcm()");
Assert.AreEqual((BigInteger)100, IntegerTheory.LeastCommonMultiple((BigInteger)(-100)), "Lcm(-100)");
}
/// <summary>
/// List LeastCommonMultiple checks for <c>null</c> arguments.
/// </summary>
[Test]
public void ListLcmChecksForNullArguments()
{
Assert.Throws(
typeof(ArgumentNullException),
() => IntegerTheory.LeastCommonMultiple((BigInteger[])null));
}
}
}
#endif

6
src/UnitTests/UnitTests.csproj

@ -355,9 +355,9 @@
<Compile Include="LinearAlgebraTests\Double\VectorArithmeticTheory.cs" />
<Compile Include="LinearAlgebraTests\VectorArithmeticTheory.cs" />
<Compile Include="MatrixHelpers.cs" />
<Compile Include="NumberTheoryTests\GcdRelatedTest.cs" />
<Compile Include="NumberTheoryTests\GcdRelatedTestBigInteger.cs" />
<Compile Include="NumberTheoryTests\IntegerTheoryTest.cs" />
<Compile Include="EuclidTests\GcdRelatedTest.cs" />
<Compile Include="EuclidTests\GcdRelatedTestBigInteger.cs" />
<Compile Include="EuclidTests\IntegerTheoryTest.cs" />
<Compile Include="RootFindingTests\BisectionTest.cs" />
<Compile Include="PermutationTest.cs" />
<Compile Include="PrecisionTest.cs" />

Loading…
Cancel
Save