Ticket #10983: trac_10983_doctests_from_french_book.patch

File trac_10983_doctests_from_french_book.patch, 40.0 KB (added by zimmerma, 4 years ago)

Combined patch

  • new file sage/tests/french_book/calculus_doctest.py

    # HG changeset patch
    # User Paul Zimmermann <zimmerma@loria.fr>
    # Date 1377244749 -7200
    # Node ID 0a0f2de2b210d632cd4378b9a1af7c18446d7ef4
    # Parent  8c4989a7e8d20a16ad1c464620ee616c4cde719a
    #10983: new doctests for french book about Sage
    
    diff --git a/sage/tests/french_book/calculus_doctest.py b/sage/tests/french_book/calculus_doctest.py
    new file mode 100644
    - +  
     1## -*- encoding: utf-8 -*-
     2"""
     3Doctests from French Sage book
     4Test file for chapter "Analyse et algèbre avec Sage" ("Calculus and
     5algebra with Sage")
     6
     7Tests extracted from ./calculus.tex.
     8
     9Sage example in ./calculus.tex, line 37::
     10
     11  sage: bool(x^2 + 3*x + 1 == (x+1)*(x+2))
     12  False
     13
     14Sage 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
     27Sage 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
     33Sage 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
     39Sage 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
     46Sage 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
     54Sage 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
     61Sage 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
     66Sage 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
     71Sage example in ./calculus.tex, line 254::
     72
     73  sage: (x^x/x).simplify()
     74  x^(x - 1)
     75
     76Sage 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
     81Sage 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
     87Sage 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
     94Sage 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
     100Sage 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
     107Sage 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
     116Sage example in ./calculus.tex, line 420::
     117
     118  sage: a = var('a')
     119  sage: c = (a+1)^2 - (a^2+2*a+1)
     120
     121Sage example in ./calculus.tex, line 425::
     122
     123  sage: eq =  c * x == 0
     124
     125Sage 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
     132Sage example in ./calculus.tex, line 437::
     133
     134  sage: solve(eq, x)
     135  [x == x]
     136
     137Sage example in ./calculus.tex, line 444::
     138
     139  sage: expand(c)
     140  0
     141
     142Sage 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
     149Sage example in ./calculus.tex, line 460::
     150
     151  sage: c.simplify_trig()
     152  0
     153  sage: c.is_zero()
     154  True
     155
     156Sage 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
     162Sage 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
     169Sage 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
     175Sage 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
     181Sage example in ./calculus.tex, line 544::
     182
     183  sage: solve(x^2-1, x, solution_dict=True)
     184  [{x: -1}, {x: 1}]
     185
     186Sage 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
     191Sage 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
     196Sage 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
     201Sage 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
     208Sage 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
     214Sage example in ./calculus.tex, line 605::
     215
     216  sage: find_root(expr, 0.1, pi)
     217  2.0943951023931957
     218
     219Sage 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
     226Sage 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
     236Sage example in ./calculus.tex, line 658::
     237
     238  sage: (x^3+2*x+1).roots(x, ring=RR)
     239  [(-0.453397651516404, 1)]
     240
     241Sage 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
     248Sage 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
     253Sage 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
     259Sage 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
     265Sage 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
     271Sage 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
     279Sage 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
     285Sage 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
     291Sage 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
     298Sage 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
     306Sage 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
     313Sage 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
     327Sage example in ./calculus.tex, line 914::
     328
     329  sage: plot(u(x), x, 1, 40)
     330
     331Sage 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
     338Sage 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
     345Sage 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
     350Sage 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
     356Sage 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
     361Sage 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
     366Sage 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
     373Sage 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
     381Sage 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
     389Sage 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
     398Sage 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
     404Sage 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
     415Sage 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
     422Sage 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
     429Sage 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
     440Sage 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
     447Sage 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
     455Sage 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
     468Sage example in ./calculus.tex, line 1482::
     469
     470  sage: A = matrix(QQ, [[1,2],[3,4]]); A
     471  [1 2]
     472  [3 4]
     473
     474Sage 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
     484Sage 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
     493Sage 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
     503Sage 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
     511Sage 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
     517Sage 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
     527Sage 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"""
     546Tests extracted from sol/calculus.tex.
     547
     548Sage example in ./sol/calculus.tex, line 3::
     549
     550  sage: reset()
     551
     552Sage 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
     564Sage 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
     573Sage 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
     583Sage 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
     592Sage 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
     598Sage 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
     610Sage 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
     627Sage 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
     639Sage 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
     649Sage 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
     658Sage example in ./sol/calculus.tex, line 272::
     659
     660  sage: ln(exp(J).simplify_log())
     661  pi
     662
     663Sage 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
     672Sage 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
     685Sage 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
     695Sage 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
     705Sage 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
     713Sage 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
     731Sage 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
     743Sage example in ./sol/calculus.tex, line 463::
     744
     745  sage: H = A.echelon_form()
     746
     747Sage 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
     756Sage 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
     765Sage 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
     772Sage 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
     781Sage 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
     786Sage 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
     793Sage 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
     800Sage 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
     808Sage 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.
     815from sage.all_cmdline import *   # import sage library
  • new file sage/tests/french_book/graphique_doctest.py

    diff --git a/sage/tests/french_book/graphique_doctest.py b/sage/tests/french_book/graphique_doctest.py
    new file mode 100644
    - +  
     1## -*- encoding: utf-8 -*-
     2"""
     3Doctests from French Sage book
     4Test file for chapter "Premiers pas" ("First Steps")
     5
     6Tests extracted from ./graphique.tex.
     7
     8Sage example in ./graphique.tex, line 7::
     9
     10  sage: reset()
     11
     12Sage example in ./graphique.tex, line 34::
     13
     14  sage: plot(x * sin(1/x), x, -2, 2, plot_points=500)
     15
     16Sage example in ./graphique.tex, line 94::
     17
     18  sage: def p(x, n):
     19  ...       return(taylor(sin(x), x, 0, n))
     20  ...
     21  sage: xmax = 15 ; n = 15
     22  sage: g = plot(sin(x), x, -xmax, xmax)
     23  sage: for d in range(n):
     24  ...     g += plot(p(x, 2 * d + 1), x, -xmax, xmax,\
     25  ...       color=(1.7*d/(2*n), 1.5*d/(2*n), 1-3*d/(4*n)))
     26  ...
     27  sage: g.show(ymin=-2, ymax=2)
     28
     29Sage example in ./graphique.tex, line 151::
     30
     31  sage: f2(x) = 1; f1(x) = -1
     32  sage: f = Piecewise([[(-pi,0),f1],[(0,pi),f2]])
     33  sage: S = f.fourier_series_partial_sum(20,pi); S
     34  4/19*sin(19*x)/pi + 4/17*sin(17*x)/pi + 4/15*sin(15*x)/pi +
     35  4/13*sin(13*x)/pi + 4/11*sin(11*x)/pi + 4/9*sin(9*x)/pi +
     36  4/7*sin(7*x)/pi + 4/5*sin(5*x)/pi + 4/3*sin(3*x)/pi +
     37  4*sin(x)/pi
     38  sage: g = plot(S, x, -8, 8, color='blue')
     39  sage: scie(x) = x - 2 * pi * floor((x + pi) / (2 * pi))
     40  sage: g += plot(scie(x) / abs(scie(x)), x, -8, 8, color='red')
     41  sage: g
     42
     43Sage example in ./graphique.tex, line 216::
     44
     45  sage: t = var('t')
     46  sage: x = cos(t) + cos(7*t)/2 + sin(17*t)/3
     47  sage: y = sin(t) + sin(7*t)/2 + cos(17*t)/3
     48  sage: g = parametric_plot( (x, y), (t, 0, 2*pi))
     49  sage: g.show(aspect_ratio=1)
     50
     51Sage example in ./graphique.tex, line 255::
     52
     53  sage: t = var('t'); e, n = 2, 20/19
     54  sage: g1 = polar_plot(1+e*cos(n*t),(t,0,n*36*pi),plot_points=5000)
     55  sage: e, n = 1/3, 20/19
     56  sage: g2 = polar_plot(1+e*cos(n*t),(t,0,n*36*pi),plot_points=5000)
     57  sage: g1.show(aspect_ratio=1); g2.show(aspect_ratio=1) # long time
     58
     59Sage example in ./graphique.tex, line 369::
     60
     61  sage: bar_chart([randrange(15) for i in range(20)])
     62  sage: bar_chart([x^2 for x in range(1,20)], width=0.2)
     63
     64Sage example in ./graphique.tex, line 403::
     65
     66  sage: liste = [10 + floor(10*sin(i)) for i in range(100)]
     67  sage: bar_chart(liste)
     68  sage: finance.TimeSeries(liste).plot_histogram(bins=20)
     69
     70Sage example in ./graphique.tex, line 506::
     71
     72  sage: from random import *
     73  sage: n, l, x, y = 10000, 1, 0, 0; p = [[0, 0]]
     74  sage: for k in range(n): # long time
     75  ...       theta = (2 * pi * random()).n(digits=5)
     76  ...       x, y = x + l * cos(theta), y + l * sin(theta)
     77  ...       p.append([x, y])
     78  ...
     79  sage: g1 = line([p[n], [0, 0]], color='red', thickness=2) # long time
     80  sage: g1 += line(p, thickness=.4); g1.show(aspect_ratio=1) # long time
     81
     82Sage example in ./graphique.tex, line 546::
     83
     84  sage: length = 200; n = var('n')
     85  sage: u = lambda n: n * sqrt(2)
     86  sage: z = lambda n: exp(2 * I * pi * u(n)).n()
     87  sage: vertices = [CC(0, 0)]
     88  sage: for n in range(1, length):
     89  ...       vertices.append(vertices[n - 1] + CC(z(n)))
     90  ...
     91  sage: line(vertices).show(aspect_ratio=1)
     92
     93Sage example in ./graphique.tex, line 672::
     94
     95  sage: x = var('x'); y = function('y')
     96  sage: DE = x*diff(y(x), x) == 2*y(x) + x^3
     97  sage: desolve(DE, [y(x),x])
     98  (c + x)*x^2
     99  sage: sol = []
     100  sage: for i in srange(-2, 2, 0.2):
     101  ...       sol.append(desolve(DE, [y(x), x], ics=[1, i]))
     102  ...       sol.append(desolve(DE, [y(x), x], ics=[-1, i]))
     103  ...
     104  sage: g = plot(sol, x, -2, 2)
     105  sage: y = var('y')
     106  sage: g += plot_vector_field((x, 2*y+x^3), (x,-2,2), (y,-1,1))
     107  sage: g.show(ymin=-1, ymax=1)
     108
     109Sage example in ./graphique.tex, line 694::
     110
     111  sage: x = var('x'); y = function('y')
     112  sage: DE = x*diff(y(x), x) == 2*y(x) + x^3
     113  sage: g = Graphics()             # crée un graphique vide
     114  sage: for i in srange(-2, 2, 0.2):
     115  ...       g += line(desolve_rk4(DE, y(x), ics=[1, i],\
     116  ...                         step=0.05, end_points=[0,2]))
     117  ...       g += line(desolve_rk4(DE, y(x), ics=[-1, i],\
     118  ...                         step=0.05, end_points=[-2,0]))
     119  ...
     120  sage: x, y = var('x, y')
     121  sage: g += plot_vector_field((x,2*y+x^3), (x,-2,2), (y,-1,1))
     122  sage: g.show(ymin=-1, ymax=1)
     123
     124Sage example in ./graphique.tex, line 776::
     125
     126  sage: import scipy; from scipy import integrate
     127  sage: f = lambda y, t: - cos(y * t)
     128  sage: t = srange(0, 5, 0.1); p = Graphics()
     129  sage: for k in srange(0, 10, 0.15):
     130  ...         y = integrate.odeint(f, k, t)
     131  ...         p += line(zip(t, flatten(y)))
     132  ...
     133  sage: t = srange(0, -5, -0.1); q = Graphics()
     134  sage: for k in srange(0, 10, 0.15):
     135  ...         y = integrate.odeint(f, k, t)
     136  ...         q += line(zip(t, flatten(y)))
     137  ...
     138  sage: y = var('y')
     139  sage: v = plot_vector_field((1, -cos(x*y)), (x,-5,5), (y,-2,11))
     140  sage: g = p + q + v; g.show()
     141
     142Sage example in ./graphique.tex, line 850::
     143
     144  sage: import scipy; from scipy import integrate
     145  sage: a, b, c, d = 1., 0.1, 1.5, 0.75
     146  sage: def dX_dt(X, t=0):   # Renvoie l'augmentation des populations
     147  ...       return [a*X[0] - b*X[0]*X[1], -c*X[1] + d*b*X[0]*X[1]]
     148  ...
     149  sage: t = srange(0, 15, .01)                     # échelle de temps
     150  sage: X0 = [10, 5]  # conditions initiales : 10 lapins et 5 renards
     151  sage: X = integrate.odeint(dX_dt, X0, t)     # résolution numérique
     152  sage: lapins, renards =  X.T         # raccourcis de  X.transpose()
     153  sage: p = line(zip(t, lapins), color='red') # tracé du nb de lapins
     154  sage: p += text("Lapins",(12,37), fontsize=10, color='red')
     155  sage: p += line(zip(t, renards), color='blue')# idem pr les renards
     156  sage: p += text("Renards",(12,7), fontsize=10, color='blue')
     157  sage: p.axes_labels(["temps", "population"]); p.show(gridlines=True)
     158  ...
     159  sage: ### Deuxième graphique :
     160  sage: n = 11;  L = srange(6, 18, 12 / n); R = srange(3, 9, 6 / n)
     161  sage: CI = zip(L, R)                # liste des conditions initiales
     162  sage: def g(x,y):
     163  ...       v = vector(dX_dt([x, y]))  # pour un tracé plus lisible,
     164  ...       return v/v.norm()          # on norme le champ de vecteurs
     165  ...
     166  sage: x, y = var('x, y')
     167  sage: q = plot_vector_field(g(x, y), (x, 0, 60), (y, 0, 36))
     168  sage: for j in range(n):
     169  ...       X = integrate.odeint(dX_dt, CI[j], t)        # résolution
     170  ...       q += line(X, color=hue(.8-float(j)/(1.8*n))) #  graphique
     171  ...
     172  sage: q.axes_labels(["lapins","renards"]); q.show()
     173
     174Sage example in ./graphique.tex, line 1016::
     175
     176  sage: x, y, t = var('x, y, t')
     177  sage: alpha(t) = 1; beta(t) = t / 2; gamma(t) = t + t^3 / 8
     178  sage: env = solve([alpha(t) * x + beta(t) * y == gamma(t),\
     179  ...       diff(alpha(t), t) * x + diff(beta(t), t) * y == \
     180  ...       diff(gamma(t), t)], [x,y])
     181
     182Sage example in ./graphique.tex, line 1037::
     183
     184  sage: f(x) = x^2 / 4
     185  sage: p = plot(f, -8, 8, rgbcolor=(0.2,0.2,0.4)) # la parabole
     186  sage: for u in srange(0, 8, 0.1):     # normales à la parabole
     187  ...      p += line([[u, f(u)], [-8*u, f(u) + 18]], thickness=.3)
     188  ...      p += line([[-u, f(u)], [8*u, f(u) + 18]], thickness=.3)
     189  ...
     190  sage: p += parametric_plot((env[0][0].rhs(),env[0][1].rhs()),\
     191  ...      (t, -8, 8),color='red')        # trace la développée
     192  sage: p.show(xmin=-8, xmax=8, ymin=-1, ymax=12, aspect_ratio=1)
     193
     194Sage example in ./graphique.tex, line 1099::
     195
     196  sage: t = var('t'); p = 2
     197  sage: x(t) = t; y(t) = t^2 / (2 * p); f(t) = [x(t), y(t)]
     198  sage: df(t) = [x(t).diff(t), y(t).diff(t)]
     199  sage: d2f(t) = [x(t).diff(t, 2), y(t).diff(t, 2)]
     200  sage: T(t) = [df(t)[0] / df(t).norm(), df[1](t) / df(t).norm()]
     201  sage: N(t) = [-df(t)[1] / df(t).norm(), df[0](t) / df(t).norm()]
     202  sage: R(t) = (df(t).norm())^3 /                              \
     203  ...          (df(t)[0]*d2f(t)[1] - df(t)[1]*d2f(t)[0])
     204  sage: Omega(t) = [f(t)[0] + R(t)*N(t)[0], f(t)[1] + R(t)*N(t)[1]]
     205  sage: g = parametric_plot(f(t), (t,-8,8), color='green',thickness=2)
     206  sage: for u in srange(.4, 4, .2):
     207  ...       g += line([f(t=u), Omega(t=u)], color='red', alpha = .5)
     208  ...       g += circle(Omega(t=u), R(t=u), color='blue')
     209  ...
     210  sage: g.show(aspect_ratio=1,xmin=-12,xmax=7,ymin=-3,ymax=12)
     211
     212Sage example in ./graphique.tex, line 1200::
     213
     214  sage: u, v = var('u, v')
     215  sage: h = lambda u,v: u^2 + 2*v^2
     216  sage: f = plot3d(h, (u,-1,1), (v,-1,1), aspect_ratio=[1,1,1])
     217
     218Sage example in ./graphique.tex, line 1241::
     219
     220  sage: f(x, y) = x^2 * y / (x^4 + y^2)
     221  sage: t, theta = var('t, theta')
     222  sage: limit(f(t * cos(theta), t * sin(theta)) / t, t=0)
     223  cos(theta)^2/sin(theta)
     224
     225Sage example in ./graphique.tex, line 1252::
     226
     227  sage: solve(f(x,y) == 1/2, y)
     228  [y == x^2]
     229  sage: a = var('a'); h = f(x, a*x^2).simplify_rational(); h
     230  a/(a^2 + 1)
     231
     232Sage example in ./graphique.tex, line 1261::
     233
     234  sage: plot(h, a, -4, 4)
     235
     236Sage example in ./graphique.tex, line 1288::
     237
     238  sage: p = plot3d(f(x,y),(x,-2,2),(y,-2,2),plot_points=[150,150])
     239
     240Sage example in ./graphique.tex, line 1311::
     241
     242  sage: for i in range(1,4):
     243  ...    p += plot3d(-0.5 + i / 4, (x, -2, 2), (y, -2, 2),\
     244  ...                 color=hue(i / 10), opacity=.1)
     245
     246Sage example in ./graphique.tex, line 1321::
     247
     248  sage: x, y, z = var('x, y, z'); a = 1
     249  sage: h = lambda x, y, z:(a^2 + x^2 + y^2)^2 - 4*a^2*x^2-z^4
     250  sage: f = implicit_plot3d(h, (x,-3,3), (y,-3,3), (z,-2,2),\
     251  ...                    plot_points=100, adaptative=True)
     252
     253Sage example in ./graphique.tex, line 1361::
     254
     255  sage: g1 = line3d([(-10*cos(t)-2*cos(5*t)+15*sin(2*t),\
     256  ...      -15*cos(2*t)+10*sin(t)-2*sin(5*t),\
     257  ...      10*cos(3*t)) for t in srange(0,6.4,.1)],radius=.5)
     258
     259"""
     260
     261"""
     262Tests extracted from ./sol/graphiques.tex.
     263
     264Sage example in ./sol/graphiques.tex, line 3::
     265
     266  sage: reset()
     267
     268Sage example in ./sol/graphiques.tex, line 9::
     269
     270  sage: t = var('t'); liste = [a + cos(t) for a in srange(0, 2, 0.1)]
     271  sage: g = polar_plot(liste, (t, 0, 2 * pi)); g.show(aspect_ratio=1)
     272
     273Sage example in ./sol/graphiques.tex, line 36::
     274
     275  sage: f = lambda x: abs(x**2 - 1/4)
     276  sage: def liste_pts(u0, n):
     277  ...       u = u0; liste = [[u0,0]]
     278  ...       for k in range(n):
     279  ...           v, u = u, f(u)
     280  ...           liste.extend([[v,u], [u,u]])
     281  ...       return(liste)
     282  ...
     283  sage: g = line(liste_pts(1.1, 8), rgbcolor = (.9,0,0))
     284  sage: g += line(liste_pts(-.4, 8), rgbcolor = (.01,0,0))
     285  sage: g += line(liste_pts(1.3, 3), rgbcolor = (.5,0,0))
     286  sage: g += plot(f, -1, 3, rgbcolor = 'blue')
     287  sage: g += plot(x, -1, 3, rgbcolor = 'green')
     288  sage: g.show(aspect_ratio=1, ymin = -.2, ymax = 3)
     289
     290Sage example in ./sol/graphiques.tex, line 71::
     291
     292  sage: x = var('x'); y = function('y')
     293  sage: DE = x^2 * diff(y(x), x) -  y(x) == 0
     294  sage: desolve(DE, [y(x),x])
     295  c*e^(-1/x)
     296  sage: g = plot([c*e^(-1/x) for c in srange(-8, 8, 0.4)], (x, -3, 3))
     297  sage: y = var('y')
     298  sage: g += plot_vector_field((x^2, y), (x,-3,3), (y,-5,5))
     299  sage: g.show(ymin = -5, ymax = 5)
     300
     301Sage example in ./sol/graphiques.tex, line 103::
     302
     303  sage: from sage.calculus.desolvers import desolve_system_rk4
     304  sage: f = lambda x, y:[a*x-b*x*y,-c*y+d*b*x*y]
     305  sage: x,y,t = var('x y t')
     306  sage: a, b, c, d = 1., 0.1, 1.5, 0.75
     307  sage: P = desolve_system_rk4(f(x,y),[x,y],\
     308  ...          ics=[0,10,5],ivar=t,end_points=15)
     309  sage: Ql = [ [i,j] for i,j,k in P]
     310  sage: p = line(Ql,color='red')
     311  sage: p += text("Lapins",(12,37),fontsize=10,color='red')
     312  sage: Qr = [ [i,k] for i,j,k in P]
     313  sage: p += line(Qr,color='blue')
     314  sage: p += text("Renards",(12,7),fontsize=10,color='blue')
     315  sage: p.axes_labels(["temps","population"])
     316  sage: p.show(gridlines=True)
     317  sage: ### Deuxième graphique
     318  sage: n = 10;  L = srange(6, 18, 12 / n); R = srange(3, 9, 6 / n)
     319  sage: def g(x,y):
     320  ...       v = vector(f(x, y))
     321  ...       return v/v.norm()
     322  ...
     323  sage: q = plot_vector_field(g(x, y), (x, 0, 60), (y, 0, 36))
     324  sage: for j in range(n): # long time
     325  ...       P = desolve_system_rk4(f(x,y),[x,y],
     326  ...              ics=[0,L[j],R[j]],ivar=t,end_points=15)
     327  ...       Q = [ [j,k] for i,j,k in P]
     328  ...       q += line(Q, color=hue(.8-j/(2*n)))
     329  ...
     330  sage: q.axes_labels(["nombre de lapins","nombre de renards"])
     331  sage: q.show()
     332
     333Sage example in ./sol/graphiques.tex, line 151::
     334
     335  sage: import scipy; from scipy import integrate
     336  sage: def dX_dt(X, t=0):
     337  ...       return [X[1], 0.5*X[1] - X[0] - X[1]**3]
     338  ...
     339  sage: t = srange(0, 40, 0.01);  x0 = srange(-2, 2, 0.1)
     340  sage: y0 = 2.5
     341  sage: CI = [[i, y0] for i in x0] + [[i, -y0] for i in x0]
     342  sage: def g(x,y):
     343  ...       v = vector(dX_dt([x, y]))
     344  ...       return v/v.norm()
     345  ...
     346  sage: x, y = var('x, y')
     347  sage: q = plot_vector_field(g(x, y), (x, -3, 3), (y, -y0, y0))
     348  sage: for j in xrange(len(CI)): # long time
     349  ...       X = integrate.odeint(dX_dt, CI[j], t)
     350  ...       q += line(X, color=(1.7*j/(4*n),1.5*j/(4*n),1-3*j/(8*n)))
     351  ...
     352  sage: X = integrate.odeint(dX_dt, [0.01,0], t)
     353  sage: q += line(X, color = 'red'); q.show()
     354
     355Sage example in ./sol/graphiques.tex, line 201::
     356
     357  sage: import scipy
     358  sage: from scipy import integrate
     359  sage: t = srange(0, 40, 0.2)
     360  sage: n = 35
     361  sage: CI_cart = [[4, .2 * i] for i in range(n)]
     362  sage: CI = map(lambda x:[sqrt(x[0]**2+x[1]**2),\
     363  ...        pi - arctan(x[1] / x[0])], CI_cart)
     364  sage: alpha = [0.1, 0.5, 1, 1.25]
     365  sage: for a in alpha:                               # long time
     366  ...       dX_dt = lambda X, t=0: [cos(X[1])*(1-1/X[0]^2), \
     367  ...               -sin(X[1]) * (1/X[0]+1/X[0]^3) + 2*a/X[0]^2]
     368  ...       q = circle((0, 0), 1, fill=True, rgbcolor='purple')
     369  ...       for j in range(n):
     370  ...           X = integrate.odeint(dX_dt, CI[j], t)
     371  ...           Y = [[u[0]*cos(u[1]), u[0]*sin(u[1])] for u in X]
     372  ...           q += line(Y, xmin = -4, xmax = 4, color='blue')
     373  ...       q.show(aspect_ratio = 1, axes = False)
     374  ...
     375
     376"""
     377
     378from sage.all_cmdline import *   # import sage library
  • new file sage/tests/french_book/premierspas_doctest.py

    diff --git a/sage/tests/french_book/premierspas_doctest.py b/sage/tests/french_book/premierspas_doctest.py
    new file mode 100644
    - +  
     1## -*- encoding: utf-8 -*-
     2"""
     3Doctests from French Sage book
     4Test file for chapter "Premiers pas" ("First Steps")
     5
     6Tests extracted from ./premierspas.tex.
     7
     8Sage example in ./premierspas.tex, line 308::
     9
     10  sage: 1+1
     11  2
     12
     13Sage example in ./premierspas.tex, line 340::
     14
     15  sage: ( 1 + 2 * (3 + 5) ) * 2
     16  34
     17
     18Sage example in ./premierspas.tex, line 347::
     19
     20  sage: 2^3
     21  8
     22  sage: 2**3
     23  8
     24
     25Sage example in ./premierspas.tex, line 354::
     26
     27  sage: 20/6
     28  10/3
     29
     30Sage example in ./premierspas.tex, line 381::
     31
     32  sage: 20.0 / 14
     33  1.42857142857143
     34
     35Sage example in ./premierspas.tex, line 399::
     36
     37  sage: numerical_approx(20/14, digits=60)
     38  1.42857142857142857142857142857142857142857142857142857142857
     39
     40Sage example in ./premierspas.tex, line 439::
     41
     42  sage: 20 // 6
     43  3
     44  sage: 20 % 6
     45  2
     46
     47Sage example in ./premierspas.tex, line 459::
     48
     49  sage: factor(2^(2^5)+1)
     50  641 * 6700417
     51
     52Sage example in ./premierspas.tex, line 548::
     53
     54  sage: sin(pi)
     55  0
     56  sage: tan(pi/3)
     57  sqrt(3)
     58  sage: arctan(1)
     59  1/4*pi
     60  sage: exp(2 * I * pi)
     61  1
     62
     63Sage example in ./premierspas.tex, line 559::
     64
     65  sage: arccos(sin(pi/3))
     66  arccos(1/2*sqrt(3))
     67  sage: sqrt(2)
     68  sqrt(2)
     69  sage: exp(I*pi/6)
     70  e^(1/6*I*pi)
     71
     72Sage example in ./premierspas.tex, line 574::
     73
     74  sage: simplify(arccos(sin(pi/3)))
     75  1/6*pi
     76  sage: simplify(exp(i*pi/6))
     77  1/2*sqrt(3) + 1/2*I
     78
     79Sage example in ./premierspas.tex, line 584::
     80
     81  sage: numerical_approx(6*arccos(sin(pi/3)), digits=60)
     82  3.14159265358979323846264338327950288419716939937510582097494
     83  sage: numerical_approx(sqrt(2), digits=60)
     84  1.41421356237309504880168872420969807856967187537694807317668
     85
     86Sage example in ./premierspas.tex, line 672::
     87
     88  sage: y = 1 + 2
     89
     90Sage example in ./premierspas.tex, line 676::
     91
     92  sage: y
     93  3
     94  sage: (2 + y) * y
     95  15
     96
     97Sage example in ./premierspas.tex, line 685::
     98
     99  sage: y = 1 + 2; y
     100  3
     101
     102Sage example in ./premierspas.tex, line 694::
     103
     104  sage: y = 3 * y + 1; y
     105  10
     106  sage: y = 3 * y + 1; y
     107  31
     108  sage: y = 3 * y + 1; y
     109  94
     110
     111Sage example in ./premierspas.tex, line 724::
     112
     113  sage: pi = -I/2
     114  sage: exp(2*i*pi)
     115  e
     116
     117Sage example in ./premierspas.tex, line 732::
     118
     119  sage: from sage.all import pi
     120
     121Sage example in ./premierspas.tex, line 752::
     122
     123  sage: z = SR.var('z')
     124  sage: 2*z + 3
     125  2*z + 3
     126
     127Sage example in ./premierspas.tex, line 764::
     128
     129  sage: y = SR.var('z')
     130  sage: 2*y + 3
     131  2*z + 3
     132
     133Sage example in ./premierspas.tex, line 775::
     134
     135  sage: c = 2 * y + 3
     136  sage: z = 1
     137  sage: 2*y + 3
     138  2*z + 3
     139  sage: c
     140  2*z + 3
     141
     142Sage example in ./premierspas.tex, line 785::
     143
     144  sage: x = SR.var('x')
     145  sage: expr = sin(x); expr
     146  sin(x)
     147  sage: expr(x=1)
     148  sin(1)
     149
     150Sage example in ./premierspas.tex, line 796::
     151
     152  sage: u = SR.var('u')
     153  sage: u = u+1
     154  sage: u = u+1
     155  sage: u
     156  u + 2
     157
     158Sage example in ./premierspas.tex, line 809::
     159
     160  sage: var('a, b, c, x, y')
     161  (a, b, c, x, y)
     162  sage: a * x + b * y + c
     163  a*x + b*y + c
     164
     165Sage example in ./premierspas.tex, line 826::
     166
     167  sage: var('bla')
     168  bla
     169
     170"""
     171from sage.all_cmdline import *   # import sage library