2  Quickstart

(ns quickstart
  (:require
   [wolframite.core :as wl]
   [wolframite.wolfram :as w]
   [wolframite.impl.jlink-instance :as jlink-instance]
   [wolframite.tools.hiccup :as wh]
   [wolframite.base.parse :as parse]
   [scicloj.kindly.v4.kind :as k]))

A brief demonstration of what Wolframite can do. It’s a quick read, but you can also jump to any section of interest.

To be extra ‘meta’, this page is itself a demonstration of a literate programming workflow and, as such, is simply a Clojure namespace annotated using the Kindly system. Currently, this is the recommended way of using Wolframite: for the ability to clearly display equations and plots. To use this system, simply look at the relevant source. It is not necessary however; it is still possible, and productive, to use the REPL directly.

2.1 Init

First, we must initialize Wolframite and connect it to a Wolfram kernel, which will perform the computations.

(wl/start)

If you get an error then please refer to the Wolframite README for further instructions. Your Wolfram installation is probably just in an unusual place and so you will have to provide the correct path.

2.2 First things first

To check that the kernel is working, try the following command:

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

As you can see, we can treat Wolfram commands similarly to unevaluated Clojure source code, with some predictable syntax adaptations. This works fine as a starting point, but dealing with long chains of raw symbols doesn’t always scale well. Instead, you can also write

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

or, using one of our fancy aliases,

(wl/eval (w/<*> [1 2 3] [4 5 6]))
32

, which may be more familiar to the Mathematically inclined. If you’re interested in adding your own aliases, then have a look at Section 5.4.

Here, the w namespace is a preconfigured, but configurable, intermediary to Wolfram’s built-in symbols. This allows you to manipulate Wolfram functions just like any other Clojure symbol (and get reliable editor autocompletion).

2.3 Code strings

The above examples are the preferred ways for Clojure and Wolfram to interoperate. You can however, use Wolfram command strings directly, e.g.

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

2.4 Bidirectional translation (experimental)

Code translation in both directions is more difficult and is still somewhat fragile (especially in the wl->clj direction), but the basics work as expected, e.g.

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

Both these functions may be helpful when writing and troubleshooting your Wolframite code.

2.5 Graphics

The above code however, within Mathematica, actually produces graphics. Does Wolframite support this?

Yes!

(wh/view (w/GridGraph [5 5]))
GridGraph[{5, 5}]

(wh/view (w/ChemicalData "Ethanol" "StructureDiagram"))
ChemicalData["Ethanol", "StructureDiagram"]

(wh/view (w/TextStructure "The cat sat on the mat."))
TextStructure["The cat sat on the mat."]

The above graphics were created using the view function from wolframite.tools.hiccup, as required above, and assumes that graphics are to be displayed in a browser.

2.6 Computational knowledge

Wolfram is also known for its dynamic or ‘computational knowledge’ engine.

This can be accessed by many functions directly,

(wh/view (w/GeoNearest (w/Entity "Ocean") w/Here))
GeoNearest[Entity["Ocean"], Here]

, or by posting a request to its online platform, Wolfram Alpha.

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

Here, we’ve shown a response with and without the view function, for reference.

2.7 Mathematics and beyond

In a nutshell, this is how Wolframite is normally used, but, of course, this barely scratches the surface.

In particular, the flagship product of Wolfram, the one you’ve probably heard of, is Mathematica. And, as the name suggests, this entire system was built around the performance of abstract calculation and the manipulation of equations, e.g.

(-> (w/== 'E (w/* 'm (w/Power 'c 2)))

    w/TeXForm
    w/ToString
    wl/eval
    k/tex)

\[e=c^2 m\]

originally answered the question ‘what is mass?’

(-> (w/== 'E (w/* 'm (w/Power 'c 2)))
    (w/Solve 'm)
    w/First w/First

    w/TeXForm
    w/ToString
    wl/eval
    k/tex)

\[m\to \frac{e}{c^2}\]

This is where Wolfram, and so Wolframite, really shines. And if you’re interested in exploring this further, have a look at one of our longer tutorials.

source: notebooks/quickstart.clj