py: Separate common functions and classes into mathlib.py

Importing the library would precompile the source, which would make
starting the program faster.