Numerical Recipes: The Art of Scientific Computing
numerical.recipes
numerical.recipes
Despite the availability of the source code, already the usage of NR programs itself is seriously restricted, even after license purchasing. Distribution of the software is not allowed (neither in the original of modified versions), which prevents proper verification and advance of computations. Adaptations or copies of the source code (extracted from the software or from the book) constitute a copyright infringement. Academic citations alone do not avoid such infringement. Rather, specific exceptions should be asked (and may not be granted).
Scientific codes widely used in physics often have comments such as 'this is taken from NR' (clearly violating the license/copyright). It shows that what most of the scientific community expects is everyone to be allowed to use/modify/distribute scientific codes, given proprer attribution. In the same way as everyone would feel free to write down and modify the standard model Lagrangian in their papers by citing appropriate references, and to publish their results without any of the above restrictions.
Definitely, recommend you stay as far as way from this as you can. It's my own 1-star review on Amazon.
And they're code doesn't exactly conform to best practices anyway, so if it's small (e.g. a random number generator), then implementing it yourself is a good idea. And if it's big (e.g. nonlinear optimization), you should use some other off-the-shelf implementation anyway, and just use NR to understand the theory behind it, strengths/weaknesses, etc.
From the book:
Copyright does not protect ideas, but only the expression of those ideas in a particular form. In the case of a computer program, the ideas consist of the program’s methodology and algorithm, including the necessary sequence of steps adopted by the programmer. The expression of those ideas is the program source code (particularly any arbitrary or stylistic choices embodied in it), its derived object code, and any other derivative works.
If you analyze the ideas contained in a program, and then express those ideas in your own completely different implementation, then that new program implementation belongs to you.
It is also not that clear all of the algorithms are fully original clean-room implementations written by the authors --- the techniques in the book are well-known and previous implementations do exist, written by other academics. As such, claiming copyright on some the codes is not morally defensible, even if legal.
It's just better to not read the book at all. Since you cannot use the codes as is, if you want to learn about the theory behind it, it's better to pick a different book.
In my experience and opinion, Numerical Recipes hits a pretty unique spot by explaining enough about the ideas behind and how the algorithms work, that one can start implementing them (with or without reading the code) but not getting theory-heavy and full of equations and proofs, like a real numerical analysis textbook.
Kahaner, Moler, Nash: Numerical Methods and Software is a similar book, maybe even better, but it's from 1988 and never updated, and it's not nearly as widely known. Also it covers a smaller range of topics.
For anything to be used in the real world, in production, you're better off taking code from somewhere else.
What alternative to use depends on the topic/algorithm in question. But NR should just be a starting point, not what you actually use.
First, I think the publishers started to see that there was money to be had as the copyright/left wars took off and the book left academia (where you weren't going to get any money suing them anyway).
Second, as the web grew (late 90s) "programmers" started just copy-pasting "code"/html from wherever they liked without understanding or attribution, and that is something that still exists today... although I hope that more people recognize it as a problem to be stamped out.
So now, NR is somewhat of a relic that oldsters remember as a useful tool, and a new generation see as another symbol of a problem they're trying to get rid of. It's really both.
"It is naïve to hope that every computational problem can be solved by a
simple procedure that can be described in a few pages of chatty prose, and
using a page or two of Fortran or C code. Today's ambitions for
correctness, accuracy, precision, stability, "robustness", efficiency,
etc. demand sophisticated codes developed by experts with deep
understanding of their disciplines. We have long ago outgrown the
capabilities of the simplistic approaches of 30 years ago."
===[1] http://www.uwyo.edu/buerkle/misc/wnotnr.html
[2] https://web.archive.org/web/20021015200910/http://math.jpl.n...
TL;DR: Perfect is the enemy of the good. Unless your project actually demands the best possible algorithm, get your answer faster from NR and be on your way.
The real problem with this book is it is sometimes the first and only introduction to applied mathematics. I first saw it in 2005 as an undergraduate physics major. Later, when I mentioned to my applied math PhD advisor, he was appalled. I was at first surprised by his response, but after finishing my PhD program and spending a few years in the field, I agree with him.
Unfortunately, applied mathematics has not produced a competing generalist manual to the breadth of our field. In part, this is because the field is large and complex; to even understand the basics of modern algorithms requires a level of mathematical sophistication not taught in undergraduate math (see, e.g., GMRES). There are good books that cover specific topics in Numerical Recipes, e.g., Trefethen's Spectral Methods in Matlab and Trefethen and Bau's Numerical Linear Algebra, but they (rightfully) don't provide a pithy answers as to which algorithm to use. These books showcase different algorithms and illustrate the situations where they are or are not appropriate. This latter part requires significant significant sophistication to understand. Implementing an algorithm, easy. Choosing the right algorithm; aye, there's the rub. And that is where Numerical Recipes fails.
I'm not sure how to incentivize peer review, but that seems like the right solution: to not only put out code that's openly reviewable, but where you create some incentive for people to review it. Maybe that's the point of paid third-party audits, or rewards for identifying bugs, but maybe there's some other options that could be encouraged more?
Best of it: I was calling the C module from COBOL...
- It's a collection of numerical algorithms and their implementations, not a systematic study of it. Meaning the style is "if you wish to do this, you use that" and so on.
- It developed a bad reputation in the 1990s for the authors disallowing use of the implementations in the book without some kind of contract or permission. I'm not sure if the authors still do that but if you use any of the implementations from the book, you would always be concerned.
A book (not the only one but one that I learned from) that solves both problems is Scientific Computing by Heath (https://amzn.com/0072399104). (Well it doesn't provide implementations in any programming language, but if you understand the algorithms you should be able to implement them yourself).
Finally, I would like to say that numerical-algorithms is the bigger of the two main areas of algorithms (the other being, what I call, combinatorial algorithms), and not only that, it's extremely relevant today for computer scientists in a world where the trend is to go from traditional programming to data science and machine learning. (I'm of the belief that CS departments and book-authors of the world have fooled their students for decades by teaching them only combinatorial algorithms (CLRS, Knuth, Skiena, that sort of stuff), and calling the class "Algorithms". This is a big reason CS students today have difficulty getting into machine learning theory, and CG theory, unless they go to grad school).
Many years ago, I tried using NR for optimization problems. The functions were dog slow, because they tended to set aside space at the start and free it up at the end, and that burns up a lot of time for functions that are called frequently.
The alternative, used in the much-faster NAG and IMSL systems, was for the user to set aside memory before calling any functions, and to hand functions "working memory". This scheme demanded more of the user (yielding bad problems when you gave insufficient memory!) but it delivered much better performance.
For my particular problem, a particular NAG routine (name now forgotten) was almost magical in finding solutions in a relatively small number of iterations. As I recall, the NAG code was closed, so I never knew how the authors got such good results. I imagine the secret was in the realm of clever numerical analysis, as opposed to clever coding.
Relative to other systems, NR had the advantage that you could see the code. Quite often, the code provided examples of how not to do things. But at least you could see the code.
As for matters of licensing, I think they are covered very well in other comments in this thread.
The best way to use NR, IME, is as a reference that guides you through the huge body of literature to arrive at some specific method recommendations, like a more helpful combination of Wikipedia + Stack Overflow. Use those references to help you understand the algorithm in original form, and where the NR authors have made changes to the algorithm or parameters, it's good to understand why.
I wholeheartedly agree with the parent's description of their code as "how not to do things". This I think is less true from a strictly numerics perspective than it is true of the general structure of their code, naming, interfaces, etc. I would throw much of those aspects out if implementing their methods.
I had a numerics professor on my grad committee ask for NR to be removed as a reference in a journal paper we were preparing, on the basis that it is "not taken seriously" among the numerics community. He wanted the source references cited, i.e., the original books and papers in which the method I was using were detailed. However, NR tied all those pieces together in a very coherent and approachable way, and gave helpful recommendations on implementation details. People steeped in the field don't appreciate the encyclopedic nature of NR, which is not to be lightly dismissed for engineers and practitioners. I have not seen a numerics prof-blessed text in the vein of NR that gets the right level of depth for practitioners and presents enough opinion/judgment on state-of-the-art methods to actually be of value. If such a thing exists, please enlighten us!
In terms of code implementations, I also wholeheartedly agree that if you can find a high-quality vetted library in your language of choice whose license terms agree with your code/intent, use them! That said, there are definitely areas where NR presents better methods than what is in some of these libraries (ODE solvers are one such area I'm familiar with).
Lastly, note that NR has evolved a fair bit over the years. Don't base too much of your opinion on prior versions.
Last night, I was looking for something to read on my bookshelf and the now my copy caught my eye. But before reading it I was curious about it as a book...i.e. was it rare and so I googled it up, came across the link, and posted it here since its history was interesting. And then I shut off the computer and started reading.
There's a lot of criticism here about the licensing model. While I agree that GNU or MIT might be more utopian, I think the premises of the criticism are largely premised in historical counter-factuals.
The book was published in 1985-1986 (see the Wikipedia article) [2]. There was no GNU license, no BSD, [1] and as best I can tell from a few minutes googling, no MIT license either. It was the time of the Unix Wars that led to our contemporary licensing landscape. The big choice was between public domain and proprietary licensing (and the practices of proprietary licensing were typically a long way from where we are today).
All this is to say that the code in Numerical Recipes was an improvement in the landscape at the time it was released. A person could look at the source code for the price of a book versus the cost of a commercial license or dealing with a black box. A programmer could kick the tires of Numerical Recipes before buying. A paid license also provided an incentive to improve the product and that produced code in Fortran, Pascal, C, C++ and a host of other languages...all with the ability to kick the tires.
It is also worth pointing out that the code in many many books is not released under an open source license. The typical case is for code in books to be published with strong copyright claims and all rights reserved. Even today, the availability of an explicit software license is an improvement over a number of currently published programming books.
Again, there are utopias I'd rather live in, but the licensing of Numerical Recipes is better than most commercial operating systems.
[1]: https://en.wikipedia.org/wiki/History_of_free_and_open-sourc...
'Numerical Recipes in Java™!'
i was/am under the impression that it was either FORTRAN or C++ in HPC/scientific computing arena :)edit-001 : slight clarification.
This shows us that the war between 1-based and 0-based indexing was not decided for a long time.
http://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EW...
This is only the case for 1..N, not for I..J, when 'I' is not one. In this general case, the number of elements is 'J-I' if using zero-based numbering and exclusive upper bounds, which is more convenient. Zero-base also simplifies index arithmetic with modulo operations, such as when you are mapping between different index spaces.
Here's some more grist to the mill, currently on the HN front page:
I agree (based on Dijkstra's arguments, I'm not smart enough to come up with my own) that 0-based indexing is the more logical, general choice, but 1-based indexing is more convenient for about 99% of the code written in Fortran, Matlab or Julia.
No, it's not. There is no universally preferred indexing in mathematics. The indexing scheme that is "natural" changes with the particular application.
There are of course instances when 0-based indexing makes more sense [1], [2]. But, there are cases when starting at 1 is more natural, as well [3]. Moreover, there are instances where starting with negative indices is more natural, say with the coefficients of the truncated Fourier series, or with the weights of a finite difference operator or of a cross-correlation operator.
Fortran supports all these conventions, which allows for a more direct translation of the mathematics to code.
[1]: https://en.wikipedia.org/wiki/Geometric_series#Formula
[2]: https://en.wikipedia.org/wiki/Taylor_series#Definition
[3]: https://en.wikipedia.org/wiki/Harmonic_series_(mathematics)
You could make the same argument for octal literals over the scary hex numbers with letters mixed in. 1-based indexing was invented by mathematicians using conventional notation without fully appreciating the problems it causes in computing systems. There is no reason to continue the insanity.
Julia, as far as I understand, consciously mimicked Matlab there, to facilitate transition.
- Ed Post, Real Programmers Don't Use Pascal
This doesn't fit my recollection of having the C edition with a foreword that described how hard and error prone it was to convert everything from 1-based to 0-based. Maybe my memory plays tricks on me. I'm sure I had the C edition and not the C++ edition and this was before 2000.
[1] http://apps.nrbook.com/c/index.html
[2] http://numerical.recipes/forum/showthread.php?t=65&highlight...
I have worked with customers that are heavy users of either IMSL or Alea:
http://www.roguewave.com/products-services/imsl-numerical-li...
http://quantalea.azurewebsites.net/
Mostly used on their in-house desktop solutions for data manipulation.