mirror of
https://github.com/apachecn/ailearning.git
synced 2026-05-11 19:17:27 +08:00
2020-10-19 21:08:55
This commit is contained in:
122
docs/da/058.md
122
docs/da/058.md
@@ -12,7 +12,7 @@ $$\frac{d}{dx} F(x) = f(x) \Rightarrow F(x) = \int f(x) dx$$
|
||||
|
||||
In [1]:
|
||||
|
||||
```
|
||||
```py
|
||||
from sympy import init_printing
|
||||
init_printing()
|
||||
|
||||
@@ -20,7 +20,7 @@ init_printing()
|
||||
|
||||
In [2]:
|
||||
|
||||
```
|
||||
```py
|
||||
from sympy import symbols, integrate
|
||||
import sympy
|
||||
|
||||
@@ -30,7 +30,7 @@ import sympy
|
||||
|
||||
In [3]:
|
||||
|
||||
```
|
||||
```py
|
||||
x, y = symbols('x y')
|
||||
sympy.sqrt(x ** 2 + y ** 2)
|
||||
|
||||
@@ -42,7 +42,7 @@ Out[3]:$$\sqrt{x^{2} + y^{2}}$$
|
||||
|
||||
In [4]:
|
||||
|
||||
```
|
||||
```py
|
||||
z = sympy.sqrt(x ** 2 + y ** 2)
|
||||
z.subs(x, 3)
|
||||
|
||||
@@ -54,7 +54,7 @@ Out[4]:$$\sqrt{y^{2} + 9}$$
|
||||
|
||||
In [5]:
|
||||
|
||||
```
|
||||
```py
|
||||
z.subs(x, 3).subs(y, 4)
|
||||
|
||||
```
|
||||
@@ -65,7 +65,7 @@ Out[5]:$$5$$
|
||||
|
||||
In [6]:
|
||||
|
||||
```
|
||||
```py
|
||||
from sympy.abc import theta
|
||||
y = sympy.sin(theta) ** 2
|
||||
y
|
||||
@@ -78,7 +78,7 @@ Out[6]:$$\sin^{2}{\left (\theta \right )}$$
|
||||
|
||||
In [7]:
|
||||
|
||||
```
|
||||
```py
|
||||
Y = integrate(y)
|
||||
Y
|
||||
|
||||
@@ -90,7 +90,7 @@ Out[7]:$$\frac{\theta}{2} - \frac{1}{2} \sin{\left (\theta \right )} \cos{\left
|
||||
|
||||
In [8]:
|
||||
|
||||
```
|
||||
```py
|
||||
import numpy as np
|
||||
np.set_printoptions(precision=3)
|
||||
|
||||
@@ -104,7 +104,7 @@ Out[8]:$$1.5707963267949$$
|
||||
|
||||
In [9]:
|
||||
|
||||
```
|
||||
```py
|
||||
integrate(y, (theta, 0, sympy.pi))
|
||||
|
||||
```
|
||||
@@ -115,14 +115,14 @@ Out[9]:$$\frac{\pi}{2}$$
|
||||
|
||||
In [10]:
|
||||
|
||||
```
|
||||
```py
|
||||
integrate(y, (theta, 0, sympy.pi)).evalf()
|
||||
|
||||
```
|
||||
|
||||
Out[10]:$$1.5707963267949$$In [11]:
|
||||
|
||||
```
|
||||
```py
|
||||
integrate(y, (theta, 0, np.pi))
|
||||
|
||||
```
|
||||
@@ -135,7 +135,7 @@ Out[11]:$$1.5707963267949$$
|
||||
|
||||
In [12]:
|
||||
|
||||
```
|
||||
```py
|
||||
Y_indef = sympy.Integral(y)
|
||||
Y_indef
|
||||
|
||||
@@ -143,12 +143,12 @@ Y_indef
|
||||
|
||||
Out[12]:$$\int \sin^{2}{\left (\theta \right )}\, d\theta$$In [13]:
|
||||
|
||||
```
|
||||
```py
|
||||
print type(Y_indef)
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
<class 'sympy.integrals.integrals.Integral'>
|
||||
|
||||
```
|
||||
@@ -157,7 +157,7 @@ print type(Y_indef)
|
||||
|
||||
In [14]:
|
||||
|
||||
```
|
||||
```py
|
||||
Y_def = sympy.Integral(y, (theta, 0, sympy.pi))
|
||||
Y_def
|
||||
|
||||
@@ -169,7 +169,7 @@ Out[14]:$$\int_{0}^{\pi} \sin^{2}{\left (\theta \right )}\, d\theta$$
|
||||
|
||||
In [15]:
|
||||
|
||||
```
|
||||
```py
|
||||
Y_raw = lambda x: integrate(y, (theta, 0, x))
|
||||
Y = np.vectorize(Y_raw)
|
||||
|
||||
@@ -177,7 +177,7 @@ Y = np.vectorize(Y_raw)
|
||||
|
||||
In [16]:
|
||||
|
||||
```
|
||||
```py
|
||||
%matplotlib inline
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
@@ -356,14 +356,14 @@ $$F(x) = \lim_{n \rightarrow \infty} \sum_{i=0}^{n-1} f(x_i)(x_{i+1}-x_i) \Right
|
||||
|
||||
In [17]:
|
||||
|
||||
```
|
||||
```py
|
||||
from scipy.special import jv
|
||||
|
||||
```
|
||||
|
||||
In [18]:
|
||||
|
||||
```
|
||||
```py
|
||||
def f(x):
|
||||
return jv(2.5, x)
|
||||
|
||||
@@ -371,7 +371,7 @@ def f(x):
|
||||
|
||||
In [19]:
|
||||
|
||||
```
|
||||
```py
|
||||
x = np.linspace(0, 10)
|
||||
p = plt.plot(x, f(x), 'k-')
|
||||
|
||||
@@ -594,7 +594,7 @@ quad 返回一个 (积分值,误差) 组成的元组:
|
||||
|
||||
In [20]:
|
||||
|
||||
```
|
||||
```py
|
||||
from scipy.integrate import quad
|
||||
interval = [0, 6.5]
|
||||
value, max_err = quad(f, *interval)
|
||||
@@ -605,12 +605,12 @@ value, max_err = quad(f, *interval)
|
||||
|
||||
In [21]:
|
||||
|
||||
```
|
||||
```py
|
||||
print value
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
1.28474297234
|
||||
|
||||
```
|
||||
@@ -619,12 +619,12 @@ print value
|
||||
|
||||
In [22]:
|
||||
|
||||
```
|
||||
```py
|
||||
print max_err
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
2.34181853668e-09
|
||||
|
||||
```
|
||||
@@ -633,7 +633,7 @@ print max_err
|
||||
|
||||
In [23]:
|
||||
|
||||
```
|
||||
```py
|
||||
print "integral = {:.9f}".format(value)
|
||||
print "upper bound on error: {:.2e}".format(max_err)
|
||||
x = np.linspace(0, 10, 100)
|
||||
@@ -644,7 +644,7 @@ p = plt.fill_between(x, f(x), where=f(x)<0, color="red", interpolate=True)
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
integral = 1.284742972
|
||||
upper bound on error: 2.34e-09
|
||||
|
||||
@@ -854,7 +854,7 @@ R6zoG2NMHLGib4wxceT/AZhppPNX9o1QAAAAAElFTkSuQmCC
|
||||
|
||||
In [24]:
|
||||
|
||||
```
|
||||
```py
|
||||
from numpy import inf
|
||||
interval = [0., inf]
|
||||
|
||||
@@ -865,7 +865,7 @@ def g(x):
|
||||
|
||||
In [25]:
|
||||
|
||||
```
|
||||
```py
|
||||
value, max_err = quad(g, *interval)
|
||||
x = np.linspace(0, 10, 50)
|
||||
fig = plt.figure(figsize=(10,3))
|
||||
@@ -877,7 +877,7 @@ print "upper bound on error: {:.1e}".format(max_err)
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
upper bound on error: 7.2e-11
|
||||
|
||||
```
|
||||
@@ -1083,7 +1083,7 @@ TkSuQmCC
|
||||
|
||||
$$ I_n = \int \limits_0^{\infty} \int \limits_1^{\infty} \frac{e^{-xt}}{t^n}dt dx = \frac{1}{n}$$In [26]:
|
||||
|
||||
```
|
||||
```py
|
||||
def h(x, t, n):
|
||||
"""core function, takes x, t, n"""
|
||||
return np.exp(-x * t) / (t ** n)
|
||||
@@ -1094,7 +1094,7 @@ def h(x, t, n):
|
||||
|
||||
In [27]:
|
||||
|
||||
```
|
||||
```py
|
||||
from numpy import vectorize
|
||||
@vectorize
|
||||
def int_h_dx(t, n):
|
||||
@@ -1105,7 +1105,7 @@ def int_h_dx(t, n):
|
||||
|
||||
In [28]:
|
||||
|
||||
```
|
||||
```py
|
||||
@vectorize
|
||||
def I_n(n):
|
||||
return quad(int_h_dx, 1, np.inf, args=(n))
|
||||
@@ -1114,14 +1114,14 @@ def I_n(n):
|
||||
|
||||
In [29]:
|
||||
|
||||
```
|
||||
```py
|
||||
I_n([0.5, 1.0, 2.0, 5])
|
||||
|
||||
```
|
||||
|
||||
Out[29]:
|
||||
|
||||
```
|
||||
```py
|
||||
(array([ 1.97, 1\. , 0.5 , 0.2 ]),
|
||||
array([ 9.804e-13, 1.110e-14, 5.551e-15, 2.220e-15]))
|
||||
```
|
||||
@@ -1130,7 +1130,7 @@ Out[29]:
|
||||
|
||||
In [30]:
|
||||
|
||||
```
|
||||
```py
|
||||
from scipy.integrate import dblquad
|
||||
@vectorize
|
||||
def I(n):
|
||||
@@ -1145,14 +1145,14 @@ def I(n):
|
||||
|
||||
In [31]:
|
||||
|
||||
```
|
||||
```py
|
||||
I_n([0.5, 1.0, 2.0, 5])
|
||||
|
||||
```
|
||||
|
||||
Out[31]:
|
||||
|
||||
```
|
||||
```py
|
||||
(array([ 1.97, 1\. , 0.5 , 0.2 ]),
|
||||
array([ 9.804e-13, 1.110e-14, 5.551e-15, 2.220e-15]))
|
||||
```
|
||||
@@ -1163,7 +1163,7 @@ Out[31]:
|
||||
|
||||
In [32]:
|
||||
|
||||
```
|
||||
```py
|
||||
from scipy.integrate import trapz, simps
|
||||
|
||||
```
|
||||
@@ -1172,7 +1172,7 @@ from scipy.integrate import trapz, simps
|
||||
|
||||
In [33]:
|
||||
|
||||
```
|
||||
```py
|
||||
x_s = np.linspace(0, np.pi, 5)
|
||||
y_s = np.sin(x_s)
|
||||
x = np.linspace(0, np.pi, 100)
|
||||
@@ -1182,7 +1182,7 @@ y = np.sin(x)
|
||||
|
||||
In [34]:
|
||||
|
||||
```
|
||||
```py
|
||||
p = plt.plot(x, y, 'k:')
|
||||
p = plt.plot(x_s, y_s, 'k+-')
|
||||
p = plt.fill_between(x_s, y_s, color="gray")
|
||||
@@ -1416,7 +1416,7 @@ QmCC
|
||||
|
||||
In [35]:
|
||||
|
||||
```
|
||||
```py
|
||||
result_s = trapz(y_s, x_s)
|
||||
result_s_s = simps(y_s, x_s)
|
||||
result = trapz(y, x)
|
||||
@@ -1426,7 +1426,7 @@ print "Trapezoidal Integration over 100 points : {:.3f}".format(result)
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
Trapezoidal Integration over 5 points : 1.896
|
||||
Simpson Integration over 5 points : 2.005
|
||||
Trapezoidal Integration over 100 points : 2.000
|
||||
@@ -1439,25 +1439,25 @@ Trapezoidal Integration over 100 points : 2.000
|
||||
|
||||
In [36]:
|
||||
|
||||
```
|
||||
```py
|
||||
type(np.add)
|
||||
|
||||
```
|
||||
|
||||
Out[36]:
|
||||
|
||||
```
|
||||
```py
|
||||
numpy.ufunc
|
||||
```
|
||||
|
||||
In [37]:
|
||||
|
||||
```
|
||||
```py
|
||||
np.info(np.add.accumulate)
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
accumulate(array, axis=0, dtype=None, out=None)
|
||||
|
||||
Accumulate the result of applying the operator to all elements.
|
||||
@@ -1534,14 +1534,14 @@ array([[ 1., 1.],
|
||||
|
||||
In [38]:
|
||||
|
||||
```
|
||||
```py
|
||||
result_np = np.add.accumulate(y) * (x[1] - x[0]) - (x[1] - x[0]) / 2
|
||||
|
||||
```
|
||||
|
||||
In [39]:
|
||||
|
||||
```
|
||||
```py
|
||||
p = plt.plot(x, - np.cos(x) + np.cos(0), 'rx')
|
||||
p = plt.plot(x, result_np)
|
||||
|
||||
@@ -1719,7 +1719,7 @@ RoFfRCTGKPCLiMSY/weoVZxsAST89wAAAABJRU5ErkJggg==
|
||||
|
||||
In [40]:
|
||||
|
||||
```
|
||||
```py
|
||||
import sympy
|
||||
from sympy.abc import x, theta
|
||||
sympy_x = x
|
||||
@@ -1728,7 +1728,7 @@ sympy_x = x
|
||||
|
||||
In [41]:
|
||||
|
||||
```
|
||||
```py
|
||||
x = np.linspace(0, 20 * np.pi, 1e+4)
|
||||
y = np.sin(x)
|
||||
sympy_y = vectorize(lambda x: sympy.integrate(sympy.sin(theta), (theta, 0, x)))
|
||||
@@ -1739,14 +1739,14 @@ sympy_y = vectorize(lambda x: sympy.integrate(sympy.sin(theta), (theta, 0, x)))
|
||||
|
||||
In [42]:
|
||||
|
||||
```
|
||||
```py
|
||||
%timeit np.add.accumulate(y) * (x[1] - x[0])
|
||||
y0 = np.add.accumulate(y) * (x[1] - x[0])
|
||||
print y0[-1]
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
The slowest run took 4.32 times longer than the fastest. This could mean that an intermediate result is being cached
|
||||
10000 loops, best of 3: 56.2 µs per loop
|
||||
-2.34138044756e-17
|
||||
@@ -1757,7 +1757,7 @@ The slowest run took 4.32 times longer than the fastest. This could mean that an
|
||||
|
||||
In [43]:
|
||||
|
||||
```
|
||||
```py
|
||||
%timeit quad(np.sin, 0, 20 * np.pi)
|
||||
y2 = quad(np.sin, 0, 20 * np.pi, full_output=True)
|
||||
print "result = ", y2[0]
|
||||
@@ -1765,7 +1765,7 @@ print "number of evaluations", y2[-1]['neval']
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
10000 loops, best of 3: 40.5 µs per loop
|
||||
result = 3.43781337153e-15
|
||||
number of evaluations 21
|
||||
@@ -1776,14 +1776,14 @@ number of evaluations 21
|
||||
|
||||
In [44]:
|
||||
|
||||
```
|
||||
```py
|
||||
%timeit trapz(y, x)
|
||||
y1 = trapz(y, x)
|
||||
print y1
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
10000 loops, best of 3: 105 µs per loop
|
||||
-4.4408920985e-16
|
||||
|
||||
@@ -1793,14 +1793,14 @@ print y1
|
||||
|
||||
In [45]:
|
||||
|
||||
```
|
||||
```py
|
||||
%timeit simps(y, x)
|
||||
y3 = simps(y, x)
|
||||
print y3
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
1000 loops, best of 3: 801 µs per loop
|
||||
3.28428554968e-16
|
||||
|
||||
@@ -1810,14 +1810,14 @@ print y3
|
||||
|
||||
In [46]:
|
||||
|
||||
```
|
||||
```py
|
||||
%timeit sympy_y(20 * np.pi)
|
||||
y4 = sympy_y(20 * np.pi)
|
||||
print y4
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
100 loops, best of 3: 6.86 ms per loop
|
||||
0
|
||||
|
||||
|
||||
Reference in New Issue
Block a user