Erlang/OTP Forums

Author Message

<  Erlang  ~  Multidimensional Arrays?

Tichy
Posted: Mon Nov 12, 2007 1:16 pm Reply with quote
Joined: 12 Nov 2007 Posts: 2
Hello,

is there an elegant way to deal with multidimensional arrays? I am new to Erlang and still not so used to the "assign once only" variables. It seems to me the way to deal with it with lists is to update the whole list in one recursive operation. But for more dimensions (or more complex data structures) it would probably get really messy, since the [H|T] trick doesn't work? I couldn't yet think of an elegant way to do it for a two-dimensional array.

What I wanted is a kind of cellular automaton, that is every cell is being updated according to the values of it's neighbors (more complicated rules than in a CA, though).

One desperate thought I have in the corner of my mind is to use one process per cell, as presumably at least a process can change it's state without all the other processes having to be changed, too?
View user's profile Send private message
a-priori
Posted: Mon Nov 12, 2007 8:17 pm Reply with quote
Joined: 12 Nov 2007 Posts: 1
If your simulation's "world" is relatively sparse, you could instead implement it as a list of key-value pairs where the key is the X-Y coordinates and the value is the state of that cell. Erlang's 'lists' module provides the key* functions, such as lists:keysearch/3 and lists:keyreplace/4 for manipulating lists like this.
View user's profile Send private message
francesco
Posted: Tue Nov 13, 2007 7:42 am Reply with quote
User Joined: 07 Jul 2006 Posts: 249 Location: London
An array module which will be part of stlib will appear in the next OTP release. It was presented at the Erlang User Conference on Thursday and should cover all your points. The proceedings will be published http://www.erlang.se/euc/07/ very soon.

Francesco
--
http://www.erlang-consulting.com
View user's profile Send private message Visit poster's website
Tichy
Posted: Wed Nov 14, 2007 4:02 pm Reply with quote
Joined: 12 Nov 2007 Posts: 2
Thank you for the replies!
View user's profile Send private message
anderst
Posted: Thu Nov 15, 2007 8:50 am Reply with quote
User Joined: 21 Nov 2006 Posts: 37
Looks like the presentation is now online!

http://www.erlang.se/euc/07/papers/1645Carlsson.pdf

Hilsen,
Anders
View user's profile Send private message Visit poster's website
francesco
Posted: Thu Nov 15, 2007 4:29 pm Reply with quote
User Joined: 07 Jul 2006 Posts: 249 Location: London
You should be able to look at the array module and documentation in the OTP R12 snapshot/daily build available at

http://www.erlang.org/download/snapshots/

I am sure they would appreciate your feedback.

Francesco
--
http://www.erlang-consulting.com
View user's profile Send private message Visit poster's website
tow21
Posted: Sat Mar 22, 2008 6:59 pm Reply with quote
Joined: 22 Mar 2008 Posts: 1
Hello, I just found this thread when looking for info on multidimensional arrays.

Unless I'm completely misreading the documentation, though, the array module in R12 only provides one-dimensional arrays.

a) Am I misreading the docs?

b) If not, is there a well-established way to build something like a multidimensional array so I can avoid reinventing the wheel?

Strictly speaking, I only need 2-D arrays - or rather, I need a data structure that lets me easily express things like: "all the elements between a(5,2) and a(5,7)", or equally, "all the elements between a(2,5) and a(7,5)". Obviously this could be built out of a list of lists, it just seems like overkill.
View user's profile Send private message
Bill M.
Posted: Tue Jun 10, 2008 12:46 am Reply with quote
User Joined: 06 Jun 2008 Posts: 24 Location: New York
While I haven't done this in Erlang, I've done some of this in C++ (not the array subarray/slicing approach) for large dynamically allocated arrays.

The trick is to create a 2d array interface that uses the one dimensional array as a primitive and use base + offset calculations to index the elements. For slicing you might need to create and return an array. Because you might want to keep bounds and other such information in the array, you may need a programming construct to encapsulate the salient information,
perhaps an Erlang Record (there are no classes, so I'm guessing that is the ticket) The record could have fields like min_row, max_row, min_col, max_col, array_1d.

I'm an Erlang newbie, so a more seasoned Erlang developer might have a better approach, but this is my gut reaction from other experiences.

Regards:

Bill M.
View user's profile Send private message

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum