Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #8068
| From | Murray Eisenberg <murray@math.umass.edu> |
|---|---|
| Newsgroups | comp.soft-sys.math.mathematica |
| Subject | Re: Tips for writing correct, non trivial Mathematica Libraries |
| Date | 2012-01-21 10:15 +0000 |
| Organization | Steven M. Christensen and Associates, Inc and MathTensor, Inc. |
| Message-ID | <jfe37j$9rj$1@smc.vnet.net> (permalink) |
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:
> 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)
>
>
>
>>
>>> 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@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
Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread
Re: Tips for writing correct, non trivial Mathematica Libraries Murray Eisenberg <murray@math.umass.edu> - 2012-01-21 10:15 +0000
csiph-web