Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168698
Image: ubuntu2004
# Very basic Sage with linear algebra and graph theory commands # by Leslie Hogben
# Anything that starts with a number sign is a comment # this is an example of a comment
# If you can't see everything in a cell, drag the lower right corner # to make it bigger.
# Sage is cell based and chronological. # This means Sage knows what you have entered previously and # uses the last entered value. # You do not have to go from the top down. # To enter (evaluate) the cell, place the curser in the cell # (and click to get a vertical cursor "|"). # Then press the SHIFT and RETURN keys at the same time.
# Enter this cell to compute 2+3*4 and assign it to the variable a a=2+3*4
# Enter this cell to see what you just computed. a
14
# Edit and enter this cell to do your own computations. # Do this repeatedly. b=6*2-5 b
7
# Make a new cell by clicking when you see a horizontal blue line # (mover cursor between cells). # Enter your own computation in your new cell.
################################ # MATRICES ################################
# Now we will enter matrices and do arithmetic with them. # You have to tell Sage that what you are entering is a matrix. m1=matrix([[1,2],[3,4]]) m1
[1 2] [3 4]
show(m1)
\left(1234\begin{array}{rr} 1 & 2 \\ 3 & 4 \end{array}\right)
m2=matrix([[7,-3],[0,5]]) show(m2)
\left(7305\begin{array}{rr} 7 & -3 \\ 0 & 5 \end{array}\right)
# Matrix arithmetic is pretty self evident m1+m2
[ 8 -1] [ 3 9]
m1*m2
[ 7 7] [21 11]
m1^3
[ 37 54] [ 81 118]
# inverse m1^(-1)
[ -2 1] [ 3/2 -1/2]
# matrix entry (Sage numbers starting with zero so the rest of the # world would call this the 1,1-entry but Sage calls it the # 0,0-entry) m1[0,0]
1
m1
[1 2] [3 4]
m1[0,1]
2
identity_matrix(3)
[1 0 0] [0 1 0] [0 0 1]
rank(m1)
2
transpose(m1)
[1 3] [2 4]
det(m1)
-2
charpoly(m1)
x^2 - 5*x - 2
# Sage uses weird notation for a lot of its functions, using # "x.function()" # rather than function(x). All are available this way, and # this is the only way some are available
# Unreduced (staircase) Row Echelon Form (REF) mQ = matrix([[1,2,3],[4,5,6],[7,8,9]]) print mQ print rrefm=mQ.echelon_form() rrefm
[1 2 3] [4 5 6] [7 8 9] [1 2 3] [0 3 6] [0 0 0]
# the "print" command is also to display the matrix
print m1 print m1.eigenvalues()
[1 2] [3 4] [-0.3722813232690144?, 5.372281323269015?]
print m2 print m2.eigenvalues()
[ 7 -3] [ 0 5] [7, 5]
# get eigenvectors as well m2.eigenmatrix_right()
([7 0] [0 5], [ 1 1] [ 0 2/3])
m2.eigenvectors_right()
[(7, [ (1, 0) ], 1), (5, [ (1, 2/3) ], 1)]
# I will demonstrate use of tab and ? key
# You can use all the standard matrix functions this weird way if you want m1.det()
-2
# Example of a for loop. Note you must use indentation or # it won't work. for i in [1..3]: print m1-i*identity_matrix(2) print
[0 2] [3 3] [-1 2] [ 3 2] [-2 2] [ 3 1]
# matrix over other ring b=matrix(GF(2),[[7,-3],[0,5]]) b
[1 1] [0 1]
c=matrix(GF(2),[[0,1,1],[1,0,1],[1,1,0]]) c
[0 1 1] [1 0 1] [1 1 0]
c.rank()
2
c2=c.change_ring(QQ) c2
[0 1 1] [1 0 1] [1 1 0]
c2.rank()
3
################################ # GRAPHS ################################
g=Graph({1:[3,4,5,6],2:[8,10,12],6:[7,8],8:[9],10:[11]}) show(g)
m = g.adjacency_matrix() m
[0 0 1 1 1 1 0 0 0 0 0 0] [0 0 0 0 0 0 0 1 0 1 0 1] [1 0 0 0 0 0 0 0 0 0 0 0] [1 0 0 0 0 0 0 0 0 0 0 0] [1 0 0 0 0 0 0 0 0 0 0 0] [1 0 0 0 0 0 1 1 0 0 0 0] [0 0 0 0 0 1 0 0 0 0 0 0] [0 1 0 0 0 1 0 0 1 0 0 0] [0 0 0 0 0 0 0 1 0 0 0 0] [0 1 0 0 0 0 0 0 0 0 1 0] [0 0 0 0 0 0 0 0 0 1 0 0] [0 1 0 0 0 0 0 0 0 0 0 0]
gg=Graph(m) show(gg)
g=graphs.PathGraph(4) show(g)
C5=graphs.CycleGraph(5) show(C5)
Pet=graphs.PetersenGraph() show(Pet)
C5.is_planar()
True
Pet.is_planar()
False
C5.order()
5
g=C5.complement() show(g)
Pet.is_connected()
True
h=Pet.copy() h.delete_vertex(5) show(h)
h=Pet.copy() h.delete_edge([0,5]) show(h)
g=graphs.CirculantGraph(7,[1,2]) show(g)