使用便利类

多项式包提供的便利类有:

名字

提供

多项式的

幂级数

切比雪夫

切比雪夫系

勒让德

勒让德级数

拉盖尔

Laguerre级数

埃尔米特

厄米特统

厄米

赫米特系列

本文中的级数是相应多项式基函数乘以系数的有限和。例如,电源系列看起来像

System Message: WARNING/2 (P(x)=1+2x+3x^2)

latex exited with error [stdout] This is pdfTeX, Version 3.14159265-2.6-1.40.19 (TeX Live 2019/dev/Debian) (preloaded format=latex) restricted \write18 enabled. entering extended mode (./math.tex LaTeX2e <2018-12-01> (/usr/share/texlive/texmf-dist/tex/latex/base/article.cls Document Class: article 2018/09/03 v1.4i Standard LaTeX document class (/usr/share/texlive/texmf-dist/tex/latex/base/size12.clo)) (/usr/share/texlive/texmf-dist/tex/latex/base/inputenc.sty) (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsmath.sty For additional information on amsmath, use the `?' option. (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amstext.sty (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsgen.sty)) (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsbsy.sty) (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsopn.sty)) (/usr/share/texlive/texmf-dist/tex/latex/amscls/amsthm.sty) (/usr/share/texlive/texmf-dist/tex/latex/amsfonts/amssymb.sty (/usr/share/texlive/texmf-dist/tex/latex/amsfonts/amsfonts.sty)) (/usr/share/texlive/texmf-dist/tex/latex/anyfontsize/anyfontsize.sty) (/usr/share/texlive/texmf-dist/tex/latex/tools/bm.sty) (./math.aux) (/usr/share/texlive/texmf-dist/tex/latex/amsfonts/umsa.fd) (/usr/share/texlive/texmf-dist/tex/latex/amsfonts/umsb.fd) ! Package inputenc Error: Unicode character ( (U+FF08) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 \begin{split}P(x)=1+2x+3x^2\end{split} ! Package inputenc Error: Unicode character ) (U+FF09) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 \begin{split}P(x)=1+2x+3x^2\end{split} ! Package inputenc Error: Unicode character ( (U+FF08) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 \begin{split}P(x)=1+2x+3x^2\end{split} ! Package inputenc Error: Unicode character ) (U+FF09) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 \begin{split}P(x)=1+2x+3x^2\end{split} [1] (./math.aux) ) (see the transcript file for additional information) Output written on math.dvi (1 page, 356 bytes). Transcript written on math.log.

并且有系数 [1, 2, 3] . 具有相同系数的切比雪夫级数看起来像

System Message: WARNING/2 (P(x)=1 t_0(x)+2 t_1(x)+3 t_2(x))

latex exited with error [stdout] This is pdfTeX, Version 3.14159265-2.6-1.40.19 (TeX Live 2019/dev/Debian) (preloaded format=latex) restricted \write18 enabled. entering extended mode (./math.tex LaTeX2e <2018-12-01> (/usr/share/texlive/texmf-dist/tex/latex/base/article.cls Document Class: article 2018/09/03 v1.4i Standard LaTeX document class (/usr/share/texlive/texmf-dist/tex/latex/base/size12.clo)) (/usr/share/texlive/texmf-dist/tex/latex/base/inputenc.sty) (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsmath.sty For additional information on amsmath, use the `?' option. (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amstext.sty (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsgen.sty)) (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsbsy.sty) (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsopn.sty)) (/usr/share/texlive/texmf-dist/tex/latex/amscls/amsthm.sty) (/usr/share/texlive/texmf-dist/tex/latex/amsfonts/amssymb.sty (/usr/share/texlive/texmf-dist/tex/latex/amsfonts/amsfonts.sty)) (/usr/share/texlive/texmf-dist/tex/latex/anyfontsize/anyfontsize.sty) (/usr/share/texlive/texmf-dist/tex/latex/tools/bm.sty) (./math.aux) (/usr/share/texlive/texmf-dist/tex/latex/amsfonts/umsa.fd) (/usr/share/texlive/texmf-dist/tex/latex/amsfonts/umsb.fd) ! Package inputenc Error: Unicode character ( (U+FF08) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...�x)+2 t_1(x)+3 t_2(x)\end{split} ! Package inputenc Error: Unicode character ) (U+FF09) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...�x)+2 t_1(x)+3 t_2(x)\end{split} ! Package inputenc Error: Unicode character ( (U+FF08) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...�x)+2 t_1(x)+3 t_2(x)\end{split} ! Package inputenc Error: Unicode character ) (U+FF09) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...�x)+2 t_1(x)+3 t_2(x)\end{split} ! Package inputenc Error: Unicode character ( (U+FF08) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...�x)+2 t_1(x)+3 t_2(x)\end{split} ! Package inputenc Error: Unicode character ) (U+FF09) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...�x)+2 t_1(x)+3 t_2(x)\end{split} ! Package inputenc Error: Unicode character ( (U+FF08) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...�x)+2 t_1(x)+3 t_2(x)\end{split} ! Package inputenc Error: Unicode character ) (U+FF09) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...�x)+2 t_1(x)+3 t_2(x)\end{split} ! Package inputenc Error: Unicode character ( (U+FF08) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...�x)+2 t_1(x)+3 t_2(x)\end{split} ! Package inputenc Error: Unicode character ) (U+FF09) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...�x)+2 t_1(x)+3 t_2(x)\end{split} ! Package inputenc Error: Unicode character ( (U+FF08) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...�x)+2 t_1(x)+3 t_2(x)\end{split} ! Package inputenc Error: Unicode character ) (U+FF09) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...�x)+2 t_1(x)+3 t_2(x)\end{split} ! Package inputenc Error: Unicode character ( (U+FF08) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...�x)+2 t_1(x)+3 t_2(x)\end{split} ! Package inputenc Error: Unicode character ) (U+FF09) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...�x)+2 t_1(x)+3 t_2(x)\end{split} ! Package inputenc Error: Unicode character ( (U+FF08) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...�x)+2 t_1(x)+3 t_2(x)\end{split} ! Package inputenc Error: Unicode character ) (U+FF09) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...�x)+2 t_1(x)+3 t_2(x)\end{split} [1] (./math.aux) ) (see the transcript file for additional information) Output written on math.dvi (1 page, 388 bytes). Transcript written on math.log.

更一般地说

System Message: WARNING/2 (p(x)=\总和i=0 ^n c_i t_i(x))

latex exited with error [stdout] This is pdfTeX, Version 3.14159265-2.6-1.40.19 (TeX Live 2019/dev/Debian) (preloaded format=latex) restricted \write18 enabled. entering extended mode (./math.tex LaTeX2e <2018-12-01> (/usr/share/texlive/texmf-dist/tex/latex/base/article.cls Document Class: article 2018/09/03 v1.4i Standard LaTeX document class (/usr/share/texlive/texmf-dist/tex/latex/base/size12.clo)) (/usr/share/texlive/texmf-dist/tex/latex/base/inputenc.sty) (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsmath.sty For additional information on amsmath, use the `?' option. (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amstext.sty (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsgen.sty)) (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsbsy.sty) (/usr/share/texlive/texmf-dist/tex/latex/amsmath/amsopn.sty)) (/usr/share/texlive/texmf-dist/tex/latex/amscls/amsthm.sty) (/usr/share/texlive/texmf-dist/tex/latex/amsfonts/amssymb.sty (/usr/share/texlive/texmf-dist/tex/latex/amsfonts/amsfonts.sty)) (/usr/share/texlive/texmf-dist/tex/latex/anyfontsize/anyfontsize.sty) (/usr/share/texlive/texmf-dist/tex/latex/tools/bm.sty) (./math.aux) (/usr/share/texlive/texmf-dist/tex/latex/amsfonts/umsa.fd) (/usr/share/texlive/texmf-dist/tex/latex/amsfonts/umsb.fd) ! Package inputenc Error: Unicode character ( (U+FF08) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...��=\总和i=0 ^n c_i t_i(x)\end{split} ! Package inputenc Error: Unicode character ) (U+FF09) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...��=\总和i=0 ^n c_i t_i(x)\end{split} ! Undefined control sequence. <argument> \split@tag \begin {split}p(x)=\� ��和i=0 ^n c_i t_i(x)\en... l.14 ...��=\总和i=0 ^n c_i t_i(x)\end{split} ! Package inputenc Error: Invalid UTF-8 byte "80. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...��=\总和i=0 ^n c_i t_i(x)\end{split} ! Package inputenc Error: Invalid UTF-8 byte "BB. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...��=\总和i=0 ^n c_i t_i(x)\end{split} ! Package inputenc Error: Unicode character 和 (U+548C) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...��=\总和i=0 ^n c_i t_i(x)\end{split} ! Package inputenc Error: Unicode character ( (U+FF08) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...��=\总和i=0 ^n c_i t_i(x)\end{split} ! Package inputenc Error: Unicode character ) (U+FF09) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...��=\总和i=0 ^n c_i t_i(x)\end{split} ! Package inputenc Error: Unicode character ( (U+FF08) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...��=\总和i=0 ^n c_i t_i(x)\end{split} ! Package inputenc Error: Unicode character ) (U+FF09) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...��=\总和i=0 ^n c_i t_i(x)\end{split} ! Undefined control sequence. <argument> \split@tag \begin {split}p(x)=\� ��和i=0 ^n c_i t_i(x)\en... l.14 ...��=\总和i=0 ^n c_i t_i(x)\end{split} ! Package inputenc Error: Invalid UTF-8 byte "80. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...��=\总和i=0 ^n c_i t_i(x)\end{split} ! Package inputenc Error: Invalid UTF-8 byte "BB. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...��=\总和i=0 ^n c_i t_i(x)\end{split} ! Package inputenc Error: Unicode character 和 (U+548C) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...��=\总和i=0 ^n c_i t_i(x)\end{split} ! Package inputenc Error: Unicode character ( (U+FF08) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...��=\总和i=0 ^n c_i t_i(x)\end{split} ! Package inputenc Error: Unicode character ) (U+FF09) (inputenc) not set up for use with LaTeX. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...��=\总和i=0 ^n c_i t_i(x)\end{split} [1] (./math.aux) ) (see the transcript file for additional information) Output written on math.dvi (1 page, 376 bytes). Transcript written on math.log.

在这种情况下 T_n 切比雪夫函数是度的函数吗? n 但也可以很容易地成为任何其他类的基函数。所有类别的惯例是 c[i] 与I度的基函数一致。

所有类都是不可变的,并且具有相同的方法,尤其是它们实现了python数字运算符+、-, * ,//,%,divmod,, * * ,=,和!=最后两个可能有点问题,因为浮点舍入错误。我们现在使用numpy版本1.7.0快速演示各种操作。

基础

首先,我们需要一个多项式类和一个多项式实例。类可以直接从多项式包或相关类型的模块导入。这里我们从包中导入并使用传统的多项式类,因为它的熟悉性:

>>> from numpy.polynomial import Polynomial as P
>>> p = P([1,2,3])
>>> p
Polynomial([1., 2., 3.], domain=[-1,  1], window=[-1,  1])

请注意,打印输出的长版本有三个部分。第一个是系数,第二个是域,第三个是窗口:

>>> p.coef
array([ 1.,  2.,  3.])
>>> p.domain
array([-1.,  1.])
>>> p.window
array([-1.,  1.])

打印多项式会以更熟悉的格式生成多项式表达式:

>>> print(p)
1.0 + 2.0·x¹ + 3.0·x²

请注意,多项式的字符串表示默认使用Unicode字符(Windows除外)来表示幂和下标。还提供了基于ASCII的表示(Windows上的默认值)。多项式字符串格式可以在包级别通过 set_default_printstyle 功能:

>>> numpy.polynomial.set_default_printstyle('ascii')
>>> print(p)
1.0 + 2.0 x**1 + 3.0 x**2

或对具有字符串格式的单个多项式实例进行控制:

>>> print(f"{p:unicode}")
1.0 + 2.0·x¹ + 3.0·x²

当我们开始拟合时,我们将处理域和窗口,此时我们忽略它们并运行基本的代数和算术运算。

加减法:

>>> p + p
Polynomial([2., 4., 6.], domain=[-1.,  1.], window=[-1.,  1.])
>>> p - p
Polynomial([0.], domain=[-1.,  1.], window=[-1.,  1.])

乘法::

>>> p * p
Polynomial([ 1.,   4.,  10.,  12.,   9.], domain=[-1.,  1.], window=[-1.,  1.])

权力::

>>> p**2
Polynomial([ 1.,   4., 10., 12.,  9.], domain=[-1.,  1.], window=[-1.,  1.])

师:

楼层除法,“/”,是多项式类的除法运算符,在这方面多项式被视为整数。对于小于3.x的python版本,''operator映射到'/',就像对python一样,对于更高版本,''在某种程度上,它将被弃用:

>>> p // P([-1, 1])
Polynomial([5.,  3.], domain=[-1.,  1.], window=[-1.,  1.])

剩余部分:

>>> p % P([-1, 1])
Polynomial([6.], domain=[-1.,  1.], window=[-1.,  1.])

Divmod:

>>> quo, rem = divmod(p, P([-1, 1]))
>>> quo
Polynomial([5.,  3.], domain=[-1.,  1.], window=[-1.,  1.])
>>> rem
Polynomial([6.], domain=[-1.,  1.], window=[-1.,  1.])

评价::

>>> x = np.arange(5)
>>> p(x)
array([  1.,   6.,  17.,  34.,  57.])
>>> x = np.arange(6).reshape(3,2)
>>> p(x)
array([[  1.,   6.],
       [ 17.,  34.],
       [ 57.,  86.]])

替代:

用多项式代替X并展开结果。在这里,我们用p来代替它,在展开后得到一个新的四次多项式。如果多项式被视为函数,则这是函数的组成:

>>> p(p)
Polynomial([ 6., 16., 36., 36., 27.], domain=[-1.,  1.], window=[-1.,  1.])

根::

>>> p.roots()
array([-0.33333333-0.47140452j, -0.33333333+0.47140452j])

显式使用多项式实例并不总是很方便,因此在算术运算中,元组、列表、数组和标量都会自动强制转换:

>>> p + [1, 2, 3]
Polynomial([2., 4., 6.], domain=[-1.,  1.], window=[-1.,  1.])
>>> [1, 2, 3] * p
Polynomial([ 1.,  4., 10., 12.,  9.], domain=[-1.,  1.], window=[-1.,  1.])
>>> p / 2
Polynomial([0.5, 1. , 1.5], domain=[-1.,  1.], window=[-1.,  1.])

域、窗口或类中不同的多项式不能在算术中混合::

>>> from numpy.polynomial import Chebyshev as T
>>> p + P([1], domain=[0,1])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 213, in __add__
TypeError: Domains differ
>>> p + P([1], window=[0,1])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 215, in __add__
TypeError: Windows differ
>>> p + T([1])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 211, in __add__
TypeError: Polynomial types differ

但是可以使用不同的类型进行替换。实际上,对于类型、域和窗口转换,多项式类之间的转换是这样完成的:

>>> p(T([0, 1]))
Chebyshev([2.5, 2. , 1.5], domain=[-1.,  1.], window=[-1.,  1.])

它给出了多项式 p 以切比雪夫的形式。这是因为 T_1(x) = x 替换 x 对于 x 不会更改原始多项式。然而,所有的乘法和除法都将使用切比雪夫级数来完成,因此是结果的类型。

它的目的是所有多项式实例都是不可变的,因此增加了运算 (+=-= 等等)以及任何其他违反多项式实例不可变性的功能都是有意未实现的。

微积分

多项式实例可以集成和区分。::

>>> from numpy.polynomial import Polynomial as P
>>> p = P([2, 6])
>>> p.integ()
Polynomial([0., 2., 3.], domain=[-1.,  1.], window=[-1.,  1.])
>>> p.integ(2)
Polynomial([0., 0., 1., 1.], domain=[-1.,  1.], window=[-1.,  1.])

第一个示例集成了 p 第二个示例将它集成了两次。默认情况下,集成的下限和集成常量为0,但两者都可以指定。::

>>> p.integ(lbnd=-1)
Polynomial([-1.,  2.,  3.], domain=[-1.,  1.], window=[-1.,  1.])
>>> p.integ(lbnd=-1, k=1)
Polynomial([0., 2., 3.], domain=[-1.,  1.], window=[-1.,  1.])

在第一种情况下,积分的下限设置为-1,积分常数为0。在第二个过程中,积分常数也被设置为1。微分更简单,因为唯一的选择是多项式的微分次数:

>>> p = P([1, 2, 3])
>>> p.deriv(1)
Polynomial([2., 6.], domain=[-1.,  1.], window=[-1.,  1.])
>>> p.deriv(2)
Polynomial([6.], domain=[-1.,  1.], window=[-1.,  1.])

其他多项式构造函数

通过指定系数构造多项式只是获得多项式实例的一种方法,也可以通过指定其根、从其他多项式类型转换以及通过最小二乘拟合来创建多项式实例。拟合在本节中进行了讨论,其他方法如下所示:

>>> from numpy.polynomial import Polynomial as P
>>> from numpy.polynomial import Chebyshev as T
>>> p = P.fromroots([1, 2, 3])
>>> p
Polynomial([-6., 11., -6.,  1.], domain=[-1.,  1.], window=[-1.,  1.])
>>> p.convert(kind=T)
Chebyshev([-9.  , 11.75, -3.  ,  0.25], domain=[-1.,  1.], window=[-1.,  1.])

convert方法还可以转换域和窗口:

>>> p.convert(kind=T, domain=[0, 1])
Chebyshev([-2.4375 ,  2.96875, -0.5625 ,  0.03125], domain=[0.,  1.], window=[-1.,  1.])
>>> p.convert(kind=P, domain=[0, 1])
Polynomial([-1.875,  2.875, -1.125,  0.125], domain=[0.,  1.], window=[-1.,  1.])

在numpy版本中>=1.7.0 basiscast 类方法也可用。cast方法的工作方式与convert方法类似,而basis方法返回给定次数的基多项式:

>>> P.basis(3)
Polynomial([0., 0., 0., 1.], domain=[-1.,  1.], window=[-1.,  1.])
>>> T.cast(p)
Chebyshev([-9.  , 11.75, -3. ,  0.25], domain=[-1.,  1.], window=[-1.,  1.])

类型之间的转换可能很有用,但是 not 建议日常使用。从50度切比雪夫级数到同阶多项式级数的数值精度损失,可以使数值计算结果基本上是随机的。

适合的

试衣是因为 domainwindow 属性是便利类的一部分。为了说明这个问题,下面绘制了切比雪夫多项式5阶的值。

>>> import matplotlib.pyplot as plt
>>> from numpy.polynomial import Chebyshev as T
>>> x = np.linspace(-1, 1, 100)
>>> for i in range(6): ax = plt.plot(x, T.basis(i)(x), lw=2, label="$T_%d$"%i)
...
>>> plt.legend(loc="upper left")
<matplotlib.legend.Legend object at 0x3b3ee10>
>>> plt.show()
../_images/routines-polynomials-classes-1.png

在-1范围内<= x <=1它们是很好的,等波纹函数位于+/-1之间。相同的图在范围-2<= x <=2看起来非常不同:

>>> import matplotlib.pyplot as plt
>>> from numpy.polynomial import Chebyshev as T
>>> x = np.linspace(-2, 2, 100)
>>> for i in range(6): ax = plt.plot(x, T.basis(i)(x), lw=2, label="$T_%d$"%i)
...
>>> plt.legend(loc="lower right")
<matplotlib.legend.Legend object at 0x3b3ee10>
>>> plt.show()
../_images/routines-polynomials-classes-2.png

可以看出,“好”的部分已经缩小到微不足道。在使用切比雪夫多项式进行拟合时,我们希望使用 x 介于-1和1之间,这就是 window 指定。但是,要拟合的数据不太可能在该时间间隔内具有所有数据点,因此我们使用 domain 指定数据点所在的间隔。拟合完成后,首先通过线性变换将域映射到窗口,然后使用映射的数据点进行常用的最小二乘拟合。拟合的窗口和域是返回序列的一部分,并且在计算值、导数等时自动使用。如果在调用中没有指定,拟合例程将使用默认窗口和保存所有数据点的最小域。下面说明了适合于噪声正弦曲线的情况。

>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> from numpy.polynomial import Chebyshev as T
>>> np.random.seed(11)
>>> x = np.linspace(0, 2*np.pi, 20)
>>> y = np.sin(x) + np.random.normal(scale=.1, size=x.shape)
>>> p = T.fit(x, y, 5)
>>> plt.plot(x, y, 'o')
[<matplotlib.lines.Line2D object at 0x2136c10>]
>>> xx, yy = p.linspace()
>>> plt.plot(xx, yy, lw=2)
[<matplotlib.lines.Line2D object at 0x1cf2890>]
>>> p.domain
array([ 0.        ,  6.28318531])
>>> p.window
array([-1.,  1.])
>>> plt.show()
../_images/routines-polynomials-classes-3.png