numpy.gradient

numpy.gradient(f, *varargs, axis=None, edge_order=1)[源代码]

返回n维数组的渐变。

使用内部点的二阶精确中心差和边界处的一阶或二阶精确一侧(向前或向后)差计算梯度。因此,返回的渐变与输入数组具有相同的形状。

参数
farray_like

包含标量函数样本的n维数组。

varargs标量或数组列表,可选

f值之间的间距。所有尺寸的默认单位间距。间距可通过以下方式指定:

  1. 单标量指定所有维度的采样距离。

  2. n个标量指定每个维度的恒定采样距离。即 dxdydz ,…

  3. n数组,指定沿f的每个维度的值的坐标。数组的长度必须与相应维度的大小匹配。

  4. n个标量/数组的任意组合,含义为2。3。

如果 axis 如果给定,varargs的数目必须等于轴的数目。默认值:1。

edge_order1,2,可选

利用边界处的n阶精确差计算梯度。默认值:1。

1.9.1 新版功能.

axis无、int或int的元组,可选

渐变仅沿给定轴计算。默认值(轴=无)是计算输入数组所有轴的渐变。轴可以是负的,在这种情况下,它从最后一个轴计数到第一个轴。

1.11.0 新版功能.

返回
gradient日历或日历列表

一组数据数组(如果只有一个维度,则为单个数据数组),对应于f对每个维度的导数。每个导数的形状都与f相同。

笔记

假设 f\in C^{{3}} (即, f 至少有3个连续的导数)并让 h_{{*}} 作为一个非均匀的台阶尺寸,我们将“一致性误差”最小化。 \eta_{{i}} 从相邻网格点的线性组合得出的真实梯度及其估计值之间:

System Message: WARNING/2 (\eta{i}=f{i}^{\左(1\右)}-)

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) ! Undefined control sequence. <argument> ...@tag \begin {split}\eta {i}=f{i}^{\� ��(1\右)}-\end {split} l.14 ...\eta{i}=f{i}^{\左(1\右)}-\end{split} ! Package inputenc Error: Invalid UTF-8 byte "B7. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...\eta{i}=f{i}^{\左(1\右)}-\end{split} ! Package inputenc Error: Invalid UTF-8 byte "A6. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...\eta{i}=f{i}^{\左(1\右)}-\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 ...\eta{i}=f{i}^{\左(1\右)}-\end{split} ! Undefined control sequence. <argument> ...gin {split}\eta {i}=f{i}^{\左(1\� ��)}-\end {split} l.14 ...\eta{i}=f{i}^{\左(1\右)}-\end{split} ! Package inputenc Error: Invalid UTF-8 byte "8F. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...\eta{i}=f{i}^{\左(1\右)}-\end{split} ! Package inputenc Error: Invalid UTF-8 byte "B3. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...\eta{i}=f{i}^{\左(1\右)}-\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 ...\eta{i}=f{i}^{\左(1\右)}-\end{split} ! Undefined control sequence. <argument> ...@tag \begin {split}\eta {i}=f{i}^{\� ��(1\右)}-\end {split} l.14 ...\eta{i}=f{i}^{\左(1\右)}-\end{split} ! Package inputenc Error: Invalid UTF-8 byte "B7. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...\eta{i}=f{i}^{\左(1\右)}-\end{split} ! Package inputenc Error: Invalid UTF-8 byte "A6. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...\eta{i}=f{i}^{\左(1\右)}-\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 ...\eta{i}=f{i}^{\左(1\右)}-\end{split} ! Undefined control sequence. <argument> ...gin {split}\eta {i}=f{i}^{\左(1\� ��)}-\end {split} l.14 ...\eta{i}=f{i}^{\左(1\右)}-\end{split} ! Package inputenc Error: Invalid UTF-8 byte "8F. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...\eta{i}=f{i}^{\左(1\右)}-\end{split} ! Package inputenc Error: Invalid UTF-8 byte "B3. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 ...\eta{i}=f{i}^{\左(1\右)}-\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 ...\eta{i}=f{i}^{\左(1\右)}-\end{split} [1] (./math.aux) ) (see the transcript file for additional information) Output written on math.dvi (1 page, 396 bytes). Transcript written on math.log.

代用 f(x_{{i}} + h_{{d}})f(x_{{i}} - h_{{s}}) 通过泰勒级数展开,这转化为求解以下线性系统:

System Message: WARNING/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) ! Undefined control sequence. <argument> \split@tag \begin {split}\� ��\{\end {split} l.14 \begin{split}\左\{\end{split} ! Package inputenc Error: Invalid UTF-8 byte "B7. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 \begin{split}\左\{\end{split} ! Package inputenc Error: Invalid UTF-8 byte "A6. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 \begin{split}\左\{\end{split} ! Undefined control sequence. <argument> \split@tag \begin {split}\� ��\{\end {split} l.14 \begin{split}\左\{\end{split} ! Package inputenc Error: Invalid UTF-8 byte "B7. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 \begin{split}\左\{\end{split} ! Package inputenc Error: Invalid UTF-8 byte "A6. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 \begin{split}\左\{\end{split} [1] (./math.aux) ) (see the transcript file for additional information) Output written on math.dvi (1 page, 236 bytes). Transcript written on math.log.

得出的近似值 f_{{i}}^{{(1)}} 如下所示:

System Message: WARNING/2 (\帽子{i}^{(1)}=)

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) ! Undefined control sequence. <argument> \split@tag \begin {split}\� ��子{i}^{(1)}=\end {split} l.14 \begin{split}\帽子{i}^{(1)}=\end{split} ! Package inputenc Error: Invalid UTF-8 byte "B8. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 \begin{split}\帽子{i}^{(1)}=\end{split} ! Package inputenc Error: Invalid UTF-8 byte "BD. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 \begin{split}\帽子{i}^{(1)}=\end{split} ! Package inputenc Error: Unicode character 子 (U+5B50) (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}\帽子{i}^{(1)}=\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}\帽子{i}^{(1)}=\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}\帽子{i}^{(1)}=\end{split} ! Undefined control sequence. <argument> \split@tag \begin {split}\� ��子{i}^{(1)}=\end {split} l.14 \begin{split}\帽子{i}^{(1)}=\end{split} ! Package inputenc Error: Invalid UTF-8 byte "B8. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 \begin{split}\帽子{i}^{(1)}=\end{split} ! Package inputenc Error: Invalid UTF-8 byte "BD. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 \begin{split}\帽子{i}^{(1)}=\end{split} ! Package inputenc Error: Unicode character 子 (U+5B50) (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}\帽子{i}^{(1)}=\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}\帽子{i}^{(1)}=\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}\帽子{i}^{(1)}=\end{split} [1] (./math.aux) ) (see the transcript file for additional information) Output written on math.dvi (1 page, 332 bytes). Transcript written on math.log.

值得注意的是,如果 h_{{s}}=h_{{d}} (即,数据是均匀分布的)我们找到了标准的二阶近似值:

System Message: WARNING/2 (\帽子{i}^{(1)}=)

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) ! Undefined control sequence. <argument> \split@tag \begin {split}\� ��子{i}^{(1)}=\end {split} l.14 \begin{split}\帽子{i}^{(1)}=\end{split} ! Package inputenc Error: Invalid UTF-8 byte "B8. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 \begin{split}\帽子{i}^{(1)}=\end{split} ! Package inputenc Error: Invalid UTF-8 byte "BD. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 \begin{split}\帽子{i}^{(1)}=\end{split} ! Package inputenc Error: Unicode character 子 (U+5B50) (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}\帽子{i}^{(1)}=\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}\帽子{i}^{(1)}=\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}\帽子{i}^{(1)}=\end{split} ! Undefined control sequence. <argument> \split@tag \begin {split}\� ��子{i}^{(1)}=\end {split} l.14 \begin{split}\帽子{i}^{(1)}=\end{split} ! Package inputenc Error: Invalid UTF-8 byte "B8. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 \begin{split}\帽子{i}^{(1)}=\end{split} ! Package inputenc Error: Invalid UTF-8 byte "BD. See the inputenc package documentation for explanation. Type H <return> for immediate help. ... l.14 \begin{split}\帽子{i}^{(1)}=\end{split} ! Package inputenc Error: Unicode character 子 (U+5B50) (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}\帽子{i}^{(1)}=\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}\帽子{i}^{(1)}=\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}\帽子{i}^{(1)}=\end{split} [1] (./math.aux) ) (see the transcript file for additional information) Output written on math.dvi (1 page, 332 bytes). Transcript written on math.log.

使用类似的程序,可以导出用于边界的前向/后向近似值。

工具书类

1

Quartroni A.、Sacco R.、Saleri F.(2007)数值数学(应用数学课本)。纽约:斯普林格。

2

Durran D.R.(1999)地球物理流体动力学中波动方程的数值方法。纽约:斯普林格。

3

Fornberg B.(1988)任意间距网格上有限差分公式的生成,计算数学51,第184号:699-706。 PDF .

实例

>>> f = np.array([1, 2, 4, 7, 11, 16], dtype=float)
>>> np.gradient(f)
array([1. , 1.5, 2.5, 3.5, 4.5, 5. ])
>>> np.gradient(f, 2)
array([0.5 ,  0.75,  1.25,  1.75,  2.25,  2.5 ])

间距也可以用一个数组指定,该数组表示沿尺寸的值f的坐标。例如,均匀间距:

>>> x = np.arange(f.size)
>>> np.gradient(f, x)
array([1. ,  1.5,  2.5,  3.5,  4.5,  5. ])

或不统一的:

>>> x = np.array([0., 1., 1.5, 3.5, 4., 6.], dtype=float)
>>> np.gradient(f, x)
array([1. ,  3. ,  3.5,  6.7,  6.9,  2.5])

对于二维数组,返回将是按轴排序的两个数组。在本例中,第一个数组代表行中的渐变,第二个数组代表列方向的渐变:

>>> np.gradient(np.array([[1, 2, 6], [3, 4, 5]], dtype=float))
[array([[ 2.,  2., -1.],
       [ 2.,  2., -1.]]), array([[1. , 2.5, 4. ],
       [1. , 1. , 1. ]])]

在此示例中,还指定了间距:轴=0为均匀间距,轴=1为非均匀间距。

>>> dx = 2.
>>> y = [1., 1.5, 3.5]
>>> np.gradient(np.array([[1, 2, 6], [3, 4, 5]], dtype=float), dx, y)
[array([[ 1. ,  1. , -0.5],
       [ 1. ,  1. , -0.5]]), array([[2. , 2. , 2. ],
       [2. , 1.7, 0.5]])]

可以指定如何使用 edge_order

>>> x = np.array([0, 1, 2, 3, 4])
>>> f = x**2
>>> np.gradient(f, edge_order=1)
array([1.,  2.,  4.,  6.,  7.])
>>> np.gradient(f, edge_order=2)
array([0., 2., 4., 6., 8.])

这个 axis 关键字可用于指定计算渐变的轴的子集

>>> np.gradient(np.array([[1, 2, 6], [3, 4, 5]], dtype=float), axis=0)
array([[ 2.,  2., -1.],
       [ 2.,  2., -1.]])