Re:Generating inverse functions
- To: mathgroup at smc.vnet.net
- Subject: [mg3536] Re:[mg3493] Generating inverse functions
- From: fransm at win.tue.nl (Frans Martens)
- Date: Thu, 21 Mar 1996 01:09:00 -0500
- Sender: owner-wri-mathgroup at wolfram.com
In comp.soft-sys.math.mathematica article <4ifhjk$518 at ralph.vnet.net> you wrote: > I was wondering if anyone out there has any advice for me on the > construction of the inverse of a function supplied by the user. > These functions have only symbolic arguments. For example, > > The user provides us with: > > func[i] = 1; func[j] = 2; func[k] = 3; func[l] = 4; > > We would like to automatically construct the inverse function: > > funcInverse[1] = i; > funcInverse[2] = j; > funcInverse[3] = k; > funcInverse[4] = l; > > Perhaps this question has an obvious answer, but I don't see it. Of > course, if the functions were numerical and we knew their domain and > range, we could simply generate the inverse with a loop. In this case, > though, we don't know what domain the original function might have. Is > there a way to use Mathematica's stored values for the original function > to build the inverse? > > Thanks in advance for any help you can provide! > -Daniel > -- > T. Daniel Crawford Center for Computational Quantum > crawdad at otanes.ccqc.uga.edu Chemistry > > The use of DownValues[func] is a possibility. If the list DownValues[func] exists of elements of the form Literal[func[expr1]] :> expr2 with expr1 and expr2 free of blanks, then the following function will work: makeinverse[fun_,invfun_Symbol] := (DownValues[fun] /. (_[_[fun[a_]],b_] :> (Literal[invfun[b]]:>a)));) Example: In[13]:= Clear[f]; Do[f[i]=i^2,{i,1,5}]; In[15]:= makeinverse[f,g] In[16]:= ?g >From In[16]:= Global`g >From In[16]:= g[1] := 1 g[4] := 2 g[9] := 3 g[16] := 4 g[25] := 5 Frans Martens ==== [MESSAGE SEPARATOR] ====