kephale:cantor

primitive math for clojure. clojure 1.4 version.



Documentation

Cantor is a high-performance floating point math library.

The idiomatic way to add together two 2-vectors in Clojure is

> (map + [1 2] [3 4])
(4 6)

Sadly, this is quite slow. Cantor defines operators which work efficiently across a variety of types. The above operation, in Cantor, is accomplished thus:

> (add (vec2 1 2) (vec2 3 4))
[ x=4.0 y=6.0 ]

This is roughly 10-20x faster than the idiomatic approach. These operators also work on normal numbers:

> (add 1 2)
3

The available operators include:

  • add
  • sub
  • mul
    • (mul (vec2 1 2) 2) => [ x=2.0 y=4.0 ]
    • (mul (vec3 1 2 3) (vec3 4 5 6)) => [ x=4.0 y=7.0 z=9.0 ]
  • div
    • (div (vec3 2 4 6) 2) => [ x=1.0 y=2.0 z=3.0 ]
    • (div (vec2 4 8) (vec2 1 2)) => [ x=4.0 y=4.0 ]
  • dot
    • (dot (vec2 1 0) (vec2 0 1)) => 0.0
  • polar
    • (polar (vec2 1 1)) => [ theta=45 r=1.414 ]
  • map*
    • (map* #(+ % 1) (vec3 1 2 3)) => [ x=2.0 y=3.0 z=4.0 ]
  • cartesian
    • (cartesian (polar2 45 1)) => [ x=0.707 y=0.707 ]
    • (cartesian 45) => [ x=0.707 y=0.707 ]

Complete documentation can be found here.