May 1, 2013

Geometric Algebra with Clojure - Part 1

I've been exploring Geometric Algebra (GA) and Clojure for a while, and I'm finding some potential to combine my learning of both. Here are my first steps on a Mac:

  1. Download and install GaiGen2.5 (http://sourceforge.net/projects/g25/) which is a GA code generator for a variety of languages, including java.
  2. Generate some sample algebra specs, generate the code from the sample Conformal 3d Java spec file, compile, and package into a jar.  I get a warning, "Warning: No operator bindings are possible for output language Java."  I wonder if Gaigen would benefit from being rewritten in Clojure, to gain from its metaprogramming capabilities ...
  3. cd ~/Develop/GA/gaigen25/
    g25_test_generator -sa .
    cd TestG25
    g25 c3ga_java/c3ga_java.xml
    javac c3ga_pkg/*.java
    jar cvf c3ga.jar c3ga_pkg
    
  4. Create a new clojure project, add the GA jar as a resource to the project.clj, and start a repl:
  5. cd ..
    lein new gaclj
    cd gaclj
    mkdir lib
    cp ../TestG25/c3ga.jar lib
    nano project.clj
    
    (defproject gaclj "0.1.0-SNAPSHOT"
      :description "First try at doing geometric algebra in clojure"
      :url "https://www.thosmos.com/?p=256"
      :license {:name "Eclipse Public License"
                :url "http://www.eclipse.org/legal/epl-v10.html"}
      :resource-paths ["lib/c3ga.jar"]
      :dependencies [[org.clojure/clojure "1.4.0"]])
        
    lein repl
    
    user=> (def mv (c3ga_pkg.mv.))
    #'user/mv
    
    user=> (.set mv 25.0)
    nil
    
    user=>; mv
    #<mv 25.00>
    
    user=> (.toString mv)
    "25.00"
    
    user=> (def e1 (c3ga_pkg.c3ga/vectorE1))
    #'user/e1
    
    user=>; e1
    #<vectorE3GA 1.00*e1>
    
    user=> (def e2 (c3ga_pkg.c3ga/vectorE2))
    #'user/e2
    
    user=> e2
    #<vectorE3GA 1.00*e2>
    
    user=> (def e3 (c3ga_pkg.c3ga/vectorE3))
    #'user/e3
    
    user=> (c3ga_pkg.c3ga/add e1 e2)
    #<vectorE3GA 1.00*e1 + 1.00*e2>
    
    user=> (def e12 (c3ga_pkg.c3ga/add e1 e2))
    #'user/e12
    
    user=> e12
    #<vectorE3GA 1.00*e1 + 1.00*e2>
    
    user=> (.toString e12)
    "1.00*e1 + 1.00*e2"

    This looks promising. Some next steps will be to get the objects drawn on a canvas of some kind, and then to get some live updating going on, ideally in a LightTable and Bret Victor style. I've also been exploring TEALsim, used by MIT for their EM simulations. So another goal is to get the GA working with an EM simulator...

Tags: geometric algebra clojure