Re: Options in self-defined functions
- To: mathgroup at christensen.cybernetics.net
- Subject: [mg1600] Re: Options in self-defined functions
- From: Scott.A.Hill at williams.edu (Lancelot)
- Date: Tue, 4 Jul 1995 01:38:27 -0400
- Organization: Williams College, Williamstown MA
I've received several replies already concerning my previous question; thank you. For anyone who is curious, here's a summary (which is explained moderately well by the Mathematica text): Say we want to have a function f with a parameter x and options y and z. Options[f]={y->1,z->2} (*The 1 and the 2 are the default values*) f[x_,opts___]:=(x+y+z)/.{opts}/.Options[f] Then f[1] will return 4, f[2,y->3] will equal 7, f[3,y->0,z->0] will equal 3, and so forth. One thing I did notice: what I tried to do initially was to take a function with 10 parameters and assign it to another function with only two parameters and a lot of options. In this case, I needed to Hold[] the original function, apply the /.'s to it, and then use ReleaseHold. i.e. If F[x,y,z] was my original function, f[x_,opts___]:=ReleaseHold[ Hold[F[x,y,z]]/.{opts}/.Options[f] ] I suppose if F returned a simple symbolic formula-- i.e. x+y+z or some sort-- this wouldn't be necessary. However in my case F was a gradient search, so the Hold was necessary. Thanks again to everyone who answered my letter. / :@-) Scott \