A lot of numerical algorithms (integration, differentiation, interpolation, special functions, etc.) are available in scientific computation libraries like GSL. But I often see code with "hand-rolled" implementations of these functions. For small programs which are not necessarily intended for public distribution, is it common practice among computational scientists to just implement numerical algorithms yourself (by which I mean copying or transcribing from a website, Numerical Recipes, or similar) when you need them? If so, is there a particular reason to avoid linking to something like GSL, or is it just more "tradition" than anything else?

I ask because I'm a big fan of code reuse, which would suggest that I should try to use existing implementations when possible. But I'm curious whether there are reasons that principle is less valuable in scientific computation than in general programming.



Forgot to mention: I'm specifically asking about C and C++, as opposed to languages like Python where there is a clear benefit (speed of execution) to using a library.

I used to implement everything myself, but lately have begun using libraries much more.  I think there are several very important advantages of using a library, beyond just the issue of whether you have to write a routine yourself or not.  If you use a library, you get


Code that has been tested by hundreds/thousands/more users
Code that will continue to be updated and improved in the future, without any work on your part
Optimized code that is more efficient and perhaps more scalable than what you would write in a first attempt
Depending on the library, by establishing an interface to it in your code you may get access to many algorithms that you currently don't use but may want to in the future


In the last bullet point above, I'm thinking of large libraries like Trilinos or PETSc.  I can reinforce this with a couple of concrete personal examples in development of PyClaw.  Although it would have been straightforward to parallelize Clawpack with MPI calls, we chose to use PETSc.  This allowed us to limit the paralle code in the package to less than 300 lines of Python, but even better, by putting our data in PETSc's format we gained immediate access to PETSc's implicit solvers, enabling current work on an implicit solver in PyClaw.  As a second example, PyClaw initially included hand-code fifth-order WENO reconstruction, but we eventually decided to rely on the PyWENO package for this.  This was a huge gain, since PyWENO can automatically generate WENO routines of any order in several languages.

Finally, if you use libraries, you can contribute back by developing improvements or finding bugs, which will benefit many other people, whereas debugging or improving your own code only benefits you.
