# How to generate a tensor by a given list, which specifies the ranges and dimensions of the tensor?

I am trying to create a function that generates a tensor according to a list, each of whose elements specifies the range of the corresponding dimension of the tensor and whose length specifies the total dimension of the tensor. I tried two versions using mainly "Table" and "Splice" functions.

Version 1:

ClearAll[i]
imax = {2, 3, 4};
Table[i[n], {n, Length[imax]}]
Table[{i[n], imax[[n]]}, {n, Length[imax]}]
Table[%%, Splice[%, Table]]


which produces

{i, i, i}
{{i, 2}, {i, 3}, {i, 4}}
Table[%%, Splice[%, Table]]


where "Splice" fails to work.

Version 2:

ClearAll[i]
imax = {2, 3, 4};
Table[i[n], {n, Length[imax]}]
Table[{i[n], imax[[n]]}, {n, Length[imax]}]
Table[%%, Splice[{{i, 2}, {i, 3}, {i, 4}}, Table]]


which produces

{i, i, i}
{{i, 2}, {i, 3}, {i, 4}}
{{{{1, 1, 1}, {1, 1, 2}, {1, 1, 3}, {1, 1, 4}}, {{1, 2, 1}, {1, 2,
2}, {1, 2, 3}, {1, 2, 4}}, {{1, 3, 1}, {1, 3, 2}, {1, 3, 3}, {1,
3, 4}}}, {{{2, 1, 1}, {2, 1, 2}, {2, 1, 3}, {2, 1, 4}}, {{2, 2,
1}, {2, 2, 2}, {2, 2, 3}, {2, 2, 4}}, {{2, 3, 1}, {2, 3, 2}, {2,
3, 3}, {2, 3, 4}}}}


where "Splice" works as expected.

Questions:

1. Why "Splice" does not work in the first version?
2. How can I embed "Splice" properly into "Table", so that I could make a function such as
TensorByList[list_]:=Module[{tensor},...;tensor]


which creates a tensor without a need to setup the iterators, which was the case with "Table" when I want to create a high dimension tensor:

Table[{i1,i2,...},{i1,i1max},{i2,i2max},...]

• Welcome to the Mathematica Stack Exchange. I will admit that I cannot understand your question; but try: Array[List, {2, 3, 4}]. A good question should (1) present the task that needs to be accomplished, (2) input data available in copy-paste-able form, and (3) desired output. Optionally, you can provide other details. As presented, your question is asking the participants primarily to do a code review.
– Syed
Sep 2, 2022 at 7:16
• According to document of Splice: "Splice is resolved as part of the standard evaluation process. It is not resolved in expressions that are held or inactive. " Table is a function with HoldAll attribute (it does analyze its argument to some degree, though), so the behavior of version 1 is actually expected, and the standard way to fix the code is to use Evaluate: Table[%%, Evaluate@Splice[%, Table]]. So, the real problem is not "Why "Splice" does not work in the first version?", but "Why "Splice" works in the second version?", which I'm not sure how to answer. Sep 2, 2022 at 7:30
• @Syed Indeed, the code sample given by OP isn't minimal, but (s)he isn't asking for code review. This is definitely an on-topic problem about the core language. (Particularly related to evaluation order and the surprisng evaluation mechanism of Table and Splice. Sep 2, 2022 at 7:34
• Sep 2, 2022 at 7:36
• @xzczd, I stand corrected then. Thanks for the feedback.
– Syed
Sep 2, 2022 at 7:41

i = 1; 