0
$\begingroup$

I'm trying to create a list of symbols called "x" for calculating symbolic gradients of some functions, so I write:

x = Table[Symbol["x"][i], {i, 1, 3}]

why it complains $RecursionLimit: Recursion depth of 1024 exceeded?

When I change the expression name from x to a, it works

a = Table[Symbol["x"][i], {i, 1, 3}]

Outputs

{x[1], x[2], x[3]}

I knew c++ and python, but feel really hard to understand what happened when I type these symbol creation sentences in mathematica.

Is there any relevant document to read or learn about this topic? Any help is appreciated.

$\endgroup$
3
  • $\begingroup$ Welcome to Mathematica StackExchange! I would suggest reading this Fast Introduction to Wolfram Language for programmers. It also has special notes for people coming from Java/Python background. I think this will be useful for you to grasp some basic differences and peculiarities of Mathematica. $\endgroup$
    – Domen
    Commented Apr 20, 2023 at 9:53
  • $\begingroup$ Welcome to the Mathematica Stack Exchange. The introductory book written by the inventor is a good learning resource. There is a fast intro for math students as well as a fast intro for programmers to choose from. $\endgroup$
    – Syed
    Commented Apr 21, 2023 at 4:16
  • $\begingroup$ Many thanks Domen and Syed. I didn't manage to find these precesie resource instead of the offical documents. I will now go to learn them and evaluate my problem again. $\endgroup$
    – Xudong
    Commented Apr 22, 2023 at 6:11

1 Answer 1

1
$\begingroup$

The evaluator will evaluate an expression until nothing changes any more. Consider some code that gives a recursion:

x = Table[Symbol["x"][i], {i, 1, 2}]

To begin, the right side is evaluated and gives "x" the value:

{x[1],x[2]}

"x" has now the value {x1,x[2]}. This contains the symbol "x". Therefore this is again evaluated to:

{{x1,x[2]}1,{x1,x[2]}[2]}

We can simulate this by giving different names to each new value of x:

x1 = Table[Symbol["x"][i], {i, 1, 2}]
x2 = Table[Symbol["x1"][i], {i, 1, 2}]
x3 = Table[Symbol["x2"][i], {i, 1, 2}]

enter image description here

On the other hand, the following does not create a recursion because after one step the evaluation stops because "x" has no value:

a = Table[Symbol["x"][i], {i, 1, 2}]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.