/// <summary>Compute bits of Pi in the local machine.</summary> public static double ComputePi(long b) { double pi = 0; foreach (Bellard.Parameter p in Bellard.Parameter.Values()) { pi = Modular.AddMod(pi, new Bellard.Sum(b, p, 1, null).GetValue()); } return(pi); }
// N' // R - 1 /// <summary>Set the modular and initialize this object.</summary> internal virtual Montgomery Set(long n) { if (n % 2 != 1) { throw new ArgumentException("n % 2 != 1, n=" + n); } N = n; R = long.HighestOneBit(n) << 1; NI = R - Modular.ModInverse(N, R); R1 = R - 1; s = long.NumberOfTrailingZeros(R); return(this); }
/// <summary> /// Compute the value using /// <see cref="Montgomery.Mod(long)"/> /// . /// </summary> internal virtual double Compute_montgomery() { long e = E.value; long n = N.value; double s = 0; for (; e > E.limit; e += E.delta) { s = Modular.AddMod(s, montgomery.Set(n).Mod(e) / (double)n); n += N.delta; } return(s); }
/// <summary> /// Compute the value using /// <see cref="Modular.Mod(long, long)"/> /// . /// </summary> internal virtual double Compute_modular() { long e = E.value; long n = N.value; double s = 0; for (; e > E.limit; e += E.delta) { s = Modular.AddMod(s, Modular.Mod(e, n) / (double)n); n += N.delta; } return(s); }
internal virtual double Compute_modPow() { long e = E.value; long n = N.value; double s = 0; for (; e > E.limit; e += E.delta) { s = Modular.AddMod(s, Two.ModPow(BigInteger.ValueOf(e), BigInteger.ValueOf(n)) / n); n += N.delta; } return(s); }
/// <summary>get the value of sigma</summary> public virtual double GetValue() { if (sigma.GetValue() == null) { double d = 0; for (int i = 0; i < parts.Length; i++) { d = Modular.AddMod(d, parts[i].Compute()); } sigma.SetValue(d); } double s = Modular.AddMod(sigma.GetValue(), tail.Compute()); return(parameter.isplus ? s : -s); }
/// <summary> /// <inheritDoc/> /// /// </summary> public virtual Summation GetElement() { if (sigma.GetValue() == null) { int i = 0; double d = 0; for (; i < parts.Length && parts[i].GetValue() != null; i++) { d = Modular.AddMod(d, parts[i].GetValue()); } if (i == parts.Length) { sigma.SetValue(d); } } return(sigma); }
/// <summary> /// <inheritDoc/> /// /// </summary> public virtual Org.Apache.Hadoop.Examples.PI.Math.Summation Combine(Org.Apache.Hadoop.Examples.PI.Math.Summation that) { if (this.N.delta != that.N.delta || this.E.delta != that.E.delta) { throw new ArgumentException("this.N.delta != that.N.delta || this.E.delta != that.E.delta" + ",\n this=" + this + ",\n that=" + that); } if (this.E.limit == that.E.value && this.N.limit == that.N.value) { double v = Modular.AddMod(this.value, that.value); Org.Apache.Hadoop.Examples.PI.Math.Summation s = new Org.Apache.Hadoop.Examples.PI.Math.Summation (new ArithmeticProgression(N.symbol, N.value, N.delta, that.N.limit), new ArithmeticProgression (E.symbol, E.value, E.delta, that.E.limit)); s.SetValue(v); return(s); } return(null); }
/// <summary>Compute bits of Pi from the results.</summary> public static double ComputePi <T>(long b, IDictionary <Bellard.Parameter, T> results ) where T : Container <Summation> { if (results.Count != Bellard.Parameter.Values().Length) { throw new ArgumentException("m.size() != Parameter.values().length" + ", m.size()=" + results.Count + "\n m=" + results); } double pi = 0; foreach (Bellard.Parameter p in Bellard.Parameter.Values()) { Summation sigma = results[p].GetElement(); Bellard.Sum s = new Bellard.Sum(b, p, 1, null); s.SetValue(sigma); pi = Modular.AddMod(pi, s.GetValue()); } return(pi); }
internal static void ModBenchmarks() { Util.Timer t = new Util.Timer(false); t.Tick("modBenchmarks()"); long[][][] en = GenerateEN(10000, 10); t.Tick("generateEN"); for (int i = 0; i < en.Length; i++) { long n = en[i][0][0]; for (int j = 1; j < en[i].Length; j++) { long e = en[i][j][0]; long answer = en[i][j][1]; long s = Modular.Mod(e, n); if (s != answer) { NUnit.Framework.Assert.AreEqual("e=" + e + ", n=" + n + ", answer=" + answer + " but s=" + s, answer, s); } } } t.Tick("Modular.mod"); TestModular.Montgomery2 m2 = new TestModular.Montgomery2(); for (int i_1 = 0; i_1 < en.Length; i_1++) { long n = en[i_1][0][0]; m2.Set(n); for (int j = 1; j < en[i_1].Length; j++) { long e = en[i_1][j][0]; long answer = en[i_1][j][1]; long s = m2.Mod(e); if (s != answer) { NUnit.Framework.Assert.AreEqual("e=" + e + ", n=" + n + ", answer=" + answer + " but s=" + s, answer, s); } } } t.Tick("montgomery.mod"); for (int i_2 = 0; i_2 < en.Length; i_2++) { long n = en[i_2][0][0]; m2.Set(n); for (int j = 1; j < en[i_2].Length; j++) { long e = en[i_2][j][0]; long answer = en[i_2][j][1]; long s = m2.Mod2(e); if (s != answer) { NUnit.Framework.Assert.AreEqual("e=" + e + ", n=" + n + ", answer=" + answer + " but s=" + s, answer, s); } } } t.Tick("montgomery.mod2"); for (int i_3 = 0; i_3 < en.Length; i_3++) { long n = en[i_3][0][0]; BigInteger N = BigInteger.ValueOf(n); for (int j = 1; j < en[i_3].Length; j++) { long e = en[i_3][j][0]; long answer = en[i_3][j][1]; long s = Two.ModPow(BigInteger.ValueOf(e), N); if (s != answer) { NUnit.Framework.Assert.AreEqual("e=" + e + ", n=" + n + ", answer=" + answer + " but s=" + s, answer, s); } } } t.Tick("BigInteger.modPow(e, n)"); }