Help defining options; selecting particular version of a function;
- To: mathgroup at smc.vnet.net
- Subject: [mg110447] Help defining options; selecting particular version of a function;
- From: telefunkenvf14 <rgorka at gmail.com>
- Date: Sat, 19 Jun 2010 07:46:12 -0400 (EDT)
Dear MathGroup: I'm working on building up a package of functions and I need some guidance. After several prior attempts, I'm learning the value in minimizing the number of named functions and maximizing what each can do. (BTW, is there a more technical term for this?--my goal, in part, is to demonstrate how to teach others to build a package.) Please consider the following function. It's the solution to a profit maximization problem, and this particular case assumes Cobb-Douglas technology. As you can see, I have set Profit[] up so that I can pass it either a list of wages 'w' and shares 's', or have it generate the function with 'n' number of inputs, with wages indicated by the symbolic 'w', etc. Profit[w_List,s_List,p_Symbol:p]:= Module[{sumShares}, sumShares=Plus@@s; ((1-sumShares)*p^(1/(1-sumShares)))*Inner[Power,w/s,-(s/(1- sumShares)),Times] ] Profit[n_?IntegerQ/;n>0,w_Symbol:w,s_Symbol:s,p_Symbol:p]:= Module[{sumShares,wages,shares}, wages=Table[Subscript[w, i],{i,1,n}]; shares=Table[Subscript[s, i],{i,1,n}]; sumShares=Plus@@shares; (1-sumShares)*p^(1/(1-sumShares))*Inner[Power,wages/shares,-(shares/ (1-sumShares)),Times] ] At this point, what I'd like to do is build on this framework by including the appropriate profit functions for other types of technology, such as 'CES', 'Leontief', etc. My question: What is the best way to go about this? (my brainstorm of possible ways to do this are below) 1. Profit[w_List,s_List,p_Symbol:p,tech_:"CobbDouglas"] and then use If[] or Which[] to switch to "CES" or "Leontief". (This seems sloppy to me, although it is similar to (3), below. Isn't it?) 2. I suspect that Condition[] might be another way to go, although I have little practice using /; in the body of a function. Someone would really have to hold my hand... 3. ClearAll[Profit] Options[Profit] = {Technology -> "CobbDouglas", Technology -> "CES", Technology -> "Leontief"}; Profit[x_, OptionsPattern[]] := Which[ OptionValue[Technology] == "CobbDouglas", CobbDouglasVersion[x], OptionValue[Technology] == "CES", CESVersion[x], OptionValue[Technology] == "Leontief", LeontiefVersion[x] ] So then I could call: Profit[variables, Technology -> "CobbDouglas"] Profit[go, Technology -> "CES"] Profit[here, Technology -> "Leontief"] This last approach seems to be the most appropriate. Am I missing anything? Pitfalls to avoid? I really don't want to end up starting this over again... :) -RG