I have the first solution to this problem (Farid).
def subset_to_monomial(SS,d):
S=list(SS)
vx=[var("x"+str(i)) for i in range(1,len(S)+2)]
a=[]
b=1
for j in range(len(S)+1):
if j==0:
a.append(vx[j]^(S[j]-1))
elif j>0 and j<=(len(S)-1):
a.append(vx[j]^(S[j]-S[j-1]-1))
else:
a.append(vx[j]^(j+d-S[j-1]))
for k in range(len(S)+1):
b=b*a[k]
return b
Second solution (Eric and Pavel).
def count_zeros(word):
"""
This function counts the occurrences of 0's in a word.
"""
out = 0
for i in range(word.length()):
if word[i] == 0:
out = out+1
return out
def list_all_monomials(n,k):
"""
Generates a list of all monomials in n variables with
degree k. The procedure uses binary strings of length
n+k-1 with n-1 zeros to construct the monomials.
EXAMPLES::
sage: list_all_monomials(3,2)
[x3^2, x2*x3, x2^2, x1*x3, x1*x2, x1^2]
"""
vx =[var("x"+str(i)) for i in range(n+1)]
s =[]
for w in Words(alphabet=[0,1],length=n+k-1):
if count_zeros(w)==n-1:
mono=1
j=1
for i in range(w.length()):
if w[i]== 1:
mono= mono*vx[j]
else:
j=j+1
s.append(mono)
return s
Third solution (Sam).
ax = [var('a'+str(i)) for i in range(2^10)]
def subset_to_monomial(SS,d):
return var('a1')^(SS[0]-1)*mul( ax[i+1]^( SS[i] - SS[i-1]-1) for i in range(1,len(SS)))*var( 'a'+str(len(SS)+1))^(d+(len(SS))-SS[len(SS)-1])
def list_all_monomials(n,d):
return [subset_to_monomial(SS,d) for SS in Subsets(n+d-1, n-1)]
Fourth solution (Sirous).
def subset_to_monomial(SS,d):
S=list(SS)
vx=[var("x"+str(i)) for i in range(1,len(S)+2)]
a=1
for j in range(1,len(S)):
a=a*(vx[j]^(S[j]-S[j-1]-1))
b=vx[0]^(S[0]-1)*a*vx[len(S)]^(len(S)+d-S[len(S)-1])
return b
|