<p>Are there any good online resources for how to create, maintain and think about writing test routines for numerical analysis code?</p>

<p>One of the limitations I can see for something like testing matrix multiplication is that the obvious tests (like having one matrix being the identity) may not fully test the functionality of the code.</p>

<p>Also, there is the fact that you are usually dealing with large data structures as well. Does anyone have some good ideas about ways to approach this, or have pointers to good places to look?</p>

[{'answer_id': 16453, 'author': 'jacobko', 'author_id': 1318, 'author_profile': 'https://Stackoverflow.com/users/1318', 'pm_score': 1, 'selected': False, 'text': '<p>Check out a book by <a href="http://en.wikipedia.org/wiki/David_Gries" rel="nofollow noreferrer">David Gries</a> called <a href="https://rads.stackoverflow.com/amzn/click/com/0387964800" rel="nofollow noreferrer" rel="nofollow noreferrer">The Science of Programming</a>. It\'s about proving the correctness of programs. If you want to be sure that your programs are correct (to the point of proving their correctness), this book is a good place to start.</p>\n\n<p>Probably not exactly what you\'re looking for, but it\'s the computer science answer to a software engineering question.</p>\n'}, {'answer_id': 129435, 'author': 'Bob Cross', 'author_id': 5812, 'author_profile': 'https://Stackoverflow.com/users/5812', 'pm_score': 3, 'selected': True, 'text': '<p>It sounds as if you need to think about testing in at least two different ways:</p>\n\n<ol>\n<li><p>Some numerical methods allow for some meta-thinking.  For example, invertible operations allow you to set up test cases to see if the result is within acceptable error bounds of the original.  For example, matrix <em>M-inverse</em> times the matrix <em>M</em> * random vector <em>V</em> should result in <em>V</em> again, to within some acceptable measure of error.<br>\nObviously, this example exercises matrix inverse, matrix multiplication and matrix-vector multiplication.  I like chains like these because you can generate quite a lot of random test cases and get statistical coverage that would be a slog to have to write by hand.  They don\'t exercise single operations in isolation, though.</p></li>\n<li><p>Some numerical methods have a closed-form expression of their error.  If you can set up a situation with a known solution, you can then compare the difference between the solution and the calculated result, looking for a difference that exceeds these known bounds.</p></li>\n</ol>\n\n<p>Fundamentally, this question illustrates the problem that testing complex methods well requires quite a lot of domain knowledge.  Specific references would require a little more specific information about what you\'re testing.  I\'d definitely recommend that you at least have <a href="http://steve-yegge.blogspot.com/2006/03/math-for-programmers.html" rel="nofollow noreferrer">Steve Yegge\'s recommended book list</a> on hand.</p>\n'}, {'answer_id': 215501, 'author': 'John D. Cook', 'author_id': 25188, 'author_profile': 'https://Stackoverflow.com/users/25188', 'pm_score': 2, 'selected': False, 'text': "<p>If you're going to be doing matrix calculations, use LAPACK. This is very well-tested code.  Very smart people have been working on it for decades.  They've thought deeply about issues that the uninitiated would never think about. </p>\n\n<p>In general, I'd recommend two kinds of testing: systematic and random. By systematic I mean exploring edge cases etc.  It helps if you can read the source code.  Often algorithms have branch points: calculate this way for numbers in this range, this other way for numbers in another range, etc.  Test values close to the branch points on either side because that's where approximation error is often greatest.  </p>\n\n<p>Random input values are important too.  If you rationally pick all the test cases, you may systematically avoid something that you don't realize is a problem. Sometimes you can make good use of random input values even if you don't have the exact values to test against.  For example, if you have code to calculate a function and its inverse, you can generate 1000 random values and see whether applying the function and its inverse put you back close to where you started.  </p>\n"}]
['https://Stackoverflow.com/questions/16434', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/277/']