# Université de Sherbrooke, 14 avril 2011

Dessins 2D

{{{id=3| f = sin(1/x) P = plot(f, -10, 10, color='red') P /// }}} {{{id=43| Q = line([(3,0.9), (7,0.9), (7,1.1), (3,1.1), (3,0.9)], color='green') Q /// }}} {{{id=6| R = text('$f(x) = \\sin(\\frac{1}{x})$', (5,1)) R /// }}} {{{id=44| Q + R + P /// }}} {{{id=157| /// }}} {{{id=116| /// }}}

# L'outil interact (exemples tirés du wiki de Sage : http://wiki.sagemath.org/)

## Curves of Pursuit

by Marshall Hampton.

{{{id=46| %hide npi = RDF(pi) from math import cos,sin def rot(t): return matrix([[cos(t),sin(t)],[-sin(t),cos(t)]]) def pursuit(n,x0,y0,lamb,steps = 100, threshold = .01): paths = [[[x0,y0]]] for i in range(1,n): rx,ry = list(rot(2*npi*i/n)*vector([x0,y0])) paths.append([[rx,ry]]) oldpath = [x[-1] for x in paths] for q in range(steps): diffs = [[oldpath[(j+1)%n][0]-oldpath[j][0],oldpath[(j+1)%n][1]-oldpath[j][1]] for j in range(n)] npath = [[oldpath[j][0]+lamb*diffs[j][0],oldpath[j][1]+lamb*diffs[j][1]] for j in range(n)] for j in range(n): paths[j].append(npath[j]) oldpath = npath return paths html('

### Curves of Pursuit

') @interact def curves_of_pursuit(n = slider([2..20],default = 5, label="# of points"),steps = slider([floor(1.4^i) for i in range(2,18)],default = 10, label="# of steps"), stepsize = slider(srange(.01,1,.01),default = .2, label="stepsize"), colorize = selector(['BW','Line color', 'Filled'],default = 'BW')): outpaths = pursuit(n,0,1,stepsize, steps = steps) mcolor = (0,0,0) outer = line([q[0] for q in outpaths]+[outpaths[0][0]], rgbcolor = mcolor) polys = Graphics() if colorize=='Line color': colors = [hue(j/steps,1,1) for j in range(len(outpaths[0]))] elif colorize == 'BW': colors = [(0,0,0) for j in range(len(outpaths[0]))] else: colors = [hue(j/steps,1,1) for j in range(len(outpaths[0]))] polys = sum([polygon([outpaths[(i+1)%n][j+1],outpaths[(i+1)%n][j], outpaths[i][j+1]], rgbcolor = colors[j]) for i in range(n) for j in range(len(outpaths[0])-1)]) #polys = polys[0] colors = [(0,0,0) for j in range(len(outpaths[0]))] nested = sum([line([q[j] for q in outpaths]+[outpaths[0][j]], rgbcolor = colors[j]) for j in range(len(outpaths[0]))]) lpaths = [line(x, rgbcolor = mcolor) for x in outpaths] show(sum(lpaths)+nested+polys, axes = False, figsize = [5,5], xmin = -1, xmax = 1, ymin = -1, ymax =1) /// }}}

## Factor Trees

by William Stein

{{{id=11| %hide import random def ftree(rows, v, i, F): if len(v) > 0: # add a row to g at the ith level. rows.append(v) w = [] for i in range(len(v)): k, _, _ = v[i] if k is None or is_prime(k): w.append((None,None,None)) else: d = random.choice(divisors(k)[1:-1]) w.append((d,k,i)) e = k//d if e == 1: w.append((None,None)) else: w.append((e,k,i)) if len(w) > len(v): ftree(rows, w, i+1, F) def draw_ftree(rows,font): g = Graphics() for i in range(len(rows)): cur = rows[i] for j in range(len(cur)): e, f, k = cur[j] if not e is None: if is_prime(e): c = (1,0,0) else: c = (0,0,.4) g += text(str(e), (j*2-len(cur),-i), fontsize=font, rgbcolor=c) if not k is None and not f is None: g += line([(j*2-len(cur),-i), ((k*2)-len(rows[i-1]),-i+1)], alpha=0.5) return g @interact def factor_tree(n=100, font=(10, (8..20)), redraw=['Redraw']): n = Integer(n) rows = [] v = [(n,None,0)] ftree(rows, v, 0, factor(n)) show(draw_ftree(rows, font), axes=False) /// }}} {{{id=114| /// }}}

## Illustrating the prime number theorem

by William Stein

{{{id=156| @interact def _(N=(100,(2..2000))): html("$\pi(x)$ and $x/(\log(x)-1)$ for $x < %s$"%N) show(plot(prime_pi, 0, N, rgbcolor='red') + plot(x/(log(x)-1), 5, N, rgbcolor='blue')) /// }}}

## Stock Market data, fetched from Yahoo and Google

by William Stein

{{{id=115| %hide import urllib class Day: def __init__(self, date, open, high, low, close, volume): self.date = date self.open=float(open); self.high=float(high); self.low=float(low); self.close=float(close) self.volume=int(volume) def __repr__(self): return '%10s %4.2f %4.2f %4.2f %4.2f %10d'%(self.date, self.open, self.high, self.low, self.close, self.volume) class Stock: def __init__(self, symbol): self.symbol = symbol.upper() def __repr__(self): return "%s (%s)"%(self.symbol, self.yahoo()['price']) def yahoo(self): url = 'http://finance.yahoo.com/d/quotes.csv?s=%s&f=%s' % (self.symbol, 'l1c1va2xj1b4j4dyekjm3m4rr5p5p6s7') values = urllib.urlopen(url).read().strip().strip('"').split(',') data = {} data['price'] = values[0] data['change'] = values[1] data['volume'] = values[2] data['avg_daily_volume'] = values[3] data['stock_exchange'] = values[4] data['market_cap'] = values[5] data['book_value'] = values[6] data['ebitda'] = values[7] data['dividend_per_share'] = values[8] data['dividend_yield'] = values[9] data['earnings_per_share'] = values[10] data['52_week_high'] = values[11] data['52_week_low'] = values[12] data['50day_moving_avg'] = values[13] data['200day_moving_avg'] = values[14] data['price_earnings_ratio'] = values[15] data['price_earnings_growth_ratio'] = values[16] data['price_sales_ratio'] = values[17] data['price_book_ratio'] = values[18] data['short_ratio'] = values[19] return data def historical(self): try: return self.__historical except AttributeError: pass symbol = self.symbol def get_data(exchange): name = get_remote_file('http://finance.google.com/finance/historical?q=%s:%s&output=csv'%(exchange, symbol.upper()), verbose=False) return open(name).read() R = get_data('NASDAQ') if "Bad Request" in R: R = get_data("NYSE") R = R.splitlines() headings = R[0].split(',') self.__historical = [] try: for x in reversed(R[1:]): date, opn, high, low, close, volume = x.split(',') self.__historical.append(Day(date, opn,high,low,close,volume)) except ValueError: pass self.__historical = Sequence(self.__historical,cr=True,universe=lambda x:x) return self.__historical def plot_average(self, spline_samples=10): d = self.historical() if len(d) == 0: return text('no historical data at Google Finance about %s'%self.symbol, (0,3)) avg = list(enumerate([(z.high+z.low)/2 for z in d])) P = line(avg) + points(avg, rgbcolor='black', pointsize=4) + \ text(self.symbol, (len(d)*1.05, d[-1].low), horizontal_alignment='right', rgbcolor='black') if spline_samples > 0: k = 250//spline_samples spl = spline([avg[i*k] for i in range(len(d)//k)] + [avg[-1]]) P += plot(spl, (0,len(d)+30), color=(0.7,0.7,0.7)) P.xmax(260) return P def plot_diff(self): d = self.historical() if len(d) == 0: return text('no historical data at Google Finance about %s'%self.symbol, (0,3)) diff = [] for i in range(1, len(d)): z1 = d[i]; z0 = d[i-1] diff.append((i, (z1.high+z1.low)/2 - (z0.high + z0.low)/2)) P = line(diff,thickness=0.5) + points(diff, rgbcolor='black', pointsize=4) + \ text(self.symbol, (len(d)*1.05, 0), horizontal_alignment='right', rgbcolor='black') P.xmax(260) return P symbols = ['bsc', 'vmw', 'sbux', 'aapl', 'amzn', 'goog', 'wfmi', 'msft', 'yhoo', 'ebay', 'java', 'rht', ]; symbols.sort() stocks = dict([(s,Stock(s)) for s in symbols]) @interact def data(symbol = symbols, other_symbol='', spline_samples=(8,[0..15])): if other_symbol != '': symbol = other_symbol S = Stock(symbol) html('

# %s

'%S) S.plot_average(spline_samples).save('avg.png', figsize=[10,2]) S.plot_diff().save('diff.png', figsize=[10,2]) Y = S.yahoo() k = Y.keys(); k.sort() html('Price during last 52 weeks:
Grey line is a spline through %s points (do not take seriously!):
'%spline_samples) html('Difference from previous day:
') html('' + '\n'.join(''%(k[i], Y[k[i]]) for i in range(len(k))) + '
 %s %s
') /// }}} {{{id=167| /// }}}

## Dessiner une fonction $\mathbb{R}^2\mapsto \mathbb{R}$ : la commande plot3d

{{{id=58| def f(x, y): return x^2 - y^2 plot3d(f, (-10,10), (-10,10), viewer='tachyon') /// }}} {{{id=59| plot3d(f, (-10,10), (-10,10), viewer='jmol') /// }}}

Animations

{{{id=181| reset() # on doit réinitialiser les variables, autrement il y a un bug avec l'affichage de l'animation ci-bas /// }}} {{{id=20| a = animate([sin(x + float(k)) for k in srange(0,2*3.14,0.31)], xmin=0, xmax=2*pi, figsize=[2,1]) /// }}} {{{id=19| a.show() # nécessite la commande convert /// }}} {{{id=34| /// }}}

La commande complex_plot pour les fonctions $\mathbb{C}\mapsto \mathbb{C}$

{{{id=182| complex_plot(lambda x:x, (-30,30), (-30,30)) /// }}} {{{id=76| f(x) = x^4 - 1 /// }}} {{{id=37| complex_plot(f, (-2,2), (-2,2)) /// }}} {{{id=65| def newton(f, z, precision=0.001) : while abs(f(x=z)) >= precision: z = z - f(x=z) / diff(f)(x=z) return z /// }}} {{{id=170| newton(f, 4.0) /// 1.00000015203739 }}} {{{id=169| newton(f, 4.0 * i) /// 1.00013817217418 }}} {{{id=171| newton(f, 4.0 * i + 1.0, precision=0.00000000000001) /// 5.59623227309069e-22 + 1.00000000000000*I }}} {{{id=66| complex_plot(lambda z : newton(f, z), (-1,1), (-1,1)) /// }}} {{{id=67| /// }}} {{{id=68| /// }}} {{{id=147| /// }}}

## Utilisation du Notebook : Écriture, édition et évaluation d'une saisie

Pour évaluer une saisie dans le Notebook de Sage, tapez la saisie dans une cellule et faites shift-entrée ou cliquer le lien evaluate.  Essayez-le maintenant avec une expression simple (e.g., 2 + 2).   La première évaluation d'une cellule prend plus de temps que les fois suivantes, car un processus commence.

{{{id=159| 2+3 /// 5 }}} {{{id=2| 4+5 /// 9 }}}

Créez de nouvelles cellules de saisie en cliquant sur la ligne bleue qui apparaît entre les cellules lorsque vous déplacez la souris. Essayez-le maintenant.

Vous pouvez rééditer n'importe quelle cellule en cliquant dessus (ou en utilisant les flèches du clavier). Retournez plus haut et changez votre 2 + 2 en un 3 + 3 et réévaluez la cellule.

{{{id=9| /// }}}

Vous pouvez aussi éditer ce texte-ci en double cliquant dessus ce qui fera apparaître un éditeur de texte TinyMCE Javascript. Vous pouvez même ajouter des expressions mathématiques telles que $\sin(x) - y^3$ comme avec LaTeX.

{{{id=54| /// }}}

$$\int e^x dx = e^x + c$$

{{{id=75| /// }}}

## Comment consulter l'aide contextuelle et obtenir de la documentation

Vous trouvez la liste des fonctions que vous pouvez appelez sur un objet X en tappant X.<touche de tabulation>.

{{{id=33| X = 2011 /// }}}

Écrivez X. et appuyez sur la touche de tabulation.

{{{id=28| a = X.factor();a /// 2011 }}}

Une fois que vous avez sélectionné une fonction, disons factor, tappez X.factor(<touche de tabulation> ou X.factor?<touche de tabulation> pour obtenir de l'aide et des exemples d'utilisation de cette fonction. Essayez-le maintenant avec X.factor.

{{{id=177| X.is_prime() /// True }}} {{{id=161| /// }}} {{{id=39| /// }}}

Pour obtenir l'aide complète et un tutoriel plus exhaustif, cliquez sur le lien Help en haut à droite de cette page, et cliquer ensuite sur Fast Static Versions of the Documentation.

# Résolution d'équations polynomiales

{{{id=108| a,b,c,d,X = var('a,b,c,d,X') /// }}} {{{id=25| s = solve(a*X^2 + b*X + c == 0, X) show(s) ///
\newcommand{\Bold}[1]{\mathbf{#1}}\left[X = -\frac{b + \sqrt{-4 \, a c + b^{2}}}{2 \, a}, X = -\frac{b - \sqrt{-4 \, a c + b^{2}}}{2 \, a}\right]
}}} {{{id=40| s = solve(a*X^3 + b*X^2 + c*X + d == 0, X) show(s[0]) ///
\newcommand{\Bold}[1]{\mathbf{#1}}X = -\frac{1}{2} \, {\left(i \, \sqrt{3} + 1\right)} {\left(\frac{\sqrt{27 \, a^{2} d^{2} + 4 \, a c^{3} - b^{2} c^{2} - 2 \, {\left(9 \, a b c - 2 \, b^{3}\right)} d} \sqrt{3}}{18 \, a^{2}} - \frac{27 \, a^{2} d - 9 \, a b c + 2 \, b^{3}}{54 \, a^{3}}\right)}^{\left(\frac{1}{3}\right)} - \frac{b}{3 \, a} + \frac{{\left(-i \, \sqrt{3} + 1\right)} {\left(3 \, a c - b^{2}\right)}}{18 \, {\left(\frac{\sqrt{27 \, a^{2} d^{2} + 4 \, a c^{3} - b^{2} c^{2} - 2 \, {\left(9 \, a b c - 2 \, b^{3}\right)} d} \sqrt{3}}{18 \, a^{2}} - \frac{27 \, a^{2} d - 9 \, a b c + 2 \, b^{3}}{54 \, a^{3}}\right)}^{\left(\frac{1}{3}\right)} a^{2}}
}}} {{{id=71| s[0] /// X == -1/2*(I*sqrt(3) + 1)*(1/18*sqrt(27*a^2*d^2 + 4*a*c^3 - b^2*c^2 - 2*(9*a*b*c - 2*b^3)*d)*sqrt(3)/a^2 - 1/54*(27*a^2*d - 9*a*b*c + 2*b^3)/a^3)^(1/3) - 1/3*b/a + 1/18*(-I*sqrt(3) + 1)*(3*a*c - b^2)/((1/18*sqrt(27*a^2*d^2 + 4*a*c^3 - b^2*c^2 - 2*(9*a*b*c - 2*b^3)*d)*sqrt(3)/a^2 - 1/54*(27*a^2*d - 9*a*b*c + 2*b^3)/a^3)^(1/3)*a^2) }}} {{{id=41| /// }}} {{{id=18| /// }}}

# Algèbre linéaire

{{{id=38| A = matrix(3, [9,4,2,4,6,1,6,4,3,2,3,4,2,7,8,6,5,3]); A /// [9 4 2 4 6 1] [6 4 3 2 3 4] [2 7 8 6 5 3] }}} {{{id=110| show(A) ///
\newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrrrrr} 9 & 4 & 2 & 4 & 6 & 1 \\ 6 & 4 & 3 & 2 & 3 & 4 \\ 2 & 7 & 8 & 6 & 5 & 3 \end{array}\right)
}}} {{{id=111| latex(A) /// \left(\begin{array}{rrrrrr} 9 & 4 & 2 & 4 & 6 & 1 \\ 6 & 4 & 3 & 2 & 3 & 4 \\ 2 & 7 & 8 & 6 & 5 & 3 \end{array}\right) }}} {{{id=112| r = random_matrix(ZZ, 200) r[0] /// (0, 2, -1, 0, 72, -1, 1, 3, -2, -1, 4, 0, 0, 2, -3, -1, 1, 1, 0, -3, 0, 0, 0, -16, -6, 2, 274, 10, 0, -1, 46, -1, -1, -4, 0, 6, -3, 2, 1, 1, -5, -2, 1, 2, 0, 0, 0, -1, -1, 1, -3, -29, 2, -6, 1, -1, 1, 17, 2, -19, -3, 0, 0, 0, 2, -11, 1, 0, -4, 4, 0, 1, -8, 0, 0, -4, 1, -1, -6, 0, 1, -8, -2, 1, 0, 1, -19, 1, 2, 3, 0, -2, 1, -2, 0, -1, 0, 1, 3, -2, -1, 1, -9, 1, -1, -1, 2, 2, -2, 2, 0, 18, -1, 2, 0, -1, -17, 0, -2, 1, 0, 0, 4, 3, -1, 1, 1, 1, -3, -1, 3, 13, -1, 1, 3, 0, 1, -2, 2, -1, 1, 2, 2, 2, 0, -1, 0, 1, -2, 1, 1, -1, 1, -1, 2, 1, -1, 3, 1, 0, -1, 0, -2, 2, 0, 1, 1, 3, 0, 1, 3, -64, 2, 2, -1, 4, -8, 34, -1, 1, -1, 43, 7, 0, 0, 1, 0, -3, 1, -1, 5, -5, -1, -2, -2, -1, 10, 1, -1, -2) }}} {{{id=24| time r.determinant() /// -10267484656774284179227846201810370311311625108925526901154486673861915999151469246919468784028181929893183349225012477244723240066760674252095047304328620079746058713237493029408799183913242680380785292057907990042835366178315653730750995322822903785563424600840713388934072939028143786296322461443925820963868903300239269166738806082072311632032374801155212684874047626540446257368123486954982484119128403627687345906251209739032007065383743394105219950445643 Time: CPU 0.66 s, Wall: 1.16 s }}} {{{id=105| r.determinant? ///

File: /Users/slabbe/Applications/sage-4.6.2/devel/sage/sage/matrix/matrix_integer_dense.pyx

Type: <type ‘builtin_function_or_method’>

Definition: r.determinant(algorithm=’default’, proof=None, stabilize=2)

Docstring:

Return the determinant of this matrix.

INPUT:

• algorithm

• 'default' – use ‘pari’ when number of rows less than 50;

• 'padic' - uses a p-adic / multimodular algorithm that relies on code in IML and linbox

• 'linbox' - calls linbox det (you must set proof=False to use this!)

• 'ntl' - calls NTL’s det function

• 'pari' - uses PARI

• proof - bool or None; if None use proof.linear_algebra(); only relevant for the padic algorithm.

Note

It would be VERY VERY hard for det to fail even with proof=False.

• stabilize - if proof is False, require det to be the same for this many CRT primes in a row. Ignored if proof is True.

ALGORITHM: The p-adic algorithm works by first finding a random vector v, then solving A*x = v and taking the denominator d. This gives a divisor of the determinant. Then we compute \det(A)/d using a multimodular algorithm and the Hadamard bound, skipping primes that divide d.

TIMINGS: This is perhaps the fastest implementation of determinants in the world. E.g., for a 500x500 random matrix with 32-bit entries on a core2 duo 2.6Ghz running OS X, Sage takes 4.12 seconds, whereas Magma takes 62.87 seconds (both with proof False). With proof=True on the same problem Sage takes 5.73 seconds. For another example, a 200x200 random matrix with 1-digit entries takes 4.18 seconds in pari, 0.18 in Sage with proof True, 0.11 in Sage with proof False, and 0.21 seconds in Magma with proof True and 0.18 in Magma with proof False.

EXAMPLES:

sage: A = matrix(ZZ,8,8,[3..66])
sage: A.determinant()
0

sage: A = random_matrix(ZZ,20,20)
sage: D1 = A.determinant()
sage: A._clear_cache()
sage: D2 = A.determinant(algorithm='ntl')
sage: D1 == D2
True


Next we try the Linbox det. Note that we must have proof=False.

sage: A = matrix(ZZ,5,[1,2,3,4,5,4,6,3,2,1,7,9,7,5,2,1,4,6,7,8,3,2,4,6,7])
sage: A.determinant(algorithm='linbox')
Traceback (most recent call last):
...
RuntimeError: you must pass the proof=False option to the determinant command to use LinBox's det algorithm
sage: A.determinant(algorithm='linbox',proof=False)
-21
sage: A._clear_cache()
sage: A.determinant()
-21


A bigger example:

sage: A = random_matrix(ZZ,30)
sage: d = A.determinant()
sage: A._clear_cache()
sage: A.determinant(algorithm='linbox',proof=False) == d
True

}}}

# Théorie des graphes

{{{id=178| graphs.[TAB] /// }}} {{{id=49| D = graphs.DodecahedralGraph() D.show() /// }}} {{{id=48| D.show3d() /// }}} {{{id=77| D.chromatic_polynomial() /// x^20 - 30*x^19 + 435*x^18 - 4060*x^17 + 27393*x^16 - 142194*x^15 + 589875*x^14 - 2004600*x^13 + 5673571*x^12 - 13518806*x^11 + 27292965*x^10 - 46805540*x^9 + 68090965*x^8 - 83530946*x^7 + 85371335*x^6 - 71159652*x^5 + 46655060*x^4 - 22594964*x^3 + 7171160*x^2 - 1111968*x }}} {{{id=183| graph_editor(D) /// }}} {{{id=174| D.show() /// }}} {{{id=52| latex(D) /// WARNING: Output truncated! full_output.txt \begin{tikzpicture} % \useasboundingbox (0,0) rectangle (5.0cm,5.0cm); % \definecolor{cv0}{rgb}{0.0,0.0,0.0} \definecolor{cfv0}{rgb}{1.0,1.0,1.0} \definecolor{clv0}{rgb}{0.0,0.0,0.0} \definecolor{cv1}{rgb}{0.0,0.0,0.0} \definecolor{cfv1}{rgb}{1.0,1.0,1.0} \definecolor{clv1}{rgb}{0.0,0.0,0.0} \definecolor{cv2}{rgb}{0.0,0.0,0.0} \definecolor{cfv2}{rgb}{1.0,1.0,1.0} \definecolor{clv2}{rgb}{0.0,0.0,0.0} \definecolor{cv3}{rgb}{0.0,0.0,0.0} \definecolor{cfv3}{rgb}{1.0,1.0,1.0} \definecolor{clv3}{rgb}{0.0,0.0,0.0} \definecolor{cv4}{rgb}{0.0,0.0,0.0} \definecolor{cfv4}{rgb}{1.0,1.0,1.0} \definecolor{clv4}{rgb}{0.0,0.0,0.0} \definecolor{cv5}{rgb}{0.0,0.0,0.0} \definecolor{cfv5}{rgb}{1.0,1.0,1.0} \definecolor{clv5}{rgb}{0.0,0.0,0.0} \definecolor{cv6}{rgb}{0.0,0.0,0.0} \definecolor{cfv6}{rgb}{1.0,1.0,1.0} \definecolor{clv6}{rgb}{0.0,0.0,0.0} \definecolor{cv7}{rgb}{0.0,0.0,0.0} \definecolor{cfv7}{rgb}{1.0,1.0,1.0} \definecolor{clv7}{rgb}{0.0,0.0,0.0} \definecolor{cv8}{rgb}{0.0,0.0,0.0} \definecolor{cfv8}{rgb}{1.0,1.0,1.0} \definecolor{clv8}{rgb}{0.0,0.0,0.0} \definecolor{cv9}{rgb}{0.0,0.0,0.0} \definecolor{cfv9}{rgb}{1.0,1.0,1.0} \definecolor{clv9}{rgb}{0.0,0.0,0.0} \definecolor{cv10}{rgb}{0.0,0.0,0.0} \definecolor{cfv10}{rgb}{1.0,1.0,1.0} \definecolor{clv10}{rgb}{0.0,0.0,0.0} \definecolor{cv11}{rgb}{0.0,0.0,0.0} \definecolor{cfv11}{rgb}{1.0,1.0,1.0} \definecolor{clv11}{rgb}{0.0,0.0,0.0} \definecolor{cv12}{rgb}{0.0,0.0,0.0} \definecolor{cfv12}{rgb}{1.0,1.0,1.0} \definecolor{clv12}{rgb}{0.0,0.0,0.0} \definecolor{cv13}{rgb}{0.0,0.0,0.0} \definecolor{cfv13}{rgb}{1.0,1.0,1.0} \definecolor{clv13}{rgb}{0.0,0.0,0.0} \definecolor{cv14}{rgb}{0.0,0.0,0.0} \definecolor{cfv14}{rgb}{1.0,1.0,1.0} \definecolor{clv14}{rgb}{0.0,0.0,0.0} \definecolor{cv15}{rgb}{0.0,0.0,0.0} \definecolor{cfv15}{rgb}{1.0,1.0,1.0} \definecolor{clv15}{rgb}{0.0,0.0,0.0} \definecolor{cv16}{rgb}{0.0,0.0,0.0} \definecolor{cfv16}{rgb}{1.0,1.0,1.0} \definecolor{clv16}{rgb}{0.0,0.0,0.0} \definecolor{cv17}{rgb}{0.0,0.0,0.0} \definecolor{cfv17}{rgb}{1.0,1.0,1.0} \definecolor{clv17}{rgb}{0.0,0.0,0.0} \definecolor{cv18}{rgb}{0.0,0.0,0.0} ... \definecolor{cv9v12}{rgb}{0.0,0.0,0.0} \definecolor{cv11v12}{rgb}{0.0,0.0,0.0} \definecolor{cv11v15}{rgb}{0.0,0.0,0.0} \definecolor{cv12v13}{rgb}{0.0,0.0,0.0} \definecolor{cv13v14}{rgb}{0.0,0.0,0.0} \definecolor{cv14v15}{rgb}{0.0,0.0,0.0} \definecolor{cv15v16}{rgb}{0.0,0.0,0.0} \definecolor{cv16v17}{rgb}{0.0,0.0,0.0} \definecolor{cv17v18}{rgb}{0.0,0.0,0.0} % \Vertex[style={minimum size=1.0cm,draw=cv0,fill=cfv0,text=clv0,shape=circle},LabelOut=false,L=\hbox{$0$},x=2.1525cm,y=1.5471cm]{v0} \Vertex[style={minimum size=1.0cm,draw=cv1,fill=cfv1,text=clv1,shape=circle},LabelOut=false,L=\hbox{$1$},x=1.2715cm,y=0.5618cm]{v1} \Vertex[style={minimum size=1.0cm,draw=cv2,fill=cfv2,text=clv2,shape=circle},LabelOut=false,L=\hbox{$2$},x=2.7504cm,y=0.0cm]{v2} \Vertex[style={minimum size=1.0cm,draw=cv3,fill=cfv3,text=clv3,shape=circle},LabelOut=false,L=\hbox{$3$},x=4.2288cm,y=0.5553cm]{v3} \Vertex[style={minimum size=1.0cm,draw=cv4,fill=cfv4,text=clv4,shape=circle},LabelOut=false,L=\hbox{$4$},x=4.9039cm,y=1.8714cm]{v4} \Vertex[style={minimum size=1.0cm,draw=cv5,fill=cfv5,text=clv5,shape=circle},LabelOut=false,L=\hbox{$5$},x=3.494cm,y=1.8717cm]{v5} \Vertex[style={minimum size=1.0cm,draw=cv6,fill=cfv6,text=clv6,shape=circle},LabelOut=false,L=\hbox{$6$},x=2.2913cm,y=1.0798cm]{v6} \Vertex[style={minimum size=1.0cm,draw=cv7,fill=cfv7,text=clv7,shape=circle},LabelOut=false,L=\hbox{$7$},x=1.0978cm,y=1.87cm]{v7} \Vertex[style={minimum size=1.0cm,draw=cv8,fill=cfv8,text=clv8,shape=circle},LabelOut=false,L=\hbox{$8$},x=0.0cm,y=1.5259cm]{v8} \Vertex[style={minimum size=1.0cm,draw=cv9,fill=cfv9,text=clv9,shape=circle},LabelOut=false,L=\hbox{$9$},x=0.0193cm,y=3.0286cm]{v9} \Vertex[style={minimum size=1.0cm,draw=cv10,fill=cfv10,text=clv10,shape=circle},LabelOut=false,L=\hbox{$10$},x=1.2539cm,y=2.6486cm]{v10} \Vertex[style={minimum size=1.0cm,draw=cv11,fill=cfv11,text=clv11,shape=circle},LabelOut=false,L=\hbox{$11$},x=2.1063cm,y=5.0cm]{v11} \Vertex[style={minimum size=1.0cm,draw=cv12,fill=cfv12,text=clv12,shape=circle},LabelOut=false,L=\hbox{$12$},x=0.6655cm,y=4.3611cm]{v12} \Vertex[style={minimum size=1.0cm,draw=cv13,fill=cfv13,text=clv13,shape=circle},LabelOut=false,L=\hbox{$13$},x=1.2443cm,y=3.2209cm]{v13} \Vertex[style={minimum size=1.0cm,draw=cv14,fill=cfv14,text=clv14,shape=circle},LabelOut=false,L=\hbox{$14$},x=2.7709cm,y=3.1281cm]{v14} \Vertex[style={minimum size=1.0cm,draw=cv15,fill=cfv15,text=clv15,shape=circle},LabelOut=false,L=\hbox{$15$},x=3.5779cm,y=4.2367cm]{v15} \Vertex[style={minimum size=1.0cm,draw=cv16,fill=cfv16,text=clv16,shape=circle},LabelOut=false,L=\hbox{$16$},x=5.0cm,y=3.3922cm]{v16} \Vertex[style={minimum size=1.0cm,draw=cv17,fill=cfv17,text=clv17,shape=circle},LabelOut=false,L=\hbox{$17$},x=4.2948cm,y=2.6702cm]{v17} \Vertex[style={minimum size=1.0cm,draw=cv18,fill=cfv18,text=clv18,shape=circle},LabelOut=false,L=\hbox{$18$},x=3.6779cm,y=1.5799cm]{v18} \Vertex[style={minimum size=1.0cm,draw=cv19,fill=cfv19,text=clv19,shape=circle},LabelOut=false,L=\hbox{$19$},x=4.9155cm,y=4.0657cm]{v19} % \Edge[lw=0.1cm,style={color=cv0v1,},](v0)(v1) \Edge[lw=0.1cm,style={color=cv0v10,},](v0)(v10) \Edge[lw=0.1cm,style={color=cv0v18,},](v0)(v18) \Edge[lw=0.1cm,style={color=cv1v2,},](v1)(v2) \Edge[lw=0.1cm,style={color=cv1v8,},](v1)(v8) \Edge[lw=0.1cm,style={color=cv2v3,},](v2)(v3) \Edge[lw=0.1cm,style={color=cv2v6,},](v2)(v6) \Edge[lw=0.1cm,style={color=cv3v4,},](v3)(v4) \Edge[lw=0.1cm,style={color=cv3v18,},](v3)(v18) \Edge[lw=0.1cm,style={color=cv4v5,},](v4)(v5) \Edge[lw=0.1cm,style={color=cv4v16,},](v4)(v16) \Edge[lw=0.1cm,style={color=cv5v6,},](v5)(v6) \Edge[lw=0.1cm,style={color=cv5v14,},](v5)(v14) \Edge[lw=0.1cm,style={color=cv6v7,},](v6)(v7) \Edge[lw=0.1cm,style={color=cv7v8,},](v7)(v8) \Edge[lw=0.1cm,style={color=cv7v13,},](v7)(v13) \Edge[lw=0.1cm,style={color=cv8v9,},](v8)(v9) \Edge[lw=0.1cm,style={color=cv9v10,},](v9)(v10) \Edge[lw=0.1cm,style={color=cv9v12,},](v9)(v12) \Edge[lw=0.1cm,style={color=cv11v12,},](v11)(v12) \Edge[lw=0.1cm,style={color=cv11v15,},](v11)(v15) \Edge[lw=0.1cm,style={color=cv12v13,},](v12)(v13) \Edge[lw=0.1cm,style={color=cv13v14,},](v13)(v14) \Edge[lw=0.1cm,style={color=cv14v15,},](v14)(v15) \Edge[lw=0.1cm,style={color=cv15v16,},](v15)(v16) \Edge[lw=0.1cm,style={color=cv16v17,},](v16)(v17) \Edge[lw=0.1cm,style={color=cv17v18,},](v17)(v18) % \end{tikzpicture} }}}

# Recherche dans l'encyclopédie de séquences en ligne de Sloane

{{{id=80| sloane_find([1,5,29,169],1) /// Searching Sloane's online database... [] }}} {{{id=81| sloane_find([1,2,3,4,5,6],1) /// Searching Sloane's online database... [] }}} {{{id=162| /// }}} {{{id=96| /// }}} {{{id=97| /// }}} {{{id=82| /// }}}

# Cython

The Sage notebook allows transparently editing and compiling Cython code simply by typing %cython at the top of a cell and evaluate it. Variables and functions defined in a Cython cell are imported into the running session.

Example 1, pure Python

Here is some simple Python code to numerically integrate the function $f(x) = x^2$.

{{{id=47| /// }}} {{{id=1| from math import sin def f(x): return sin(x**2) def integrate_f_py(a, b, N): s = 0 dx = (b-a)/N for i in range(N): s += f(a+i*dx) return s * dx /// }}} {{{id=62| timeit('integrate_f_py(0, 1, 1000)', number=50) /// 50 loops, best of 3: 18.1 ms per loop }}} {{{id=7| /// }}} {{{id=63| /// }}}

Example 1, compiled with Cython (no other changes)

Simply compiling this in Cython gives a speedup.

{{{id=5| %cython from math import sin def f(x): return sin(x**2) def integrate_f_cy0(a, b, N): s = 0 dx = (b-a)/N for i in range(N): s += f(a+i*dx) return s * dx /// }}} {{{id=4| timeit('integrate_f_cy0(0, 1, 1000)', number=50) /// 50 loops, best of 3: 16.8 ms per loop }}} {{{id=8| /// }}} {{{id=69| /// }}}

Example 1, typed and compiled with Cython

Adding some static type declarations makes a much greater difference.

{{{id=10| %cython from math import sin def f(double x): return sin(x**2) def integrate_f_cy(double a, double b, int N): cdef int i cdef double s, dx s = 0 dx = (b-a)/N for i in range(N): s += f(a+i*dx) return s * dx /// }}} {{{id=72| timeit('integrate_f_cy(0, 1, 1000)') /// 625 loops, best of 3: 499 µs per loop }}} {{{id=12| 18500 /489.0 /// 37.8323108384458 }}} {{{id=17| /// }}}

Example 2, pure Python

Here is a Python function that computes the sum of the first $n$ positive integers.

{{{id=29| def mysum_py(n): s = 0 for k in range(n): s += k return s /// }}} {{{id=31| time mysum_py(10^6) /// 499999500000 Time: CPU 2.09 s, Wall: 2.13 s }}} {{{id=32| /// }}} {{{id=74| /// }}}

Example 2, just compiled with Cython

Simply compiling this function with Cython provides a speedup.

{{{id=83| %cython def mysum_cy0(n): s = 0 for k in range(n): s += k return s /// }}} {{{id=84| time mysum_cy0(10^6) /// 499999500000L Time: CPU 0.22 s, Wall: 0.22 s }}} {{{id=85| 2.09 / 0.22 /// 9.50000000000000 }}} {{{id=86| /// }}}

Example 2, typed and compiled with Cython

Adding some static type declarations makes a much greater difference.

{{{id=88| %cython def mysum_cy1(n): cdef int k cdef long long s s = 0 for k in range(n): s += k return s /// }}} {{{id=42| time mysum_cy1(10^6) /// 499999500000L Time: CPU 0.00 s, Wall: 0.00 s }}} {{{id=89| 2.09 / 0.00 /// +infinity }}} {{{id=179| timeit('mysum_cy1(10^6)') /// 125 loops, best of 3: 1.63 ms per loop }}} {{{id=90| 2.09/0.00163 /// 1282.20858895706 }}}

Two important (but minor) differences between Sage language and Python

Integer division in Python :

{{{id=173| t = 2 /// }}} {{{id=117| %python 2/3 + 4/5 + 1/7 /// 0 }}}

in Sage:

{{{id=125| 2/3 + 4/5 + 1/7 /// 169/105 }}}

Exponent (^) in Python :

{{{id=130| %python 10^14 #exclusive OR /// 4 }}}

in Sage :

{{{id=149| 10^14 /// 100000000000000 }}}

The preparser

{{{id=154| preparse('2/3 + 2^3 + 3.0') /// "Integer(2)/Integer(3) + Integer(2)**Integer(3) + RealNumber('3.0')" }}} {{{id=35| preparse('2^3') /// 'Integer(2)**Integer(3)' }}} {{{id=163| /// }}}