1 | r""" |
---|
2 | sage: var ('a,x') ; y = cos(x+a)*(x+1) ; y |
---|
3 | (a, x) |
---|
4 | (x + 1)*cos(a + x) |
---|
5 | sage: var ('a,x') ; y = cos(x+a)*(x+1) ; y |
---|
6 | (a, x) |
---|
7 | (x + 1)*cos(a + x) |
---|
8 | sage: y.subs(a=-x) ; y.subs(x=pi/2,a=pi/3) ; y.subs(x=0.5,a=2.3) |
---|
9 | x + 1 |
---|
10 | -1/4*(pi + 2)*sqrt(3) |
---|
11 | -1.41333351100299 |
---|
12 | sage: y(a=-x) ; y(x=pi/2,a=pi/3) ; y(x=0.5,a=2.3) |
---|
13 | x + 1 |
---|
14 | -1/4*(pi + 2)*sqrt(3) |
---|
15 | -1.41333351100299 |
---|
16 | sage: var('x,y,z') ; q = x*y+y*z+z*x |
---|
17 | (x, y, z) |
---|
18 | sage: bool (q(x=y,y=z,z=x)==q), bool(q(z=y)(y=x) == 3*x^2) |
---|
19 | (True, True) |
---|
20 | """ |
---|
21 | |
---|
22 | r""" |
---|
23 | sage: var('y z'); f = x^3+y^2+z; f.subs_expr(x^3==y^2, z==1) |
---|
24 | (y, z) |
---|
25 | 2*y^2 + 1 |
---|
26 | sage: f(x)=(2*x+1)^3 ; f(-3) |
---|
27 | -125 |
---|
28 | sage: f(x).expand() |
---|
29 | 8*x^3 + 12*x^2 + 6*x + 1 |
---|
30 | sage: var('y'); u = sin(x) + x*cos(y) |
---|
31 | y |
---|
32 | sage: v = u.function(x, y); v |
---|
33 | (x, y) |--> x*cos(y) + sin(x) |
---|
34 | sage: w(x, y) = u; w |
---|
35 | (x, y) |--> x*cos(y) + sin(x) |
---|
36 | """ |
---|
37 | |
---|
38 | r""" |
---|
39 | sage: (x^x/x).simplify() |
---|
40 | x^(x - 1) |
---|
41 | sage: f = (e^x-1)/(1+e^(x/2)); f.simplify_exp() |
---|
42 | e^(1/2*x) - 1 |
---|
43 | sage: f = cos(x)^6 + sin(x)^6 + 3 * sin(x)^2 * cos(x)^2 |
---|
44 | sage: f.simplify_trig() |
---|
45 | 1 |
---|
46 | sage: f = cos(x)^6; f.reduce_trig() |
---|
47 | 15/32*cos(2*x) + 3/16*cos(4*x) + 1/32*cos(6*x) + 5/16 |
---|
48 | sage: f = sin(5 * x); f.expand_trig() |
---|
49 | sin(x)^5 - 10*sin(x)^3*cos(x)^2 + 5*sin(x)*cos(x)^4 |
---|
50 | sage: n = var('n'); f = factorial(n+1)/factorial(n) |
---|
51 | sage: f.simplify_factorial() |
---|
52 | n + 1 |
---|
53 | """ |
---|
54 | |
---|
55 | r""" |
---|
56 | sage: reset() |
---|
57 | sage: f = sqrt(x^2); f.simplify_radical() |
---|
58 | abs(x) |
---|
59 | sage: var('y') |
---|
60 | y |
---|
61 | sage: f = log(x*y); f.simplify_radical() |
---|
62 | log(x) + log(y) |
---|
63 | sage: assume(x > 0); bool(sqrt(x^2) == x) |
---|
64 | True |
---|
65 | sage: forget(x > 0); bool(sqrt(x^2) == x) |
---|
66 | False |
---|
67 | sage: var('n'); assume(n, 'integer'); sin(n*pi).simplify() |
---|
68 | n |
---|
69 | 0 |
---|
70 | """ |
---|
71 | |
---|
72 | r""" |
---|
73 | sage: z, phi = var('z, phi') |
---|
74 | sage: solve(z**2 -2 / cos(phi) * z + 5 / cos(phi) ** 2 - 4, z) |
---|
75 | [z == -(2*sqrt(cos(phi)^2 - 1) - 1)/cos(phi), z == (2*sqrt(cos(phi)^2 - 1) + 1)/cos(phi)] |
---|
76 | sage: var('y'); solve(y^6==y, y) |
---|
77 | y |
---|
78 | [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] |
---|
79 | sage: solve(x^2-1, x, solution_dict=True) |
---|
80 | [{x: -1}, {x: 1}] |
---|
81 | sage: solve([x+y == 3, 2*x+2*y == 6],x,y) |
---|
82 | [[x == -r1 + 3, y == r1]] |
---|
83 | sage: solve([cos(x)*sin(x) == 1/2, x+y == 0], x, y) |
---|
84 | [[x == 1/4*pi + pi*z30, y == -1/4*pi - pi*z30]] |
---|
85 | sage: solve(x^2+x-1>0,x) |
---|
86 | [[x < -1/2*sqrt(5) - 1/2], [x > 1/2*sqrt(5) - 1/2]] |
---|
87 | """ |
---|
88 | |
---|
89 | r""" |
---|
90 | sage: x, y, z = var('x, y, z') |
---|
91 | sage: solve([x^2 * y * z == 18, x * y^3 * z == 24,\ |
---|
92 | x * y * z^4 == 6], x, y, z) |
---|
93 | [[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)]] |
---|
94 | """ |
---|
95 | |
---|
96 | r""" |
---|
97 | sage: expr = sin(x) + sin(2 * x) + sin(3 * x) |
---|
98 | sage: solve(expr, x) |
---|
99 | [sin(3*x) == -sin(2*x) - sin(x)] |
---|
100 | sage: find_root(expr, 0.1, pi) |
---|
101 | 2.0943951023931957 |
---|
102 | sage: f = expr.simplify_trig(); f |
---|
103 | 2*(2*cos(x)^2 + cos(x))*sin(x) |
---|
104 | sage: solve(f, x) |
---|
105 | [x == 0, x == 2/3*pi, x == 1/2*pi] |
---|
106 | sage: (x^3+2*x+1).roots(x) |
---|
107 | [(-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)] |
---|
108 | sage: (x^3+2*x+1).roots(x, ring=RR) |
---|
109 | [(-0.453397651516404, 1)] |
---|
110 | sage: (x^3+2*x+1).roots(x, ring=CC) |
---|
111 | [(-0.453397651516404, 1), (0.226698825758202 - 1.46771150871022*I, 1), (0.226698825758202 + 1.46771150871022*I, 1)] |
---|
112 | """ |
---|
113 | |
---|
114 | r""" |
---|
115 | sage: var('k n'); sum(k, k, 1, n).factor() |
---|
116 | (k, n) |
---|
117 | 1/2*(n + 1)*n |
---|
118 | sage: var('n y'); sum(binomial(n,k) * x^k * y^(n-k), k, 0, n) |
---|
119 | (n, y) |
---|
120 | (x + y)^n |
---|
121 | sage: var('k n'); sum(binomial(n,k), k, 0, n),\ |
---|
122 | sum(k * binomial(n, k), k, 0, n),\ |
---|
123 | sum((-1)^k*binomial(n,k), k, 0, n) |
---|
124 | (k, n) |
---|
125 | (2^n, n*2^(n - 1), 0) |
---|
126 | sage: var('a q'); sum(a*q^k, k, 0, n) |
---|
127 | (a, q) |
---|
128 | (a*q^(n + 1) - a)/(q - 1) |
---|
129 | sage: assume(abs(q) < 1) |
---|
130 | sage: sum(a*q^k, k, 0, infinity) |
---|
131 | -a/(q - 1) |
---|
132 | """ |
---|
133 | |
---|
134 | r""" |
---|
135 | sage: limit((x**(1/3) -2) / ((x + 19)**(1/3) - 3), x = 8) |
---|
136 | 9/4 |
---|
137 | sage: f(x) = (cos(pi/4-x)-tan(x))/(1-sin(pi/4 + x)) |
---|
138 | sage: limit(f(x), x = pi/4, dir='minus') |
---|
139 | +Infinity |
---|
140 | sage: limit(f(x), x = pi/4, dir='plus') |
---|
141 | -Infinity |
---|
142 | sage: u(n) = n^100 / 100.^n |
---|
143 | sage: u(1);u(2);u(3);u(4);u(5);u(6);u(7);u(8);u(9);u(10) |
---|
144 | 0.0100000000000000 |
---|
145 | 1.26765060022823e26 |
---|
146 | 5.15377520732011e41 |
---|
147 | 1.60693804425899e52 |
---|
148 | 7.88860905221012e59 |
---|
149 | 6.53318623500071e65 |
---|
150 | 3.23447650962476e70 |
---|
151 | 2.03703597633449e74 |
---|
152 | 2.65613988875875e77 |
---|
153 | 1.00000000000000e80 |
---|
154 | sage: plot(u(x), x, 1, 40) |
---|
155 | sage: v(x) = diff(u(x), x); sol = solve(v(x) == 0, x); sol |
---|
156 | [x == 0, x == (268850/12381)] |
---|
157 | sage: sol[1].rhs().n().floor() |
---|
158 | 21 |
---|
159 | sage: limit(u(n), n=infinity) |
---|
160 | 0 |
---|
161 | sage: n0 = find_root(u(n) - 1e-8 == 0, 22, 1000); n0 |
---|
162 | 105.07496210187252 |
---|
163 | """ |
---|
164 | |
---|
165 | r""" |
---|
166 | sage: taylor((1+arctan(x))**(1/x), x, 0, 3) |
---|
167 | 1/16*x^3*e + 1/8*x^2*e - 1/2*x*e + e |
---|
168 | sage: (ln(2* sin(x))).series(x==pi/6, 3) |
---|
169 | (sqrt(3))*(-1/6*pi + x) + (-2)*(-1/6*pi + x)^2 + Order(-1/216*(pi - 6*x)^3) |
---|
170 | sage: (ln(2* sin(x))).series(x==pi/6, 3).truncate() |
---|
171 | -1/18*(pi - 6*x)^2 - 1/6*(pi - 6*x)*sqrt(3) |
---|
172 | sage: taylor((x**3+x)**(1/3) - (x**3-x)**(1/3), x, infinity, 2) |
---|
173 | 2/3/x |
---|
174 | sage: tan(4*arctan(1/5)).simplify_trig() |
---|
175 | 120/119 |
---|
176 | sage: tan(pi/4+arctan(1/239)).simplify_trig() |
---|
177 | 120/119 |
---|
178 | sage: f = arctan(x).series(x, 10); f |
---|
179 | 1*x + (-1/3)*x^3 + 1/5*x^5 + (-1/7)*x^7 + 1/9*x^9 + Order(x^10) |
---|
180 | sage: (16*f.subs(x==1/5) - 4*f.subs(x==1/239)).n(); pi.n() |
---|
181 | 3.14159268240440 |
---|
182 | 3.14159265358979 |
---|
183 | """ |
---|
184 | |
---|
185 | r""" |
---|
186 | sage: var('k'); sum(1/k^2, k, 1, infinity),\ |
---|
187 | sum(1/k^4, k, 1, infinity),\ |
---|
188 | sum(1/k^5, k, 1, infinity) |
---|
189 | k |
---|
190 | (1/6*pi^2, 1/90*pi^4, zeta(5)) |
---|
191 | sage: s = 2*sqrt(2)/9801*(sum((factorial(4*k))*(1103+26390*k)\ |
---|
192 | /((factorial(k)) ^ 4 * 396 ^ (4 * k)) for k in (0..11))) |
---|
193 | sage: (1 / s).n(digits=100); pi.n(digits=100) |
---|
194 | 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342121432 |
---|
195 | 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068 |
---|
196 | sage: print "%e" % (pi - 1 / s).n(digits=100) |
---|
197 | -4.364154e-96 |
---|
198 | sage: var('n'); u = sin(pi*(sqrt(4*n^2+1)-2*n)) |
---|
199 | n |
---|
200 | sage: taylor(u, n, infinity, 3) |
---|
201 | 1/4*pi/n - 1/384*(6*pi + pi^3)/n^3 |
---|
202 | sage: diff(sin(x^2), x) |
---|
203 | 2*x*cos(x^2) |
---|
204 | """ |
---|
205 | |
---|
206 | r""" |
---|
207 | sage: function('f',x); function('g',x); diff(f(g(x)), x) |
---|
208 | f(x) |
---|
209 | g(x) |
---|
210 | D[0](f)(g(x))*D[0](g)(x) |
---|
211 | sage: diff(ln(f(x)), x) |
---|
212 | D[0](f)(x)/f(x) |
---|
213 | sage: f(x,y) = x*y + sin(x^2) + e^(-x); derivative(f, x) |
---|
214 | (x, y) |--> 2*x*cos(x^2) + y - e^(-x) |
---|
215 | sage: derivative(f, y) |
---|
216 | (x, y) |--> x |
---|
217 | sage: var('x y'); f = ln(x**2+y**2) / 2 |
---|
218 | (x, y) |
---|
219 | sage: delta = diff(f,x,2)+diff(f,y,2) |
---|
220 | sage: delta.simplify_full() |
---|
221 | 0 |
---|
222 | sage: sin(x).integral(x, 0, pi/2) |
---|
223 | 1 |
---|
224 | sage: integrate(1/(1+x^2), x) |
---|
225 | arctan(x) |
---|
226 | sage: integrate(1/(1+x^2), x, -infinity, infinity) |
---|
227 | pi |
---|
228 | sage: integrate(exp(-x**2), x, 0, infinity) |
---|
229 | 1/2*sqrt(pi) |
---|
230 | sage: u = var('u'); f = x * cos(u) / (u^2 + x^2) |
---|
231 | sage: assume(x>0); f.integrate(u, 0, infinity) |
---|
232 | 1/2*pi*e^(-x) |
---|
233 | sage: forget(); assume(x<0); f.integrate(u, 0, infinity) |
---|
234 | -1/2*pi*e^x |
---|
235 | sage: integral_numerical(sin(x)/x, 0, 1) |
---|
236 | (0.946083070367182..., 1.050363207929708...e-14) |
---|
237 | sage: g = integrate(exp(-x**2), x, 0, infinity); g, g.n() |
---|
238 | (1/2*sqrt(pi), 0.886226925452...) |
---|
239 | sage: approx = integral_numerical(exp(-x**2), 0, infinity) |
---|
240 | sage: approx[0] |
---|
241 | 0.88622692545275... |
---|
242 | """ |
---|
243 | |
---|
244 | r""" |
---|
245 | sage: A = matrix(QQ,[[1,2],[3,4]]); A |
---|
246 | [1 2] |
---|
247 | [3 4] |
---|
248 | sage: A = matrix(QQ, [[2, 4, 3],[-4,-6,-3],[3,3,1]]) |
---|
249 | sage: A.characteristic_polynomial() |
---|
250 | x^3 + 3*x^2 - 4 |
---|
251 | sage: A.eigenvalues() |
---|
252 | [1, -2, -2] |
---|
253 | sage: A.minimal_polynomial().factor() |
---|
254 | (x - 1) * (x + 2)^2 |
---|
255 | sage: A.eigenvectors_right() |
---|
256 | [(1, [ |
---|
257 | (1, -1, 1) |
---|
258 | ], 1), (-2, [ |
---|
259 | (1, -1, 0) |
---|
260 | ], 2)] |
---|
261 | sage: A.jordan_form(transformation=True) |
---|
262 | ( |
---|
263 | [ 1| 0 0] |
---|
264 | [--+-----] [ 1 1 1] |
---|
265 | [ 0|-2 1] [-1 -1 0] |
---|
266 | [ 0| 0 -2], [ 1 0 -1] |
---|
267 | ) |
---|
268 | """ |
---|
269 | |
---|
270 | r""" |
---|
271 | sage: A = matrix(QQ, [[1,-1/2],[-1/2,-1]]) |
---|
272 | sage: A.minimal_polynomial() |
---|
273 | x^2 - 5/4 |
---|
274 | sage: R = QQ[sqrt(5)] |
---|
275 | sage: A = A.change_ring(R) |
---|
276 | sage: A.jordan_form(transformation=True, subdivide=False) |
---|
277 | ( |
---|
278 | [ 1/2*sqrt5 0] [ 1 1] |
---|
279 | [ 0 -1/2*sqrt5], [-sqrt5 + 2 sqrt5 + 2] |
---|
280 | ) |
---|
281 | sage: K.<sqrt2> = NumberField(x^2 - 2) |
---|
282 | sage: L.<sqrt3> = K.extension(x^2 - 3) |
---|
283 | sage: A = matrix(L, [[2, sqrt2*sqrt3, sqrt2], [sqrt2*sqrt3, 3, sqrt3], [sqrt2, sqrt3, 1]]) |
---|
284 | sage: A.jordan_form(transformation=True) |
---|
285 | ( |
---|
286 | [6|0|0] |
---|
287 | [-+-+-] |
---|
288 | [0|0|0] [ 1 1 0] |
---|
289 | [-+-+-] [1/2*sqrt2*sqrt3 0 1] |
---|
290 | [0|0|0], [ 1/2*sqrt2 -sqrt2 -sqrt3] |
---|
291 | ) |
---|
292 | """ |
---|