Change Dir

          先知cd——熱愛(ài)生活是一切藝術(shù)的開(kāi)始

          統(tǒng)計(jì)

          留言簿(18)

          積分與排名

          “?!眰兊牟┛?/h3>

          各個(gè)公司技術(shù)

          我的鏈接

          淘寶技術(shù)

          閱讀排行榜

          評(píng)論排行榜

          Commons Math學(xué)習(xí)筆記——多項(xiàng)式函數(shù)

           

          2.2 多項(xiàng)式函數(shù)

          看其他篇章到目錄選擇。

          Commons Math中的analysis.polynomials包中有所有的與多項(xiàng)式函數(shù)相關(guān)的類(lèi)和接口定義。這一篇主要從這個(gè)包分析,來(lái)研究一下多項(xiàng)式函數(shù)的應(yīng)用。



           

          Polynomials包中沒(méi)有interface的定義,下屬含有5個(gè)類(lèi):PolynomialFunctionPolynomialFunctionLagrangeForm、PolynomialFunctionNewtonForm、PolynomialSplineFunctionPolynomialsUtils。其中主要的只有PolynomialFunctionPolynomialSplineFunction,正如api doc中的介紹,PolynomialFunction類(lèi)是Immutable representation of a real polynomial function with real coefficients——實(shí)數(shù)多項(xiàng)式的表示;PolynomialSplineFunction類(lèi)是Represents a polynomial spline function.——樣條曲線(xiàn)多項(xiàng)式的表示。另外兩個(gè)表示拉格朗日和牛頓形式的多項(xiàng)式函數(shù)。而PolynomialsUtils類(lèi)中提供了幾個(gè)構(gòu)造個(gè)別(比如切比雪夫多項(xiàng)式)多項(xiàng)式的靜態(tài)方法。

          我覺(jué)得最常用的應(yīng)該就是實(shí)數(shù)系數(shù)的多項(xiàng)式了,因此以PolynomialFunction類(lèi)為例來(lái)進(jìn)行分析。實(shí)數(shù)系數(shù)的多項(xiàng)式函數(shù)形如:f(x) = ax^2 + bx + c。PolynomialFunction類(lèi)實(shí)現(xiàn)了DifferentiableUnivariateRealFunction接口,因此必須實(shí)現(xiàn)value()derivative()方法,并且實(shí)現(xiàn)該接口也表明這是一元可微分的實(shí)數(shù)函數(shù)形式。PolynomialFunction類(lèi)定義了一組final double coefficients[]作為多項(xiàng)式系數(shù),其中coefficients[0]表示常數(shù)項(xiàng)的系數(shù),coefficients[n]表示指數(shù)為nx^n次項(xiàng)的系數(shù)。因此,這個(gè)類(lèi)所表達(dá)的多項(xiàng)式函數(shù)是這樣的:f(x)=coeff[0] + coeff[1]x + coeff[2]x^2 + … + coeff[n]x^n。它的構(gòu)造方法是PolynomialFunction(double [])就是接受這樣的coefficients數(shù)組作為系數(shù)輸入?yún)?shù)來(lái)構(gòu)造多項(xiàng)式的。這個(gè)是很好表達(dá)也很方便理解的。那么它的value(double x)方法是通過(guò)調(diào)用double evaluate(double[] coefficients, double argument)實(shí)現(xiàn)的,本質(zhì)用Horner's Method求解多項(xiàng)式的值,沒(méi)有什么技術(shù)難點(diǎn),非常好理解的一個(gè)給定參數(shù)和函數(shù)求值過(guò)程。剩余定義的一些加減乘等操作,都是通過(guò)一個(gè)類(lèi)似public PolynomialFunction add(final PolynomialFunction p)這樣的結(jié)構(gòu)實(shí)現(xiàn)的。求導(dǎo)數(shù)的方法derivative()是通過(guò)這樣的一個(gè)微分操作實(shí)現(xiàn)的。見(jiàn)源碼:


           
           1protected static double[] differentiate(double[] coefficients) {
           2        int n = coefficients.length;
           3        if (n < 1{
           4            throw MathRuntimeException.createIllegalArgumentException("empty polynomials coefficients array");
           5        }

           6        if (n == 1{
           7            return new double[]{0};
           8        }

           9        double[] result = new double[n - 1];
          10        for (int i = n - 1; i  > 0; i--{
          11            result[i - 1= i * coefficients[i];
          12        }

          13        return result;
          14    }

          15
           

          測(cè)試代碼示例如下:

           1/**
           2 * 
           3 */

           4package algorithm.math;
           5
           6import org.apache.commons.math.ArgumentOutsideDomainException;
           7import org.apache.commons.math.analysis.polynomials.PolynomialFunction;
           8import org.apache.commons.math.analysis.polynomials.PolynomialSplineFunction;
           9
          10/**
          11 * @author Jia Yu
          12 * @date 2010-11-21
          13 */

          14public class PolinomialsFunctionTest {
          15
          16    /**
          17     * @param args
          18     */

          19    public static void main(String[] args) {
          20        // TODO Auto-generated method stub
          21        polynomials();
          22        System.out.println("-----------------------------------------------");
          23        polynomialsSpline();
          24    }

          25
          26    private static void polynomialsSpline() {
          27        // TODO Auto-generated method stub
          28        PolynomialFunction[] polynomials = {
          29                new PolynomialFunction(new double[] { 0d, 1d, 1d }),
          30                new PolynomialFunction(new double[] { 2d, 1d, 1d }),
          31                new PolynomialFunction(new double[] { 4d, 1d, 1d }) }
          ;
          32        double[] knots = -1012 };
          33        PolynomialSplineFunction spline = new PolynomialSplineFunction(knots,
          34                polynomials);
          35        //output directly
          36        System.out.println("poly spline func is "+spline);
          37        // get the value when x = 0.5
          38        try {
          39            System.out.println("f(0.5) = "+spline.value(0.5));
          40        }
           catch (ArgumentOutsideDomainException e) {
          41            // TODO Auto-generated catch block
          42            e.printStackTrace();
          43        }

          44        // the number of spline segments
          45        System.out.println("spline segments number is "+spline.getN());
          46        // the polynomials functions
          47        for(int i=0;i<spline.getN();i++){
          48            System.out.println("spline:f"+i+"(x) = "+spline.getPolynomials()[i]);
          49        }

          50        //function derivative
          51        System.out.println("spline func derivative is "+spline.derivative());
          52    }

          53
          54    private static void polynomials() {
          55        // TODO Auto-generated method stub
          56        double[] f1_coeff = 3.06.0-2.01.0 };
          57        double[] f2_coeff = 1.02.0-1.0-2.0 };
          58        PolynomialFunction f1 = new PolynomialFunction(f1_coeff);
          59        PolynomialFunction f2 = new PolynomialFunction(f2_coeff);
          60        // output directly
          61        System.out.println("f1(x) is : " + f1);
          62        System.out.println("f2(x) is : " + f2);
          63        // polynomial degree
          64        System.out.println("f1(x)'s degree is " + f1.degree());
          65        // get the value when x = 2
          66        System.out.println("f1(2) = " + f1.value(2));
          67        // function add
          68        System.out.println("f1(x)+f2(x) = " + f1.add(f2));
          69        // function substract
          70        System.out.println("f1(x)-f2(x) = " + f1.subtract(f2));
          71        // function multiply
          72        System.out.println("f1(x)*f2(x) = " + f1.multiply(f2));
          73        // function derivative
          74        System.out.println("f1'(x) = " + f1.derivative());
          75        System.out.println("f2''(x) = "
          76                + ((PolynomialFunction) f2.derivative()).derivative());
          77
          78    }

          79
          80}

          81

          輸出如下:
          f1(x) is : 3.0 + 6.0 x - 2.0 x^2 + x^3
          f2(x) is : 1.0 + 2.0 x - x^2 - 2.0 x^3
          f1(x)'s degree is 3
          f1(2) = 15.0
          f1(x)+f2(x) = 4.0 + 8.0 x - 3.0 x^2 - x^3
          f1(x)-f2(x) = 2.0 + 4.0 x - x^2 + 3.0 x^3
          f1(x)*f2(x) = 3.0 + 12.0 x + 7.0 x^2 - 15.0 x^3 - 8.0 x^4 + 3.0 x^5 - 2.0 x^6
          f1'(x) = 6.0 - 4.0 x + 3.0 x^2
          f2''(x) = -2.0 - 12.0 x
          -----------------------------------------------
          poly spline func is org.apache.commons.math.analysis.polynomials.PolynomialSplineFunction@69b332
          f(0.5) = 2.75
          spline segments number is 3
          spline:f0(x) = x + x^2
          spline:f1(x) = 2.0 + x + x^2
          spline:f2(x) = 4.0 + x + x^2
          spline func derivative is org.apache.commons.math.analysis.polynomials.PolynomialSplineFunction@173a10f

          PolynomialFunction類(lèi)也是重寫(xiě)了toString方法和hashCodeequals方法的。

          PolynomialSplineFunction類(lèi)是多項(xiàng)式樣條函數(shù),樣條是一種特殊的函數(shù),由多項(xiàng)式分段定義。表示了一個(gè)由多個(gè)多項(xiàng)式組成的樣條曲線(xiàn)。它的實(shí)現(xiàn)主要是內(nèi)部定義了一個(gè)多項(xiàng)式函數(shù)組PolynomialFunction polynomials[]和一個(gè)樣條分界節(jié)點(diǎn)數(shù)組double knots[]。這兩個(gè)內(nèi)部成員分別表示什么呢?分界節(jié)點(diǎn)表示整條曲線(xiàn)對(duì)應(yīng)在x等于knots[i]的時(shí)候開(kāi)始使用其他多項(xiàng)式樣條,其構(gòu)造方法public PolynomialSplineFunction(double knots[], PolynomialFunction polynomials[])完成這樣的功能。

          舉例來(lái)說(shuō),一個(gè)多項(xiàng)式樣條函數(shù)就是一個(gè)分段函數(shù):

                X^2+x    [-1,0)

          F(x) = x^2+x+2   [0,1)

                X^2+x+4 [1,2)

          當(dāng)然,構(gòu)造方法中的參數(shù),knots[]數(shù)組必須是遞增的。

          可以看到,直接輸出PolynomialSplineFunction是多么丑陋啊~~,因?yàn)樗鼪](méi)有重寫(xiě)toString方法。同樣,它的導(dǎo)數(shù)也是一樣的丑陋。其中如果給定的值不在定義域內(nèi),value方法還拋出異常ArgumentOutsideDomainException。

          最后PolynomialFunctionLagrangeFormPolynomialFunctionNewtonForm類(lèi)完成的其實(shí)是多項(xiàng)式插值的功能,放到下一節(jié)研究的。

          相關(guān)資料:

          多項(xiàng)式:http://zh.wikipedia.org/zh-cn/%E5%A4%9A%E9%A1%B9%E5%BC%8F%E5%87%BD%E6%95%B0#.E5.A4.9A.E9.A0.85.E5.BC.8F.E5.87.BD.E6.95.B8.E5.8F.8A.E5.A4.9A.E9.A0.85.E5.BC.8F.E7.9A.84.E6.A0.B9

          樣條函數(shù):http://zh.wikipedia.org/zh-cn/%E6%A0%B7%E6%9D%A1%E5%87%BD%E6%95%B0

          Horner Methodshttp://mathworld.wolfram.com/HornersMethod.html

          Commons math包:http://commons.apache.org/math/index.html

          posted on 2010-12-15 10:48 changedi 閱讀(2924) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): 數(shù)學(xué)

          主站蜘蛛池模板: 连平县| 青海省| 阜南县| 同心县| 潢川县| 图们市| 葵青区| 新邵县| 昭通市| 晋江市| 西充县| 富裕县| 宁蒗| 佛山市| 沾益县| 清原| 灵寿县| 徐州市| 辰溪县| 邓州市| 德保县| 东安县| 扎兰屯市| 卢湾区| 永胜县| 汤阴县| 嘉善县| 石柱| 容城县| 武义县| 崇阳县| 探索| 平阴县| 江山市| 双鸭山市| 丰镇市| 哈巴河县| 新乐市| 柳江县| 团风县| 革吉县|