Ticket #10983: calculus.2.py

File calculus.2.py, 9.7 KB (added by casamayou, 6 years ago)
Line 
1r"""
2sage: var ('a,x') ; y = cos(x+a)*(x+1) ; y
3(a, x)
4(x + 1)*cos(a + x)
5sage: var ('a,x') ; y = cos(x+a)*(x+1) ; y
6(a, x)
7(x + 1)*cos(a + x)
8sage: y.subs(a=-x) ; y.subs(x=pi/2,a=pi/3) ; y.subs(x=0.5,a=2.3)
9x + 1
10-1/4*(pi + 2)*sqrt(3)
11-1.41333351100299
12sage: y(a=-x) ; y(x=pi/2,a=pi/3) ; y(x=0.5,a=2.3)
13x + 1
14-1/4*(pi + 2)*sqrt(3)
15-1.41333351100299
16sage: var('x,y,z') ; q = x*y+y*z+z*x
17(x, y, z)
18sage: bool (q(x=y,y=z,z=x)==q), bool(q(z=y)(y=x) == 3*x^2)
19(True, True)
20sage: var('y z'); f = x^3+y^2+z; f.subs_expr(x^3==y^2, z==1)
21(y, z)
222*y^2 + 1
23sage: f(x)=(2*x+1)^3 ; f(-3)
24  -125
25sage: f(x).expand()
268*x^3 + 12*x^2 + 6*x + 1
27sage: var('y'); u = sin(x) + x*cos(y)
28y
29sage: v = u.function(x, y); v
30(x, y) |--> x*cos(y) + sin(x)
31sage: w(x, y) = u; w
32(x, y) |--> x*cos(y) + sin(x)
33sage: (x^x/x).simplify()
34x^(x - 1)
35sage: f = (e^x-1)/(1+e^(x/2)); f.simplify_exp()
36e^(1/2*x) - 1
37sage: f = cos(x)^6 + sin(x)^6 + 3 * sin(x)^2 * cos(x)^2
38sage: f.simplify_trig()
391
40sage: f = cos(x)^6; f.reduce_trig()
4115/32*cos(2*x) + 3/16*cos(4*x) + 1/32*cos(6*x) + 5/16
42sage: f = sin(5 * x); f.expand_trig()
43sin(x)^5 - 10*sin(x)^3*cos(x)^2 + 5*sin(x)*cos(x)^4
44sage: n = var('n'); f = factorial(n+1)/factorial(n)
45sage: f.simplify_factorial()
46n + 1
47sage: f = sqrt(x^2); f.simplify_radical()
48abs(x)
49sage: f = log(x*y); f.simplify_radical()
50log(x) + log(y)
51sage: assume(x > 0); bool(sqrt(x^2) == x)
52True
53sage: forget(x > 0); bool(sqrt(x^2) == x)
54False
55sage: var('n'); assume(n, 'integer'); sin(n*pi).simplify()
56n
570
58sage: z, phi = var('z, phi')
59sage: solve(z**2 -2 / cos(phi) * z + 5 / cos(phi) ** 2 - 4, z)
60[z == -(2*sqrt(cos(phi)^2 - 1) - 1)/cos(phi), z == (2*sqrt(cos(phi)^2 - 1) + 1)/cos(phi)]
61sage: var('y'); solve(y^6==y, y)
62y
63[y == e^(2/5*I*pi), y == e^(4/5*I*pi), y == e^(-4/5*I*pi), y == e^(-2/5*I*pi), y == 1, y == 0]
64sage: solve(x^2-1, x, solution_dict=True)
65[{x: -1}, {x: 1}]
66sage: solve([x+y == 3, 2*x+2*y == 6],x,y)
67[[x == -r1 + 3, y == r1]]
68sage: solve([cos(x)*sin(x) == 1/2, x+y == 0], x, y)
69[[x == 1/4*pi + pi*z30, y == -1/4*pi - pi*z30]]
70sage: solve(x^2+x-1>0,x)
71[[x < -1/2*sqrt(5) - 1/2], [x > 1/2*sqrt(5) - 1/2]]
72sage: x, y, z = var('x, y, z')
73sage: solve([x^2 * y * z == 18, x * y^3 * z == 24,\
74             x * y * z^4 == 6], x, y, z)
75[[x == 3, y == 2, z == 1], [x == (1.33721506733 - 2.68548987407*I), y == (-1.70043427146 + 1.05286432575*I), z == (0.932472229404 - 0.361241666187*I)], [x == (1.33721506733 + 2.68548987407*I), y == (-1.70043427146 - 1.05286432575*I), z == (0.932472229404 + 0.361241666187*I)], [x == (-2.55065140719 - 1.57929648863*I), y == (-0.547325980144 + 1.92365128635*I), z == (-0.982973099684 - 0.183749517817*I)], [x == (-2.55065140719 + 1.57929648863*I), y == (-0.547325980144 - 1.92365128635*I), z == (-0.982973099684 + 0.183749517817*I)], [x == (0.27680507839 - 2.98720252889*I), y == (1.47801783444 - 1.34739128729*I), z == (-0.85021713573 - 0.526432162877*I)], [x == (0.27680507839 + 2.98720252889*I), y == (1.47801783444 + 1.34739128729*I), z == (-0.85021713573 + 0.526432162877*I)], [x == (-0.820988970216 + 2.88547692952*I), y == (-1.20526927276 - 1.59603445456*I), z == (0.0922683594633 - 0.995734176295*I)], [x == (-0.820988970216 - 2.88547692952*I), y == (-1.20526927276 + 1.59603445456*I), z == (0.0922683594633 + 0.995734176295*I)], [x == (-1.80790390914 - 2.39405168184*I), y == (0.891476711553 - 1.79032658271*I), z == (0.739008917221 - 0.673695643647*I)], [x == (-1.80790390914 + 2.39405168184*I), y == (0.891476711553 + 1.79032658271*I), z == (0.739008917221 + 0.673695643647*I)], [x == (2.21702675166 + 2.02108693094*I), y == (1.86494445881 + 0.722483332374*I), z == (-0.273662990072 - 0.961825643173*I)], [x == (2.21702675166 - 2.02108693094*I), y == (1.86494445881 - 0.722483332374*I), z == (-0.273662990072 + 0.961825643173*I)], [x == (2.79741668821 - 1.08372499856*I), y == (-1.96594619937 + 0.367499035633*I), z == (-0.602634636379 - 0.79801722728*I)], [x == (2.79741668821 + 1.08372499856*I), y == (-1.96594619937 - 0.367499035633*I), z == (-0.602634636379 + 0.79801722728*I)], [x == (-2.94891929905 + 0.55124855345*I), y == (0.184536718927 + 1.99146835259*I), z == (0.445738355777 - 0.895163291355*I)], [x == (-2.94891929905 - 0.55124855345*I), y == (0.184536718927 - 1.99146835259*I), z == (0.445738355777 + 0.895163291355*I)]]
76sage: expr = sin(x) + sin(2 * x) + sin(3 * x)
77sage: solve(expr, x)
78[sin(3*x) == -sin(2*x) - sin(x)]
79sage: find_root(expr, 0.1, pi)
802.0943951023931957
81sage: f = expr.simplify_trig(); f
822*(2*cos(x)^2 + cos(x))*sin(x)
83sage: solve(f, x)
84[x == 0, x == 2/3*pi, x == 1/2*pi]
85sage: (x^3+2*x+1).roots(x)
86[(-1/2*(I*sqrt(3) + 1)*(1/18*sqrt(3)*sqrt(59) - 1/2)^(1/3) - 1/3*(I*sqrt(3) - 1)/(1/18*sqrt(3)*sqrt(59) - 1/2)^(1/3), 1), (-1/2*(-I*sqrt(3) + 1)*(1/18*sqrt(3)*sqrt(59) - 1/2)^(1/3) - 1/3*(-I*sqrt(3) - 1)/(1/18*sqrt(3)*sqrt(59) - 1/2)^(1/3), 1), ((1/18*sqrt(3)*sqrt(59) - 1/2)^(1/3) - 2/3/(1/18*sqrt(3)*sqrt(59) - 1/2)^(1/3), 1)]
87sage: (x^3+2*x+1).roots(x, ring=RR)
88 [(-0.453397651516404, 1)]
89sage: (x^3+2*x+1).roots(x, ring=CC)
90 [(-0.453397651516404, 1), (0.226698825758202 - 1.46771150871022*I, 1), (0.226698825758202 + 1.46771150871022*I, 1)]
91sage: var('k n'); sum(k, k, 1, n).factor()
92 (k, n)
93 1/2*(n + 1)*n
94sage: var('n y'); sum(binomial(n,k) * x^k * y^(n-k), k, 0, n)
95 (n, y)
96 (x + y)^n
97sage: var('k n'); sum(binomial(n,k), k, 0, n),\
98                  sum(k * binomial(n, k), k, 0, n),\
99                  sum((-1)^k*binomial(n,k), k, 0, n)
100(k, n)
101(2^n, n*2^(n - 1), 0)
102sage: var('a q'); sum(a*q^k, k, 0, n)
103(a, q)
104(a*q^(n + 1) - a)/(q - 1)
105sage: assume(abs(q) < 1)
106sage: sum(a*q^k, k, 0, infinity)
107 -a/(q - 1)
108sage: limit((x**(1/3) -2) / ((x + 19)**(1/3) - 3), x = 8)
1099/4
110sage: f(x) = (cos(pi/4-x)-tan(x))/(1-sin(pi/4 + x))
111sage: limit(f(x), x = pi/4, dir='minus')
112+Infinity
113sage: limit(f(x), x = pi/4, dir='plus')
114-Infinity
115sage: u(n) = n^100 / 100.^n
116sage: u(1);u(2);u(3);u(4);u(5);u(6);u(7);u(8);u(9);u(10)
1170.0100000000000000
1181.26765060022823e26
1195.15377520732011e41
1201.60693804425899e52
1217.88860905221012e59
1226.53318623500071e65
1233.23447650962476e70
1242.03703597633449e74
1252.65613988875875e77
1261.00000000000000e80
127sage: plot(u(x), x, 1, 40)
128sage: v(x) = diff(u(x), x); sol = solve(v(x) == 0, x); sol
129[x == 0, x == (268850/12381)]
130sage: sol[1].rhs().n().floor()
13121
132sage: limit(u(n), n=infinity)
1330
134sage: n0 = find_root(u(n) - 1e-8 == 0, 22, 1000); n0
135105.07496210187252
136sage: taylor((1+arctan(x))**(1/x), x, 0, 3)
1371/16*x^3*e + 1/8*x^2*e - 1/2*x*e + e
138sage: (ln(2* sin(x))).series(x==pi/6, 3)
139(sqrt(3))*(-1/6*pi + x) + (-2)*(-1/6*pi + x)^2 + Order(-1/216*(pi - 6*x)^3)
140sage: (ln(2* sin(x))).series(x==pi/6, 3).truncate()
141-1/18*(pi - 6*x)^2 - 1/6*(pi - 6*x)*sqrt(3)
142sage: taylor((x**3+x)**(1/3) - (x**3-x)**(1/3), x, infinity, 2)
1432/3/x
144sage: tan(4*arctan(1/5)).simplify_trig()
145120/119
146sage: tan(pi/4+arctan(1/239)).simplify_trig()
147120/119
148sage: f = arctan(x).series(x, 10); f
1491*x + (-1/3)*x^3 + 1/5*x^5 + (-1/7)*x^7 + 1/9*x^9 + Order(x^10)
150sage: (16*f.subs(x==1/5) - 4*f.subs(x==1/239)).n(); pi.n()
1513.14159268240440
1523.14159265358979
153sage: var('k'); sum(1/k^2, k, 1, infinity),\
154                sum(1/k^4, k, 1, infinity),\
155                sum(1/k^5, k, 1, infinity)
156k
157(1/6*pi^2, 1/90*pi^4, zeta(5))
158sage: s = 2*sqrt(2)/9801*(sum((factorial(4*k))*(1103+26390*k)\
159     /((factorial(k)) ^ 4 * 396 ^ (4 * k)) for k in (0..11)))
160sage: (1 / s).n(digits=100); pi.n(digits=100)
1613.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342121432
1623.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068
163sage: print "%e" % (pi - 1 / s).n(digits=100)
164-4.364154e-96
165sage: var('n'); u = sin(pi*(sqrt(4*n^2+1)-2*n))
166n
167sage: taylor(u, n, infinity, 3)
1681/4*pi/n - 1/384*(6*pi + pi^3)/n^3
169sage: diff(sin(x^2), x)
1702*x*cos(x^2)
171sage: function('f',x); function('g',x); diff(f(g(x)), x)
172f(x)
173g(x)
174D[0](f)(g(x))*D[0](g)(x)
175sage: diff(ln(f(x)), x)
176D[0](f)(x)/f(x)
177sage: f(x,y) = x*y + sin(x^2) + e^(-x); derivative(f, x)
178(x, y) |--> 2*x*cos(x^2) + y - e^(-x)
179sage: derivative(f, y)
180(x, y) |--> x
181sage: var('x y'); f = ln(x**2+y**2) / 2
182(x, y)
183sage: delta = diff(f,x,2)+diff(f,y,2)
184sage: delta.simplify_full()
1850
186sage: sin(x).integral(x, 0, pi/2)
1871
188sage: integrate(1/(1+x^2), x)
189arctan(x)
190sage: integrate(1/(1+x^2), x, -infinity, infinity)
191pi
192sage: integrate(exp(-x**2), x, 0, infinity)
1931/2*sqrt(pi)
194sage: u = var('u'); f = x * cos(u) / (u^2 + x^2)
195sage: assume(x>0); f.integrate(u, 0, infinity)
1961/2*pi*e^(-x)
197sage: forget(); assume(x<0); f.integrate(u, 0, infinity)
198-1/2*pi*e^x
199sage: integral_numerical(sin(x)/x, 0, 1)
200(0.946083070367182..., 1.050363207929708...e-14)
201sage: g = integrate(exp(-x**2), x, 0, infinity); g, g.n()
202(1/2*sqrt(pi), 0.886226925452...)
203sage: approx = integral_numerical(exp(-x**2), 0, infinity)
204sage: approx[0]
2050.88622692545275...
206sage: A = matrix(QQ,[[1,2],[3,4]]); A
207[1 2]
208[3 4]
209sage: A = matrix(QQ, [[2, 4, 3],[-4,-6,-3],[3,3,1]])
210sage: A.characteristic_polynomial()
211x^3 + 3*x^2 - 4
212sage: A.eigenvalues()
213[1, -2, -2]
214sage: A.minimal_polynomial().factor()
215(x - 1) * (x + 2)^2
216sage: A.eigenvectors_right()
217[(1, [
218(1, -1, 1)
219], 1), (-2, [
220(1, -1, 0)
221], 2)]
222sage: A.jordan_form(transformation=True)
223(
224[ 1| 0  0]
225[--+-----]  [ 1  1  1]
226[ 0|-2  1]  [-1 -1  0]
227[ 0| 0 -2], [ 1  0 -1]
228)
229sage: A = matrix(QQ, [[1,-1/2],[-1/2,-1]])
230sage: A.minimal_polynomial()
231x^2 - 5/4
232sage: R = QQ[sqrt(5)]
233sage: A = A.change_ring(R)
234sage: A.jordan_form(transformation=True, subdivide=False)
235(
236[ 1/2*sqrt5          0]  [         1          1]
237[         0 -1/2*sqrt5], [-sqrt5 + 2  sqrt5 + 2]
238)
239sage: K.<sqrt2> = NumberField(x^2 - 2)
240sage: L.<sqrt3> = K.extension(x^2 - 3)
241sage: A = matrix(L, [[2, sqrt2*sqrt3, sqrt2], [sqrt2*sqrt3, 3, sqrt3], [sqrt2, sqrt3, 1]])
242sage: A.jordan_form(transformation=True)
243(
244[6|0|0]
245[-+-+-]
246[0|0|0]  [              1               1               0]
247[-+-+-]  [1/2*sqrt2*sqrt3               0               1]
248[0|0|0], [      1/2*sqrt2          -sqrt2          -sqrt3]
249)
250
251"""