Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2000
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

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: [mg21761] Re: [mg21733] Making a function dynamically define another conditional function...
  • From: Hartmut Wolf <hwolf at debis.com>
  • Date: Thu, 27 Jan 2000 22:56:42 -0500 (EST)
  • Organization: debis Systemhaus
  • References: <200001260845.DAA02315@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Paul Howland schrieb:
> 
> 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?
> 


Hello Paul,

in your definitions above you seem to define function values only for
certain discrete arguments, so I understand your function to be
equivalent with

     f[A] = A+a 
     f[B] = B+b 
     etc.

For that just do

In[25]:= Apply[(f[#2] = #1 + #2) &, {{1, 2}, {3, 4}, {5, 6}}, {1}];
In[26]:= Information["f", LongForm -> False]
"Global`f"
f[2] = 3
 
f[4] = 7
 
f[6] = 11


To make up another example, and to fully show the metaprogramming
method, e.g. if you want to have

     ff[x_] := a /; x <= A
     ff[x_] := b /; x <= B
     etc.

In[35]:= makeFun[f_Symbol, data_] := 
      (Apply[(f[x_] := #1 /; x <= #2) &, data, {1}])

In(36]:= makeFun[ff, {{1, 2}, {3, 4}, {5, 6}}];

In[37]:= Information["ff", LongForm -> False]
"Global`ff"
ff[x_] := 1 /; x <= 2
 
ff[x_] := 3 /; x <= 4
 
ff[x_] := 5 /; x <= 6

Regard that parentheses enclosing Set or SetDelayed in the defining
functions are neccessary.

Kind Regards,  Hartmut


  • Prev by Date: RE: Making a function dynamically define another conditional function...
  • Next by Date: Re: Making a function dynamically define another conditional function...
  • Previous by thread: Making a function dynamically define another conditional function...
  • Next by thread: Re: Making a function dynamically define another conditional function...