Browse Source

number theory: ported from iridium, as needed by quadrature

Signed-off-by: Christoph Ruegg <git@cdrnet.ch>
pull/2/head
Christoph Ruegg 17 years ago
parent
commit
1fe9b3dea0
  1. 1
      src/Managed.UnitTests/Managed.UnitTests.csproj
  2. 136
      src/Managed.UnitTests/NumberTheoryTests/IntegerTheoryTest.cs
  3. 1
      src/Managed/Managed.csproj
  4. 126
      src/Managed/NumberTheory/IntegerTheory.cs
  5. 5
      src/Native.UnitTests/Native.UnitTests.csproj
  6. 3
      src/Native/Native.csproj

1
src/Managed.UnitTests/Managed.UnitTests.csproj

@ -64,6 +64,7 @@
<Compile Include="DistributionTests\Continuous\NormalTests.cs" />
<Compile Include="InterpolationTests\InterpolationContract.cs" />
<Compile Include="InterpolationTests\InterpolationTest.cs" />
<Compile Include="NumberTheoryTests\IntegerTheoryTest.cs" />
<Compile Include="PrecisionTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SpecialFunctionsTest\ErfTests.cs" />

136
src/Managed.UnitTests/NumberTheoryTests/IntegerTheoryTest.cs

@ -0,0 +1,136 @@
// <copyright file="IntegerTheoryTest.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://mathnet.opensourcedotnet.info
//
// Copyright (c) 2009 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 MbUnit.Framework;
[TestFixture]
public class IntegerTheoryTest
{
[Test]
public void TestEvenOdd32()
{
Assert.IsTrue(IntegerTheory.IsEven(0), "0 is even");
Assert.IsFalse(IntegerTheory.IsOdd(0), "0 is not odd");
Assert.IsFalse(IntegerTheory.IsEven(1), "1 is not even");
Assert.IsTrue(IntegerTheory.IsOdd(1), "1 is odd");
Assert.IsFalse(IntegerTheory.IsEven(-1), "-1 is not even");
Assert.IsTrue(IntegerTheory.IsOdd(-1), "-1 is odd");
Assert.IsFalse(IntegerTheory.IsEven(Int32.MaxValue), "Int32.Max is not even");
Assert.IsTrue(IntegerTheory.IsOdd(Int32.MaxValue), "Int32.Max is odd");
Assert.IsTrue(IntegerTheory.IsEven(Int32.MinValue), "Int32.Min is even");
Assert.IsFalse(IntegerTheory.IsOdd(Int32.MinValue), "Int32.Min is not odd");
}
[Test]
public void TestEvenOdd64()
{
Assert.IsTrue(IntegerTheory.IsEven((long)0), "0 is even");
Assert.IsFalse(IntegerTheory.IsOdd((long)0), "0 is not odd");
Assert.IsFalse(IntegerTheory.IsEven((long)1), "1 is not even");
Assert.IsTrue(IntegerTheory.IsOdd((long)1), "1 is odd");
Assert.IsFalse(IntegerTheory.IsEven((long)-1), "-1 is not even");
Assert.IsTrue(IntegerTheory.IsOdd((long)-1), "-1 is odd");
Assert.IsFalse(IntegerTheory.IsEven(Int64.MaxValue), "Int64.Max is not even");
Assert.IsTrue(IntegerTheory.IsOdd(Int64.MaxValue), "Int64.Max is odd");
Assert.IsTrue(IntegerTheory.IsEven(Int64.MinValue), "Int64.Min is even");
Assert.IsFalse(IntegerTheory.IsOdd(Int64.MinValue), "Int64.Min is not odd");
}
[Test]
public void TestIsPerfectSquare32()
{
// Test all known suares
int lastRadix = (int)Math.Floor(Math.Sqrt(Int32.MaxValue));
for (int i = 0; i <= lastRadix; i++)
{
Assert.IsTrue(IntegerTheory.IsPerfectSquare(i * i), i + "^2 (+)");
}
// Test 1-offset from all known squares
for (int i = 2; i <= lastRadix; i++)
{
Assert.IsFalse(IntegerTheory.IsPerfectSquare((i * i) - 1), i + "^2-1 (-)");
Assert.IsFalse(IntegerTheory.IsPerfectSquare((i * i) + 1), i + "^2+1 (-)");
}
// Selected Cases
Assert.IsTrue(IntegerTheory.IsPerfectSquare(100000000), "100000000 (+)");
Assert.IsFalse(IntegerTheory.IsPerfectSquare(100000001), "100000001 (-)");
Assert.IsFalse(IntegerTheory.IsPerfectSquare(99999999), "99999999 (-)");
Assert.IsFalse(IntegerTheory.IsPerfectSquare(-4), "-4 (-)");
Assert.IsFalse(IntegerTheory.IsPerfectSquare(Int32.MinValue), "Int32.MinValue (-)");
Assert.IsFalse(IntegerTheory.IsPerfectSquare(Int32.MaxValue), "Int32.MaxValue (-)");
Assert.IsTrue(IntegerTheory.IsPerfectSquare(1), "1 (+)");
Assert.IsTrue(IntegerTheory.IsPerfectSquare(0), "0 (+)");
Assert.IsFalse(IntegerTheory.IsPerfectSquare(-1), "-1 (-)");
}
[Test]
public void TestIsPerfectSquare64()
{
// Test all known suares
for (int i = 0; i < 32; i++)
{
long t = ((long)1) << i;
Assert.IsTrue(IntegerTheory.IsPerfectSquare(t * t), t + "^2 (+)");
}
// Test 1-offset from all known squares
for (int i = 1; i < 32; i++)
{
long t = ((long)1) << i;
Assert.IsFalse(IntegerTheory.IsPerfectSquare((t * t) - 1), t + "^2-1 (-)");
Assert.IsFalse(IntegerTheory.IsPerfectSquare((t * t) + 1), t + "^2+1 (-)");
}
// Selected Cases
Assert.IsTrue(IntegerTheory.IsPerfectSquare((long)1000000000000000000), "1000000000000000000 (+)");
Assert.IsFalse(IntegerTheory.IsPerfectSquare((long)1000000000000000001), "1000000000000000001 (-)");
Assert.IsFalse(IntegerTheory.IsPerfectSquare((long)999999999999999999), "999999999999999999 (-)");
Assert.IsFalse(IntegerTheory.IsPerfectSquare((long)999999999999999993), "999999999999999993 (-)");
Assert.IsFalse(IntegerTheory.IsPerfectSquare((long)-4), "-4 (-)");
Assert.IsFalse(IntegerTheory.IsPerfectSquare(Int64.MinValue), "Int32.MinValue (-)");
Assert.IsFalse(IntegerTheory.IsPerfectSquare(Int64.MaxValue), "Int32.MaxValue (-)");
Assert.IsTrue(IntegerTheory.IsPerfectSquare((long)1), "1 (+)");
Assert.IsTrue(IntegerTheory.IsPerfectSquare((long)0), "0 (+)");
Assert.IsFalse(IntegerTheory.IsPerfectSquare((long)-1), "-1 (-)");
}
}
}

1
src/Managed/Managed.csproj

@ -65,6 +65,7 @@
<Compile Include="Interpolation\IInterpolation.cs" />
<Compile Include="Interpolation\Interpolate.cs" />
<Compile Include="Interpolation\SplineBoundaryCondition.cs" />
<Compile Include="NumberTheory\IntegerTheory.cs" />
<Compile Include="Precision.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\Resources.Designer.cs">

126
src/Managed/NumberTheory/IntegerTheory.cs

@ -0,0 +1,126 @@
// <copyright file="IntegerTheory.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://mathnet.opensourcedotnet.info
//
// Copyright (c) 2009 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 for Integers
/// </summary>
public static class IntegerTheory
{
/// <summary>
/// Find out whether the provided 32 bit integer is an even number.
/// </summary>
/// <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>
/// <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>
/// <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>
/// <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 square, i.e. a square of an integer.
/// </summary>
/// <returns>True if and only if it is a perfect square.</returns>
public static bool IsPerfectSquare(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>
/// <returns>True if and only if it is a perfect square.</returns>
public static bool IsPerfectSquare(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;
}
}
}

5
src/Native.UnitTests/Native.UnitTests.csproj

@ -78,12 +78,15 @@
<Compile Include="..\Managed.UnitTests\InterpolationTests\InterpolationTest.cs">
<Link>InterpolationTests\InterpolationTest.cs</Link>
</Compile>
<Compile Include="..\Managed.UnitTests\NumberTheoryTests\IntegerTheoryTest.cs">
<Link>NumberTheoryTests\IntegerTheoryTest.cs</Link>
</Compile>
<Compile Include="..\Managed.UnitTests\PrecisionTest.cs">
<Link>PrecisionTest.cs</Link>
</Compile>
<Compile Include="..\Managed.UnitTests\SpecialFunctionsTest\ErfTests.cs">
<Link>SpecialFunctionsTest\ErfTests.cs</Link>
</Compile>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>

3
src/Native/Native.csproj

@ -104,6 +104,9 @@
<Compile Include="..\Managed\Interpolation\SplineBoundaryCondition.cs">
<Link>Interpolation\SplineBoundaryCondition.cs</Link>
</Compile>
<Compile Include="..\Managed\NumberTheory\IntegerTheory.cs">
<Link>NumberTheory\IntegerTheory.cs</Link>
</Compile>
<Compile Include="..\Managed\Precision.cs">
<Link>Precision.cs</Link>
</Compile>

Loading…
Cancel
Save