in reply to Perl and maths

Math::Algebra::Symbols helps with these kinds of algebra tasks. It can also solve equations (even quadratic ones!). Here's an article (sorry, German only!) showing a couple of examples.