Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/calculus/functional.py
4045 views
1
"""
2
Functional notation support for common calculus methods
3
4
EXAMPLES: We illustrate each of the calculus functional functions.
5
6
::
7
8
sage: simplify(x - x)
9
0
10
sage: a = var('a')
11
sage: derivative(x^a + sin(x), x)
12
a*x^(a - 1) + cos(x)
13
sage: diff(x^a + sin(x), x)
14
a*x^(a - 1) + cos(x)
15
sage: derivative(x^a + sin(x), x)
16
a*x^(a - 1) + cos(x)
17
sage: integral(a*x*sin(x), x)
18
-(x*cos(x) - sin(x))*a
19
sage: integrate(a*x*sin(x), x)
20
-(x*cos(x) - sin(x))*a
21
sage: limit(a*sin(x)/x, x=0)
22
a
23
sage: taylor(a*sin(x)/x, x, 0, 4)
24
1/120*a*x^4 - 1/6*a*x^2 + a
25
sage: expand( (x-a)^3 )
26
-a^3 + 3*a^2*x - 3*a*x^2 + x^3
27
sage: laplace( e^(x+a), x, a)
28
e^a/(a - 1)
29
sage: inverse_laplace( e^a/(a-1), x, a)
30
ilt(e^a/(a - 1), x, a)
31
"""
32
33
from calculus import SR
34
from sage.symbolic.expression import Expression
35
36
def simplify(f):
37
r"""
38
Simplify the expression `f`.
39
40
EXAMPLES: We simplify the expression `i + x - x`.
41
42
::
43
44
sage: f = I + x - x; simplify(f)
45
I
46
47
In fact, printing `f` yields the same thing - i.e., the
48
simplified form.
49
"""
50
try:
51
return f.simplify()
52
except AttributeError:
53
return f
54
55
def derivative(f, *args, **kwds):
56
"""
57
The derivative of `f`.
58
59
Repeated differentiation is supported by the syntax given in the
60
examples below.
61
62
ALIAS: diff
63
64
EXAMPLES: We differentiate a callable symbolic function::
65
66
sage: f(x,y) = x*y + sin(x^2) + e^(-x)
67
sage: f
68
(x, y) |--> x*y + e^(-x) + sin(x^2)
69
sage: derivative(f, x)
70
(x, y) |--> 2*x*cos(x^2) + y - e^(-x)
71
sage: derivative(f, y)
72
(x, y) |--> x
73
74
We differentiate a polynomial::
75
76
sage: t = polygen(QQ, 't')
77
sage: f = (1-t)^5; f
78
-t^5 + 5*t^4 - 10*t^3 + 10*t^2 - 5*t + 1
79
sage: derivative(f)
80
-5*t^4 + 20*t^3 - 30*t^2 + 20*t - 5
81
sage: derivative(f, t)
82
-5*t^4 + 20*t^3 - 30*t^2 + 20*t - 5
83
sage: derivative(f, t, t)
84
-20*t^3 + 60*t^2 - 60*t + 20
85
sage: derivative(f, t, 2)
86
-20*t^3 + 60*t^2 - 60*t + 20
87
sage: derivative(f, 2)
88
-20*t^3 + 60*t^2 - 60*t + 20
89
90
We differentiate a symbolic expression::
91
92
sage: var('a x')
93
(a, x)
94
sage: f = exp(sin(a - x^2))/x
95
sage: derivative(f, x)
96
-2*e^(sin(-x^2 + a))*cos(-x^2 + a) - e^(sin(-x^2 + a))/x^2
97
sage: derivative(f, a)
98
e^(sin(-x^2 + a))*cos(-x^2 + a)/x
99
100
Syntax for repeated differentiation::
101
102
sage: R.<u, v> = PolynomialRing(QQ)
103
sage: f = u^4*v^5
104
sage: derivative(f, u)
105
4*u^3*v^5
106
sage: f.derivative(u) # can always use method notation too
107
4*u^3*v^5
108
109
::
110
111
sage: derivative(f, u, u)
112
12*u^2*v^5
113
sage: derivative(f, u, u, u)
114
24*u*v^5
115
sage: derivative(f, u, 3)
116
24*u*v^5
117
118
::
119
120
sage: derivative(f, u, v)
121
20*u^3*v^4
122
sage: derivative(f, u, 2, v)
123
60*u^2*v^4
124
sage: derivative(f, u, v, 2)
125
80*u^3*v^3
126
sage: derivative(f, [u, v, v])
127
80*u^3*v^3
128
"""
129
try:
130
return f.derivative(*args, **kwds)
131
except AttributeError:
132
pass
133
if not isinstance(f, Expression):
134
f = SR(f)
135
return f.derivative(*args, **kwds)
136
137
diff = derivative
138
139
def integral(f, *args, **kwds):
140
r"""
141
The integral of `f`.
142
143
EXAMPLES::
144
145
sage: integral(sin(x), x)
146
-cos(x)
147
sage: integral(sin(x)^2, x, pi, 123*pi/2)
148
121/4*pi
149
sage: integral( sin(x), x, 0, pi)
150
2
151
152
We integrate a symbolic function::
153
154
sage: f(x,y,z) = x*y/z + sin(z)
155
sage: integral(f, z)
156
(x, y, z) |--> x*y*log(z) - cos(z)
157
158
::
159
160
sage: var('a,b')
161
(a, b)
162
sage: assume(b-a>0)
163
sage: integral( sin(x), x, a, b)
164
cos(a) - cos(b)
165
sage: forget()
166
167
::
168
169
sage: integral(x/(x^3-1), x)
170
1/3*sqrt(3)*arctan(1/3*(2*x + 1)*sqrt(3)) + 1/3*log(x - 1) - 1/6*log(x^2 + x + 1)
171
172
::
173
174
sage: integral( exp(-x^2), x )
175
1/2*sqrt(pi)*erf(x)
176
177
We define the Gaussian, plot and integrate it numerically and
178
symbolically::
179
180
sage: f(x) = 1/(sqrt(2*pi)) * e^(-x^2/2)
181
sage: P = plot(f, -4, 4, hue=0.8, thickness=2)
182
sage: P.show(ymin=0, ymax=0.4)
183
sage: numerical_integral(f, -4, 4) # random output
184
(0.99993665751633376, 1.1101527003413533e-14)
185
sage: integrate(f, x)
186
x |--> 1/2*erf(1/2*sqrt(2)*x)
187
188
You can have Sage calculate multiple integrals. For example,
189
consider the function `exp(y^2)` on the region between the
190
lines `x=y`, `x=1`, and `y=0`. We find the
191
value of the integral on this region using the command::
192
193
sage: area = integral(integral(exp(y^2),x,0,y),y,0,1); area
194
1/2*e - 1/2
195
sage: float(area)
196
0.859140914229522...
197
198
We compute the line integral of `\sin(x)` along the arc of
199
the curve `x=y^4` from `(1,-1)` to
200
`(1,1)`::
201
202
sage: t = var('t')
203
sage: (x,y) = (t^4,t)
204
sage: (dx,dy) = (diff(x,t), diff(y,t))
205
sage: integral(sin(x)*dx, t,-1, 1)
206
0
207
sage: restore('x,y') # restore the symbolic variables x and y
208
209
Sage is unable to do anything with the following integral::
210
211
sage: integral( exp(-x^2)*log(x), x )
212
integrate(e^(-x^2)*log(x), x)
213
214
Note, however, that::
215
216
sage: integral( exp(-x^2)*ln(x), x, 0, oo)
217
-1/4*(euler_gamma + 2*log(2))*sqrt(pi)
218
219
This definite integral is easy::
220
221
sage: integral( ln(x)/x, x, 1, 2)
222
1/2*log(2)^2
223
224
Sage can't do this elliptic integral (yet)::
225
226
sage: integral(1/sqrt(2*t^4 - 3*t^2 - 2), t, 2, 3)
227
integrate(1/sqrt(2*t^4 - 3*t^2 - 2), t, 2, 3)
228
229
A double integral::
230
231
sage: y = var('y')
232
sage: integral(integral(x*y^2, x, 0, y), y, -2, 2)
233
32/5
234
235
This illustrates using assumptions::
236
237
sage: integral(abs(x), x, 0, 5)
238
25/2
239
sage: a = var("a")
240
sage: integral(abs(x), x, 0, a)
241
1/2*a*abs(a)
242
sage: integral(abs(x)*x, x, 0, a)
243
Traceback (most recent call last):
244
...
245
ValueError: Computation failed since Maxima requested additional
246
constraints; using the 'assume' command before integral evaluation
247
*may* help (example of legal syntax is 'assume(a>0)',
248
see `assume?` for more details)
249
Is a positive, negative, or zero?
250
sage: assume(a>0)
251
sage: integral(abs(x)*x, x, 0, a)
252
1/3*a^3
253
sage: forget() # forget the assumptions.
254
255
We integrate and differentiate a huge mess::
256
257
sage: f = (x^2-1+3*(1+x^2)^(1/3))/(1+x^2)^(2/3)*x/(x^2+2)^2
258
sage: g = integral(f, x)
259
sage: h = f - diff(g, x)
260
261
::
262
263
sage: [float(h(i)) for i in range(5)] #random
264
265
[0.0,
266
-1.1102230246251565e-16,
267
-5.5511151231257827e-17,
268
-5.5511151231257827e-17,
269
-6.9388939039072284e-17]
270
sage: h.factor()
271
0
272
sage: bool(h == 0)
273
True
274
"""
275
try:
276
return f.integral(*args, **kwds)
277
except AttributeError:
278
pass
279
280
if not isinstance(f, Expression):
281
f = SR(f)
282
return f.integral(*args, **kwds)
283
284
integrate = integral
285
286
def limit(f, dir=None, taylor=False, **argv):
287
r"""
288
Return the limit as the variable `v` approaches `a`
289
from the given direction.
290
291
::
292
293
limit(expr, x = a)
294
limit(expr, x = a, dir='above')
295
296
297
INPUT:
298
299
- ``dir`` - (default: None); dir may have the value
300
'plus' (or 'above') for a limit from above, 'minus' (or 'below')
301
for a limit from below, or may be omitted (implying a two-sided
302
limit is to be computed).
303
304
- ``taylor`` - (default: False); if True, use Taylor
305
series, which allows more limits to be computed (but may also
306
crash in some obscure cases due to bugs in Maxima).
307
308
- ``\*\*argv`` - 1 named parameter
309
310
ALIAS: You can also use lim instead of limit.
311
312
EXAMPLES::
313
314
sage: limit(sin(x)/x, x=0)
315
1
316
sage: limit(exp(x), x=oo)
317
+Infinity
318
sage: lim(exp(x), x=-oo)
319
0
320
sage: lim(1/x, x=0)
321
Infinity
322
sage: limit(sqrt(x^2+x+1)+x, taylor=True, x=-oo)
323
-1/2
324
sage: limit((tan(sin(x)) - sin(tan(x)))/x^7, taylor=True, x=0)
325
1/30
326
327
Sage does not know how to do this limit (which is 0), so it returns
328
it unevaluated::
329
330
sage: lim(exp(x^2)*(1-erf(x)), x=infinity)
331
-limit((erf(x) - 1)*e^(x^2), x, +Infinity)
332
"""
333
if not isinstance(f, Expression):
334
f = SR(f)
335
return f.limit(dir=dir, taylor=taylor, **argv)
336
337
lim = limit
338
339
def taylor(f, *args):
340
"""
341
Expands self in a truncated Taylor or Laurent series in the
342
variable `v` around the point `a`, containing terms
343
through `(x - a)^n`. Functions in more variables are also
344
supported.
345
346
INPUT:
347
348
- ``*args`` - the following notation is supported
349
350
- ``x, a, n`` - variable, point, degree
351
352
- ``(x, a), (y, b), ..., n`` - variables with points, degree of polynomial
353
354
EXAMPLES::
355
356
sage: var('x,k,n')
357
(x, k, n)
358
sage: taylor (sqrt (1 - k^2*sin(x)^2), x, 0, 6)
359
-1/720*(45*k^6 - 60*k^4 + 16*k^2)*x^6 - 1/24*(3*k^4 - 4*k^2)*x^4 - 1/2*k^2*x^2 + 1
360
361
::
362
363
sage: taylor ((x + 1)^n, x, 0, 4)
364
1/24*(n^4 - 6*n^3 + 11*n^2 - 6*n)*x^4 + 1/6*(n^3 - 3*n^2 + 2*n)*x^3 + 1/2*(n^2 - n)*x^2 + n*x + 1
365
366
::
367
368
sage: taylor ((x + 1)^n, x, 0, 4)
369
1/24*(n^4 - 6*n^3 + 11*n^2 - 6*n)*x^4 + 1/6*(n^3 - 3*n^2 + 2*n)*x^3 + 1/2*(n^2 - n)*x^2 + n*x + 1
370
371
Taylor polynomial in two variables::
372
373
sage: x,y=var('x y'); taylor(x*y^3,(x,1),(y,-1),4)
374
(y + 1)^3*(x - 1) + (y + 1)^3 - 3*(y + 1)^2*(x - 1) - 3*(y + 1)^2 + 3*(y + 1)*(x - 1) - x + 3*y + 3
375
"""
376
if not isinstance(f, Expression):
377
f = SR(f)
378
return f.taylor(*args)
379
380
def expand(x, *args, **kwds):
381
"""
382
EXAMPLES::
383
384
sage: a = (x-1)*(x^2 - 1); a
385
(x - 1)*(x^2 - 1)
386
sage: expand(a)
387
x^3 - x^2 - x + 1
388
389
You can also use expand on polynomial, integer, and other
390
factorizations::
391
392
sage: x = polygen(ZZ)
393
sage: F = factor(x^12 - 1); F
394
(x - 1) * (x + 1) * (x^2 - x + 1) * (x^2 + 1) * (x^2 + x + 1) * (x^4 - x^2 + 1)
395
sage: expand(F)
396
x^12 - 1
397
sage: F.expand()
398
x^12 - 1
399
sage: F = factor(2007); F
400
3^2 * 223
401
sage: expand(F)
402
2007
403
404
Note: If you want to compute the expanded form of a polynomial
405
arithmetic operation quickly and the coefficients of the polynomial
406
all lie in some ring, e.g., the integers, it is vastly faster to
407
create a polynomial ring and do the arithmetic there.
408
409
::
410
411
sage: x = polygen(ZZ) # polynomial over a given base ring.
412
sage: f = sum(x^n for n in range(5))
413
sage: f*f # much faster, even if the degree is huge
414
x^8 + 2*x^7 + 3*x^6 + 4*x^5 + 5*x^4 + 4*x^3 + 3*x^2 + 2*x + 1
415
416
TESTS::
417
418
sage: t1 = (sqrt(3)-3)*(sqrt(3)+1)/6;
419
sage: tt1 = -1/sqrt(3);
420
sage: t2 = sqrt(3)/6;
421
sage: float(t1)
422
-0.577350269189625...
423
sage: float(tt1)
424
-0.577350269189625...
425
sage: float(t2)
426
0.28867513459481287
427
sage: float(expand(t1 + t2))
428
-0.288675134594812...
429
sage: float(expand(tt1 + t2))
430
-0.288675134594812...
431
"""
432
try:
433
return x.expand(*args, **kwds)
434
except AttributeError:
435
return x
436
437