Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #7970 > unrolled thread
| Started by | "l.i.b." <nehal.alum@gmail.com> |
|---|---|
| First post | 2012-01-18 11:02 +0000 |
| Last post | 2012-01-22 12:23 +0000 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.soft-sys.math.mathematica
Tips for writing correct, non trivial Mathematica Libraries "l.i.b." <nehal.alum@gmail.com> - 2012-01-18 11:02 +0000
Re: Tips for writing correct, non trivial Mathematica Libraries Richard Fateman <fateman@cs.berkeley.edu> - 2012-01-21 10:19 +0000
Re: Tips for writing correct, non trivial Mathematica Libraries David Bailey <dave@removedbailey.co.uk> - 2012-01-22 12:23 +0000
| From | "l.i.b." <nehal.alum@gmail.com> |
|---|---|
| Date | 2012-01-18 11:02 +0000 |
| Subject | Tips for writing correct, non trivial Mathematica Libraries |
| Message-ID | <jf68se$g99$1@smc.vnet.net> |
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!
[toc] | [next] | [standalone]
| From | Richard Fateman <fateman@cs.berkeley.edu> |
|---|---|
| Date | 2012-01-21 10:19 +0000 |
| Message-ID | <jfe3fo$9v6$1@smc.vnet.net> |
| In reply to | #7970 |
On 1/18/2012 3:02 AM, l.i.b. wrote: .... > > 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. Large parts of Mathematica (and presumably Alpha) are not written in the Mathematica language and therefore do not suffer from design issues of that language. They suffer from the design issues of other languages like C, C++, Java, whatever. As Andrzej pointed out, you can do a kind of type checking by looking at the Heads of arguments. This may remedy some of the problems you anticipate. Using Mathematica generally involves a leap of faith that any of the routines that you are using continue to do the same thing as when you tested them, and that they have not been "improved" so as to fail in your particular context. It is a common approach in computer algebra system design that if you have a program f(x) that computes something if x is of a special type, then if x is NOT of that type, "f(x)" is returned. That is, if an integral cannot be resolved to an integral-free expression, it is returned with an integral in it. Consequently, most programs that one would expect to operate on (say) numbers, must also do something reasonable if the arguments are somehow unresolved symbolic expressions. A good library will therefore respond politely to strange inputs. There are ways of specifying how N should work on your functions, if that is your main concern. NHoldAll, NumericFunction, and I suppose you can define N[X_YourFunction,d_digits] to define number evaluation strategy. RJF > > > >
[toc] | [prev] | [next] | [standalone]
| From | David Bailey <dave@removedbailey.co.uk> |
|---|---|
| Date | 2012-01-22 12:23 +0000 |
| Message-ID | <jfgv3c$pdv$1@smc.vnet.net> |
| In reply to | #7970 |
On 18/01/2012 11:02, 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])
>
A lot of Mathematica functions that usually operate on lists, are
defined more generally so as to apply to any compound object. While this
can be very useful, it can produce weird results such as this. Simply
restricting your function to operate only on lists is very easy:
geom[list_List] := Apply[Times, list]^(1/Length[list])
This has the useful feature that geom[Sqrt[x y]] will return without
change. This is generally valuable, because in general the argument
might resolve to a list later on, for example
geom[x] /. x->{1,2,3}
Numerical calculations often do weird things is they are applied to
non-numerical data, so it is often useful to require arguments of a
function to be numeric:
f[x_?NumericQ]:=Module[{x1=N[x]},
doSomething[x1]
];
The reason for applying N (where appropriate) to the input x, is that a
numerical algorithm may fail, or operate extremely slowly if forced to
operate on integer data. To see why that is true, try comparing:
Nest[#^2/(1 + #) &, 22, 4]
with
Nest[#^2/(1 + #) &, 22.0, 4]
Again, the function f will return unchanged if applied to other types of
arguments, which is exactly what is normally required.
In more complicated cases, it is possible to validate the arguments in
an arbitrary way using a function definition of the form:
f[args]:=Calculation/;test
Where test can perform any calculation on the arguments, and unless it
returns True, no evaluation will take place.
Another thing that is almost vital, is to place your library of
functions in its own package, so that only selected symbols are shared
with 'the outside world', and to define complicated functions using
Module or Block, to ensure that they don't use global variables.
You don't want to create a function that works in almost all cases,
except when someone already has a definition for the variable x! The
automatic colouring is very useful in this regard.
David Bailey
http://www.dbaileyconsultancy.co.uk
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web