Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #14698 > unrolled thread

defining a function whose parameter must be a function with two parameters

Started byRoman <rschmied@gmail.com>
First post2013-06-01 10:08 +0000
Last post2013-06-11 06:22 +0000
Articles 3 — 3 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  defining a function whose parameter must be a function with two parameters Roman <rschmied@gmail.com> - 2013-06-01 10:08 +0000
    Re: defining a function whose parameter must be a function with two David Bailey <dave@removedbailey.co.uk> - 2013-06-02 04:25 +0000
      Re: defining a function whose parameter must be a function with two debguy <johnandsara2@cox.net> - 2013-06-11 06:22 +0000

#14698 — defining a function whose parameter must be a function with two parameters

FromRoman <rschmied@gmail.com>
Date2013-06-01 10:08 +0000
Subjectdefining a function whose parameter must be a function with two parameters
Message-ID<koch6b$t5t$1@smc.vnet.net>
Dear all,
I am trying to define a function F which will only execute if its parameter is a function with two parameters. Let's say I define it thus, without any checks on the parameter pattern f:

In[1] := F[f_] := f[2,3]

There are several ways of calling F:
1) pass it a function of two parameters:
In[2] := F[Function[{a,b},a^2-b^2]]
Out[2] = -5

2) pass it an anonymous function of two parameters:
In[3] := F[#1^2-#2^2 &]
Out[3] = -5

3) pass it a pre-defined function:
In[4] := g[a_,b_] = a^2-b^2;
In[5] := F[g]
Out[5] = -5

My question is: how can I define a pattern in the definition of F[f_] such that this function F will execute these three cases while not executing if called with any other kind of parameter? The following calls should fail, for example:

In[6] := F[Function[{a,b,c},a^2-b^2-3c]]
Out[6] = F[Function[{a,b,c},a^2-b^2-3c]]

In[7] := F[#1^2-#2^2-3#3 &]
Out[7] = F[#1^2-#2^2-3#3 &]

In[8] := h[a_,b_,c_] = a^2-b^2-3c;
In[9] := F[h]
Out[9] = F[h]

Further, for bonus points, if there are multiple definitions of a function, I'd like to pick the one with two parameters:
In[10] := k[a_,b_] = a^2-b^2;
In[11] := k[a_,b_,c_] = a^2-b^2-3c;
In[12] := F[k]
Out[12] = -5

Thanks for any help!
Roman

[toc] | [next] | [standalone]


#14705 — Re: defining a function whose parameter must be a function with two

FromDavid Bailey <dave@removedbailey.co.uk>
Date2013-06-02 04:25 +0000
SubjectRe: defining a function whose parameter must be a function with two
Message-ID<koehf1$2st$1@smc.vnet.net>
In reply to#14698
On 01/06/2013 11:08, Roman wrote:
> Dear all,
> I am trying to define a function F which will only execute if its parameter is a function with two parameters. Let's say I define it thus, without any checks on the parameter pattern f:
>
> In[1] := F[f_] := f[2,3]
>
> There are several ways of calling F:
> 1) pass it a function of two parameters:
> In[2] := F[Function[{a,b},a^2-b^2]]
> Out[2] = -5
>
> 2) pass it an anonymous function of two parameters:
> In[3] := F[#1^2-#2^2 &]
> Out[3] = -5
>
> 3) pass it a pre-defined function:
> In[4] := g[a_,b_] = a^2-b^2;
> In[5] := F[g]
> Out[5] = -5
>
> My question is: how can I define a pattern in the definition of F[f_] such that this function F will execute these three cases while not executing if called with any other kind of parameter? The following calls should fail, for example:
>
> In[6] := F[Function[{a,b,c},a^2-b^2-3c]]
> Out[6] = F[Function[{a,b,c},a^2-b^2-3c]]
>
> In[7] := F[#1^2-#2^2-3#3 &]
> Out[7] = F[#1^2-#2^2-3#3 &]
>
> In[8] := h[a_,b_,c_] = a^2-b^2-3c;
> In[9] := F[h]
> Out[9] = F[h]
>
> Further, for bonus points, if there are multiple definitions of a function, I'd like to pick the one with two parameters:
> In[10] := k[a_,b_] = a^2-b^2;
> In[11] := k[a_,b_,c_] = a^2-b^2-3c;
> In[12] := F[k]
> Out[12] = -5
>
> Thanks for any help!
> Roman
>
I don't think you can possibly achieve this with a simple pattern, but 
don't forget you can use an arbitrary predicate to control a pattern match:

F[x_?suitableFunctionQ]:= .........

Now you can test all sorts of possibilities inside your function 
suitableFunctionQ :

If x is an atom, you can retrieve its definitions with DownValues etc.

Then you can look for the Function head. This will cover the shorthand 
pure function notation as well because:

In[6]:= #1^2 - #2^2 - 3 #3 & // Head

Out[6]= Function

Etc.

The only problem might be performance!

David Bailey
http://www.dbaileyconsultancy.co.uk



[toc] | [prev] | [next] | [standalone]


#14798 — Re: defining a function whose parameter must be a function with two

Fromdebguy <johnandsara2@cox.net>
Date2013-06-11 06:22 +0000
SubjectRe: defining a function whose parameter must be a function with two
Message-ID<kp6fms$ncv$1@smc.vnet.net>
In reply to#14705
did you try this yet?

g[x_,y_]:=1;

f[Function[x_, y_]] := g[x, y]

it may be easier than (holding the argument to F and checking it's
pattern manually)

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web