Ticket #10983: sol_calculus_fixed.py

File sol_calculus_fixed.py, 7.7 KB (added by casamayou, 5 years ago)
Line 
1r"""
2sage: var('n k'); p = 4; s = [n + 1]
3(n, k)
4sage: for k in (1..p):
5...     s = s + [factor((((n + 1)^(k + 1) - sum(binomial(k + 1, j)*s[j]
6...              for j in (0..k - 1))) / (k + 1)))]
7sage: s
8[n + 1, 1/2*(n + 1)*n, 1/6*(n + 1)*(2*n + 1)*n, 1/4*(n + 1)^2*n^2, 1/30*(n + 1)*(2*n + 1)*(3*n^2 + 3*n - 1)*n]
9"""
10
11r"""
12sage: var('x h a'); f = function('f', x)
13(x, h, a)
14sage: g(x) = taylor(f, x, a, 3)
15sage: phi(h) = (g(a+3*h)-3*g(a+2*h)+3*g(a+h)-g(a))/h**3
16sage: phi(h).expand()
17D[0, 0, 0](f)(a)
18sage: n = 7; var('x h a'); f = function('f', x)
19(x, h, a)
20sage: g(x) = taylor(f, x, a, n)
21sage: phi(h) = sum(binomial(n,k)*(-1)^(n-k)*g(a+k*h) for k in (0..n))/h**n
22sage: phi(h).expand()
23D[0, 0, 0, 0, 0, 0, 0](f)(a)
24"""
25
26r"""
27sage: theta = 12 * arctan(1/38) + 20 * arctan(1/57) + 7 * arctan(1/239) + 24 * arctan(1/268)
28sage: x = tan(theta)
29sage: y = x.trig_expand()
30sage: y.trig_simplify()
311
32sage: M = 12*(1/38)+20*(1/57)+ 7*(1/239)+24*(1/268)
33sage: M
3437735/48039
35sage: x = var('x')
36sage: f(x) = taylor(arctan(x), x, 0, 21)
37sage: approx = 4 * (12 * f(1/38) + 20 * f(1/57) + 7 * f(1/239) + 24 * f(1/268))
38sage: approx.n(digits = 50); pi.n(digits = 50)
393.1415926535897932384626433832795028851616168852864
403.1415926535897932384626433832795028841971693993751
41sage: approx.n(digits = 50) - pi.n(digits = 50)
429.6444748591132486785420917537404705292978817080880e-37
43"""
44
45r"""
46sage: n = var('n'); phi = lambda x: n*pi +pi/2 - arctan(1/x); x = pi * n
47sage: for i in range(4): x = taylor(phi(x), n, oo, 2 * i); x
481/2*pi + pi*n
491/2*pi + pi*n - 1/(pi*n) + 1/2/(pi*n^2)
501/2*pi + pi*n - 1/(pi*n) + 1/2/(pi*n^2) - 1/12*(3*pi^2 + 8)/(pi^3*n^3) + 1/8*(pi^2 + 8)/(pi^3*n^4)
511/2*pi + pi*n - 1/(pi*n) + 1/2/(pi*n^2) - 1/12*(3*pi^2 + 8)/(pi^3*n^3) + 1/8*(pi^2 + 8)/(pi^3*n^4) - 1/240*(15*pi^4 + 240*pi^2 + 208)/(pi^5*n^5) + 1/96*(3*pi^4 + 80*pi^2 + 208)/(pi^5*n^6)
52"""
53
54r"""
55sage: var('h')
56h
57sage: f(x, y) = x * y * (x**2 - y**2) / (x**2 + y**2)
58sage: D1f(x, y) = diff(f(x,y), x)
59sage: limit((D1f(0,h) - 0) / h, h=0)
60-1
61sage: D2f(x, y) = diff(f(x,y), y)
62sage: limit((D2f(h,0) - 0) / h, h=0)
631
64sage: g = plot3d(f(x, y), (x, -3, 3), (y, -3, 3))
65"""
66
67r"""
68sage: var('n t')
69(n, t)
70sage: v(n) = (4/(8*n+1)-2/(8*n+4)-1/(8*n+5)-1/(8*n+6))*1/16^n
71sage: assume(8*n+1>0)
72sage: u(n) = integrate((4*sqrt(2)-8*t^3-4*sqrt(2)*t^4-8*t^5)\
73...                     * t^(8*n), t, 0, 1/sqrt(2))
74sage: (u(n)-v(n)).simplify_full()
750
76sage: J = integrate((4*sqrt(2)-8*t^3-4*sqrt(2)*t^4-8*t^5)\
77...                 / (1-t^8), t, 0, 1/sqrt(2))
78sage: J.simplify_full()
79pi + 2*log(sqrt(2) - 1) + 2*log(sqrt(2) + 1)
80sage: ln(exp(J).simplify_log())
81pi
82sage: l = sum(v(n) for n in (0..40)); l.n(digits=60); pi.n(digits=60)
833.14159265358979323846264338327950288419716939937510581474759
843.14159265358979323846264338327950288419716939937510582097494
85sage: print "%e" % (l-pi).n(digits=60)
86-6.227358e-54
87"""
88
89r"""
90sage: var('X'); ps = lambda f,g : integral(f * g, X, -pi, pi)
91X
92sage: n = 5; Q = sin(X)
93sage: var('a a0 a1 a2 a3 a4 a5'); a= [a0, a1, a2, a3, a4, a5]
94(a, a0, a1, a2, a3, a4, a5)
95sage: P = sum(a[k] * X^k for k in (0..n))
96sage: equ = [ps(P - Q, X^k) for k in (0..n)]
97sage: sol = solve(equ, a)
98sage: P = sum(sol[0][k].rhs() * X^k for k in (0..n))
99sage: g = plot(P,X,-4,4,color='red') + plot(Q,X,-4,4,color='blue')
100"""
101
102r"""
103sage: var('p e theta1 theta2 theta3')
104(p, e, theta1, theta2, theta3)
105sage: r(theta) = p / (1-e * cos(theta))
106sage: r1 = r(theta1); r2 = r(theta2); r3 = r(theta3)
107sage: R1 = vector([r1 * cos(theta1), r1 * sin(theta1), 0])
108sage: R2 = vector([r2 * cos(theta2), r2 * sin(theta2), 0])
109sage: R3 = vector([r3 * cos(theta3), r3 * sin(theta3), 0])
110sage: D = R1.cross_product(R2) + R2.cross_product(R3) + R3.cross_product(R1)
111sage: i = vector([1, 0, 0])
112sage: S = (r1 - r3) * R2 + (r3 - r2) * R1 +   (r2 - r1) * R3
113sage: V =  S + e * i.cross_product(D)
114sage: map(lambda x:x.simplify_full(), V) # rep. : [0, 0, 0]
115[0, 0, 0]
116sage: map(lambda x:x.simplify_full(), S.cross_product(D))
117[(e*p^4*sin(theta1)^2*cos(theta2)^2 - 2*e*p^4*sin(theta1)*sin(theta2)*cos(theta1)*cos(theta2) + e*p^4*sin(theta2)^2*cos(theta1)^2 + (e*p^4*cos(theta1)^2 - 2*e*p^4*cos(theta1)*cos(theta2) + e*p^4*cos(theta2)^2)*sin(theta3)^2 + (e*p^4*sin(theta1)^2 - 2*e*p^4*sin(theta1)*sin(theta2) + e*p^4*sin(theta2)^2)*cos(theta3)^2 - 2*(e*p^4*sin(theta1)^2*cos(theta2) + e*p^4*sin(theta2)^2*cos(theta1) - (e*p^4*sin(theta1)*cos(theta1) + e*p^4*sin(theta1)*cos(theta2))*sin(theta2))*cos(theta3) + 2*(e*p^4*sin(theta1)*cos(theta1)*cos(theta2) - e*p^4*sin(theta1)*cos(theta2)^2 - (e*p^4*cos(theta1)^2 - e*p^4*cos(theta1)*cos(theta2))*sin(theta2) - (e*p^4*sin(theta1)*cos(theta1) - e*p^4*sin(theta1)*cos(theta2) - (e*p^4*cos(theta1) - e*p^4*cos(theta2))*sin(theta2))*cos(theta3))*sin(theta3))/(e^2*cos(theta1)^2 + (e^4*cos(theta1)^2 - 2*e^3*cos(theta1) + e^2)*cos(theta2)^2 + (e^4*cos(theta1)^2 - 2*e^3*cos(theta1) + (e^6*cos(theta1)^2 - 2*e^5*cos(theta1) + e^4)*cos(theta2)^2 - 2*(e^5*cos(theta1)^2 - 2*e^4*cos(theta1) + e^3)*cos(theta2) + e^2)*cos(theta3)^2 - 2*(e^3*cos(theta1)^2 - 2*e^2*cos(theta1) + e)*cos(theta2) - 2*(e^3*cos(theta1)^2 + (e^5*cos(theta1)^2 - 2*e^4*cos(theta1) + e^3)*cos(theta2)^2 - 2*e^2*cos(theta1) - 2*(e^4*cos(theta1)^2 - 2*e^3*cos(theta1) + e^2)*cos(theta2) + e)*cos(theta3) - 2*e*cos(theta1) + 1), 0, 0]
118sage: N = r3 * R1.cross_product(R2) + r1 * R2.cross_product(R3) + r2 * R3.cross_product(R1)
119sage: W =  p * S + e * i.cross_product(N)
120sage: print map(lambda x:x.simplify_full(), W)  # rep. : [0, 0, 0]
121[0, 0, 0]
122sage: R1=vector([0,1.,0]);R2=vector([2.,2.,0]);R3=vector([3.5,0,0])
123sage: r1 = R1.norm(); r2 = R2.norm(); r3 = R3.norm()
124sage: D = R1.cross_product(R2) + R2.cross_product(R3) + R3.cross_product(R1)
125sage: S = (r1 - r3) * R2 + (r3 - r2) * R1 + (r2 - r1) * R3
126sage: V =  S + e * i.cross_product(D)
127sage: N = r3 * R1.cross_product(R2) + r1 * R2.cross_product(R3) \
128...  + r2 * R3.cross_product(R1)
129sage: W =  p * S + e * i.cross_product(N)
130sage: e = S.norm() / D.norm()
131sage: p = N.norm() / D.norm()
132sage: a = p/(1-e^2)
133sage: c = a * e
134sage: b = sqrt(a^2 - c^2)
135sage: X = S.cross_product(D)
136sage: i = X / X.norm()
137sage: phi = atan2(i[1],i[0]) * 180 / pi.n()
138sage: print "%.3f %.3f %.3f %.3f %.3f %.3f" % (a, b, c, e, p, phi)
1392.360 1.326 1.952 0.827 0.746 17.917
140"""
141
142r"""
143sage: A = matrix(QQ, [[2, -3, 2, -12, 33],
144...                   [ 6, 1, 26, -16, 69],
145...                   [10, -29, -18, -53, 32],
146...                   [2, 0, 8, -18, 84]])
147sage: A.right_kernel()
148Vector space of degree 5 and dimension 2 over Rational Field
149Basis matrix:
150[     1      0  -7/34   5/17   1/17]
151[     0      1  -3/34 -10/17  -2/17]
152sage: H = A.echelon_form()
153sage: A.column_space()
154Vector space of degree 4 and dimension 3 over Rational Field
155Basis matrix:
156[       1        0        0 1139/350]
157[       0        1        0    -9/50]
158[       0        0        1   -12/35]
159sage: S.<x,y,z,t>=QQ[]
160sage: C = matrix(S, 4,1,[x,y,z,t])
161sage: B = block_matrix([A,C], ncols=2)
162sage: C = B.echelon_form()
163sage: C[3,5]*350
164-1139*x + 63*y + 120*z + 350*t
165sage: K = A.kernel(); K
166Vector space of degree 4 and dimension 1 over Rational Field
167Basis matrix:
168[        1  -63/1139 -120/1139 -350/1139]
169sage: matrix(K.0).right_kernel()
170Vector space of degree 4 and dimension 3 over Rational Field
171Basis matrix:
172[       1        0        0 1139/350]
173[       0        1        0    -9/50]
174[       0        0        1   -12/35]
175sage: A = matrix(QQ, [[-2, 1, 1], [8, 1, -5], [4, 3, -3]])
176sage: C = matrix(QQ, [[1, 2, -1], [2, -1, -1], [-5, 0, 3]])
177sage: B = C.solve_left(A); B
178[ 0 -1  0]
179[ 2  3  0]
180[ 2  1  0]
181sage: C.left_kernel()
182Vector space of degree 3 and dimension 1 over Rational Field
183Basis matrix:
184[1 2 1]
185sage: var('x y z'); v = matrix([[1, 2, 1]])
186(x, y, z)
187sage: B = B+(x*v).stack(y*v).stack(z*v); B
188[      x 2*x - 1       x]
189[  y + 2 2*y + 3       y]
190[  z + 2 2*z + 1       z]
191sage: A == B*C
192True
193
194"""