You are on page 1of 40

Sage

October 12, 2013


1 Sage ......................................................................... 2

1.1 Sage ........................................................................................... 2

1.2 Sage........................................................................................... 2

1.2.1 Sage . . . . . . . . . . . . . . . . . . . . . . . . . . 2

1.2.2 Web . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

1.2.3 App . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

2 Python .......................................................................... 4

2.1 ............................................................................................ 4

2.2 (list)........................................................................................... 5

2.3 range ...................................................................................... 6

2.4 for .................................................................................. 7

1
3 Sage ......................................................................... 8

3.1 Sage ....................................................................................... 8

3.2 Sage ................................................................................ 10

3.3 Sage ....................................................................................... 11

3.4 ................................................................................................ 11

3.5 ......................................................................................... 11

4 .......................................................................................... 12

4.1 .............................................................................. 12

4.2 ......................................................................................... 13

4.3 ..................................................................................... 15

4.4 ..................................................................................... 15

5 Sage ............................................................... 19

5.1 ............................................................................................ 19

5.2 gcd, xgcd ...................................................................................... 20

5.3 Zn ....................................................................................... 20

5.4 ................................................................................................... 22

6 ............................................................................ 24

6.1 .............................................................................. 24

2
6.2 .......................................................................... 25

6.3 ................................................................................. 26

6.4 ................................................................................. 27

6.5 ................................................................................................... 27

7 Dihedral ......................................................................................... 29

7.1 Dihedral ......................................................................... 29

7.2 Dihedral Group .................................................................. 29

7.3 Dihedral Group Cayley Table....................................................... 30

7.4 Cayley Graph .................................................................................... 32

8 U (n) ........................................................................................ 34

8.1 Sage U (n) ................................................................. 34

8.2 Sage U(n) ....................................................... 35

8.3 U (n) Cayley Table................................................................... 36

3
1

Sage

1.1 Sage

Sage , Maxima, Octave, R, GAP , Mathe-


matica, Maple, Matlab , (Python)

Sage , Sage
,

1.2 Sage

1.2.1 Sage

Sage Sage:

4
http://www.sagemath.org/

Mac, Linux, Windows , :

Mac Linux ,

Windows , VirtualBox

1.2.2 Web

Sage:

http://www.sagenb.org

Google OpenID

1.2.3 App

iOS Android Sage Math App,


, ,
Sage Server ,

5
2

Python

Sage Python , :
, Python
, Python

2.1

, Python (Sage) , ==

sage: a = 3
sage: b = 5
sage: a == b
False

, , :

6
sage: a > b
False
sage: a != b # a b
True
sage: a <= b # a b
True

2.2 (list)

(list) Python , Sage


:

sage: L = [1, 2, 8, 6, 6, 5]

, ,
(, , Python ), 0,
1, ,

sage: L[0]
1
sage: L[2]
8

7
2.3 range

range, , , 0 ,
0, 1, 2, ..., 9 range(10) :

sage: L = range(10)
sage: L
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

range ,

sage: range(2, 10)


[2, 3, 4, 5, 6, 7, 8, 9]

1 15 , 1 , 2, 15 ( 16-1):

sage: range(1, 16, 2)


[1, 3, 5, 7, 9, 11, 13, 15]

Sage 2, 3, . . . , 9 ,
[2..9] !

sage: [2..9]
[2, 3, 4, 5, 6, 7, 8, 9]

8
2.4 for

range , ,
1 100 ,

sage: L = range(1,101)
sage: for i in L:
..... print i

9
3

Sage

, Sage ,
, Sage

3.1 Sage

Sage , :

sage: 43 + 77
120
sage: 300*1.5
450.000000000000
sage: (2+3)*17
85

10
Sage Python ,
,

sage: a=1323123124861782461782469123876127612832168372176
382163921873612983721687391283621281472914
sage: a + 1
132312312486178246178246912387612761283216837217638216392
1873612983721687391283621281472915

1 ? , Python **, Sage


^,

sage: 2^3
8

, Sage :

sage: 1/2 + 1/3


5/6

, Sage ,
%,

sage: 13 % 5
3

, //:

11
sage: 13 // 5
2

, quo_rem

sage: a = 13
sage: b = 5
sage: a.quo_rem(b)
(2, 3)

Sage ( Python ), ,
,

3.2 Sage

, Sage ,

sage: A = matrix([[1,2,3], [3,1,1], [1,2,5]])


sage: A
[1 2 3]
[3 1 1]
[1 2 5]
sage: A.inverse()
[-3/10 2/5 1/10]
[ 7/5 -1/5 -4/5]
[ -1/2 0 1/2]

12
3.3 Sage

, , Sage

sage: plot(sin, (-2*pi, 2*pi))

, Sage sin

3.4

sage: integrate(sec(x))
log(tan(x) + sec(x))
sage: diff(sin(x))
cos(x)

, Sage, ?

3.5

Sage , pi, e e, , 77
? :

sage: pi.n(digits=77)
3.1415926535897932384626433832795028841971693993751058209
749445923078164062862

13
4

4.1

v, w

sage: v = vector([2,1,-3])
sage: w = vector([3,-2,1])

v w

sage: v.dot_product(w)
1

v w

14
sage: v.cross_product(w)
(-5, -11, -7)

sage: t = var('t')
sage: v = vector([t,t^2,t^3])
sage: w = vector([1,-t,2*t])

sage: v.dot_product(w)
2*t^4 - t^3 + t
sage: v.cross_product(w)
(t^4 + 2*t^3, t^3 - 2*t^2, -2*t^2)

4.2

r(t) = (cos t, sin t, t)

sage: t = var('t')
sage: r = vector([cos(t), sin(t), t])

t [0, 2], r(t)

15
sage: parametric_plot3d(r, (t,0,2*pi))

r (t)

sage: dr = diff(r,t)
sage: dr
(-sin(t), cos(t), 1)

r (t) r (t)

sage: norm(dr)
sqrt(abs(-sin(t))^2 + abs(cos(t))^2 + 1)

2
L= r (t) dt
0

16
sage: integral(norm(dr), (t,0,2*pi))
2*sqrt(2)*pi


2 2

4.3

, Sage , reset

reset()

4.4

(4 sin (t) , 2 cos (2 t) , 5 t) ,

t 0 2

, r(t)

sage: t = var('t')
sage: r = vector([4*sin(-t), 2*cos(2*t), 5*t])

r (t)

17
sage: dr = diff(r,t)
sage: dr
(-4*cos(-t), -4*sin(2*t), 5)

, , show

c = parametric_plot3d(r,(t,0,2*pi))

unit tangent vector,

r (t)
.
r (t)

sage: ut = dr/norm(dr)
sage: ut
(-4*cos(-t)/sqrt(abs(-4*cos(-t))^2 + abs(-4*sin(2*t))^2
+ 25), -4*sin(2*t)/sqrt(abs(-4*cos(-t))^2 + abs(
-4*sin(2*t))^2 + 25), 5/sqrt(abs(-4*cos(-t))^2 +
abs(-4*sin(2*t))^2 + 25))

t = 2 unit tangent vector r (2)

sage: p1 = r.substitute(t=2)
sage: p1
(-4*sin(2), 2*cos(4), 10)

18
sage: p1 = r(t=2)

t = 2 unit tangent vector

sage: unit = ut.substitute(t=2)


sage: unit
(-4*cos(2)/sqrt(16*cos(2)^2 + 16*sin(4)^2 + 25),
-4*sin(4)/sqrt(16*cos(2)^2 + 16*sin(4)^2 + 25),
5/sqrt(16*cos(2)^2 + 16*sin(4)^2 + 25))

sage: unit = ut(t=2)

P1 unit tangent vector

sage: p2 = p1 + unit

P1 P2

sage: unitvec= line3d([p1,p2], rgbcolor=(1,0,0),


arrow_head=True, thickness=3)

unit tangent vector

sage: show(c + unitvec)

19
20
5

Sage

Sage

5.1

sage: a = 3377123
sage: a.is_prime()
False

sage: a = 3377141
sage: a.is_prime()
True

, ?

21
5.2 gcd, xgcd

sage: gcd(37, 18)


1

1, m, n 37m + 18n = 1

, , xgcd

sage: xgcd(37, 18)


(1, 1, -2)

1 , 1, -2 (1) 37 + (2) 18 = 1

5.3 Zn

, Z7 :

sage: Z7 = Integers(7)
sage: Z7
Ring of integers modulo 7

? Z7 :

22
sage: a = Z7(5)
sage: b = Z7(3)
sage: a + b
1

! Sage Z7 ( Cayley Table) :

sage: G.addition_table()
+ a b c d e f g
+--------------
a| a b c d e f g
b| b c d e f g a
c| c d e f g a b
d| d e f g a b c
e| e f g a b c d
f| f g a b c d e
g| g a b c d e f

, ?

sage: G.addition_table(names="elements")
+ 0 1 2 3 4 5 6
+--------------
0| 0 1 2 3 4 5 6
1| 1 2 3 4 5 6 0
2| 2 3 4 5 6 0 1
3| 3 4 5 6 0 1 2
4| 4 5 6 0 1 2 3
5| 5 6 0 1 2 3 4
6| 6 0 1 2 3 4 5

23
, , , :

sage: G.addition_table(name=[' ', ' ', ' ', ' ',


' ', ' ', ' '])

5.4

Sage

sage: a = 68828863
sage: a.is_prime()
True

prime_range , 2 18

sage: L = prime_range(2,19)
sage: L
[2, 3, 5, 7, 11, 13, 17]

prime_range(a,b) a b 1

random_prime

sage: p = random_prime(99999999, proof=True)

2 () 99999999 proof=True
proof=False ,

24

sage: a = 300
sage: a.factor()
2^2 * 3 * 5^2

, :

sage: L = a.factor()
sage: L[0]
(2, 2)
sage: L[1]
(3, 1)
sage: L[2]
(5, 2)

25
6

, , ,

Sage

6.1

Sage , S5 , :

sage: S5 = SymmetricGroup(5)

? , , :

sage: sigma = S5("(1,3)(2,5,4)")

26
Sage :

sage: sigma * sigma


(2,4,5)

, !

sage: gamma = S5("(2,3)(1,4,5)")


sage: sigma * gamma
(1,2)(3,4)

, Sage , , Gallian ,
, :

sage: gamma * sigma


(1,2)(3,5)

6.2

S5 :

[ ]
1 2 3 4 5
=
2 1 3 5 4

cycle notation

= (12)(45)

27
( S5):

sage: alpha = S5("(1,2)(4,5)")

sage: alpha = S5([2,1,3,5,4])


sage: alpha
(1,2)(4,5)

6.3

Sage , , S5

sage: S5.is_cyclic()
False
sage: S5.is_abelian()
False
sage: S5.order()
120

, S5 (
):

sage: S5.list()

28
6.4

= (13)(254) S5

sage: sigma.order()
6
sage: sigma.sign()
-1

1 , 1

6.5

, , S5 , :

sage: alpha = S5("(1,2)(4,5)")


sage: beta = S5("(1,2,3,4,5)")
sage: H = S5.subgroup([alpha, beta])

H order:

sage: H.order()
60

H ,

alternating group :

29
sage: A5 = AlternatingGroup(5)

A5 :

sage: A5.is_subgroup(S5)
True
sage: A5.order()
60

, H , A5 ?

sage: A5 == H
Ture

A5 simple?

sage: A5.is_simple()
True

, , Sage

30
7

Dihedral

Sage Dihedral Dn

7.1 Dihedral

Dihedral Group D4 , G

sage: D4 = DihedralGroup(4)

Sage D4 S4 , S4

7.2 Dihedral Group

D4 rotation, ip D4 :

31
sage: D4.gens()
[(1,2,3,4), (1,4)(2,3)]

Gallian , Gallian , R90 (1, 2, 3, 4),


H (1, 4)(2, 3)

7.3 Dihedral Group Cayley Table

Dihedral Group D4 Cayley table

sage: D4.cayley_table()
* a b c d e f g h
+----------------
a| a b c d e f g h
b| b a d c f e h g
c| c g a e d h b f
d| d h b f c g a e
e| e f g h a b c d
f| f e h g b a d c
g| g c e a h d f b
h| h d f b g c e a

, Sage LATEX latex


:

sage: latex(D4.cayley_table())
{\setlength{\arraycolsep}{2ex}
\begin{array}{r|*{8}{r}}

32
\multicolumn{1}{c|}{\ast}&a&b&c&d&e&f&g&h\\\hline
{}a&a&b&c&d&e&f&g&h\\
{}b&b&a&d&c&f&e&h&g\\
{}c&c&g&a&e&d&h&b&f\\
{}d&d&h&b&f&c&g&a&e\\
{}e&e&f&g&h&a&b&c&d\\
{}f&f&e&h&g&b&a&d&c\\
{}g&g&c&e&a&h&d&f&b\\
{}h&h&d&f&b&g&c&e&a\\
\end{array}}

LATEX , LATEX , :

a b c d e f g h
a a b c d e f g h
b b a d c f e h g
c c g a e d h b f
d d h b f c g a e
e e f g h a b c d
f f e h g b a d c
g g c e a h d f b
h h d f b g c e a

, a, b, c . . . D4

sage: list(D4)
[(), (2,4), (1,2)(3,4), (1,2,3,4), (1,3), (1,3)(2,4),
(1,4,3,2), (1,4)(2,3)]

Gallian , Cayley table, LATEX :

33
sage: latex(G.cayley_table(names = ["R_0", "D", "V",
"R_{90}", "D'", "R_{180}", "R_{270}", "H"]))

R0 D V R90 D R180 R270 H


R0 R0 D V R90 D R180 R270 H
D D R0 R90 V R180 D H R270
V V R270 R0 D R90 H D R180
R90 R90 H D R180 V R270 R0 D
D D R180 R270 H R0 D V R90
R180 R180 D H R270 D R0 R90 V
R270 R270 V D R0 H R90 R180 D
H H R90 R180 D R270 V D R0

Gallian D4 Cayley table, D4 Gallian


, Sage

7.4 Cayley Graph

D4

sage: show(D4.cayley_graph())

34
35
8

U (n)

8.1 Sage U (n)

Sage U (n) (),


fu,

U (50) , Z50 , :

sage: G = Integers(50)

U (50) order, U (50) group of units mod 50,


:

sage: G.unit_group_order()

order 20, :

36
sage: G.list_of_elements_of_multiplicative_group()

U (50) = [1, 3, 7, 9, 11, 13, 17, 19, 21, 23, 27, 29, 31, 33, 37, 39, 41, 43, 47, 49]

8.2 Sage U(n)

U (50) ,
3 ,

31 , 32 , 33 , . . .

, , mod

sage: 3^10 % 50

49, U (50) , 3
, , 7 , U (100)
? , , 3
Python , , a
U (50) G = Z50 ,

G = Z50 = {0, 1, 2, 3, . . .}

0 , 0 0, 1 1, ,
3 , G(3):

37
sage: a = G(3)

3, 3 U (50) 20 (?)
Python , 20

sage: [a^i for i in range(20)]

20 , ? Python,
, , ,
, , !

8.3 U (n) Cayley Table

Sage U (n) Zn , U (n) Cayley table


, Sage ,
U (n)

U (10) , U (10)

sage: G = Integers(10)
sage: H = G.list_of_elements_of_multiplicative_group()

H U (10)

sage: H
[1, 3, 7, 9]

38
multiplication_table , H

sage: G.multiplication_table(elements=H)
* a b c d
+--------
a| a b c d
b| b d a c
c| c a d b
d| d c b a

sage: G.multiplication_table(elements=H, names="elements")


* 1 3 7 9
+--------
1| 1 3 7 9
3| 3 9 1 7
7| 7 1 9 3
9| 9 7 3 1

39

You might also like