Exemplo n.º 1
0
        //-------------------------------------------------------------------------
        private void testSensitivity(double[] xValues, double[] yValues, double[] keys, double delta)
        {
            PiecewisePolynomialWithSensitivityFunction1D func        = new PiecewisePolynomialWithSensitivityFunction1D();
            PiecewisePolynomialResultsWithSensitivity    resultSensi = INTERP.interpolateWithSensitivity(xValues, yValues);

            DoubleArray[] computedArray = func.nodeSensitivity(resultSensi, keys);
            for (int i = 0; i < keys.Length; ++i)
            {
                double      @base    = func.evaluate(resultSensi, keys[i]).get(0);
                DoubleArray computed = func.nodeSensitivity(resultSensi, keys[i]);
                assertEquals(computed, computedArray[i]);
                for (int j = 0; j < yValues.Length; ++j)
                {
                    double[] yValuesBump = Arrays.copyOf(yValues, yValues.Length);
                    yValuesBump[j] += delta;
                    PiecewisePolynomialResult resultBump = INTERP.interpolate(xValues, yValuesBump);
                    double expected = (func.evaluate(resultBump, keys[i]).get(0) - @base) / delta;
                    assertEquals(computed.get(j), expected, delta);
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Left extrapolation by linear function unless extra node is added on the left
        /// </summary>
        private PiecewisePolynomialResult extrapolateByLinearFunction(PiecewisePolynomialResult result, double[] xValues)
        {
            int nIntervalsAll = result.NumberOfIntervals;

            double[] nodes = result.Knots.toArray();
            if (Math.Abs(xValues[xValues.Length - 1] - nodes[nIntervalsAll]) < EPS)
            {
                double   lastNodeX       = nodes[nIntervalsAll];
                double   lastNodeY       = FUNC.evaluate(result, lastNodeX).get(0);
                double   extraNode       = 2.0 * nodes[nIntervalsAll] - nodes[nIntervalsAll - 1];
                double   extraDerivative = FUNC.differentiate(result, lastNodeX).get(0);
                double[] newKnots        = new double[nIntervalsAll + 2];
                Array.Copy(nodes, 0, newKnots, 0, nIntervalsAll + 1);
                newKnots[nIntervalsAll + 1] = extraNode;   // dummy node, outside the data range
                double[][] newCoefMatrix = new double[nIntervalsAll + 1][];
                for (int i = 0; i < nIntervalsAll; ++i)
                {
                    newCoefMatrix[i] = Arrays.copyOf(result.CoefMatrix.row(i).toArray(), result.Order);
                }
                newCoefMatrix[nIntervalsAll] = new double[result.Order];
                newCoefMatrix[nIntervalsAll][result.Order - 1] = lastNodeY;
                newCoefMatrix[nIntervalsAll][result.Order - 2] = extraDerivative;
                if (result is PiecewisePolynomialResultsWithSensitivity)
                {
                    PiecewisePolynomialResultsWithSensitivity resultCast = (PiecewisePolynomialResultsWithSensitivity)result;
                    double[]       extraSense    = FUNC.nodeSensitivity(resultCast, lastNodeX).toArray();
                    double[]       extraSenseDer = FUNC.differentiateNodeSensitivity(resultCast, lastNodeX).toArray();
                    DoubleMatrix[] newCoefSense  = new DoubleMatrix[nIntervalsAll + 1];
                    for (int i = 0; i < nIntervalsAll; ++i)
                    {
                        newCoefSense[i] = resultCast.getCoefficientSensitivity(i);
                    }
//JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java:
//ORIGINAL LINE: double[][] extraCoefSense = new double[resultCast.Order][extraSense.Length];
                    double[][] extraCoefSense = RectangularArrays.ReturnRectangularDoubleArray(resultCast.Order, extraSense.Length);
                    extraCoefSense[resultCast.Order - 1] = Arrays.copyOf(extraSense, extraSense.Length);
                    extraCoefSense[resultCast.Order - 2] = Arrays.copyOf(extraSenseDer, extraSenseDer.Length);
                    newCoefSense[nIntervalsAll]          = DoubleMatrix.copyOf(extraCoefSense);
                    return(new PiecewisePolynomialResultsWithSensitivity(DoubleArray.copyOf(newKnots), DoubleMatrix.copyOf(newCoefMatrix), resultCast.Order, 1, newCoefSense));
                }
                return(new PiecewisePolynomialResult(DoubleArray.copyOf(newKnots), DoubleMatrix.copyOf(newCoefMatrix), result.Order, 1));
            }
            return(result);
        }
        public virtual void interpolationTest()
        {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int nExamples = Y.length;
            int nExamples = Y.Length;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int n = XX.length;
            int n = XX.Length;

            for (int example = 0; example < nExamples; example++)
            {
                PiecewisePolynomialResult pp = PCHIP_S.interpolateWithSensitivity(X, Y[example]);
                for (int i = 0; i < n; i++)
                {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double y = PPVAL_S.evaluate(pp, XX[i]).get(0);
                    double y = PPVAL_S.evaluate(pp, XX[i]).get(0);

                    assertEquals("example: " + example + ", index:" + i, YY[example][i], y, 1e-14);
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// No clamped points added
        /// </summary>
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void notClampedTest()
        public virtual void notClampedTest()
        {
            double[][] xValuesSet = new double[][]
            {
                new double[] { -5.0, -1.4, 3.2, 3.5, 7.6 },
                new double[] { 1.0, 2.0, 4.5, 12.1, 14.2 },
                new double[] { -5.2, -3.4, -3.2, -0.9, -0.2 }
            };
            double[][] yValuesSet = new double[][]
            {
                new double[] { -2.2, 1.1, 1.9, 2.3, -0.1 },
                new double[] { 3.4, 5.2, 4.3, 1.1, 0.2 },
                new double[] { 1.4, 2.2, 4.1, 1.9, 0.99 }
            };

            for (int k = 0; k < xValuesSet.Length; ++k)
            {
                double[] xValues  = Arrays.copyOf(xValuesSet[k], xValuesSet[k].Length);
                double[] yValues  = Arrays.copyOf(yValuesSet[k], yValuesSet[k].Length);
                int      nData    = xValues.Length;
                double[] xyValues = new double[nData];
                for (int j = 0; j < nData; ++j)
                {
                    xyValues[j] = xValues[j] * yValues[j];
                }
                int    nKeys    = 100;
                double interval = (xValues[nData - 1] - xValues[0]) / (nKeys - 1.0);

                int n = INTERP.Length;
                for (int i = 0; i < n; ++i)
                {
                    ProductPiecewisePolynomialInterpolator interp = new ProductPiecewisePolynomialInterpolator(INTERP[i]);
                    for (int j = 0; j < nKeys; ++j)
                    {
                        double key = xValues[0] + interval * j;
                        InterpolatorTestUtil.assertRelative("notClampedTest", INTERP[i].interpolate(xValues, xyValues, key), interp.interpolate(xValues, yValues, key), EPS);
                    }
                }
                n = INTERP_SENSE.Length;
                for (int i = 0; i < n; ++i)
                {
                    ProductPiecewisePolynomialInterpolator    interp     = new ProductPiecewisePolynomialInterpolator(INTERP_SENSE[i]);
                    PiecewisePolynomialResultsWithSensitivity result     = interp.interpolateWithSensitivity(xValues, yValues);
                    PiecewisePolynomialResultsWithSensitivity resultBase = INTERP_SENSE[i].interpolateWithSensitivity(xValues, xyValues);
                    for (int j = 0; j < nKeys; ++j)
                    {
                        double key = xValues[0] + interval * j;
                        InterpolatorTestUtil.assertRelative("notClampedTest", FUNC.evaluate(resultBase, key).get(0), FUNC.evaluate(result, key).get(0), EPS);
                        InterpolatorTestUtil.assertArrayRelative("notClampedTest", FUNC.nodeSensitivity(resultBase, key).toArray(), FUNC.nodeSensitivity(result, key).toArray(), EPS);
                    }
                }
            }
        }