The Math Forum

Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Math Forum » Discussions » Inactive » comp.soft-sys.math.mathematica

Topic: Tips for writing correct, non trivial Mathematica Libraries
Replies: 4   Last Post: Jan 20, 2012 1:58 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Andrzej Kozlowski

Posts: 2,112
Registered: 1/29/05
Re: Tips for writing correct, non trivial Mathematica Libraries
Posted: Jan 20, 2012 1:58 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

On 19 Jan 2012, at 14:14, Nehal Patel wrote:

> On Jan 19, 2012, at 6:39 AM, Andrzej Kozlowski wrote:

>> On 19 Jan 2012, at 11:12, Bill Rowe wrote:

>>>> 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

>> Clearly he sees what actually is a powerful feature of Mathematica as

a "bad thing". The feature is that a function whose arguments are not
restricted by pattern maching can be put to a more general use. I think
it's great and not "bad". If you want to restrict the function to work
on lists you can do this:
> Hi -- thanks for your comment. Actually I think the pattern matching

is the coolest thing. my point is that when combined with how
Mathematica treats + (and in general, Flat and Orderless operators) it
becomes a lot harder to reason about whether a function handles all
edge cases correctly. So for instance nobody would expect the geometric
mean of x + y to be Sqrt[x y], it should be (x + y)^1 = x+ y

No, it shouldn't be. There is no such thing as a mean of a number or an
algebraic expression. What you would expect is

In[7]:= geom[{x + y}]

Out[7]= x + y

which is indeed what you get.

> Separately, GeometricMean does do something reasonable for

GeometricMean[x + y] (give it a try), and I wish it were easier to see
what syntax it uses.

It is very easy to do that, it in fact:


geom::nonlist =
"The argument `1` is neither a non-empty vector nor a non-empty

geom[ls_] /;If[Head[ls] === List, True, Message[geom::nonlist, x];
False] :=
Apply[Times, ls]^(1/Length[ls])

geom[{x + y}]

x + y

geom[x + y]

The argument x is neither a non-empty vector nor a non-empty matrix

geom[x + y]

Andrzej Kozlowski

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2017. All Rights Reserved.