Re: Problem with access nested list
- To: mathgroup at smc.vnet.net
- Subject: [mg121158] Re: Problem with access nested list
- From: Heike Gramberg <heike.gramberg at gmail.com>
- Date: Wed, 31 Aug 2011 07:41:49 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <201108311004.GAA12087@smc.vnet.net>
To set the value in a matrix at position {n1,=85,nk}, you could use
ReplacePart:
Permutator[n_] :=
Module[{temp, perms, i, setValue},
temp = ConstantArray[0, ConstantArray[n, n]];
perms = Permutations[Range[1, n]];
ReplacePart[temp, Thread[perms -> Signature /@ perms]]
];
By the way, since the permutation matrices are mostly zero it might be
more efficient to use sparse arrays
in which case your function becomes something like
Permutator2[n_] := Block[{temp},
temp = Permutations[Range[n]];
SparseArray[temp -> (Signature /@ temp)]]
You can use Normal to convert a SparseArray to a normal array.
Heike.
On 31 Aug 2011, at 12:04, Sam Takoy wrote:
> Hi,
>
> Below is a simple code, that constructs the n-dimensional permutation
> symbol (http://mathworld.wolfram.com/PermutationSymbol.html). I found
> myself jumping through hoops to accomplish a simple task.
>
> The main obstacle was: If I have and n x n x n list "temp", and I want
> to access the entry temp[[1, 2, 3]], but the numbers 1 2 and 3 are
> available in a list indicies = {1,2,3}, how do I do it? I tried
> temp[[list]] but that's not it. What I came up with is essentially
>
> Apply[Part[temp, #]&, indices]
>
> to convert from a list to arguments. I'm sure I'm missing something.
>
> Also, how to avoid the ugly loop?
>
> Many thanks in advance!
>
> Sam
>
> Permutator[n_] := Module[{temp, perms, i},
> temp = ConstantArray[0, ConstantArray[n, n]];
> setValue[val_][indices__] :=
> Module[{}, Part[temp, indices] = val];
>
> perms = Permutations[Range[1, n]];
> For[i = 1, i <= Length[perms], i = i + 1,
> setValue[Signature[perms[[i]]]] @@ perms[[i]];
> ];
> temp
> ];
>
- References:
- Problem with access nested list
- From: Sam Takoy <sam.takoy@yahoo.com>
- Problem with access nested list