       Re: Function to handle array with variable _number_ of dimensions?

• To: mathgroup at smc.vnet.net
• Subject: [mg58548] Re: Function to handle array with variable _number_ of dimensions?
• From: Daniel Huber <dh at metrohm.ch>
• Date: Wed, 6 Jul 2005 03:11:40 -0400 (EDT)
• Sender: owner-wri-mathgroup at wolfram.com

```Hi Joe,
English speaking people often complain about German speaking people
making long contorted, hard to understand, sentences. But consider:

its value times the sum over
consecutive diamond-shaped shells of the averages of the other values in
that shell, weighted by the reciprocal of their taxicab distance from
the middle cell.

maybe you would enjoy learning German.

If what you call "quantity" is a linear function of the array elements,
then the thing you are looking for is called "convolution". It is
heavily used in 1 and 2 dimensions for signal and image processing. I
never saw it in higher dimension, but it is stright forward.
In Mathematica there is "ListConvolve" for this purpose.
Let's assume we want to lightly smear out a signal by replacing every
point by the avarage of its neighbours.

1 dimensional example: This would e.g.  require a kernel of {1,1,1}/3
d=Table[0,{5}];
d[]=1;
ker={1,1,1}/3;
ListConvolve[ker,d,{3}]
this would transform a point:

into a smeared out point:
{0, 1/3, 1/3, 1/3, 0}

2 dimensional example:
Kernel: {{1, 1, 1}, {1, 1, 1}, {1, 1, 1}}

d = Table[0, {5}, {5}];
d[[3, 3]] = 1;
ker = Table[1, {3}, {3}]/9;
ListConvolve[ker, d, {2, 2}]
this would e.g. transform a point
0 0 0 0 0
0 0 0 0 0
0 0 1 0 0
0 0 0 0 0
0 0 0 0 0
into a smeared out point:
0    0        0       0     0
0    1/9    1/9    1/9    0
0    1/9    1/9    1/9    0
0    1/9    1/9    1/9    0
0     0       0       0     0

3 dimensional example:
d = Table[0, {5}, {5},{5}];
d[[3, 3,3]] = 1;
ker = Table[1, {3}, {3}, {3}]/27;
ListConvolve[ker, d, {2, 2,2}]

The output is a smeared out cube..

sincerely, Daniel

Joe Christy wrote:

>  Vis-a-vis Daniel's note of 07/05/2005 04:01 AM:
>
>
>>Hi Joe,
>>becomes sensible.
>>You have 2 integer arguments, P and Q. Why is PxPx..xP an array? What
>>does x mean? Is P an array?
>>...
>>
>>
>
>	Sorry to be unclear. P is an Integer and x means "by" in my original
>post (at the end of this post), so PxPx...xP is not an array itself, but
>rather describes the size and shape of an array.
>
>	I want to compute various quantities derived from a Q-dimensional array
>of P^Q integers, where not only the individual integers, but also their
>position within the array matters.
>	E.G.
>In the case P=7 and Q=2 a typical array of 49 integers is:
>2    45   32   31   28   22   43
>
>39   3    40   30   5	 36   10
>
>29   6    16   27   15   34   12
>
>4    7    8    21   26   19   46
>
>35   20   24   42   14   23   37
>
>9    25   48   1    33   41   17
>
>38   18   0    44   11   13   47
>and a typical quantity would be the average, over the entire array, of
>the function which assigns to, for example, the middle cell the value
>21*[(26+27+8+42)/4 + (19/2+15/2+30/2+16/2+7/2+24/2+1/2+14/2)/8 +
>(46/3+34/3+...+23/3)/12 + ...], i.e. its value times the sum over
>consecutive diamond-shaped shells of the averages of the other values in
>that shell, weighted by the reciprocal of their taxicab distance from
>the middle cell.
>
>Or, in the case P=3 and Q=4, the array (of 81 integers) might be:
>4  52 18   68 69 77   46 8  30
>79 32 20   76 47 24   23 73 13
>66 62 55   67 5  0    75 34 49
>
>33 63 53   7  36 64   72 39 60
>48 6  70   29 28 37   12 44 58
>74 35 9    45 27 40   22 26 15
>
>61 59 16   51 57 2    31 14 50
>54 71 10   17 11 42   19 21 56
>65 38 41   3  43 80   78 25 1
>and the quantity might be the sum of the values of a function on the
>array, which, at the cell with coordinates (1,2,3,2), depends on the the
>numbers 12 (at that cell) and 19, 22, 44, 72, 23, and 29 in the
>(12-19)+(12-22)+(12-44)+(12-72)+(12-23)+(12-29).
>
>	The point is that once I am given P and Q, I know how to compute the
>P^Q values in the array, but what I want to do is calculate a sort of
>integral over the entire array of the values "smeared" by a sort of
>kernel function of the Q coordinates.
>
>	In other words, I'd like to write a function like:
>F[P_Integer,Q_Integer]:=
>Sum[
>	g[P,Q,z1,z2, ... ,zQ],
>	{z1,0,P},{z2,0,P}, ... ,{zQ,0,P}
>]
>g[P_Integer,Q_Integer,zList__Integer]:=
>SomeExpression/;Length[zList]==Q
>My problem is that I don't know how, in Mathematica, to "handle the
>ellipsis" in order to allow for the variable number, Q, of indices for
>the array.
>
>
>
>>Joe Christy wrote:
>>
>>
>>
>>>I'd like to write a function that takes two integer arguments, P and Q
>>>say, and then returns a value calculated from the P x P x ... x P (Q
>>>factors) array, whose entries depend on both P and Q. My first impulse
>>>is to try and iterate over the array, but I don't see how to generate a
>>> non-fixed number of iteration variables.
>>>
>>>Does anyone have a suggestion of a good way to do this, short of writing
>>>a a distinct variant function for each possible value of Q?
>>>
>>>
>>>
>
>
>

--

Daniel Huber
Metrohm Ltd.
Oberdorfstr. 68
CH-9100 Herisau
Tel. +41 71 353 8585, Fax +41 71 353 8907
E-Mail:<mailto:dh at metrohm.ch>
Internet:<http://www.metrohm.ch>

```

• Prev by Date: Re: Partial diff equations
• Next by Date: Re: Re: Re: how to find n in expression x^n using a pattern?
• Previous by thread: Re: Function to handle array with variable _number_ of dimensions?
• Next by thread: Partial diff equations