Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/calculus/tests.py
4045 views
1
r"""
2
Calculus Tests and Examples
3
4
Compute the Christoffel symbol.
5
6
::
7
8
sage: var('r t theta phi')
9
(r, t, theta, phi)
10
sage: m = matrix(SR, [[(1-1/r),0,0,0],[0,-(1-1/r)^(-1),0,0],[0,0,-r^2,0],[0,0,0,-r^2*(sin(theta))^2]])
11
sage: print m
12
[ -1/r + 1 0 0 0]
13
[ 0 1/(1/r - 1) 0 0]
14
[ 0 0 -r^2 0]
15
[ 0 0 0 -r^2*sin(theta)^2]
16
17
::
18
19
sage: def christoffel(i,j,k,vars,g):
20
... s = 0
21
... ginv = g^(-1)
22
... for l in range(g.nrows()):
23
... s = s + (1/2)*ginv[k,l]*(g[j,l].diff(vars[i])+g[i,l].diff(vars[j])-g[i,j].diff(vars[l]))
24
... return s
25
26
::
27
28
sage: christoffel(3,3,2, [t,r,theta,phi], m)
29
-sin(theta)*cos(theta)
30
sage: X = christoffel(1,1,1,[t,r,theta,phi],m)
31
sage: X
32
1/2/((1/r - 1)*r^2)
33
sage: X.rational_simplify()
34
-1/2/(r^2 - r)
35
36
Some basic things::
37
38
sage: f(x,y) = x^3 + sinh(1/y)
39
sage: f
40
(x, y) |--> x^3 + sinh(1/y)
41
sage: f^3
42
(x, y) |--> (x^3 + sinh(1/y))^3
43
sage: (f^3).expand()
44
(x, y) |--> x^9 + 3*x^6*sinh(1/y) + 3*x^3*sinh(1/y)^2 + sinh(1/y)^3
45
46
A polynomial over a symbolic base ring::
47
48
sage: R = SR[x]
49
sage: f = R([1/sqrt(2), 1/(4*sqrt(2))])
50
sage: f
51
1/8*sqrt(2)*x + 1/2*sqrt(2)
52
sage: -f
53
-1/8*sqrt(2)*x - 1/2*sqrt(2)
54
sage: (-f).degree()
55
1
56
57
A big product. Notice that simplifying simplifies the product further::
58
59
sage: A = exp(I*pi/5)
60
sage: b = A*A*A*A*A*A*A*A*A*A
61
sage: b
62
1
63
64
We check a statement made at the beginning of Friedlander and
65
Joshi's book on Distributions::
66
67
sage: f(x) = sin(x^2)
68
sage: g(x) = cos(x) + x^3
69
sage: u = f(x+t) + g(x-t)
70
sage: u
71
-(t - x)^3 + sin((t + x)^2) + cos(-t + x)
72
sage: u.diff(t,2) - u.diff(x,2)
73
0
74
75
Restoring variables after they have been turned into functions::
76
77
sage: x = function('x')
78
sage: type(x)
79
<class 'sage.symbolic.function_factory.NewSymbolicFunction'>
80
sage: x(2/3)
81
x(2/3)
82
sage: restore('x')
83
sage: sin(x).variables()
84
(x,)
85
86
MATHEMATICA: Some examples of integration and differentiation taken
87
from some Mathematica docs::
88
89
sage: var('x n a')
90
(x, n, a)
91
sage: diff(x^n, x) # the output looks funny, but is correct
92
n*x^(n - 1)
93
sage: diff(x^2 * log(x+a), x)
94
2*x*log(a + x) + x^2/(a + x)
95
sage: derivative(arctan(x), x)
96
1/(x^2 + 1)
97
sage: derivative(x^n, x, 3)
98
(n - 2)*(n - 1)*n*x^(n - 3)
99
sage: derivative( function('f')(x), x)
100
D[0](f)(x)
101
sage: diff( 2*x*f(x^2), x)
102
4*x^2*D[0](f)(x^2) + 2*f(x^2)
103
sage: integrate( 1/(x^4 - a^4), x)
104
1/4*log(-a + x)/a^3 - 1/4*log(a + x)/a^3 - 1/2*arctan(x/a)/a^3
105
sage: expand(integrate(log(1-x^2), x))
106
x*log(-x^2 + 1) - 2*x - log(x - 1) + log(x + 1)
107
sage: integrate(log(1-x^2)/x, x)
108
1/2*log(-x^2 + 1)*log(x^2) + 1/2*polylog(2, -x^2 + 1)
109
sage: integrate(exp(1-x^2),x)
110
1/2*sqrt(pi)*e*erf(x)
111
sage: integrate(sin(x^2),x)
112
1/8*((I + 1)*sqrt(2)*erf((1/2*I + 1/2)*sqrt(2)*x) + (I - 1)*sqrt(2)*erf((1/2*I - 1/2)*sqrt(2)*x))*sqrt(pi)
113
114
sage: integrate((1-x^2)^n,x)
115
integrate((-x^2 + 1)^n, x)
116
sage: integrate(x^x,x)
117
integrate(x^x, x)
118
sage: integrate(1/(x^3+1),x)
119
1/3*sqrt(3)*arctan(1/3*(2*x - 1)*sqrt(3)) + 1/3*log(x + 1) - 1/6*log(x^2 - x + 1)
120
sage: integrate(1/(x^3+1), x, 0, 1)
121
1/9*pi*sqrt(3) + 1/3*log(2)
122
123
::
124
125
sage: forget()
126
sage: c = var('c')
127
sage: assume(c > 0)
128
sage: integrate(exp(-c*x^2), x, -oo, oo)
129
sqrt(pi)/sqrt(c)
130
sage: forget()
131
132
The following are a bunch of examples of integrals that Mathematica
133
can do, but Sage currently can't do::
134
135
sage: integrate(log(x)*exp(-x^2), x) # todo -- Mathematica can do this
136
integrate(e^(-x^2)*log(x), x)
137
138
Todo - Mathematica can do this and gets `\pi^2/15`.
139
140
::
141
142
sage: integrate(log(1+sqrt(1+4*x)/2)/x, x, 0, 1) # not tested
143
Traceback (most recent call last):
144
...
145
ValueError: Integral is divergent.
146
147
::
148
149
sage: integrate(ceil(x^2 + floor(x)), x, 0, 5) # todo: Mathematica can do this
150
integrate(ceil(x^2) + floor(x), x, 0, 5)
151
152
MAPLE: The basic differentiation and integration examples in the
153
Maple documentation::
154
155
sage: diff(sin(x), x)
156
cos(x)
157
sage: diff(sin(x), y)
158
0
159
sage: diff(sin(x), x, 3)
160
-cos(x)
161
sage: diff(x*sin(cos(x)), x)
162
-x*sin(x)*cos(cos(x)) + sin(cos(x))
163
sage: diff(tan(x), x)
164
tan(x)^2 + 1
165
sage: f = function('f'); f
166
f
167
sage: diff(f(x), x)
168
D[0](f)(x)
169
sage: diff(f(x,y), x, y)
170
D[0, 1](f)(x, y)
171
sage: diff(f(x,y), x, y) - diff(f(x,y), y, x)
172
0
173
sage: g = function('g')
174
sage: var('x y z')
175
(x, y, z)
176
sage: diff(g(x,y,z), x,z,z)
177
D[0, 2, 2](g)(x, y, z)
178
sage: integrate(sin(x), x)
179
-cos(x)
180
sage: integrate(sin(x), x, 0, pi)
181
2
182
183
::
184
185
sage: var('a b')
186
(a, b)
187
sage: integrate(sin(x), x, a, b)
188
cos(a) - cos(b)
189
190
::
191
192
sage: integrate( x/(x^3-1), x)
193
1/3*sqrt(3)*arctan(1/3*(2*x + 1)*sqrt(3)) + 1/3*log(x - 1) - 1/6*log(x^2 + x + 1)
194
sage: integrate(exp(-x^2), x)
195
1/2*sqrt(pi)*erf(x)
196
sage: integrate(exp(-x^2)*log(x), x) # todo: maple can compute this exactly.
197
integrate(e^(-x^2)*log(x), x)
198
sage: f = exp(-x^2)*log(x)
199
sage: f.nintegral(x, 0, 999)
200
(-0.87005772672831..., 7.5584...e-10, 567, 0)
201
sage: integral(1/sqrt(2*t^4 - 3*t^2 - 2), t, 2, 3) # todo: maple can do this
202
integrate(1/sqrt(2*t^4 - 3*t^2 - 2), t, 2, 3)
203
sage: integral(integral(x*y^2, x, 0, y), y, -2, 2)
204
32/5
205
206
We verify several standard differentiation rules::
207
208
sage: function('f, g')
209
(f, g)
210
sage: diff(f(t)*g(t),t)
211
f(t)*D[0](g)(t) + g(t)*D[0](f)(t)
212
sage: diff(f(t)/g(t), t)
213
-f(t)*D[0](g)(t)/g(t)^2 + D[0](f)(t)/g(t)
214
sage: diff(f(t) + g(t), t)
215
D[0](f)(t) + D[0](g)(t)
216
sage: diff(c*f(t), t)
217
c*D[0](f)(t)
218
"""
219
220