Browse Source

Constants: sqrt(3) #194

pull/197/head
Christoph Ruegg 12 years ago
parent
commit
efd0b14ae2
  1. 3
      src/Numerics/Constants.cs
  2. 2
      src/Numerics/RootFinding/Cubic.cs
  3. 2
      src/UnitTests/ComplexTests/Complex32Test.cs
  4. 16
      src/UnitTests/RootFindingTests/RobustNewtonRaphsonTest.cs

3
src/Numerics/Constants.cs

@ -67,6 +67,9 @@ namespace MathNet.Numerics
/// <summary>The number sqrt(2)</summary>
public const double Sqrt2 = 1.4142135623730950488016887242096980785696718753769d;
/// <summary>The number sqrt(3)</summary>
public const double Sqrt3 = 1.7320508075688772935274463415058723669428052538104d;
/// <summary>The number sqrt(1/2) = 1/sqrt(2) = sqrt(2)/2</summary>
public const double Sqrt1Over2 = 0.70710678118654752440084436210484903928483593768845d;

2
src/Numerics/RootFinding/Cubic.cs

@ -82,7 +82,7 @@ namespace MathNet.Numerics.RootFinding
var S = Complex.Pow(R + rootD, 1d / 3d);
var T = Complex.Pow(R - rootD, 1d / 3d);
var shift = -a2 / 3d;
var sharedI = 0.5 * Complex.ImaginaryOne * Math.Sqrt(3) * (S - T);
var sharedI = 0.5 * Complex.ImaginaryOne * Constants.Sqrt3 * (S - T);
var x1 = shift + (S + T);
var x2 = shift - 0.5 * (S + T);

2
src/UnitTests/ComplexTests/Complex32Test.cs

@ -290,7 +290,7 @@ namespace MathNet.Numerics.UnitTests.ComplexTests
public void CanCreateComplexNumberWithModulusArgument()
{
var complex = Complex32.FromPolarCoordinates(2, (float)-Math.PI / 6);
Assert.AreEqual((float)Math.Sqrt(3), complex.Real, 1e-7f, "Real part is Sqrt(3).");
Assert.AreEqual((float)Constants.Sqrt3, complex.Real, 1e-7f, "Real part is Sqrt(3).");
Assert.AreEqual(-1.0f, complex.Imaginary, 1e-7f, "Imaginary part is -1.");
}

16
src/UnitTests/RootFindingTests/RobustNewtonRaphsonTest.cs

@ -86,17 +86,17 @@ namespace MathNet.Numerics.UnitTests.RootFindingTests
Func<double, double> f3 = x => 1/(x - 2) + x + 2;
Func<double, double> df3 = x => -1/(x*x - 4*x + 4) + 1;
Assert.AreEqual(-Math.Sqrt(3), RobustNewtonRaphson.FindRoot(f3, df3, -2, -1, 1e-14, 100, 20), 1e-14);
Assert.AreEqual(Math.Sqrt(3), RobustNewtonRaphson.FindRoot(f3, df3, 1, 1.99, 1e-14, 100, 20));
Assert.AreEqual(Math.Sqrt(3), RobustNewtonRaphson.FindRoot(f3, df3, -1.5, 1.99, 1e-14, 100, 20));
Assert.AreEqual(Math.Sqrt(3), RobustNewtonRaphson.FindRoot(f3, df3, 1, 6, 1e-14, 100, 20));
Assert.AreEqual(-Constants.Sqrt3, RobustNewtonRaphson.FindRoot(f3, df3, -2, -1, 1e-14, 100, 20), 1e-14);
Assert.AreEqual(Constants.Sqrt3, RobustNewtonRaphson.FindRoot(f3, df3, 1, 1.99, 1e-14, 100, 20));
Assert.AreEqual(Constants.Sqrt3, RobustNewtonRaphson.FindRoot(f3, df3, -1.5, 1.99, 1e-14, 100, 20));
Assert.AreEqual(Constants.Sqrt3, RobustNewtonRaphson.FindRoot(f3, df3, 1, 6, 1e-14, 100, 20));
Func<double, double> f4 = x => 1/(2 - x) - x + 6;
Func<double, double> df4 = x => 1/(x*x - 4*x + 4) - 1;
Assert.AreEqual(4 + Math.Sqrt(3), RobustNewtonRaphson.FindRoot(f4, df4, 5, 6, 1e-14, 100, 20), 1e-14);
Assert.AreEqual(4 - Math.Sqrt(3), RobustNewtonRaphson.FindRoot(f4, df4, 2.01, 3, 1e-14, 100, 20));
Assert.AreEqual(4 - Math.Sqrt(3), RobustNewtonRaphson.FindRoot(f4, df4, 2.01, 5, 1e-14, 100, 20));
Assert.AreEqual(4 - Math.Sqrt(3), RobustNewtonRaphson.FindRoot(f4, df4, -2, 4, 1e-14, 100, 20));
Assert.AreEqual(4 + Constants.Sqrt3, RobustNewtonRaphson.FindRoot(f4, df4, 5, 6, 1e-14, 100, 20), 1e-14);
Assert.AreEqual(4 - Constants.Sqrt3, RobustNewtonRaphson.FindRoot(f4, df4, 2.01, 3, 1e-14, 100, 20));
Assert.AreEqual(4 - Constants.Sqrt3, RobustNewtonRaphson.FindRoot(f4, df4, 2.01, 5, 1e-14, 100, 20));
Assert.AreEqual(4 - Constants.Sqrt3, RobustNewtonRaphson.FindRoot(f4, df4, -2, 4, 1e-14, 100, 20));
}
[Test]

Loading…
Cancel
Save