mathlib rewrite

New mathlib based on templates. Doesn't use malloc.

WIP, compiles, but errors on linking.