MathGroup Archive 2005

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

Search the Archive

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

  • To: mathgroup at smc.vnet.net
  • Subject: [mg58535] Re: [mg58519] Re: Function to handle array with variable _number_ of dimensions?
  • From: Daniel Lichtblau <danl at wolfram.com>
  • Date: Wed, 6 Jul 2005 03:11:19 -0400 (EDT)
  • References: <dad7t1$36$1@smc.vnet.net> <200507050852.EAA07201@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Paul Abbott wrote:
> In article <dad7t1$36$1 at smc.vnet.net>, Joe Christy <joe at eshu.net> 
> 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?
> 
> 
> I'm not exactly sure what you are trying to do, but I would have a look 
> at Outer and Tuples (in 5.1). Also have a look at the Advanced 
> Documentation for these functions in the Help Browser.
> 
> Cheers,
> Paul
> 

These are probably the best approaches. If explicit iteration is for 
some reason really required, then you can generate the indexing 
programmatically as below.

arrayIndices[pp_,qq_] := Module[
   {indices=Array[index,qq]},
   Apply[Sequence,Map[{#,pp}&, indices]]
   ]

Example:
In[2]:= arrayIndices[3,4]
Out[2]= Sequence[{index[1], 3}, {index[2], 3},
   {index[3], 3}, {index[4], 3}]

It is not hard to modify to have later index bounds depend on earlier 
ones, etc. But if your index requirements are tensorial that is, no such 
dependencies), then the functionality offered by Outer and relatives is 
almost certainly going to be better for your purposes than generating 
and using an explicit set as above.


Daniel Lichtblau
Wolfram Research





  • Prev by Date: Re: Partial diff equations
  • Next by Date: Re: How to simulate random samples from crooked coin toss?
  • Previous by thread: Re: Function to handle array with variable _number_ of dimensions?
  • Next by thread: Re: Function to handle array with variable _number_ of dimensions?