{{{id=1| # Section - Graph Walks G=graphs.CubeGraph(3) print G print G.adjacency_matrix() #A random walk. def random_walk(G, v, n): """ Input a graph G and vertex v and length of walk n. """ for i in xrange(n): r=randint(0,len(G.neighbors(v))-1) N=G.neighbors(v) #print v, '\t', N v=N[r] return v v=G.vertices()[0] print random_walk(G,v, 100) def uniform_prob_matrix(G): M=matrix(QQ, len(G.vertices())) for i in xrange(len(G.vertices())): M[i,i] = 1/G.degree()[i] return M * G.adjacency_matrix() A=uniform_prob_matrix(G) print A, '\n' A^2 /// 3-Cube [0 1 1 0 1 0 0 0] [1 0 0 1 0 1 0 0] [1 0 0 1 0 0 1 0] [0 1 1 0 0 0 0 1] [1 0 0 0 0 1 1 0] [0 1 0 0 1 0 0 1] [0 0 1 0 1 0 0 1] [0 0 0 1 0 1 1 0] 101 [ 0 1/3 1/3 0 1/3 0 0 0] [1/3 0 0 1/3 0 1/3 0 0] [1/3 0 0 1/3 0 0 1/3 0] [ 0 1/3 1/3 0 0 0 0 1/3] [1/3 0 0 0 0 1/3 1/3 0] [ 0 1/3 0 0 1/3 0 0 1/3] [ 0 0 1/3 0 1/3 0 0 1/3] [ 0 0 0 1/3 0 1/3 1/3 0] [1/3 0 0 2/9 0 2/9 2/9 0] [ 0 1/3 2/9 0 2/9 0 0 2/9] [ 0 2/9 1/3 0 2/9 0 0 2/9] [2/9 0 0 1/3 0 2/9 2/9 0] [ 0 2/9 2/9 0 1/3 0 0 2/9] [2/9 0 0 2/9 0 1/3 2/9 0] [2/9 0 0 2/9 0 2/9 1/3 0] [ 0 2/9 2/9 0 2/9 0 0 1/3] }}} {{{id=8| /// }}} {{{id=7| L=[] for i in xrange(10): l=(A^i)[1] L.append(list(l)) print Matrix(RR, L) /// [0.000000000000000 1.00000000000000 0.000000000000000 0.000000000000000 0.000000000000000 0.000000000000000 0.000000000000000 0.000000000000000] [0.333333333333333 0.000000000000000 0.000000000000000 0.333333333333333 0.000000000000000 0.333333333333333 0.000000000000000 0.000000000000000] [0.000000000000000 0.333333333333333 0.222222222222222 0.000000000000000 0.222222222222222 0.000000000000000 0.000000000000000 0.222222222222222] [0.259259259259259 0.000000000000000 0.000000000000000 0.259259259259259 0.000000000000000 0.259259259259259 0.222222222222222 0.000000000000000] [0.000000000000000 0.259259259259259 0.246913580246914 0.000000000000000 0.246913580246914 0.000000000000000 0.000000000000000 0.246913580246914] [0.251028806584362 0.000000000000000 0.000000000000000 0.251028806584362 0.000000000000000 0.251028806584362 0.246913580246914 0.000000000000000] [0.000000000000000 0.251028806584362 0.249657064471879 0.000000000000000 0.249657064471879 0.000000000000000 0.000000000000000 0.249657064471879] [0.250114311842707 0.000000000000000 0.000000000000000 0.250114311842707 0.000000000000000 0.250114311842707 0.249657064471879 0.000000000000000] [0.000000000000000 0.250114311842707 0.249961896052431 0.000000000000000 0.249961896052431 0.000000000000000 0.000000000000000 0.249961896052431] [0.250012701315856 0.000000000000000 0.000000000000000 0.250012701315856 0.000000000000000 0.250012701315856 0.249961896052431 0.000000000000000] }}} {{{id=9| /// Traceback (most recent call last): File "", line 1, in File "_sage_input_131.py", line 10, in exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("Qj1NYXRyaXgoUlIsTCkKQg=="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))' + '\n', '', 'single') File "", line 1, in File "/tmp/tmpyEWOX7/___code___.py", line 3, in exec compile(u'B' + '\n', '', 'single') File "", line 1, in File "matrix0.pyx", line 1729, in sage.matrix.matrix0.Matrix.__repr__ (sage/matrix/matrix0.c:9487) File "/home/kaibutsu/sage-5.10.rc2/local/lib/python2.7/site-packages/sage/misc/sageinspect.py", line 1915, in sage_getvariablename for frame in inspect.stack(): File "/home/kaibutsu/sage-5.10.rc2/local/lib/python/inspect.py", line 1054, in stack return getouterframes(sys._getframe(1), context) File "/home/kaibutsu/sage-5.10.rc2/local/lib/python/inspect.py", line 1032, in getouterframes framelist.append((frame,) + getframeinfo(frame, context)) File "/home/kaibutsu/sage-5.10.rc2/local/lib/python/inspect.py", line 1007, in getframeinfo lines, lnum = findsource(frame) File "/home/kaibutsu/sage-5.10.rc2/local/lib/python/inspect.py", line 528, in findsource if not sourcefile and file[0] + file[-1] != '<>': IndexError: string index out of range }}} {{{id=2| # Partitions, diagrams. P=Partitions(5) p=P.an_element() print p p.pp() /// [4, 1] **** * }}} {{{id=3| # q_analogues # q_binomial is built in, print q_binomial(4,3) x=q_binomial(4,3) R=x.parent() print R def q_int(n): R=QQ['q'] q=R.gens()[0] return sum([q^i for i in xrange(n)]) print q_int(5) def q_factorial(n): return prod([q_int(i) for i in xrange(1,n+1)]) print q_factorial(5) print q_binomial(4,3)*q_int(2) /// q^3 + q^2 + q + 1 Univariate Polynomial Ring in q over Integer Ring q^4 + q^3 + q^2 + q + 1 q^10 + 4*q^9 + 9*q^8 + 15*q^7 + 20*q^6 + 22*q^5 + 20*q^4 + 15*q^3 + 9*q^2 + 4*q + 1 q^4 + 2*q^3 + 2*q^2 + 2*q + 1 }}} {{{id=4| #Generating functions, kind of. x=var('x') f = 1/(1-x-x^2) #Slow the first time, but once symmetrica's started in the background this is fast. g=f.taylor(x, 0, 40) print g, '\n' ff=sum([fibonacci(i)*x^(i-1) for i in xrange(0,42)]) print ff, '\n' print bool(ff-g==0) /// 165580141*x^40 + 102334155*x^39 + 63245986*x^38 + 39088169*x^37 + 24157817*x^36 + 14930352*x^35 + 9227465*x^34 + 5702887*x^33 + 3524578*x^32 + 2178309*x^31 + 1346269*x^30 + 832040*x^29 + 514229*x^28 + 317811*x^27 + 196418*x^26 + 121393*x^25 + 75025*x^24 + 46368*x^23 + 28657*x^22 + 17711*x^21 + 10946*x^20 + 6765*x^19 + 4181*x^18 + 2584*x^17 + 1597*x^16 + 987*x^15 + 610*x^14 + 377*x^13 + 233*x^12 + 144*x^11 + 89*x^10 + 55*x^9 + 34*x^8 + 21*x^7 + 13*x^6 + 8*x^5 + 5*x^4 + 3*x^3 + 2*x^2 + x + 1 165580141*x^40 + 102334155*x^39 + 63245986*x^38 + 39088169*x^37 + 24157817*x^36 + 14930352*x^35 + 9227465*x^34 + 5702887*x^33 + 3524578*x^32 + 2178309*x^31 + 1346269*x^30 + 832040*x^29 + 514229*x^28 + 317811*x^27 + 196418*x^26 + 121393*x^25 + 75025*x^24 + 46368*x^23 + 28657*x^22 + 17711*x^21 + 10946*x^20 + 6765*x^19 + 4181*x^18 + 2584*x^17 + 1597*x^16 + 987*x^15 + 610*x^14 + 377*x^13 + 233*x^12 + 144*x^11 + 89*x^10 + 55*x^9 + 34*x^8 + 21*x^7 + 13*x^6 + 8*x^5 + 5*x^4 + 3*x^3 + 2*x^2 + x + 1 True }}} {{{id=5| #Finite Fields G=GF(2) V=VectorSpace(G, 8) print V /// Vector space of dimension 8 over Finite Field of size 2 }}} {{{id=6| #Posets! P=Posets(6) print P print P.cardinality() Q=P.list() Q.reverse() for q in Q: if q.is_graded(): p=q break print p p.show() print sum([x^p.rank_function()(v) for v in p]) /// Posets containing 6 vertices 318 Finite poset containing 6 elements x^4 + 2*x^3 + x^2 + x + 1 }}} {{{id=10| #Antichains p.antichains().list() l=[0] for c in p.antichains().list(): print c, len(c) if len(c)>len(l): l=c print 'Longest antichain:', l /// [] 0 [0] 1 [1] 1 [2] 1 [3] 1 [3, 4] 2 [4] 1 [5] 1 Longest antichain: [3, 4] }}} {{{id=11| #Rank function on poset p #Find the rank of a single element: print p.rank(2) #The rank function is a method that returns a FUNCTION. print p.rank_function() print p.rank_function()(3) /// 2 at 0x9df6aa0> 3 }}} {{{id=12| def maximal_rank_size(p): ranks={} for v in p: r=p.rank(v) if r not in ranks: ranks[r]=0 ranks[r]+=1 return max(ranks.values()) maximal_rank_size(p) /// 2 }}} {{{id=13| p.upper_covers( /// False }}} {{{id=14| C=CombinatorialFreeModule(QQ, p) print C /// Free module generated by Finite poset containing 6 elements over Rational Field }}} {{{id=15| /// }}}