{{{id=35| /// }}}

The function Subsets(n, k) creates all subsets of range(1, n+1) of length k

However, to list them you should use Subsets(n,k).list() or list(Subsets(n,k))

{{{id=2| Subsets(4,2).list() /// [{1, 2}, {1, 3}, {1, 4}, {2, 3}, {2, 4}, {3, 4}] }}}

The following function is one half of the answer to Exercise 1, in order to compete the problem you should write the function "subset_to_monomial" which takes a subset $SS = \{ a_1 < a_2 < ... < a_{n-1} \}$ and converts it into the monomial

$$x_1^{a_1 - 1} x_2^{a_2-a_1-1} \cdots x_{n-1}^{a_{n-1}-a_{n-2}-1} x_n^{n+d-1-a_{n-1}}$$

{{{id=1| def list_all_monomials(n, d): return [subset_to_monomial(SS, d) for SS in Subsets(n+d-1,d)] /// }}} {{{id=3| list_all_monomials(3,2) /// Traceback (most recent call last): File "", line 1, in File "_sage_input_7.py", line 10, in exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("bGlzdF9hbGxfbW9ub21pYWxzKDMsMik="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))' + '\n', '', 'single') File "", line 1, in File "/private/var/folders/_3/qq_ptsxd0bd645cgmgt_ssrm0000gn/T/tmpxKSUg9/___code___.py", line 3, in exec compile(u'list_all_monomials(_sage_const_3 ,_sage_const_2 )' + '\n', '', 'single') File "", line 1, in File "/private/var/folders/_3/qq_ptsxd0bd645cgmgt_ssrm0000gn/T/tmp14LB_Z/___code___.py", line 4, in list_all_monomials return [subset_to_monomial(SS, n) for SS in Subsets(n+d-_sage_const_1 ,d)] NameError: global name 'subset_to_monomial' is not defined }}}

The following command make a list of 10 variables x0 through x9

{{{id=5| vx = [var("x"+str(i)) for i in range(10)] /// }}} {{{id=6| vx[3] /// x3 }}}

The following is an expample of a product over a set

syntax:

mul( expr for var in aset )

{{{id=7| mul( vx[i]^i for i in range(10) ) /// x1*x2^2*x3^3*x4^4*x5^5*x6^6*x7^7*x8^8*x9^9 }}} {{{id=42| /// }}}

You can also create a monomial with words instead of subsets, but since they are in bijection, they should be equivalent.

{{{id=11| def num_0_eq_2( w ): return w.count('0')==2 # true if the number of 0's is equal to 2 /// }}} {{{id=8| Words(alphabet=['0','1'], length=4).filter( num_0_eq_2 ).list() /// [word: 0011, word: 0101, word: 0110, word: 1001, word: 1010, word: 1100] }}} {{{id=25| for w in Words(alphabet=[0,1], length=4): if count_zeros(w)==4: print w /// 0000 }}} {{{id=10| w = Word(['0','1','0','1']) /// }}} {{{id=26| LL = [[1,2],[3,4],[5,6,7]] /// }}} {{{id=27| LL[0] /// [1, 2] }}} {{{id=28| LL[0][0] /// 1 }}} {{{id=29| [mul(var("x"+str(v)) for v in L if v>3) for L in LL] /// [1, x4, x5*x6*x7] }}} {{{id=30| SS = [3,5,7] /// }}} {{{id=34| deg = 7 /// }}} {{{id=33| SSp = [0]+SS+[deg+len(SS)+1] /// }}} {{{id=31| mul(var("x"+str(j))^(SSp[j+1]-SSp[j]-1) for j in range(len(SSp)-1)) /// x0^2*x1*x2*x3^3 }}} {{{id=24| def subset_to_monomial( SS, n ): /// }}} {{{id=32| [0]+[1,2,3]+[8] /// [0, 1, 2, 3, 8] }}} {{{id=15| w.count('0') /// 2 }}} {{{id=17| w = Word([1,0,0,0,1,0,0,0,0,1]) /// }}} {{{id=18| for i in range(len(w)): print w[i] /// 1 0 0 0 1 0 0 0 0 1 }}} {{{id=16| out = 0 for i in range(len(w)): if w[i]==0: out=out+1 out /// 7 }}} {{{id=19| def count_zeros( w ): out = 0 for i in range(len(w)): if w[i]==0: out=out+1 return out /// }}} {{{id=20| count_zeros(w) /// 7 }}} {{{id=21| w.count(0) /// 7 }}} {{{id=22| /// }}}