Re: Tips for writing correct, non trivial Mathematica Libraries

• To: mathgroup at smc.vnet.net
• Subject: [mg124476] Re: Tips for writing correct, non trivial Mathematica Libraries
• From: Murray Eisenberg <murray at math.umass.edu>
• Date: Sat, 21 Jan 2012 05:13:50 -0500 (EST)
• Delivered-to: l-mathgroup@mail-archive0.wolfram.com

```Why _should_ a "new user" expect geom[s] to return s when s is a scalar
rather than a list?  After all, a lion is not the same thing as a lion
in a cage!

Whether a function whose natural domain consists of lists should coerce
a scalar into a one-element list is purely a matter of convenience.

That the built-in GeometricMean does do that whereas the documentation's
example geom does not is perhaps just a sloppy oversight in constructing
that example.

On 1/20/12 1:46 AM, l.i.b. wrote:
>
>>> ** 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)
>
>
>
>>
>>> 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.

--
Murray Eisenberg                     murray at math.umass.edu
Mathematics & Statistics Dept.
Lederle Graduate Research Tower      phone 413 549-1020 (H)
University of Massachusetts                413 545-2859 (W)
710 North Pleasant Street            fax   413 545-1801
Amherst, MA 01003-9305

```

• Prev by Date: General advice on making slides that show examples
• Next by Date: Re: Tips for writing correct, non trivial Mathematica Libraries
• Previous by thread: Re: Tips for writing correct, non trivial Mathematica Libraries
• Next by thread: Re: Tips for writing correct, non trivial Mathematica Libraries