Re: Tips for writing correct, non trivial Mathematica Libraries

*To*: mathgroup at smc.vnet.net*Subject*: [mg124441] Re: Tips for writing correct, non trivial Mathematica Libraries*From*: Andrzej Kozlowski <akoz at mimuw.edu.pl>*Date*: Fri, 20 Jan 2012 01:46:29 -0500 (EST)*Delivered-to*: l-mathgroup@mail-archive0.wolfram.com*References*: <201201191012.FAA03722@smc.vnet.net>

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: geom[list_List] := Apply[Times, list]^(1/Length[list]) and now geom[list_List] := Apply[Times, list]^(1/Length[list]) geom[x + y] geom[x + y] and if you really wish you can restrict it only to vectors you can do geom[list_?VectorQ] := Apply[Times, list]^(1/Length[list]) and so on. This is much more powerful and flexible than the "other" approach. Andrzej Kozlowski

**References**:**Re: Tips for writing correct, non trivial Mathematica Libraries***From:*Bill Rowe <readnews@sbcglobal.net>