Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/calculus/wester.py
4057 views
1
r"""
2
Further examples from Wester's paper
3
4
These are all the problems at
5
http://yacas.sourceforge.net/essaysmanual.html
6
7
They come from the 1994 paper "Review of CAS mathematical
8
capabilities", by Michael Wester, who put forward 123 problems that
9
a reasonable computer algebra system should be able to solve and
10
tested the then current versions of various commercial CAS on this
11
list. Sage can do most of the problems natively now, i.e., with no
12
explicit calls to Maxima or other systems.
13
14
::
15
16
sage: # (YES) factorial of 50, and factor it
17
sage: factorial(50)
18
30414093201713378043612608166064768844377641568960512000000000000
19
sage: factor(factorial(50))
20
2^47 * 3^22 * 5^12 * 7^8 * 11^4 * 13^3 * 17^2 * 19^2 * 23^2 * 29 * 31 * 37 * 41 * 43 * 47
21
22
::
23
24
sage: # (YES) 1/2+...+1/10 = 4861/2520
25
sage: sum(1/n for n in range(2,10+1)) == 4861/2520
26
True
27
28
::
29
30
sage: # (YES) Evaluate e^(Pi*Sqrt(163)) to 50 decimal digits
31
sage: a = e^(pi*sqrt(163)); a
32
e^(pi*sqrt(163))
33
sage: print RealField(150)(a)
34
2.6253741264076874399999999999925007259719820e17
35
36
::
37
38
sage: # (YES) Evaluate the Bessel function J[2] numerically at z=1+I.
39
sage: bessel_J (2, 1+I)
40
0.0415798869439621 + 0.247397641513306*I
41
42
::
43
44
sage: # (YES) Obtain period of decimal fraction 1/7=0.(142857).
45
sage: a = 1/7
46
sage: print a
47
1/7
48
sage: print a.period()
49
6
50
51
::
52
53
sage: # (YES) Continued fraction of 3.1415926535
54
sage: a = 3.1415926535
55
sage: continued_fraction(a)
56
[3, 7, 15, 1, 292, 1, 1, 6, 2, 13, 4]
57
58
::
59
60
sage: # (YES) Sqrt(2*Sqrt(3)+4)=1+Sqrt(3).
61
sage: # The Maxima backend equality checker does this;
62
sage: # note the equality only holds for one choice of sign,
63
sage: # but Maxima always chooses the "positive" one
64
sage: a = sqrt(2*sqrt(3) + 4); b = 1 + sqrt(3)
65
sage: print float(a-b)
66
0.0
67
sage: print bool(a == b)
68
True
69
sage: # We can, of course, do this in a quadratic field
70
sage: k.<sqrt3> = QuadraticField(3)
71
sage: asqr = 2*sqrt3 + 4
72
sage: b = 1+sqrt3
73
sage: asqr == b^2
74
True
75
76
::
77
78
sage: # (NOT REALLY) Sqrt(14+3*Sqrt(3+2*Sqrt(5-12*Sqrt(3-2*Sqrt(2)))))=3+Sqrt(2).
79
sage: a = sqrt(14+3*sqrt(3+2*sqrt(5-12*sqrt(3-2*sqrt(2)))))
80
sage: b = 3+sqrt(2)
81
sage: a, b
82
(sqrt(3*sqrt(2*sqrt(-12*sqrt(-2*sqrt(2) + 3) + 5) + 3) + 14), sqrt(2) + 3)
83
sage: bool(a==b)
84
False
85
sage: abs(float(a-b)) < 1e-10
86
True
87
sage: # 2*Infinity-3=Infinity.
88
sage: 2*infinity-3 == infinity
89
True
90
91
::
92
93
sage: # (YES) Standard deviation of the sample (1, 2, 3, 4, 5).
94
sage: v = vector(RDF, 5, [1,2,3,4,5])
95
sage: v.standard_deviation()
96
1.58113883008
97
98
::
99
100
sage: # (NO) Hypothesis testing with t-distribution.
101
sage: # (NO) Hypothesis testing with chi^2 distribution
102
sage: # (But both are included in Scipy and R)
103
104
::
105
106
sage: # (YES) (x^2-4)/(x^2+4*x+4)=(x-2)/(x+2).
107
sage: R.<x> = QQ[]
108
sage: (x^2-4)/(x^2+4*x+4) == (x-2)/(x+2)
109
True
110
sage: restore('x')
111
112
::
113
114
sage: # (YES -- Maxima doesn't immediately consider them
115
sage: # equal, but simplification shows that they are)
116
sage: # (Exp(x)-1)/(Exp(x/2)+1)=Exp(x/2)-1.
117
sage: f = (exp(x)-1)/(exp(x/2)+1)
118
sage: g = exp(x/2)-1
119
sage: f
120
(e^x - 1)/(e^(1/2*x) + 1)
121
sage: g
122
e^(1/2*x) - 1
123
sage: f.simplify_full(),g
124
(e^(1/2*x) - 1, e^(1/2*x) - 1)
125
sage: f(x=10.0).n(53), g(x=10.0).n(53)
126
(147.413159102577, 147.413159102577)
127
sage: bool(f == g)
128
True
129
130
::
131
132
sage: # (YES) Expand (1+x)^20, take derivative and factorize.
133
sage: # first do it using algebraic polys
134
sage: R.<x> = QQ[]
135
sage: f = (1+x)^20; f
136
x^20 + 20*x^19 + 190*x^18 + 1140*x^17 + 4845*x^16 + 15504*x^15 + 38760*x^14 + 77520*x^13 + 125970*x^12 + 167960*x^11 + 184756*x^10 + 167960*x^9 + 125970*x^8 + 77520*x^7 + 38760*x^6 + 15504*x^5 + 4845*x^4 + 1140*x^3 + 190*x^2 + 20*x + 1
137
sage: deriv = f.derivative()
138
sage: deriv
139
20*x^19 + 380*x^18 + 3420*x^17 + 19380*x^16 + 77520*x^15 + 232560*x^14 + 542640*x^13 + 1007760*x^12 + 1511640*x^11 + 1847560*x^10 + 1847560*x^9 + 1511640*x^8 + 1007760*x^7 + 542640*x^6 + 232560*x^5 + 77520*x^4 + 19380*x^3 + 3420*x^2 + 380*x + 20
140
sage: deriv.factor()
141
(20) * (x + 1)^19
142
sage: restore('x')
143
sage: # next do it symbolically
144
sage: var('y')
145
y
146
sage: f = (1+y)^20; f
147
(y + 1)^20
148
sage: g = f.expand(); g
149
y^20 + 20*y^19 + 190*y^18 + 1140*y^17 + 4845*y^16 + 15504*y^15 + 38760*y^14 + 77520*y^13 + 125970*y^12 + 167960*y^11 + 184756*y^10 + 167960*y^9 + 125970*y^8 + 77520*y^7 + 38760*y^6 + 15504*y^5 + 4845*y^4 + 1140*y^3 + 190*y^2 + 20*y + 1
150
sage: deriv = g.derivative(); deriv
151
20*y^19 + 380*y^18 + 3420*y^17 + 19380*y^16 + 77520*y^15 + 232560*y^14 + 542640*y^13 + 1007760*y^12 + 1511640*y^11 + 1847560*y^10 + 1847560*y^9 + 1511640*y^8 + 1007760*y^7 + 542640*y^6 + 232560*y^5 + 77520*y^4 + 19380*y^3 + 3420*y^2 + 380*y + 20
152
sage: deriv.factor()
153
20*(y + 1)^19
154
155
::
156
157
sage: # (YES) Factorize x^100-1.
158
sage: factor(x^100-1)
159
(x - 1)*(x + 1)*(x^2 + 1)*(x^4 - x^3 + x^2 - x + 1)*(x^4 + x^3 + x^2 + x + 1)*(x^8 - x^6 + x^4 - x^2 + 1)*(x^20 - x^15 + x^10 - x^5 + 1)*(x^20 + x^15 + x^10 + x^5 + 1)*(x^40 - x^30 + x^20 - x^10 + 1)
160
sage: # Also, algebraically
161
sage: x = polygen(QQ)
162
sage: factor(x^100 - 1)
163
(x - 1) * (x + 1) * (x^2 + 1) * (x^4 - x^3 + x^2 - x + 1) * (x^4 + x^3 + x^2 + x + 1) * (x^8 - x^6 + x^4 - x^2 + 1) * (x^20 - x^15 + x^10 - x^5 + 1) * (x^20 + x^15 + x^10 + x^5 + 1) * (x^40 - x^30 + x^20 - x^10 + 1)
164
sage: restore('x')
165
166
::
167
168
sage: # (YES) Factorize x^4-3*x^2+1 in the field of rational numbers extended by roots of x^2-x-1.
169
sage: k.< a> = NumberField(x^2 - x -1)
170
sage: R.< y> = k[]
171
sage: f = y^4 - 3*y^2 + 1
172
sage: f
173
y^4 - 3*y^2 + 1
174
sage: factor(f)
175
(y - a) * (y - a + 1) * (y + a - 1) * (y + a)
176
177
::
178
179
sage: # (YES) Factorize x^4-3*x^2+1 mod 5.
180
sage: k.< x > = GF(5) [ ]
181
sage: f = x^4 - 3*x^2 + 1
182
sage: f.factor()
183
(x + 2)^2 * (x + 3)^2
184
sage: # Alternatively, from symbol x as follows:
185
sage: reset('x')
186
sage: f = x^4 - 3*x^2 + 1
187
sage: f.polynomial(GF(5)).factor()
188
(x + 2)^2 * (x + 3)^2
189
190
::
191
192
sage: # (YES) Partial fraction decomposition of (x^2+2*x+3)/(x^3+4*x^2+5*x+2)
193
sage: f = (x^2+2*x+3)/(x^3+4*x^2+5*x+2); f
194
(x^2 + 2*x + 3)/(x^3 + 4*x^2 + 5*x + 2)
195
sage: f.partial_fraction()
196
-2/(x + 1) + 2/(x + 1)^2 + 3/(x + 2)
197
198
::
199
200
sage: # (YES) Assuming x>=y, y>=z, z>=x, deduce x=z.
201
sage: forget()
202
sage: var('x,y,z')
203
(x, y, z)
204
sage: assume(x>=y, y>=z,z>=x)
205
sage: print bool(x==z)
206
True
207
208
::
209
210
sage: # (YES) Assuming x>y, y>0, deduce 2*x^2>2*y^2.
211
sage: forget()
212
sage: assume(x>y, y>0)
213
sage: print list(sorted(assumptions()))
214
[x > y, y > 0]
215
sage: print bool(2*x^2 > 2*y^2)
216
True
217
sage: forget()
218
sage: print assumptions()
219
[]
220
221
::
222
223
sage: # (NO) Solve the inequality Abs(x-1)>2.
224
sage: # Maxima doesn't solve inequalities
225
sage: # (but some Maxima packages do):
226
sage: eqn = abs(x-1) > 2
227
sage: print eqn
228
abs(x - 1) > 2
229
230
::
231
232
sage: # (NO) Solve the inequality (x-1)*...*(x-5)<0.
233
sage: eqn = prod(x-i for i in range(1,5 +1)) < 0
234
sage: # but don't know how to solve
235
sage: eqn
236
(x - 5)*(x - 4)*(x - 3)*(x - 2)*(x - 1) < 0
237
238
::
239
240
sage: # (YES) Cos(3*x)/Cos(x)=Cos(x)^2-3*Sin(x)^2 or similar equivalent combination.
241
sage: f = cos(3*x)/cos(x)
242
sage: g = cos(x)^2 - 3*sin(x)^2
243
sage: h = f-g
244
sage: print h.trig_simplify()
245
0
246
247
::
248
249
sage: # (YES) Cos(3*x)/Cos(x)=2*Cos(2*x)-1.
250
sage: f = cos(3*x)/cos(x)
251
sage: g = 2*cos(2*x) - 1
252
sage: h = f-g
253
sage: print h.trig_simplify()
254
0
255
256
::
257
258
sage: # (GOOD ENOUGH) Define rewrite rules to match Cos(3*x)/Cos(x)=Cos(x)^2-3*Sin(x)^2.
259
sage: # Sage has no notion of "rewrite rules", but
260
sage: # it can simplify both to the same thing.
261
sage: (cos(3*x)/cos(x)).simplify_full()
262
4*cos(x)^2 - 3
263
sage: (cos(x)^2-3*sin(x)^2).simplify_full()
264
4*cos(x)^2 - 3
265
266
::
267
268
sage: # (YES) Sqrt(997)-(997^3)^(1/6)=0
269
sage: a = sqrt(997) - (997^3)^(1/6)
270
sage: a.simplify()
271
0
272
sage: bool(a == 0)
273
True
274
275
::
276
277
sage: # (YES) Sqrt(99983)-99983^3^(1/6)=0
278
sage: a = sqrt(99983) - (99983^3)^(1/6)
279
sage: bool(a==0)
280
True
281
sage: float(a)
282
1.1368683772...e-13
283
sage: print 13*7691
284
99983
285
286
::
287
288
sage: # (YES) (2^(1/3) + 4^(1/3))^3 - 6*(2^(1/3) + 4^(1/3))-6 = 0
289
sage: a = (2^(1/3) + 4^(1/3))^3 - 6*(2^(1/3) + 4^(1/3)) - 6; a
290
(2^(1/3) + 4^(1/3))^3 - 6*2^(1/3) - 6*4^(1/3) - 6
291
sage: bool(a==0)
292
True
293
sage: abs(float(a)) < 1e-10
294
True
295
sage: ## or we can do it using number fields.
296
sage: reset('x')
297
sage: k.<b> = NumberField(x^3-2)
298
sage: a = (b + b^2)^3 - 6*(b + b^2) - 6
299
sage: print a
300
0
301
302
::
303
304
sage: # (NO, except numerically) Ln(Tan(x/2+Pi/4))-ArcSinh(Tan(x))=0
305
# Sage uses the Maxima convention when comparing symbolic expressions and
306
# returns True only when it can prove equality. Thus, in this case, we get
307
# False even though the equality holds.
308
sage: f = log(tan(x/2 + pi/4)) - arcsinh(tan(x))
309
sage: bool(f == 0)
310
False
311
sage: [abs(float(f(x=i/10))) < 1e-15 for i in range(1,5)]
312
[True, True, True, True]
313
sage: # Numerically, the expression Ln(Tan(x/2+Pi/4))-ArcSinh(Tan(x))=0 and its derivative at x=0 are zero.
314
sage: g = f.derivative()
315
sage: abs(float(f(x=0))) < 1e-10
316
True
317
sage: abs(float(g(x=0))) < 1e-10
318
True
319
sage: g
320
-sqrt(tan(x)^2 + 1) + 1/2*(tan(1/4*pi + 1/2*x)^2 + 1)/tan(1/4*pi + 1/2*x)
321
322
::
323
324
sage: # (NO) Ln((2*Sqrt(r) + 1)/Sqrt(4*r 4*Sqrt(r) 1))=0.
325
sage: var('r')
326
r
327
sage: f = log( (2*sqrt(r) + 1) / sqrt(4*r + 4*sqrt(r) + 1))
328
sage: f
329
log((2*sqrt(r) + 1)/sqrt(4*r + 4*sqrt(r) + 1))
330
sage: bool(f == 0)
331
False
332
sage: [abs(float(f(r=i))) < 1e-10 for i in [0.1,0.3,0.5]]
333
[True, True, True]
334
335
::
336
337
sage: # (NO)
338
sage: # (4*r+4*Sqrt(r)+1)^(Sqrt(r)/(2*Sqrt(r)+1))*(2*Sqrt(r)+1)^(2*Sqrt(r)+1)^(-1)-2*Sqrt(r)-1=0, assuming r>0.
339
sage: assume(r>0)
340
sage: f = (4*r+4*sqrt(r)+1)^(sqrt(r)/(2*sqrt(r)+1))*(2*sqrt(r)+1)^(2*sqrt(r)+1)^(-1)-2*sqrt(r)-1
341
sage: f
342
(2*sqrt(r) + 1)^(1/(2*sqrt(r) + 1))*(4*r + 4*sqrt(r) + 1)^(sqrt(r)/(2*sqrt(r) + 1)) - 2*sqrt(r) - 1
343
sage: bool(f == 0)
344
False
345
sage: [abs(float(f(r=i))) < 1e-10 for i in [0.1,0.3,0.5]]
346
[True, True, True]
347
348
::
349
350
sage: # (YES) Obtain real and imaginary parts of Ln(3+4*I).
351
sage: a = log(3+4*I); a
352
log(4*I + 3)
353
sage: a.real()
354
log(5)
355
sage: a.imag()
356
arctan(4/3)
357
358
::
359
360
sage: # (YES) Obtain real and imaginary parts of Tan(x+I*y)
361
sage: z = var('z')
362
sage: a = tan(z); a
363
tan(z)
364
sage: a.real()
365
tan(real_part(z))/(tan(real_part(z))^2*tan(imag_part(z))^2 + 1)
366
sage: a.imag()
367
tanh(imag_part(z))/(tan(real_part(z))^2*tan(imag_part(z))^2 + 1)
368
369
370
::
371
372
sage: # (YES) Simplify Ln(Exp(z)) to z for -Pi<Im(z)<=Pi.
373
sage: # Unfortunately (?), Maxima does this even without
374
sage: # any assumptions.
375
sage: # We *would* use assume(-pi < imag(z))
376
sage: # and assume(imag(z) <= pi)
377
sage: f = log(exp(z)); f
378
log(e^z)
379
sage: f.simplify()
380
z
381
sage: forget()
382
383
::
384
385
sage: # (YES) Assuming Re(x)>0, Re(y)>0, deduce x^(1/n)*y^(1/n)-(x*y)^(1/n)=0.
386
sage: # Maxima 5.26 has different behaviours depending on the current
387
sage: # domain.
388
sage: # To stick with the behaviour of previous versions, the domain is set
389
sage: # to 'real' in the following.
390
sage: # See Trac #10682 for further details.
391
sage: n = var('n')
392
sage: f = x^(1/n)*y^(1/n)-(x*y)^(1/n)
393
sage: assume(real(x) > 0, real(y) > 0)
394
sage: f.simplify()
395
x^(1/n)*y^(1/n) - (x*y)^(1/n)
396
sage: maxima = sage.calculus.calculus.maxima
397
sage: maxima.set('domain', 'real') # set domain to real
398
sage: f.simplify()
399
0
400
sage: maxima.set('domain', 'complex') # set domain back to its default value
401
sage: forget()
402
403
::
404
405
sage: # (YES) Transform equations, (x==2)/2+(1==1)=>x/2+1==2.
406
sage: eq1 = x == 2
407
sage: eq2 = SR(1) == SR(1)
408
sage: eq1/2 + eq2
409
1/2*x + 1 == 2
410
411
::
412
413
sage: # (SOMEWHAT) Solve Exp(x)=1 and get all solutions.
414
sage: # to_poly_solve in Maxima can do this.
415
sage: solve(exp(x) == 1, x)
416
[x == 0]
417
418
::
419
420
sage: # (SOMEWHAT) Solve Tan(x)=1 and get all solutions.
421
sage: # to_poly_solve in Maxima can do this.
422
sage: solve(tan(x) == 1, x)
423
[x == 1/4*pi]
424
425
::
426
427
sage: # (YES) Solve a degenerate 3x3 linear system.
428
sage: # x+y+z==6,2*x+y+2*z==10,x+3*y+z==10
429
sage: # First symbolically:
430
sage: solve([x+y+z==6, 2*x+y+2*z==10, x+3*y+z==10], x,y,z)
431
[[x == -r1 + 4, y == 2, z == r1]]
432
433
::
434
435
sage: # (YES) Invert a 2x2 symbolic matrix.
436
sage: # [[a,b],[1,a*b]]
437
sage: # Using multivariate poly ring -- much nicer
438
sage: R.<a,b> = QQ[]
439
sage: m = matrix(2,2,[a,b, 1, a*b])
440
sage: zz = m^(-1)
441
sage: print zz
442
[ a/(a^2 - 1) (-1)/(a^2 - 1)]
443
[(-1)/(a^2*b - b) a/(a^2*b - b)]
444
445
::
446
447
sage: # (YES) Compute and factor the determinant of the 4x4 Vandermonde matrix in a, b, c, d.
448
sage: var('a,b,c,d')
449
(a, b, c, d)
450
sage: m = matrix(SR, 4, 4, [[z^i for i in range(4)] for z in [a,b,c,d]])
451
sage: print m
452
[ 1 a a^2 a^3]
453
[ 1 b b^2 b^3]
454
[ 1 c c^2 c^3]
455
[ 1 d d^2 d^3]
456
sage: d = m.determinant()
457
sage: d.factor()
458
(c - d)*(b - d)*(b - c)*(a - d)*(a - c)*(a - b)
459
460
::
461
462
sage: # (YES) Compute and factor the determinant of the 4x4 Vandermonde matrix in a, b, c, d.
463
sage: # Do it instead in a multivariate ring
464
sage: R.<a,b,c,d> = QQ[]
465
sage: m = matrix(R, 4, 4, [[z^i for i in range(4)] for z in [a,b,c,d]])
466
sage: print m
467
[ 1 a a^2 a^3]
468
[ 1 b b^2 b^3]
469
[ 1 c c^2 c^3]
470
[ 1 d d^2 d^3]
471
sage: d = m.determinant()
472
sage: print d
473
a^3*b^2*c - a^2*b^3*c - a^3*b*c^2 + a*b^3*c^2 + a^2*b*c^3 - a*b^2*c^3 - a^3*b^2*d + a^2*b^3*d + a^3*c^2*d - b^3*c^2*d - a^2*c^3*d + b^2*c^3*d + a^3*b*d^2 - a*b^3*d^2 - a^3*c*d^2 + b^3*c*d^2 + a*c^3*d^2 - b*c^3*d^2 - a^2*b*d^3 + a*b^2*d^3 + a^2*c*d^3 - b^2*c*d^3 - a*c^2*d^3 + b*c^2*d^3
474
sage: print d.factor()
475
(-1) * (c - d) * (b - d) * (b - c) * (-a + b) * (a - d) * (a - c)
476
477
::
478
479
sage: # (YES) Find the eigenvalues of a 3x3 integer matrix.
480
sage: m = matrix(QQ, 3, [5,-3,-7, -2,1,2, 2,-3,-4])
481
sage: m.eigenspaces_left()
482
[
483
(3, Vector space of degree 3 and dimension 1 over Rational Field
484
User basis matrix:
485
[ 1 0 -1]),
486
(1, Vector space of degree 3 and dimension 1 over Rational Field
487
User basis matrix:
488
[ 1 1 -1]),
489
(-2, Vector space of degree 3 and dimension 1 over Rational Field
490
User basis matrix:
491
[0 1 1])
492
]
493
494
::
495
496
sage: # (YES) Verify some standard limits found by L'Hopital's rule:
497
sage: # Verify(Limit(x,Infinity) (1+1/x)^x, Exp(1));
498
sage: # Verify(Limit(x,0) (1-Cos(x))/x^2, 1/2);
499
sage: limit( (1+1/x)^x, x = oo)
500
e
501
sage: limit( (1-cos(x))/(x^2), x = 1/2)
502
-4*cos(1/2) + 4
503
504
::
505
506
sage: # (OK-ish) D(x)Abs(x)
507
sage: # Verify(D(x) Abs(x), Sign(x));
508
sage: diff(abs(x))
509
x/abs(x)
510
511
::
512
513
sage: # (YES) (Integrate(x)Abs(x))=Abs(x)*x/2
514
sage: integral(abs(x), x)
515
1/2*x*abs(x)
516
517
::
518
519
sage: # (YES) Compute derivative of Abs(x), piecewise defined.
520
sage: # Verify(D(x)if(x<0) (-x) else x,
521
sage: # Simplify(if(x<0) -1 else 1))
522
Piecewise defined function with 2 parts, [[(-10, 0), -1], [(0, 10), 1]]
523
sage: # (NOT really) Integrate Abs(x), piecewise defined.
524
sage: # Verify(Simplify(Integrate(x)
525
sage: # if(x<0) (-x) else x),
526
sage: # Simplify(if(x<0) (-x^2/2) else x^2/2));
527
sage: f = piecewise([ [[-10,0], -x], [[0,10], x]])
528
sage: f.integral(definite=True)
529
100
530
531
::
532
533
sage: # (YES) Taylor series of 1/Sqrt(1-v^2/c^2) at v=0.
534
sage: var('v,c')
535
(v, c)
536
sage: taylor(1/sqrt(1-v^2/c^2), v, 0, 7)
537
1/2*v^2/c^2 + 3/8*v^4/c^4 + 5/16*v^6/c^6 + 1
538
539
::
540
541
sage: # (OK-ish) (Taylor expansion of Sin(x))/(Taylor expansion of Cos(x)) = (Taylor expansion of Tan(x)).
542
sage: # TestYacas(Taylor(x,0,5)(Taylor(x,0,5)Sin(x))/
543
sage: # (Taylor(x,0,5)Cos(x)), Taylor(x,0,5)Tan(x));
544
sage: f = taylor(sin(x), x, 0, 8)
545
sage: g = taylor(cos(x), x, 0, 8)
546
sage: h = taylor(tan(x), x, 0, 8)
547
sage: f = f.power_series(QQ)
548
sage: g = g.power_series(QQ)
549
sage: h = h.power_series(QQ)
550
sage: f - g*h
551
O(x^8)
552
553
::
554
555
sage: # (YES) Taylor expansion of Ln(x)^a*Exp(-b*x) at x=1.
556
sage: a,b = var('a,b')
557
sage: taylor(log(x)^a*exp(-b*x), x, 1, 3)
558
-1/48*(x - 1)^3*((6*b + 5)*(x - 1)^a*a^2 + (x - 1)^a*a^3 + 8*(x - 1)^a*b^3 + 2*(6*b^2 + 5*b + 3)*(x - 1)^a*a)*e^(-b) + 1/24*(x - 1)^2*((12*b + 5)*(x - 1)^a*a + 3*(x - 1)^a*a^2 + 12*(x - 1)^a*b^2)*e^(-b) - 1/2*(x - 1)*((x - 1)^a*a + 2*(x - 1)^a*b)*e^(-b) + (x - 1)^a*e^(-b)
559
560
::
561
562
sage: # (YES) Taylor expansion of Ln(Sin(x)/x) at x=0.
563
sage: taylor(log(sin(x)/x), x, 0, 10)
564
-1/467775*x^10 - 1/37800*x^8 - 1/2835*x^6 - 1/180*x^4 - 1/6*x^2
565
566
::
567
568
sage: # (NO) Compute n-th term of the Taylor series of Ln(Sin(x)/x) at x=0.
569
sage: # need formal functions
570
571
::
572
573
sage: # (NO) Compute n-th term of the Taylor series of Exp(-x)*Sin(x) at x=0.
574
sage: # (Sort of, with some work)
575
sage: # Solve x=Sin(y)+Cos(y) for y as Taylor series in x at x=1.
576
sage: # TestYacas(InverseTaylor(y,0,4) Sin(y)+Cos(y),
577
sage: # (y-1)+(y-1)^2/2+2*(y-1)^3/3+(y-1)^4);
578
sage: # Note that InverseTaylor does not give the series in terms of x but in terms of y which is semantically
579
sage: # wrong. But other CAS do the same.
580
sage: f = sin(y) + cos(y)
581
sage: g = f.taylor(y, 0, 10)
582
sage: h = g.power_series(QQ)
583
sage: k = (h - 1).reversion()
584
sage: print k
585
y + 1/2*y^2 + 2/3*y^3 + y^4 + 17/10*y^5 + 37/12*y^6 + 41/7*y^7 + 23/2*y^8 + 1667/72*y^9 + 3803/80*y^10 + O(y^11)
586
587
::
588
589
sage: # (OK) Compute Legendre polynomials directly from Rodrigues's formula, P[n]=1/(2^n*n!) *(Deriv(x,n)(x^2-1)^n).
590
sage: # P(n,x) := Simplify( 1/(2*n)!! *
591
sage: # Deriv(x,n) (x^2-1)^n );
592
sage: # TestYacas(P(4,x), (35*x^4)/8+(-15*x^2)/4+3/8);
593
sage: P = lambda n, x: simplify(diff((x^2-1)^n,x,n) / (2^n * factorial(n)))
594
sage: P(4,x).expand()
595
35/8*x^4 - 15/4*x^2 + 3/8
596
597
::
598
599
sage: # (YES) Define the polynomial p=Sum(i,1,5,a[i]*x^i).
600
sage: # symbolically
601
sage: ps = sum(var('a%s'%i)*x^i for i in range(1,6)); ps
602
a5*x^5 + a4*x^4 + a3*x^3 + a2*x^2 + a1*x
603
sage: ps.parent()
604
Symbolic Ring
605
sage: # algebraically
606
sage: R = PolynomialRing(QQ,5,names='a')
607
sage: S.<x> = PolynomialRing(R)
608
sage: p = S(list(R.gens()))*x; p
609
a4*x^5 + a3*x^4 + a2*x^3 + a1*x^2 + a0*x
610
sage: p.parent()
611
Univariate Polynomial Ring in x over Multivariate Polynomial Ring in a0, a1, a2, a3, a4 over Rational Field
612
613
::
614
615
sage: # (YES) Convert the above to Horner's form.
616
sage: # Verify(Horner(p, x), ((((a[5]*x+a[4])*x
617
sage: # +a[3])*x+a[2])*x+a[1])*x);
618
sage: # We use the trick of evaluating the algebraic poly at a symbolic variable:
619
sage: restore('x')
620
sage: p(x)
621
((((a4*x + a3)*x + a2)*x + a1)*x + a0)*x
622
623
::
624
625
sage: # (NO) Convert the result of problem 127 to Fortran syntax.
626
sage: # CForm(Horner(p, x));
627
628
::
629
630
sage: # (YES) Verify that True And False=False.
631
sage: (True and False) == False
632
True
633
634
::
635
636
sage: # (YES) Prove x Or Not x.
637
sage: for x in [True, False]:
638
... print x or (not x)
639
True
640
True
641
642
::
643
644
sage: # (YES) Prove x Or y Or x And y=>x Or y.
645
sage: for x in [True, False]:
646
... for y in [True, False]:
647
... if x or y or x and y:
648
... if not (x or y):
649
... print "failed!"
650
"""
651
652