Since last year, the Raspberry Pi has come bundled with a copy of the computational program Mathematica – making it the first computer to come with the software since Steve Jobs’ NeXT in the 1980s. While Mathematica is best known as a tool for helping university students understand and use various mathematical techniques, it can also be used as a specialised programming tool, and a gateway to the Wolfram Alpha knowledge engine. The Home Edition of Mathematica for Windows, OS X or Linux costs US$295 – so the fact that the Raspberry Pi comes with free access to this huge repository of data and powerful algorithms is exciting.

Although the software that ships with the Pi is described as a “pilot release”, it’s an almost complete implementation of Mathematica 10, the latest version. That means it’s a serious piece of software, so things will get a little sluggish once you start exploiting its more sophisticated and powerful features. It will work on any Raspberry Pi, but I think it’s only really usable on the Raspberry Pi 2.

Writing code in Mathematica is a steep learning curve, as the idiosyncratic Wolfram Language bears little resemblance to other mainstream programming languages. Most languages have a relatively small set of native elements that can be combined by programmers into libraries or full working projects. The Wolfram Language, by contrast, comes with a bewildering array of specialist functions built in. This ensures that all functions work in a consistent way and efficiently build upon other elements of the Language. The net result is that the Wolfram Language covers some areas in great depth, but others not at all.

One of the key features of the Wolfram Language is that it’s “symbolic”. This means that everything is a symbol – whether it’s a simple variable, 3D graph or an entire program – and any part of a piece of code can be manipulated by the Language. For example, the EdgeDetect image-processing function doesn’t care whether it’s sent a photo, line art or a dynamically generated 3D chart. Thanks to this consistent approach, applications written in the Wolfram Language work across all sorts of purposes and platforms, removing the need to write bespoke code for different types of input and device.

Mathematica also makes it simple to build interactivity into its output. For example, you can create a sine waveform with a single line:

`Plot[Sin[x(1 + 2*x)],{x,0,6}]`

To turn that into an interactive chart, simply use the “Manipulate” keyword:

`Manipulate[Plot[Sin[x(1 + a*x)],{x,0,6}],{a,0,2}]`

All we’ve done is wrap the command in a “Manipulate” function, changed the number 2 to the variable “a” and added a series of parameters specifying that “a” is the variable to alter, and that it can range from zero to two. This is a powerful capability, especially for educational purposes where the results of an equation can be hard to visualise.

The Wolfram Language is enormous and takes in hundreds of keywords and expressions, so the best way to get started with Mathematica is to find a suitable task and work out how to implement it.

Let’s begin with something simple – plotting the rainfall in your location last year. On your Raspberry Pi, click the Mathematica logo on the menu bar to open the Mathematica interface with an empty document, or “notebook”. You’ll see a plus sign in the top left, which can be used to select how you want to enter your code. One very powerful aspect of Mathematica is that you can enter free-form queries and it will both provide a response and show you the equivalent Wolfram Language code.

Each notebook is made up of a series of cells, each of which contains one or more inputs that are evaluated by Mathematica. To plot the rainfall data, type the line below and press Ctrl+Enter to evaluate it.

`DateListPlot[WeatherData[$GeoLocation,”TotalPrecipitation”,{{2014,1,1},{2014,12,31},”Week”}]]`

After a few seconds, this will generate a line graph of the weekly rainfall between those dates at your location – all in a single line of code.

The data comes from Wolfram Alpha, which can supply a huge range of information. Visit wolframalpha.com to browse through the data categories and see examples such as movie box-office receipts to the locations of various spacecraft. Let’s explore more of that data: on a new line in your notebook, press Ctrl+= to open the discovery box, type Voyager 2 and press return to generate an object that represents the space probe. We can now find out how far away it is:

`DeepSpaceProbeData[Voyager 2, “DistanceFromEarth”]`

The reference to Voyager 2 represents the object shown within an orange box in the Mathematica interface. A query such as this might not seem useful on its own (because you could just have asked Google), but a great strength of Mathematica is that it allows you to create sophisticated queries and present the results graphically. For example, we could generate a chart showing the relative distances of all deep space missions with a couple of lines of code.

Moreover, the Wolfram Language is capable of much more than just retrieving data and presenting it. Mathematica offers machine learning functions similar to those that enable Google’s Photos app to recognise people and scenes. This may be of particular interest to Raspberry Pi owners, as there are a number of Pi-based photography projects, including remote camera traps that capture an image of an animal when it triggers an infrared sensor. Since Mathematica can control the official camera module of the Raspberry Pi, it should be able to identify particular species from photographs.

Let’s give it a try, and see if we can train Mathematica to identify whether a photo is of a cat or a dog. You can see the complete code on YouTube here.

Begin by creating a new notebook in Mathematica. Press Ctrl+=, type “cat breeds” and press return to create the entity “domestic cat”. Immediately after the entity, type “// EntityProperties” and press Shift+Enter to evaluate the cell. This brings up a list of cat properties: copy “image” into the clipboard. Now, change this first input cell so it reads as below and press Shift+Enter.

`catpics=cat breeds[image]`

After a few seconds, Mathematica will display all the images associated with the entity cat breeds. Now that these are safely stored, we need to do the same for dogs, so scroll down the page until you see the horizontal line with a plus symbol to start a new cell. Follow the same process to “discover” the dog breeds entity and its image property. Wolfram knows hundreds of dog breeds, but we only want to import 50 images:

`dogpics=Take[dog breeds[image]],50]`

Now we must create cat and dog entities for Mathematica to link to the pictures. On a new line press Ctrl+= and type “cat” before pressing Enter, and you should get the entity “domestic cat”, which you should assign to the variable like this:

`cat=domestic cat (Species)`

Repeat this with the dog. We now need to create a single list containing both sets of data, which the learning algorithm can use for training.

`allPics={Thread[catpics -> cat], Thread[dogpics ->dog]} //Flatten`

The curly braces mean that we’re creating a list – threading together the existing list of cat pics and assigning them all the entity “cat”. We’re then doing the same with dogs, and flattening it to force it to be a single, one-dimensional, list of name/image pairs.

With all this done, we need to feed this list into Mathematica’s classification engine:

`DogOrCat=Classify[allPics]`

Once this is done, you’ll have your own image-processing function ready for action. The final step is to feed in a picture and see whether your function can tell whether it’s canine or feline. I found an image of a Rottweiler online, downloaded it to my desktop and fed it into our function:

`DogOrCat[Import[“/home/pi/Desktop/rott.jpg”]]`

My function correctly identified it as a dog and worked with most of the other photos I tried – although it was stumped by a Westie.

This sort of machine learning function becomes more accurate with a larger dataset, but the limited memory of the Pi restricted the number of images we could use. You could reduce its memory footprint by using the image processing features, but make sure that your processing doesn’t reduce the usefulness of the images.