Làm thế nào để bạn gọi một chức năng cụ thể từ một lớp trong python?

Chương này được trích từ cuốn sách A Primer on Scientific Programming with Python của H. P. Langtangen, tái bản lần thứ 5, Springer, 2016

Một số phương thức lớp có tên bắt đầu và kết thúc bằng dấu gạch dưới kép. Các phương thức này cho phép một cú pháp đặc biệt trong chương trình và được gọi là các phương thức đặc biệt. Hàm tạo

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
0 là một ví dụ. Phương thức này được gọi tự động khi một thể hiện được tạo [bằng cách gọi lớp như một hàm], nhưng chúng ta không cần phải viết rõ ràng
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
0. Các phương thức đặc biệt khác giúp thực hiện các phép tính số học với các thể hiện, để so sánh các thể hiện với
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
2,
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
3,
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
4, v.v. , để gọi các thể hiện khi chúng ta gọi các hàm thông thường và để kiểm tra xem một thể hiện có giá trị là
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
5 hay
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
6 hay không, đề cập đến một số khả năng

Phương thức gọi đặc biệt

Việc tính toán giá trị của hàm toán học được đại diện bởi lớp

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
7 từ phần Biểu diễn hàm dưới dạng một lớp, với tên của thể hiện là
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
8, được thực hiện bằng cách viết
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
9. Nếu chúng ta chỉ có thể viết
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
00, thể hiện của
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
8 sẽ giống như một hàm bình thường. Một cú pháp như vậy thực sự có thể và được cung cấp bởi phương thức đặc biệt có tên là
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
02. Viết
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
00 ngụ ý một cuộc gọi

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
4nếu lớp
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
7 có phương thức
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
02 được xác định. Chúng tôi có thể dễ dàng thêm phương pháp đặc biệt này.
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
7Phương pháp
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
06 trước đó hiện không còn cần thiết. Một quy ước lập trình tốt là bao gồm một phương thức
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
02 trong tất cả các lớp biểu diễn một hàm toán học. Các trường hợp có phương thức
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
02 được cho là đối tượng có thể gọi được, giống như các hàm đơn giản cũng là đối tượng có thể gọi được. Cú pháp gọi cho các đối tượng có thể gọi được là như nhau, bất kể đối tượng đó là một hàm hay một thể hiện của lớp. Đưa ra một đối tượng
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
09, ______22kiểm tra xem
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
09 có hoạt động như một đối tượng có thể gọi hay không, tôi. e. , nếu
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
09 là một hàm Python hoặc một thể hiện có phương thức
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
02

Đặc biệt, một thể hiện của lớp

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
7 có thể được chuyển dưới dạng đối số
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
74 cho hàm
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
75 từ phần Thử thách. chức năng với các tham số

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
9Bên trong
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
75, chúng ta có thể kiểm tra rằng
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
74 không phải là một hàm mà là một thể hiện của lớp
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
7. Tuy nhiên, chúng tôi chỉ sử dụng
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
74 trong các cuộc gọi, như
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
40 và vì mục đích này, một phiên bản có phương thức
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
02 hoạt động như một hàm đơn giản. Tính năng này rất tiện lợi

Phần tiếp theo trình bày một ứng dụng gọn gàng của toán tử cuộc gọi

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
02 trong một thuật toán số

Thí dụ. Tự động phân biệt

Vấn đề

Với triển khai Python

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
40 của hàm toán học \[ f[x] \], chúng tôi muốn tạo một đối tượng hoạt động như một hàm Python để tính đạo hàm \[ f'[x] \]. Ví dụ: nếu đối tượng này thuộc loại
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
44, chúng ta có thể viết một cái gì đó như

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
9Tức là,
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
45 hoạt động như một hàm Python thẳng để triển khai đạo hàm \[ 3x^2 \] của \[ x^3 \] [à, câu trả lời chỉ là gần đúng, có lỗi ở chữ số thập phân thứ 7, nhưng phép tính gần đúng có thể

Maple, Mathematica và nhiều gói phần mềm khác có thể thực hiện toán học ký hiệu chính xác, bao gồm cả vi phân và tích hợp. Gói Python

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
46 dành cho toán học tượng trưng khiến việc tính đạo hàm chính xác của một lớp hàm lớn \[ f[x] \] trở nên đơn giản và biến kết quả thành một hàm Python thông thường. Tuy nhiên, các hàm toán học được xác định theo cách thuật toán [e. g. , lời giải của một bài toán khác], hoặc các hàm có nhánh, số ngẫu nhiên, v.v. , đặt ra các vấn đề cơ bản đối với sự khác biệt về ký hiệu, và sau đó cần có sự khác biệt về số. Do đó, chúng tôi tính toán các đạo hàm trong các trường hợp
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
44 dựa trên các công thức sai phân hữu hạn. Cũng có thể sử dụng phân biệt biểu tượng chính xác thông qua SymPy

Dung dịch

Công thức cơ bản nhất [nhưng không phải là tốt nhất] cho một đạo hàm số là $$ \begin{equation} f'[x]\approx {f[x+h]-f[x]\over h}\tp \tag{ . Các biến này có thể được đặt trong hàm tạo. Toán tử

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
02 tính toán đạo hàm với sự trợ giúp của [1]. Tất cả điều này có thể được mã hóa trong một vài dòng

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
Lưu ý rằng chúng tôi biến
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
49 thành
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
82 để tránh khả năng chia số nguyên

Dưới đây là một ứng dụng của lớp để phân biệt hai hàm \[ f[x]=\sin x \] và \[ g[t]=t^3 \]

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
0Các biểu thức
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
83 và
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
84 trông giống như các hàm Python thông thường đánh giá đạo hàm của các hàm
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
85 và
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
86. Lớp
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
44 hoạt động cho [hầu hết] mọi chức năng \[ f[x] \]

xác minh

Một thói quen lập trình tốt là bao gồm một chức năng kiểm tra để xác minh việc triển khai một lớp. Chúng ta có thể xây dựng một phép thử dựa trên thực tế là công thức vi phân gần đúng [2] là chính xác cho các hàm tuyến tính

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
7Ở đây, chúng ta đã sử dụng hàm lambda để định nghĩa gọn một hàm
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
74. Giải pháp thay thế là định nghĩa
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
74 theo cách tiêu chuẩn
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
4Một tính năng đặc biệt của
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
74 là nó ghi nhớ các biến
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
09 và
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
702 khi
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
74 được gửi đến lớp
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
44 [nó là một bao đóng, xem phần Đóng]. Lưu ý rằng chức năng kiểm tra ở trên tuân theo các quy ước về chức năng kiểm tra được nêu trong phần Vòng tròn A

Đăng kí. phương pháp Newton

Trong những tình huống nào sẽ thuận tiện để tự động tạo hàm Python

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
83 là đạo hàm của một hàm Python khác
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
40? . Xét một hàm
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
707 để giải \[ f[x]=0 \].
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
708. Một triển khai cụ thể được tìm thấy trong tệp mô-đun Newton. py. Các đối số là một hàm Python
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
74 cho \[ f[x] \], một
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
710 cho dự đoán ban đầu [giá trị bắt đầu] của \[ x \], một hàm Python
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
45 cho \[ f'[x] \], một . các thuật toán lặp lại cho đến khi \[. f[x]. < \epsilon \] và một
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
714 cho số lần lặp lại tối đa mà chúng tôi cho phép. Tất cả các đối số đều dễ dàng cung cấp, ngoại trừ
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
45, yêu cầu tính toán \[ f'[x] \] bằng tay sau đó triển khai công thức trong hàm Python. Giả sử phương trình đích của chúng ta là $$ \begin{equation*} f[x] = 10^5[x-0. 9]^2[x-1. 1]^3=0 \thinspace. \end{equation*} $$ Hàm \[ f[x] \] được vẽ trong Hình 2. Phần sau sử dụng lớp
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
44 để nhanh chóng tạo đạo hàm để chúng ta có thể gọi phương thức của Newton

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
8Bộ 3 đầu ra giữ giá trị gần đúng của gốc, số lần lặp và giá trị của \[ f \] tại gốc gần đúng [số đo sai số trong phương trình]

Hình 2. Âm mưu của \[ y = 10^5[x-0. 9]^2[x-1. 1]^3 \]

Gốc chính xác là 1. 1, và sự hội tụ về giá trị này là rất chậm. [Phương pháp Newton hội tụ rất chậm khi đạo hàm của \[ f \] tại các nghiệm của \[ f \] bằng 0. Sự hội tụ thậm chí còn chậm hơn xuất hiện khi các đạo hàm bậc cao hơn cũng bằng 0, như trong ví dụ này. Lưu ý rằng sai số trong

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
717 lớn hơn nhiều so với sai số trong phương trình [
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
713]. Ví dụ: dung sai
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
713 của \[ 10^{-10} \] yêu cầu 18 lần lặp với sai số \[ 10^{-3} \]. ] Sử dụng một đạo hàm chính xác cho kết quả gần như giống nhau

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
70Ví dụ này chỉ ra rằng hầu như không có bất kỳ nhược điểm nào trong việc sử dụng phương pháp phân biệt chung chung không chính xác "thông minh" như trong lớp
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
44. Có rất nhiều ưu điểm - đáng chú ý nhất là,
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
44 tránh được các lỗi tiềm ẩn do mã hóa thủ công có thể không chính xác của các biểu thức có thể dài dòng của các phép tính tay có thể sai. Các sai số trong các phép tính gần đúng có liên quan có thể được làm nhỏ hơn, thường là nhỏ hơn nhiều so với các sai số khác, như dung sai trong phương pháp của Newton trong ví dụ này hoặc độ không đảm bảo của các tham số vật lý trong các bài toán thực tế

Giải pháp sử dụng SymPy

lớp

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
44 dựa trên phân biệt số, nhưng có thể tạo một lớp ngắn bằng nhau có thể thực hiện phân biệt chính xác. Trong SymPy, người ta có thể thực hiện phân biệt ký hiệu của một biểu thức
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
723 đối với một biến độc lập ký hiệu
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
717 bởi
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
725. Giả sử rằng hàm
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
74 của người dùng có thể được đánh giá cho một biến độc lập tượng trưng
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
717, chúng ta có thể gọi
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
40 để lấy biểu thức SymPy cho công thức trong
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
74 và sau đó sử dụng
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
75 để tính đạo hàm chính xác. Sau đó, chúng ta biến biểu thức tượng trưng của đạo hàm thành một hàm Python thông thường [thông qua
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
731] và định nghĩa hàm này là phương thức
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
02. Mã Python thích hợp rất ngắn

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
71Lưu ý cách định nghĩa phương thức
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
02 bằng cách gán một hàm cho nó [mặc dù hàm được trả về bởi
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
731 chỉ là một hàm của
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
717, nhưng nó hoạt động để gọi
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
736 cho một thể hiện
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
737 của loại
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
738]

Cả trình diễn lớp và xác minh việc triển khai đều có thể được đặt trong một chức năng kiểm tra

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
72

Ví dụ với

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
86 nên dễ hiểu. Trong phương thức khởi tạo của lớp
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
738, chúng ta gọi
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
741, với ký hiệu là
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
717, và
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
743 trả về biểu thức SymPy
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
744. Phương thức
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
02 sau đó trở thành một hàm
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
746

Tuy nhiên, chức năng

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
747 xứng đáng được giải thích thêm. Khi đó hàm tạo của lớp
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
738 thực hiện cuộc gọi
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
749, với ký hiệu
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
717, hàm
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
49 sẽ trả về biểu thức SymPy
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
752, với điều kiện là
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
753 và
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
754 là các hàm SymPy. Vì chúng ta thực hiện
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
755 trước khi gọi hàm tạo trong lớp
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
738, tên
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
753 và
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
754 được xác định trong hàm kiểm tra và hàm
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
49 cục bộ của chúng ta sẽ có quyền truy cập vào tất cả các biến cục bộ, vì nó là một hàm đóng như đã đề cập ở trên và trong phần . Điều này có nghĩa là
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
49 có quyền truy cập vào
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
761 và
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
762 khi hàm tạo trong lớp
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
738 gọi
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
49. Sau đó, chúng tôi muốn thực hiện một số tính toán số và cần
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
753,
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
754 và
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
767 từ mô-đun
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
768. Nếu chúng tôi đã cố gắng thực hiện
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
769 sau khi nhập từ
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
768, thì sau đó,
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
49 sẽ gọi
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
772 và
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
773 với ký hiệu SymPy làm đối số và sẽ gây ra một
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
774 vì
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
772 mong đợi một đối tượng
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
82, chứ không phải một đối tượng
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
777 từ SymPy

Mặc dù lớp

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
738 nhỏ và gọn, cấu trúc và cách sử dụng của nó như được giải thích ở đây mang đến nhiều chủ đề nâng cao hơn so với lớp
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
44 và các tính toán số đơn giản của nó. Tuy nhiên, có thể thú vị khi thấy rằng một lớp để phân biệt chính xác hàm Python có thể được thực hiện trong rất ít dòng

Thí dụ. Tích hợp tự động

Chúng ta có thể áp dụng các ý tưởng từ phần Ví dụ. Tự động phân biệt để tạo một lớp tính toán tích phân của một hàm số. Cho một hàm \[ f[x] \], chúng ta muốn tính $$ \begin{equation*} F[x; a] = \int_a^x f[t]dt \thinspace. \end{equation*} $$ Kỹ thuật tính toán bao gồm sử dụng quy tắc Hình thang với \[ n \] khoảng [\[ n+1 \] điểm]. $$ \begin{equation} \int_a^x f[t]dt = h\left[\frac{1}{2}f[a] + \sum_{i=1}^{n-1} f[a+ . Trong một chương trình ứng dụng, chúng ta muốn tính \[ F[x;a] \] bằng một cú pháp đơn giản như

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
73Ở đây,
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
40 là hàm Python được tích hợp và
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
781 hoạt động như một hàm Python tính toán các giá trị của \[ F[x;a] \]

Một thực hiện đơn giản

Xem xét cách triển khai đơn giản quy tắc Hình thang trong hàm Python

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
74

Lớp

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
782 phải có một số thuộc tính dữ liệu và phương thức
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
02. Vì phương thức thứ hai được cho là lấy ____1717 làm đối số, nên các tham số khác ____109, ____274 và ____ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _. Việc thực hiện sau đó trở thành

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
75Các bạn lưu ý là chúng ta chỉ sử dụng lại hàm
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
788 để thực hiện phép tính. Ngoài ra, chúng ta có thể sao chép phần thân của hàm
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
788 vào phương thức
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
02. Tuy nhiên, nếu chúng ta đã triển khai và kiểm tra thuật toán này như một hàm, thì tốt hơn là gọi hàm. Sau đó, lớp này được gọi là trình bao bọc của hàm cơ bản. Trình bao bọc cho phép gọi một thứ gì đó bằng cú pháp thay thế

Một chương trình ứng dụng tính toán \[ \int_0^{2\pi}\sin x\, dx \] có thể trông như sau

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
76Một phép tính tương đương là
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
77

Xác minh thông qua tính toán tượng trưng

Chúng ta phải luôn cung cấp chức năng kiểm tra để xác minh việc triển khai. Để tránh xử lý các lỗi xấp xỉ chưa biết của quy tắc Hình thang, chúng tôi sử dụng thực tế hiển nhiên là các hàm tuyến tính được tích hợp chính xác theo quy tắc. Mặc dù thật dễ dàng để chọn một hàm tuyến tính, tích phân nó và tìm ra tích phân là gì, nhưng chúng ta cũng có thể trình bày cách tự động hóa quy trình như vậy bằng SymPy. Về cơ bản, chúng tôi định nghĩa một biểu thức trong SymPy, yêu cầu SymPy tích hợp nó, sau đó biến tích phân ký hiệu thu được thành một hàm Python đơn giản để tính toán

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
78Sử dụng chức năng như vậy để thực hiện tích hợp chính xác, chúng tôi có thể viết hàm thử nghiệm của mình là
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
79Nếu bạn nghĩ rằng việc sử dụng SymPy để tích hợp các hàm tuyến tính là quá mức cần thiết, bạn cũng có thể tự làm điều đó và xác định
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
791 và
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
792

Nhận xét

Lớp

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
782 không hiệu quả [nhưng có lẽ là quá đủ nhanh] để vẽ \[ F[x;a] \] dưới dạng hàm \[ x \]. bài tập 22. Tăng tốc các phép tính tích phân lặp đi lặp lại gợi ý để tối ưu hóa lớp cho mục đích này

Biến một thể hiện thành một chuỗi

Một phương pháp đặc biệt hữu ích khác là

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
794. Nó được gọi khi một thể hiện của lớp cần được chuyển đổi thành một chuỗi. Điều này xảy ra khi chúng ta nói
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
795 và
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
09 là một ví dụ. Sau đó, Python sẽ xem xét phiên bản
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
09 để tìm phương thức
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
794, phương thức này được cho là sẽ trả về một chuỗi. Nếu một phương thức đặc biệt như vậy được tìm thấy, chuỗi trả về sẽ được in, nếu không thì chỉ tên của lớp được in. Một ví dụ sẽ minh họa điểm. Trước tiên, chúng tôi cố gắng in một thể hiện
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
8 của lớp
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
7 từ phần Đại diện cho một hàm dưới dạng một lớp [nơi không có phương thức
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
794]

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
20Điều này có nghĩa là
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
8 là một phiên bản
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
7 trong mô-đun
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
204 [chương trình chính hoặc phiên tương tác]. Đầu ra cũng chứa một địa chỉ cho biết nơi lưu trữ phiên bản
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
8 trong bộ nhớ của máy tính

Nếu chúng ta muốn

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
206 in ra thể hiện của
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
8, chúng ta cần định nghĩa phương thức
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
794 trong lớp
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
7

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
21Thông thường,
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
794 thay thế phương pháp
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
211 trước đây của chúng tôi và
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
02 thay thế phương pháp
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
06 trước đây của chúng tôi. Do đó, các lập trình viên Python với kinh nghiệm mà chúng ta đã có được sẽ chỉ viết lớp
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
7 với các phương thức đặc biệt.
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
22Hãy để chúng tôi xem hoạt động của lớp học.
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
23Chúng tôi đã thu được gì khi sử dụng các phương pháp đặc biệt? . Điểm mấu chốt của việc sử dụng các phương thức đặc biệt là đạt được cú pháp thân thiện với người dùng hơn. Các phần tiếp theo minh họa điểm này hơn nữa

Lưu ý rằng phương thức

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
794 được gọi bất cứ khi nào chúng ta thực hiện
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
218 và
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
795 thực sự là
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
220, i. e. ,
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
221

Thí dụ. Danh bạ điện thoại với các phương pháp đặc biệt

Hãy để chúng tôi xem xét lại lớp

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
222 từ phần Danh bạ điện thoại. Phương thức
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
223 trong lớp đó được triển khai tốt hơn như một phương thức đặc biệt của
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
794. Điều này thật dễ dàng. chúng tôi chỉ thay đổi tên phương thức và thay thế
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
225 bằng
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
226

Lưu trữ các phiên bản

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
222 trong từ điển để tạo danh bạ điện thoại rất đơn giản. Tuy nhiên, chúng tôi làm cho từ điển dễ sử dụng hơn một chút nếu chúng tôi bọc một lớp xung quanh nó. Nghĩa là, chúng ta tạo một lớp
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
228 chứa từ điển làm thuộc tính. Một phương pháp
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
229 có thể được sử dụng để thêm một người mới

________ 224A ________ 1794 có thể in danh bạ điện thoại theo thứ tự bảng chữ cái.
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
25Để truy xuất một phiên bản
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
222, chúng tôi sử dụng
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
02 với tên của người đó làm đối số.
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
26Ưu điểm duy nhất của phương pháp này là cú pháp đơn giản hơn. đối với
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
228
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
702, chúng tôi có thể lấy dữ liệu về
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
235 bằng cách gọi
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
236 thay vì truy cập từ điển nội bộ
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
237

Chúng ta có thể tạo mã demo đơn giản cho danh bạ điện thoại có ba tên

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
27Đầu ra trở thành
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
28Bạn được khuyến khích mạnh mẽ để làm việc thông qua chương trình demo cuối cùng này bằng tay và mô phỏng những gì chương trình thực hiện. Đó là, nhảy xung quanh mã và viết ra một tờ giấy những biến khác nhau chứa sau mỗi câu lệnh. Đây là một bài tập quan trọng và tốt. Bạn tận hưởng niềm hạnh phúc của việc làm chủ các lớp nếu bạn đạt được kết quả như trên. Chương trình hoàn chỉnh với các lớp
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
222 và
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
228 và bài kiểm tra ở trên được tìm thấy trong tệp PhoneBook. py. Bạn có thể chạy chương trình này, từng câu lệnh, trong Online Python Tutor hoặc trong trình gỡ lỗi [xem tài liệu Gỡ lỗi trong Python [3]] để đảm bảo rằng hiểu biết của bạn về dòng chương trình là chính xác

Nhận xét

Lưu ý rằng các tên được sắp xếp theo tên đầu tiên. Lý do là các chuỗi được sắp xếp sau ký tự đầu tiên, sau đó là ký tự thứ hai, v.v. Chúng tôi có thể cung cấp chức năng sắp xếp phù hợp của riêng mình. Một khả năng là chia tên thành các từ và sử dụng từ cuối cùng để sắp xếp

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
29

Thêm đối tượng

Đặt

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
09 và
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
702 là thể hiện của một số lớp
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
242. Viết
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
243 có hợp lý không?

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
90Phương thức
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
245 nên thêm các phiên bản
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
247 và
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
248 và trả về kết quả dưới dạng một phiên bản. Vì vậy, khi Python gặp
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
243, nó sẽ kiểm tra xem lớp
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
242 có phương thức
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
245 hay không và diễn giải
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
243 thành lệnh gọi
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
253. Ví dụ tiếp theo hy vọng sẽ làm rõ ý tưởng này có thể được sử dụng để làm gì

Thí dụ. Hạng cho đa thức

Hãy để chúng tôi tạo một lớp

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
254 cho đa thức. Các hệ số trong đa thức có thể được cung cấp cho hàm tạo dưới dạng danh sách. Số chỉ mục \[ i \] trong danh sách này đại diện cho các hệ số của số hạng \[ x^i \] trong đa thức. Nghĩa là, cách viết
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
255 xác định một đa thức $$ \begin{equation*} 1 + 0\cdot x - 1\cdot x^2 + 2\cdot x^3 = 1 - x^2 + 2x^3 \thinspace. \end{equation*} $$ Có thể cộng đa thức [chỉ cần cộng các hệ số tương ứng với cùng lũy ​​thừa] để lớp chúng ta có thể có phương thức
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
245. Phương pháp
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
02 là tự nhiên để đánh giá đa thức, được cung cấp giá trị \[ x \]. Lớp được liệt kê dưới đây và giải thích sau đó

Thực hiện

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
91Lớp
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
254 có một thuộc tính dữ liệu. danh sách các hệ số. Để đánh giá đa thức, chúng tôi chỉ tổng hợp các hệ số không. \[ i \] nhân \[ x^i \] cho \[ i=0 \] thành số hệ số trong danh sách

Phương pháp

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
245 có vẻ tiên tiến hơn. Mục tiêu là cộng hai danh sách các hệ số. Tuy nhiên, có thể xảy ra trường hợp các danh sách có độ dài không bằng nhau. Do đó, chúng tôi bắt đầu với danh sách dài nhất và thêm vào danh sách khác, từng phần tử một. Quan sát rằng
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
260 bắt đầu như một bản sao của
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
261. nếu không, những thay đổi trong
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
260 khi chúng tôi tính tổng sẽ được phản ánh trong
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
261. Điều này có nghĩa là
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
247 sẽ là tổng của chính nó và đối tượng
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
248, hay nói cách khác, cộng hai đối tượng,
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
266, thay đổi
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
267 - đây không phải là điều chúng ta muốn. Một triển khai thay thế của lớp
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
254 được tìm thấy trong Bài tập 24. Tìm một lỗi trong một lớp cho đa thức

Một phương pháp trừ

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
269 có thể được thực hiện theo dòng của
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
245, nhưng phức tạp hơn một chút và để lại như Bài tập 25. Thực hiện phép trừ đa thức. Bạn được khuyến khích thực hiện bài tập này vì nó sẽ giúp nâng cao hiểu biết về tác động qua lại giữa toán học và lập trình trong lớp
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
254

Một phép toán phức tạp hơn trên đa thức, theo quan điểm toán học, là phép nhân hai đa thức. Gọi \[ p[x]=\sum_{i=0}^Mc_ix^i \] và \[ q[x]=\sum_{j=0}^N d_jx^j \] là hai đa thức. Tích sẽ trở thành $$ \begin{equation*} \left[\sum_{i=0}^Mc_ix^i\right]\left[ \sum_{j=0}^N d_jx^j\right] = \sum_{ . \end{equation*} $$ Tổng kép phải được thực hiện dưới dạng một vòng lặp kép, nhưng trước tiên, danh sách cho đa thức kết quả phải được tạo với độ dài \[ M+N+1 \] [số mũ cao nhất là \[ M+ . Việc thực hiện toán tử nhân trở thành

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
92

Chúng ta cũng có thể bao gồm một phương pháp để đạo hàm đa thức theo công thức $$ \begin{equation*} {d\over dx}\sum_{i=0}^n c_ix^i = \sum_{i=1}^n . \end{equation*} $$ Nếu \[ c_i \] được lưu trữ dưới dạng danh sách

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
272, biểu diễn danh sách của đạo hàm, giả sử tên của nó là
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
273, đáp ứng
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
274 cho
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
275 chạy từ 1 đến chỉ số lớn nhất trong
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
272. Lưu ý rằng
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
273 có một phần tử nhỏ hơn
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
272

Có hai cách khác nhau để triển khai chức năng phân biệt, bằng cách thay đổi các hệ số đa thức hoặc bằng cách trả về một thể hiện

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
254 mới từ phương thức sao cho thể hiện đa thức ban đầu còn nguyên vẹn. Chúng tôi để
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
280 là một triển khai của phương pháp đầu tiên, tôi. e. , phương thức này không trả về bất cứ thứ gì, nhưng các hệ số trong trường hợp
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
254
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
282 bị thay đổi. Cách tiếp cận khác được triển khai bởi
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
283, trả về một đối tượng
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
254 mới với các hệ số tương ứng với đạo hàm của
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
282

Việc thực hiện đầy đủ hai phương pháp được đưa ra dưới đây

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
93Lớp
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
254 có phương thức
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
287 chứ không phải phương thức
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
288 sẽ có thể thay đổi [i. e. , nội dung của đối tượng có thể thay đổi] và cho phép thay đổi dữ liệu tại chỗ, trong khi lớp
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
254 có
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
288 chứ không phải
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
287 sẽ tạo ra một đối tượng bất biến trong đó đa thức được khởi tạo trong hàm tạo không bao giờ bị thay đổi. [Về mặt kỹ thuật, có thể lấy biến
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
292 trong một thể hiện của lớp và thay đổi danh sách này. Bằng cách bắt đầu
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
292 với dấu gạch dưới, quy ước lập trình Python cho người lập trình biết rằng biến này chỉ dành cho sử dụng nội bộ trong lớp và không được thay đổi bởi người dùng của phiên bản, xem phần Tài khoản ngân hàng và Hoạt động bất hợp pháp. ] Một quy tắc tốt là chỉ cung cấp một trong hai hàm này sao cho đối tượng
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
254 có thể thay đổi hoặc không thể thay đổi [nếu chúng ta bỏ qua
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
287, tất nhiên thân hàm của nó phải được sao chép vào
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
288 vì
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
288 hiện dựa vào mã đó]. Tuy nhiên, vì mục đích chính của lớp này là để minh họa các loại kỹ thuật lập trình khác nhau, nên chúng tôi giữ cả hai phiên bản

Cách sử dụng

Để minh họa chức năng của lớp

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
254, chúng tôi giới thiệu hai đa thức $$ \begin{equation*} p_1[x]= 1-x,\quad p_2[x]=x - 6x^4 - x^5 \thinspace. \end{phương trình*} $$

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
94Một xác minh của việc triển khai có thể là so sánh
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
299 tại [e. g. ] \[ x=1/2 \] với \[ p_1[x] + p_2[x] \].
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
95Lưu ý rằng
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
900 rất khác với
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
901. Trong trường hợp trước, chúng tôi thêm hai thể hiện của lớp
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
254, trong khi trong trường hợp sau, chúng tôi thêm hai thể hiện của lớp
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
82 [vì
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
904 và
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
905 ngụ ý gọi
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
02 và phương thức đó trả về một đối tượng
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
82]

Bản in đẹp của đa thức

Lớp

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
254 cũng có thể được trang bị phương thức
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
794 để in đa thức ra màn hình. Việc triển khai sơ bộ đầu tiên có thể chỉ cần cộng các chuỗi có dạng
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
910

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
96Tuy nhiên, việc triển khai này dẫn đến đầu ra xấu từ quan điểm toán học. Chẳng hạn, một đa thức có hệ số
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
911 được in là
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
97Một đầu ra mong muốn hơn sẽ là
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
98Tức là, các số hạng có hệ số bằng 0 sẽ bị loại bỏ; . e. ,
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
914 nên được thay thế bằng dấu cách
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
915; . Những điều chỉnh này có thể được thực hiện bằng cách sử dụng phương pháp
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
919 trong các đối tượng chuỗi và bằng cách soạn các lát của chuỗi. Phiên bản mới của phương pháp
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
794 bên dưới chứa các điều chỉnh cần thiết. Nếu bạn thấy kiểu thao tác chuỗi này phức tạp và khó hiểu, bạn có thể yên tâm bỏ qua việc kiểm tra thêm mã
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
794 đã được cải tiến vì các chi tiết không cần thiết cho việc học hiện tại của bạn về khái niệm lớp và các phương thức đặc biệt.
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
99Lập trình đôi khi biến thành viết mã [điều mà người ta nghĩ là] một giải pháp chung, theo sau là một loạt các trường hợp đặc biệt để khắc phục các cảnh báo trong giải pháp "chung", giống như chúng tôi đã trải nghiệm với phương pháp
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
794 ở trên. Tình huống này thường yêu cầu các bản sửa lỗi bổ sung trong tương lai và thường là dấu hiệu của một giải pháp dưới mức tối ưu cho vấn đề lập trình

Bản in đẹp của các phiên bản

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
254 có thể được hiển thị trong một phiên tương tác

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
90

Kiểm chứng việc thực hiện

Luôn luôn là một thói quen tốt để bao gồm một chức năng kiểm tra

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
924 để xác minh chức năng trong lớp
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
254. Để đạt được điều này, chúng tôi xây dựng một số ví dụ về cộng, nhân và phân biệt đa thức bằng tay và thực hiện các bài kiểm tra mà lớp
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
254 tái tạo kết quả chính xác. Kiểm tra phương pháp
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
794 còn lại như Bài tập 26. Kiểm tra chức năng in đẹp các đa thức

Lỗi làm tròn có thể là một vấn đề trong lớp

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
254.
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
245,
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
288 và
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
287 sẽ dẫn đến các hệ số nguyên nếu các đa thức được thêm vào có hệ số nguyên, trong khi đó,
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
932 luôn dẫn đến một đa thức có các hệ số được lưu trữ trong mảng
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
933 với các phần tử
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
82. Hệ số nguyên trong danh sách có thể được so sánh bằng cách sử dụng
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
935 cho danh sách, trong khi hệ số trong mảng
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
933 phải được so sánh với dung sai. Người ta có thể trừ các mảng
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
933 và sử dụng phương pháp
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
938 để tìm độ lệch lớn nhất và so sánh giá trị này với dung sai hoặc người ta có thể sử dụng
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
939 để so sánh các mảng
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
09 và
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
702 với dung sai [tương đối]
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
942

Hãy để chúng tôi chọn các đa thức có hệ số nguyên làm trường hợp thử nghiệm sao cho

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
245,
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
288 và
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
287 có thể được xác minh bằng cách kiểm tra đẳng thức [
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
935] của danh sách
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
292. Phép nhân trong
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
932 phải sử dụng
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
949

Chúng tôi tuân theo quy ước rằng tất cả các bài kiểm tra đều ở dạng

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
950, ​​trong đó
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
951 là biểu thức boolean cho bài kiểm tra. [Bản thật của hàm kiểm tra trong file Polynomial. py thêm thông báo lỗi
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
952 vào bài kiểm tra.
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
953. ] Một phần khác của quy ước là hàm bắt đầu bằng
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
954 và hàm không nhận đối số

Chức năng kiểm tra của chúng tôi bây giờ trở thành

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
91

Các phép tính số học và các phương thức đặc biệt khác

Cho trước hai trường hợp

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
09 và
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
702, các phép tính số học nhị phân tiêu chuẩn với
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
09 và
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
702 được xác định bằng các phương thức đặc biệt sau

  • class Derivative[object]:
        def __init__[self, f, h=1E-5]:
            self.f = f
            self.h = float[h]
    
        def __call__[self, x]:
            f, h = self.f, self.h      # make short forms
            return [f[x+h] - f[x]]/h
    
    243.
    class Derivative[object]:
        def __init__[self, f, h=1E-5]:
            self.f = f
            self.h = float[h]
    
        def __call__[self, x]:
            f, h = self.f, self.h      # make short forms
            return [f[x+h] - f[x]]/h
    
    253
  • class Derivative[object]:
        def __init__[self, f, h=1E-5]:
            self.f = f
            self.h = float[h]
    
        def __call__[self, x]:
            f, h = self.f, self.h      # make short forms
            return [f[x+h] - f[x]]/h
    
    961.
    class Derivative[object]:
        def __init__[self, f, h=1E-5]:
            self.f = f
            self.h = float[h]
    
        def __call__[self, x]:
            f, h = self.f, self.h      # make short forms
            return [f[x+h] - f[x]]/h
    
    962
  • class Derivative[object]:
        def __init__[self, f, h=1E-5]:
            self.f = f
            self.h = float[h]
    
        def __call__[self, x]:
            f, h = self.f, self.h      # make short forms
            return [f[x+h] - f[x]]/h
    
    963.
    class Derivative[object]:
        def __init__[self, f, h=1E-5]:
            self.f = f
            self.h = float[h]
    
        def __call__[self, x]:
            f, h = self.f, self.h      # make short forms
            return [f[x+h] - f[x]]/h
    
    964
  • class Derivative[object]:
        def __init__[self, f, h=1E-5]:
            self.f = f
            self.h = float[h]
    
        def __call__[self, x]:
            f, h = self.f, self.h      # make short forms
            return [f[x+h] - f[x]]/h
    
    965.
    class Derivative[object]:
        def __init__[self, f, h=1E-5]:
            self.f = f
            self.h = float[h]
    
        def __call__[self, x]:
            f, h = self.f, self.h      # make short forms
            return [f[x+h] - f[x]]/h
    
    966
  • class Derivative[object]:
        def __init__[self, f, h=1E-5]:
            self.f = f
            self.h = float[h]
    
        def __call__[self, x]:
            f, h = self.f, self.h      # make short forms
            return [f[x+h] - f[x]]/h
    
    967.
    class Derivative[object]:
        def __init__[self, f, h=1E-5]:
            self.f = f
            self.h = float[h]
    
        def __call__[self, x]:
            f, h = self.f, self.h      # make short forms
            return [f[x+h] - f[x]]/h
    
    968
Một số phương pháp đặc biệt khác cũng thường hữu ích
  • chiều dài của
    class Derivative[object]:
        def __init__[self, f, h=1E-5]:
            self.f = f
            self.h = float[h]
    
        def __call__[self, x]:
            f, h = self.f, self.h      # make short forms
            return [f[x+h] - f[x]]/h
    
    09,
    class Derivative[object]:
        def __init__[self, f, h=1E-5]:
            self.f = f
            self.h = float[h]
    
        def __call__[self, x]:
            f, h = self.f, self.h      # make short forms
            return [f[x+h] - f[x]]/h
    
    970.
    class Derivative[object]:
        def __init__[self, f, h=1E-5]:
            self.f = f
            self.h = float[h]
    
        def __call__[self, x]:
            f, h = self.f, self.h      # make short forms
            return [f[x+h] - f[x]]/h
    
    971
  • giá trị tuyệt đối của
    class Derivative[object]:
        def __init__[self, f, h=1E-5]:
            self.f = f
            self.h = float[h]
    
        def __call__[self, x]:
            f, h = self.f, self.h      # make short forms
            return [f[x+h] - f[x]]/h
    
    09,
    class Derivative[object]:
        def __init__[self, f, h=1E-5]:
            self.f = f
            self.h = float[h]
    
        def __call__[self, x]:
            f, h = self.f, self.h      # make short forms
            return [f[x+h] - f[x]]/h
    
    973.
    class Derivative[object]:
        def __init__[self, f, h=1E-5]:
            self.f = f
            self.h = float[h]
    
        def __call__[self, x]:
            f, h = self.f, self.h      # make short forms
            return [f[x+h] - f[x]]/h
    
    974
  • class Derivative[object]:
        def __init__[self, f, h=1E-5]:
            self.f = f
            self.h = float[h]
    
        def __call__[self, x]:
            f, h = self.f, self.h      # make short forms
            return [f[x+h] - f[x]]/h
    
    975.
    class Derivative[object]:
        def __init__[self, f, h=1E-5]:
            self.f = f
            self.h = float[h]
    
        def __call__[self, x]:
            f, h = self.f, self.h      # make short forms
            return [f[x+h] - f[x]]/h
    
    976
  • class Derivative[object]:
        def __init__[self, f, h=1E-5]:
            self.f = f
            self.h = float[h]
    
        def __call__[self, x]:
            f, h = self.f, self.h      # make short forms
            return [f[x+h] - f[x]]/h
    
    977.
    class Derivative[object]:
        def __init__[self, f, h=1E-5]:
            self.f = f
            self.h = float[h]
    
        def __call__[self, x]:
            f, h = self.f, self.h      # make short forms
            return [f[x+h] - f[x]]/h
    
    978
  • class Derivative[object]:
        def __init__[self, f, h=1E-5]:
            self.f = f
            self.h = float[h]
    
        def __call__[self, x]:
            f, h = self.f, self.h      # make short forms
            return [f[x+h] - f[x]]/h
    
    979.
    class Derivative[object]:
        def __init__[self, f, h=1E-5]:
            self.f = f
            self.h = float[h]
    
        def __call__[self, x]:
            f, h = self.f, self.h      # make short forms
            return [f[x+h] - f[x]]/h
    
    980
  • class Derivative[object]:
        def __init__[self, f, h=1E-5]:
            self.f = f
            self.h = float[h]
    
        def __call__[self, x]:
            f, h = self.f, self.h      # make short forms
            return [f[x+h] - f[x]]/h
    
    981.
    class Derivative[object]:
        def __init__[self, f, h=1E-5]:
            self.f = f
            self.h = float[h]
    
        def __call__[self, x]:
            f, h = self.f, self.h      # make short forms
            return [f[x+h] - f[x]]/h
    
    982
  • class Derivative[object]:
        def __init__[self, f, h=1E-5]:
            self.f = f
            self.h = float[h]
    
        def __call__[self, x]:
            f, h = self.f, self.h      # make short forms
            return [f[x+h] - f[x]]/h
    
    983.
    class Derivative[object]:
        def __init__[self, f, h=1E-5]:
            self.f = f
            self.h = float[h]
    
        def __call__[self, x]:
            f, h = self.f, self.h      # make short forms
            return [f[x+h] - f[x]]/h
    
    984
  • class Derivative[object]:
        def __init__[self, f, h=1E-5]:
            self.f = f
            self.h = float[h]
    
        def __call__[self, x]:
            f, h = self.f, self.h      # make short forms
            return [f[x+h] - f[x]]/h
    
    985.
    class Derivative[object]:
        def __init__[self, f, h=1E-5]:
            self.f = f
            self.h = float[h]
    
        def __call__[self, x]:
            f, h = self.f, self.h      # make short forms
            return [f[x+h] - f[x]]/h
    
    986
  • class Derivative[object]:
        def __init__[self, f, h=1E-5]:
            self.f = f
            self.h = float[h]
    
        def __call__[self, x]:
            f, h = self.f, self.h      # make short forms
            return [f[x+h] - f[x]]/h
    
    987.
    class Derivative[object]:
        def __init__[self, f, h=1E-5]:
            self.f = f
            self.h = float[h]
    
        def __call__[self, x]:
            f, h = self.f, self.h      # make short forms
            return [f[x+h] - f[x]]/h
    
    988
  • đánh giá
    class Derivative[object]:
        def __init__[self, f, h=1E-5]:
            self.f = f
            self.h = float[h]
    
        def __call__[self, x]:
            f, h = self.f, self.h      # make short forms
            return [f[x+h] - f[x]]/h
    
    09 dưới dạng biểu thức boolean [như trong thử nghiệm
    class Derivative[object]:
        def __init__[self, f, h=1E-5]:
            self.f = f
            self.h = float[h]
    
        def __call__[self, x]:
            f, h = self.f, self.h      # make short forms
            return [f[x+h] - f[x]]/h
    
    990] hàm ý gọi phương thức đặc biệt
    class Derivative[object]:
        def __init__[self, f, h=1E-5]:
            self.f = f
            self.h = float[h]
    
        def __call__[self, x]:
            f, h = self.f, self.h      # make short forms
            return [f[x+h] - f[x]]/h
    
    991, phương thức này phải trả về
    class Derivative[object]:
        def __init__[self, f, h=1E-5]:
            self.f = f
            self.h = float[h]
    
        def __call__[self, x]:
            f, h = self.f, self.h      # make short forms
            return [f[x+h] - f[x]]/h
    
    5 hoặc
    class Derivative[object]:
        def __init__[self, f, h=1E-5]:
            self.f = f
            self.h = float[h]
    
        def __call__[self, x]:
            f, h = self.f, self.h      # make short forms
            return [f[x+h] - f[x]]/h
    
    6 - nếu
    class Derivative[object]:
        def __init__[self, f, h=1E-5]:
            self.f = f
            self.h = float[h]
    
        def __call__[self, x]:
            f, h = self.f, self.h      # make short forms
            return [f[x+h] - f[x]]/h
    
    994 không được xác định, thì
    class Derivative[object]:
        def __init__[self, f, h=1E-5]:
            self.f = f
            self.h = float[h]
    
        def __call__[self, x]:
            f, h = self.f, self.h      # make short forms
            return [f[x+h] - f[x]]/h
    
    995 được gọi để xem độ dài có bằng không [
    class Derivative[object]:
        def __init__[self, f, h=1E-5]:
            self.f = f
            self.h = float[h]
    
        def __call__[self, x]:
            f, h = self.f, self.h      # make short forms
            return [f[x+h] - f[x]]/h
    
    6] hay không [
    class Derivative[object]:
        def __init__[self, f, h=1E-5]:
            self.f = f
            self.h = float[h]
    
        def __call__[self, x]:
            f, h = self.f, self.h      # make short forms
            return [f[x+h] - f[x]]/h
    
    5]
Chúng ta có thể triển khai các phương thức như vậy trong lớp
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
254, xem Bài tập 25. Thực hiện phép trừ đa thức. Phần Ví dụ. Lớp cho các vectơ trong mặt phẳng chứa các ví dụ về cách triển khai các phương thức đặc biệt được liệt kê ở trên

Các phương pháp đặc biệt để chuyển đổi chuỗi

Nhìn vào lớp này với một phương pháp

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
794

________ 392 Hy vọng bạn hiểu rõ tại sao chúng ta có kết quả này [nếu không, quay lại phần Biến thể hiện thành chuỗi]

Nhưng điều gì sẽ xảy ra nếu chúng ta chỉ viết

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
09 tại dấu nhắc lệnh trong trình bao tương tác?

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
93Khi chỉ viết
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
09 trong một phiên tương tác, Python tìm kiếm một phương thức đặc biệt
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
902 trong
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
09. Phương pháp này tương tự như
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
794 ở chỗ nó biến thể hiện thành một chuỗi, nhưng có một quy ước rằng
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
794 là một bản in đẹp của nội dung thể hiện trong khi
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
902 là một đại diện hoàn chỉnh cho nội dung của thể hiện. Đối với nhiều lớp Python, bao gồm
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
907,
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
82,
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
909,
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
910,
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
911 và
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
912,
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
902 và
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
794 cho kết quả giống hệt nhau. Trong lớp
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
915 của chúng tôi,
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
902 bị thiếu và chúng tôi cần thêm nó nếu chúng tôi muốn
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
94 viết nội dung như
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
795

Đưa ra một ví dụ

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
09,
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
218 ngụ ý gọi
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
920 và
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
921 ngụ ý gọi
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
922. Điều này có nghĩa rằng

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
94 thực sự là một cuộc gọi
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
921 và
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
96 thực sự là một câu lệnh
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
220

Một biện pháp khắc phục đơn giản trong lớp

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
915 là định nghĩa

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
97Tuy nhiên, như chúng tôi giải thích bên dưới,
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
902 được định nghĩa khác đi tốt nhất

Tái tạo đối tượng từ chuỗi

Hàm Python

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
927 đánh giá một biểu thức Python hợp lệ có trong chuỗi
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
723. Đó là một quy ước rằng
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
902 trả về một chuỗi sao cho
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
930 được áp dụng cho chuỗi sẽ tạo lại thể hiện. Ví dụ: trong trường hợp lớp
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
7 từ phần Biểu diễn hàm dưới dạng lớp,
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
902 sẽ trả về
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
933 nếu biến
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
934 có giá trị
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
935. Sau đó,
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
936 sẽ giống như khi chúng tôi đã mã hóa
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
937 trực tiếp trong chương trình hoặc phiên tương tác

Dưới đây chúng tôi đưa ra các ví dụ về các phương thức

class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
902 trong các lớp
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
7 [phần Biểu diễn một hàm dưới dạng một lớp],
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
254 [phần Ví dụ. Hạng cho đa thức], và
class Derivative[object]:
    def __init__[self, f, h=1E-5]:
        self.f = f
        self.h = float[h]

    def __call__[self, x]:
        f, h = self.f, self.h      # make short forms
        return [f[x+h] - f[x]]/h
915 [ở trên]

Bạn có thể gọi một hàm từ một lớp khác trong Python không?

Gọi phương thức từ một lớp khác trong một lớp khác trong Python. chúng ta có thể gọi phương thức của lớp khác bằng cách sử dụng tên lớp và hàm của lớp đó với toán tử dấu chấm . sau đó chúng ta có thể gọi method_A từ lớp B bằng cách sau. hạng A. method_A[self]. {} hạng B. method_B[self]. Một.

một chức năng gọi là gì trong một lớp học?

Hàm thành viên của một lớp là một hàm có định nghĩa hoặc nguyên mẫu của nó trong định nghĩa lớp giống như bất kỳ biến nào khác. Nó hoạt động trên bất kỳ đối tượng nào của lớp mà nó là thành viên và có quyền truy cập vào tất cả các thành viên của lớp cho đối tượng đó.

Hàm __ gọi __ trong Python là gì?

Phương thức __call__ cho phép các lập trình viên Python viết các lớp trong đó các thể hiện hoạt động giống như các hàm và có thể được gọi giống như một hàm . Khi thể hiện được gọi là một hàm; . ] là viết tắt của x. __gọi__[arg1, arg2,. ].

Cách tốt nhất để gọi một hàm trong Python là gì?

Sau khi một hàm được tạo bằng Python, chúng ta có thể gọi hàm đó bằng cách viết chính tên_hàm[] hoặc một hàm/hàm lồng nhau khác . Sau đây là cú pháp gọi một hàm. cú pháp. def function_name[].

Chủ Đề