Mathematica not evaluating #1

Related searches

I defined 2 objects:

f=x^2
g=x->#1

Why does this:

f /. x -> #1 &[5]

give me the expected result:

25

But this:

f /. g &[5]

gives me:

#1^2

As if the #1 wasn't evaluated to 5. Please help.

Function (short form &) has attribute HoldAll:

Attributes[Function]
{HoldAll, Protected}

Therefore g remains unevaluated. You can force it with Evaluate:

Evaluate[f /. g] &[5]
25

Evaluate will not work deeper in the expression; you cannot write f /. Evaluate[g] &

Variables Inside Functions Not Evaluating, Without going into too much detail, this is a result of the way pattern matching is carried out. When you write the rule f[y_]:={x,y} for instance,� Without going into too much detail, this is a result of the way pattern matching is carried out. When you write the rule f[y_]:={x,y} for instance, it tells the compiler that it should match anything and call it y, then replace any instance of y on the right side with the match.

You can make it work by keeping the pure function components together.

f = x^2
g = x -> #1 &

f/. g[5]

25

To run it over a list form the function before mapping.

f = x^2
g = x -> #1
list = {1, 2, 3, 4, 5};
b = Block[{a}, Function[f /. a] /. a -> g]
Map[b, list]

{1, 4, 9, 16, 25}

And for the specific problem in the comments...

vars = {x, y};
f = x + y;
g = Table[vars[[i]] -> Slot[i], {i, 1, Length[vars]}];
b = Block[{a}, Function[f /. a] /. a -> g];
list = {{1, 2}, {3, 4}, {5, 6}};
Map[b[Sequence @@ #] &, list]

{3, 7, 11}

With Mr. Wizard's answer this can become:

vars = {x, y};
f = x + y;
g = Table[vars[[i]] -> Slot[i], {i, 1, Length[vars]}];
list = {{1, 2}, {3, 4}, {5, 6}};
Map[Evaluate[f /. g] &[Sequence @@ #] &, list]

{3, 7, 11}

Evaluation Control—Wolfram Language Documentation, The Wolfram Language normally takes any expression it is given, and evaluates it as far as possible. But built into the Wolfram Language is a collection of� $\begingroup$ From the MoreInformation section of MatrixForm in the docs: MatrixForm acts as a "wrapper", which affects printing, but not evaluation. $\endgroup$ – kglr Mar 16 '12 at 19:54 1 $\begingroup$ @kguler and not all functions actually respect it, such as MatrixExp . $\endgroup$ – rcollyer Mar 16 '12 at 19:57

Replace g=x->#1 with g=x->#1 & and f /. g &[5] with f/. g[5]

Wolfram Mathematica does not evaluate at all, Wolfram Community forum discussion about Wolfram Mathematica does not evaluate at all. Stay on top of important topics and build connections by joining� Integrate can evaluate integrals of rational functions. It can also evaluate integrals that involve exponential, logarithmic, trigonometric, and inverse trigonometric functions, so long as the result comes out in terms of the same set of functions. Integrate can give results in terms of many special functions.

Evaluation of Expressions—Wolfram Language Documentation, Some functions that use non‐standard evaluation procedures. When you give a definition such as a=1, the Wolfram Language does not evaluate the a that� All expressions that do not explicitly depend on the variables given are taken to have zero partial derivative. The setting NonConstants {u 1, …} specifies that u i depends on all variables x, y, etc. and does not have zero partial derivative. »

Evaluate—Wolfram Language Documentation, Evaluate[expr] causes expr to be evaluated even if it appears as the argument of a function Basic Examples (1)Summary of the most common use cases. How come? This isn't an issue of not considering the negative root. The expressions are identical. As demonstrated by Simplify[a > m, a < m], replacing m with more complex expressions aside from having the Sqrt function. In fact, if I use Surd, no matter the nth root, even or odd, or square root, Simplify will evaluate completely into False. Why?

Another common syntax mistake is the naming of built-in Mathematica functions. Mathematica functions are always capitalized. Forgetting to capitalize a Sin[x] or an Exp[y-1] will cause errors because Mathematica will not recognize them. Defining Functions: Defining a new function in Mathematica is also slightly tricky, syntax-wise.

Comments
  • As so often with Mathematica, wrapping your expressions in Trace[] and examining the output is instructive here.
  • Thanks. But how could I modify the statement which gives me the wrong result to work as expected? I'm really a beginner in Mathematica so I don't know most things.
  • Interesting - I didn't try wrapping the fuller expression in Evaluate.
  • Okay, the problem is: Map[f /. {x -> #1} &, list]
  • Okay, the problem is, I'd like to do something like this: vars={x,y}; f=x+y; g=Table[vars[[i]] -> Slot[i], {i, 1, Length[vars]}]; list={{1,2},{3,4},{5,6}}; Map[f /. g &, list]; and get {5, 7, 11} This is because f would some expression from user and vars would be variable list so I can't hardcode the variables.
  • Great, thanks. One thing I don't understand: you send a Sequence to the block b. So the sequence is understood as parameter list for that block?
  • @ user1161552 - the function b expects input as a sequence, e.g. b[1,2] and will not accept a list like b[{1,2}]. Applying Sequence formats the input to b[1,2]. The Block is just used to shield 'a' in case 'a' was defined anywhere else, which would upset the replacement.
  • If you look at FullForm[b] you will see the Block has gone.