# Replacing the Slot in an expression

So I've got Mathematica to handle the grunt work for me to obtain expression:

Root[ (* blah, blah *) + #^2 + (* etc, etc *)  ]


Now, the value inside Root is a polynomial whose all coefficients are at a range of values that Root cannot evaluate symbolically. I actually tried to it forcefully (that is, numerically), but the computer just keeps at it for hours. So I gave up on that.

Anyways, for further processing, I just extract the arguments of Root -- which can be done with function substitution and a some simple text manipulation:

sol = (* blah, blah *) + #^2 + (* etc, etc *)


Then I want to replace slot # and other slots so I can then apply more operations. And, this is where I get stuck... I don't know how to do it. I've tried:

sol & [x]   (* say I want all # to be x *)


So how do I do this?

EDIT: I've editted #3 to #. I really don't know why I didn't do so in the first place, but I soon as I noticed possible misconstrusion, I just panicked.

• The #3 tells me that you do not have a simple Root[] object, but a "triangular" one obtained from not finishing the Gröbner basis reduction in full. Why not share the entire thing? Alternatively, you might first want to do a RootReduce[] to turn it into a simple Root[] object where all the slots are of the form #1 and the polynomial inside is really the minimal polynomial corresponding to the root. – J. M.'s technical difficulties Jul 31 '15 at 10:45
• Alright, truth is it's #1, but say, if I used that people would think I'm trying to imply I'm number 1. So all I'd get is a lot of disses from other users here. Similarly I couldn't say #2, as it might be misinterpreted as me saying I'm number #2 which is understandably worse without having to explain it here. So actually it's just #1. – Dehbop Jul 31 '15 at 11:05
• Ignore all comment from here until up. – Dehbop Jul 31 '15 at 11:08
• That's… not really how we think of slots; the indexing merely refers to the corresponding argument. So, #3 is a short way to refer to the third argument of a pure function. I don't think anyone here will fault you for using #1. – J. M.'s technical difficulties Jul 31 '15 at 11:09
• In any case: have a look at MinimalPolynomial[]. – J. M.'s technical difficulties Jul 31 '15 at 11:11

I think this is what you're trying to get at. Say you're solving an equation that generates Root expressions:

solution = Solve[x^5 - x^4 + 13 == 0, x]


This gives a list of rules with a number of Root expression solutions. Let's just take a look at the first one, for simplicity:

firstroot = x /. First[solution] (* gives: Root[13 - #1^4 + #1^5 & , 1, 0] *)


Now you want just the first argument of this Root expression:

rootfunction = First[firstroot] (* gives: 13 - #1^4 + #1^5 & *)


And then you don't want those silly # characters, but something like x instead:

rootfunction[x] (* gives: 13 - x^4 + x^5 *)


I think a possible mistake you made is that you put an extra & after an expression that is already a pure function, i.e.:

rootfunction&[x] (* gives: 13 - #1^4 + #1^5 & *)


The reason for this, perhaps unexpected, behavior is that when you have a nested function like ( #1 & ) & [x] the outer function has no slots (no #s) for the x to fit into. So the x just "disappears" (it's not used in the outer function) and that just leaves the inner function #1& to be returned.

• Root does work even if the first argument is not a real function though. This works: N@Root[13 - #1^4 + #1^5, 1] (note there's no &!). And even this works: N@Root[13 - x^4 + x^5, 1]. These forms are undocumented and probably a bad idea, but I've seen them used a few times on online forums like this one. Maybe this has contributed to the confusion. – Szabolcs Jul 31 '15 at 14:41