CoCalc Public FilesCuenta Funciones.sagews
Author: Sebastian Cabas
Views : 40
Compute Environment: Ubuntu 18.04 (Deprecated)
def cartesiano (A,n):
fuf=[]
for ii in range (n):
fuf.append(A)
%python

return cartesian_product(fuf)

A={1,2,3}
cartesiano(A,4)

The Cartesian product of ({1, 2, 3}, {1, 2, 3}, {1, 2, 3}, {1, 2, 3})
if i == 1:
print 'i equals 1'
else:
print 'i is not 1'


tutu.cardinality()

81

K=GF(2)
sss=cartesiano(K,6)

#Nos preguntamos por las funciones con 3 elementos en el recorrido que suman k

def suma (uu):    #Suma los elementos de uu
conta=0
for mm in uu:
conta+=mm
return conta

AA=range(3)
for ii in range (1,8):
sss=cartesiano(AA,ii)
rr=vector(ZZ,ii*3)
for uu in sss:
toy=suma(uu)
rr[toy]+=1
print rr

(1, 1, 1) (1, 2, 3, 2, 1, 0) (1, 3, 6, 7, 6, 3, 1, 0, 0) (1, 4, 10, 16, 19, 16, 10, 4, 1, 0, 0, 0) (1, 5, 15, 30, 45, 51, 45, 30, 15, 5, 1, 0, 0, 0, 0) (1, 6, 21, 50, 90, 126, 141, 126, 90, 50, 21, 6, 1, 0, 0, 0, 0, 0) (1, 7, 28, 77, 161, 266, 357, 393, 357, 266, 161, 77, 28, 7, 1, 0, 0, 0, 0, 0, 0)
def imagenes (uu):
ron=[]
for xx in uu:
ron.append(xx)
return Set(ron)

imagenes ((1,2,1,3,1,2,1))

{1, 2, 3}

def iny (uu):
if imagenes(uu).cardinality()==len(uu):
return True
else:
return False

def inyectivas (n,m):
AA=range(m)
Qu=cartesiano(AA,n)
gorda=[uu for uu in Qu if iny(uu)]
return gorda

inyectivas (3,4)

[(0, 1, 2), (0, 1, 3), (0, 2, 1), (0, 2, 3), (0, 3, 1), (0, 3, 2), (1, 0, 2), (1, 0, 3), (1, 2, 0), (1, 2, 3), (1, 3, 0), (1, 3, 2), (2, 0, 1), (2, 0, 3), (2, 1, 0), (2, 1, 3), (2, 3, 0), (2, 3, 1), (3, 0, 1), (3, 0, 2), (3, 1, 0), (3, 1, 2), (3, 2, 0), (3, 2, 1)]

def cuentinyec(n,m):
return len(inyectivas(n,m))



cuentinyec(3,10)

720

#Vamos a contar las palabras de 4 digitos sin repeticiones contienen el 8
melba=inyectivas(4,10)
nico=[uu for uu in melba if 8 in imagenes(uu)]

len(nico)

2016

AA=range(10)
mel=cartesiano(AA,4)
nic=[uu for uu in mel if 8 in imagenes(uu)]

len(nic)

3439

naco=[uu for uu in mel if imagenes(uu).cardinality()==3]
len(naco)

4320

iny ((1,2,3,4,1))

False

def sobre (uu, mm):
if imagenes(uu).cardinality()==mm:
return True
else:
return False


def sobrevas (n,m):
AA=range(m)
Qu=cartesiano(AA,n)
gorda=[uu for uu in Qu if sobre(uu, m)]
return gorda


sobrevas (4,2)

[(0, 0, 0, 1), (0, 0, 1, 0), (0, 0, 1, 1), (0, 1, 0, 0), (0, 1, 0, 1), (0, 1, 1, 0), (0, 1, 1, 1), (1, 0, 0, 0), (1, 0, 0, 1), (1, 0, 1, 0), (1, 0, 1, 1), (1, 1, 0, 0), (1, 1, 0, 1), (1, 1, 1, 0)]

len(sobrevas(4,3))

36

A=range(3)
mol=cartesiano(A,4)
nace=[uu for uu in mol if 1 in imagenes(uu)]

nace

[(0, 0, 0, 1), (0, 0, 1, 0), (0, 0, 1, 1), (0, 0, 1, 2), (0, 0, 2, 1), (0, 1, 0, 0), (0, 1, 0, 1), (0, 1, 0, 2), (0, 1, 1, 0), (0, 1, 1, 1), (0, 1, 1, 2), (0, 1, 2, 0), (0, 1, 2, 1), (0, 1, 2, 2), (0, 2, 0, 1), (0, 2, 1, 0), (0, 2, 1, 1), (0, 2, 1, 2), (0, 2, 2, 1), (1, 0, 0, 0), (1, 0, 0, 1), (1, 0, 0, 2), (1, 0, 1, 0), (1, 0, 1, 1), (1, 0, 1, 2), (1, 0, 2, 0), (1, 0, 2, 1), (1, 0, 2, 2), (1, 1, 0, 0), (1, 1, 0, 1), (1, 1, 0, 2), (1, 1, 1, 0), (1, 1, 1, 1), (1, 1, 1, 2), (1, 1, 2, 0), (1, 1, 2, 1), (1, 1, 2, 2), (1, 2, 0, 0), (1, 2, 0, 1), (1, 2, 0, 2), (1, 2, 1, 0), (1, 2, 1, 1), (1, 2, 1, 2), (1, 2, 2, 0), (1, 2, 2, 1), (1, 2, 2, 2), (2, 0, 0, 1), (2, 0, 1, 0), (2, 0, 1, 1), (2, 0, 1, 2), (2, 0, 2, 1), (2, 1, 0, 0), (2, 1, 0, 1), (2, 1, 0, 2), (2, 1, 1, 0), (2, 1, 1, 1), (2, 1, 1, 2), (2, 1, 2, 0), (2, 1, 2, 1), (2, 1, 2, 2), (2, 2, 0, 1), (2, 2, 1, 0), (2, 2, 1, 1), (2, 2, 1, 2), (2, 2, 2, 1)]


def cuentasobres (nn,mm):
return len(sobrevas(nn,mm))

True
cuentasobres(8,4)

40824

def condicion (jj,k):
toto=len(jj)
conta=0
for ii in range (0,toto):
if jj[ii]!=jj[(ii+k)%toto]:
conta+=1
if conta==0:
return True
else:
return False

def sebas(n,m,k):
A=range (m) #Se crea el codominio de la funcion
Q=cartesiano (A,n)
func =[jj for jj in Q if condicion (jj,k)]
return func

def cumple(jj,k):
cont=0
for ii in range (0,k):
if jj[ii]>=jj[ii+1]:
cont+=1
if cont==0:
return True
else:
return False

def yuyi(n,m,k):
B=range(m)
Q=cartesiano(B,n)
func=[ii for ii in Q if cumple (ii,k)]
return func

def cuentasebas (n,m,k):
return len(sebas(n,m,k))

yuyi (6,6,4)


[(0, 1, 2, 3, 4, 0), (0, 1, 2, 3, 4, 1), (0, 1, 2, 3, 4, 2), (0, 1, 2, 3, 4, 3), (0, 1, 2, 3, 4, 4), (0, 1, 2, 3, 4, 5), (0, 1, 2, 3, 5, 0), (0, 1, 2, 3, 5, 1), (0, 1, 2, 3, 5, 2), (0, 1, 2, 3, 5, 3), (0, 1, 2, 3, 5, 4), (0, 1, 2, 3, 5, 5), (0, 1, 2, 4, 5, 0), (0, 1, 2, 4, 5, 1), (0, 1, 2, 4, 5, 2), (0, 1, 2, 4, 5, 3), (0, 1, 2, 4, 5, 4), (0, 1, 2, 4, 5, 5), (0, 1, 3, 4, 5, 0), (0, 1, 3, 4, 5, 1), (0, 1, 3, 4, 5, 2), (0, 1, 3, 4, 5, 3), (0, 1, 3, 4, 5, 4), (0, 1, 3, 4, 5, 5), (0, 2, 3, 4, 5, 0), (0, 2, 3, 4, 5, 1), (0, 2, 3, 4, 5, 2), (0, 2, 3, 4, 5, 3), (0, 2, 3, 4, 5, 4), (0, 2, 3, 4, 5, 5), (1, 2, 3, 4, 5, 0), (1, 2, 3, 4, 5, 1), (1, 2, 3, 4, 5, 2), (1, 2, 3, 4, 5, 3), (1, 2, 3, 4, 5, 4), (1, 2, 3, 4, 5, 5)]

def stir2 (n):
RR=[[],[0,1,0]]
for kk in range(2,n+1):
SS=RR[kk-1]
TT=[0]
ll=len(SS)
for ii in range(1,ll):
qq=SS[ii-1]+ii*SS[ii]
TT.append(qq)
TT.append(0)
RR.append(TT)
return RR

stir2(3)

[[], [0, 1, 0], [0, 1, 1, 0], [0, 1, 3, 1, 0]]

utu=Permutations(5)

len(utu)

120
for uu in Permutations(4):
print uu

[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]

def desa(unn):
oto=len(unn)
for mi in range (oto):
if unn[mi]==mi+1:
return False
break
return True


desa([2,3,4,1])

True


def orbita (uu,jj):
tt=[]
while not jj in tt:
tt.append(jj)
yotas=uu[jj-1]
jj=yotas
return tt


orbita([1,2,5,4,3],3)

[3, 5]

len([1,2]