/// <summary>Gets the value.</summary> /// <value>The value.</value> /// <exception cref="System.Exception">Cannot find value for + Name + XProperty: + XProperty</exception> public double Value(int arrayIndex = -1) { double XValue = 0; try { object v = this.FindByPath(XProperty)?.Value; if (v == null) { throw new Exception("Cannot find value for " + Name + " XProperty: " + XProperty); } if (v is Array && arrayIndex > -1) { XValue = Convert.ToDouble((v as Array).GetValue(arrayIndex), System.Globalization.CultureInfo.InvariantCulture); } else { XValue = Convert.ToDouble(v, System.Globalization.CultureInfo.InvariantCulture); } } catch (IndexOutOfRangeException) { } if (spline == null) { spline = CubicSpline.InterpolateBoundaries(XYPairs.X, XYPairs.Y, SplineBoundaryCondition.FirstDerivative, 0, SplineBoundaryCondition.FirstDerivative, 0); } return(Interpolate(XValue)); }
public void CheckNaturalSplineMinMaxValuesPerformance() { //first generate the test data and spline double amplitude = 100; double ofset = 0; double period = 10; double decay = 0.1; double minX = -50; double maxX = 50; int points = 100000; var data = GenerateSplineData(amplitude, period, decay, minX, maxX, ofset, points); CubicSpline it = CubicSpline.InterpolateBoundaries(data.Item1, data.Item2, SplineBoundaryCondition.Natural, 0, SplineBoundaryCondition.Natural, 0); //start the time and calculate the min max values var t = DateTime.Now; var minMax = it.Extrema(); Assert.IsTrue(minMax.Item2.AlmostEqual(ofset, 0.3), "Expexted max value near ofset."); Assert.IsTrue(minMax.Item1.AlmostEqual(ofset + period / 2, 0.3) || minMax.Item1.AlmostEqual(ofset - period / 2, 0.3), "Expexted min value near ofset +- period/2."); //spit out the time it took to calculate Console.WriteLine("Extrema took: " + (DateTime.Now - t).TotalMilliseconds.ToString("000.00") + " ms for " + points.ToString() + " points."); //determine if the values are correct var sp = it.StationaryPoints(); foreach (var x in sp) { //check that the stationary point falls roughly at a half period Assert.IsTrue(Math.Abs((Math.Abs((x - ofset) * 2 / period) - Math.Round(Math.Abs(x - ofset) * 2 / period, 0))).AlmostEqual(0, 0.3), "Stationary point found outside of period/2 for x=" + x.ToString()); } }
/// <summary>Gets the value.</summary> /// <value>The value.</value> /// <exception cref="System.Exception">Cannot find value for + Name + XProperty: + XProperty</exception> public double Value(int arrayIndex = -1) { double XValue = 0; try { object v = Apsim.Get(this, XProperty); if (v == null) { throw new Exception("Cannot find value for " + Name + " XProperty: " + XProperty); } if (v is Array && arrayIndex > -1) { XValue = Convert.ToDouble((v as Array).GetValue(arrayIndex)); } else { XValue = Convert.ToDouble(v); } } catch (IndexOutOfRangeException) { } if (spline == null) { spline = CubicSpline.InterpolateBoundaries(XYPairs.X, XYPairs.Y, SplineBoundaryCondition.FirstDerivative, 0, SplineBoundaryCondition.FirstDerivative, 0); } return(Interpolate(XValue)); }
public static double Integrate(IEnumerable <double> x, IEnumerable <double> y) { var spline = CubicSpline.InterpolateBoundaries(x, y, SplineBoundaryCondition.ParabolicallyTerminated, 0, SplineBoundaryCondition.ParabolicallyTerminated, 0); return(spline.Integrate(x.Minimum(), x.Maximum())); }
public void FixedFirstDerivativeFitsAtSamplePoints() { IInterpolation it = CubicSpline.InterpolateBoundaries(_t, _y, SplineBoundaryCondition.FirstDerivative, 1.0, SplineBoundaryCondition.FirstDerivative, -1.0); for (int i = 0; i < _y.Length; i++) { Assert.AreEqual(_y[i], it.Interpolate(_t[i]), "A Exact Point " + i); } }
public void NaturalSplineGetMinMaxTvalues() { CubicSpline it = CubicSpline.InterpolateBoundaries(_x, _z, SplineBoundaryCondition.SecondDerivative, -4.0, SplineBoundaryCondition.SecondDerivative, 4.0); var minMax = it.Extrema(); Assert.AreEqual(-4, minMax.Item1, "Spline returns wrong t value for global minimum"); Assert.AreEqual(4, minMax.Item2, "Spline returns wrong t value for global maximum"); Console.WriteLine("GetMinMaxTValues checked out ok for cubic spline."); }
public Func <double, double> MakeInterpolator(IEnumerable <double> x, IEnumerable <double> y) { SplineBoundaryCondition bc; int xlength = x.Count(); int mode = InterpolationModeComboBox.SelectedIndex; if (mode == 0 || mode == 1) { int order; order = mode * 2 + 1; // linear => 1; cubic => 3 int npoints; if (!int.TryParse(extrapolationPointsTextBox.Text, out npoints)) { throw new Exception("Cannot parse number of points"); } if (npoints > xlength) { throw new Exception("Too many points required"); } if (npoints < order) { throw new Exception("More points must be used"); } var leftInterp = Fit.PolynomialFunc(x.Take(npoints).ToArray(), y.Take(npoints).ToArray(), order); var rightInterp = Fit.PolynomialFunc(x.Reverse().Take(npoints).Reverse().ToArray(), y.Reverse().Take(npoints).Reverse().ToArray(), order); double middlex = x.Skip((int)(xlength / 2)).First(); return(x2 => (x2 < middlex)?leftInterp(x2) : rightInterp(x2)); } else if (mode == 5) { IInterpolation I = LinearSpline.Interpolate(x, y); return(x2 => I.Interpolate(x2)); } else { if (InterpolationModeComboBox.SelectedIndex == 1) { bc = SplineBoundaryCondition.ParabolicallyTerminated; } else { bc = SplineBoundaryCondition.Natural; } IInterpolation I = CubicSpline.InterpolateBoundaries( x, y, bc, 0, bc, 0); // Values are unused for natural and parabolic termination return(x2 => I.Interpolate(x2)); } }
public void NaturalSplineGetHorizontalDerivativeTValues() { CubicSpline it = CubicSpline.InterpolateBoundaries(_x, _z, SplineBoundaryCondition.SecondDerivative, -4.0, SplineBoundaryCondition.SecondDerivative, 4.0); var horizontalDerivatives = it.StationaryPoints(); //readonly double[] _x = { -4, -3, -2, -1, 0, 1, 2, 3, 4 }; //readonly double[] _z = { -7, 2, 5, 0, -3, -1, -4, 0, 6 }; Assert.AreEqual(4, horizontalDerivatives.Length, "Incorrect number of points with derivative value equal to 0"); Assert.IsTrue(horizontalDerivatives[0] >= -3 && horizontalDerivatives[0] <= -2, "Spline returns wrong t value: " + horizontalDerivatives[0] + " for first point"); Assert.IsTrue(horizontalDerivatives[1] >= -1 && horizontalDerivatives[1] <= 0, "Spline returns wrong t value: " + horizontalDerivatives[1] + " for second point"); Assert.IsTrue(horizontalDerivatives[2] >= 0 && horizontalDerivatives[2] <= 1, "Spline returns wrong t value: " + horizontalDerivatives[2] + " for third point"); Assert.IsTrue(horizontalDerivatives[3] >= 2 && horizontalDerivatives[3] <= 3, "Spline returns wrong t value: " + horizontalDerivatives[3] + " for fourth point"); Console.WriteLine("GetHorizontalDerivativeTValues checked out ok for cubic spline."); }
//CubicSpline lookX, lookY, lookZ; /// <summary> /// Initialize our path with a sequence of locations and lookAt directions. /// </summary> public void Initialize(IEnumerable <Vector3D> locations, IEnumerable <Vector3D> lookAts) { int count = locations.Count(); //if( lookAts.Count() != count ) // throw new System.ArgumentException(); double[] times = Enumerable.Range(0, count).Select(i => (double)i / (count - 1)).ToArray(); //.6, 0, -.8 locX = CubicSpline.InterpolateBoundaries(times, locations.Select(v => v.X), SplineBoundaryCondition.FirstDerivative, .6, SplineBoundaryCondition.FirstDerivative, .6); locY = CubicSpline.InterpolateBoundaries(times, locations.Select(v => v.Y), SplineBoundaryCondition.FirstDerivative, 0, SplineBoundaryCondition.FirstDerivative, 0); locZ = CubicSpline.InterpolateBoundaries(times, locations.Select(v => v.Z), SplineBoundaryCondition.FirstDerivative, -.8, SplineBoundaryCondition.FirstDerivative, -.8); locX = CubicSpline.InterpolateNatural(times, locations.Select(v => v.X)); locY = CubicSpline.InterpolateNatural(times, locations.Select(v => v.Y)); locZ = CubicSpline.InterpolateNatural(times, locations.Select(v => v.Z)); /*lookX = CubicSpline.InterpolateNatural( times, lookAts.Select( v => v.X ) ); * lookY = CubicSpline.InterpolateNatural( times, lookAts.Select( v => v.Y ) ); * lookZ = CubicSpline.InterpolateNatural( times, lookAts.Select( v => v.Z ) );*/ }
public void FixedSecondDerivativeFitsAtArbitraryPoints(double t, double x, double maxAbsoluteError) { IInterpolation it = CubicSpline.InterpolateBoundaries(_t, _y, SplineBoundaryCondition.SecondDerivative, -5.0, SplineBoundaryCondition.SecondDerivative, -1.0); Assert.AreEqual(x, it.Interpolate(t), maxAbsoluteError, "Interpolation at {0}", t); }