Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

อนุพันธ์พหุนามที่มีสัมประสิทธิ์หลายมิติใน Python

หากต้องการหาอนุพันธ์ของพหุนาม ให้ใช้เมธอด polynomial.polyder() ใน Python NumPy

คืนค่าสัมประสิทธิ์พหุนาม c ที่ถูกหาอนุพันธ์ m ครั้งตามแกน axis ที่แต่ละรอบผลลัพธ์จะคูณด้วย scl อาร์กิวเมนต์ c เป็นอาร์เรย์สัมประสิทธิ์จากอันดับต่ำไปสูง ตัวอย่าง [1,2,3] แทนพหุนาม 1 + 2x + 3x² ในขณะที่ [[1,2],[1,2]] แทน 1 + 1x + 2y + 2xy หาก axis=0 คือ x และ axis=1 คือ y

พารามิเตอร์

  • c: อาร์เรย์สัมประสิทธิ์พหุนาม หากหลายมิติ แกนต่างๆ ตรงกับตัวแปรต่างๆ
  • m: จำนวนครั้งของการหาอนุพันธ์ (default: 1)
  • scl: ตัวคูณสเกล (default: 1)
  • axis: แกนที่หาอนุพันธ์ (default: 0)

ขั้นตอนการใช้งาน

1. นำเข้าไลบรารีที่จำเป็น

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

2. สร้างอาร์เรย์สัมประสิทธิ์พหุนามหลายมิติ

c = np.arange(4).reshape(2, 2)

3. แสดงอาร์เรย์สัมประสิทธิ์

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

4. ตรวจสอบมิติ

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

5. ตรวจสอบชนิดข้อมูล

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

6. ตรวจสอบรูปร่าง

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

7. หาอนุพันธ์ (default m=1, axis=0) ด้วย polynomial.polyder()

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

ตัวอย่างโค้ดเต็ม

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

# สร้างอาร์เรย์สัมประสิทธิ์พหุนามหลายมิติ
c = np.arange(4).reshape(2, 2)

# แสดงอาร์เรย์สัมประสิทธิ์
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)

# หาอนุพันธ์
print("\nResult...\n", P.polyder(c))

ผลลัพธ์ที่ได้

Our coefficient Array...
 [[0 1]
 [2 3]]
Dimensions of our Array...
 2
Datatype of our Array object...
 int64
Shape of our Array object...
 (2, 2)
Result...
 [[2. 3.]]

คำอธิบายเพิ่มเติม

อาร์เรย์สัมประสิทธิ์ c (shape 2×2):

[[0 1]
 [2 3]]

ถูกตีความเป็นพหุนาม 2 ตัวแปร (x, y) โดย axis=0 คือ x, axis=1 คือ y:

P(x,y) = 0·x⁰y⁰ + 1·x⁰y¹ + 2·x¹y⁰ + 3·x¹y¹ = y + 2x + 3xy

หาอนุพันธ์ตาม axis=0 (x) (default):

∂P/∂x = 2 + 3y

สัมประสิทธิ์: [[2, 3]] (shape 1×2) ตรงกับผลลัพธ์

  • ค่าคงที่ (y⁰): 2
  • สัมประสิทธิ์ y¹: 3

หากหาอนุพันธ์ตาม axis=1 (y) จะได้ [[1, 3]] (แทน 1 + 3x)

การใช้งาน: การคำนวณอนุพันธ์บางส่วน (partial derivatives) ทางคณิตศาสตร์และฟิสิกส์

อนุพันธ์พหุนามที่มีสัมประสิทธิ์หลายมิติใน Python

ใช้ polynomial.polyder() หาอนุพันธ์ตาม axis=0 ของพหุนาม 2 มิติ y + 2x + 3xy ได้ ∂P/∂x = 2 + 3y สัมประสิทธิ์ [[2, 3]]