Port _KPolyMath to Scala