 
 
 
 
 
 
Re: matrices with arbitrary dimensionality
- To: mathgroup at smc.vnet.net
- Subject: [mg76708] Re: matrices with arbitrary dimensionality
- From: David Bailey <dave at Remove_Thisdbailey.co.uk>
- Date: Sat, 26 May 2007 04:30:17 -0400 (EDT)
- References: <f36ea9$7tn$1@smc.vnet.net>
alexxx.magni at gmail.com wrote:
> Hi there,
> with V.6.0 I keep on with my experiment in translating a large C++
> spin simulation program.
> 
> What I'm bouncing my head against today is the following:
> 
> there is a way in M to describe objects (matrices) having an arbitrary
> number of dimensions, defined just at runtime?
> 
> In detail, I work on an orthogonal lattice where my spins are defined,
> but the simulations require sometimes to deal with 1d (arrays),
> sometimes more.
> 
> Yet I'd like to write the most general code it is possible, e.g. when
> writing a procedure that - given a location in the lattice at the
> coordinates {i,j,...} - returns a list of locations of the nearest
> neighbors
> (in 1d: {{i-1},{i+1}}; in 2d: {{i-1,j},{i+1,j},{i,j-1},{i,j+1}} and so
> on...)
> 
> thanks for any hint...
> 
> Alessandro Magni
> 
> 
I suppose you could write code in which the low level 
dimension-dependent operations are overloaded:
Nearestpoints[x_ /; ArrayDepth[x] == 1] := ...
Nearestpoints[x_ /; ArrayDepth[x] == 2] := ...
Nearestpoints[x_ /; ArrayDepth[x] == 3] := ...
However, I would be a little cautious before changing your code to this 
or any other scheme, because I suspect it will be critical to ensure 
that your code operates as fast as possible. The speed of Mathematica 
code can vary in some fairly subtle ways - so time any change before you 
decide to adopt it!
Also, if you intend to use the Compile function for production use, use 
it for this test - you don't want any unpleasant surprises just at the 
end of your project as you start to scale up to serious examples!
David Bailey
http://www.dbaileyconsultancy.co.uk

