Skip to main content
Added more about Block.
Source Link
Somos
  • 5.1k
  • 1
  • 10
  • 16

There is more than one way to do things in Mathematica. A simple example code

build[a_buildExpr[a_, b_, x_]v_] := a + b x;v;
ClearAll[x]; (* clears values associated with 'x' *)
exprList = Table[build[aTable[buildExpr[a, b, x], {a, 3}, {b, 3}];
exprFunc[a_, b_, v_] := Block[{x = v}, exprList[[a, b]]];
exprFunc[1, 2, u] == 1 + 2 u

returns True. The initial problem you encountered was the Evaluate[exprList[[a, b]]] which was being evaluated in defining the exprFunc but a and b were not defined yet. You want to avoid using Evaluate and use Block instead to give a local value of the variable x which will be used by the expressions in exprList. The documentation for Evaluate shows that it is only needed for some special cases to override Holds.

There is more than one way to do things in Mathematica. A simple example code

build[a_, b_, x_] := a + b x;
exprList = Table[build[a, b, x], {a, 3}, {b, 3}];
exprFunc[a_, b_, v_] := Block[{x = v}, exprList[[a, b]]];
exprFunc[1, 2, u] == 1 + 2 u

returns True. The initial problem you encountered was the Evaluate[exprList[[a, b]]] which was being evaluated in defining the exprFunc but a and b were not defined yet. You want to avoid using Evaluate and use Block instead to give a local value of the variable x. The documentation for Evaluate shows that it is only needed for some special cases to override Holds.

There is more than one way to do things in Mathematica. A simple example code

buildExpr[a_, b_, v_] := a + b v;
ClearAll[x]; (* clears values associated with 'x' *)
exprList = Table[buildExpr[a, b, x], {a, 3}, {b, 3}];
exprFunc[a_, b_, v_] := Block[{x = v}, exprList[[a, b]]];
exprFunc[1, 2, u] == 1 + 2 u

returns True. The initial problem you encountered was the Evaluate[exprList[[a, b]]] which was being evaluated in defining the exprFunc but a and b were not defined yet. You want to avoid using Evaluate and use Block instead to give a local value of the variable x which will be used by the expressions in exprList. The documentation for Evaluate shows that it is only needed for some special cases to override Holds.

Added example use of code.
Source Link
Somos
  • 5.1k
  • 1
  • 10
  • 16

There is more than one way to do things in Mathematica. Here is one way:A simple example code

build[a_, b_, x_] := a + b x;
exprList = Table[build[a, b, x], {a, 3}, {b, 3}];
exprFunc[a_, b_, v_] := Block[{x = v}, exprList[[a, b]]];
exprFunc[1, 2, u] == 1 + 2 u

returns True. The initial problem you encountered was the Evaluate[exprList[[a, b]]] which was being evaluated in defining the exprFunc but a and b were not defined yet. You want to avoid using Evaluate in this case and use Block instead to give a local value of the variable x. The documentation for Evaluate shows that it is only needed for some special cases to override Holds.

There is more than one way to do things in Mathematica. Here is one way:

exprFunc[a_, b_, v_] := Block[{x = v}, exprList[[a, b]]];

The initial problem you encountered was the Evaluate[exprList[[a, b]]] which was evaluated in defining the exprFunc but a and b were not defined yet. You want to avoid using Evaluate in this case and use Block to give a local value of the variable x.

There is more than one way to do things in Mathematica. A simple example code

build[a_, b_, x_] := a + b x;
exprList = Table[build[a, b, x], {a, 3}, {b, 3}];
exprFunc[a_, b_, v_] := Block[{x = v}, exprList[[a, b]]];
exprFunc[1, 2, u] == 1 + 2 u

returns True. The initial problem you encountered was the Evaluate[exprList[[a, b]]] which was being evaluated in defining the exprFunc but a and b were not defined yet. You want to avoid using Evaluate and use Block instead to give a local value of the variable x. The documentation for Evaluate shows that it is only needed for some special cases to override Holds.

Source Link
Somos
  • 5.1k
  • 1
  • 10
  • 16

There is more than one way to do things in Mathematica. Here is one way:

exprFunc[a_, b_, v_] := Block[{x = v}, exprList[[a, b]]];

The initial problem you encountered was the Evaluate[exprList[[a, b]]] which was evaluated in defining the exprFunc but a and b were not defined yet. You want to avoid using Evaluate in this case and use Block to give a local value of the variable x.