When it comes to advanced numerical functions, my advice is clear: Look at 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 to 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 Subprograms (BLAS), wavelets, eigenvalues, partial differential equations—you get the picture.
This came up thanks to an e-mail 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 to let your programmers do all the work?
Advanced numerical algorithms are very complex 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, and the code has been reviewed by many individuals, including mathematicians. Generally speaking, you can be assured 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 and also the 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 Assembly 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 have 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 pocket calculator.
Alan Zeichick is editorial director of SD Times. Read his blog at ztrek.blogspot.com.