Re: Tips for writing correct, non trivial Mathematica Libraries
- To: mathgroup at smc.vnet.net
- Subject: [mg124489] Re: Tips for writing correct, non trivial Mathematica Libraries
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Sat, 21 Jan 2012 05:18:21 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
On 1/20/12 at 1:46 AM, nehal.alum at gmail.com (l.i.b.) wrote: >Thanks for your comments. My followups are inlined below: > >>>** Take for example the following: (Taken from the mathematica 8 >>>virtual book section "Applying Functions to Lists and Other >>>Expressions") geom[list_] := Apply[Times, list]^(1/Length[list]) >>>So, this does a bad thing for geom[ x+ y] (returns (Sqrt[x y]) >>What were you expecting here? This looks correct to me >the snippet for 'geom' is taken for the mathematica tutorial, and is >meant to show the user how to define a function that implements the >geometric mean. so for instance, geom[{x,y,z}] = (x y z >)^(1/3). But geom[x+y] treats the expression as a list and >returns Sqrt[x y], >which is not what a new user would expect ( a better answer would be >x+y) If you want geom to return the geometric mean when either a list or when given multiple arguments separated by commas try this: In[8]:= geom[x_List] := (Times @@ x)^(1/Length[x]) In[9]:= geom[x__] := Times[x]^(1/Length[{x}]) In[10]:= geom[{x, y, z}] Out[10]= (x*y*z)^(1/3) In[11]:= geom[x, y, z] Out[11]= (x*y*z)^(1/3) >>>The built-in function GeometricMean properly complains when given >>>this input, >I'm saying GeometricMean does not get fooled by GeometricMean[x+y], >which means the author thought of how to protect it from doing the >wrong thing -- and it would be nice to see exactly what syntax they >used. Syntax for any built-in function is generally available by doing ?functionName That is doing ?GeometricMean returns something that immediately tells you the intended argument is a list. More information can be found in the Document Center. On the Mac, selecting name and doing cmd-shift-f brings up the document page for name. Or on Windows use F1. >>If you want to look at examples of well written Mathematica >>packages, a good place to start is to look at the packages that >>ship with Mathematica. >Actually that's exactly what I've started to do, once I nailed down >the location of the bundled packages. Though I've yet to find the >definition for GeometricMean in the install directory -- presumable >it is a native command. It is a built-in function. For anything defined in Mathematica doing Context[name] will give the context the definition for name appears in. Context[GeometricMean] returns System` telling you it is a built-in symbol. Further, paying attention to syntax coloring will tell you a lot. That is by default the color will change from blue (an undefined symbol) to black (a defined symbol) when the final letter of the symbol name is typed. Anytime this happens you know what you typed must be defined. So, if you haven't defined it yourself or loaded a package, it must be a built-in symbol.