# Question:Translating a List of Lists with arbitrary nesting to a Matrix

## Question:Translating a List of Lists with arbitrary nesting to a Matrix

Maple
Hello everyone, I asked yesterday a question in the newbies section. I have found a partial answer which I detail further down but still have some questions and errors. I hope someone can give me a hand. Thanks. The question is more or less the following: Given the parameter k. One declares a nested list like the following: ``` L[n[1],n[2],...n[k],m[1],m[2],.... m[k] ], ``` where all 'n' and 'm' indexes run in the interval: ``` n[i] =0,1,.. ,d-1 m[i] =0,1,.. ,d-1 ``` QUESTION: How can we translate this list into a square matrix of dimension d^k that has the indexes: M(i,j) where i,j = 0,1,2...., d^(k-1). ? The expression that relates the indexes is: ``` i := sum(d^(i)*n[i], i = 0 ..(k-1)) j:= sum(d^(i)*m[i], i = 0 .. (k-1)) ``` or also: ``` i = n[0] + n[1]*d+...+n[k-1]*d^(k-1) j = m[0] + m[1]*d+...+m[k-1]*d^(k-1) ``` Therefore, if I make an integer division of the index "j" `j := floor ( j / d )` The first term of the sum will disapear: ``` floor ( j / d ) = floor (m[0]/d + m[1]*d/d+...+m[k-1]*d^(k-1)/d) =m[1]+...+m[k-1]*d^(k-2) ``` so the transformation can be written as: ``` for i from 0 to (d^k -1) do for i from 0 to (d^k -1) do Indx:=i ; Jindx:=j ; for s from 0 to (k-1) do n[s]:= Indx mod d: Indx:= floor (Indx/d) ; n[s]:= Jindx mod d : Jindx:= floor (Jindx/d) ; end do; M(i,j):= L(n[0],n[1],...,n[k-1],m[1],m[2],...,m[k-1]) ; end do; end do; ``` Which saves us from doing 2k "do" loops. But when I try to write it down to translate the indexes: for example: ``` k:=3; d:=3; for i from 0 to (d^k -1) do for i from 0 to (d^k -1) do Indx:=i ; Jindx:=j ; for s from 0 to (k-1) do n[s]:= Indx mod d: Indx:= floor (Indx/d) ; n[s]:= Jindx mod d : Jindx:= floor (Jindx/d) ; end do; end do; end do; ``` then I obtain the error: Error, illegal use of an object as a name Does anyone know why? My other problem is once k is given, how can I declare an object like: ``` L(n[0],n[1],...,n[k-1],m[1],m[2],...,m[k-1]) ; ``` and how can I use it in the above loop. I tried something like: ``` L[cat(n, 1 .. k-1), cat(m, 1 .. k-1)]: ``` but it declares indexes like: n1,n2,n3.... instead of n[1],n[2],n[3] ... and then I don't know what to do with these indexes in the loop. Thank you all for your time, Cheers, Al.
﻿