public void FitsAtArbitraryPointsWithMaple(double t, double x, double maxAbsoluteError) { IInterpolation interpolation = new NevillePolynomialInterpolation(_t, _x); Assert.AreEqual(x, interpolation.Interpolate(t), maxAbsoluteError, "Interpolation at {0}", t); var actual = interpolation.DifferentiateAll(t); Assert.AreEqual(x, actual.Item1, maxAbsoluteError, "Interpolation as by-product of differentiation at {0}", t); }
public void FitsAtArbitraryPointsWithMaple(double t, double x, double maxAbsoluteError) { IInterpolation interpolation = new NevillePolynomialInterpolation(_t, _x); Assert.AreEqual(x, interpolation.Interpolate(t), maxAbsoluteError, "Interpolation at {0}", t); double interpolatedValue; double secondDerivative; interpolation.Differentiate(t, out interpolatedValue, out secondDerivative); Assert.AreEqual(x, interpolatedValue, maxAbsoluteError, "Interpolation as by-product of differentiation at {0}", t); }
public void SupportsLinearCase(int samples) { double[] x, y, xtest, ytest; LinearInterpolationCase.Build(out x, out y, out xtest, out ytest, samples); IInterpolation interpolation = new NevillePolynomialInterpolation(x, y); for (int i = 0; i < xtest.Length; i++) { Assert.AreEqual(ytest[i], interpolation.Interpolate(xtest[i]), 1e-13, "Linear with {0} samples, sample {1}", samples, i); } }
public void FitsAtSamplePoints() { IInterpolation interpolation = new NevillePolynomialInterpolation(_t, _x); for (int i = 0; i < _x.Length; i++) { Assert.AreEqual(_x[i], interpolation.Interpolate(_t[i]), "A Exact Point " + i); var actual = interpolation.DifferentiateAll(_t[i]); Assert.AreEqual(_x[i], actual.Item1, "B Exact Point " + i); } }
public void FitsAtSamplePoints() { IInterpolation interpolation = new NevillePolynomialInterpolation(_t, _x); for (int i = 0; i < _x.Length; i++) { Assert.AreEqual(_x[i], interpolation.Interpolate(_t[i]), "A Exact Point " + i); double interpolatedValue; double secondDerivative; interpolation.Differentiate(_t[i], out interpolatedValue, out secondDerivative); Assert.AreEqual(_x[i], interpolatedValue, "B Exact Point " + i); } }
public void FitsAtArbitraryPointsWithMaple( [Values(0.1, 0.4, 1.1, 3.2, 4.5, 10.0, -10.0)] double t, [Values(.57225, 1.884, 3.0314166666666666667, 1.034666666666666667, 6.28125, 277.5, -1010.8333333333333333)] double x, [Values(1e-15, 1e-15, 1e-15, 1e-15, 1e-15, 1e-15, 1e-12)] double maxAbsoluteError) { IInterpolation interpolation = new NevillePolynomialInterpolation(_t, _x); Assert.AreEqual(x, interpolation.Interpolate(t), maxAbsoluteError, "Interpolation at {0}", t); double interpolatedValue; double secondDerivative; interpolation.Differentiate(t, out interpolatedValue, out secondDerivative); Assert.AreEqual(x, interpolatedValue, maxAbsoluteError, "Interpolation as by-product of differentiation at {0}", t); }
public void Interpolate_LogLogAttenuationData_InterpolationShouldNotYieldNaN( [Values(0.0025, 0.035, 0.45, 5.5, 18.5, 35.0)] double value) { var data = File.ReadLines(@"./data/Github-Cureos-1.csv"). Select(line => { var vals = line.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); return(Tuple.Create(vals[2], vals[3])); }).ToArray(); var x = data.Select(tuple => Double.Parse(tuple.Item1, CultureInfo.InvariantCulture)).ToArray(); var y = data.Select(tuple => Double.Parse(tuple.Item2, CultureInfo.InvariantCulture)).ToArray(); IInterpolation interpolation = new NevillePolynomialInterpolation(x, y); var actual = interpolation.Interpolate(Math.Log(value)); Assert.That(actual, Is.Not.NaN); }
static void Main(string[] args) { Console.WriteLine("Welcome to the program that will stop WW3"); var inputs = new ConsoleInput(); k = inputs.k; // Number that need to coperate to solve it privatePolynomial = inputs.privatePolynomial; // My private poly, starting with constant and ending in highest factor var polynomailShares = inputs.polynomialShares; //Other participants value of f_i(1) var sharedSums = inputs.sharedSums; // The shared points in the master plynomial polynomailShares.Insert(0, calcPol(1)); // Insert my value of f_1(1) var sharedX = new List <double> { 1 }; var sharedY = new List <double>(); for (int j = 2; j < sharedSums.Count + 2; j++) { if (sharedSums[j - 2] == -1) { continue; } sharedX.Add(j); sharedY.Add(sharedSums[j - 2]); } var sumPoint = polynomailShares.Sum(); sharedY.Insert(0, sumPoint); var key = new NevillePolynomialInterpolation(sharedX.ToArray(), sharedY.ToArray()); Console.WriteLine("Got the deactivation code: {0}", key.Interpolate(0)); Console.WriteLine("Press any key to quit"); Console.ReadKey(); }
public void Constructor_SamplePointsNotUnique_Throws() { var x = new[] { -1.0, 0.0, 1.5, 1.5, 2.5, 4.0 }; var y = new[] { 1.0, 0.3, -0.7, -0.6, -0.1, 0.4 }; var interpolation = new NevillePolynomialInterpolation(x, y); }
/// <summary> /// Create a Neville polynomial interpolation based on arbitrary points. /// If the points happen to be equidistant, consider to use the much more robust PolynomialEquidistant instead. /// Otherwise, consider whether RationalWithoutPoles would not be a more robust alternative. /// </summary> /// <param name="points">The sample points t.</param> /// <param name="values">The sample point values x(t).</param> /// <returns> /// An interpolation scheme optimized for the given sample points and values, /// which can then be used to compute interpolations and extrapolations /// on arbitrary points. /// </returns> /// <remarks> /// if your data is already sorted in arrays, consider to use /// MathNet.Numerics.Interpolation.NevillePolynomialInterpolation.InterpolateSorted /// instead, which is more efficient. /// </remarks> public static IInterpolation Polynomial(IEnumerable <double> points, IEnumerable <double> values) { return(NevillePolynomialInterpolation.Interpolate(points, values)); }
public void Interpolate_LogLogAttenuationData_InterpolationShouldNotYieldNaN( [Values(0.0025, 0.035, 0.45, 5.5, 18.5, 35.0)] double value) { var data = File.ReadAllLines(@"./data/Github-Cureos-1.csv"). Select(line => { var vals = line.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries); return new Tuple<string, string>(vals[2], vals[3]); }).ToArray(); var x = data.Select(tuple => Double.Parse(tuple.Item1, CultureInfo.InvariantCulture)).ToArray(); var y = data.Select(tuple => Double.Parse(tuple.Item2, CultureInfo.InvariantCulture)).ToArray(); IInterpolation interpolation = new NevillePolynomialInterpolation(x, y); var actual = interpolation.Interpolate(Math.Log(value)); Assert.That(actual, Is.Not.NaN); }
public void FitsAtArbitraryPointsWithMaple(double t, double x, double maxAbsoluteError) { IInterpolation interpolation = new NevillePolynomialInterpolation(_t, _x); Assert.AreEqual(x, interpolation.Interpolate(t), maxAbsoluteError, "Interpolation at {0}", t); }
public void FewSamples() { Assert.That(() => NevillePolynomialInterpolation.Interpolate(new double[0], new double[0]), Throws.ArgumentException); Assert.That(NevillePolynomialInterpolation.Interpolate(new[] { 1.0 }, new[] { 2.0 }).Interpolate(1.0), Is.EqualTo(2.0)); }
private DenseVector GradOfEvaluatingValueRidder(Func <DenseVector, double> inputFunction, DenseVector dvCurrentParametersPoint, DenseVector dvPreviousParametersIncrement, out DenseVector dvNewParametersIncrement, double maxRelativeError = 0.005d) { DenseVector dvCurrentParametersIncrement = dvPreviousParametersIncrement.Copy() * 2.0d; Func <DenseVector, double> theFunction = inputFunction; double maxRelPartialDerivAboveMin = 1000.0d; for (int i = 0; i < dvCurrentParametersIncrement.Count; i++) { if (Math.Abs(dvCurrentParametersIncrement[i]) / dvParametersScale[i] < 1.0e-2) { dvCurrentParametersIncrement[i] = dvParametersScale[i] * 1.0e-2; } else if (Math.Abs(dvCurrentParametersIncrement[i]) / dvParametersScale[i] > 1.0e+2) { dvCurrentParametersIncrement[i] = dvParametersScale[i]; } } DenseVector grad = DenseVector.Create(dvCurrentParametersPoint.Count, (index) => { DenseVector dvPartialParametersIncrement = DenseVector.Create(dvCurrentParametersIncrement.Count, (i => (i == index) ? (dvCurrentParametersIncrement[i]) : (0.0d))); return((theFunction(dvCurrentParametersPoint + dvPartialParametersIncrement) - theFunction(dvCurrentParametersPoint - dvPartialParametersIncrement)) / (2.0d * dvPartialParametersIncrement.Sum())); }); DenseVector gradHalfed = DenseVector.Create(dvCurrentParametersPoint.Count, (index) => { DenseVector dvPartialParametersIncrementHalfed = DenseVector.Create(dvCurrentParametersIncrement.Count, (i => (i == index) ? (dvCurrentParametersIncrement[i] / 2.0d) : (0.0d))); return((theFunction(dvCurrentParametersPoint + dvPartialParametersIncrementHalfed) - theFunction(dvCurrentParametersPoint - dvPartialParametersIncrementHalfed)) / (2.0d * dvPartialParametersIncrementHalfed.Sum())); }); double relativeError = (grad - gradHalfed).Abs() / grad.Abs(); for (int varIndex = 0; varIndex < dvCurrentParametersIncrement.Count; varIndex++) { double partialRelativeError = relativeError; double currentVarIncrement = dvCurrentParametersIncrement[varIndex]; List <Tuple <double, double, int> > lTplRelErrAndPartDeriv = new List <Tuple <double, double, int> >(); DenseVector dvPartialParametersIncrement = DenseVector.Create(dvCurrentParametersIncrement.Count, (i => (i == varIndex) ? (dvCurrentParametersIncrement[i]) : (0.0d))); List <DenseVector> dvParametersPointsToDetermineDerivative = new List <DenseVector>(); dvParametersPointsToDetermineDerivative.Add(dvCurrentParametersPoint - dvPartialParametersIncrement); dvParametersPointsToDetermineDerivative.Add(dvCurrentParametersPoint + dvPartialParametersIncrement); NevillePolynomialInterpolation interpolator = new NevillePolynomialInterpolation( dvParametersPointsToDetermineDerivative.ConvertAll <double>(dv => dv[varIndex]).ToArray(), dvParametersPointsToDetermineDerivative.ConvertAll <double>(dv => theFunction(dv)).ToArray()); double prevPartialDeriv = interpolator.Differentiate(dvCurrentParametersPoint[varIndex]); int iNumberOfPointsBetween = 0; while (partialRelativeError > maxRelativeError) { iNumberOfPointsBetween += 2; List <DenseVector> dvCurrParametersPoints = new List <DenseVector>(); dvCurrParametersPoints.InsertRange(0, dvParametersPointsToDetermineDerivative); for (int i = 1; i <= iNumberOfPointsBetween; i++) { dvCurrParametersPoints.Add(dvParametersPointsToDetermineDerivative[0] + (dvParametersPointsToDetermineDerivative[1] - dvParametersPointsToDetermineDerivative[0]) / (i + 1)); } dvCurrParametersPoints.Sort( (dv1, dv2) => (dv1[varIndex] < dv2[varIndex]) ? (-1) : (1)); NevillePolynomialInterpolation currInterpolator = new NevillePolynomialInterpolation( dvCurrParametersPoints.ConvertAll <double>(dv => dv[varIndex]).ToArray(), dvCurrParametersPoints.ConvertAll <double>(dv => theFunction(dv)).ToArray()); double currPartialDeriv = currInterpolator.Differentiate(dvCurrentParametersPoint[varIndex]); #region // OBSOLETE //currentVarIncrement = currentVarIncrement / 2.0d; //if (Math.Abs(currentVarIncrement) / dvParametersScale[varIndex] < 1.0e-20) //{ // currentVarIncrement = currentVarIncrement * 2.0d; // break; //} //else if (Math.Abs(currentVarIncrement) / dvParametersScale[varIndex] > 1.0e+20) //{ // currentVarIncrement = currentVarIncrement / 2.0d; // break; //} //DenseVector gradPartial = DenseVector.Create(dvCurrentParametersPoint.Count, new Func<int, double>((index) => //{ // if (index != varIndex) return 0.0d; // DenseVector dvPartialParametersIncrement = DenseVector.Create(dvCurrentParametersIncrement.Count, // (i => (i == index) ? (currentVarIncrement) : (0.0d))); // return (theFunction(dvCurrentParametersPoint + dvPartialParametersIncrement) - // theFunction(dvCurrentParametersPoint)) / currentVarIncrement; //})); // //DenseVector gradPartialHalfed = DenseVector.Create(dvCurrentParametersPoint.Count, new Func<int, double>((index) => //{ // if (index != varIndex) return 0.0d; // DenseVector dvPartialParametersIncrement = DenseVector.Create(dvCurrentParametersIncrement.Count, // (i => (i == index) ? (currentVarIncrement / 2.0d) : (0.0d))); // return (theFunction(dvCurrentParametersPoint + dvPartialParametersIncrement) - // theFunction(dvCurrentParametersPoint)) / (currentVarIncrement / 2.0d); //})); //double gradPartialModule = gradPartial.Abs(); //double gradPartialHalfedModule = gradPartialHalfed.Abs(); //if ((gradPartialModule == 0.0d) || (gradPartialHalfedModule == 0.0d)) //{ // currentVarIncrement = currentVarIncrement * 2.0d; // break; //} // (gradPartial - gradPartialHalfed).Abs() / gradPartial.Abs(); #endregion // OBSOLETE partialRelativeError = Math.Abs((currPartialDeriv - prevPartialDeriv) / prevPartialDeriv); prevPartialDeriv = currPartialDeriv; dvCurrentParametersIncrement[varIndex] = currentVarIncrement / (iNumberOfPointsBetween + 2); lTplRelErrAndPartDeriv.Add(new Tuple <double, double, int>(partialRelativeError, currPartialDeriv, iNumberOfPointsBetween)); if (partialRelativeError / lTplRelErrAndPartDeriv.Min(tpl => tpl.Item1) > maxRelPartialDerivAboveMin) { prevPartialDeriv = lTplRelErrAndPartDeriv.Min(tpl => tpl.Item2); dvCurrentParametersIncrement[varIndex] = currentVarIncrement / (lTplRelErrAndPartDeriv.Min(tpl => tpl.Item3) + 2); break; } if (SelfWorker != null) { if (SelfWorker.CancellationPending) { break; } } } grad[varIndex] = prevPartialDeriv; } #region // OBSOLETE //grad = DenseVector.Create(dvCurrentParametersPoint.Count, new Func<int, double>((index) => //{ // DenseVector dvPartialParametersIncrement = DenseVector.Create(dvCurrentParametersIncrement.Count, // (i => (i == index) ? (dvCurrentParametersIncrement[i]) : (0.0d))); // return (theFunction(dvCurrentParametersPoint + dvPartialParametersIncrement) - // theFunction(dvCurrentParametersPoint - dvPartialParametersIncrement)) / (2.0d*dvPartialParametersIncrement.Sum()); //})); #endregion // OBSOLETE dvNewParametersIncrement = DenseVector.OfEnumerable(dvCurrentParametersIncrement); dvNewParametersIncrement = (grad / grad.Abs()) * dvNewParametersIncrement.Abs(); return(grad); }
public virtual double Interpolate_Polynomial(double[] x, double[] y, double xValue) { return(NevillePolynomialInterpolation.Interpolate(x, y).Interpolate(xValue)); }