Pluggable Math

Note: please merge this changes before marging #907 if possible