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:

>
>>>** 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:= geom[x_List] := (Times @@ x)^(1/Length[x])

In:= geom[x__] := Times[x]^(1/Length[{x}])

In:= geom[{x, y, z}]

Out= (x*y*z)^(1/3)

In:= geom[x, y, z]

Out= (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.

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.

```

• Prev by Date: Re: Slow plotting of reflected parametric "butterflies"
• Next by Date: Re: MatrixForm odd behaviour
• Previous by thread: Re: Tips for writing correct, non trivial Mathematica Libraries
• Next by thread: Re: Tips for writing correct, non trivial Mathematica Libraries
{"ft_lang_label":"__label__en","ft_lang_prob":0.8551904,"math_prob":0.7166694,"size":3060,"snap":"2022-27-2022-33","text_gpt3_token_len":786,"char_repetition_ratio":0.10340314,"word_repetition_ratio":0.024742268,"special_character_ratio":0.26862746,"punctuation_ratio":0.12155592,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.987672,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14],"im_url_duplicate_count":[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-08-20T02:22:40Z\",\"WARC-Record-ID\":\"<urn:uuid:b88cfc36-6a8e-4e3b-8b1a-f3fd1b86874d>\",\"Content-Length\":\"46760\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:78884d0b-baf4-4a02-b420-c0814e74513d>\",\"WARC-Concurrent-To\":\"<urn:uuid:3125ac9c-6835-44c9-84e3-01911db17255>\",\"WARC-IP-Address\":\"140.177.205.73\",\"WARC-Target-URI\":\"http://forums.wolfram.com/mathgroup/archive/2012/Jan/msg00572.html\",\"WARC-Payload-Digest\":\"sha1:HZVZZJJCBOJ3JCEKDOHBLBLHN6EOSRZA\",\"WARC-Block-Digest\":\"sha1:Z5WUWTMXCUUYNFBRYV6RAZOYXKTBAIKB\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-33/CC-MAIN-2022-33_segments_1659882573876.92_warc_CC-MAIN-20220820012448-20220820042448-00066.warc.gz\"}"}