public static object GetInterpolatedValues(object[] X, object[] Y, object OnlyReturnBlanks) { bool flag = Utils.GetOptionalParameter(OnlyReturnBlanks, false); SortedDictionary <double, double> xy = new SortedDictionary <double, double>(); for (int i = 0; i < X.Length; ++i) { if (i < Y.Length && !(Y[i] is ExcelMissing || Y[i] is ExcelEmpty || Y[i] is ExcelError || Y[i] is string)) { xy.Add((double)X[i], (double)Y[i]); } } CubicSplineInterpolation csi = new CubicSplineInterpolation(xy.Keys.ToArray(), xy.Values.ToArray()); double[] values = new double[X.Length]; for (int i = 0; i < X.Length; ++i) { if (!flag || !xy.ContainsKey((double)X[i])) { values[i] = csi.Interpolate((double)X[i]); } } return(values.ToRange()); }
/// <summary> /// Verifies that at points other than the provided sample points, the interpolation matches the one computed by Maple as a reference. /// </summary> /// <param name="t">Sample point.</param> /// <param name="x">Sample value.</param> /// <param name="maxAbsoluteError">Maximum absolute error.</param> /// <remarks> /// Maple: /// with(CurveFitting); /// evalf(subs({x=-2.4},Spline([[-2,1],[-1,2],[0,-1],[1,0],[2,1]], x, degree=3, endpoints=Matrix(2,13,{(1,3)=1,(1,13)=-5,(2,10)=1,(2,13)=-1}))),20); /// </remarks> public void FixedSecondDerivativeFitsAtArbitraryPoints(double t, double x, double maxAbsoluteError) { var it = CubicSplineInterpolation.InterpolateBoundaries(_t, _y, SplineBoundaryCondition.SecondDerivative, -5.0, SplineBoundaryCondition.SecondDerivative, -1.0); Assert.AreEqual(x, it.ValueAt(t), maxAbsoluteError, "Interpolation at {0}", t); }
/// <summary> /// Returns a list of 3d POINTS. Uses cubic spline to interpolate. /// </summary> /// <param name="NumberOfPoints"></param> /// <returns></returns> public List <Point3D> Get3DPoints(int NumberOfPoints) { List <Point3D> interpolatedpoints = new List <Point3D>(); CubicSplineInterpolation spline = new CubicSplineInterpolation(); List <double> xcoors = new List <double>(); List <double> zcoors = new List <double>(); for (int i = 0; i < _xsec.Points.Count(); i++) { xcoors.Add(_xsec.Points[i].X); zcoors.Add(_xsec.Points[i].Z); } spline.Initialize(xcoors, zcoors); double xOffset = _xsec.LowestPoint.X; double dx = (xcoors.Last() - xcoors.First()) / NumberOfPoints; for (int i = 0; i < NumberOfPoints; i++) { double x = xcoors.First() + i * dx; double z = spline.Interpolate(x); interpolatedpoints.Add(new Point3D(MidStreamLocation.X - UnityVector.Y * (x - xOffset), MidStreamLocation.Y + UnityVector.X * (x - xOffset), z)); } return(interpolatedpoints); }
public string ProtocolBuild(ICollection <XYDataModel> xYDatas, double step, double a = 0, double b = 0) { string Protocol = $"При n = {step}\nwi | xi | Ei | f(Ei) | wi * f(Ei) | Сума"; // int n = (int)step >= 1 && (int)step <= 7 ? (int)step : 2; IntegralChebishev.MakeQuadratureCoefficients(n); IInterpolation interpolation = new CubicSplineInterpolation(); double f = 0; for (int i = 0; i < n; i++) { double el = i <= QuadratureElementValue.Count - 1 ? QuadratureElementValue[i] : -QuadratureElementValue[n - i - 1]; double Ei = (b + a) / 2.0 + ((b - a) / 2.0) * el; double Yi = interpolation.InterpolationPolynom(xYDatas, Ei); f += Yi; Protocol += $"\n{el} | {Ei} | {Yi} | {f}"; } f *= ((b - a) / n); Protocol += $"\nИнтеграл = {f} при " + "{a, b} " + $"{{{a},{b}}}"; // return(Protocol); }
public double Integral(ICollection <XYDataModel> xYDatas, double step, double a = 0, double b = 0) { int n = (int)step >= 1 && (int)step <= 7 ? (int)step : 2; IntegralChebishev.MakeQuadratureCoefficients(n); IInterpolation interpolation = new CubicSplineInterpolation(); double f = 0; for (int i = 0; i < n; i++) { double el = i <= QuadratureElementValue.Count - 1 ? QuadratureElementValue[i] : -QuadratureElementValue[n - i - 1]; double Ei = (b + a) / 2.0 + ((b - a) / 2.0) * el; double Yi = interpolation.InterpolationPolynom(xYDatas, Ei); f += Yi; } f *= ((b - a) / n); return(f); }
public void NaturalFitsAtSamplePoints() { var it = CubicSplineInterpolation.InterpolateNatural(_t, _y); for (int i = 0; i < _y.Length; i++) { Assert.AreEqual(_y[i], it.ValueAt(_t[i]), "A Exact Point " + i); } }
public void FixedSecondDerivativeFitsAtSamplePoints() { var it = CubicSplineInterpolation.InterpolateBoundaries(_t, _y, SplineBoundaryCondition.SecondDerivative, -5.0, SplineBoundaryCondition.SecondDerivative, -1.0); for (int i = 0; i < _y.Length; i++) { Assert.AreEqual(_y[i], it.ValueAt(_t[i]), "A Exact Point " + i); } }
public void NaturalFitsAtArbitraryPointsWithMaple(double t, double x, double maxAbsoluteError) { IInterpolation interpolation = new CubicSplineInterpolation(_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 FixedSecondDerivativeFitsAtArbitraryPointsWithMaple(double t, double x, double maxAbsoluteError) { IInterpolation interpolation = new CubicSplineInterpolation(_t, _x, SplineBoundaryCondition.SecondDerivative, -5.0, SplineBoundaryCondition.SecondDerivative, -1.0); 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 FixedFirstDerivativeFitsAtArbitraryPointsWithMaple(double t, double x, double maxAbsoluteError) { IInterpolation interpolation = new CubicSplineInterpolation(_t, _x, SplineBoundaryCondition.FirstDerivative, 1.0, SplineBoundaryCondition.FirstDerivative, -1.0); 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 NaturalSupportsLinearCase(int samples) { double[] x, y, xtest, ytest; LinearInterpolationCase.Build(out x, out y, out xtest, out ytest, samples); IInterpolation interpolation = new CubicSplineInterpolation(x, y); for (int i = 0; i < xtest.Length; i++) { Assert.AreEqual(ytest[i], interpolation.Interpolate(xtest[i]), 1e-15, "Linear with {0} samples, sample {1}", samples, i); } }
public void NaturalFitsAtArbitraryPointsWithMaple(double t, double x, double maxAbsoluteError) { IInterpolation interpolation = new CubicSplineInterpolation(_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 NaturalFitsAtSamplePoints() { IInterpolation interpolation = new CubicSplineInterpolation(_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 TestInterpolatedCubicCurve() { IInterpolation interp = new CubicSplineInterpolation(); DiscreteCurve curve = new DiscreteCurve(_pointCoords, _pointValues); IInterpolatedSpace interpCurve = new InterpolatedCurve(curve, interp, true); foreach (double point in _testPointArray) { IPoint p = new Point1D(point); Console.WriteLine(interpCurve.Value(p)); } }
public void FixedSecondDerivativeFitsAtSamplePoints() { IInterpolation interpolation = new CubicSplineInterpolation(_t, _x, SplineBoundaryCondition.SecondDerivative, -5.0, SplineBoundaryCondition.SecondDerivative, -1.0); 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); } }
CreateNaturalCubicSpline( IList <double> points, IList <double> values ) { CubicSplineInterpolation method = new CubicSplineInterpolation(); method.Init( points, values ); return(method); }
public void NaturalFitsAtSamplePoints() { IInterpolation interpolation = new CubicSplineInterpolation(_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 FixedSecondDerivativeFitsAtSamplePoints() { IInterpolation interpolation = new CubicSplineInterpolation(_t, _x, SplineBoundaryCondition.SecondDerivative, -5.0, SplineBoundaryCondition.SecondDerivative, -1.0); 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); } }
/// <summary> /// Interpolation of the interface points onto the equidistant Fourier points /// </summary> protected void InterpolateOntoFourierPoints(MultidimensionalArray interP, double[] samplP) { int numP = interP.Lengths[0]; // set interpolation data double[] independentVal = new double[numP + 2]; double[] dependentVal = new double[numP + 2]; for (int sp = 1; sp <= numP; sp++) { independentVal[sp] = interP[sp - 1, 0]; dependentVal[sp] = interP[sp - 1, 1]; } // extend the interpolation data for sample points at the boundary of the domain independentVal[0] = interP[numP - 1, 0] - DomainSize; dependentVal[0] = interP[numP - 1, 1]; independentVal[numP + 1] = interP[0, 0] + DomainSize; dependentVal[numP + 1] = interP[0, 1]; switch (this.InterpolationType) { case Interpolationtype.LinearSplineInterpolation: LinearSplineInterpolation LinSpline = new LinearSplineInterpolation(); LinSpline.Initialize(independentVal, dependentVal); for (int sp = 0; sp < numFp; sp++) { samplP[sp] = LinSpline.Interpolate(FourierP[sp]); //invDFT_coeff[sp] = (Complex)samplP[sp]; } break; case Interpolationtype.CubicSplineInterpolation: CubicSplineInterpolation CubSpline = new CubicSplineInterpolation(); CubSpline.Initialize(independentVal, dependentVal); for (int sp = 0; sp < numFp; sp++) { samplP[sp] = CubSpline.Interpolate(FourierP[sp]); //invDFT_coeff[sp] = (Complex)samplP[sp]; } break; default: throw new NotImplementedException(); } }
public void FixedFirstDerivativeFitsAtArbitraryPointsWithMaple( [Values(-2.4, -0.9, -0.5, -0.1, 0.1, 0.4, 1.2, 10.0, -10.0)] double t, [Values(1.12, 1.8243928571428571428, .54910714285714285715, -.78903571428571428572, -1.1304642857142857143, -1.1040000000000000000, .4148571428571428571, -608.14285714285714286, 1330.1428571428571429)] double x, [Values(1e-15, 1e-15, 1e-15, 1e-15, 1e-15, 1e-15, 1e-15, 1e-12, 1e-12)] double maxAbsoluteError) { IInterpolation interpolation = new CubicSplineInterpolation(_t, _x, SplineBoundaryCondition.FirstDerivative, 1.0, SplineBoundaryCondition.FirstDerivative, -1.0); 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 NaturalFitsAtArbitraryPointsWithMaple( [Values(-2.4, -0.9, -0.5, -0.1, 0.1, 0.4, 1.2, 10.0, -10.0)] double t, [Values(.144, 1.7906428571428571429, .47321428571428571431, -.80992857142857142857, -1.1089285714285714286, -1.0285714285714285714, .30285714285714285716, 189, 677)] double x, [Values(1e-15, 1e-15, 1e-15, 1e-15, 1e-15, 1e-15, 1e-15, 1e-15, 1e-12)] double maxAbsoluteError) { IInterpolation interpolation = new CubicSplineInterpolation(_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 FixedFirstDerivativeFitsAtSamplePoints() { IInterpolation interpolation = new CubicSplineInterpolation(_t, _x, SplineBoundaryCondition.FirstDerivative, 1.0, SplineBoundaryCondition.FirstDerivative, -1.0); 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 TestClampedCubicSplineInterpolation() { Random r = new Random(Environment.TickCount); TearDown(); var interpolation = new CubicSplineInterpolation(); interpolation.Initialize(_times, _rates); for (int i = 0; i < 10; ++i) { double time = (i + r.Next(-10000, 10000) / 10000); double interpRate = interpolation.ValueAt(time, true); Debug.WriteLine($"interpolatedRate : {interpRate} Time: {time}"); } }
public void FixedSecondDerivativeFitsAtArbitraryPointsWithMaple( [Values(-2.4, -0.9, -0.5, -0.1, 0.1, 0.4, 1.2, 10.0, -10.0)] double t, [Values(-.8999999999999999993, 1.7590357142857142857, .41517857142857142854, -.82010714285714285714, -1.1026071428571428572, -1.0211428571428571429, .31771428571428571421, 39, -37)] double x, [Values(1e-15, 1e-15, 1e-15, 1e-15, 1e-15, 1e-15, 1e-15, 1e-13, 1e-12)] double maxAbsoluteError) { IInterpolation interpolation = new CubicSplineInterpolation(_t, _x, SplineBoundaryCondition.SecondDerivative, -5.0, SplineBoundaryCondition.SecondDerivative, -1.0); 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); }
CreateCubicSpline( IList <double> points, IList <double> values, SplineBoundaryCondition leftBoundaryCondition, double leftBoundary, SplineBoundaryCondition rightBoundaryCondition, double rightBoundary ) { CubicSplineInterpolation method = new CubicSplineInterpolation(); method.Init( points, values, leftBoundaryCondition, leftBoundary, rightBoundaryCondition, rightBoundary ); return(method); }
public void Constructor_SamplePointsNotStrictlyAscending_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 CubicSplineInterpolation(x, y); }
public void FewSamples() { Assert.AreEqual( CubicSplineInterpolation.InterpolateAkima(new[] { 1.0, 2.0, 3.0, 4.0, 5.0 }, new[] { 2.0, 2.0, 2.0, 2.0, 2.0 }) .ValueAt(1.0), 2.0); }
/// <summary> /// Verifies that at points other than the provided sample points, the interpolation matches the one computed by Maple as a reference. /// </summary> /// <param name="t">Sample point.</param> /// <param name="x">Sample value.</param> /// <param name="maxAbsoluteError">Maximum absolute error.</param> public void FitsAtArbitraryPoints(double t, double x, double maxAbsoluteError) { var it = CubicSplineInterpolation.InterpolateAkima(_t, _y); Assert.AreEqual(x, it.ValueAt(t), maxAbsoluteError, "Interpolation at {0}", t); }
/// <summary> /// Interpolation of no equidistant sample point onto equidistant Fourier points /// </summary> public static void InterpolateOntoFourierPoints(MultidimensionalArray samplP, double DomainSize, double[] samplFp) { int numSp = samplP.Lengths[0]; // set interpolation data (delete multiple independent values) ArrayList independentList = new ArrayList(); ArrayList dependentList = new ArrayList(); for (int sp = 0; sp < numSp; sp++) { if (independentList.Contains(samplP[sp, 0]) == false) { independentList.Add(samplP[sp, 0]); dependentList.Add(samplP[sp, 1]); } } // extend the interpolation data for sample points at the boundary of the domain independentList.Insert(0, samplP[numSp - 1, 0] - DomainSize); independentList.Insert(independentList.Count, samplP[0, 0] + DomainSize); dependentList.Insert(0, samplP[numSp - 1, 1]); dependentList.Insert(dependentList.Count, samplP[0, 1]); double[] independentVal = (double[])independentList.ToArray(typeof(double)); double[] dependentVal = (double[])dependentList.ToArray(typeof(double)); // set Fourier points int numSFp = samplFp.Length; double[] FourierP = new double[numSFp]; for (int i = 0; i < numSFp; i++) { FourierP[i] = (DomainSize / numSFp) * i; } switch (InterpolationType) { case Interpolationtype.LinearSplineInterpolation: LinearSplineInterpolation LinSpline = new LinearSplineInterpolation(); LinSpline.Initialize(independentVal, dependentVal); for (int Fp = 0; Fp < numSFp; Fp++) { samplFp[Fp] = LinSpline.Interpolate(FourierP[Fp]); } break; case Interpolationtype.CubicSplineInterpolation: CubicSplineInterpolation CubSpline = new CubicSplineInterpolation(); CubSpline.Initialize(independentVal, dependentVal); for (int Fp = 0; Fp < numSFp; Fp++) { samplFp[Fp] = CubSpline.Interpolate(FourierP[Fp]); } break; default: throw new NotImplementedException(); } }
private void button1_Click(object sender, EventArgs e) { Plots.Series[0].Points.Clear(); Plots.Series[1].Points.Clear(); Plots.ChartAreas[0].AxisX.Minimum = -1; Plots.ChartAreas[0].AxisX.Maximum = 1; Plots.ChartAreas[0].AxisX.Interval = 0.5; int n = Convert.ToInt32(SegmentCount.Text); int addN = Convert.ToInt32(AddSegmentCount.Text); int idx = comboBox1.SelectedIndex; Function f = new FirstFunc(); if (idx == 0) { f = new TestFunc(); } if (idx == 1) { f = new FirstFunc(); } if (idx == 2) { f = new SecondFunc(); } if (idx == 3) { f = new ThirdFunc(); } int cond = 0; if (radioButton2.Checked == true) { cond = 1; } var task = new CubicSplineInterpolation(f, f.a, f.b, n, addN, cond); bool b = task.verify(); CubicSplineInterpolation.CubicSline[] cs = task.splines; double y; int N = 10; double h = 0; if (checkBox1.Checked == false) { for (int i = 1; i <= n; i++) { h = (task.grid.points[i] - task.grid.points[i - 1]) / N; for (int j = 0; j <= N; j++) { y = task.sp(task.grid.points[i - 1] + j * h); Plots.Series[0].Points.AddXY(task.grid.points[i - 1] + j * h, y); } } int PointsCount = 50; h = (f.b - f.a) / PointsCount; for (int i = 0; i <= PointsCount; i++) { y = f.val(f.a + i * h); Plots.Series[1].Points.AddXY(f.a + i * h, y); } } if (checkBox1.Checked == true) { for (int i = 1; i <= n; i++) { h = (task.grid.points[i] - task.grid.points[i - 1]) / N; for (int j = 0; j <= N; j++) { y = task.spFirstDerivative(task.grid.points[i - 1] + j * h); Plots.Series[0].Points.AddXY(task.grid.points[i - 1] + j * h, y); } } int PointsCount = 50; h = (f.b - f.a) / PointsCount; for (int i = 0; i <= PointsCount; i++) { y = f.firstDerivative(f.a + i * h); Plots.Series[1].Points.AddXY(f.a + i * h, y); } } dataTable.RowCount = n + 1; dataTable.ColumnCount = 7; dataTable[0, 0].Value = "N"; dataTable[1, 0].Value = "Xi"; dataTable[2, 0].Value = "Xi+1"; dataTable[3, 0].Value = "a"; dataTable[4, 0].Value = "b"; dataTable[5, 0].Value = "c"; dataTable[6, 0].Value = "d"; for (int i = 0; i < n; i++) { dataTable[0, i + 1].Value = i + 1; dataTable[1, i + 1].Value = task.grid.points[i]; dataTable[2, i + 1].Value = task.grid.points[i + 1]; dataTable[3, i + 1].Value = cs[i + 1].a; dataTable[4, i + 1].Value = cs[i + 1].b; dataTable[5, i + 1].Value = cs[i + 1].c; dataTable[6, i + 1].Value = cs[i + 1].d; } var Values = new double[addN * n + 1]; var ValuesFirstDer = new double[addN * n + 1]; for (int i = 0; i < addN * n; i++) { Values[i] = Math.Abs(f.val(task.AddGrid.points[i]) - task.sp(task.AddGrid.points[i])); ValuesFirstDer[i] = Math.Abs(f.firstDerivative(task.AddGrid.points[i]) - task.spFirstDerivative(task.AddGrid.points[i])); } double max1 = Values[0], max2 = ValuesFirstDer[0]; for (int i = 0; i < addN * n; i++) { if (max1 < Values[i]) { max1 = Values[i]; } if (max2 < ValuesFirstDer[i]) { max2 = ValuesFirstDer[i]; } } dataTable2.RowCount = addN * n + 2; dataTable2.ColumnCount = 8; dataTable2[0, 0].Value = "N"; dataTable2[1, 0].Value = "x"; dataTable2[2, 0].Value = "f(x)"; dataTable2[3, 0].Value = "S(x)"; dataTable2[4, 0].Value = "|f(x)-S(x)|"; dataTable2[5, 0].Value = "f'(x)"; dataTable2[6, 0].Value = "S'(x)"; dataTable2[7, 0].Value = "|f'(x)-S'(x)|"; for (int i = 0; i <= addN * n; i++) { dataTable2[0, i + 1].Value = i + 1; dataTable2[1, i + 1].Value = task.AddGrid.points[i]; dataTable2[2, i + 1].Value = f.val(task.AddGrid.points[i]); dataTable2[3, i + 1].Value = task.sp(task.AddGrid.points[i]); dataTable2[4, i + 1].Value = Math.Abs(f.val(task.AddGrid.points[i]) - task.sp(task.AddGrid.points[i])); dataTable2[5, i + 1].Value = f.firstDerivative(task.AddGrid.points[i]); dataTable2[6, i + 1].Value = task.spFirstDerivative(task.AddGrid.points[i]); dataTable2[7, i + 1].Value = Math.Abs(f.firstDerivative(task.AddGrid.points[i]) - task.spFirstDerivative(task.AddGrid.points[i])); } label4.Text = "Max|f(x)-S(x)| = " + max1.ToString(); label5.Text = "Max|f'(x)-S'(x)| = " + max2.ToString(); }
public void FixedFirstDerivativeFitsAtArbitraryPointsWithMaple(double t, double x, double maxAbsoluteError) { IInterpolation interpolation = new CubicSplineInterpolation(_t, _x, SplineBoundaryCondition.FirstDerivative, 1.0, SplineBoundaryCondition.FirstDerivative, -1.0); 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); }
/// <summary> /// Create a natural cubic spline interpolation based on arbitrary points. /// Natural splines are cubic splines with zero second derivative at the boundaries (i.e. straigth lines). /// </summary> /// <param name="points">The sample points t. Supports both lists and arrays.</param> /// <param name="values">The sample point values x(t). Supports both lists and arrays.</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> public static IInterpolationMethod CreateNaturalCubicSpline( IList<double> points, IList<double> values ) { CubicSplineInterpolation method = new CubicSplineInterpolation(); method.Init( points, values ); return method; }
static void Main(string[] args) { char[] replace = { ' ', ',', '\t', '\n', Convert.ToChar(10), Convert.ToChar(13) }; char[] replace2SiO2 = { ' ', '\t', '\n', Convert.ToChar(10), Convert.ToChar(13) }; //rn fn double ChangeX, ChangeY1, ChangeY2; #region 1.1 Dat 파일 읽기 //700nm 미포함 string[] Dat_sioDat = File.ReadAllLines("SiO2 1000nm_on_Si.dat", Encoding.Default); List <sio2_2nm_on_si_dat> Dat_wavelen = new List <sio2_2nm_on_si_dat>(); StreamReader sio2_2nm_text = new StreamReader(new FileStream("SiO2 1000nm_on_Si.dat", FileMode.Open)); foreach (var line in Dat_sioDat) { string[] splietData = line.Split(replace, StringSplitOptions.RemoveEmptyEntries); Dat_wavelen.Add(new sio2_2nm_on_si_dat { wavelength = splietData[0], AOI = splietData[1], Alpha = splietData[2], Beta = splietData[3], }); } List <double> List_dat_wave = new List <double>(); List <double> List_dat_aoi = new List <double>(); List <double> List_dat_alpha = new List <double>(); List <double> List_dat_beta = new List <double>(); double waveToDoubeWabe; for (int i = 1; i < Dat_sioDat.Length; i++) { waveToDoubeWabe = Convert.ToDouble(Dat_wavelen[i].wavelength); if (waveToDoubeWabe > 350 && waveToDoubeWabe < 980) { List_dat_wave.Add(waveToDoubeWabe); List_dat_aoi.Add(Convert.ToDouble(Dat_wavelen[i].AOI)); List_dat_alpha.Add(Convert.ToDouble(Dat_wavelen[i].Alpha)); List_dat_beta.Add(Convert.ToDouble(Dat_wavelen[i].Beta)); } } double[] Dat_double_wavelength = List_dat_wave.ToArray(); double[] Dat_double_AOI = List_dat_aoi.ToArray(); double[] Dat_double_Alpha = List_dat_alpha.ToArray(); double[] Dat_double_Beta = List_dat_beta.ToArray(); #endregion #region 1.2 Dat 파일 읽기 //700nm 포함 List <double> List_dat_wave_700 = new List <double>(); List <double> List_dat_aoi_700 = new List <double>(); List <double> List_dat_alpha_700 = new List <double>(); List <double> List_dat_beta_700 = new List <double>(); double waveToDoubeWabe_700; waveToDoubeWabe_700 = Convert.ToDouble(Dat_wavelen[1].wavelength); //0~399 총400개 400번을 700을 넣어주기 List_dat_wave_700 = List_dat_wave; List_dat_wave_700.Add(700.0); double[] Sort_Dat_Wave = List_dat_wave_700.ToArray(); Array.Sort(Sort_Dat_Wave); Console.WriteLine("12"); //새 파일 만들기 //StreamWriter Dat_700nm= new StreamWriter(new FileStream("SiO2 1000nm_on_Si(700nm).dat", FileMode.Create)); //List<sio2_2nm_on_si_dat> List_Dat_700 = new List<sio2_2nm_on_si_dat>(); //Dat_700nm.WriteLine("wavelength(nm)\tAOI\tAlpha\tBeta"); //for (int i = 0; i < Dat_double_wavelength.Length+1; i++) //{ // List_Dat_700.Add(new sio2_2nm_on_si_dat // { // wavelength = List_dat_wave[i], // }) ; //} sio2_2nm_text.Close(); #endregion #region 2.SiN 읽기--------------------------------------- string[] siLines = File.ReadAllLines("SiN.txt", Encoding.Default); List <SiData> ReadData = new List <SiData>(); StreamWriter newdatfile = new StreamWriter(new FileStream("SIN.txt", FileMode.Open)); foreach (var line in siLines) { string[] splitData = line.Split(replace, StringSplitOptions.RemoveEmptyEntries); if (ReadData.Count <= 1) { ReadData.Add(new SiData()); } if (ReadData.Count > 1) { ReadData.Add( new SiData { NM = splitData[0], N = splitData[1], K = splitData[2] }); } } List <double> List_SIN_NM = new List <double>(); List <double> List_SIN_N = new List <double>(); List <double> List_SIN_K = new List <double>(); for (int i = 3; i < siLines.Length; i++) { ChangeX = double.Parse(ReadData[i].NM); ChangeY1 = double.Parse(ReadData[i].N); ChangeY2 = double.Parse(ReadData[i].K); List_SIN_NM.Add(ChangeX); List_SIN_N.Add(ChangeY1); List_SIN_K.Add(ChangeY2); } double[] Sin_nm = List_SIN_NM.ToArray(); double[] Sin_n = List_SIN_N.ToArray(); double[] Sin_K = List_SIN_K.ToArray(); Dat_cublic_spline.Cal.CubicSplineInterpolation Sin_CSN = new Dat_cublic_spline.Cal.CubicSplineInterpolation(Sin_nm, Sin_n); Dat_cublic_spline.Cal.CubicSplineInterpolation Sin_CSK = new Dat_cublic_spline.Cal.CubicSplineInterpolation(Sin_nm, Sin_K); newdatfile.Close(); #endregion #region 3. SiO2_nm 읽기 string[] string_sio2_nm = File.ReadAllLines("SIO2_nm.txt", Encoding.Default); List <SiData> List_sion_NM = new List <SiData>(); StreamWriter newSio2Txt = new StreamWriter(new FileStream("SIO2_nm.txt", FileMode.Open)); foreach (var lines in string_sio2_nm) { string[] splitData = lines.Split(replace2SiO2, StringSplitOptions.RemoveEmptyEntries); List_sion_NM.Add(new SiData { NM = splitData[0], N = splitData[1], K = splitData[2] }); } List <double> List_SiO2_NM = new List <double>(); List <double> List_SiO2_N = new List <double>(); List <double> List_SiO2_K = new List <double>(); for (int i = 1; i < string_sio2_nm.Length; i++) { ChangeX = double.Parse(List_sion_NM[i].NM); ChangeY1 = double.Parse(List_sion_NM[i].N); ChangeY2 = double.Parse(List_sion_NM[i].K); List_SiO2_NM.Add(ChangeX); List_SiO2_N.Add(ChangeY1); List_SiO2_K.Add(ChangeY2); } double[] SiO2_nm = List_SiO2_NM.ToArray(); double[] SiO2_n = List_SiO2_N.ToArray(); double[] SiO2_k = List_SiO2_K.ToArray(); Cal.CubicSplineInterpolation SiO2_CSN = new Cal.CubicSplineInterpolation(SiO2_nm, SiO2_n); Cal.CubicSplineInterpolation SiO2_CSK = new Cal.CubicSplineInterpolation(SiO2_nm, SiO2_k); newSio2Txt.Close(); #endregion #region 4. Si_nm 읽기 string[] string_si_nm = File.ReadAllLines("Si_nm.txt", Encoding.Default); List <SiData> List_si_NM = new List <SiData>(); StreamWriter newSiTxt = new StreamWriter(new FileStream("Si_nm.txt", FileMode.Open)); foreach (var lines in string_si_nm) { string[] splitData = lines.Split(replace2SiO2, StringSplitOptions.RemoveEmptyEntries); List_si_NM.Add(new SiData { NM = splitData[0], N = splitData[1], K = splitData[2] }); } List <double> List_Si_NM = new List <double>(); List <double> List_Si_N = new List <double>(); List <double> List_Si_K = new List <double>(); for (int i = 1; i < string_si_nm.Length; i++) { ChangeX = double.Parse(List_si_NM[i].NM); ChangeY1 = double.Parse(List_si_NM[i].N); ChangeY2 = double.Parse(List_si_NM[i].K); List_Si_NM.Add(ChangeX); List_Si_N.Add(ChangeY1); List_Si_K.Add(ChangeY2); } double[] Si_nm = List_Si_NM.ToArray(); double[] Si_n = List_Si_N.ToArray(); double[] Si_k = List_Si_K.ToArray(); CubicSplineInterpolation Si_CSN = new CubicSplineInterpolation(Si_nm, Si_n); CubicSplineInterpolation Si_CSK = new CubicSplineInterpolation(Si_nm, Si_k); newSiTxt.Close(); #endregion #region 5. SiN_New 파일 쓰기 StreamWriter ChangeTxt = new StreamWriter(new FileStream("SIN_New.txt", FileMode.Create)); List <NewData> List_SiN = new List <NewData>(); ChangeTxt.WriteLine("wavelength(nm)\tn\tk"); for (int i = 1; i < Dat_double_wavelength.Length; i++) { if (Dat_double_wavelength[i] > 350 && Dat_double_wavelength[i] < 980) { List_SiN.Add(new NewData { NEWnm = Dat_double_wavelength[i], NEWN = (double)Sin_CSN.Interpolate(Dat_double_wavelength[i]), NEWK = (double)Sin_CSK.Interpolate(Dat_double_wavelength[i]) }); } } for (int i = 0; i < List_SiN.Count; i++) { if (List_SiN[i].NEWnm >= 350) { ChangeTxt.WriteLine("{0}\t{1}\t{2}", List_SiN[i].NEWnm, List_SiN[i].NEWN, List_SiN[i].NEWK); } } ChangeTxt.Close(); #endregion #region 5. SiN_New_700 파일 쓰기 StreamWriter ChangeTxt_700 = new StreamWriter(new FileStream("SIN_New_700.txt", FileMode.Create)); List <NewData> List_SiN_700 = new List <NewData>(); ChangeTxt_700.WriteLine("wavelength(nm)\tn\tk"); for (int i = 1; i < Sort_Dat_Wave.Length; i++) { if (Sort_Dat_Wave[i] > 350 && Sort_Dat_Wave[i] < 980) { List_SiN_700.Add(new NewData { NEWnm = Sort_Dat_Wave[i], NEWN = (double)Sin_CSN.Interpolate(Sort_Dat_Wave[i]), NEWK = (double)Sin_CSK.Interpolate(Sort_Dat_Wave[i]) }); } } for (int i = 0; i < List_SiN.Count; i++) { if (List_SiN[i].NEWnm >= 350) { ChangeTxt_700.WriteLine("{0}\t{1}\t{2}", List_SiN_700[i].NEWnm, List_SiN_700[i].NEWN, List_SiN_700[i].NEWK); } } ChangeTxt_700.Close(); #endregion #region 6. SiO2_new 파일 쓰기 StreamWriter ChageSio2Txt = new StreamWriter(new FileStream("SiO2_new.txt", FileMode.Create)); List <NewData> List_SiO2 = new List <NewData>(); ChageSio2Txt.WriteLine("wavelength(nm)\tn\tk"); //i=0은 해더, Dat_Double_NM= 파장값 ~.dat파일에서 뽑아온거 for (int i = 0; i < Dat_double_wavelength.Length; i++) { if (Dat_double_wavelength[i] > 980) { break; } List_SiO2.Add(new NewData { NEWnm = Dat_double_wavelength[i], NEWN = (double)SiO2_CSN.Interpolate(Dat_double_wavelength[i]), NEWK = (double)SiO2_CSK.Interpolate(Dat_double_wavelength[i]) }); ChageSio2Txt.WriteLine("{0}\t{1}\t{2}", List_SiO2[i].NEWnm, List_SiO2[i].NEWN, List_SiO2[i].NEWK); } ChageSio2Txt.Close(); #endregion #region 6. SiO2_new_700 파일 쓰기 StreamWriter ChageSio2Txt_700 = new StreamWriter(new FileStream("SiO2_new_700.txt", FileMode.Create)); List <NewData> List_SiO2_700 = new List <NewData>(); ChageSio2Txt_700.WriteLine("wavelength(nm)\tn\tk"); //i=0은 해더, Dat_Double_NM= 파장값 ~.dat파일에서 뽑아온거 for (int i = 0; i < Sort_Dat_Wave.Length; i++) { if (Sort_Dat_Wave[i] > 980) { break; } List_SiO2_700.Add(new NewData { NEWnm = Sort_Dat_Wave[i], NEWN = (double)SiO2_CSN.Interpolate(Sort_Dat_Wave[i]), NEWK = (double)SiO2_CSK.Interpolate(Sort_Dat_Wave[i]) }); ChageSio2Txt_700.WriteLine("{0}\t{1}\t{2}", List_SiO2_700[i].NEWnm, List_SiO2_700[i].NEWN, List_SiO2_700[i].NEWK); } ChageSio2Txt_700.Close(); #endregion #region 7. Si_new 파일 쓰기 StreamWriter ChageSiTxt = new StreamWriter(new FileStream("Si_new.txt", FileMode.Create)); List <NewData> List_Si = new List <NewData>(); ChageSiTxt.WriteLine("wavelength(nm)\tn\tk"); for (int i = 0; i < Dat_double_wavelength.Length; i++) { if (Dat_double_wavelength[i] > 980) { break; } List_Si.Add(new NewData { NEWnm = Dat_double_wavelength[i], NEWN = (double)Si_CSN.Interpolate(Dat_double_wavelength[i]), NEWK = (double)Si_CSK.Interpolate(Dat_double_wavelength[i]) }); ChageSiTxt.WriteLine("{0}\t{1}\t{2}", List_Si[i].NEWnm, List_Si[i].NEWN, List_Si[i].NEWK); } ChageSiTxt.Close(); #endregion #region 7. Si_new_700 파일 쓰기 StreamWriter ChageSiTxt_700 = new StreamWriter(new FileStream("Si_new_700.txt", FileMode.Create)); List <NewData> List_Si_700 = new List <NewData>(); ChageSiTxt_700.WriteLine("wavelength(nm)\tn\tk"); for (int i = 0; i < Sort_Dat_Wave.Length; i++) { if (Sort_Dat_Wave[i] > 980) { break; } List_Si_700.Add(new NewData { NEWnm = Sort_Dat_Wave[i], NEWN = (double)Si_CSN.Interpolate(Sort_Dat_Wave[i]), NEWK = (double)Si_CSK.Interpolate(Sort_Dat_Wave[i]) }); ChageSiTxt_700.WriteLine("{0}\t{1}\t{2}", List_Si_700[i].NEWnm, List_Si_700[i].NEWN, List_Si_700[i].NEWK); } ChageSiTxt_700.Close(); #endregion }
/// <summary> /// Create a cubic spline interpolation based on arbitrary points, with specified boundary conditions. /// </summary> /// <param name="points">The sample points t. Supports both lists and arrays.</param> /// <param name="values">The sample point values x(t). Supports both lists and arrays.</param> /// <param name="leftBoundaryCondition">Condition of the left boundary.</param> /// <param name="leftBoundary">Left boundary value. Ignored in the parabolic case.</param> /// <param name="rightBoundaryCondition">Condition of the right boundary.</param> /// <param name="rightBoundary">Right boundary value. Ignored in the parabolic case.</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> public static IInterpolationMethod CreateCubicSpline( IList<double> points, IList<double> values, SplineBoundaryCondition leftBoundaryCondition, double leftBoundary, SplineBoundaryCondition rightBoundaryCondition, double rightBoundary ) { CubicSplineInterpolation method = new CubicSplineInterpolation(); method.Init( points, values, leftBoundaryCondition, leftBoundary, rightBoundaryCondition, rightBoundary ); return method; }