Re: matrices with arbitrary dimensionality

*To*: mathgroup at smc.vnet.net*Subject*: [mg76910] Re: matrices with arbitrary dimensionality*From*: "alexxx.magni at gmail.com" <alexxx.magni at gmail.com>*Date*: Wed, 30 May 2007 05:10:47 -0400 (EDT)*References*: <f36ea9$7tn$1@smc.vnet.net><f38tn5$ivf$1@smc.vnet.net>

Thanks everybody. I solved in the following way - with your help - my needs concerning arbitrary dimension matrices. Here I generate it, with dim dimensions, and hypercube lateral size = lspins: generateArbMat[dim_, lspins_] := Module[{ind}, ind = Map[{#, lspins} & , Map[Unique, Map[FromCharacterCode, 64 + Range[dim]]]]; Apply[Table[0, ##] &, ind] ] In[301]:= generateArbMat[4, 2] Dimensions[%] Out[301]= {{{{0, 0}, {0, 0}}, {{0, 0}, {0, 0}}}, {{{0, 0}, {0, 0}}, {{0, 0}, {0, 0}}}} Out[302]= {2, 2, 2, 2} Here is the code for the next neighbors (thank you Ray!), adapted for periodic boundary conditions: neighbors[v_List?VectorQ] := Module[{a}, a = Flatten[{v - #, v + #} & /@ IdentityMatrix[Length[v]], 1]; a = a /. 0 -> 1; a = a /. lspins + 1 -> 1; a ] Here is the function giving the array of all the matrix indices: locations[] := Distribute[Table[Range[lspins], {dim}], List] In[307]:= locations[] (* dim=2 and lspins=4 *) Out[307]= {{1, 1, 1}, {1, 1, 2}, {1, 1, 3}, {1, 2, 1}, {1, 2, 2}, {1, 2, 3}, {1, 3, 1}, {1, 3, 2}, {1, 3, 3}, {2, 1, 1}, {2, 1, 2}, {2, 1, 3}, {2, 2, 1}, {2, 2, 2}, {2, 2, 3}, {2, 3, 1}, {2, 3, 2}, {2, 3, 3}, {3, 1, 1}, {3, 1, 2}, {3, 1, 3}, {3, 2, 1}, {3, 2, 2}, {3, 2, 3}, {3, 3, 1}, {3, 3, 2}, {3, 3, 3}} To setup the matrix values (e.g. with random values): f = generateArbMat[d, lspins]; f = f /. 0 :> RandomReal[NormalDistribution[0., 1.]]; Last, to operate on the matrix, e.g. to sum all the f values of the neighbors of a given location i0: Apply[Plus, Map[Extract[f, #] &, neighbors[i0]]] any other suggestion is welcome, thanks! Alessandro On 26 Mag, 11:13, Ray Koopman <koop... at sfu.ca> wrote: > 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}}