Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
1998
*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 1998

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

Search the Archive

Re: strange function that defines a function---how do I do it?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg15229] Re: strange function that defines a function---how do I do it?
  • From: dreissBLOOP at bloop.earthlink.net (David Reiss)
  • Date: Wed, 23 Dec 1998 01:04:06 -0500
  • Sender: owner-wri-mathgroup at wolfram.com

Here is one way to do it  (I allow you to give the head  of the function
that you define as the first argument):

In[1]:=

Clear[FunMake1];

FunMake1[f_,x_List,expr_]:=

 (
  Clear[f];
  f[Sequence@@(Pattern[#,_]&)/@x]:=expr
 )

In[2]:= FunMake1[h, {x,y,z},x^2 + y/z]

In[3]:= ?h

"Global`h"

h[x_, y_, z_] := x^2 + y/z

In[4]:= h[a,b,c]

Out[4]=

\!\(a\^2 + b\/c\)


Also, if you wan to add a NumberQ condition you can write:


In[5]:=
Clear[FunMake2];

FunMake2[f_,x_List,expr_]:=

 (
  Clear[f];
  f[Sequence@@(Pattern[#,_?NumberQ]&)/@x]:=expr
 )

In[6]:= FunMake2[g, {x,y,z,t},x^2 + y/z+Sin[t]]


In[7]:= ?g

"Global`g"

g[x:_?NumberQ, y:_?NumberQ, z:_?NumberQ, t:_?NumberQ] := x^2 + y/z +
Sin[t]

In[8]:= g[a,1,2,3]

Out[8]= g[a,1,2,3]

In[9]:= g[3,4,5,6]

Out[9]= \!\(49\/5 + Sin[6]\)


Regards,

David


In article <75no5e$sgj at smc.vnet.net>, belotto at vassar.edu (Benjamin
Lotto) wrote:

> Working with Mathematica 3.0:
> 
> Suppose I define
> 
>    makefunction[var_, expr_]:=f[var_?NumberQ]:=NIntegrate[expr,{x,0,1}]
> 
> Then I can do something like
> 
>    z=x^2
>    makefunction[x,z]
> 
> and it will be just as if I typed
> 
>    f[x_?NumberQ]:=NIntegrate[x^2,{x,0,1}]
> 
> In fact, entering
> 
>    ?f
> 
> at this point yields
> 
>    "Global`f"
> 
>    f[(x_)?NumberQ] := NIntegrate[x^2, {x, 0, 1}]
> 
> OK, now suppose I have a list of variables  x1  up through  xn.  Suppose
> they're in a list:
> 
>    list={x1,x2,...,xn}
> 
> I want something like "makefunction" above so that when I type
> 
>    makefunction[list,expr]
> 
> it will be just as if I typed
> 
>    f[x1_?NumberQ,x2_?NumberQ,...,xn_?NumberQ]:=expr
> 
> analogous to the above.  Can anyone help me?
> 
> Thanks in advance.  Y'all out there have been very helpful in the past.

-- 
David Reiss
dreissBLOOP at bloop.earthlink.net
To send personal email, remove the words  "bloop" and "BLOOP" from the
email address


  • Prev by Date: Getting rid of annoying zeroes in algebraic expressions
  • Next by Date: Launching a front end notebook from a Kernel (mathlink & Borland C++) (fwd)
  • Previous by thread: strange function that defines a function---how do I do it?
  • Next by thread: Re: strange function that defines a function---how do I do it?