void Update() { if (currentResolution != resolution || optimizationPoints == null) { CreateOptimizationPoints(); } FunctionDelegate f = functionDelegates[(int)function]; float t = Time.timeSinceLevelLoad; //optimization steps Matrix a = QuadraticFormMatrix(t); Matrix currentPoint = new Matrix(new double[][] { new double[] { xStart }, new double[] { zStart } }); Matrix currentGradient; Matrix currentHessianInv; Matrix lastPoint = currentPoint.Clone(); double[] ts = new double[iterationCount + 1]; double[] xs = new double[iterationCount + 1]; double[] zs = new double[iterationCount + 1]; xs[0] = currentPoint.GetArray()[0][0]; zs[0] = currentPoint.GetArray()[1][0]; for (int i = 0; i < iterationCount; i++) { ts[i] = i; currentGradient = a * lastPoint; //currentHessianInv = a.Inverse(); //currentPoint = lastPoint - ((double) learningRate) * currentHessianInv * currentGradient; currentPoint = lastPoint - ((double)learningRate) * currentGradient; xs[i + 1] = currentPoint.GetArray()[0][0]; zs[i + 1] = currentPoint.GetArray()[1][0]; lastPoint = currentPoint.Clone(); } ts[iterationCount] = iterationCount; IInterpolationMethod xInterp = Interpolation.Create(ts, xs); IInterpolationMethod zInterp = Interpolation.Create(ts, zs); float increment = ((float)iterationCount) / ((float)(resolution - 1)); for (int i = 0; i < resolution; i++) { float curInc = increment * i; Vector3 p = new Vector3((float)xInterp.Interpolate(curInc), 0.0f, (float)zInterp.Interpolate(curInc)); p.y = f(p, t); optimizationPoints[i].position = p; Color c = optimizationPoints [i].color; c.g = ((float)i) / ((float)resolution); c.b = ((float)curInc) / ((float)iterationCount); //c.g = 1.0f / Mathf.Exp((float) i / 15.0f); optimizationPoints[i].color = c; } particleSystem.SetParticles(optimizationPoints, optimizationPoints.Length); }
public void IRID209_InterpolationWithThreeSamples() { double[] values = new double[] { 6.0, 12.0, 16.0 }; double[] points = new double[] { 1.0, 2.0, 3.0 }; IInterpolationMethod method2 = Interpolation.CreatePolynomial(points, values); double b = method2.Interpolate(2.5); Assert.That(b, Is.Not.NaN, "polynomial (neville)"); IInterpolationMethod method = Interpolation.Create(points, values); double a = method.Interpolate(2.5); Assert.That(a, Is.Not.NaN, "rational pole-free"); }
//private void CreateOptimizationPoints () { // currentResolution = resolution; // optimizationPoints = new ParticleSystem.Particle[resolution]; // float increment = 1f / (resolution - 1); // int i = 0; // for (int t = 0; t < resolution; t++) { // Vector3 p = new Vector3(0f, 0f, 0f); // optimizationPoints[i].position = p; // optimizationPoints[i].color = new Color(p.x + increment * resolution / 2, 0f, p.z + increment * resolution / 2); // optimizationPoints[i++].size = 0.15f; // } //} void Update() { if (currentResolution != resolution || points == null || currentGridOption != gridOption) { CreateGridPoints(); } //if (currentResolution != resolution || optimizationPoints == null) { // CreateOptimizationPoints(); //} FunctionDelegate f = functionDelegates[(int)function]; float t = Time.timeSinceLevelLoad; //function graph steps for (int i = 0; i < points.Length; i++) { Vector3 p = points [i].position; p.y = f(p, t); points[i].position = p; Color c = points [i].color; c.g = p.y; points[i].color = c; } //optimization steps Matrix a = QuadraticFormMatrix(t); Matrix currentPoint = new Matrix(new double[][] { new double[] { xStart }, new double[] { zStart } }); Matrix lastPoint = currentPoint.Clone(); double[] ts = new double[iterationCount + 1]; double[] xs = new double[iterationCount + 1]; double[] zs = new double[iterationCount + 1]; xs[0] = currentPoint.GetArray()[0][0]; zs[0] = currentPoint.GetArray()[1][0]; for (int i = 0; i < iterationCount; i++) { ts[i] = i; currentPoint = lastPoint - ((double)learningRate) * a * lastPoint; xs[i + 1] = currentPoint.GetArray()[0][0]; zs[i + 1] = currentPoint.GetArray()[1][0]; lastPoint = currentPoint.Clone(); } ts[iterationCount] = iterationCount; IInterpolationMethod xInterp = Interpolation.Create(ts, xs); IInterpolationMethod zInterp = Interpolation.Create(ts, zs); float increment = ((float)iterationCount) / ((float)(resolution - 1)); for (int i = 0; i < resolution; i++) { float curInc = increment * i; Vector3 p = new Vector3((float)xInterp.Interpolate(curInc), 0.0f, (float)zInterp.Interpolate(curInc)); p.y = f(p, t); points[i].position = p; Color c = points [i].color; c.g = p.y; points[i].color = c; } particleSystem.SetParticles(points, points.Length); //particleSystem.SetParticles(optimizationPoints, optimizationPoints.Length); //Matrix m = new Matrix(new double[][] { // new double[] { 10.0, -18.0 }, // new double[] { 6.0, -11.0 } }); // alternative way to create the matrix: // double[][] data = Matrix.CreateMatrixData(2, 2); // data[0][0] = 10.0; // data[1][0] = 6.0; // data[0][1] = -18.0; // data[1][1] = -11.0; // Matrix m = new Matrix(data); //EigenvalueDecomposition eigen = m.EigenvalueDecomposition; //Complex[] eigenValues = eigen.EigenValues; // eigenvalues: 1, -2 //Matrix eigenVectors = eigen.EigenVectors; // eigenvectors: [0.894...,0.447...] and [6.708...,4.473...] // alternative way to access the eigenvalues witout the Complex type: // double[] eigenValuesReal = eigen.RealEigenvalues; // real part // double[] eigenValuesImag = eigen.ImagEigenvalues; // imaginary part }
public void TestInterpolationMethod_RationalPoleFreeBarycentric() { /************************************************************************************************** * 1st: polynomial case (equidistant polynomial generates the same values; rational would have pole) **************************************************************************************************/ double[] t = new double[] { -2.0, -1.0, 0.0, 1.0, 2.0 }; double[] x = new double[] { 1.0, 2.0, -1.0, 0.0, 1.0 }; IInterpolationMethod method = Interpolation.Create(t, x); Assert.That(method, Is.TypeOf(typeof(RationalPoleFreeInterpolation)), "Type"); for (int i = 0; i < t.Length; i++) { // verify the interpolated values exactly at the sample points. Assert.That(method.Interpolate(t[i]), Is.EqualTo(x[i]), "A Exact Point " + i.ToString()); } // Maple: "with(CurveFitting);" // Maple: "PolynomialInterpolation([[-2,1],[-1,2],[0,-1],[1,0],[2,1]], x);" Assert.That(method.Interpolate(-2.4), NumericIs.AlmostEqualTo(-4.5968, 1e-15), "A -2.4"); Assert.That(method.Interpolate(-0.9), NumericIs.AlmostEqualTo(1.65395, 1e-15), "A -0.9"); Assert.That(method.Interpolate(-0.5), NumericIs.AlmostEqualTo(0.21875, 1e-15), "A -0.5"); Assert.That(method.Interpolate(-0.1), NumericIs.AlmostEqualTo(-0.84205, 1e-15), "A -0.1"); Assert.That(method.Interpolate(0.1), NumericIs.AlmostEqualTo(-1.10805, 1e-15), "A 0.1"); Assert.That(method.Interpolate(0.4), NumericIs.AlmostEqualTo(-1.1248, 1e-15), "A 0.4"); Assert.That(method.Interpolate(1.2), NumericIs.AlmostEqualTo(0.5392, 1e-15), "A 1.2"); Assert.That(method.Interpolate(10.0), NumericIs.AlmostEqualTo((double)(-4431), 1e-12), "A 10.0"); Assert.That(method.Interpolate(-10.0), NumericIs.AlmostEqualTo((double)(-5071), 1e-12), "A -10.0"); /****************************************************************************** * 2nd: x(t) = 1/(1+t^2), t=-5..5 (polynomial can' t interpolate that function!) ******************************************************************************/ t = new double[40]; x = new double[40]; const double step = 10.0 / 39.0; for (int i = 0; i < t.Length; i++) { double tt = -5 + (i * step); t[i] = tt; x[i] = 1.0 / (1.0 + (tt * tt)); } RationalPoleFreeInterpolation methodTyped = (RationalPoleFreeInterpolation)method; methodTyped.Init(t, x); // re-initialize for another set of points/values. for (int i = 0; i < t.Length; i++) { // verify the interpolated values exactly at the sample points. Assert.That(method.Interpolate(t[i]), Is.EqualTo(x[i]), "B Exact Point " + i.ToString()); } // Maple: "with(CurveFitting);" // Maple: "tt := [seq(-5+(i-1)*10/39,i=1..40)]: xx := [seq(1/(1+tt[i]*tt[i]),i=1..40)]:" // Maple: "RationalInterpolation(tt, xx, x);" // Test Linear Case for (int k = 2; k < 7; k++) { double[] linx, liny, linxtest, linytest; BuildLinearCase(2, k, out linx, out liny, out linxtest, out linytest); IInterpolationMethod linearMethod = Interpolation.Create(linx, liny); for (int i = 0; i < linxtest.Length; i++) { Assert.That(linearMethod.Interpolate(linxtest[i]), NumericIs.AlmostEqualTo(linytest[i], 1e-12), String.Format("Linear k={0} i={1}", k, i)); } } }
public void TestInterpolationMethod_RationalPoleFreeBarycentric() { // ************************************************************************************************* // 1st: polynomial case (equidistant polynomial generates the same values; rational would have pole) // ************************************************************************************************* double[] t = new double[] { -2.0, -1.0, 0.0, 1.0, 2.0 }; double[] x = new double[] { 1.0, 2.0, -1.0, 0.0, 1.0 }; IInterpolationMethod method = Interpolation.Create(t, x); Assert.IsInstanceOfType(typeof(RationalPoleFreeInterpolation), method, "Type"); for (int i = 0; i < t.Length; i++) { // verify the interpolated values exactly at the sample points. Assert.AreEqual(x[i], method.Interpolate(t[i]), "A Exact Point " + i.ToString()); } // Maple: "with(CurveFitting);" // Maple: "PolynomialInterpolation([[-2,1],[-1,2],[0,-1],[1,0],[2,1]], x);" NumericAssert.AreAlmostEqual(-4.5968, method.Interpolate(-2.4), 1e-15, "A -2.4"); NumericAssert.AreAlmostEqual(1.65395, method.Interpolate(-0.9), 1e-15, "A -0.9"); NumericAssert.AreAlmostEqual(0.21875, method.Interpolate(-0.5), 1e-15, "A -0.5"); NumericAssert.AreAlmostEqual(-0.84205, method.Interpolate(-0.1), 1e-15, "A -0.1"); NumericAssert.AreAlmostEqual(-1.10805, method.Interpolate(0.1), 1e-15, "A 0.1"); NumericAssert.AreAlmostEqual(-1.1248, method.Interpolate(0.4), 1e-15, "A 0.4"); NumericAssert.AreAlmostEqual(0.5392, method.Interpolate(1.2), 1e-15, "A 1.2"); NumericAssert.AreAlmostEqual(-4431, method.Interpolate(10.0), 1e-12, "A 10.0"); NumericAssert.AreAlmostEqual(-5071, method.Interpolate(-10.0), 1e-12, "A -10.0"); // ***************************************************************************** // 2nd: x(t) = 1/(1+t^2), t=-5..5 (polynomial can' t interpolate that function!) // ***************************************************************************** t = new double[40]; x = new double[40]; double step = 10.0 / 39.0; for (int i = 0; i < t.Length; i++) { double tt = -5 + i * step; t[i] = tt; x[i] = 1.0 / (1.0 + tt * tt); } RationalPoleFreeInterpolation methodTyped = (RationalPoleFreeInterpolation)method; methodTyped.Init(t, x); // re-initialize for another set of points/values. for (int i = 0; i < t.Length; i++) { // verify the interpolated values exactly at the sample points. Assert.AreEqual(x[i], method.Interpolate(t[i]), "B Exact Point " + i.ToString()); } // Maple: "with(CurveFitting);" // Maple: "tt := [seq(-5+(i-1)*10/39,i=1..40)]: xx := [seq(1/(1+tt[i]*tt[i]),i=1..40)]:" // Maple: "RationalInterpolation(tt, xx, x);" }