It is easy for programmers to over-abstract, creating versatile systems that only ever get used once. It is comforting to see the cases where something actually does find more than one use. I recently had occasion to use MatLat again, so it’s something I’ll be keeping an eye on.

Second Times the Charm

I was attempting to puzzle out the formula behind an energy monitoring chip. The datasheet is written on the assumption that you will be calibrating based on an output clock frequency. The problem is, we aren’t, and pretty much every formula works derives from the clock frequency.

I started writing down interesting equations and lists of terms to find definitions for. Before long I was drawing connections between equations and realized that my equation graphing system would be best available tool to tackle the problem.

I was reasonably correct; I’ve worked the calibration equations back to primitive values without reference to the clock frequency (and, surprisingly, without reference to voltage)

Of course, I made some changes along the way. I pulled the operator out of the box; I’m not completely happy with this arrangement, but I kept reading it wrong when it was next to the result. I’m also planning to rename it MathViz, since it’s based on GraphViz. The major new feature, however is unit tracking.


I found that I kept going back to paper and pencil to check my units or see if combining some values got something sensible. Which of course got me wondering if I could automate the process, making the unit handling less error prone, and providing a check on the numbers I was throwing around as well.

Syntactically Ruby makes it pretty easy, provided one is willing to open up the system Numeric class and make globally visible changes to every number. Thankfully, I’m not expecting to have to integrate with anything else, thought I might look at a way the extension optional at some point. The units also allow me to dump the const eyesore. Now, instead of having something like i = const 10, it can read i = 10.A or 3600.s.per.h.

Tracking units worked out fairly well also. I doubt I’m the first person to use ‘hash of powers’ as it’s a very nice representation. Multiplication and division reduce to adding or subtracting powers, (with a normalization step to clean up the zeros) and most other operations to ensuring that the units match.

There was also minimal complication to the existing system – unit tracking is done by sending the same operator to both the primary data and the units, which handle them in their own particular way. There is some dependence since the unit methods return Term objects, but it shouldn’t take much to make the units a completely generic system. But perhaps I should wait until I have second use for it.

Posted Monday, December 21st, 2009 under Devlog.

Comments are closed.