Tips for writing correct, non trivial Mathematica Libraries

*To*: mathgroup at smc.vnet.net*Subject*: [mg124393] Tips for writing correct, non trivial Mathematica Libraries*From*: "l.i.b." <nehal.alum at gmail.com>*Date*: Wed, 18 Jan 2012 06:00:12 -0500 (EST)*Delivered-to*: l-mathgroup@mail-archive0.wolfram.com

hi -- I'm new-ish to Mathematica, but come from a fairly strong programming background. I find myself impressed with the elegance and utility of the approach -- but I get the feeling that there is quite a big learning curve going from using Mathematica to solve various math problems (arising from engineering and number theory or what have you) to creating non-trivial, robust libraries (e.g. perhaps creating a library for doing matrix differential calculus). At the same time, clearly there are large Mathematica projects out there (e.g. Wolfram|Alpha), so there must be a way to do this efficiently, I'm just not seeing it. I'm looking for comments and/or advise regarding my point of view. To give more context, here are some additional thoughts and examples: ** 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]) The built-in function GeometricMean properly complains when given this input, which leaves me wishing the source code for Mathematica functions were more easily available (e.g. like using the 'type' command in another system). Obviously Wolfram is quite proud of several of the complicated algorithms that they provide -- I don't need to see this -- but I really wish I could see how a 'good' mathematica developer implements GeometricMean robustly. ** 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. 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. Maybe it's possible to write carefully guarded Mathematica code, but it also seems likely that the only solution is to write unit tests. And maybe at the end of the day, a comparable Mathematica library might have a factor of 2 or 3 more unit tests than some procedural language, and despite this it might be faster to develop this way, I just just don't have enough experience to say. ** 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"? ---- Thanks!