2  Quickstart

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 Clojure REPL (→ Section 10.5) directly.

First, let’s require few Wolframite namespaces

(ns quickstart
  (:require
   [clojure.set :as set]
   [clojure.string :as str]
   [wolframite.api.v1 :as wl]
   [wolframite.core :as wc]
   [wolframite.lib.helpers :as h]
   [wolframite.runtime.defaults :as defaults]
   [wolframite.tools.hiccup :as wh]
   [wolframite.wolfram :as w :refer :all
    :exclude [* + - -> / < <= = == > >= fn
              Byte Character Integer Number Short String Thread]]
   [scicloj.kindly.v4.kind :as k]))

2.1 Init

Before we do anything else, we must initialize Wolframite and connect it to a Wolfram kernel, which will perform the computations:

(wl/start!)
{:status :ok, :wolfram-version 14.1, :started? true}

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/! (w/Dot [1 2 3] [4 5 6]))
32

wl/! asks Wolframite to translate the following expression to Wolfram and send it to a Wolfram kernel for evaluation.

We could also use one of our fancy aliases,

(wl/! (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/! "{1 , 2, 3} . {4, 5, 6}")
32

More info in Understanding Wolframite > Wolfram string form Section 3.2.5

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]))
"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.

There are other also ways to display graphics, for example using wolframite.tools.graphics to display it in a Java window, or using wl/Export to export it into an image file.

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/! (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.

(k/tex
 (-> (w/== 'E (w/* 'm (Power 'c 2)))
     TeXForm
     ToString
     wl/!))

\[e=c^2 m\]

originally answered the question ‘what is mass?’

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

     TeXForm
     ToString
     wl/!))

\[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.

2.8 Productivity tip

When you write a lot of Wolframite, it may be a little annoying with all the w/ prefixes. Therefore, we recommend to use this require form instead:

(ns whatever
  (:require [wolframite.wolfram :as w :refer :all
             :exclude [* + - -> / < <= = == > >= fn Byte Character Integer Number Short String Thread]]))

Then you can refer to most symbols directly, with the exception of those that conflict with Clojure’s core functions and Java classes, which could lead to confusion and mistakes.

With this, you can write:

(wl/! (Map (w/fn [x] (Power x 2)) (Table 'i ['i 1 3])))
[1 4 9]

2.9 Wolfram has a lot to offer

You may also want to browse the thousands of functions from various domains that Wolfram provides. These domains include Machine Learning, Calculus & Algebra, Optimization, Geography, and many more.

source: notebooks/quickstart.clj