MathGroup Archive 2000

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

Search the Archive

Re: Making a function dynamically define another conditional function...

  • To: mathgroup at smc.vnet.net
  • Subject: [mg21794] Re: Making a function dynamically define another conditional function...
  • From: Leszek Sczaniecki <lsczanie at motown.lmco.com>
  • Date: Thu, 27 Jan 2000 22:57:33 -0500 (EST)
  • Organization: Lockheed Martin Corp, Valley Forge PA
  • References: <86md60$2cr@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Paul Howland wrote:

> How can I make a function dynamically define a conditional function?
>
> Given a list of arguments {{a,A}, {b,B}, ...} I want to write a function
> that will take these arguments, and generate a new function, f say,
> which is defined as (for example):
>     f[x_] := x+a /; x==A
>     f[x_] := x+b /; x==B
>     etc.
>
> So, the obvious solution is to define a function as follows:
>
> In[1] := TestFn[data_List] := Module[{args},
>             ClearAll[f];
>             Do[
>                 args = data[[i]];
>                 f[x_] = x + args[[1]] /; x==args[[2]],
>                 {i, Length[data]}
>             ]]
>
> and call it using something like TestFn[{{1,2},{3,4},{5,6}}].
>
> But this doesn't work (see attached notebook) as it appears that
> Mathematica does not evaluate any part of the condition at the time of
> definition, so args[[2]] remains unevaluated.  As a consequence, the
> resulting function definition is not properly defined.
>
> So, the obvious solution to this is to wrap Evaluate[] around the
> condition (i.e. define the function as f[x_] = x + args[[1]] /;
> Evaluate[x == args[[2]]].  And this appears to work.  If you do ?f, then
> you see a function comprising a number of conditional definitions.
> However, if you come to use the function, then it appears that
> Mathematica does not perform the condition test that appears in the
> definition!  It simply uses the first definition it finds.
>
> What is going on?!  How can I make this work?
>
> I attach a notebook with example code. [Contact Paul to
> get this notebook - Moderator]
>
> Many thanks for any help.
>
> Paul

I am not sure that I fully understand what you want. If your goal is to add
second level elements of a two dimensional list, perhaps the functional
approach would be the best.

In[1]:= lst = {{A,a},{B,b},{C,c}};

In[2]:=
Map[Plus @@# &, lst ]

Out[2]= {a+A,b+B,c+C}

If you need a function that deals with individual elements of the list,
perhaps the simplest solution would be to define an auxiliary function, say
aux, and use it in the definition of f.

In[3]:=
Map[ToExpression["aux["<>ToString[#[[1]]]<>"]="<>ToString[#[[2]]]]&, lst];

In[4]:=
aux[A]

Out[4]=
a

In[5]:= f[x_]:= x + aux[x]

In[6]:= f[B]

Out[6]=
b+B

If your example is just a simplified version of more complicated operations
on functions, keep in mind that you can define functions that modify other
functions like in the following example.

In[7]:= ShiftValue[f_, s_] := Function[x, f[x]+ s]

In[8]:= g := ShiftValue[Sin, a]

In[9]:= g[x]

Out[9]=
a+Sin[x]

-- Leszek Sczaniecki



  • Prev by Date: Re: Making a function dynamically define another conditional function...
  • Next by Date: Re: DSolve problems with system of ODEs. Out in pure notation?
  • Previous by thread: RE: Making a function dynamically define another conditional function...
  • Next by thread: Re: Making a function dynamically define another conditional function...