Re: matrices with arbitrary dimensionality

• To: mathgroup at smc.vnet.net
• Subject: [mg76703] Re: [mg76640] matrices with arbitrary dimensionality
• From: Sseziwa Mukasa <mukasa at jeol.com>
• Date: Sat, 26 May 2007 04:27:42 -0400 (EDT)
• References: <200705251028.GAA07480@smc.vnet.net>

```On May 25, 2007, at 6:28 AM, 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...)

How's this?

In[11]:=
neighbors[pnt_]:=
Table[pnt+Table[d[i],{i,Length[pnt]}],
Evaluate[Sequence@@Table[{d[i],-1,1,2},{i,Length[pnt]}]]]
neighbors[{i,j}]
Out[12]=
{{{-1+i,-1+j},{-1+i,1+j}},{{1+i,-1+j},{1+i,1+j}}}

You have to specify 1D points as {i} but you could do

neighbors[pnt_List]:=
Table[pnt+Table[d[i],{i,Length[pnt]}],
Evaluate[Sequence@@Table[{d[i],-1,1,2},{i,Length[pnt]}]]]
neighbors[pnt_?SymbolQ]:=neighbors[{pnt}]

or

neighbors[pnt_?AtomQ]:=neighbors[{pnt}]

or something like that as suits your needs.

Regards,

Ssezi

```

• Prev by Date: Re: Stopping Automatic Animation in v6
• Next by Date: Re: New in Version 6
• Previous by thread: matrices with arbitrary dimensionality
• Next by thread: Re: matrices with arbitrary dimensionality