<commit_before>/*
 * 
 * Copyright (c) 2004 The Apache Software Foundation. All rights reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy
 * of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 *  
 */
package org.apache.commons.math.analysis;

import java.util.Arrays;
import junit.framework.TestCase;

import org.apache.commons.math.FunctionEvaluationException;

/**
 * Tests the PolynomialSplineFunction implementation.
 *
 * @version $Revision: 1.1 $
 */
public class PolynomialSplineFunctionTest extends TestCase {

    /** Error tolerance for tests */
    protected double tolerance = 1.0e-12;
    
    /** 
     * Quadratic polynomials used in tests: 
     * 
     * x^2 + x            [-1, 0)
     * x^2 + x + 2        [0, 1)
     * x^2 + x + 4        [1, 2)
     * 
     * Defined so that evaluation using PolynomialSplineFunction evaluation
     * algorithm agrees at knot point boundaries.
     */
    protected PolynomialFunction[] polynomials = {
        new PolynomialFunction(new double[] {0d, 1d, 1d}), 
        new PolynomialFunction(new double[] {2d, 1d, 1d}),
        new PolynomialFunction(new double[] {4d, 1d, 1d})
    };
    
    /** Knot points  */
    protected double[] knots = {-1, 0, 1, 2};
    
    /** Derivative of test polynomials -- 2x + 1  */
    protected PolynomialFunction dp = 
        new PolynomialFunction(new double[] {1d, 2d});
    
    
    public void testConstructor() {
        PolynomialSplineFunction spline = 
            new PolynomialSplineFunction(knots, polynomials);
        assertTrue(Arrays.equals(knots, spline.getKnots()));
        assertEquals(1d, spline.getPolynomials()[0].getCoefficients()[2], 0);
        
        try { // too few knots
            spline = 
                new PolynomialSplineFunction(new double[] {0}, polynomials);
            fail("Expecting IllegalArgumentException");
        } catch (IllegalArgumentException ex) {
            // expected
        }
        
        try { // too many knots
            spline = 
                new PolynomialSplineFunction(new double[] {0,1,2,3,4}, polynomials);
            fail("Expecting IllegalArgumentException");
        } catch (IllegalArgumentException ex) {
            // expected<commit_msg>Add test for getN.<commit_after>/*
 * 
 * Copyright (c) 2004 The Apache Software Foundation. All rights reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy
 * of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 *  
 */
package org.apache.commons.math.analysis;

import java.util.Arrays;
import junit.framework.TestCase;

import org.apache.commons.math.FunctionEvaluationException;

/**
 * Tests the PolynomialSplineFunction implementation.
 *
 * @version $Revision: 1.2 $
 */
public class PolynomialSplineFunctionTest extends TestCase {

    /** Error tolerance for tests */
    protected double tolerance = 1.0e-12;
    
    /** 
     * Quadratic polynomials used in tests: 
     * 
     * x^2 + x            [-1, 0)
     * x^2 + x + 2        [0, 1)
     * x^2 + x + 4        [1, 2)
     * 
     * Defined so that evaluation using PolynomialSplineFunction evaluation
     * algorithm agrees at knot point boundaries.
     */
    protected PolynomialFunction[] polynomials = {
        new PolynomialFunction(new double[] {0d, 1d, 1d}), 
        new PolynomialFunction(new double[] {2d, 1d, 1d}),
        new PolynomialFunction(new double[] {4d, 1d, 1d})
    };
    
    /** Knot points  */
    protected double[] knots = {-1, 0, 1, 2};
    
    /** Derivative of test polynomials -- 2x + 1  */
    protected PolynomialFunction dp = 
        new PolynomialFunction(new double[] {1d, 2d});
    
    
    public void testConstructor() {
        PolynomialSplineFunction spline = 
            new PolynomialSplineFunction(knots, polynomials);
        assertTrue(Arrays.equals(knots, spline.getKnots()));
        assertEquals(1d, spline.getPolynomials()[0].getCoefficients()[2], 0);
        assertEquals(3, spline.getN());
        
        try { // too few knots
            spline = 
                new PolynomialSplineFunction(new double[] {0}, polynomials);
            fail("Expecting IllegalArgumentException");
        } catch (IllegalArgumentException ex) {
            // expected
        }
        
        try { // too many knots
            spline = 
                new PolynomialSplineFunction(new double[] {0,1,2,3,4}, polynomials);
            fail("Expecting IllegalArgumentException");
        } catch (IllegalArgumentException ex) {
            // expected
{'fraction_non_alphanumeric': 0.10153221340225217, 'fraction_numerical': 0.01938342255861178, 'mean_word_length': 2.4864864864864864, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6442484', 'n_tokens_mistral': 1599, 'n_tokens_neox': 1476, 'n_words': 480}