Ticket #10983: trac_10983_doctests_from_french_book_v2.patch

File trac_10983_doctests_from_french_book_v2.patch, 41.5 KB (added by chapoton, 4 years ago)
  • new file sage/tests/french_book/calculus_doctest.py

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