BasicSage2022

576 days ago by reuf1

# Basic Sage emphasizing 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 
       
# Edit and enter this cell to do your own computations. # Do this repeatedly. b=6*2-5 b 
       
# 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. 
       
# complex arithmetic a=1+i print a 
       
a*a 
       
conjugate(a) 
       
1/a 
       
# ordinary functions 
       
sin(1.5) 
       
e^1. 
       
################################ # GETTING INFORMATION FROM SAGE ################################ 
       
sin? 
       
# I will demonstrate the use of tab 
       
################################ # PLOTTING & DRAWING ################################ 
       
P = plot(sin, (0,10)) show(P) 
       
c1=circle((1,1),2) c2=circle((1,-1),2) L=line([(0,0), (1,1)]) p=list_plot([1,2,-1+I],size=30) show(c1+c2+L+p) 
       
################################ # variables, numbers, summation, and mod n ################################ 
       
k = var('k') sum(k,k,1,5) 
       
sum(k^2,k,1,5) 
       
# the infinity symbol is oo (oh-oh) 
       
sum(1/k^2,k,1,oo) 
       
mod(14,3) 
       
################################ # ELEMENTARY PROGRAMMING COMMANDS ################################ 
       
# Example of a for loop. # Note you must use indentation and :, or it won't work. for i in [1..3]: print i 
       
for i in [1..3]: print i 
       
a=2 d=3 for i in [0..2]: print mod(a+i*d,7) 
       
# Example using while and if s=5 t=0 while t < s: print t if 2*floor(t/2)==t: for j in [0..t]: print 'Ha' t=t+1 
       
# define your own function 
       
def f(x): return x+3 
       
f(5) 
       
################################ # MATRICES & VECTORS ################################ 
       
# 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 
       
# this is a pretty way to display a matrix (if it works) show(m1) 
       
m2=matrix([[7,-3],[0,5]]) show(m2) 
       
# Matrix arithmetic is pretty self evident m1+m2 
       
# * is matrix multiplication m1*m2 
       
m2^2 
       
# inverse m1^(-1) 
       
# 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] 
       
m1 
       
m1[0,1] 
       
m2.column(1) 
       
mm=copy(m2) mm.set_column(1,[0,0]) mm 
       
mm[1,0]=-1 mm 
       
identity_matrix(3) 
       
rank(m1) 
       
transpose(m1) 
       
det(m1) 
       
charpoly(m1) 
       
# Vectors # vectors are different from matrices, but can be multiplied by matrices # and will orient as needed when clear 
       
v = vector([2, 1, -2]) 
       
identity_matrix(3)*v 
       
v*identity_matrix(3) 
       
# vectors cannot be transposed without conversion to matrix transpose(v) 
       
# to transpose first convert to matrix w=matrix([v]) show(w) show(transpose(w)) 
       
# it is not recommended to muliply vectors as below # but it is treated as dot product if you do v*v 
       
show(v) norm(v) 
       
# Sage uses weird notation for a lot of its functions, using # "x.function()" # rather than function(x). Most 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 refm=mQ.echelon_form() refm 
       
# the "print" command is also to display the matrix 
       
print m1 print m1.eigenvalues() 
       
print m2 print m2.eigenvalues() 
       
# get eigenvectors as well m2.eigenmatrix_right() 
       
m2.eigenvectors_right() 
       
# You can use most of the standard matrix functions this weird way if you want m1.det() 
       
c=matrix(GF(2),[[0,1,1],[1,0,1],[1,1,0]]) c 
       
c.rank() 
       
c2=c.change_ring(QQ) c2 
       
c2.rank() 
       
# the next function computes the (Hermitian) adjoint 
       
def adj(m): return conjugate(m.transpose()) 
       
# warning: the sage command m.adjoint() computes the adjugate NOT the # (Hermitian) adjoint 
       
################################ # 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 
       
gg=Graph(m) show(gg) 
       
g=graphs.PathGraph(4) show(g) 
       
C5=graphs.CycleGraph(5) show(C5) 
       
C5.is_planar() 
       
Pet=graphs.PetersenGraph() show(Pet) 
       
Pet.is_planar() 
       
C5.order() 
       
g=C5.complement() show(g) 
       
Pet.is_connected() 
       
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)