MathGroup Archive 2004

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Struggling with list element assignment in functions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg45584] Re: [mg45568] Struggling with list element assignment in functions
  • From: Daniel Lichtblau <danl at wolfram.com>
  • Date: Fri, 16 Jan 2004 06:04:59 -0500 (EST)
  • References: <200401140626.BAA19721@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Michael Tobis wrote:
> 
> For context, I am trying to create a toroidal data structure by
> causing edges of an array to be copies of
> rows and columns on the first interior line of the opposite edge.
> 
> I'm not entirely clear on what Hold does, but it seemed to solve my
> problem when wrapping the rows of the array:
> 
> ===
> Attributes[haloarray] = HoldFirst;
> haloarray[array_]/;MatrixQ[array] := Module[{},
>                 If [Length[array] <4 || Length[array[[1]] ]< 4,
>                         (Print["haloarray: matrix too small"];Return[Null])];
>                Map[halo,array];
>                array[[1]] = array[[Length[array] - 1]];
>                array[[Length[array]]] = array[[2]];
>         ]
> 
> Attributes[halo] = {};
> halo[x_]/;MatchQ[x,z_List] := Module[{y},
>                 y = x;
>                 If [Length[x] <4,(Print["halo: list too short"];Return[Null])];
>                y[[1]] = x[[(Length[x] - 1)]];
>                y[[Length[x]]] = x[[2]];
>         ]
> ===
> so, by putting in the HoldFirst, I could assign directly to the called
> array in haloarray[] rather than having to copy it twice, to and from
> a dummy array. Imagine my disappointment, then, in trying to extend
> this idea to the halo[] routine. This
> ===
> Attributes[halo] = HoldFirst;
> halo[x_]/;MatchQ[x,z_List] := Module[{},
>                 If [Length[x] <4,(Print["halo: list too short"];Return[Null])];
>                x[[1]] = x[[(Length[x] - 1)]];
>                x[[Length[x]]] = x[[2]];
>         ]
> ===
> refuses to do the assignments and yields all sorts of error messages.
> Any ideas? It's not a showstopper for me but I'd like to know what's
> up.
> 
> thanks
> Michael Tobis
> http://geosci.uchicago.edu/~tobis


A short way to make the new array is with ListConvolve. I think the
example below does what you have in mind.

In[47]:= mat = Table[(j+k^2), {j,3}, {k,5}]
Out[47]= {{2, 5, 10, 17, 26}, {3, 6, 11, 18, 27}, {4, 7, 12, 19, 28}}

In[48]:= ListConvolve[{{1}}, mat, {-2,2}]
Out[48]= {{28, 4, 7, 12, 19, 28, 4}, {26, 2, 5, 10, 17, 26, 2}, 
  {27, 3, 6, 11, 18, 27, 3}, {28, 4, 7, 12, 19, 28, 4}, 
  {26, 2, 5, 10, 17, 26, 2}}

It occurs to me that you might accomplish what you want (a toroidal
structure) without actually augmenting the array. You can instead use
the three-argument variant of Mod for element referencing. For example,

In[53]:= haloArrayRow[m_?MatrixQ, n_Integer] :=  m[[Mod[n,Length[m],1]]]

In[54]:=  haloArrayRow[mat, 8]
Out[54]= {3, 6, 11, 18, 27}

In[56]:= haloArrayElement[mat, 12,-7]
Out[56]= 12

One can similarly define a haloArrayColumn if so desired.


Daniel Lichtblau
Wolfram Research


  • Prev by Date: RE: Struggling with list element assignment in functions
  • Next by Date: divergence
  • Previous by thread: Struggling with list element assignment in functions
  • Next by thread: Re: Struggling with list element assignment in functions