///
        public virtual void crossDerivativeTest()
        {
            double[] x0Values = new double[] { 1.0, 2.0, 3.0, 4.0 };
            double[] x1Values = new double[] { -1.0, 0.0, 1.0, 2.0, 3.0 };
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int n0Data = x0Values.length;
            int n0Data = x0Values.Length;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int n1Data = x1Values.length;
            int n1Data = x1Values.Length;

            double[][] yValues = new double[][]
            {
                new double[] { 1.0, -1.0, 0.0, 1.0, 0.0 },
                new double[] { 1.0, -1.0, 0.0, 1.0, -2.0 },
                new double[] { 1.0, -2.0, 0.0, -2.0, -2.0 },
                new double[] { -1.0, -1.0, -2.0, -2.0, -1.0 }
            };

            NaturalSplineInterpolator         method = new NaturalSplineInterpolator();
            PiecewisePolynomialInterpolator2D interp = new BicubicSplineInterpolator(method);
            PiecewisePolynomialResult2D       result = interp.interpolate(x0Values, x1Values, yValues);

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int n0IntExp = n0Data - 1;
            int n0IntExp = n0Data - 1;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int n1IntExp = n1Data - 1;
            int       n1IntExp = n1Data - 1;
            const int orderExp = 4;

            const int n0Keys = 51;
            const int n1Keys = 61;

            double[] x0Keys = new double[n0Keys];
            double[] x1Keys = new double[n1Keys];
            for (int i = 0; i < n0Keys; ++i)
            {
                x0Keys[i] = 0.0 + 5.0 * i / (n0Keys - 1);
            }
            for (int i = 0; i < n1Keys; ++i)
            {
                x1Keys[i] = -2.0 + 6.0 * i / (n1Keys - 1);
            }

            assertEquals(result.NumberOfIntervals[0], n0IntExp);
            assertEquals(result.NumberOfIntervals[1], n1IntExp);
            assertEquals(result.Order[0], orderExp);
            assertEquals(result.Order[1], orderExp);

            for (int i = 0; i < n0Data; ++i)
            {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double ref = Math.abs(x0Values[i]) == 0.0 ? 1.0 : Math.abs(x0Values[i]);
                double @ref = Math.Abs(x0Values[i]) == 0.0 ? 1.0 : Math.Abs(x0Values[i]);
                assertEquals(result.Knots0.get(i), x0Values[i], @ref * EPS);
                assertEquals(result.Knots2D[0].get(i), x0Values[i], @ref * EPS);
            }
            for (int i = 0; i < n1Data; ++i)
            {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double ref = Math.abs(x1Values[i]) == 0.0 ? 1.0 : Math.abs(x1Values[i]);
                double @ref = Math.Abs(x1Values[i]) == 0.0 ? 1.0 : Math.Abs(x1Values[i]);
                assertEquals(result.Knots1.get(i), x1Values[i], @ref * EPS);
                assertEquals(result.Knots2D[1].get(i), x1Values[i], @ref * EPS);
            }

            for (int i = 0; i < n0Data - 1; ++i)
            {
                for (int j = 0; j < n1Data - 1; ++j)
                {
                    double @ref = Math.Abs(yValues[i][j]) == 0.0 ? 1.0 : Math.Abs(yValues[i][j]);
                    assertEquals(result.Coefs[i][j].get(orderExp - 1, orderExp - 1), yValues[i][j], @ref * EPS);
                }
            }

            DoubleMatrix resValues = interp.interpolate(x0Values, x1Values, yValues, x0Values, x1Values);
            PiecewisePolynomialFunction2D func2D = new PiecewisePolynomialFunction2D();
            DoubleMatrix resDiffX0 = func2D.differentiateX0(result, x0Values, x1Values);
            DoubleMatrix resDiffX1 = func2D.differentiateX1(result, x0Values, x1Values);

            PiecewisePolynomialFunction1D func1D = new PiecewisePolynomialFunction1D();
            DoubleMatrix expDiffX0 = func1D.differentiate(method.interpolate(x0Values, OG_ALGEBRA.getTranspose(DoubleMatrix.copyOf(yValues)).toArray()), x0Values);
            DoubleMatrix expDiffX1 = func1D.differentiate(method.interpolate(x1Values, yValues), x1Values);

            for (int i = 0; i < n0Data; ++i)
            {
                for (int j = 0; j < n1Data; ++j)
                {
                    double expVal = expDiffX1.get(i, j);
                    double @ref   = Math.Abs(expVal) == 0.0 ? 1.0 : Math.Abs(expVal);
                    assertEquals(resDiffX1.get(i, j), expVal, @ref * EPS);
                }
            }

            for (int i = 0; i < n0Data; ++i)
            {
                for (int j = 0; j < n1Data; ++j)
                {
                    double expVal = expDiffX0.get(j, i);
                    double @ref   = Math.Abs(expVal) == 0.0 ? 1.0 : Math.Abs(expVal);
                    assertEquals(resDiffX0.get(i, j), expVal, @ref * EPS);
                }
            }

            for (int i = 0; i < n0Data; ++i)
            {
                for (int j = 0; j < n1Data; ++j)
                {
                    double expVal = yValues[i][j];
                    double @ref   = Math.Abs(expVal) == 0.0 ? 1.0 : Math.Abs(expVal);
                    assertEquals(resValues.get(i, j), expVal, @ref * EPS);
                }
            }
        }
Ejemplo n.º 2
0
//JAVA TO C# CONVERTER WARNING: 'final' parameters are not available in .NET:
//ORIGINAL LINE: @Override public PiecewisePolynomialResult2D interpolate(final double[] x0Values, final double[] x1Values, final double[][] yValues)
        public override PiecewisePolynomialResult2D interpolate(double[] x0Values, double[] x1Values, double[][] yValues)
        {
            ArgChecker.notNull(x0Values, "x0Values");
            ArgChecker.notNull(x1Values, "x1Values");
            ArgChecker.notNull(yValues, "yValues");

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int nData0 = x0Values.length;
            int nData0 = x0Values.Length;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int nData1 = x1Values.length;
            int nData1 = x1Values.Length;

            DoubleMatrix yValuesMatrix = DoubleMatrix.copyOf(yValues);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final com.opengamma.strata.math.impl.function.PiecewisePolynomialFunction1D func = new com.opengamma.strata.math.impl.function.PiecewisePolynomialFunction1D();
            PiecewisePolynomialFunction1D func = new PiecewisePolynomialFunction1D();

//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[][] diff0 = new double[nData1][nData0];
            double[][] diff0 = RectangularArrays.ReturnRectangularDoubleArray(nData1, nData0);
//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[][] diff1 = new double[nData0][nData1];
            double[][] diff1 = RectangularArrays.ReturnRectangularDoubleArray(nData0, nData1);
//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[][] cross = new double[nData0][nData1];
            double[][] cross = RectangularArrays.ReturnRectangularDoubleArray(nData0, nData1);

            PiecewisePolynomialResult result0 = _method[0].interpolate(x0Values, OG_ALGEBRA.getTranspose(yValuesMatrix).toArray());

            diff0 = func.differentiate(result0, x0Values).toArray();

            PiecewisePolynomialResult result1 = _method[1].interpolate(x1Values, yValuesMatrix.toArray());

            diff1 = func.differentiate(result1, x1Values).toArray();

            const int order = 4;

            for (int i = 0; i < nData0; ++i)
            {
                for (int j = 0; j < nData1; ++j)
                {
                    if (yValues[i][j] == 0.0)
                    {
                        if (diff0[j][i] == 0.0)
                        {
                            cross[i][j] = diff1[i][j];
                        }
                        else
                        {
                            if (diff1[i][j] == 0.0)
                            {
                                cross[i][j] = diff0[j][i];
                            }
                            else
                            {
                                cross[i][j] = Math.Sign(diff0[j][i] * diff1[i][j]) * Math.Sqrt(Math.Abs(diff0[j][i] * diff1[i][j]));
                            }
                        }
                    }
                    else
                    {
                        cross[i][j] = diff0[j][i] * diff1[i][j] / yValues[i][j];
                    }
                }
            }

//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: DoubleMatrix[][] coefMat = new DoubleMatrix[nData0 - 1][nData1 - 1];
            DoubleMatrix[][] coefMat = RectangularArrays.ReturnRectangularDoubleMatrixArray(nData0 - 1, nData1 - 1);
            for (int i = 0; i < nData0 - 1; ++i)
            {
                for (int j = 0; j < nData1 - 1; ++j)
                {
                    double[] diffsVec = new double[16];
                    for (int l = 0; l < 2; ++l)
                    {
                        for (int m = 0; m < 2; ++m)
                        {
                            diffsVec[l + 2 * m] = yValues[i + l][j + m];
                        }
                    }
                    for (int l = 0; l < 2; ++l)
                    {
                        for (int m = 0; m < 2; ++m)
                        {
                            diffsVec[4 + l + 2 * m] = diff0[j + m][i + l];
                        }
                    }
                    for (int l = 0; l < 2; ++l)
                    {
                        for (int m = 0; m < 2; ++m)
                        {
                            diffsVec[8 + l + 2 * m] = diff1[i + l][j + m];
                        }
                    }
                    for (int l = 0; l < 2; ++l)
                    {
                        for (int m = 0; m < 2; ++m)
                        {
                            diffsVec[12 + l + 2 * m] = cross[i + l][j + m];
                        }
                    }
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final com.opengamma.strata.collect.array.DoubleArray diffs = com.opengamma.strata.collect.array.DoubleArray.copyOf(diffsVec);
                    DoubleArray diffs = DoubleArray.copyOf(diffsVec);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final com.opengamma.strata.collect.array.DoubleArray ansVec = ((com.opengamma.strata.collect.array.DoubleArray) OG_ALGEBRA.multiply(INV_MAT, diffs));
                    DoubleArray ansVec = ((DoubleArray)OG_ALGEBRA.multiply(INV_MAT, diffs));

                    double @ref = 0.0;
//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[][] coefMatTmp = new double[order][order];
                    double[][] coefMatTmp = RectangularArrays.ReturnRectangularDoubleArray(order, order);
                    for (int l = 0; l < order; ++l)
                    {
                        for (int m = 0; m < order; ++m)
                        {
                            coefMatTmp[order - l - 1][order - m - 1] = ansVec.get(l + m * (order)) / Math.Pow((x0Values[i + 1] - x0Values[i]), l) / Math.Pow((x1Values[j + 1] - x1Values[j]), m);
                            ArgChecker.isFalse(double.IsNaN(coefMatTmp[order - l - 1][order - m - 1]), "Too large/small input");
                            ArgChecker.isFalse(double.IsInfinity(coefMatTmp[order - l - 1][order - m - 1]), "Too large/small input");
                            @ref += coefMatTmp[order - l - 1][order - m - 1] * Math.Pow((x0Values[i + 1] - x0Values[i]), l) * Math.Pow((x1Values[j + 1] - x1Values[j]), m);
                        }
                    }
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double bound = Math.max(Math.abs(ref) + Math.abs(yValues[i + 1][j + 1]), 0.1);
                    double bound = Math.Max(Math.Abs(@ref) + Math.Abs(yValues[i + 1][j + 1]), 0.1);
                    ArgChecker.isTrue(Math.Abs(@ref - yValues[i + 1][j + 1]) < ERROR * bound, "Input is too large/small or data points are too close");
                    coefMat[i][j] = DoubleMatrix.copyOf(coefMatTmp);
                }
            }

            return(new PiecewisePolynomialResult2D(DoubleArray.copyOf(x0Values), DoubleArray.copyOf(x1Values), coefMat, new int[] { order, order }));
        }
        /// <summary>
        /// f(x0,x1) = ( x0 - 1.)^3 * (x1  + 14./13.)^3
        /// </summary>
        public virtual void cubicTest()
        {
            double[] x0Values = new double[] { 1.0, 2.0, 3.0, 4.0 };
            double[] x1Values = new double[] { -1.0, 0.0, 1.0, 2.0, 3.0 };
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int n0Data = x0Values.length;
            int n0Data = x0Values.Length;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int n1Data = x1Values.length;
            int n1Data = x1Values.Length;

//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[][] yValues = new double[n0Data][n1Data];
            double[][] yValues = RectangularArrays.ReturnRectangularDoubleArray(n0Data, n1Data);

            for (int i = 0; i < n0Data; ++i)
            {
                for (int j = 0; j < n1Data; ++j)
                {
                    yValues[i][j] = (x0Values[i] - 1.0) * (x0Values[i] - 1.0) * (x0Values[i] - 1.0) * (x1Values[j] + 14.0 / 13.0) * (x1Values[j] + 14.0 / 13.0) * (x1Values[j] + 14.0 / 13.0);
                }
            }

            CubicSplineInterpolator           method = new CubicSplineInterpolator();
            PiecewisePolynomialInterpolator2D interp = new BicubicSplineInterpolator(method);
            PiecewisePolynomialResult2D       result = interp.interpolate(x0Values, x1Values, yValues);

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int n0IntExp = n0Data - 1;
            int n0IntExp = n0Data - 1;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int n1IntExp = n1Data - 1;
            int       n1IntExp = n1Data - 1;
            const int orderExp = 4;

            const int n0Keys = 51;
            const int n1Keys = 61;

            double[] x0Keys = new double[n0Keys];
            double[] x1Keys = new double[n1Keys];
            for (int i = 0; i < n0Keys; ++i)
            {
                x0Keys[i] = 0.0 + 5.0 * i / (n0Keys - 1);
            }
            for (int i = 0; i < n1Keys; ++i)
            {
                x1Keys[i] = -2.0 + 6.0 * i / (n1Keys - 1);
            }

            assertEquals(result.NumberOfIntervals[0], n0IntExp);
            assertEquals(result.NumberOfIntervals[1], n1IntExp);
            assertEquals(result.Order[0], orderExp);
            assertEquals(result.Order[1], orderExp);

            for (int i = 0; i < n0Data; ++i)
            {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double ref = Math.abs(x0Values[i]) == 0.0 ? 1.0 : Math.abs(x0Values[i]);
                double @ref = Math.Abs(x0Values[i]) == 0.0 ? 1.0 : Math.Abs(x0Values[i]);
                assertEquals(result.Knots0.get(i), x0Values[i], @ref * EPS);
                assertEquals(result.Knots2D[0].get(i), x0Values[i], @ref * EPS);
            }
            for (int i = 0; i < n1Data; ++i)
            {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double ref = Math.abs(x1Values[i]) == 0.0 ? 1.0 : Math.abs(x1Values[i]);
                double @ref = Math.Abs(x1Values[i]) == 0.0 ? 1.0 : Math.Abs(x1Values[i]);
                assertEquals(result.Knots1.get(i), x1Values[i], @ref * EPS);
                assertEquals(result.Knots2D[1].get(i), x1Values[i], @ref * EPS);
            }

            for (int i = 0; i < n0Data - 1; ++i)
            {
                for (int j = 0; j < n1Data - 1; ++j)
                {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double ref = Math.abs(yValues[i][j]) == 0.0 ? 1.0 : Math.abs(yValues[i][j]);
                    double @ref = Math.Abs(yValues[i][j]) == 0.0 ? 1.0 : Math.Abs(yValues[i][j]);
                    assertEquals(result.Coefs[i][j].get(orderExp - 1, orderExp - 1), yValues[i][j], @ref * EPS);
                }
            }

            DoubleMatrix resValues = interp.interpolate(x0Values, x1Values, yValues, x0Values, x1Values);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final com.opengamma.strata.math.impl.function.PiecewisePolynomialFunction2D func2D = new com.opengamma.strata.math.impl.function.PiecewisePolynomialFunction2D();
            PiecewisePolynomialFunction2D func2D = new PiecewisePolynomialFunction2D();
            DoubleMatrix resDiffX0 = func2D.differentiateX0(result, x0Values, x1Values);
            DoubleMatrix resDiffX1 = func2D.differentiateX1(result, x0Values, x1Values);

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final com.opengamma.strata.math.impl.function.PiecewisePolynomialFunction1D func1D = new com.opengamma.strata.math.impl.function.PiecewisePolynomialFunction1D();
            PiecewisePolynomialFunction1D func1D = new PiecewisePolynomialFunction1D();
            DoubleMatrix expDiffX0 = func1D.differentiate(method.interpolate(x0Values, OG_ALGEBRA.getTranspose(DoubleMatrix.copyOf(yValues)).toArray()), x0Values);
            DoubleMatrix expDiffX1 = func1D.differentiate(method.interpolate(x1Values, yValues), x1Values);

            for (int i = 0; i < n0Data; ++i)
            {
                for (int j = 0; j < n1Data; ++j)
                {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double expVal = expDiffX1.get(i, j);
                    double expVal = expDiffX1.get(i, j);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double ref = Math.abs(expVal) == 0.0 ? 1.0 : Math.abs(expVal);
                    double @ref = Math.Abs(expVal) == 0.0 ? 1.0 : Math.Abs(expVal);
                    assertEquals(resDiffX1.get(i, j), expVal, @ref * EPS);
                }
            }

            for (int i = 0; i < n0Data; ++i)
            {
                for (int j = 0; j < n1Data; ++j)
                {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double expVal = expDiffX0.get(j, i);
                    double expVal = expDiffX0.get(j, i);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double ref = Math.abs(expVal) == 0.0 ? 1.0 : Math.abs(expVal);
                    double @ref = Math.Abs(expVal) == 0.0 ? 1.0 : Math.Abs(expVal);
                    assertEquals(resDiffX0.get(i, j), expVal, @ref * EPS);
                }
            }

            for (int i = 0; i < n0Data; ++i)
            {
                for (int j = 0; j < n1Data; ++j)
                {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double expVal = yValues[i][j];
                    double expVal = yValues[i][j];
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double ref = Math.abs(expVal) == 0.0 ? 1.0 : Math.abs(expVal);
                    double @ref = Math.Abs(expVal) == 0.0 ? 1.0 : Math.Abs(expVal);
                    assertEquals(resValues.get(i, j), expVal, @ref * EPS);
                }
            }
        }