Example #1
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);
        }
//JAVA TO C# CONVERTER WARNING: 'final' parameters are not available in .NET:
//ORIGINAL LINE: @Override public PiecewisePolynomialResult interpolate(final double[] xValues, final double[] yValues)
        public override PiecewisePolynomialResult interpolate(double[] xValues, double[] yValues)
        {
            ArgChecker.notNull(xValues, "xValues");
            ArgChecker.notNull(yValues, "yValues");

            ArgChecker.isTrue(xValues.Length == yValues.Length | xValues.Length + 2 == yValues.Length, "(xValues length = yValues length) or (xValues length + 2 = yValues length)");
            ArgChecker.isTrue(xValues.Length > 2, "Data points should be more than 2");

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int nDataPts = xValues.length;
            int nDataPts = xValues.Length;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int yValuesLen = yValues.length;
            int yValuesLen = yValues.Length;

            for (int i = 0; i < nDataPts; ++i)
            {
                ArgChecker.isFalse(double.IsNaN(xValues[i]), "xValues containing NaN");
                ArgChecker.isFalse(double.IsInfinity(xValues[i]), "xValues containing Infinity");
            }
            for (int i = 0; i < yValuesLen; ++i)
            {
                ArgChecker.isFalse(double.IsNaN(yValues[i]), "yValues containing NaN");
                ArgChecker.isFalse(double.IsInfinity(yValues[i]), "yValues containing Infinity");
            }

            for (int i = 0; i < nDataPts - 1; ++i)
            {
                for (int j = i + 1; j < nDataPts; ++j)
                {
                    ArgChecker.isFalse(xValues[i] == xValues[j], "xValues should be distinct");
                }
            }

            double[] xValuesSrt = Arrays.copyOf(xValues, nDataPts);
            double[] yValuesSrt = new double[nDataPts];
            if (nDataPts == yValuesLen)
            {
                yValuesSrt = Arrays.copyOf(yValues, nDataPts);
            }
            else
            {
                yValuesSrt = Arrays.copyOfRange(yValues, 1, nDataPts + 1);
            }
            DoubleArrayMath.sortPairs(xValuesSrt, yValuesSrt);

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double[] intervals = _solver.intervalsCalculator(xValuesSrt);
            double[] intervals = _solver.intervalsCalculator(xValuesSrt);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double[] slopes = _solver.slopesCalculator(yValuesSrt, intervals);
            double[] slopes = _solver.slopesCalculator(yValuesSrt, intervals);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final PiecewisePolynomialResult result = _method.interpolate(xValues, yValues);
            PiecewisePolynomialResult result = _method.interpolate(xValues, yValues);

            ArgChecker.isTrue(result.Order == 4, "Primary interpolant is not cubic");

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double[] initialFirst = _function.differentiate(result, xValuesSrt).rowArray(0);
            double[] initialFirst = _function.differentiate(result, xValuesSrt).rowArray(0);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double[] first = firstDerivativeCalculator(yValuesSrt, intervals, slopes, initialFirst);
            double[] first = firstDerivativeCalculator(yValuesSrt, intervals, slopes, initialFirst);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double[][] coefs = _solver.solve(yValuesSrt, intervals, slopes, first);
            double[][] coefs = _solver.solve(yValuesSrt, intervals, slopes, first);

            for (int i = 0; i < nDataPts - 1; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    ArgChecker.isFalse(double.IsNaN(coefs[i][j]), "Too large input");
                    ArgChecker.isFalse(double.IsInfinity(coefs[i][j]), "Too large input");
                }
            }

            return(new PiecewisePolynomialResult(DoubleArray.copyOf(xValuesSrt), DoubleMatrix.copyOf(coefs), 4, 1));
        }