在 Python 中對多項式求導


在 Python Numpy 中,可以使用 polynomial.polyder() 方法對多項式求導。沿軸對多項式係數 c 求 m 次導數。在每次迭代中,結果都乘以 scl(縮放因子用於變數的線性變化)。引數 c 是一個從低到高次冪的係數陣列,例如,[1,2,3] 表示多項式 1 + 2*x + 3*x**2,而 [[1,2],[1,2]] 表示 1 + 1*x + 2*y + 2*x*y(如果 axis=0 是 x,axis=1 是 y)。

該方法返回導數的多項式係數。第一個引數 c 是一個多項式係數陣列。如果 c 是多維的,則不同的軸對應於不同的變數,每個軸的次數由相應的索引給出。第二個引數 m 是導數的次數,必須是非負數。(預設值:1)

第三個引數是 scl。每個微分都乘以 scl。最終結果是乘以 scl**m。這用於變數的線性變化。(預設值:1)。第四個引數是 axis。它是求導的軸。(預設值:0)。結果是 (d/dx)(c) = 2 + 6x + 12x**2

步驟

首先,匯入所需的庫 -

import numpy as np
from numpy.polynomial import polynomial as P

建立一個多項式係數陣列,即 1 + 2x + 3x**2 + 4x**3 -

c = np.array([1,2,3,4])

顯示係數陣列 -

print("Our coefficient Array...\n",c)

檢查維度 -

print("\nDimensions of our Array...\n",c.ndim)

獲取資料型別 -

print("\nDatatype of our Array object...\n",c.dtype)

獲取形狀 -

print("\nShape of our Array object...\n",c.shape)

在 Python Numpy 中,可以使用 polynomial.polyder() 方法對多項式求導 -

print("\nResult...\n",P.polyder(c))

示例

import numpy as np
from numpy.polynomial import polynomial as P

# Create an array of polynomial coefficients i.e.
# 1 + 2x + 3x**2 + 4x**3
c = np.array([1,2,3,4])

# Display the coefficient array
print("Our coefficient Array...\n",c)

# Check the Dimensions
print("\nDimensions of our Array...\n",c.ndim)

# Get the Datatype
print("\nDatatype of our Array object...\n",c.dtype)

# Get the Shape
print("\nShape of our Array object...\n",c.shape)

# To differentiate a polynomial, use the polynomial.polyder() method in Python Numpy.
# Return the polynomial coefficients c differentiated m times along axis. At each iteration the result is multiplied by scl (the scaling factor is for use in a linear change of variable). The argument c is an array of coefficients from low to high degree along each axis, e.g., [1,2,3] represents the polynomial 1 + 2*x + 3*x**2 while [[1,2],[1,2]] represents 1 + 1*x + 2*y + 2*x*y if axis=0 is x and axis=1 is y.

print("\nResult...\n",P.polyder(c))

輸出

Our coefficient Array...
   [1 2 3 4]

Dimensions of our Array...
1

Datatype of our Array object...
int64

Shape of our Array object...
(4,)

Result...
   [ 2. 6. 12.]

更新時間: 2022-03-01

924 次瀏覽

啟動您的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.