2
$\begingroup$

I am working on an program that uses a parametric dimensions table created like this:

t = Table @@ Prepend[{2, 3, 4, 5}, 0]

No problem to get the value of a given item by coordinates: Part@@ Prepend[{2, 2, 2, 2}, t] The problem is that I cannot use the above technique to set a given item value. The system answers me:

"Tag Apply in Part@@{{{{{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0}},{{0,0,0,0,\
0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0}},{{0,0,0,0,0},{0,0,0,0,0},{0,0,\
0,0,0},{0,0,0,0,0}}},{{{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0}\
},{{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0}},{{0,0,0,0,0},{0,0,\
0,0,0},{0,0,0,0,0},{0,0,0,0,0}}}},1,2,1,2} is Protected."
$\endgroup$

2 Answers 2

2
$\begingroup$

This is because t gets evaluated to its value instead of staying a symbol whose value you want to change. If you have part specifications in a list, you can use Sequence to use them in Part.

t[[Sequence @@ {2, 2, 2, 2}]] = 3;

(* or *)

Part[t, Sequence @@ {2, 2, 2, 2}] = 5;
$\endgroup$
1
  • $\begingroup$ Yes, that is the explanation of the error message. That is not the thought process error that lead to the error. It seems reasonable that Giovanni Russo is thinking procedurally like Fortran or C and is preallocating memory for a table and then going to fill the table. After for 5 years at an unversity and 32 years at major research laboratories (I am retired), I have learned how they think and that you have take them back to what caused their problem because the answer must make sense to them in their context. $\endgroup$
    – anon
    Apr 1, 2023 at 1:18
0
$\begingroup$

Please further down. If it can be done with Ackmann function, then it can be done with any computable function.

Try ConstantArray[0,{2, 3, 4, 5}] instead.

I guess that you plan on follow up with nested Do or For statement to fill in your table. This is not the Mathematica way. Mathematica is not like Fortran, C, or other procedural languages. This would be a good time to read What are the most common pitfalls awaiting new users?

Table[someFunction[i, j, k, m], {i, 2}, {j, 3}, {k, 4}, {m, 5}]

where someFunction is an expression using i,j,k,m.

Example using Ackermann function as the demonstration function.

By the way, values of Ackermann function are huge even for small integers. There also is a demonstration of memoization of previous results included. The demonstration used small integers because Ackermann function's become huge with very small integers. Please, see Wolfram MathWorld and Demonstrating Ackermann's Function.

ClearAll[someFunction]; ClearAll[someFunctionMemo]; 
someFunctionMemo = Association[]; 
someFunction = Function[{m, n}, Block[{key = {m, n}, result}, 
     Catch[Which[ !(IntegerQ[m] && m >= 0 && IntegerQ[n] && n >= 0), 
        Throw[Undefined], KeyExistsQ[someFunctionMemo, {m, n}], 
        result = someFunctionMemo[[Key[key]]], m == 0, 
        result = n + 1; AppendTo[someFunctionMemo, key -> result]; 
         result, n == 0, result = someFunction[m - 1, 1]; 
         AppendTo[someFunctionMemo, key -> result]; result, True, 
        result = someFunction[m - 1, someFunction[m, n - 1]]]; 
       result]]]; 
$RecursionLimit = 8192
TableForm[Table[someFunction[i, j], {i, 0, 3}, {j, 0, 8}]]

1 2 3 4 5 6 7 8 9
2 3 4 5 6 7 8 9 10
3 5 7 9 11 13 15 17 19
5 13 29 61 125 253 509 1021 2045

$\endgroup$
2
  • $\begingroup$ This does not really answer the OP's question. It is also not always possible to use this approach because the table elements might depend on each other, so they cannot be independently calculated by someFunction. $\endgroup$
    – Domen
    Apr 1, 2023 at 10:21
  • $\begingroup$ @Domen Your statement is false. $\endgroup$
    – anon
    Apr 1, 2023 at 15:41

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.