| Hosted by CoCalc | Download
n=8 G=SymmetricGroup(n) t=G.random_element() t
(1,2,4,5,3,6)(7,8)
tc=t.cycle_tuples()
def lengthOfLongestCycle(t): #t is a perm tc = t.cycle_tuples() tcLens = [len(c) for c in tc] return max(tcLens)
mean([lengthOfLongestCycle(G.random_element()) for _ in range(100)]).n()
13.0500000000000
tc[0]
(1, 2, 4, 5, 3, 6)
len(tc[0])
6 6
4!
Error in lines 1-1 Traceback (most recent call last): File "/projects/sage/sage-6.10/local/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 957, in execute exec compile(block+'\n', '', 'single') in namespace, locals File "<string>", line 1 Integer(4)! ^ SyntaxError: invalid syntax
len(tc)
2
binomial(16,3)*binomial(13,3)*binomial(10,3)*(2^3)*binomial(7,5)*factorial(4)/factorial(3)
12915302400
binomial(16,9)*8*7*5*4*2*binomial(7,5)*factorial(4)
12915302400
binomial(13,3)*6^3*40
2471040

This is exe^x

nick = SymmetricGroup(11)
nick.cardinality()
39916800
t=nick.random_element()
t
(1,9,11,3,4,10,5,6,2,8,7)
tc = t.cycle_tuples() tc
[(1, 9, 11, 3, 4, 10, 5, 6, 2, 8, 7)]
tc[0]
(1, 9, 11, 3, 4, 10, 5, 6, 2, 8, 7)
len(tc[0])
11
len(tc)
1
trials = 1000 output =[] for _ in range(trials): t = nick.random_element() tc = t.cycle_tuples() cycleLengths = [len(c) for c in tc] mx = max(cycleLengths) output.append(mx)
output
[9, 7, 5, 6, 6, 5, 5, 4, 5, 6, 10, 5, 6, 6, 7, 6, 11, 10, 3, 9, 8, 4, 7, 9, 6, 8, 7, 6, 5, 5, 9, 7, 4, 7, 7, 5, 10, 8, 7, 8, 8, 4, 9, 5, 10, 7, 4, 9, 5, 6, 7, 4, 9, 9, 9, 5, 4, 7, 7, 9, 8, 11, 6, 6, 7, 8, 8, 8, 9, 6, 9, 8, 9, 11, 10, 8, 5, 7, 7, 7, 7, 7, 6, 6, 10, 8, 9, 4, 9, 9, 6, 3, 9, 11, 6, 4, 8, 8, 9, 6]
n(mean(output))
7.21500000000000
Partitions(7)
Partitions of the integer 7
Partitions(7).list()
[[7], [6, 1], [5, 2], [5, 1, 1], [4, 3], [4, 2, 1], [4, 1, 1, 1], [3, 3, 1], [3, 2, 2], [3, 2, 1, 1], [3, 1, 1, 1, 1], [2, 2, 2, 1], [2, 2, 1, 1, 1], [2, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1]]
len(Partitions(7).list())
15
number_of_partitions(7)
15
Partitions(7,length=3).list()
[[5, 1, 1], [4, 2, 1], [3, 3, 1], [3, 2, 2]]
P = [2,3,5] Partitions(7, parts_in = P).list()
[[5, 2], [3, 2, 2]]
stirling_number2(6,2)
31
binomial(15,9)
5005
number_of_partitions(55)
451276
2^14.4
21618.8176101031
2^1.4
2.63901582154579
n=50 relPrimeArray=[[gcd(x,y)==1 for x in range(n)] for y in range(n)] matrix_plot(relPrimeArray,origin='lower')
n = 500 relprimesum=0 for x in [1..n]: for y in [1..n]: relprimesum += (gcd(x,y)==1) print n, float(relprimesum/n^2)
500 0.608924
n=100000 trials = 100000 sum=0 for _ in range(trials): x=randint(1,n) y=randint(1,n) sum += (gcd(x,y)==1) print n, float(sum/trials)
100000 0.60902
p=(1+x^7+x^13)^100
len(p.coefficients())
1235
5*8
40
moebius(9)
0
sum=0 n=1000 for k in [1..n]: sum += float(moebius(k)/k^2) sum
0.6079319118542257
float(6/pi^2)
0.6079271018540267
stirling_number2(7,4)
350
4^7-4*3^7+6*2^7-4
8400
350*24
8400
7728/24
322
4^7
16384
3^7
2187
3^5-3*2^5+3
150
n=90 cycleMax=10 #look at up to length cycleMax trials =5000 cycleLengthCounts=[0 for _ in range(cycleMax+1)] #ignore the 0th list member G=SymmetricGroup(n) for _ in range(trials): t=G.random_element() #list the lengths cycleLengths=[len(c) for c in t.cycle_tuples()] #now count them and keep track for k in [1..cycleMax]: cycleLengthCounts[k] += cycleLengths.count(k) [a*float(1/trials) for a in cycleLengthCounts]
[0.0, 0.0, 0.5144000000000001, 0.327, 0.25880000000000003, 0.2056, 0.1602, 0.1468, 0.125, 0.11120000000000001, 0.097]
binomial(18,8)
43758
floor(9^10/7)
498112057
longseq=[] sequence =[3,-2,-5, 2, 1,0, 2] longseq.extend(sequence) longseq.extend(sequence) longseq.extend(sequence) longseq.extend(sequence) pSum =[sum(longseq[0:k]) for k in range(len(longseq))] list_plot(pSum, plotjoined=True, gridlines= [range(25),range(-7,7)],aspect_ratio=1,marker='.')
7*9
63
777^999
33931087734103828901344268647256476554494850685762924621597342996464801589784605059475133384865164529036252066031994855345871665234356842806888449267093509234359078884875609318110606666510890869142825561053120919076669473956860107412958442850376297067619489373931325071407523822884590872646827517799418402810326100066742600757821843472688026520254916700218626719112742578105903860540314961495111422395630099850083222143586130977336793295286700263670768264758729167200080145039533621666985306881245948901798609310054451638995373023989628953955349320399434346505274696979982991692890568489697253604073901819244160638333613324109911532785670872242460987501092320395090524227584198011767301043501547054296185078440499819172566754617574465833324107891106395177027939832474344870734963502234713429840122131504136395080491920841542292117828585309123698386841431487925884672048097653436060166475057523741982012435447399021924160728572742932223618898581884526373480112488174279669517774268361093607090965758281557376997913620979310983462794465757333445530534099570736670074890569660474796577577674932657399492932328300141091012315884393051563872513896814510314701255767131661167855578818687047645394220003316556048000069638255157070961602769749078334816125790877097187873567471166880211709457699577692105308896649632851452814290230417778177094724367273446957406072684664685873234298355936558262411829413708595972450043859309938603417902840645173173789378852070784862031684579435206835757153121368065547594934047652144429563176654017561008554016628781999832120575525343668137868130684497675977124015549991825613898902813521554470684667215386490392454534482209869698083482450465168175971341558330428500922282511700067447539581778630674297796312467346371250185503892422658227103409485995968206774501698230856662772642424582441087021916747112358759991535113065761776986423267601319525420294876674280089342519455574486023936442553386103432916629919668048219224864951111272699130893906353027159740738835823517199856842410474351493420074365266969587584629623246170925954814760916633251108183367047829039642772329603065432062881303171326473274784088864711175502725522891692976325286289960874148287795332021019170175743912385323139721110937345954228970698098814088780032960736716127543386784085410250453102855226979009140947483110598671549236302405980242835053724646921600516023954682356191143023647945836544213782337196393383035101150902925967111001304312561534088860639031598058785817836265690855929443081305806085774999206906102199749512038292462521597332925432940093993451970313676982765732305448371997449528447367439213093823699213224857906816915506680824065402571625875013869166514632665730219518789150281073973527353790917415227277239925729920311755321764104602590582257364606165141999378702412830424318748754721986601241465067971885822122610210203460316734017436524225768072047627756306030304078713
p=Partitions(11)
p.list()
[[11], [10, 1], [9, 2], [9, 1, 1], [8, 3], [8, 2, 1], [8, 1, 1, 1], [7, 4], [7, 3, 1], [7, 2, 2], [7, 2, 1, 1], [7, 1, 1, 1, 1], [6, 5], [6, 4, 1], [6, 3, 2], [6, 3, 1, 1], [6, 2, 2, 1], [6, 2, 1, 1, 1], [6, 1, 1, 1, 1, 1], [5, 5, 1], [5, 4, 2], [5, 4, 1, 1], [5, 3, 3], [5, 3, 2, 1], [5, 3, 1, 1, 1], [5, 2, 2, 2], [5, 2, 2, 1, 1], [5, 2, 1, 1, 1, 1], [5, 1, 1, 1, 1, 1, 1], [4, 4, 3], [4, 4, 2, 1], [4, 4, 1, 1, 1], [4, 3, 3, 1], [4, 3, 2, 2], [4, 3, 2, 1, 1], [4, 3, 1, 1, 1, 1], [4, 2, 2, 2, 1], [4, 2, 2, 1, 1, 1], [4, 2, 1, 1, 1, 1, 1], [4, 1, 1, 1, 1, 1, 1, 1], [3, 3, 3, 2], [3, 3, 3, 1, 1], [3, 3, 2, 2, 1], [3, 3, 2, 1, 1, 1], [3, 3, 1, 1, 1, 1, 1], [3, 2, 2, 2, 2], [3, 2, 2, 2, 1, 1], [3, 2, 2, 1, 1, 1, 1], [3, 2, 1, 1, 1, 1, 1, 1], [3, 1, 1, 1, 1, 1, 1, 1, 1], [2, 2, 2, 2, 2, 1], [2, 2, 2, 2, 1, 1, 1], [2, 2, 2, 1, 1, 1, 1, 1], [2, 2, 1, 1, 1, 1, 1, 1, 1], [2, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
p3=p.list()[8]
p3
[7, 3, 1]
3
3
p=(x+x^2/2+x^3/6+x^4/24+x^5/120)^3
p.simplify()
1/1728000*(x^5 + 5*x^4 + 20*x^3 + 60*x^2 + 120*x)^3
p.expand()
1/1728000*x^15 + 1/115200*x^14 + 1/12800*x^13 + 181/345600*x^12 + 9/3200*x^11 + 71/5760*x^10 + 391/8640*x^9 + 67/480*x^8 + 43/120*x^7 + 3/4*x^6 + 5/4*x^5 + 3/2*x^4 + x^3
p.coefficients()
[[1, 3], [3/2, 4], [5/4, 5], [3/4, 6], [43/120, 7], [67/480, 8], [391/8640, 9], [71/5760, 10], [9/3200, 11], [181/345600, 12], [1/12800, 13], [1/115200, 14], [1/1728000, 15]]
stirling_number2(10,3)
9330
p.coefficients()[10]
[1/12800, 13]
factorial(10)*71/5760
44730
p=(x^2+x^3+x^4+x^5+x^6+x^7)^4
p.expand()
x^28 + 4*x^27 + 10*x^26 + 20*x^25 + 35*x^24 + 56*x^23 + 80*x^22 + 104*x^21 + 125*x^20 + 140*x^19 + 146*x^18 + 140*x^17 + 125*x^16 + 104*x^15 + 80*x^14 + 56*x^13 + 35*x^12 + 20*x^11 + 10*x^10 + 4*x^9 + x^8
binomial(13,3)-4*binomial(7,3)
146
p.coefficients()
[[1, 8], [4, 9], [10, 10], [20, 11], [35, 12], [56, 13], [80, 14], [104, 15], [125, 16], [140, 17], [146, 18], [140, 19], [125, 20], [104, 21], [80, 22], [56, 23], [35, 24], [20, 25], [10, 26], [4, 27], [1, 28]]
q=(x^10-x^26)*(x-x^16)*(x^20-x^46)
q.expand()
-x^88 + x^73 + x^72 + x^62 - x^57 - x^47 - x^46 + x^31
myl=[50.factorial()/((c+10).factorial()*(40-2*c).factorial()*c.factorial()) for c in [0..20]]
sum(myl)
11152843844517873604890
q=(1+x+x^2)^50
q.coefficients()[40]
[11152843844517873604890, 40]
D=DihedralGroup(4)
D.cayley_table()
* a b c d e f g h +---------------- a| a b c d e f g h b| b a f h g c e d c| c e d g h b a f d| d h g a f e c b e| e c b f a d h g f| f g h e d a b c g| g f a c b h d e h| h d e b c g f a
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)]]
c=3 F=(c^6+3*c^4+12*c^3+8*c^2)/24 V=(c^8+17*c^4+6*c^2)/24 print F, V
57 333
%var a,b,c def cy(k): return a^k+b^k+c^k ind = (cy(1)^6+ 8*cy(3)^2+3*cy(1)^2*cy(2)^2)/12 ind
1/12*(a + b + c)^6 + 1/4*(a^2 + b^2 + c^2)^2*(a + b + c)^2 + 2/3*(a^3 + b^3 + c^3)^2
ind.expand()
a^6 + a^5*b + 2*a^4*b^2 + 4*a^3*b^3 + 2*a^2*b^4 + a*b^5 + b^6 + a^5*c + 3*a^4*b*c + 6*a^3*b^2*c + 6*a^2*b^3*c + 3*a*b^4*c + b^5*c + 2*a^4*c^2 + 6*a^3*b*c^2 + 9*a^2*b^2*c^2 + 6*a*b^3*c^2 + 2*b^4*c^2 + 4*a^3*c^3 + 6*a^2*b*c^3 + 6*a*b^2*c^3 + 4*b^3*c^3 + 2*a^2*c^4 + 3*a*b*c^4 + 2*b^2*c^4 + a*c^5 + b*c^5 + c^6
fibonacci_sequence?
File: /projects/sage/sage-7.3/local/lib/python2.7/site-packages/sage/combinat/combinat.py Signature : fibonacci_sequence(start, stop=None, algorithm=None) Docstring : Return an iterator over the Fibonacci sequence, for all fibonacci numbers f_n from "n = start" up to (but not including) "n = stop" INPUT: * "start" -- starting value * "stop" -- stopping value * "algorithm" -- (default: "None") passed on to fibonacci function (or not passed on if None, i.e., use the default) EXAMPLES: sage: fibs = [i for i in fibonacci_sequence(10, 20)] sage: fibs [55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181] sage: sum([i for i in fibonacci_sequence(100, 110)]) 69919376923075308730013 See also: "fibonacci_xrange()" AUTHORS: * Bobby Moretti
fibs = [n(log(i,10)) for i in fibonacci_sequence(1009, 1010)]
fibs
[210.519044010060]
10^0.519044010060
3.30403021361001
5^4%2017
625
5^63%2017
500
for k in [1..20]: k, 5^k%2017
(1, 5) (2, 25) (3, 125) (4, 625) (5, 1108) (6, 1506) (7, 1479) (8, 1344) (9, 669) (10, 1328) (11, 589) (12, 928) (13, 606) (14, 1013) (15, 1031) (16, 1121) (17, 1571) (18, 1804) (19, 952) (20, 726)
229^2
52441
52441%2017
2016
%cython import numpy as N cimport numpy as N # this Cython-side numpy import is needed # to declare the types and dimensions of arrays x0, x1 = -2.0, 1.0 y0, y1 = -1.0, 1.0 def mandel_cython(int n=400,int maxi=512): # declare the type and dimension of numpy arrays # (and create them in the same line, C-style) cdef N.ndarray[double,ndim=1] xs = N.linspace(x0,x1,n) cdef N.ndarray[double,ndim=1] ys = N.linspace(y0,y1,n) cdef N.ndarray[int,ndim=2] escape = N.empty((n,n),'int32') # declare integer counters cdef int i,j,it,esc # declare complex variables cdef double complex z,c for i in range(n): for j in range(n): z = 0 + 0j c = xs[i] + 1j * ys[j] esc = maxi for it in range(maxi): z = z*z + c # let's allow ourselves one hand-tuned optimization, # which avoids the sqrt implicit in abs if z.real*z.real + z.imag*z.imag > 4: esc = it break escape[j,i] = esc return escape
Defined N, mandel_cython, x0, x1, y0, y1
Auto-generated code...
import matplotlib.pyplot as P #P.figure() P.imshow(N.array(mandel_cython()),origin='lower',extent=(x0,x1,y0,y1)) P.show()
Error in lines 2-2 Traceback (most recent call last): File "/projects/sage/sage-7.3/local/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 976, in execute exec compile(block+'\n', '', 'single') in namespace, locals File "", line 1, in <module> File "/projects/sage/sage-7.3/local/lib/python2.7/site-packages/matplotlib-1.5.1-py2.7-linux-x86_64.egg/matplotlib/pyplot.py", line 3022, in imshow **kwargs) File "/projects/sage/sage-7.3/local/lib/python2.7/site-packages/matplotlib-1.5.1-py2.7-linux-x86_64.egg/matplotlib/__init__.py", line 1812, in inner return func(ax, *args, **kwargs) File "/projects/sage/sage-7.3/local/lib/python2.7/site-packages/matplotlib-1.5.1-py2.7-linux-x86_64.egg/matplotlib/axes/_axes.py", line 4945, in imshow resample=resample, **kwargs) File "/projects/sage/sage-7.3/local/lib/python2.7/site-packages/matplotlib-1.5.1-py2.7-linux-x86_64.egg/matplotlib/image.py", line 597, in __init__ **kwargs File "/projects/sage/sage-7.3/local/lib/python2.7/site-packages/matplotlib-1.5.1-py2.7-linux-x86_64.egg/matplotlib/image.py", line 130, in __init__ self.update(kwargs) File "/projects/sage/sage-7.3/local/lib/python2.7/site-packages/matplotlib-1.5.1-py2.7-linux-x86_64.egg/matplotlib/artist.py", line 856, in update raise AttributeError('Unknown property %s' % k) AttributeError: Unknown property aspectratio
"""Produce custom labelling for a colorbar. Contributed by Scott Sinclair """ import matplotlib.pyplot as plt import numpy as np from matplotlib import cm from numpy.random import randn # Make plot with vertical (default) colorbar fig, ax = plt.subplots() data = np.clip(randn(250, 250), -1, 1) cax = ax.imshow(data, interpolation='nearest', cmap=cm.coolwarm) ax.set_title('Gaussian noise with vertical colorbar') # Add colorbar, make sure to specify tick locations to match desired ticklabels cbar = fig.colorbar(cax, ticks=[-1, 0, 1]) cbar.ax.set_yticklabels(['< -1', '0', '> 1']) # vertically oriented colorbar # Make plot with horizontal colorbar fig, ax = plt.subplots() data = np.clip(randn(250, 250), -1, 1) cax = ax.imshow(data, interpolation='nearest', cmap=cm.afmhot) ax.set_title('Gaussian noise with horizontal colorbar') cbar = fig.colorbar(cax, ticks=[-1, 0, 1], orientation='horizontal') cbar.ax.set_xticklabels(['Low', 'Medium', 'High']) # horizontal colorbar plt.show()
'Produce custom labelling for a colorbar.\n\nContributed by Scott Sinclair\n' <matplotlib.text.Text object at 0x7f733f7bc450> [<matplotlib.text.Text object at 0x7f733f732990>, <matplotlib.text.Text object at 0x7f733f71e490>, <matplotlib.text.Text object at 0x7f733f683950>] <matplotlib.text.Text object at 0x7f733f7c2cd0> [<matplotlib.text.Text object at 0x7f733f967ed0>, <matplotlib.text.Text object at 0x7f733f91ce10>, <matplotlib.text.Text object at 0x7f733fa21f90>]
from mpl_toolkits.mplot3d import Axes3D from matplotlib import cm from matplotlib.ticker import LinearLocator import matplotlib.pyplot as plt import numpy as np fig = plt.figure() ax = fig.gca(projection='3d') X = np.arange(-5, 5, 0.25) xlen = len(X) Y = np.arange(-5, 5, 0.25) ylen = len(Y) X, Y = np.meshgrid(X, Y) R = np.sqrt(X**2 + Y**2) Z = np.sin(R) colortuple = ('y', 'b') colors = np.empty(X.shape, dtype=str) for y in range(ylen): for x in range(xlen): colors[x, y] = colortuple[(x + y) % len(colortuple)] surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors=colors, linewidth=0, antialiased=False) ax.set_zlim3d(-1, 1) ax.w_zaxis.set_major_locator(LinearLocator(6)) plt.show()
(-1, 1)
5*5
25
[g^2 for g in range(6)]
[0, 1, 4, 9, 16, 25]