2
$\begingroup$

I am generally confused about integer valued polynomials, and how to count them. Trying to learn the subject I started by listing permutations of the rows in a lower triangular table:

$$\displaystyle T = \left(\begin{matrix} 1&0&0&0&0&0&0&\cdots \\ 1&1&0&0&0&0&0 \\ 1&1&1&0&0&0&0 \\ 1&1&1&1&0&0&0 \\ 1&1&1&1&1&0&0 \\ 1&1&1&1&1&1&0 \\ 1&1&1&1&1&1&1 \\ \vdots&&&&&&&\ddots \end{matrix}\right)$$

with the following Mathematica program:

Table[
 a = Table[1, {n, 1, k}];
 nn = Length[a];
 b = Flatten[
   Table[Permutations[
     Table[Table[If[n >= k, a[[n - k + 1]], 0], {k, 1, nn}], {n, 0, 
        nn}][[i]]], {i, 1, nn + 1}], 1];
 ArrayPlot[b], {k, 0, 6}]

The last output is the following arrayplot that could be shown in matrix form too:

confusion

Is there a simpler way to program this?

$\endgroup$
1
  • $\begingroup$ I could not find the appropriate tag. $\endgroup$ Apr 26 '15 at 14:44
1
$\begingroup$

An alternative way to construct the matrix b:

m = 6;
aa = LowerTriangularize[ConstantArray[1, {m + 1, m}], -1];
bb = Join @@ Permutations /@ aa;
bb == b
(* True *)
$\endgroup$
1
$\begingroup$

In the spirit of the code review tag:

This piece of code

a = Table[1, {n, 1, k}];
nn = Length[a];

is better written as

nn = 6;
a = ConstantArray[1, nn];

because it makes the assignment of nn explicit. In your newer version of the code you could use k throughout, there is no reason to define nn.

You're not often going to see experienced Mathematica users use nested Table commands, kguler's answer shows that they are unnecessary. However if you do you should use one Table with several indices.

Table[{a, b}, {b, 5}, {a, 5}] == Table[Table[{a, b}, {a, 5}], {b, 5}]
(* True *)

So you can write your nested Table like this:

t1 = Table[
   If[n >= k, a[[n - k + 1]], 0],
   {n, 0, nn}, {k, 1, nn}
   ];

Also an iterator like {n, 1, 5} can be written more tersely as {n, 5} - the 1 is not needed.

Finally, whenever you find yourself doing something like Table[f[list[[i]]], {i, 10}] this is a clear sign that you are doing something inefficiently, probably because of your prior experience with procedural languages. This syntax is identical to the much cleaner

f /@ list

Therefore matrix b can be computed with

b = Flatten[Permutations /@ t1, 1];

All taken together:

nn = 6;
a = ConstantArray[1, nn];
t1 = Table[
   If[n >= k, a[[n - k + 1]], 0],
   {n, 0, nn}, {k, 1, nn}
   ];
b = Flatten[Permutations /@ t1, 1];
ArrayPlot[b]

I created the intermediate symbol t1 which is not in your code. It's a good idea to divide things up. While in principle every Mathematica program can be a one-liner, the readability of the code will suffer. Readability is very important.

As I indicated above, experienced users gravitate towards functional solutions and so should you. Every time you find yourself writing loop like solutions using For, Do or Table take some time and think about if you can express the same thing with the functional operators.

$\endgroup$
1
  • $\begingroup$ I will take this into consideration, but I am originally a spreadsheet formula coder in Microsoft Excel, so it will take some time to learn functional operators. $\endgroup$ Apr 26 '15 at 15:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.