Re: Re: Generating inverse functions
- To: mathgroup at smc.vnet.net
- Subject: [mg3554] Re: [mg3527] Re: Generating inverse functions
- From: Allan Hayes <hay at haystack.demon.co.uk>
- Date: Mon, 25 Mar 1996 21:31:42 -0500
- Sender: owner-wri-mathgroup at wolfram.com
Dave Wagner wagner at bullwinkle.cs.Colorado.EDU (Dave Wagner) >Subject: [mg3527] Re: Generating inverse function Gives the following solution to the question raised by T. Daniel Crawford (attached) of defining inversefunc (inverse function of func) when the 1 to 1 function, func, is defined over a finite domain element by element. DownValues[func] /. (Literal[Literal][func[x_]] :> y_) :> (inversefunc[y] := x) Here is variant that avoids the interesting problem with Literal that Dave found (and solved). DownValues[func]/.( _[x_] :> y_ ) :> (inversefunc[y] := x); Allan Hayes hay at haystack.demon.co.uk *************************************** In article <4ifhjk$518 at ralph.vnet.net>, T. Daniel Crawford <crawdad at zopyros.ccqc.uga.edu> 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; First of all, you can access the stored values of a function using: (Local) In[29]:= DownValues[func] (Local) Out[29]= {Literal[func[i]] :> 1, Literal[func[j]] :> 2, Literal[func[k]] :> 3, Literal[func[l]] :> 4} Now it turns out to be quite hard to match the elements of this list using a pattern. The problem is that if you use "Literal[func[x_]]", the pattern matcher thinks that what you want to match is the stuff *inside* of the Literal. If you use "Literal[Literal[func[x_]]", the pattern matcher figures you are persistent but stupid and removes the "redundant" Literal. I have found that the following trick does the job (note the placement of the square brackets carefully): (Local) In[30]:= DownValues[func] /. (Literal[Literal][func[x_]] :> y_) :> {x,y} (Local) Out[30]= {{i, 1}, {j, 2}, {k, 3}, {l, 4}} By the way, it was necessary to surround the pattern with parentheses to force the correct parsing of the :> operators. Here's the full solution: (Local) In[31]:= DownValues[func] /. (Literal[Literal][func[x_]] :> y_) :> (inversefunc[y] := x) (Local) Out[31]= {Null, Null, Null, Null} (Local) In[32]:= ?inversefunc Global`inversefunc inversefunc[1] := i inversefunc[2] := j inversefunc[3] := k inversefunc[4] := l Dave Wagner Principia Consulting (303) 786-8371 dbwagner at princon.com http://www.princon.com/princon ==== [MESSAGE SEPARATOR] ====