Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 218

Sage II

Linearna algebra

Vektorski prostori i vektori

R3 = VectorSpace(QQ, 3) (b1, b2, b3) = R3.basis() b1, b2, b3 vector1 = R3([-1, 2, 7]) vector2 = R3([4, -9, 2]) var('a b') a * vector1 + b * vector2
((1, 0, 0), (0, 1, 0), (0, 0, 1)) (a, b) (-a + 4*b, 2*a - 9*b, 7*a + 2*b)
sqrt(vector1 * vector1) vector1.norm() vector1.norm(2) vector1.norm(Infinity)
3*sqrt(6) 3*sqrt(6) 3*sqrt(6) 7
2 * vector1 vector1 * vector2 vector1.dot_product(vector2) vector1.cross_product(vector2)
(-2, 4, 14) -8 -8 (67, 30, 1)

Naredba VectorSpace kreira vektorski prostor gdje je prvi parametar polje (ili prsten) a drugi dimenzija. Tipičan izbor su ZZ, QQ, RR i CC, no Sage podržava izuzetno velik broj polja.

u = vector(QQ, [1, 2/7, 10/3]) u[1]
2/7
u[:2]
(1, 2/7)
u[2] = numerical_approx(pi, digits=5) u
(1, 2/7, 355/113)

Matrice

M4 = MatrixSpace(QQ, 4) show(M4.identity_matrix())
(1000010000100001)\displaystyle \left(\begin{array}{rrrr} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{array}\right)
M34 = MatrixSpace(QQ, 3, 4) show(M34.basis())
[(100000000000)\displaystyle \left(\begin{array}{rrrr} 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \end{array}\right), (010000000000)\displaystyle \left(\begin{array}{rrrr} 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \end{array}\right), (001000000000)\displaystyle \left(\begin{array}{rrrr} 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \end{array}\right), (000100000000)\displaystyle \left(\begin{array}{rrrr} 0 & 0 & 0 & 1 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \end{array}\right), (000010000000)\displaystyle \left(\begin{array}{rrrr} 0 & 0 & 0 & 0 \\ 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \end{array}\right), (000001000000)\displaystyle \left(\begin{array}{rrrr} 0 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 0 \end{array}\right), (000000100000)\displaystyle \left(\begin{array}{rrrr} 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 0 \end{array}\right), (000000010000)\displaystyle \left(\begin{array}{rrrr} 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 0 & 0 \end{array}\right), (000000001000)\displaystyle \left(\begin{array}{rrrr} 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 1 & 0 & 0 & 0 \end{array}\right), (000000000100)\displaystyle \left(\begin{array}{rrrr} 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \end{array}\right), (000000000010)\displaystyle \left(\begin{array}{rrrr} 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 \end{array}\right), (000000000001)\displaystyle \left(\begin{array}{rrrr} 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{array}\right)]
A = M4.matrix([[0, -1, -1, 1], [1, 1, 1, 1], [2, 4, 1, -2], [3, 1, -2, 2]]) b = vector(QQ, [0, 6, -1, 3]) show(A) show(b) A.solve_right(b) A\b
(0111111124123122)\displaystyle \left(\begin{array}{rrrr} 0 & -1 & -1 & 1 \\ 1 & 1 & 1 & 1 \\ 2 & 4 & 1 & -2 \\ 3 & 1 & -2 & 2 \end{array}\right)
(0,6,1,3)\displaystyle \left(0,\,6,\,-1,\,3\right)
(2, -1, 3, 2) (2, -1, 3, 2)
A[1]
(1, 1, 1, 1)
A[:,0]
[0] [1] [2] [3]
A[:,1] = vector([1,1,1,0]) A
[ 0 1 -1 1] [ 1 1 1 1] [ 2 1 1 -2] [ 3 0 -2 2]
A.row(2) A.column(2)
(2, 1, 1, -2) (-1, 1, 1, -2)
A.submatrix(2, 2, 2, 2)
[ 1 -2] [-2 2]
A = Matrix(QQ, [[1, 2, 3], [4, 5, 6], [7, 8, 9]]) A.rescale_row(1, 2) A
[ 1 2 3] [ 8 10 12] [ 7 8 9]
A.swap_rows(0, 1) A
[ 8 10 12] [ 1 2 3] [ 7 8 9]
A.add_multiple_of_row(0, 1 ,3) A
[11 16 21] [ 1 2 3] [ 7 8 9]
A.echelon_form() #Gaussova eliminacija
[ 1 0 -1] [ 0 1 2] [ 0 0 0]
M3 = MatrixSpace(QQ, 2, 3) A = M3.matrix([[3, 2, 1], [4, 5, 6]]) B = M3.matrix([[2, 2, 2], [1, 2, 3]]) show(A+B) show(1/2*A)
(543579)\displaystyle \left(\begin{array}{rrr} 5 & 4 & 3 \\ 5 & 7 & 9 \end{array}\right)
(321122523)\displaystyle \left(\begin{array}{rrr} \frac{3}{2} & 1 & \frac{1}{2} \\ 2 & \frac{5}{2} & 3 \end{array}\right)
var('a b c d e f') C = Matrix(QQ, [[4, 2, 1], [5, 3, 7]]) D = Matrix(SR, [[a, b], [c, d], [e, f]]) show(C * D)
(a, b, c, d, e, f)
(4a+2c+e4b+2d+f5a+3c+7e5b+3d+7f)\displaystyle \left(\begin{array}{rr} 4 \, a + 2 \, c + e & 4 \, b + 2 \, d + f \\ 5 \, a + 3 \, c + 7 \, e & 5 \, b + 3 \, d + 7 \, f \end{array}\right)
type(D)
<type 'sage.matrix.matrix_symbolic_dense.Matrix_symbolic_dense'>
var('x1 x2 x3') X = vector([x1,x2,x3]) show(C * X)
(x1, x2, x3)
(4x1+2x2+x3,5x1+3x2+7x3)\displaystyle \left(4 \, x_{1} + 2 \, x_{2} + x_{3},\,5 \, x_{1} + 3 \, x_{2} + 7 \, x_{3}\right)
A = matrix(QQ, [[2, 5, 4], [3, 1, 2], [5, 4, 6]]) A.det() A.rank() A.transpose() A.adjoint() A.inverse()
-16 3 [2 3 5] [5 1 4] [4 2 6] [ -2 -14 6] [ -8 -8 8] [ 7 17 -13] [ 1/8 7/8 -3/8] [ 1/2 1/2 -1/2] [ -7/16 -17/16 13/16]
A.adjoint()/A.det() == A.inverse()
True
A.norm(1) A.norm() A.norm('frob')
12.0 11.346960138635389 11.661903789690601
A = matrix([[1,2],[3,4]]) #ako ne specificiramo polje, bit će odabrano najmanje koje sadrži sve elemente matrice type(A)
<type 'sage.matrix.matrix_integer_dense.Matrix_integer_dense'>
show(block_matrix([[A,-A],[2*A, A^2]]))
(1212343424710681522)\displaystyle \left(\begin{array}{rr|rr} 1 & 2 & -1 & -2 \\ 3 & 4 & -3 & -4 \\ \hline 2 & 4 & 7 & 10 \\ 6 & 8 & 15 & 22 \end{array}\right)
A = matrix([[1,2,3],[4,5,6]]) show(block_matrix([1,A,0,0,-A,2], ncols=3))
(1012300014560000123200045602)\displaystyle \left(\begin{array}{rr|rrr|rr} 1 & 0 & 1 & 2 & 3 & 0 & 0 \\ 0 & 1 & 4 & 5 & 6 & 0 & 0 \\ \hline 0 & 0 & -1 & -2 & -3 & 2 & 0 \\ 0 & 0 & -4 & -5 & -6 & 0 & 2 \end{array}\right)
A = matrix(3,3,range(9)) A
[0 1 2] [3 4 5] [6 7 8]
R.<x> = PolynomialRing(GF(5),'x') A = random_matrix(R,2,3) A
[ 2*x^2 + 4 4*x^2 + x + 4 2*x] [3*x^2 + 3*x + 1 x^2 + 2*x + 1 x^2 + 1]

Spektar matrica

A = Matrix(QQ, [[2, -3, 1], [1, -2, 1], [1, -3, 2]]) ev = A.eigenvectors_right() for v in ev: show(v[0]) # sv. vrijednosti for v in ev: show(v[1]) # sv. vektori for v in ev: show(v[2]) # kratnosti
0\displaystyle 0
1\displaystyle 1
[(1,1,1)\displaystyle \left(1,\,1,\,1\right)]
[(1,0,1)\displaystyle \left(1,\,0,\,-1\right), (0,1,3)\displaystyle \left(0,\,1,\,3\right)]
1\displaystyle 1
2\displaystyle 2
A.eigenvalues()
[0, 1, 1]
D, P = A.eigenmatrix_right() show(D) show(P) A*P == P*D
(000010001)\displaystyle \left(\begin{array}{rrr} 0 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{array}\right)
(110101113)\displaystyle \left(\begin{array}{rrr} 1 & 1 & 0 \\ 1 & 0 & 1 \\ 1 & -1 & 3 \end{array}\right)
True
A = Matrix(RDF, [[1, -1, 4], [1, 4, -2], [1, 4, 2], [1, -1, 0]]) type(A)
<type 'sage.matrix.matrix_real_double_dense.Matrix_real_double_dense'>
Q, R = A.QR() show(Q) show(R)
(0.50.50.50.50.50.50.50.50.50.50.50.50.50.50.50.5)\displaystyle \left(\begin{array}{rrrr} -0.5 & 0.5 & -0.5 & -0.5 \\ -0.5 & -0.5 & 0.5 & -0.5 \\ -0.5 & -0.5 & -0.5 & 0.5 \\ -0.5 & 0.5 & 0.5 & 0.5 \end{array}\right)
(2.03.02.00.05.02.00.00.04.00.00.00.0)\displaystyle \left(\begin{array}{rrr} -2.0 & -3.0 & -2.0 \\ 0.0 & -5.0 & 2.0 \\ 0.0 & 0.0 & -4.0 \\ 0.0 & 0.0 & 0.0 \end{array}\right)
U, Sigma, V = A.SVD() show(U) show(Sigma) show(V)
(0.3132791204240.7715641563030.2377918110760.50.7480871267750.1468889483440.41083973470.50.5693222055090.618934107420.2068641993860.50.1345141991580.005741100539240.8554957451620.5)\displaystyle \left(\begin{array}{rrrr} -0.313279120424 & 0.771564156303 & -0.237791811076 & -0.5 \\ 0.748087126775 & -0.146888948344 & -0.4108397347 & -0.5 \\ 0.569322205509 & 0.61893410742 & 0.206864199386 & 0.5 \\ -0.134514199158 & 0.00574110053924 & -0.855495745162 & 0.5 \end{array}\right)
(6.003284668760.00.00.04.91120619160.00.00.01.356697066190.00.00.0)\displaystyle \left(\begin{array}{rrr} 6.00328466876 & 0.0 & 0.0 \\ 0.0 & 4.9112061916 & 0.0 \\ 0.0 & 0.0 & 1.35669706619 \\ 0.0 & 0.0 & 0.0 \end{array}\right)
(0.144856701070.2543876936090.9561921551110.9523837306050.2261919650940.2044564124860.2682941777870.9402787332850.209509278787)\displaystyle \left(\begin{array}{rrr} 0.14485670107 & 0.254387693609 & -0.956192155111 \\ 0.952383730605 & 0.226191965094 & 0.204456412486 \\ -0.268294177787 & 0.940278733285 & 0.209509278787 \end{array}\right)
A = matrix(QQ, [[2,4,3],[-4,-6,-3],[3,3,1]]) A.characteristic_polynomial() A.minimal_polynomial().factor()
x^3 + 3*x^2 - 4 (x - 1) * (x + 2)^2
show(A.jordan_form())
(100021002)\displaystyle \left(\begin{array}{r|rr} 1 & 0 & 0 \\ \hline 0 & -2 & 1 \\ 0 & 0 & -2 \end{array}\right)
show(A.jordan_form(transformation=True))
((100021002)\displaystyle \left(\begin{array}{r|rr} 1 & 0 & 0 \\ \hline 0 & -2 & 1 \\ 0 & 0 & -2 \end{array}\right), (111110101)\displaystyle \left(\begin{array}{rrr} 1 & 1 & 1 \\ -1 & -1 & 0 \\ 1 & 0 & -1 \end{array}\right))
A = matrix(QQ, [[1,-1/2],[-1/2,-1]]) A.jordan_form()
Error in lines 2-2 Traceback (most recent call last): File "/projects/sage/sage-7.5/local/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 995, in execute exec compile(block+'\n', '', 'single') in namespace, locals File "", line 1, in <module> File "sage/matrix/matrix2.pyx", line 10153, in sage.matrix.matrix2.Matrix.jordan_form (/projects/sage/sage-7.5/src/build/cythonized/sage/matrix/matrix2.c:76646) raise RuntimeError("Some eigenvalue does not exist in %s." %(A.base_ring())) RuntimeError: Some eigenvalue does not exist in Rational Field.
A.minimal_polynomial()
x^2 - 5/4
R = QQ[sqrt(5)] A = A.change_ring(R) show(A.jordan_form(transformation=True, subdivide=False))
((12sqrt50012sqrt5)\displaystyle \left(\begin{array}{rr} \frac{1}{2} \mathit{sqrt}_{5} & 0 \\ 0 & -\frac{1}{2} \mathit{sqrt}_{5} \end{array}\right), (11sqrt5+2sqrt5+2)\displaystyle \left(\begin{array}{rr} 1 & 1 \\ -\mathit{sqrt}_{5} + 2 & \mathit{sqrt}_{5} + 2 \end{array}\right))

Algebarske strukture

Permutacije, grupe

S5 = SymmetricGroup(5) S5.cardinality() S5.list() #ciklička notacija!
120 [(), (1,2), (1,2,3,4,5), (1,3,4,5), (2,3,4,5), (1,3,5,2,4), (1,2,4)(3,5), (1,3,4,5,2), (1,3,5)(2,4), (1,4,2,5,3), (1,4)(2,3,5), (1,3)(2,5,4), (2,4)(3,5), (1,4,3)(2,5), (1,5,4,3,2), (1,4)(2,5,3), (1,5,3)(2,4), (1,4)(3,5), (1,4,2,3,5), (1,5,3,2,4), (1,3,2,5,4), (1,5,4,2), (1,2,5,4,3), (1,4,3,2,5), (2,5,4,3), (1,5,4,2,3), (1,4,3,2), (1,4,2)(3,5), (1,5,4,3), (1,5,3,2), (1,5,2,4,3), (2,5,3), (4,5), (2,3), (1,5), (2,4,3), (1,3,2), (3,4), (2,5,4), (1,5,4)(2,3), (1,5,2), (1,5,3), (1,5)(2,4,3), (1,4,2), (1,4,3), (1,5,4), (1,2,4,3), (1,2)(4,5), (1,4), (1,2,3,5), (1,2,5,3), (1,2)(3,4), (1,5)(2,3), (3,4,5), (1,2,4,5), (1,3), (2,3)(4,5), (1,2,5), (1,5)(3,4), (2,4), (2,3,4), (1,2,3), (1,2,5,4), (2,5), (1,4,5), (1,2,3,4), (1,2,3)(4,5), (1,5)(2,3,4), (1,4,5)(2,3), (1,5,2,3,4), (1,2,3,5,4), (1,3,4), (1,4,5,2), (2,4,5), (1,2,5)(3,4), (1,2)(3,4,5), (1,3,2,4,5), (1,3,2,5), (1,5,2)(3,4), (1,2,4,3,5), (1,3,5), (1,3,4,2), (1,3,4)(2,5), (1,3)(2,4,5), (1,5,2,3), (1,2,4), (1,3,2)(4,5), (2,3,5), (1,3)(4,5), (1,4)(2,5), (1,3,5,2), (2,5)(3,4), (3,5), (2,4,5,3), (1,3)(2,5), (1,2,5,3,4), (1,5,3,4), (1,3,5,4), (2,3,5,4), (1,4,3,5), (1,3)(2,4), (1,2,4,5,3), (2,4,3,5), (1,4,5,2,3), (1,5,2,4), (1,3,4,2,5), (1,3,2,4), (1,2)(3,5), (1,4,5,3,2), (2,5,3,4), (1,4,2,5), (1,4,3,5,2), (1,4,5,3), (1,4)(2,3), (1,3,5,4,2), (1,5)(2,4), (1,5,3,4,2), (1,4,2,3), (3,5,4), (1,2)(3,5,4)]
S5.identity()
()
S5.random_element()
(1,3)(2,5)
r = S5.random_element() r r.domain() # tablična notacija
(3,4) [1, 2, 4, 3, 5]
s = S5('(1,3)(2,4)') s.domain()
[3, 4, 1, 2, 5]
t = S5([1,5,4,3,2]) t.domain()
[1, 5, 4, 3, 2]
t*s
(1,3,2,5,4)
t.order()
2
t*t
()
t.sign()
1
S4 = SymmetricGroup(4) S4.is_subgroup(S5)
True
S4.normal_subgroups()
[Subgroup of (Symmetric group of order 4! as a permutation group) generated by [()], Subgroup of (Symmetric group of order 4! as a permutation group) generated by [(1,3)(2,4), (1,4)(2,3)], Subgroup of (Symmetric group of order 4! as a permutation group) generated by [(2,4,3), (1,3)(2,4), (1,4)(2,3)], Subgroup of (Symmetric group of order 4! as a permutation group) generated by [(1,2), (1,2,3,4)]]
H = S5.subgroup([t,s]) H
Subgroup of (Symmetric group of order 5! as a permutation group) generated by [(2,5)(3,4), (1,3)(2,4)]
H.list()
[(), (1,3)(2,4), (2,5)(3,4), (1,3,2,5,4), (1,4,5,2,3), (1,2)(4,5), (1,4)(3,5), (1,2,4,3,5), (1,5,3,4,2), (1,5)(2,3)]
H.is_abelian() H.is_cyclic()
False False
S3 = SymmetricGroup(3) S3.cayley_table()
* a b c d e f +------------ a| a b c d e f b| b a f e d c c| c e d a f b d| d f a c b e e| e c b f a d f| f d e b c a
S3.cayley_table(names='elements')
* () (1,2) (1,2,3) (1,3,2) (2,3) (1,3) +------------------------------------------------ ()| () (1,2) (1,2,3) (1,3,2) (2,3) (1,3) (1,2)| (1,2) () (1,3) (2,3) (1,3,2) (1,2,3) (1,2,3)| (1,2,3) (2,3) (1,3,2) () (1,3) (1,2) (1,3,2)| (1,3,2) (1,3) () (1,2,3) (1,2) (2,3) (2,3)| (2,3) (1,2,3) (1,2) (1,3) () (1,3,2) (1,3)| (1,3) (1,3,2) (2,3) (1,2) (1,2,3) ()
S3.cayley_table(names=['id','u1','u3','r1','r2','u2'])
* id u1 u3 r1 r2 u2 +------------------ id| id u1 u3 r1 r2 u2 u1| u1 id u2 r2 r1 u3 u3| u3 r2 r1 id u2 u1 r1| r1 u2 id u3 u1 r2 r2| r2 u3 u1 u2 id r1 u2| u2 r1 r2 u1 u3 id
r = '(1,3)(2,4)(5)' s = '(1,3,2)' K = PermutationGroup([r,s]) K
Permutation Group with generators [(1,3,2), (1,3)(2,4)]
K.order()
12
D = DihedralGroup(4) D.list() #prikazana kao podgrupa permutacija
[(), (1,4)(2,3), (1,2,3,4), (1,3)(2,4), (1,3), (2,4), (1,4,3,2), (1,2)(3,4)]
D.subgroups()
[Subgroup of (Dihedral group of order 8 as a permutation group) generated by [()], Subgroup of (Dihedral group of order 8 as a permutation group) generated by [(1,3)(2,4)], Subgroup of (Dihedral group of order 8 as a permutation group) generated by [(2,4)], Subgroup of (Dihedral group of order 8 as a permutation group) generated by [(1,3)], Subgroup of (Dihedral group of order 8 as a permutation group) generated by [(1,2)(3,4)], Subgroup of (Dihedral group of order 8 as a permutation group) generated by [(1,4)(2,3)], Subgroup of (Dihedral group of order 8 as a permutation group) generated by [(2,4), (1,3)(2,4)], Subgroup of (Dihedral group of order 8 as a permutation group) generated by [(1,2,3,4), (1,3)(2,4)], Subgroup of (Dihedral group of order 8 as a permutation group) generated by [(1,2)(3,4), (1,3)(2,4)], Subgroup of (Dihedral group of order 8 as a permutation group) generated by [(2,4), (1,2,3,4), (1,3)(2,4)]]
D.center()
Subgroup of (Dihedral group of order 8 as a permutation group) generated by [(1,3)(2,4)]
D.center().list()
[(), (1,3)(2,4)]
D.centralizer(D('(1,3)(2,4)'))
Subgroup of (Dihedral group of order 8 as a permutation group) generated by [(1,2,3,4), (1,4)(2,3)]
A4 = AlternatingGroup(4) A4.cardinality()
12
g1 = A4('(1,4)(3,2)') ; g2 = A4('(2,4)(1,3)') H = A4.subgroup([g1,g2]); H.is_normal(A4);
True
Hd = A4.cosets(H, side = 'right') Hl = A4.cosets(H, side = 'left') print "Hd = ", Hd print "Hl = ", Hl
Hd = [[(), (1,2)(3,4), (1,3)(2,4), (1,4)(2,3)], [(2,3,4), (1,3,2), (1,4,3), (1,2,4)], [(2,4,3), (1,4,2), (1,2,3), (1,3,4)]] Hl = [[(), (1,2)(3,4), (1,3)(2,4), (1,4)(2,3)], [(2,3,4), (1,2,4), (1,3,2), (1,4,3)], [(2,4,3), (1,2,3), (1,3,4), (1,4,2)]]
A4.conjugacy_classes_representatives()
[(), (1,2)(3,4), (1,2,3), (1,2,4)]
B = DiCyclicGroup(3) A4.is_isomorphic(B)
False
T = [s for s in S5 if s.order() == 2] T
[(1,2), (2,4)(3,5), (1,4)(3,5), (4,5), (2,3), (1,5), (3,4), (1,2)(4,5), (1,4), (1,2)(3,4), (1,5)(2,3), (1,3), (2,3)(4,5), (1,5)(3,4), (2,4), (2,5), (1,3)(4,5), (1,4)(2,5), (2,5)(3,4), (3,5), (1,3)(2,5), (1,3)(2,4), (1,2)(3,5), (1,4)(2,3), (1,5)(2,4)]

Prsteni polinoma

R.<x>=PolynomialRing(ZZ) #x je varijabla polinoma iz polja Z R
Univariate Polynomial Ring in x over Integer Ring
p = 2*x^2 + (1/2)*x + (3/5) parent(p)
Univariate Polynomial Ring in x over Rational Field
S.<y>=PolynomialRing(ZZ) (1/2)*y in S
False
f=x+1 g=x^2+x-1 h=1/2*x+3/4 parent(f), parent(g), parent(h) f+g g-h f*g h^3
(Univariate Polynomial Ring in x over Integer Ring, Univariate Polynomial Ring in x over Integer Ring, Univariate Polynomial Ring in x over Rational Field) x^2 + 2*x x^2 + 1/2*x - 7/4 x^3 + 2*x^2 - 1 1/8*x^3 + 9/16*x^2 + 27/32*x + 27/64
f/g parent(f/g)
(x + 1)/(x^2 + x - 1) Fraction Field of Univariate Polynomial Ring in x over Integer Ring
f=x^6+x^2+1 g=x^3+x+1 f // g f % g
x^3 - x - 1 2*x^2 + 2*x + 2
p = x^4 + 2*x^3 + 2*x^2 + 2*x + 1 q = x^4 - 1 gcd(p,q)
x^3 + x^2 + x + 1
(x^3+x+1).is_irreducible()
True
(x^3+1).is_irreducible()
False
R.<x,y,z> = PolynomialRing(QQ, 3) p = -1/2*x - y*z - y + 8*z^2 p.monomials() p.coefficients() [ a*b for a,b in zip(p.coefficients(),p.monomials())]
[y*z, z^2, x, y] [-1, 8, -1/2, -1] [-y*z, 8*z^2, -1/2*x, -y]
p.lc() #vodeći koeficijent p.lt() #vodeći član
-1 -y*z
p.total_degree() p.exponents()
2 [(0, 1, 1), (0, 0, 2), (1, 0, 0), (0, 1, 0)]

Elementarna teorija brojeva

gcd(2776, 2452)
4
a = 633 b = 331 ext = xgcd(a, b) ext[0] == ext[1]*a + ext[2]*b
True
factor(2600) prime_divisors(2600)
2^3 * 5^2 * 13 [2, 5, 13]
inverse_mod(352, 917)
508

multiplikativni inverz od 352 mod 917; znači postoji m takav da vrijedi 352*508 == m*917+1

euler_phi(345)
176
m = random_prime(10000) n = random_prime(10000) euler_phi(m*n) == euler_phi(m) * euler_phi(n)
True
is_prime(14547073)
False
p = random_prime(10^21, True) #bez `True` računanje će biti puno brže, uz malu šansu da broj nije prost is_prime(p)
True
prime_range(500, 550) primes_first_n(20)
[503, 509, 521, 523, 541, 547] [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71]
Kongruencije: ab(modn)a\equiv b \pmod{n}. Broj bb možemo izračunati pomoću sljedećeg koda: mod(a,n)
mod(23, 5)
3
ammodna^m \mod n
power_mod(15, 831, 23)
10

Kombinatorika

boje = Set(["pik", "herc", "karo", "tref"]) vrijednosti = Set([2, 3, 4, 5, 6, 7, 8, 9, 10,"luda", "kraljica", "kralj", "as"]) karte = cartesian_product([vrijednosti, boje]) karte.random_element()
(3, 'pik')
ruke = Subsets(karte, 5) ruke.random_element()
{('as', 'karo'), (9, 'herc'), (9, 'pik'), ('kralj', 'pik'), (3, 'pik')}
ruke.cardinality()
2598960
S = Subsets([1,2,3,4], 2) S.random_element() S.cardinality()
{1, 4} 6
E = Set([1,2,3,4]) S = Subsets(Subsets(Subsets(E))) S.cardinality()
2003529930406846464979072351560255750447825475569751419265016973710894059556311453089506130880933348101038234342907263181822949382118812668869506364761547029165041871916351587966347219442930927982084309104855990570159318959639524863372367203002916969592156108764948889254090805911457037675208500206671563702366126359747144807111774815880914135742720967190151836282560618091458852699826141425030123391108273603843767876449043205960379124490905707560314035076162562476031863793126484703743782954975613770981604614413308692118102485959152380195331030292162800160568670105651646750568038741529463842244845292537361442533614373729088303794601274724958414864915930647252015155693922628180691650796381064132275307267143998158508811292628901134237782705567421080070065283963322155077831214288551675554073345107213112427399562982719769150054883905223804357045848197956393157853510018992000024141963706813559840464039472194016069517690156119726982337890017641517190051133466306898140219383481435426387306539552969691388024158161859561100640362119796101859534802787167200122604642492385111393400464351623867567078745259464670903886547743483217897012764455529409092021959585751622973333576159552394885297579954028471943529913543763705986928913757153740001986394332464890052543106629669165243419174691389632476560289415199775477703138064781342309596190960654591300890188887588084733625956065444888501447335706058817090162108499714529568344061979690565469813631162053579369791403236328496233046421066136200220175787851857409162050489711781820400187282939943446186224328009837323764931814789848119452713007440220765680910376203999203492023906626264491909167985461515778839060397720759279378852241294301017458086862263369284725851403039615558564330385450688652213114813638408384778263790459607186876728509763471271988890680478243230394718650525660978150729861141430305816927924971409161059417185352275887504477592218301158780701975535722241400019548102005661773589781499532325208589753463547007786690406429016763808161740550405117670093673202804549339027992491867306539931640720492238474815280619166900933805732120816350707634351669869625020969023162859350071874190579161241536897514808261904847946571736601005892476655445840838334790544144817684255327207315586349347605137419779525190365032198020108764738368682531025183377533908861426184800374008082238104076468878471647552945326947661700424461063311238021134588694532200116564076327023074292426051582811070387018345324567635625951430032037432740780879056283663406965030844225855967039271869461158513793386475699748568670079823960604393478850861649260304945061743412365828352144806726676841807083754862211408236579802961200027441324438432402331257403545019352428776430880232850855886089962774458164680857875115807014743763867976955049991643998284357290415378143438847303484261903388841494031366139854257635577105335580206622185577060082551288893332226436281984838613239570676191409638533832374343758830859233722284644287996245605476932428998432652677378373173288063210753211238680604674708428051166488709084770291208161104912555598322366244868556651402684641209694982590565519216188104341226838996283071654868525536914850299539675503954938371853405900096187489473992880432496373165753803673586710175783994818471798498246948060532081996066183434012476096639519778021441199752546704080608499344178256285092726523709898651539462193004607364507926212975917698293892367015170992091531567814439791248475706237804600009918293321306880570046591458387208088016887445835557926258465124763087148566313528934166117490617526671492672176128330845273936469244582892571388877839056300482483799839692029222215486145902373478222682521639957440801727144146179559226175083889020074169926238300282286249284182671243405751424188569994272331606998712986882771820617214453142574944015066139463169197629181506579745526236191224848063890033669074365989226349564114665503062965960199720636202603521917776740668777463549375318899587866282125469797102065747232721372918144666659421872003474508942830911535189271114287108376159222380276605327823351661555149369375778466670145717971901227117812780450240026384758788339396817962950690798817121690686929538248529830023476068454114178139110648560236549754227497231007615131870024053910510913817843721791422528587432098524957878034683703337818421444017138688124249984418618129271198533315382567321870421530631197748535214670955334626336610864667332292409879849256691109516143618601548909740241913509623043612196128165950518666022030715613684732364660868905014263913906515063908199378852318365059897299125404479443425166774299659811849233151555272883274028352688442408752811283289980625912673699546247341543333500147231430612750390307397135252069338173843322950701049061867539433130784798015655130384758155685236218010419650255596181934986315913233036096461905990236112681196023441843363334594927631946101716652913823717182394299216272538461776065694542297877071383198817036964588689811863210976900355735884624464835706291453052757101278872027965364479724025405448132748391794128826423835171949197209797145936887537198729130831738033911016128547415377377715951728084111627597186384924222802373441925469991983672192131287035585307966942713416391033882754318613643490100943197409047331014476299861725424423355612237435715825933382804986243892498222780715951762757847109475119033482241412025182688713728193104253478196128440176479531505057110722974314569915223451643121848657575786528197564843508958384722923534559464521215831657751471298708225909292655638836651120681943836904116252668710044560243704200663709001941185557160472044643696932850060046928140507119069261393993902735534545567470314903886022024639948260501762431969305640666366626090207048887438898907498152865444381862917382901051820869936382661868303915273264581286782806601337500096593364625146091723180312930347877421234679118454791311109897794648216922505629399956793483801699157439700537542134485874586856047286751065423341893839099110586465595113646061055156838541217459801807133163612573079611168343863767667307354583494789788316330129240800836356825939157113130978030516441716682518346573675934198084958947940983292500086389778563494693212473426103062713745077286156922596628573857905533240641849018451328284632709269753830867308409142247659474439973348130810986399417379789657010687026734161967196591599588537834822988270125605842365589539690306474965584147981310997157542043256395776070485100881578291408250777738559790129129407309462785944505859412273194812753225152324801503466519048228961406646890305102510916237770448486230229488966711380555607956620732449373374027836767300203011615227008921843515652121379215748206859356920790214502277133099987729459596952817044582181956080965811702798062669891205061560742325686842271306295009864421853470810407128917646906550836129916694778023822502789667843489199409657361704586786242554006942516693979292624714524945408858422726153755260071904336329196375777502176005195800693847635789586878489536872122898557806826518192703632099480155874455575175312736471421295536494084385586615208012115079075068553344489258693283859653013272046970694571546959353658571788894862333292465202735853188533370948455403336565356988172582528918056635488363743793348411845580168331827676834646291995605513470039147876808640322629616641560667508153710646723108461964247537490553744805318226002710216400980584497526023035640038083472053149941172965736785066421400842696497103241919182121213206939769143923368374709228267738708132236680086924703491586840991153098315412063566123187504305467536983230827966457417620806593177265685841681837966106144963432544111706941700222657817358351259821080769101961052229263879745049019254311900620561906577452416191913187533984049343976823310298465893318373015809592522829206820862230332585280119266496314441316442773003237792274712330696417149945532261035475145631290668854345426869788447742981777493710117614651624183616680254815296335308490849943006763654806102940094693750609845588558043970485914449584445079978497045583550685408745163316464118083123079704389849190506587586425810738422420591191941674182490452700288263983057950057341711487031187142834184499153456702915280104485145176055306971441761368582384102787659324662689978418319620312262421177391477208004883578333569204533935953254564897028558589735505751235129536540502842081022785248776603574246366673148680279486052445782673626230852978265057114624846595914210278122788941448163994973881884622768244851622051817076722169863265701654316919742651230041757329904473537672536845792754365412826553581858046840069367718605020070547247548400805530424951854495267247261347318174742180078574693465447136036975884118029408039616746946288540679172138601225419503819704538417268006398820656328792839582708510919958839448297775647152026132871089526163417707151642899487953564854553553148754978134009964854498635824847690590033116961303766127923464323129706628411307427046202032013368350385425360313636763575212604707425311209233402837482949453104727418969287275572027615272268283376741393425652653283068469997597097750005560889932685025049212884068274139881631540456490350775871680074055685724021758685439053228133770707415830756269628316955687424060527726485853050611356384851965918968649596335568216975437621430778665934730450164822432964891270709898076676625671517269062058815549666382573829274182082278960684488222983394816670984039024283514306813767253460126007269262969468672750794346190439996618979611928750519442356402644303271737341591281496056168353988188569484045342311424613559925272330064881627466723523751234311893442118885085079358163848994487544756331689213869675574302737953785262542329024881047181939037220666894702204258836895840939998453560948869946833852579675161882159410981624918741813364726965123980677561947912557957446471427868624053750576104204267149366084980238274680575982591331006919941904651906531171908926077949119217946407355129633864523035673345588033313197080365457184791550432654899559705862888286866606618021882248602144999973122164138170653480175510438406624412822803616648904257377640956326482825258407669045608439490325290526337532316509087681336614242398309530806549661879381949120033919489494065132398816642080088395554942237096734840072642705701165089075196155370186264797456381187856175457113400473810762763014953309735174180655479112660938034311378532532883533352024934365979129341284854970946826329075830193072665337782559314331110963848053940859283988907796210479847919686876539987477095912788727475874439806779824968278272200926449944559380414608770641941810440758269805688038949654616587983904660587645341810289907194293021774519976104495043196841503455514044820928933378657363052830619990077748726922998608279053171691876578860908941817057993404890218441559791092676862796597583952483926734883634745651687016166240642424241228961118010615682342539392180052483454723779219911228595914191877491793823340010078128326506710281781396029120914720100947878752551263372884222353869490067927664511634758101193875319657242121476038284774774571704578610417385747911301908583877890152334343013005282797038580359815182929600305682612091950943737325454171056383887047528950563961029843641360935641632589408137981511693338619797339821670761004607980096016024823096943043806956620123213650140549586250615282588033022908385812478469315720323233601899469437647726721879376826431828382603564520699468630216048874528424363593558622333506235945002890558581611275341783750455936126130852640828051213873177490200249552738734585956405160830583053770732533971552620444705429573538361113677523169972740292941674204423248113875075631319078272188864053374694213842169928862940479635305150560788126366206497231257579019598873041195626227343728900516561111094111745277965482790471250581999077498063821559376885546498822938985408291325129076478386322494781016753491693489288104203015610283386143827378160946341335383578340765314321417150655877547820252454780657301342277470616744241968952613164274104695474621483756288299771804186785084546965619150908695874251184435837306590951460980451247409411373899927822492983367796011015387096129749705566301637307202750734759922943792393824427421186158236161317886392553095117188421298508307238259729144142251579403883011359083331651858234967221259621812507058113759495525022747274674369887131926670769299199084467161228738858457584622726573330753735572823951616964175198675012681745429323738294143824814377139861906716657572945807804820559511881687188075212971832636442155336787751274766940790117057509819575084563565217389544179875074523854455200133572033332379895074393905312918212255259833790909463630202185353848854825062897715616963860712382771725621313460549401770413581731931763370136332252819127547191443450920711848838366818174263342949611870091503049165339464763717766439120798347494627397822171502090670190302469762151278521956142070806461631373236517853976292092025500288962012970141379640038055734949269073535145961208674796547733692958773628635660143767964038430796864138563447801328261284589184898528048048844180821639423974014362903481665458114454366460032490618763039502356402044530748210241366895196644221339200757479128683805175150634662569391937740283512075666260829890491877287833852178522792045771846965855278790447562192663992008409302075673925363735628390829817577902153202106409617373283598494066652141198183810884515459772895164572131897797907491941013148368544639616904607030107596818933741217575988165127000761262789169510406315857637534787420070222051070891257612361658026806815858499852631465878086616800733264676830206391697203064894405628195406190685242003053463156621891327309069687353181641094514288036605995220248248886711554429104721929134248346438705368508648749099178812670565665387191049721820042371492740164460943459845392536706132210616533085662021188968234005752675486101476993688738209584552211571923479686888160853631615862880150395949418529489227074410828207169303387818084936204018255222271010985653444817207470756019245915599431072949578197878590578940052540122867517142511184356437184053563024181225473266093302710397968091064939272722683035410467632591355279683837705019855234621222858410557119921731717969804339317707750755627056047831779844447637560254637033369247114220815519973691371975163241302748712199863404548248524570118553342675264715978310731245663429805221455494156252724028915333354349341217862037007260315279870771872491234494477147909520734761385425485311552773301030342476835865496093722324007154518129732692081058424090557725645803681462234493189708138897143299831347617799679712453782310703739151473878692119187566700319321281896803322696594459286210607438827416919465162267632540665070881071030394178860564893769816734159025925194611823642945652669372203155504700213598846292758012527715422016629954863130324912311029627923723899766416803497141226527931907636326136814145516376656559839788489381733082668779901962886932296597379951931621187215455287394170243669885593888793316744533363119541518404088283815193421234122820030950313341050704760159987985472529190665222479319715440331794836837373220821885773341623856441380700541913530245943913502554531886454796252260251762928374330465102361057583514550739443339610216229675461415781127197001738611494279501411253280621254775810512972088465263158094806633687670147310733540717710876615935856814098212967730759197382973441445256688770855324570888958320993823432102718224114763732791357568615421252849657903335093152776925505845644010552192644505312073756287744998163646332835816140330175813967359427327690448920361880386754955751806890058532927201493923500525845146706982628548257883267398735220457228239290207144822219885587102896991935873074277815159757620764023951243860202032596596250212578349957710085626386118233813318509014686577064010676278617583772772895892746039403930337271873850536912957126715066896688493880885142943609962012966759079225082275313812849851526902931700263136328942095797577959327635531162066753488651317323872438748063513314512644889967589828812925480076425186586490241111127301357197181381602583178506932244007998656635371544088454866393181708395735780799059730839094881804060935959190907473960904410150516321749681412100765719177483767355751000733616922386537429079457803200042337452807566153042929014495780629634138383551783599764708851349004856973697965238695845994595592090709058956891451141412684505462117945026611750166928260250950770778211950432617383223562437601776799362796099368975191394965033358507155418436456852616674243688920371037495328425927131610537834980740739158633817967658425258036737206469351248652238481341663808061505704829059890696451936440018597120425723007316410009916987524260377362177763430621616744884930810929901009517974541564251204822086714586849255132444266777127863728211331536224301091824391243380214046242223349153559516890816288487989988273630445372432174280215755777967021666317047969728172483392841015642274507271779269399929740308072770395013581545142494049026536105825409373114653104943382484379718606937214444600826798002471229489405761853892203425608302697052876621377373594394224114707074072902725461307358541745691419446487624357682397065703184168467540733466346293673983620004041400714054277632480132742202685393698869787607009590048684650626771363070979821006557285101306601010780633743344773073478653881742681230743766066643312775356466578603715192922768440458273283243808212841218776132042460464900801054731426749260826922155637405486241717031027919996942645620955619816454547662045022411449404749349832206807191352767986747813458203859570413466177937228534940031631599544093684089572533438702986717829770373332806801764639502090023941931499115009105276821119510999063166150311585582835582607179410052528583611369961303442790173811787412061288182062023263849861515656451230047792967563618345768105043341769543067538041113928553792529241347339481050532025708728186307291158911335942014761872664291564036371927602306283840650425441742335464549987055318726887926424102147363698625463747159744354943443899730051742525110877357886390946812096673428152585919924857640488055071329814299359911463239919113959926752576359007446572810191805841807342227734721397723218231771716916400108826112549093361186780575722391018186168549108500885272274374212086524852372456248697662245384819298671129452945515497030585919307198497105414181636968976131126744027009648667545934567059936995464500558921628047976365686133316563907395703272034389175415267500915011198856872708848195531676931681272892143031376818016445477367518353497857924276463354162433601125960252109501612264110346083465648235597934274056868849224458745493776752120324703803035491157544831295275891939893680876327685438769557694881422844311998595700727521393176837831770339130423060958999137314684569010422095161967070506420256733873446115655276175992727151877660010238944760539789516945708802728736225121076224091810066700883474737605156285533943565843756271241244457651663064085939507947550920463932245202535463634444791755661725962187199279186575490857852950012840229035061514937310107009446151011613712423761426722541732055959202782129325725947146417224977321316381845326555279604270541871496236585252458648933254145062642337885651464670604298564781968461593663288954299780722542264790400616019751975007460545150060291806638271497016110987951336633771378434416194053121445291855180136575558667615019373029691932076120009255065081583275508499340768797252369987023567931026804136745718956641431852679054717169962990363015545645090044802789055701968328313630718997699153166679208958768572290600915472919636381673596673959975710326015571920237348580521128117458610065152598883843114511894880552129145775699146577530041384717124577965048175856395072895337539755822087777506072339445587895905719156736
C = SetPartitions([1,2,3]) C.list()
[{{1, 2, 3}}, {{1}, {2, 3}}, {{1, 3}, {2}}, {{1, 2}, {3}}, {{1}, {2}, {3}}]
[[binomial(n,i) for i in range(n+1)] for n in range(10)]
[[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1], [1, 5, 10, 10, 5, 1], [1, 6, 15, 20, 15, 6, 1], [1, 7, 21, 35, 35, 21, 7, 1], [1, 8, 28, 56, 70, 56, 28, 8, 1], [1, 9, 36, 84, 126, 126, 84, 36, 9, 1]]
C = Permutations(4) C
Standard permutations of 4
C.list()
[[1, 2, 3, 4], [1, 2, 4, 3], [1, 3, 2, 4], [1, 3, 4, 2], [1, 4, 2, 3], [1, 4, 3, 2], [2, 1, 3, 4], [2, 1, 4, 3], [2, 3, 1, 4], [2, 3, 4, 1], [2, 4, 1, 3], [2, 4, 3, 1], [3, 1, 2, 4], [3, 1, 4, 2], [3, 2, 1, 4], [3, 2, 4, 1], [3, 4, 1, 2], [3, 4, 2, 1], [4, 1, 2, 3], [4, 1, 3, 2], [4, 2, 1, 3], [4, 2, 3, 1], [4, 3, 1, 2], [4, 3, 2, 1]]
C = Compositions(5) C.list() list(Compositions(5, max_length=2))
[[1, 1, 1, 1, 1], [1, 1, 1, 2], [1, 1, 2, 1], [1, 1, 3], [1, 2, 1, 1], [1, 2, 2], [1, 3, 1], [1, 4], [2, 1, 1, 1], [2, 1, 2], [2, 2, 1], [2, 3], [3, 1, 1], [3, 2], [4, 1], [5]] [[5], [4, 1], [3, 2], [2, 3], [1, 4]]
C = Combinations(range(4)) C.list()
[[], [0], [1], [2], [3], [0, 1], [0, 2], [0, 3], [1, 2], [1, 3], [2, 3], [0, 1, 2], [0, 1, 3], [0, 2, 3], [1, 2, 3], [0, 1, 2, 3]]
Combinations([1,2,2,3]).list()
[[], [1], [2], [3], [1, 2], [1, 3], [2, 2], [2, 3], [1, 2, 2], [1, 2, 3], [2, 2, 3], [1, 2, 2, 3]]
C = cartesian_product([Compositions(8), Permutations(20)]) C
The Cartesian product of (Compositions of 8, Standard permutations of 20)
C.cardinality()
311411457046609920000
D = Derangements(4) D.list()
[[2, 3, 4, 1], [4, 3, 1, 2], [2, 4, 1, 3], [3, 4, 2, 1], [3, 1, 4, 2], [4, 1, 2, 3], [4, 3, 2, 1], [3, 4, 1, 2], [2, 1, 4, 3]]

Teorija grafova

Nađite najveći podskup prirodnih brojeva manjih od 100 takvih da za svaki par (a,b)(a,b) vrijedi da aba-b nije kvadrat prirodnog broja.

n = 100 g=Graph(n) kvadrati = [i*i for i in range(sqrt(n))] bridovi = [(i,j) for (i,j) in CartesianProduct(range(n),range(n)) if (i!=j and abs(i-j) in kvadrati)] g.add_edges(bridovi) g.independent_set()
[3, 5, 8, 10, 15, 20, 22, 25, 27, 32, 37, 42, 49, 55, 60, 70, 75, 77, 82, 87, 92, 94, 97, 99]
g = graphs.CompleteGraph(5)
show(g)
d3-based renderer not yet implemented

Ako želimo koristiti tikz za prikzazivanje grafova

_=latex.eval(latex(g)) #https://github.com/sagemath/cloud/wiki/FAQ#tikzhttps://cloud.sagemath.com/blobs//tmp/tmpNsiUPX.png?uuid=f14a29da-dc1c-40b1-a75a-191265315dc2
G = Graph() G.add_vertices(range(10)) G.add_cycle(range(10)) show(G)
d3-based renderer not yet implemented
D = DiGraph() D.add_cycle(range(4)) show(D)
d3-based renderer not yet implemented
d = {0: [1,4,5], 1: [2,6], 2: [3,7], 3: [4,8], 4: [9], 5: [7,8], 6: [8,9], 7: [9]} G = Graph(d) show(G)
d3-based renderer not yet implemented
G.add_vertices([10,11,12]) G.vertices()
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
G.adjacency_matrix()
[0 1 0 0 1 1 0 0 0 0 0 0 0] [1 0 1 0 0 0 1 0 0 0 0 0 0] [0 1 0 1 0 0 0 1 0 0 0 0 0] [0 0 1 0 1 0 0 0 1 0 0 0 0] [1 0 0 1 0 0 0 0 0 1 0 0 0] [1 0 0 0 0 0 0 1 1 0 0 0 0] [0 1 0 0 0 0 0 0 1 1 0 0 0] [0 0 1 0 0 1 0 0 0 1 0 0 0] [0 0 0 1 0 1 1 0 0 0 0 0 0] [0 0 0 0 1 0 1 1 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0 0 0 0]
g = graphs.CompleteGraph(5) g.average_degree()
4

RandomGNP(n,p) vraća slučajan graf s n vrhova, s time da je svaki brid ubačen s vjerojatnošću p.

g = graphs.RandomGNP(20,0.5) g.min_spanning_tree() #default je Kruskalov algoritam
[(0, 1, None), (0, 7, None), (0, 10, None), (0, 11, None), (0, 16, None), (2, 11, None), (3, 11, None), (4, 16, None), (5, 11, None), (6, 11, None), (8, 14, None), (9, 16, None), (11, 14, None), (12, 16, None), (13, 14, None), (14, 15, None), (16, 17, None), (16, 18, None), (16, 19, None)]
g = graphs.CompleteGraph(5) tezina = lambda e: 1 / ((e[0] + 1) * (e[1] + 1)) g.min_spanning_tree(algorithm='Prim_fringe', starting_vertex=2, weight_function=tezina) #Primov algoritam
[(0, 4, None), (1, 4, None), (2, 4, None), (3, 4, None)]
D = DiGraph({0 : [1, 3], 1 : [2], 2 : [3], 4 : [5, 6], 5 : [6]}) D.connected_components_number() D.connected_components()
2 [[0, 1, 2, 3], [4, 5, 6]]
P = graphs.PetersenGraph() P.degree(5) P.degree()
3 [3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
G = graphs.CompleteGraph(19) G.size() G.delete_edges( [ (5, 6), (7, 8) ] ) G.size()
171 169
G = graphs.CycleGraph(9) show(G)
d3-based renderer not yet implemented
G.distance(0,5)
4
G = graphs.CompleteGraph(7) G.is_eulerian() G.eulerian_circuit(labels=False)
True [(0, 6), (6, 5), (5, 4), (4, 6), (6, 3), (3, 5), (5, 2), (2, 4), (4, 3), (3, 2), (2, 6), (6, 1), (1, 5), (5, 0), (0, 4), (4, 1), (1, 3), (3, 0), (0, 2), (2, 1), (1, 0)]
g = G.random_subgraph(0.25) show(g)
d3-based renderer not yet implemented
g = graphs.CubeGraph(4) g.is_planar()
False
g = graphs.CubeGraph(3) g.is_planar()
True
G = Graph( { 0 : [1, 2], 1 : [2], 3 : [4, 5], 4 : [5] } ) show(G)
d3-based renderer not yet implemented
G.is_connected()
False
G.add_edge(0,3) G.is_connected()
True
D = graphs.DodecahedralGraph() D.shortest_path(4, 9)
[4, 17, 16, 12, 13, 9]
n = 11 G = graphs.CompleteGraph(n) ST = G.spanning_trees_count() ST == n^(n-2)
True

Kriptografija

RSA

Neka su pp, qq (veliki) prosti brojevi. Neka je n=pqn=p\ast q. Znamo φ(n)=(p1)(q1)\varphi(n)=(p-1)(q-1).

Neka je ee tzv. enkripcijski eksponent, bilo koji broj koji je relativno prost s φ(n)\varphi(n). Kako su ee i φ(n)\varphi(n) relativno prosti, postoji multiplikativni inverz dd: ed1(modφ(n))ed \equiv 1\pmod{\varphi(n)}. Broj dd zovemo dekripcijski eksponent. Parametar (n,e)(n,e) je javan, dok su faktorizacija n=pqn=pq te broj dd tajni. Dekripcija se obavlja funkcijom f(x)=xef(x)=x^e a enkripcija funkcijom g(y)=ydmodng(y)=y^d \mod n (pretpostavljamo x<nx\lt n).

Par (n,e)(n,e) zovemo javni ključ a trojku (p,q,d)(p,q,d) privatni ključ.

p = (2^31) - 1 is_prime(p)
True
q = (2^61) - 1 is_prime(q)
True
n = p * q n
4951760154835678088235319297
phi = (p - 1)*(q - 1) phi
4951760152529835076874141700
e = ZZ.random_element(phi) while gcd(e, phi) != 1: e = ZZ.random_element(phi)
e
3466954595038139443252761563
e < n
True

ed1(modφ(n))ed \equiv 1\pmod{\varphi(n)} je ekvivalentno s dekφ(b)=1de-k\varphi(b)=1, pa dd možemo odrediti Euklidovim algoritmom.

b = xgcd(e, phi) d = Integer(mod(b[1], phi)) d
329419668378137302564189727
mod(d * e, phi)
1
(n,e) #javni ključ
(4951760154835678088235319297, 3466954595038139443252761563)
(p,q,d) #privatni ključ
(2147483647, 2305843009213693951, 329419668378137302564189727)
x = "ZDRAVO!" #želimo šifrirati m = map(ord, x) m
[90, 68, 82, 65, 86, 79, 33]
m=ZZ(m,base=100) m
33798665826890
c = power_mod(m, e, n) c #šifrirani tekst
4252744825524301971255601045
power_mod(c, d, n) == m
True

Ovdje smo samo zagrebali površinu mogućnosti u Sage-u. Ovo je lista modula iz dokumentacije:

Integers, Rationals, etc. Real and Complex Numbers Finite Rings and Fields Algebraic Numbers Polynomials Formal Power Series Function Fields p-Adic Numbers Quaternion Algebras Linear Algebra Matrices and Spaces of Matrices Vectors and Modules Tensors on free modules of finite rank Other Algebraic Structures Monoids Groups Semirings Algebras Discrete Mathematics Combinatorics Graph Theory Quivers Matroid Theory Discrete Dynamics Coding Theory Game Theory Calculus Symbolic Calculus Mathematical Constants Elementary and Special Functions Asymptotic Expansions (experimental) Geometry and Topology Combinatorial and Discrete Geometry Hyperbolic Geometry Cell Complexes and their Homology Differential Forms Manifolds Parametrized Surfaces Knot Theory Number Theory, Algebraic Geometry Diophantine approximation Quadratic Forms L-Functions Schemes Elliptic, Plane, and Hyperelliptic Curves Arithmetic Subgroups of SL_2(Z) General Hecke Algebras and Hecke Modules Modular Symbols Modular Forms Modular Forms for Hecke Triangle Groups Modular Abelian Varieties Miscellaneous Modular-Form-Related Modules Logic Symbolic Logic SAT solvers Probability and Statistics Probability Statistics Quantitative Finance Miscellaneous Cryptography Numerical Optimization Databases Games

Magične naredbe

Osim magičnih naredbi %md, %html i %latex koje možemo koristiti u ćelijama Sage radnih bilježnica (mpr. ova ćelija je dobijena korištenjem dekoratora %md), postoji još cijeli niz dekoratora koji odgovaraju različitim modovima, koje možete vidjeti dolje.

print('\n'.join(modes()))
%auto %axiom %capture %coffeescript %command %cython %default %default_mode %exercise %file %fork %fortran %fricas %gap %gap3 %giac %go %gp %hide %hideall %html %java %javascript %julia %kash %lie %lisp %load %macaulay2 %magma %maple %mathematica %matlab %maxima %md %mupad %mwrank %octave %pandoc %perl %prun %python %r %reset %ruby %runfile %sage0 %scilab %script %sh %singular %time %timeit %typeset_mode %var %wiki
%julia (1 + 2im)*(2 - 3im)
8 + 1im

Za neke, kao npr. %mathematica, potrebno je da je na računalu instaliran odgovarajući program.

%r omogućava korištenje programskog jezika R

reset('r') %r x <- c(1,2,3,4,5,6) y <- x^2 print(y) mean(y) var(y)
[1] 1 4 9 16 25 36
15.1666666666667
178.966666666667

%fortran omogućuje kompajliranje Fortran koda te njegovo korištenje.

%fortran SUBROUTINE FIB(A,N) INTEGER N REAL*8 A(N) DO I=1,N IF (I.EQ.1) THEN A(I) = 0.0D0 ELSEIF (I.EQ.2) THEN A(I) = 1.0D0 ELSE A(I) = A(I-1) + A(I-2) ENDIF ENDDO END
import numpy n = numpy.array(range(10),dtype=float) fib(n,int(10)) n
array([ 0., 1., 1., 2., 3., 5., 8., 13., 21., 34.])
%cython cimport cython from libc.math cimport sqrt cdef double cy_funkcija(double x): return sqrt(1-x**2) def cy_integral4pi(int n): cdef double korak, rez cdef int i korak = 1.0/n rez = (cy_funkcija(0)+cy_funkcija(1))/2 for i in range(n): rez += cy_funkcija(i*korak) return 4*rez*korak
Defined cy_integral4pi
Auto-generated code...
%timeit cy_integral4pi(10**7)
5 loops, best of 3: 73.1 ms per loop
%cython def is2pow(unsigned int n): while n != 0 and n%2 == 0: n = n >> 1 return n == 1
Defined is2pow
Auto-generated code...
%time [n for n in range(10^5) if is2pow(n)]
[1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536] CPU time: 0.09 s, Wall time: 0.09 s
%octave x = [1; 3; 2] A = [1, 1, 2; 3, 5, 8; 13, 21, 34] A'*x
x = 1 3 2 A = 1 1 2 3 5 8 13 21 34 ans = 36 58 94
sage.version.version
'7.5'

Zadaci za vježbanje

  • Napišite interaktivni kod za ispis faktorizacije polinoma xn1x^n-1.

  • Izračunajte 3fxy2\frac{\partial^3 f}{\partial x\partial y^2} za f(x,y)=sin(xy)+cos(yz)f(x,y)=\sin(xy)+\cos(yz).

  • Izračunajte ex2dx\int_{-\infty}^{\infty} \mathrm{e}^{-x^2}d\, x.

  • Kreirajte slučajnu 3×33\times 3 matricu nad Z\mathbb{Z}. Nađite njen karakteristični polinom.

  • Provjerite je li polinom x4+x2+1x^4+x^2+1 ireducibilan nad Q\mathbb{Q}.

  • Izračunajte koliko ima particija 20-članog skupa koje imaju barem tri elementa od kojih svaki ima barem 3 elementa.

  • Kreirajte slučajni graf s 20 vrhova, gdje svaki brid ima vjerojatnost 0.45 da bude ubačen. Iz grafa izbacite dva vrha najmanja stupnja veća ili jednaka 1.