Wolfram Langauge interop with Wolframite

Author

Daniel Slutsky

SciCloj logo
This is part of the Scicloj Clojure Data Scrapbook.

This notebook demonstrates basic usage of Wolframite in a way that would work in visual tools supporting Kindly. It is also appears at the Wolrfamite repo as dev/kindly-demo.clj. Note that to use Wolframite, you need Wolfamite in the dependencies, and if Wolfraimte fails to find the correct install path automatically, you may need to have the WOLFRAM_INSTALL_PATH environment variable set up in your system, as explained in Wolframite’s README.

(ns index
  (:refer-clojure
   ;; Exclude symbols also used by Wolfram:
   :exclude [Byte Character Integer Number Short String Thread])
  (:require
   [wolframite.core :as wl]
   [wolframite.tools.hiccup :refer [view]]
   [wolframite.base.parse :as parse]
   [wolframite.jlink]
   [scicloj.kindly.v4.kind :as kind]
   [scicloj.kindly.v4.api :as kindly])
  (:import (com.wolfram.jlink MathCanvas KernelLink)
           (java.awt Color Frame)
           (java.awt.event WindowAdapter ActionEvent)))

Init (base example)

(wl/eval '(Dot [1 2 3] [4 5 6]))
32

Strings of WL code

(wl/eval "{1 , 2, 3} . {4, 5, 6}")
32

Def / intern WL fns, i.e. effectively define WL fns as clojure fns:

(def W:Plus (parse/parse-fn 'Plus {:kernel/link @wl/kernel-link-atom}))
(W:Plus 1 2 3)
6

… and call it

(def greetings
  (wl/eval
   '(Function [x] (StringJoin "Hello, " x "! This is a Mathematica function's output."))))
(greetings "Stephen")
"Hello, Stephen! This is a Mathematica function's output."

Bidirectional translation

(Somewhat experimental, especially in the wl->clj direction)

(wl/->clj! "GridGraph[{5, 5}]")
(GridGraph [5 5])
(wl/->wl! '(GridGraph [5 5]) {:output-fn str})
"GridGraph[{5, 5}]"

Graphics

(view
 '(GridGraph [5 5]))
GridGraph[{5, 5}]

(view
 '(GridGraph [5 5])
 {:folded? true})
GridGraph[{5, 5}]

(view
 '(ChemicalData "Ethanol" "StructureDiagram"))
ChemicalData["Ethanol", "StructureDiagram"]

More Working Examples

(wl/eval '(GeoNearest (Entity "Ocean") Here))
[:entity.ocean/mediterranean-sea-eastern-basin
 :entity.ocean/mediterranean-sea]

TODO: Make this work with view as well.

(view '(TextStructure "The cat sat on the mat."))
TextStructure["The cat sat on the mat."]

Wolfram Alpha

(wl/eval '(WolframAlpha "number of moons of Saturn" "Result"))
(Quantity 145 (IndependentUnit "moons"))
(view '(WolframAlpha "number of moons of Saturn" "Result"))
WolframAlpha["number of moons of Saturn", "Result"]

source: projects/math/wolframite/notebooks/index.clj