Re: Tips for writing correct, non trivial Mathematica Libraries

*To*: mathgroup at smc.vnet.net*Subject*: [mg124428] Re: Tips for writing correct, non trivial Mathematica Libraries*From*: Bill Rowe <readnews at sbcglobal.net>*Date*: Thu, 19 Jan 2012 05:12:43 -0500 (EST)*Delivered-to*: l-mathgroup@mail-archive0.wolfram.com

On 1/18/12 at 6:00 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 built-in function GeometricMean properly complains when given >this input, I don't see a problem with the built in function. That is: In[6]:= GeometricMean[{x, y}] Out[6]= Sqrt[x*y] >** In general I get the feeling the nature of Mathematica >programming (pattern matching, transformations, etc) make it hard to >reason about the correctness of code. So while these aspects are >seem super useful for manipulating data, or building up >expressions/calculations, and interacting in general, it is hard to >have solid trust when using mathematica code. I think this comes with experience with Mathematica. >For instance it feels likely that given mathematica source code >written by a 'normal' person, a reasonably clever person could >find a >way to spoof it -- not just make it run really slow, but actually >produce the wrong answer. This is no more true of Mathematica code than code written in any other language. Well written code provides correct results and will not generate incorrect results. If what you are saying is pattern matching and replacement rules can be used by someone to rewrite code to produce an incorrect result then I agree this is possible. But, I emphasize that is *re-writing* the code not getting a bad result from well written code. >** Another difficulty I see that it is not easy to understand how to >best leverage the other parts of Mathematica when writing your own >libraries. For instance suppose I have my matrix differential >algebra library, MatDiffLib, and I'm happy with how it works on a >stand alone basis. Now suppose I want my routines to play >intelligently with // N (using specialized numerical algorithms) or >work well with Interval arithmetic -- of course I could cross my >fingers and just trust that things would work, but it's just not >clear from the documentation how to get the inner details needed to >solve problems would things not work. >**Maybe another way to ask my question is , "Are there any good open >source Mathematica libraries that could serve as a template for how >to practically develop complicated Mathematica code"? In terms of Mathematica, I interpret libraries as being Mathematica packages. With that understanding there are a great many packages available on the Internet. Start with Wolfram's site. Now, as to whether these are good packages or not, like any other open source code some will be good some will not be good. 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.

**Follow-Ups**:**Re: Tips for writing correct, non trivial Mathematica Libraries***From:*Andrzej Kozlowski <akoz@mimuw.edu.pl>