# Replace variable with # and pass function body as an argument to a function

I have a very simple problem, which I would like to make a bit general, but none of my naive approaches have worked yet.

This is what I want to do step by step:

Define a parameter:

Protect[par1]
par1


Define some rules that assign later on par1 to a number:

rule = {par1->0.5}

rulemod= {Log[10 par1] -> Log[10*0.5]}


Define a symbolic function that transforms my parameter:

fOfPar = Log[10 par1]


At the end of the day I want to find the inverse function of fOfPar:

inverseFunc =
InverseFunction[
ConditionalExpression[Log[10  #], (Element[#1, Reals] && #1 > 0)] &]


ConditionalExpression[E^#1/10,#1[Element]Reals]&

inverseFunc[1.60944]


0.500001

Until here, everything fine. But I would like to construct a function, that given a symbolic function of par1, "strips" par1 from the symbolic function, replaces somehow by # and returns the inverse function.

Maybe I am completely wrong in my use of Block and Attributes, but this is what I've tried sofar:

ClearAll[passFuncToInverse]
SetAttributes[passFuncToInverse, HoldFirst];
passFuncToInverse[x_]:=Block[{xy, xx,para=par1},
xx = (x)/.{para->#};
InverseFunction[ConditionalExpression[ReleaseHold[xx],  (Element[#1,Reals]&& #1>0)]& ]


]

 passFuncToInverse[fOfPar]


InverseFunction[ConditionalExpression[ReleaseHold[xx],#1\ [Element]Reals&&#1>0]&]

passFuncToInverse[Log[10 par1]]


InverseFunction[ConditionalExpression[ReleaseHold[xx],#1[Element]Reals&&#1>0]&]

I know I am doing something wrong, but I guess this should be a possible thing to do in Mathematica.

Thanks

• Could you explain why you need the symbolic function to depend on par1 to begin with? If you are gonne replace par1 by # anyway, why not start with #? Is it related to how you generate the symbolic function? Oct 16, 2015 at 17:17
• Yes, well this is part of a bigger program. The idea is that the 'user' can give through a rule, either the value par1 or the value of F[ par1]. F is a symbolic function. I need to keep the rule like that, because it is needed in many other places. For some computations later on, F[par1] is more convenient and for others, par1 is better. This function F can change depending on the application. So in this way the user doesn't need to worry about specifying both. Oct 16, 2015 at 17:27
• For the "replace par1 with #" part you can use Function[par1,Log[10 par1]]. But Bob Hanlon's answer might be better... Oct 16, 2015 at 18:21
• Your description "to construct a function, that given a symbolic function of par1, "strips" par1 from the symbolic function, replaces somehow by # and returns the inverse function" is a bit abstract in my view, I think you'd better add a specific example for the expected input and corresponding output. Nov 16, 2015 at 4:39

f[par1_] = Log[10  par1];

invf = InverseFunction[f]

(*  ConditionalExpression[E^#1/10, -π < Im[#1] <= π] &  *)

invf[f[x]] // FullSimplify[#, Element[x, Reals]] &

(*  x  *)

f[invf[x]] // Simplify[#, Element[x, Reals]] &

(*  x  *)

invf[f[#]] & /@ {1/2, .5, 1, 1.}

(*  {1/2, 0.5, 1, 1.}  *)

f[invf[#]] & /@ {1/2, .5, 1, 1.}

(*  {1/2, 0.5, 1, 1.}  *)