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

หาปริพันธ์ของพหุนามและตั้งค่าค่าคงที่การปริพันธ์ใน Python

บทนำ

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

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

  • c: อาร์เรย์ 1 มิติของสัมประสิทธิ์พหุนาม จัดเรียงจากดีกรีต่ำไปสูง
  • m: ลำดับการปริพันธ์ ต้องเป็นบวก (ค่าเริ่มต้น: 1)
  • k: ค่าคงที่การปริพันธ์ ค่าของปริพันธ์อันดับหนึ่งที่ศูนย์คือค่าแรกในลิสต์ หาก m=1 สามารถให้ค่า scalar เดียวได้
  • 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, k=3))

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

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)

# หาปริพันธ์พหุนาม ตั้งค่า integration constant k = 3
print("\nResult...\n", P.polyint(c, k=3))

ผลลัพธ์

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

สรุป

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