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

หาปริพันธ์ของพหุนามใน Python

บทนำ

ในการหาปริพันธ์ (integrate) ของพหุนาม สามารถใช้เมธอด polynomial.polyint() ใน Python นี่คือการใช้งานพื้นฐานกับค่าเริ่มต้นทั้งหมด

พารามิเตอร์สำคัญ

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

ขั้นตอนการทำงาน

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

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

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

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

3. แสดงข้อมูลของอาร์เรย์

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)

4. หาปริพันธ์พหุนาม (ค่าเริ่มต้น)

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

ตัวอย่างโค้ดสมบูรณ์

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

# สร้างอาร์เรย์สัมประสิทธิ์พหุนาม
c = np.array([1, 2, 3])

# แสดงข้อมูลอาร์เรย์
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.polyint(c))

ผลลัพธ์

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

สรุป

พหุนามต้นฉบับ 1 + 2x + 3x² การปริพันธ์ 1 ครั้ง (m=1) ด้วยค่าเริ่มต้นทั้งหมด ให้ผลลัพธ์ [0, 1, 1, 1] ซึ่งแทนพหุนาม 0 + x + x² + x³ (ค่าคงที่เป็น 0 เนื่องจาก k=[] เริ่มต้น)