| 1 | ## -*- encoding: utf-8 -*- |
| 2 | """ |
| 3 | Doctests from French Sage book |
| 4 | Test file for chapter "Analyse et algèbre avec Sage" ("Calculus and |
| 5 | algebra with Sage") |
| 6 | |
| 7 | Tests extracted from ./calculus.tex. |
| 8 | |
| 9 | Sage example in ./calculus.tex, line 37:: |
| 10 | |
| 11 | sage: bool(x^2 + 3*x + 1 == (x+1)*(x+2)) |
| 12 | False |
| 13 | |
| 14 | Sage example in ./calculus.tex, line 74:: |
| 15 | |
| 16 | sage: a, x = var('a, x'); y = cos(x+a) * (x+1); y |
| 17 | (x + 1)*cos(a + x) |
| 18 | sage: y.subs(a=-x); y.subs(x=pi/2, a=pi/3); y.subs(x=0.5, a=2.3) |
| 19 | x + 1 |
| 20 | -1/4*sqrt(3)*(pi + 2) |
| 21 | -1.41333351100299 |
| 22 | sage: y(a=-x); y(x=pi/2, a=pi/3); y(x=0.5, a=2.3) |
| 23 | x + 1 |
| 24 | -1/4*sqrt(3)*(pi + 2) |
| 25 | -1.41333351100299 |
| 26 | |
| 27 | Sage example in ./calculus.tex, line 91:: |
| 28 | |
| 29 | sage: x, y, z = var('x, y, z') ; q = x*y + y*z + z*x |
| 30 | sage: bool(q(x=y, y=z, z=x) == q), bool(q(z=y)(y=x) == 3*x^2) |
| 31 | (True, True) |
| 32 | |
| 33 | Sage example in ./calculus.tex, line 99:: |
| 34 | |
| 35 | sage: y, z = var('y, z'); f = x^3 + y^2 + z |
| 36 | sage: f.subs_expr(x^3 == y^2, z==1) |
| 37 | 2*y^2 + 1 |
| 38 | |
| 39 | Sage example in ./calculus.tex, line 110:: |
| 40 | |
| 41 | sage: f(x)=(2*x+1)^3 ; f(-3) |
| 42 | -125 |
| 43 | sage: f.expand() |
| 44 | x |--> 8*x^3 + 12*x^2 + 6*x + 1 |
| 45 | |
| 46 | Sage example in ./calculus.tex, line 122:: |
| 47 | |
| 48 | sage: y = var('y'); u = sin(x) + x*cos(y) |
| 49 | sage: v = u.function(x, y); v |
| 50 | (x, y) |--> x*cos(y) + sin(x) |
| 51 | sage: w(x, y) = u; w |
| 52 | (x, y) |--> x*cos(y) + sin(x) |
| 53 | |
| 54 | Sage example in ./calculus.tex, line 153:: |
| 55 | |
| 56 | sage: x, y = SR.var('x,y') |
| 57 | sage: p = (x+y)*(x+1)^2 |
| 58 | sage: p2 = p.expand(); p2 |
| 59 | x^3 + x^2*y + 2*x^2 + 2*x*y + x + y |
| 60 | |
| 61 | Sage example in ./calculus.tex, line 160:: |
| 62 | |
| 63 | sage: p2.collect(x) |
| 64 | x^3 + x^2*(y + 2) + x*(2*y + 1) + y |
| 65 | |
| 66 | Sage example in ./calculus.tex, line 165:: |
| 67 | |
| 68 | sage: ((x+y+sin(x))^2).expand().collect(sin(x)) |
| 69 | x^2 + 2*x*y + y^2 + 2*(x + y)*sin(x) + sin(x)^2 |
| 70 | |
| 71 | Sage example in ./calculus.tex, line 254:: |
| 72 | |
| 73 | sage: (x^x/x).simplify() |
| 74 | x^(x - 1) |
| 75 | |
| 76 | Sage example in ./calculus.tex, line 260:: |
| 77 | |
| 78 | sage: f = (e^x-1) / (1+e^(x/2)); f.simplify_exp() |
| 79 | e^(1/2*x) - 1 |
| 80 | |
| 81 | Sage example in ./calculus.tex, line 266:: |
| 82 | |
| 83 | sage: f = cos(x)^6 + sin(x)^6 + 3 * sin(x)^2 * cos(x)^2 |
| 84 | sage: f.simplify_trig() |
| 85 | 1 |
| 86 | |
| 87 | Sage example in ./calculus.tex, line 273:: |
| 88 | |
| 89 | sage: f = cos(x)^6; f.reduce_trig() |
| 90 | 1/32*cos(6*x) + 3/16*cos(4*x) + 15/32*cos(2*x) + 5/16 |
| 91 | sage: f = sin(5 * x); f.expand_trig() |
| 92 | 5*cos(x)^4*sin(x) - 10*cos(x)^2*sin(x)^3 + sin(x)^5 |
| 93 | |
| 94 | Sage example in ./calculus.tex, line 306:: |
| 95 | |
| 96 | sage: n = var('n'); f = factorial(n+1)/factorial(n) |
| 97 | sage: f.simplify_factorial() |
| 98 | n + 1 |
| 99 | |
| 100 | Sage example in ./calculus.tex, line 318:: |
| 101 | |
| 102 | sage: f = sqrt(abs(x)^2); f.simplify_radical() |
| 103 | abs(x) |
| 104 | sage: f = log(x*y); f.simplify_radical() |
| 105 | log(x) + log(y) |
| 106 | |
| 107 | Sage example in ./calculus.tex, line 371:: |
| 108 | |
| 109 | sage: assume(x > 0); bool(sqrt(x^2) == x) |
| 110 | True |
| 111 | sage: forget(x > 0); bool(sqrt(x^2) == x) |
| 112 | False |
| 113 | sage: n = var('n'); assume(n, 'integer'); sin(n*pi).simplify() |
| 114 | 0 |
| 115 | |
| 116 | Sage example in ./calculus.tex, line 420:: |
| 117 | |
| 118 | sage: a = var('a') |
| 119 | sage: c = (a+1)^2 - (a^2+2*a+1) |
| 120 | |
| 121 | Sage example in ./calculus.tex, line 425:: |
| 122 | |
| 123 | sage: eq = c * x == 0 |
| 124 | |
| 125 | Sage example in ./calculus.tex, line 430:: |
| 126 | |
| 127 | sage: eq2 = eq / c; eq2 |
| 128 | x == 0 |
| 129 | sage: solve(eq2, x) |
| 130 | [x == 0] |
| 131 | |
| 132 | Sage example in ./calculus.tex, line 437:: |
| 133 | |
| 134 | sage: solve(eq, x) |
| 135 | [x == x] |
| 136 | |
| 137 | Sage example in ./calculus.tex, line 444:: |
| 138 | |
| 139 | sage: expand(c) |
| 140 | 0 |
| 141 | |
| 142 | Sage example in ./calculus.tex, line 452:: |
| 143 | |
| 144 | sage: c = cos(a)^2 + sin(a)^2 - 1 |
| 145 | sage: eq = c*x == 0 |
| 146 | sage: solve(eq, x) |
| 147 | [x == 0] |
| 148 | |
| 149 | Sage example in ./calculus.tex, line 460:: |
| 150 | |
| 151 | sage: c.simplify_trig() |
| 152 | 0 |
| 153 | sage: c.is_zero() |
| 154 | True |
| 155 | |
| 156 | Sage example in ./calculus.tex, line 516:: |
| 157 | |
| 158 | sage: z, phi = var('z, phi') |
| 159 | sage: eq = z**2 - 2/cos(phi)*z + 5/cos(phi)**2 - 4 == 0; eq |
| 160 | z^2 - 2*z/cos(phi) + 5/cos(phi)^2 - 4 == 0 |
| 161 | |
| 162 | Sage example in ./calculus.tex, line 523:: |
| 163 | |
| 164 | sage: eq.lhs() |
| 165 | z^2 - 2*z/cos(phi) + 5/cos(phi)^2 - 4 |
| 166 | sage: eq.rhs() |
| 167 | 0 |
| 168 | |
| 169 | Sage example in ./calculus.tex, line 531:: |
| 170 | |
| 171 | sage: solve(eq, z) |
| 172 | [z == -(2*sqrt(cos(phi)^2 - 1) - 1)/cos(phi), |
| 173 | z == (2*sqrt(cos(phi)^2 - 1) + 1)/cos(phi)] |
| 174 | |
| 175 | Sage example in ./calculus.tex, line 537:: |
| 176 | |
| 177 | sage: y = var('y'); solve(y^6==y, y) |
| 178 | [y == e^(2/5*I*pi), y == e^(4/5*I*pi), y == e^(-4/5*I*pi), |
| 179 | y == e^(-2/5*I*pi), y == 1, y == 0] |
| 180 | |
| 181 | Sage example in ./calculus.tex, line 544:: |
| 182 | |
| 183 | sage: solve(x^2-1, x, solution_dict=True) |
| 184 | [{x: -1}, {x: 1}] |
| 185 | |
| 186 | Sage example in ./calculus.tex, line 550:: |
| 187 | |
| 188 | sage: solve([x+y == 3, 2*x+2*y == 6], x, y) |
| 189 | [[x == -r1 + 3, y == r1]] |
| 190 | |
| 191 | Sage example in ./calculus.tex, line 560:: |
| 192 | |
| 193 | sage: solve([cos(x)*sin(x) == 1/2, x+y == 0], x, y) |
| 194 | [[x == 1/4*pi + pi*z..., y == -1/4*pi - pi*z...]] |
| 195 | |
| 196 | Sage example in ./calculus.tex, line 565:: |
| 197 | |
| 198 | sage: solve(x^2+x-1 > 0, x) |
| 199 | [[x < -1/2*sqrt(5) - 1/2], [x > 1/2*sqrt(5) - 1/2]] |
| 200 | |
| 201 | Sage example in ./calculus.tex, line 583:: |
| 202 | |
| 203 | sage: x, y, z = var('x, y, z') |
| 204 | sage: solve([x^2 * y * z == 18, x * y^3 * z == 24,\ |
| 205 | ... x * y * z^4 == 3], x, y, z) |
| 206 | [[x == (-2.76736473308 - 1.71347969911*I), y == (-0.570103503963 + 2.00370597877*I), z == (-0.801684337646 - 0.14986077496*I)], ...] |
| 207 | |
| 208 | Sage example in ./calculus.tex, line 597:: |
| 209 | |
| 210 | sage: expr = sin(x) + sin(2 * x) + sin(3 * x) |
| 211 | sage: solve(expr, x) |
| 212 | [sin(3*x) == -sin(2*x) - sin(x)] |
| 213 | |
| 214 | Sage example in ./calculus.tex, line 605:: |
| 215 | |
| 216 | sage: find_root(expr, 0.1, pi) |
| 217 | 2.0943951023931957 |
| 218 | |
| 219 | Sage example in ./calculus.tex, line 610:: |
| 220 | |
| 221 | sage: f = expr.simplify_trig(); f |
| 222 | 2*(2*cos(x)^2 + cos(x))*sin(x) |
| 223 | sage: solve(f, x) |
| 224 | [x == 0, x == 2/3*pi, x == 1/2*pi] |
| 225 | |
| 226 | Sage example in ./calculus.tex, line 629:: |
| 227 | |
| 228 | sage: (x^3+2*x+1).roots(x) |
| 229 | [(-1/2*(1/18*sqrt(59)*sqrt(3) - 1/2)^(1/3)*(I*sqrt(3) + 1) |
| 230 | - 1/3*(I*sqrt(3) - 1)/(1/18*sqrt(59)*sqrt(3) - 1/2)^(1/3), 1), |
| 231 | (-1/2*(1/18*sqrt(59)*sqrt(3) - 1/2)^(1/3)*(-I*sqrt(3) + 1) |
| 232 | - 1/3*(-I*sqrt(3) - 1)/(1/18*sqrt(59)*sqrt(3) - 1/2)^(1/3), 1), |
| 233 | ((1/18*sqrt(59)*sqrt(3) - 1/2)^(1/3) - 2/3/(1/18*sqrt(59)*sqrt(3) |
| 234 | - 1/2)^(1/3), 1)] |
| 235 | |
| 236 | Sage example in ./calculus.tex, line 658:: |
| 237 | |
| 238 | sage: (x^3+2*x+1).roots(x, ring=RR) |
| 239 | [(-0.453397651516404, 1)] |
| 240 | |
| 241 | Sage example in ./calculus.tex, line 662:: |
| 242 | |
| 243 | sage: (x^3+2*x+1).roots(x, ring=CC) |
| 244 | [(-0.453397651516404, 1), |
| 245 | (0.226698825758202 - 1.46771150871022*I, 1), |
| 246 | (0.226698825758202 + 1.46771150871022*I, 1)] |
| 247 | |
| 248 | Sage example in ./calculus.tex, line 680:: |
| 249 | |
| 250 | sage: solve(x^(1/x)==(1/x)^x, x) |
| 251 | [(1/x)^x == x^(1/x)] |
| 252 | |
| 253 | Sage example in ./calculus.tex, line 706:: |
| 254 | |
| 255 | sage: y = function('y', x) |
| 256 | sage: desolve(diff(y,x,x) + x*diff(y,x) + y == 0, y, [0,0,1]) |
| 257 | -1/2*I*sqrt(2)*sqrt(pi)*erf(1/2*I*sqrt(2)*x)*e^(-1/2*x^2) |
| 258 | |
| 259 | Sage example in ./calculus.tex, line 733:: |
| 260 | |
| 261 | sage: k, n = var('k, n') |
| 262 | sage: sum(k, k, 1, n).factor() |
| 263 | 1/2*(n + 1)*n |
| 264 | |
| 265 | Sage example in ./calculus.tex, line 739:: |
| 266 | |
| 267 | sage: n, k, y = var('n, k, y') |
| 268 | sage: sum(binomial(n,k) * x^k * y^(n-k), k, 0, n) |
| 269 | (x + y)^n |
| 270 | |
| 271 | Sage example in ./calculus.tex, line 745:: |
| 272 | |
| 273 | sage: k, n = var('k, n') |
| 274 | sage: sum(binomial(n,k), k, 0, n),\ |
| 275 | ... sum(k * binomial(n, k), k, 0, n),\ |
| 276 | ... sum((-1)^k*binomial(n,k), k, 0, n) |
| 277 | (2^n, 2^(n - 1)*n, 0) |
| 278 | |
| 279 | Sage example in ./calculus.tex, line 753:: |
| 280 | |
| 281 | sage: a, q, k, n = var('a, q, k, n') |
| 282 | sage: sum(a*q^k, k, 0, n) |
| 283 | (a*q^(n + 1) - a)/(q - 1) |
| 284 | |
| 285 | Sage example in ./calculus.tex, line 760:: |
| 286 | |
| 287 | sage: assume(abs(q) < 1) |
| 288 | sage: sum(a*q^k, k, 0, infinity) |
| 289 | -a/(q - 1) |
| 290 | |
| 291 | Sage example in ./calculus.tex, line 766:: |
| 292 | |
| 293 | sage: forget(); assume(q > 1); sum(a*q^k, k, 0, infinity) |
| 294 | Traceback (most recent call last): |
| 295 | ... |
| 296 | ValueError: Sum is divergent. |
| 297 | |
| 298 | Sage example in ./calculus.tex, line 842:: |
| 299 | |
| 300 | sage: limit((x**(1/3) - 2) / ((x + 19)**(1/3) - 3), x = 8) |
| 301 | 9/4 |
| 302 | sage: f(x) = (cos(pi/4-x)-tan(x))/(1-sin(pi/4 + x)) |
| 303 | sage: limit(f(x), x = pi/4) |
| 304 | Infinity |
| 305 | |
| 306 | Sage example in ./calculus.tex, line 855:: |
| 307 | |
| 308 | sage: limit(f(x), x = pi/4, dir='minus') |
| 309 | +Infinity |
| 310 | sage: limit(f(x), x = pi/4, dir='plus') |
| 311 | -Infinity |
| 312 | |
| 313 | Sage example in ./calculus.tex, line 898:: |
| 314 | |
| 315 | sage: u(n) = n^100 / 100^n |
| 316 | sage: u(2.);u(3.);u(4.);u(5.);u(6.);u(7.);u(8.);u(9.);u(10.) |
| 317 | 1.26765060022823e26 |
| 318 | 5.15377520732011e41 |
| 319 | 1.60693804425899e52 |
| 320 | 7.88860905221012e59 |
| 321 | 6.53318623500071e65 |
| 322 | 3.23447650962476e70 |
| 323 | 2.03703597633449e74 |
| 324 | 2.65613988875875e77 |
| 325 | 1.00000000000000e80 |
| 326 | |
| 327 | Sage example in ./calculus.tex, line 914:: |
| 328 | |
| 329 | sage: plot(u(x), x, 1, 40) |
| 330 | |
| 331 | Sage example in ./calculus.tex, line 929:: |
| 332 | |
| 333 | sage: v(x) = diff(u(x), x); sol = solve(v(x) == 0, x); sol |
| 334 | [x == 100/log(100), x == 0] |
| 335 | sage: floor(sol[0].rhs()) |
| 336 | 21 |
| 337 | |
| 338 | Sage example in ./calculus.tex, line 938:: |
| 339 | |
| 340 | sage: limit(u(n), n=infinity) |
| 341 | 0 |
| 342 | sage: n0 = find_root(u(n) - 1e-8 == 0, 22, 1000); n0 |
| 343 | 105.07496210187252 |
| 344 | |
| 345 | Sage example in ./calculus.tex, line 988:: |
| 346 | |
| 347 | sage: taylor((1+arctan(x))**(1/x), x, 0, 3) |
| 348 | 1/16*x^3*e + 1/8*x^2*e - 1/2*x*e + e |
| 349 | |
| 350 | Sage example in ./calculus.tex, line 993:: |
| 351 | |
| 352 | sage: (ln(2*sin(x))).series(x==pi/6, 3) |
| 353 | (sqrt(3))*(-1/6*pi + x) + (-2)*(-1/6*pi + x)^2 |
| 354 | + Order(-1/216*(pi - 6*x)^3) |
| 355 | |
| 356 | Sage example in ./calculus.tex, line 1002:: |
| 357 | |
| 358 | sage: (ln(2*sin(x))).series(x==pi/6, 3).truncate() |
| 359 | -1/18*(pi - 6*x)^2 - 1/6*sqrt(3)*(pi - 6*x) |
| 360 | |
| 361 | Sage example in ./calculus.tex, line 1017:: |
| 362 | |
| 363 | sage: taylor((x**3+x)**(1/3) - (x**3-x)**(1/3), x, infinity, 2) |
| 364 | 2/3/x |
| 365 | |
| 366 | Sage example in ./calculus.tex, line 1041:: |
| 367 | |
| 368 | sage: tan(4*arctan(1/5)).simplify_trig() |
| 369 | 120/119 |
| 370 | sage: tan(pi/4+arctan(1/239)).simplify_trig() |
| 371 | 120/119 |
| 372 | |
| 373 | Sage example in ./calculus.tex, line 1052:: |
| 374 | |
| 375 | sage: f = arctan(x).series(x, 10); f |
| 376 | 1*x + (-1/3)*x^3 + 1/5*x^5 + (-1/7)*x^7 + 1/9*x^9 + Order(x^10) |
| 377 | sage: (16*f.subs(x==1/5) - 4*f.subs(x==1/239)).n(); pi.n() |
| 378 | 3.14159268240440 |
| 379 | 3.14159265358979 |
| 380 | |
| 381 | Sage example in ./calculus.tex, line 1093:: |
| 382 | |
| 383 | sage: k = var('k') |
| 384 | sage: sum(1/k^2, k, 1, infinity),\ |
| 385 | ... sum(1/k^4, k, 1, infinity),\ |
| 386 | ... sum(1/k^5, k, 1, infinity) |
| 387 | (1/6*pi^2, 1/90*pi^4, zeta(5)) |
| 388 | |
| 389 | Sage example in ./calculus.tex, line 1111:: |
| 390 | |
| 391 | sage: s = 2*sqrt(2)/9801*(sum((factorial(4*k)) * (1103+26390*k) / |
| 392 | ... ((factorial(k)) ^ 4 * 396 ^ (4 * k)) for k in (0..11))) |
| 393 | sage: (1/s).n(digits=100) |
| 394 | 3.141592653589793238462643383279502884197169399375105820974... |
| 395 | sage: (pi-1/s).n(digits=100).n() |
| 396 | -4.36415445739398e-96 |
| 397 | |
| 398 | Sage example in ./calculus.tex, line 1139:: |
| 399 | |
| 400 | sage: n = var('n'); u = sin(pi*(sqrt(4*n^2+1)-2*n)) |
| 401 | sage: taylor(u, n, infinity, 3) |
| 402 | 1/4*pi/n - 1/384*(6*pi + pi^3)/n^3 |
| 403 | |
| 404 | Sage example in ./calculus.tex, line 1163:: |
| 405 | |
| 406 | sage: diff(sin(x^2), x) |
| 407 | 2*x*cos(x^2) |
| 408 | sage: function('f', x); function('g', x); diff(f(g(x)), x) |
| 409 | f(x) |
| 410 | g(x) |
| 411 | D[0](f)(g(x))*D[0](g)(x) |
| 412 | sage: diff(ln(f(x)), x) |
| 413 | D[0](f)(x)/f(x) |
| 414 | |
| 415 | Sage example in ./calculus.tex, line 1180:: |
| 416 | |
| 417 | sage: f(x,y) = x*y + sin(x^2) + e^(-x); derivative(f, x) |
| 418 | (x, y) |--> 2*x*cos(x^2) + y - e^(-x) |
| 419 | sage: derivative(f, y) |
| 420 | (x, y) |--> x |
| 421 | |
| 422 | Sage example in ./calculus.tex, line 1195:: |
| 423 | |
| 424 | sage: x, y = var('x, y'); f = ln(x**2+y**2) / 2 |
| 425 | sage: delta = diff(f,x,2) + diff(f,y,2) |
| 426 | sage: delta.simplify_full() |
| 427 | 0 |
| 428 | |
| 429 | Sage example in ./calculus.tex, line 1231:: |
| 430 | |
| 431 | sage: sin(x).integral(x, 0, pi/2) |
| 432 | 1 |
| 433 | sage: integrate(1/(1+x^2), x) |
| 434 | arctan(x) |
| 435 | sage: integrate(1/(1+x^2), x, -infinity, infinity) |
| 436 | pi |
| 437 | sage: integrate(exp(-x**2), x, 0, infinity) |
| 438 | 1/2*sqrt(pi) |
| 439 | |
| 440 | Sage example in ./calculus.tex, line 1241:: |
| 441 | |
| 442 | sage: integrate(exp(-x), x, -infinity, infinity) |
| 443 | Traceback (most recent call last): |
| 444 | ... |
| 445 | ValueError: Integral is divergent. |
| 446 | |
| 447 | Sage example in ./calculus.tex, line 1254:: |
| 448 | |
| 449 | sage: u = var('u'); f = x * cos(u) / (u^2 + x^2) |
| 450 | sage: assume(x>0); f.integrate(u, 0, infinity) |
| 451 | 1/2*pi*e^(-x) |
| 452 | sage: forget(); assume(x<0); f.integrate(u, 0, infinity) |
| 453 | -1/2*pi*e^x |
| 454 | |
| 455 | Sage example in ./calculus.tex, line 1270:: |
| 456 | |
| 457 | sage: integral_numerical(sin(x)/x, 0, 1) # abs tol 1e-12 |
| 458 | (0.94608307036718287, 1.0503632079297086e-14) |
| 459 | sage: g = integrate(exp(-x**2), x, 0, infinity) |
| 460 | sage: g, g.n() # abs tol 1e-12 |
| 461 | (1/2*sqrt(pi), 0.886226925452758) |
| 462 | sage: approx = integral_numerical(exp(-x**2), 0, infinity) |
| 463 | sage: approx # abs tol 1e-12 |
| 464 | (0.88622692545275705, 1.7147744320162414e-08) |
| 465 | sage: approx[0]-g.n() # abs tol 1e-12 |
| 466 | -8.88178419700125e-16 |
| 467 | |
| 468 | Sage example in ./calculus.tex, line 1482:: |
| 469 | |
| 470 | sage: A = matrix(QQ, [[1,2],[3,4]]); A |
| 471 | [1 2] |
| 472 | [3 4] |
| 473 | |
| 474 | Sage example in ./calculus.tex, line 1629:: |
| 475 | |
| 476 | sage: A = matrix(QQ, [[2,4,3],[-4,-6,-3],[3,3,1]]) |
| 477 | sage: A.characteristic_polynomial() |
| 478 | x^3 + 3*x^2 - 4 |
| 479 | sage: A.eigenvalues() |
| 480 | [1, -2, -2] |
| 481 | sage: A.minimal_polynomial().factor() |
| 482 | (x - 1) * (x + 2)^2 |
| 483 | |
| 484 | Sage example in ./calculus.tex, line 1641:: |
| 485 | |
| 486 | sage: A.eigenvectors_right() |
| 487 | [(1, [ |
| 488 | (1, -1, 1) |
| 489 | ], 1), (-2, [ |
| 490 | (1, -1, 0) |
| 491 | ], 2)] |
| 492 | |
| 493 | Sage example in ./calculus.tex, line 1652:: |
| 494 | |
| 495 | sage: A.jordan_form(transformation=True) |
| 496 | ( |
| 497 | [ 1| 0 0] |
| 498 | [--+-----] [ 1 1 1] |
| 499 | [ 0|-2 1] [-1 -1 0] |
| 500 | [ 0| 0 -2], [ 1 0 -1] |
| 501 | ) |
| 502 | |
| 503 | Sage example in ./calculus.tex, line 1686:: |
| 504 | |
| 505 | sage: A = matrix(QQ, [[1,-1/2],[-1/2,-1]]) |
| 506 | sage: A.jordan_form() |
| 507 | Traceback (most recent call last): |
| 508 | ... |
| 509 | RuntimeError: Some eigenvalue does not exist in Rational Field. |
| 510 | |
| 511 | Sage example in ./calculus.tex, line 1695:: |
| 512 | |
| 513 | sage: A = matrix(QQ, [[1,-1/2],[-1/2,-1]]) |
| 514 | sage: A.minimal_polynomial() |
| 515 | x^2 - 5/4 |
| 516 | |
| 517 | Sage example in ./calculus.tex, line 1701:: |
| 518 | |
| 519 | sage: R = QQ[sqrt(5)] |
| 520 | sage: A = A.change_ring(R) |
| 521 | sage: A.jordan_form(transformation=True, subdivide=False) |
| 522 | ( |
| 523 | [ 1/2*sqrt5 0] [ 1 1] |
| 524 | [ 0 -1/2*sqrt5], [-sqrt5 + 2 sqrt5 + 2] |
| 525 | ) |
| 526 | |
| 527 | Sage example in ./calculus.tex, line 1734:: |
| 528 | |
| 529 | sage: K.<sqrt2> = NumberField(x^2 - 2) |
| 530 | sage: L.<sqrt3> = K.extension(x^2 - 3) |
| 531 | sage: A = matrix(L, [[2, sqrt2*sqrt3, sqrt2], \ |
| 532 | ... [sqrt2*sqrt3, 3, sqrt3], \ |
| 533 | ... [sqrt2, sqrt3, 1]]) |
| 534 | sage: A.jordan_form(transformation=True) |
| 535 | ( |
| 536 | [6|0|0] |
| 537 | [-+-+-] |
| 538 | [0|0|0] [ 1 1 0] |
| 539 | [-+-+-] [1/2*sqrt2*sqrt3 0 1] |
| 540 | [0|0|0], [ 1/2*sqrt2 -sqrt2 -sqrt3] |
| 541 | ) |
| 542 | |
| 543 | """ |
| 544 | |
| 545 | """ |
| 546 | Tests extracted from sol/calculus.tex. |
| 547 | |
| 548 | Sage example in ./sol/calculus.tex, line 3:: |
| 549 | |
| 550 | sage: reset() |
| 551 | |
| 552 | Sage example in ./sol/calculus.tex, line 9:: |
| 553 | |
| 554 | sage: n, k = var('n, k'); p = 4; s = [n + 1] |
| 555 | sage: for k in (1..p): |
| 556 | ... s += [factor((((n+1)^(k+1) \ |
| 557 | ... - sum(binomial(k+1, j)\ |
| 558 | ... * s[j] for j in (0..k-1))) / (k+1)))] |
| 559 | ... |
| 560 | sage: s |
| 561 | [n + 1, 1/2*(n + 1)*n, 1/6*(2*n + 1)*(n + 1)*n, |
| 562 | 1/4*(n + 1)^2*n^2, 1/30*(3*n^2 + 3*n - 1)*(2*n + 1)*(n + 1)*n] |
| 563 | |
| 564 | Sage example in ./sol/calculus.tex, line 34:: |
| 565 | |
| 566 | sage: x, h, a = var('x, h, a'); f = function('f') |
| 567 | sage: g(x) = taylor(f(x), x, a, 3) |
| 568 | sage: phi(h) = (g(a+3*h) - 3*g(a+2*h) \ |
| 569 | ... + 3*g(a+h) - g(a)) / h^3 |
| 570 | sage: phi(h).expand() |
| 571 | D[0, 0, 0](f)(a) |
| 572 | |
| 573 | Sage example in ./sol/calculus.tex, line 57:: |
| 574 | |
| 575 | sage: n = 7; x, h, a = var('x h a') |
| 576 | sage: f = function('f') |
| 577 | sage: g(x) = taylor(f(x), x, a, n) |
| 578 | sage: phi(h) = sum(binomial(n,k)*(-1)^(n-k) \ |
| 579 | ... * g(a+k*h) for k in (0..n)) / h^n |
| 580 | sage: phi(h).expand() |
| 581 | D[0, 0, 0, 0, 0, 0, 0](f)(a) |
| 582 | |
| 583 | Sage example in ./sol/calculus.tex, line 82:: |
| 584 | |
| 585 | sage: theta = 12*arctan(1/38) + 20*arctan(1/57) \ |
| 586 | ... + 7*arctan(1/239) + 24*arctan(1/268) |
| 587 | sage: x = tan(theta) |
| 588 | sage: y = x.trig_expand() |
| 589 | sage: y.trig_simplify() |
| 590 | 1 |
| 591 | |
| 592 | Sage example in ./sol/calculus.tex, line 94:: |
| 593 | |
| 594 | sage: M = 12*(1/38)+20*(1/57)+ 7*(1/239)+24*(1/268) |
| 595 | sage: M |
| 596 | 37735/48039 |
| 597 | |
| 598 | Sage example in ./sol/calculus.tex, line 113:: |
| 599 | |
| 600 | sage: x = var('x') |
| 601 | sage: f(x) = taylor(arctan(x), x, 0, 21) |
| 602 | sage: approx = 4 * (12 * f(1/38) + 20 * f(1/57) |
| 603 | ... + 7 * f(1/239) + 24 * f(1/268)) |
| 604 | sage: approx.n(digits = 50); pi.n(digits = 50) |
| 605 | 3.1415926535897932384626433832795028851616168852864 |
| 606 | 3.1415926535897932384626433832795028841971693993751 |
| 607 | sage: approx.n(digits = 50) - pi.n(digits = 50) |
| 608 | 9.6444748591132486785420917537404705292978817080880e-37 |
| 609 | |
| 610 | Sage example in ./sol/calculus.tex, line 143:: |
| 611 | |
| 612 | sage: n = var('n') |
| 613 | sage: phi = lambda x: n*pi+pi/2-arctan(1/x) |
| 614 | sage: x = pi*n |
| 615 | sage: for i in range(4): |
| 616 | ... x = taylor(phi(x), n, oo, 2*i); x |
| 617 | ... |
| 618 | 1/2*pi + pi*n |
| 619 | 1/2*pi + pi*n - 1/(pi*n) + 1/2/(pi*n^2) |
| 620 | 1/2*pi + pi*n - 1/(pi*n) + 1/2/(pi*n^2) |
| 621 | - 1/12*(3*pi^2 + 8)/(pi^3*n^3) + 1/8*(pi^2 + 8)/(pi^3*n^4) |
| 622 | 1/2*pi + pi*n - 1/(pi*n) + 1/2/(pi*n^2) |
| 623 | - 1/12*(3*pi^2 + 8)/(pi^3*n^3) + 1/8*(pi^2 + 8)/(pi^3*n^4) |
| 624 | - 1/240*(15*pi^4 + 240*pi^2 + 208)/(pi^5*n^5) |
| 625 | + 1/96*(3*pi^4 + 80*pi^2 + 208)/(pi^5*n^6) |
| 626 | |
| 627 | Sage example in ./sol/calculus.tex, line 192:: |
| 628 | |
| 629 | sage: h = var('h') |
| 630 | sage: f(x, y) = x * y * (x**2 - y**2) / (x**2 + y**2) |
| 631 | sage: D1f(x, y) = diff(f(x,y), x) |
| 632 | sage: limit((D1f(0,h) - 0) / h, h=0) |
| 633 | -1 |
| 634 | sage: D2f(x, y) = diff(f(x,y), y) |
| 635 | sage: limit((D2f(h,0) - 0) / h, h=0) |
| 636 | 1 |
| 637 | sage: g = plot3d(f(x, y), (x, -3, 3), (y, -3, 3)) |
| 638 | |
| 639 | Sage example in ./sol/calculus.tex, line 230:: |
| 640 | |
| 641 | sage: n, t = var('n, t') |
| 642 | sage: v(n)=(4/(8*n+1)-2/(8*n+4)-1/(8*n+5)-1/(8*n+6))*1/16^n |
| 643 | sage: assume(8*n+1>0) |
| 644 | sage: u(n) = integrate((4*sqrt(2)-8*t^3-4*sqrt(2)*t^4\ |
| 645 | ... -8*t^5) * t^(8*n), t, 0, 1/sqrt(2)) |
| 646 | sage: (u(n)-v(n)).simplify_full() |
| 647 | 0 |
| 648 | |
| 649 | Sage example in ./sol/calculus.tex, line 258:: |
| 650 | |
| 651 | sage: t = var('t') |
| 652 | sage: J = integrate((4*sqrt(2)-8*t^3 \ |
| 653 | ... - 4*sqrt(2)*t^4-8*t^5)\ |
| 654 | ... / (1-t^8), t, 0, 1/sqrt(2)) |
| 655 | sage: J.simplify_full() |
| 656 | pi + 2*log(sqrt(2) + 1) + 2*log(sqrt(2) - 1) |
| 657 | |
| 658 | Sage example in ./sol/calculus.tex, line 272:: |
| 659 | |
| 660 | sage: ln(exp(J).simplify_log()) |
| 661 | pi |
| 662 | |
| 663 | Sage example in ./sol/calculus.tex, line 281:: |
| 664 | |
| 665 | sage: l = sum(v(n) for n in (0..40)); l.n(digits=60) |
| 666 | 3.14159265358979323846264338327950288419716939937510581474759 |
| 667 | sage: pi.n(digits=60) |
| 668 | 3.14159265358979323846264338327950288419716939937510582097494 |
| 669 | sage: print("%e" % (l-pi).n(digits=60)) |
| 670 | -6.227358e-54 |
| 671 | |
| 672 | Sage example in ./sol/calculus.tex, line 302:: |
| 673 | |
| 674 | sage: X = var('X') |
| 675 | sage: ps = lambda f,g : integral(f * g, X, -pi, pi) |
| 676 | sage: n = 5; Q = sin(X) |
| 677 | sage: a, a0, a1, a2, a3, a4, a5 = var('a a0 a1 a2 a3 a4 a5') |
| 678 | sage: a= [a0, a1, a2, a3, a4, a5] |
| 679 | sage: P = sum(a[k] * X^k for k in (0..n)) |
| 680 | sage: equ = [ps(P - Q, X^k) for k in (0..n)] |
| 681 | sage: sol = solve(equ, a) |
| 682 | sage: P = sum(sol[0][k].rhs() * X^k for k in (0..n)) |
| 683 | sage: g = plot(P,X,-6,6,color='red') + plot(Q,X,-6,6,color='blue') |
| 684 | |
| 685 | Sage example in ./sol/calculus.tex, line 353:: |
| 686 | |
| 687 | sage: p, e = var('p e') |
| 688 | sage: theta1, theta2, theta3 = var('theta1 theta2 theta3') |
| 689 | sage: r(theta) = p / (1-e * cos(theta)) |
| 690 | sage: r1 = r(theta1); r2 = r(theta2); r3 = r(theta3) |
| 691 | sage: R1 = vector([r1 * cos(theta1), r1 * sin(theta1), 0]) |
| 692 | sage: R2 = vector([r2 * cos(theta2), r2 * sin(theta2), 0]) |
| 693 | sage: R3 = vector([r3 * cos(theta3), r3 * sin(theta3), 0]) |
| 694 | |
| 695 | Sage example in ./sol/calculus.tex, line 365:: |
| 696 | |
| 697 | sage: D = R1.cross_product(R2) + R2.cross_product(R3) \ |
| 698 | ... + R3.cross_product(R1) |
| 699 | sage: i = vector([1, 0, 0]) |
| 700 | sage: S = (r1 - r3) * R2 + (r3 - r2) * R1 + (r2 - r1) * R3 |
| 701 | sage: V = S + e * i.cross_product(D) |
| 702 | sage: map(lambda x:x.simplify_full(), V) |
| 703 | [0, 0, 0] |
| 704 | |
| 705 | Sage example in ./sol/calculus.tex, line 390:: |
| 706 | |
| 707 | sage: N = r3 * R1.cross_product(R2) + r1 * R2.cross_product(R3)\ |
| 708 | ... + r2 * R3.cross_product(R1) |
| 709 | sage: W = p * S + e * i.cross_product(N) |
| 710 | sage: print(map(lambda x:x.simplify_full(), W)) |
| 711 | [0, 0, 0] |
| 712 | |
| 713 | Sage example in ./sol/calculus.tex, line 409:: |
| 714 | |
| 715 | sage: R1=vector([0,1.,0]);R2=vector([2.,2.,0]);R3=vector([3.5,0,0]) |
| 716 | sage: r1 = R1.norm(); r2 = R2.norm(); r3 = R3.norm() |
| 717 | sage: D = R1.cross_product(R2) + R2.cross_product(R3) \ |
| 718 | ... + R3.cross_product(R1) |
| 719 | sage: S = (r1 - r3) * R2 + (r3 - r2) * R1 + (r2 - r1) * R3 |
| 720 | sage: V = S + e * i.cross_product(D) |
| 721 | sage: N = r3 * R1.cross_product(R2) + r1 * R2.cross_product(R3) \ |
| 722 | ... + r2 * R3.cross_product(R1) |
| 723 | sage: i = vector([1, 0, 0]); W = p * S + e * i.cross_product(N) |
| 724 | sage: e = S.norm() / D.norm(); p = N.norm() / D.norm() |
| 725 | sage: a = p/(1-e^2); c = a * e; b = sqrt(a^2 - c^2) |
| 726 | sage: X = S.cross_product(D); i = X / X.norm() |
| 727 | sage: phi = atan2(i[1],i[0]) * 180 / pi.n() |
| 728 | sage: print("%.3f %.3f %.3f %.3f %.3f %.3f" % (a, b, c, e, p, phi)) |
| 729 | 2.360 1.326 1.952 0.827 0.746 17.917 |
| 730 | |
| 731 | Sage example in ./sol/calculus.tex, line 445:: |
| 732 | |
| 733 | sage: A = matrix(QQ, [[2, -3, 2, -12, 33], |
| 734 | ... [ 6, 1, 26, -16, 69], |
| 735 | ... [10, -29, -18, -53, 32], |
| 736 | ... [2, 0, 8, -18, 84]]) |
| 737 | sage: A.right_kernel() |
| 738 | Vector space of degree 5 and dimension 2 over Rational Field |
| 739 | Basis matrix: |
| 740 | [ 1 0 -7/34 5/17 1/17] |
| 741 | [ 0 1 -3/34 -10/17 -2/17] |
| 742 | |
| 743 | Sage example in ./sol/calculus.tex, line 463:: |
| 744 | |
| 745 | sage: H = A.echelon_form() |
| 746 | |
| 747 | Sage example in ./sol/calculus.tex, line 484:: |
| 748 | |
| 749 | sage: A.column_space() |
| 750 | Vector space of degree 4 and dimension 3 over Rational Field |
| 751 | Basis matrix: |
| 752 | [ 1 0 0 1139/350] |
| 753 | [ 0 1 0 -9/50] |
| 754 | [ 0 0 1 -12/35] |
| 755 | |
| 756 | Sage example in ./sol/calculus.tex, line 496:: |
| 757 | |
| 758 | sage: S.<x, y, z, t>=QQ[] |
| 759 | sage: C = matrix(S, 4, 1, [x, y, z, t]) |
| 760 | sage: B = block_matrix([A, C], ncols=2) |
| 761 | sage: C = B.echelon_form() |
| 762 | sage: C[3,5]*350 |
| 763 | -1139*x + 63*y + 120*z + 350*t |
| 764 | |
| 765 | Sage example in ./sol/calculus.tex, line 511:: |
| 766 | |
| 767 | sage: K = A.kernel(); K |
| 768 | Vector space of degree 4 and dimension 1 over Rational Field |
| 769 | Basis matrix: |
| 770 | [ 1 -63/1139 -120/1139 -350/1139] |
| 771 | |
| 772 | Sage example in ./sol/calculus.tex, line 519:: |
| 773 | |
| 774 | sage: matrix(K.0).right_kernel() |
| 775 | Vector space of degree 4 and dimension 3 over Rational Field |
| 776 | Basis matrix: |
| 777 | [ 1 0 0 1139/350] |
| 778 | [ 0 1 0 -9/50] |
| 779 | [ 0 0 1 -12/35] |
| 780 | |
| 781 | Sage example in ./sol/calculus.tex, line 533:: |
| 782 | |
| 783 | sage: A = matrix(QQ, [[-2, 1, 1], [8, 1, -5], [4, 3, -3]]) |
| 784 | sage: C = matrix(QQ, [[1, 2, -1], [2, -1, -1], [-5, 0, 3]]) |
| 785 | |
| 786 | Sage example in ./sol/calculus.tex, line 540:: |
| 787 | |
| 788 | sage: B = C.solve_left(A); B |
| 789 | [ 0 -1 0] |
| 790 | [ 2 3 0] |
| 791 | [ 2 1 0] |
| 792 | |
| 793 | Sage example in ./sol/calculus.tex, line 548:: |
| 794 | |
| 795 | sage: C.left_kernel() |
| 796 | Vector space of degree 3 and dimension 1 over Rational Field |
| 797 | Basis matrix: |
| 798 | [1 2 1] |
| 799 | |
| 800 | Sage example in ./sol/calculus.tex, line 560:: |
| 801 | |
| 802 | sage: x, y, z = var('x, y, z'); v = matrix([[1, 2, 1]]) |
| 803 | sage: B = B+(x*v).stack(y*v).stack(z*v); B |
| 804 | [ x 2*x - 1 x] |
| 805 | [ y + 2 2*y + 3 y] |
| 806 | [ z + 2 2*z + 1 z] |
| 807 | |
| 808 | Sage example in ./sol/calculus.tex, line 568:: |
| 809 | |
| 810 | sage: A == B*C |
| 811 | True |
| 812 | |
| 813 | """ |
| 814 | # This file was *autogenerated* from the file calculus_doctest.sage. |
| 815 | from sage.all_cmdline import * # import sage library |