5 of 5
added 216 characters in body
Martin Ender
  • 196.6k
  • 65
  • 444
  • 969

Lists with repeated values

This is quite a common vector to work with:

{0,0}

It turns out this can be shortened by a byte:

0{,}

Even more bytes are saved if the vector is longer than two zeros. This can also be used to initialise zero matrices, e.g. the following gives a 2x2 matrix of zeros:

0{{,},{,}}

This can also be used for non-zero values if they're sufficiently large or sufficiently many or negative. Compare the following pairs:

{100,100}
0{,}+100
{-1,-1}
0{,}-1
{3,3,3,3}
0{,,,}+3

But remember that starting at 6 values, you're better off with 1~Table~6 in this case (potentially earlier, depending on precedence requirements).

The reason this works is that , introduces two arguments to the list, but omitted arguments (anywhere in Mathematica) are implicit Nulls. Furthermore, multiplication is Listable, and 0*x is 0 for almost any x (except for things like Infinity and Indeterminate), so here is what's happening:

  0{,}
= 0*{,}
= 0*{Null,Null}
= {0*Null,0*Null}
= {0,0}

For lists of 1s, you can use a similar trick by making use of exponentiation rules. There are two different ways to save bytes if you have at least three 1s in the list:

{1,1,1}
1^{,,}
{,,}^0
Martin Ender
  • 196.6k
  • 65
  • 444
  • 969