//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test(expectedExceptions = IllegalArgumentException.class) public void testFunctionIndexOutOfRange2()
        public virtual void testFunctionIndexOutOfRange2()
        {
            BasisFunctionKnots k = BasisFunctionKnots.fromKnots(KNOTS, 5);
            int nS = k.NumSplines;

            GENERATOR.generate(k, nS);
        }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void testInternalKnots()
        public virtual void testInternalKnots()
        {
            BasisFunctionKnots knots = BasisFunctionKnots.fromInternalKnots(KNOTS, 2);

            assertEquals(2, knots.Degree);
            assertEquals(15, knots.NumKnots);
            assertEquals(12, knots.NumSplines);
        }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void testUniform()
        public virtual void testUniform()
        {
            BasisFunctionKnots knots = BasisFunctionKnots.fromUniform(1.0, 2.0, 10, 3);

            assertEquals(3, knots.Degree);
            assertEquals(16, knots.NumKnots);
            assertEquals(12, knots.NumSplines);
        }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void testKnots()
        public virtual void testKnots()
        {
            BasisFunctionKnots knots = BasisFunctionKnots.fromKnots(KNOTS, 3);

            assertEquals(3, knots.Degree);
            assertEquals(11, knots.NumKnots);
            assertEquals(7, knots.NumSplines);
            ArrayAsserts.assertArrayEquals(KNOTS, knots.Knots, 1e-15);
        }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void testFirstOrder()
        public virtual void testFirstOrder()
        {
            BasisFunctionKnots knots = BasisFunctionKnots.fromInternalKnots(KNOTS, 1);

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.util.function.Function<double, double> func = GENERATOR.generate(knots, 3);
            System.Func <double, double> func = GENERATOR.generate(knots, 3);
            assertEquals(0.0, func(1.76), 0.0);
            assertEquals(1.0, func(3.0), 0.0);
            assertEquals(0, func(4.0), 0.0);
            assertEquals(0.5, func(2.5), 0.0);
        }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void testThreeD()
        public virtual void testThreeD()
        {
            BasisFunctionKnots knots1 = BasisFunctionKnots.fromInternalKnots(KNOTS, 2);
            BasisFunctionKnots knots2 = BasisFunctionKnots.fromInternalKnots(KNOTS, 3);
            BasisFunctionKnots knots3 = BasisFunctionKnots.fromInternalKnots(KNOTS, 1);
            IList <System.Func <double[], double> > set = GENERATOR.generateSet(new BasisFunctionKnots[] { knots1, knots2, knots3 });

            //pick of one of the basis functions for testing
            int index = FunctionUtils.toTensorIndex(new int[] { 3, 3, 3 }, new int[] { knots1.NumSplines, knots2.NumSplines, knots3.NumSplines });

            System.Func <double[], double> func = set[index];
            assertEquals(1.0 / 3.0, func(new double[] { 2.0, 2.0, 3.0 }), 0.0);
        }
Example #7
0
        /// <summary>
        /// Generate a set of b-splines with a given polynomial degree on the specified knots. </summary>
        /// <param name="knots"> holder for the knots and degree </param>
        /// <returns> a List of functions </returns>
        public virtual IList <System.Func <double, double> > generateSet(BasisFunctionKnots knots)
        {
            ArgChecker.notNull(knots, "knots");

            double[] k = knots.Knots;
            IList <System.Func <double, double> > set = null;

            for (int d = 0; d <= knots.Degree; d++)
            {
                set = generateSet(k, d, set);
            }
            return(set);
        }
        /// <summary>
        /// Given a set of data {x_i ,y_i} where each x_i is a vector and the y_i are scalars, we wish to find a function (represented
        /// by B-splines) that fits the data while maintaining smoothness in each direction. </summary>
        /// <param name="x"> The independent (vector) variables, as List&lt;double[]> </param>
        /// <param name="y"> The dependent variables, as List&lt;Double> y </param>
        /// <param name="sigma"> The error (or tolerance) on the y variables </param>
        /// <param name="xa">  The lowest value of x in each dimension </param>
        /// <param name="xb"> The highest value of x in each dimension </param>
        /// <param name="nKnots"> Number of knots in each dimension (note, the actual number of basis splines and thus fitted weights,
        ///   equals nKnots + degree-1) </param>
        /// <param name="degree"> The degree of the basis function in each dimension - 0 is piecewise constant, 1 is a sawtooth function
        ///   (i.e. two straight lines joined in the middle), 2 gives three quadratic sections joined together, etc. For a large
        ///   value of degree, the basis function tends to a gaussian </param>
        /// <param name="lambda"> The weight given to the penalty function in each dimension </param>
        /// <param name="differenceOrder"> applies the penalty the nth order difference in the weights, so a differenceOrder of 2
        ///   will penalize large 2nd derivatives etc. A difference differenceOrder can be used in each dimension </param>
        /// <returns> The results of the fit </returns>
        public virtual GeneralizedLeastSquareResults <double[]> solve(IList <double[]> x, IList <double> y, IList <double> sigma, double[] xa, double[] xb, int[] nKnots, int[] degree, double[] lambda, int[] differenceOrder)
        {
            BasisFunctionKnots[] knots = new BasisFunctionKnots[xa.Length];
            for (int i = 0; i < xa.Length; i++)
            {
                knots[i] = BasisFunctionKnots.fromUniform(xa[i], xb[i], nKnots[i], degree[i]);
            }
            IList <System.Func <double[], double> > bSplines = _generator.generateSet(knots);

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

            int[] sizes = new int[dim];
            for (int i = 0; i < dim; i++)
            {
                sizes[i] = nKnots[i] + degree[i] - 1;
            }
            return(_gls.solve(x, y, sigma, bSplines, sizes, lambda, differenceOrder));
        }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test(expectedExceptions = IllegalArgumentException.class) public void testDegreeToHigh2()
        public virtual void testDegreeToHigh2()
        {
            BasisFunctionKnots.fromInternalKnots(KNOTS, 11);
        }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test(expectedExceptions = IllegalArgumentException.class) public void testDegreeToHigh1()
        public virtual void testDegreeToHigh1()
        {
            BasisFunctionKnots.fromUniform(0.0, 10.0, 11, 11);
        }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test(expectedExceptions = IllegalArgumentException.class) public void testWrongOrderInternalKnots()
        public virtual void testWrongOrderInternalKnots()
        {
            BasisFunctionKnots.fromInternalKnots(WRONG_ORDER_KNOTS, 3);
        }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test(expectedExceptions = IllegalArgumentException.class) public void testWrongOrderUniform()
        public virtual void testWrongOrderUniform()
        {
            BasisFunctionKnots.fromUniform(2.0, 1.0, 10, 3);
        }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test(expectedExceptions = IllegalArgumentException.class) public void testNegDegree2()
        public virtual void testNegDegree2()
        {
            BasisFunctionKnots.fromInternalKnots(KNOTS, -1);
        }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test(expectedExceptions = IllegalArgumentException.class) public void testNegDegree()
        public virtual void testNegDegree()
        {
            BasisFunctionKnots.fromKnots(KNOTS, -1);
        }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test(expectedExceptions = IllegalArgumentException.class) public void testNullInternalKnots()
        public virtual void testNullInternalKnots()
        {
            BasisFunctionKnots.fromInternalKnots(null, 2);
        }
        /// <summary>
        /// Fits a curve to x-y data. </summary>
        /// <param name="x"> The independent variables </param>
        /// <param name="y"> The dependent variables </param>
        /// <param name="sigma"> The error (or tolerance) on the y variables </param>
        /// <param name="xa"> The lowest value of x </param>
        /// <param name="xb"> The highest value of x </param>
        /// <param name="nKnots"> Number of knots (note, the actual number of basis splines and thus fitted weights, equals nKnots + degree-1) </param>
        /// <param name="degree"> The degree of the basis function - 0 is piecewise constant, 1 is a sawtooth function (i.e. two straight lines joined in the middle), 2 gives three
        ///   quadratic sections joined together, etc. For a large value of degree, the basis function tends to a gaussian </param>
        /// <param name="lambda"> The weight given to the penalty function </param>
        /// <param name="differenceOrder"> applies the penalty the nth order difference in the weights, so a differenceOrder of 2 will penalise large 2nd derivatives etc </param>
        /// <returns> The results of the fit </returns>
        public virtual GeneralizedLeastSquareResults <double> solve(IList <double> x, IList <double> y, IList <double> sigma, double xa, double xb, int nKnots, int degree, double lambda, int differenceOrder)
        {
            IList <System.Func <double, double> > bSplines = _generator.generateSet(BasisFunctionKnots.fromUniform(xa, xb, nKnots, degree));

            return(_gls.solve(x, y, sigma, bSplines, lambda, differenceOrder));
        }
Example #17
0
        /// <summary>
        /// Generate the i^th basis function </summary>
        /// <param name="data"> Container for the knots and degree of the basis function </param>
        /// <param name="index"> The index (from zero) of the function. Must be in range 0 to data.getNumSplines() (exclusive)
        ///   For example if the degree is 1, and index is 0, this will cover the first three knots. </param>
        /// <returns> The i^th basis function </returns>
//JAVA TO C# CONVERTER WARNING: 'final' parameters are not available in .NET:
//ORIGINAL LINE: protected java.util.function.Function<double, double> generate(BasisFunctionKnots data, final int index)
        protected internal virtual System.Func <double, double> generate(BasisFunctionKnots data, int index)
        {
            ArgChecker.notNull(data, "data");
            ArgChecker.isTrue(index >= 0 && index < data.NumSplines, "index must be in range {} to {} (exclusive)", 0, data.NumSplines);
            return(generate(data.Knots, data.Degree, index));
        }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test(expectedExceptions = IllegalArgumentException.class) public void testDegreeToHigh3()
        public virtual void testDegreeToHigh3()
        {
            BasisFunctionKnots.fromKnots(KNOTS, 11);
        }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test(expectedExceptions = IllegalArgumentException.class) public void testFunctionIndexOutOfRange1()
        public virtual void testFunctionIndexOutOfRange1()
        {
            BasisFunctionKnots k = BasisFunctionKnots.fromKnots(KNOTS, 2);

            GENERATOR.generate(k, -1);
        }