Monday, January 31, 2011

Math and Programming


Solving certain types of differential equations or equations in the complex plain requires pages upon pages of workings. These consist of multiple substitutions and dozens of algebraic manipulations. Calculations frequently branch off into various independent calculation paths, some of which end up being dead ends while others need to be tracked in parallel and recombined to get the final solution.

One can draw many parallels between solving math problems and writing software. When writing software you create levels of abstraction by using classes and performing data transformations. You also perform tests to decide which algorithms to use and break complex problems into smaller, more manageable steps which need to be recombined in the end.
Since the problem spaces have many similarities, I think methods of solution can have similarities as well.
Having readable code is of outmost importance in software development. The key to having readable code is having simple methods which define the algorithm without exposing too many implementation details. Similarly when solving a math problem you should have a single page which starts off with statement of the problem, names steps that need to be performed, along with their results and the final result at the end. This improves readability of the entire solution and can help spot inconsistency in results.
Building software requires testing and lots of it. In fact some of the most effective software developers test their programs at the unit level. Some even write tests before they write any code. This can also be applied to math. When you find a solution to a differential equation, you should make sure that it actually solves your equation. But that is not enough, because if after hours of working on a solution you made a mistake in some algebraic transformation half way through, you will end up spending many more hours looking for your mistake. In addition to validating the final solution, make sure to define a test for the result of each step in your calculation and validate it before recombining with other results.