亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Commons Math學習筆記——多項式函數

系統 2077 0

2.2 多項式函數

看其他篇章到 目錄 選擇。

Commons Math 中的 analysis.polynomials 包中有所有的與多項式函數相關的類和接口定義。這一篇主要從這個包分析,來研究一下多項式函數的應用。

Commons Math學習筆記——多項式函數

?

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

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

?

?
?1 protected ? 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
?

測試代碼示例如下:

?1 /**?*/ /**
?2 ?*?
?3 ? */

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

14 public ? 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? = ? {? - 1 ,? 0 ,? 1 ,? 2 ?} ;
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.0 ,? 6.0 ,? - 2.0 ,? 1.0 ?} ;
57 ???????? double []?f2_coeff? = ? {? 1.0 ,? 2.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 類也是重寫了 toString 方法和 hashCode equals 方法的。

PolynomialSplineFunction 類是多項式樣條函數, 樣條 是一種特殊的函數,由多項式分段定義。表示了一個由多個多項式組成的樣條曲線。它的實現主要是內部定義了一個多項式函數組 PolynomialFunction polynomials[] 和一個樣條分界節點數組 double knots[] 。這兩個內部成員分別表示什么呢?分界節點表示整條曲線對應在 x 等于 knots[i] 的時候開始使用其他多項式樣條,其構造方法 public PolynomialSplineFunction( double knots[], PolynomialFunction polynomials[]) 完成這樣的功能。

舉例來說,一個多項式樣條函數就是一個分段函數:

????? X^2+x??? [-1,0)

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

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

當然,構造方法中的參數, knots[] 數組必須是遞增的。

可以看到,直接輸出 PolynomialSplineFunction 是多么丑陋啊 ~~ ,因為它沒有重寫 toString 方法。同樣,它的導數也是一樣的丑陋。其中如果給定的值不在定義域內, value 方法還拋出異常 ArgumentOutsideDomainException

最后 PolynomialFunctionLagrangeForm PolynomialFunctionNewtonForm 類完成的其實是多項式插值的功能,放到下一節研究的。

相關資料:

多項式: 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

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

Horner Methods http://mathworld.wolfram.com/HornersMethod.html

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

Commons Math學習筆記——多項式函數


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 色综合亚洲七七久久桃花影院 | 91sao国产在线观看 | 美女被cao的视频免费看 | 口国产成人高清在线播放 | 久久精品伊人波多野结 | 四虎永久精品免费观看 | 亚洲日本免费 | 国产一级视频 | 啪啪99久久综合精品色 | 天天干天天色综合 | 中国护士一级毛片免费版本 | 男女很黄很色床视频网站免 | 国产性较精品视频免费 | ova熟肉动漫在线 | 色播性播爱播放影院 | 久久免费福利视频 | 久久毛片网站 | 91精品成人福利在线播放 | 天天干天天拍天天射 | 九热这里只有精品 | 女人18毛片a级毛片 女人18毛片a级毛片免费 | 国产一区二区在线观看视频 | 天天色综合天天 | 日本夜爽爽一区二区三区 | 国产在线91观看免费观看 | 中文字幕在线观看国产 | 99九九精品视频 | 亚洲三级中文字幕 | 女人十八毛片一级毛片免费看 | 女网址www女影院 | 欧美激情在线免费 | 九九九九热精品免费视频 | 在线久综合色手机在线播放 | 在线亚洲成人 | 亚洲美女在线观看播放 | 天天夜天干天天爽 | 国产精品_国产精品_国产精品 | 曰韩三级 | 日韩欧美国产精品第一页不卡 | 久久伊人在 | 国产亚洲精品一区二区久久 |