Julialang and Indices

Posted
Comments None

The decision of #julialang to start arrays from 1 is dubious. As the main purpose I’m using it for is to do some discrete recursive math, a ton of things are states that are either 0 or non-zero integer.

The easiest way to go about implementing the calculus from n to n+1for individual cells is to create a m-dimensional discrete space of neigbours states that implements the map. That way I can simply stuff the cell and neighbours into the array indices and it spits out the target cell. I can’t think of any faster way to implement that map. It’s yuck to have to add 1 to every index value when I otherwise don’t have to calculate at all.

Or, of course, I’m missing some incredible lang construct that was made for exactly that.

Update: Oh boy, did I miss some things.

Think The Other Way Round

Use Lambda Notation, whenever, and let the compiler do the indexing work. In most instances one has to use array indexes, chances are one is doing things the wrong way.

Insted of looping over an array index, loop over your function variables. For a person like me growing up with object oriented paradigms, this one needed a bit. In math you write something like:

∀ 0≤i≤N, 0≤j≤M: ( somehing(i, j) )

In Julia you write:

[something(i,j) for i in 0:N, j in 0:M]

Think math. Loop over your math variables, not the array indexes. Let the interpreter do the work. (I know, that’s really only lambda notation and has been around in python for ages. But it appears to be so more elaborate and intrinsic to julia).

Or append to a list:

A = [A[:]; elem]

Use map(), filter() and get used to using min(), max() on complex datatypes.

Or get an iterator with eachindex() if you really, really, really can’t think of any other way and you need the last bit of speed optimization and other functions really really don’t do it.

Author
Categories

Comments

Commenting is closed for this article.

← Older Newer →