Mathematica and the Wolfram language on Raspberry Pi

Lists

Lists are a fundamental part of the Wolfram language. Other programming languages use lists as well (e.g., Python). Lists also are sometimes referred to as arrays or matrices; however, in the Wolfram language, lists, arrays, and matrices are three distinct things. That said, the three are pretty similar: An array is effectively a list of functions, and a matrix is a list of lists.

In the Wolfram language, a list is contained in curly brackets:

{1, 2, 3, 4, 5}
{"this", "is", "a", "list", "of", "strings"}

The curly brackets are shorthand notation for creating a list; the full function for creating a list is:

List[1, 2, 3, 4, 5]

However, when you run this code, it is automatically changed to the curly bracket format, so it is usually easier just to use the shorthand notation.

Building lists by manually entering data is a very long-winded process, and for some applications, it is much better to do this in an automated fashion. Luckily, the Wolfram language has you covered, with a number of ways in which to build lists automatically. The first of these methods is the Range function, which autofills a list of numbers:

Range[3, 10, 1/2]

This code will output a list of values starting at 3 and finishing at 10, with a step size of 1/2 (Example D).

The Table function is essentially a more complex version of the Range function that allows you to determine start, end, and step size values, and it also introduces an operator that acts on the number before building the list. For example,

Table[i+1, {i, 3, 10, 1/2}]

takes the values from the preceding Range function and adds 1 to each value, giving the output shown in Example E.

You can change the calculation within the Table function from i+1 to anything you like (e.g., i^3 or i*3).

To create a matrix, just create a list of lists:

matrix = {{5, 1, 3}, {7, 3, 0}, {9, 2, 4}}

You can then display this in a 2D format using the MatrixForm function,

MatrixForm[matrix]

Which gives the output in Example F.

You can even apply functions to an entire list because the Wolfram language treats them as a single entity. For instance,

Sqrt[{5, 1, 3, 8, 9, 4}]

gives the output in Example G.

Last, but not least, you can pull out individual values from within a list very easily using:

list = {1, 2, 3, 4, 5, 6}
2 * list[[1]]

You can also perform functions on individual elements within the list and then write the result back to the list itself:

list[[4]] = Sqrt[list[[4]]]

The output of those last few commands is shown in Example H.

As you can see, the final Sqrt command only performed the square root function on the fourth element in the list, changing it from 4 to 2, unlike the previous square root example, where the entire list was changed.

Bear in mind when using lists in the Wolfram language that the index of a list starts at 1 – in other words, if you want to address the first item in a list, you use the code list[[1]]. In many programming languages (including Python), a list index starts at 0.

Variables

In programming, a variable is a symbol or string that represents a quantity in a mathematical or computational expression. It is convenient to use variables when you have a number or expression that repeats. Additionally, if you ever need to make a change to a number in a calculation, you can use variables instead of actual numbers. In that way, if you want to change a value, you won't have to change every instance of that expression – all you have to do is change the definition of the variable.

For example, say you know the radius of a circle is 5, and you are asked to make a number of calculations to establish the magnitude of the diameter, circumference, and area of the circle. If you did not have variables, you would have to type:

diameter = 2 * 5
circumference = 2 * Pi * 5
area = Pi * 5^2

Now if you were told the radius is actually 7, you would have to change three instances of the old radius, 5, to the new radius, 7. Imagine instead that you used variables to achieve this same result:

radius = 5
diameter = 2 * radius
circumference = 2 * Pi * radius
area = Pi * radius^2

Now, if you were told to change the radius to 7, you would only have to change one parameter – the definition of the variable radius at the top. This is only a simple example to demonstrate a point, but as I am sure you can imagine, in a long and complicated series of calculations variables are extremely useful for this reason.

If you have typed in and executed any of the above code, you will have realized that for every input line you type in, an output line is generated in Mathematica. If you would rather not see this output, you can add a semicolon to the end of the line. Not only does this neaten up the output by removing unnecessary feedback, it also significantly speeds up the calculation of the results – because instead of using up CPU time to output results to the user interface, Mathematica can use all of the CPU available to it for the calculation process. For small programs like those I have used here, this is not such a problem, but it is good practice to get into the habit early of putting a semicolon at the end of every line for which you do not require output.

In the above example for the circle parameters, you are defining the radius, so you do not need or want Mathematica to generate output for this line. Therefore, you can adapt the code to

radius = 5;
diameter = 2 * radius
circumference = 2 * Pi * radius
area = Pi * radius^2

and press Shift+Enter to run. Notice the difference in output from the previous example that did not include the semicolon.

Buy this article as PDF

Express-Checkout as PDF

Pages: 2

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content