MathGroup Archive 1996

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Generating inverse functions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg3527] Re: Generating inverse functions
  • From: wagner at bullwinkle.cs.Colorado.EDU (Dave Wagner)
  • Date: Wed, 20 Mar 1996 02:51:21 -0500
  • Organization: University of Colorado, Boulder
  • Sender: owner-wri-mathgroup at wolfram.com

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] ====


  • Prev by Date: Re: .m vs .ma Packages
  • Next by Date: Re: iterator strings as lists?
  • Previous by thread: Generating inverse functions
  • Next by thread: Re:Generating inverse functions