Using zero based arrays and lists in Mathematica

Written by Nasser M. Abbasi

Updated October 26,2010

This method uses the Notation package to allow one to index an Array, list or a matrix by using a different indexing than the default which starts at 1.

The problem

Sometime there is a need to start the index of an array or a list from zero, as this can be more natural, depending on the problem.

To do this in Mathematica, all what is needed is to define a notation where the index mapping can occur. An example is shown below to explain how this was done

The solution: The Notation package is first loaded

The array is defined as normal, choosing to use a zero index for the content as this represents better the problem to solve

A notation is made to access the above array from zero to match the definition of its content. A subscript notation is selected in this example as it was found the easiest and most natural, but other notation could have been used.

The array u can now be referenced with zero index with the use of instead of the normal u[[i]]

The loop now appears more mathematically natural as it starts from zero to length-1

Comparing this to the default case. There is a mismatch in appearance

This method can be used with a vector (list) as well

Also for a matrix (table)

Even if the list or table is to be accesses using the default indexing (from 1), it might be useful to consider using this notation as it speeds entering the index. Instead of writing v[[i]], one enters which is much faster, and the code appears more clear as well

Less brackets counting and leads to a more clear code.