Shared2017-02-13-204055.ipynbOpen in CoCalc
Author: Lucas PRABEL
Views : 31
Description: Jupyter notebook 2017-02-13-204055.ipynb

Prise en main du logiciel Sage

Plot

In [35]:
plot(floor(x), (x, 1, 10), exclude=[1..10])
In [44]:
plot(x**2,xmin=-5,xmax=5,ymin=-5,ymax=5,color='purple',legend_label='x**2')
In [40]:
plot([sin(x), tan(x), 1-x^2], legend_label='automatic') # AUTOMATIC S'APPLIQUE A CE QUI EST ENTRE CROCHETS !

Tester une propriété

In [46]:
is_even(3)
False
In [47]:
is_prime(71)
True
In [48]:
is_prime_power(1872)
False

Symboles, expressions et variables

In [57]:
diff(x**3,x)
3*x^2
In [55]:
integrate(x**3,x)
1/4*x^4
In [59]:
integrate(exp(x),x)
e^x
In [60]:
var('y') integrate(exp(x),y)
y*e^x
In [64]:
integral(ln(x),x,0,1)
-1
In [65]:
integral(exp(-x^2),x,-oo,+oo)
sqrt(pi)
In [66]:
factor(4*x+4*y+2*x**2*y+9*x**2+2*x**3+9*x*y)
(2*x + 1)*(x + y)*(x + 4)
In [68]:
numerical_approx(pi,digits=200)
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303820
In [71]:
var('n') sum(1/n**2,n,1,+oo)
1/6*pi^2
In [73]:
sum(1/factorial(n),n,0,+oo)
e

Quelques objets

In [75]:
Z7 = IntegerModRing(7)
In [76]:
b = Z7(10)
In [77]:
b
3
In [78]:
ZZ
Integer Ring
In [79]:
QQ
Rational Field
In [84]:
a = Z7(2)
In [85]:
a
2
In [87]:
type(a)
<type 'sage.rings.finite_rings.integer_mod.IntegerMod_int'>
In [88]:
a**-1
4
In [89]:
a*4
1
In [91]:
Z9 = IntegerModRing(9)
In [93]:
b = Z9(2)
In [94]:
a == b
False
In [108]:
10//3
3
In [110]:
s = [2^i for i in [1..10]] ; s
[2, 4, 8, 16, 32, 64, 128, 256, 512, 1024]
In [111]:
s+s
[2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024]
In [112]:
4*s
[2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024]
In [113]:
s.append(pi)
In [114]:
s
[2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, pi]
In [120]:
L = [] f = x*exp(x) for k in [1..6]: f = diff(f) L += [factor(f)] print(L)
[(x + 1)*e^x, (x + 2)*e^x, (x + 3)*e^x, (x + 4)*e^x, (x + 5)*e^x, (x + 6)*e^x]
In [122]:
parent(5)
Integer Ring
In [123]:
parent (5/2)
Rational Field
In [124]:
QQ
Rational Field
In [125]:
parent(x)
Symbolic Ring

Un peu de programmation

In [126]:
def factorielle(n): res = 1 for k in [2..n]: res = res*k return res
In [130]:
for k in [1..10]: print(factorielle(k))
1 2 6 24 120 720 5040 40320 362880 3628800
In [131]:
def factorielle2(n): if n==0 or n==1 : return 1 return n*factorielle2(n-1)
In [132]:
for k in [1..10]: print(factorielle2(k))
1 2 6 24 120 720 5040 40320 362880 3628800
In [133]:
for k in [1..10]: print(factorial(k))
1 2 6 24 120 720 5040 40320 362880 3628800
In [135]:
next_prime(11)
13
In [142]:
def liste_premiers(N): a = 2 L = [] while a <= N : L += [a] a = next_prime(a) return L
In [143]:
liste_premiers(100)
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
In [147]:
L = [] for k in [2..1000]: if is_prime(k) and is_prime(k+2) : L += [(k,k+2)] L
[(3, 5), (5, 7), (11, 13), (17, 19), (29, 31), (41, 43), (59, 61), (71, 73), (101, 103), (107, 109), (137, 139), (149, 151), (179, 181), (191, 193), (197, 199), (227, 229), (239, 241), (269, 271), (281, 283), (311, 313), (347, 349), (419, 421), (431, 433), (461, 463), (521, 523), (569, 571), (599, 601), (617, 619), (641, 643), (659, 661), (809, 811), (821, 823), (827, 829), (857, 859), (881, 883)]
In [163]:
def fib(n): if n in [0,1] : return 1 else : return fib(n-1)+fib(n-2)
In [173]:
@cached_function def fibb(n): if n in [0,1] : return 1 else : return fibb(n-2)+fibb(n-1)
In [168]:
fib(30)
1346269
In [174]:
fibb(100)
573147844013817084101
In [175]:
def fib2(n): a, b = 1,1 for i in [1..n-1]: a, b = b+a,a return a
In [176]:
for k in [0..10]: print(k,fib2(k))
(0, 1) (1, 1) (2, 2) (3, 3) (4, 5) (5, 8) (6, 13) (7, 21) (8, 34) (9, 55) (10, 89)
In [182]:
X = [(k,fibb(k+1)/fibb(k)) for k in [1..30]] point(X)
In [187]:
N(lim(fibb(k+1)/fibb(k),k=+oo))
1.61803398874965
In [198]:
def syracuse(u0): X = [(0,u0)] k = 0 uk = u0 while uk !=1: if uk%2 == 0 : uk = uk/2 else : uk = 3*uk+1 k += 1 X += [(k,uk)] return X
In [202]:
point(syracuse(127))
In [ ]: