When it comes to writing code with advanced numerical functions, my advice is clear: Use libraries. Don’t roll your own algorithms.
Generally speaking, I’m a fan of modular code reuse, especially for complex functions like UI controls, database access drivers, PDF generation or managing images. Most of the time, it’s a good idea to find open-source components that will get your job done, or license commercial reusable components. Sometimes, though, it makes more sense to write your own functionality.
Numerical libraries are the exception. Unless you are in the math business, resist the temptation to write your own Fast Fourier Transform (FFT) functions, random number generators, basic linear algebra subprogram (BLAS), wavelets, Eigenvalues, partial differential equations – you get the picture.
This came up thanks to an email from an SD Times subscriber:
I’m an IT consultant in the software arena and would like to ask you a question on buying mathematical algorithms vs. programming them yourself. Especially for complicated mathematical subroutines, is it cost-effective to subscribe to an algorithm library, or let your programmers do all the work?
Advanced numerical algorithms are very hard to get right. Simply writing the basic code is complicated – and so is the testing of that code, to make sure that each routine delivers consistently correct results in all cases – including across different processors, hardware architectures, programming languages, compilers, runtimes, standard libraries, and so-on.
Incredible amounts of work have gone into designing, coding and testing most high-end numerical libraries. What’s more, the code has been reviewed by many individuals, including both practical and theoretical mathematicians. Generally speaking, you can be confident that the math is correct.
Beyond consistent correctness, there’s also efficiency. You’re not running that FFT or BLAS routine once; it’s being executed hundreds, thousands, millions of times, perhaps, during the execution of your program. Efficiency matters, including both raw speed but also use of resources like memory and threads.
An advantage of most numerical libraries is the tuning that goes into the code – a lot of hand-crafted C or Assembler code, in some cases. In other words, it’s fast. Increasingly, those libraries are also tuned for multicore processing. You could never justify spending the resources to do this yourself.
I had had experience with many numerical libraries, ranging from Intel’s Math Kernel Library to AMD’s Core Math Library to the IMSL Numerical Libraries to the NAG Numerical Components. They are all good, all recommended.
The tradeoff is that many numerical libraries are costly. If you need math, though, licensing one of the libraries is a bargain – and you can do the ROI calculations on a four-function pocket calculator.