Re: matrices with arbitrary dimensionality
- To: mathgroup at smc.vnet.net
- Subject: [mg76739] Re: matrices with arbitrary dimensionality
- From: Ray Koopman <koopman at sfu.ca>
- Date: Sat, 26 May 2007 04:46:20 -0400 (EDT)
- References: <f36ea9$7tn$1@smc.vnet.net>
On May 25, 3:38 am, "alexxx.ma... at gmail.com" <alexxx.ma... 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 neighbors[v_List?VectorQ] := Flatten[{v-#,v+#}& /@ IdentityMatrix@Length@v,1] neighbors[{x,y,z}] {{-1+x,y,z}, {1+x,y,z}, {x,-1+y,z}, {x,1+y,z}, {x,y,-1+z}, {x,y,1+z}}