Mathematica Beyond Mathematics The Wolfram Language in The Real World 2nbsped 2022030387 2022030388 9781032004839 9781032010236 9781003176800 - Compress
Mathematica Beyond Mathematics The Wolfram Language in The Real World 2nbsped 2022030387 2022030388 9781032004839 9781032010236 9781003176800 - Compress
Mathematics
Although many books have been written about Mathematica, very few of them cover the new
functionality added to the most recent versions of the program. This thoroughly revised second
edition of Mathematica Beyond Mathematics: The Wolfram Language in the Real World
introduces the new features using real-world examples based on the experience of the author as
a consultant and Wolfram certified instructor. The examples strike a balance between relevance
and difficulty in terms of Mathematica syntax, allowing readers to incrementally build up their
Mathematica skills as they go through the chapters While reading this book, you will also learn
more about the Wolfram Language and how to use it to solve a wide variety of problems.
The author raises questions from a wide range of topics and answers them by taking full advan-
tage of Mathematica’s latest features. For example: What sources of energy does the world really
use? Are our cities getting warmer? Is the novel El Quixote written in Pi? Is it possible to reliably
date the Earth using radioactive isotopes? How can we find planets outside our solar system?
How can we model epidemics, earthquakes and other natural phenomena? What is the best way
to compare organisms genetically?
This new edition introduces the new capabilities added to the latest version of Mathematica
(version 13), and discusses new topics related to machine learning, big data, finance economics,
and physics.
• Separate sections containing carefully selected additional resources that can be accessed
from either Mathematica or online
• Online Supplementary materials including code snippets used in the book and additional
examples.
• Updated commands to take full advantage of Mathematica 13.
He is also a Wolfram certified instructor and has extensive experience in teaching and develop-
ing programs with both applications. Among his more than 100 articles, there are several where
Mathematica and web Mathematica have been used extensively.
Taylor & Francis
Taylor & Francis Group
http://taylorandfrancis.com
Mathematica Beyond
Mathematics
The Wolfram Language in the Real World
Second Edition
Translated by
Rubén García Berasategui
Second edition published 2023
by CRC Press
6000 Broken Sound Parkway NW, Suite 300, Boca Raton, FL 33487-2742
Reasonable efforts have been made to publish reliable data and information, but the author and publisher cannot
assume responsibility for the validity of all materials or the consequences of their use. The authors and publishers
have attempted to trace the copyright holders of all material reproduced in this publication and apologize to copyright
holders if permission to publish in this form has not been obtained. If any copyright material has not been acknowl-
edged please write and let us know so we may rectify in any future reprint.
Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced, transmitted, or
utilized in any form by any electronic, mechanical, or other means, now known or hereafter invented, including pho-
tocopying, microfilming, and recording, or in any information storage or retrieval system, without written permission
from the publishers.
For permission to photocopy or use material electronically from this work, access www.copyright.com or contact the
Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA 01923, 978-750-8400. For works that are
not available on CCC please contact [email protected]
Trademark notice: Product or corporate names may be trademarks or registered trademarks and are used only for
identification and explanation without intent to infringe.
Publisher’s note: This book has been prepared from camera-ready copy provided by the authors.
Contents
Preface IX
1. Getting Started 1
1.1 Mathematica, an Integrated Technical Computing System 1
1.2 First Steps 3
1.3 Editing Notebooks 12
1.4 Basic Ideas 19
1.5 From Graphics to Machine Learning 29
1.6 Additional Resources and Supplementary Materials 40
Index 437
Preface
Book Objectives
This book will not only be useful to newcomers but also to those familiar with the
program and interested in learning more about the new functionality included in the
latest versions.
Those readers with minimal or no knowledge of Mathematica are strongly advised
to read Chapter 1 along with Stephen Wolfram’s book An Elementary Introduction
to the Wolfram Language available from within the program documentation under
the resources section: Help Wolfram Documentation Intro Book & Course
This text will also make it easy to start programming using the Wolfram Language
and to learn how to take full advantage of its capabilities.
The final objective of Mathematica Beyond Mathematics is to help you avoid
feeling overwhelmed by the software’s vast capabilities. The author has explored a
significant part of them choosing the most relevant parts and illustrating them with
examples from many different sources including the program documentation. Links
to additional resources are also provided. The main aim of all this is to reduce
significantly the amount of time required to master the tool. The Mathematica
functions used will be explained using short sentences and simple examples.
Author
J. Guillermo Sánchez León (http://diarium.usal.es/guillermo) is an engineer,
physicist, and mathematics PhD holder. He is currently a mathematics and
statistical consultant and teaches mathematical modeling at the University of
Salamanca. He has worked in the energy industry and conducted research in a
variety of fields: Modeling, optimization, medical physics, astronomy, finance, and
others. In 1999, he was awarded a research grant at Wolfram Research Inc.
headquarters in Champaign, Illinois, US after his statistical applications with
Mathematica project won a competition sponsored by the company. Since then, he
xii Mathematica beyond mathematics
has been an active Mathematica and webMathematica alpha and beta tester. He is
also a Wolfram-certified instructor and has extensive experience in teaching and
developing programs with both applications. Among his more than 100 articles,
there are several where Mathematica and webMathematica have been used
extensively.
Reviewer
Rubén García Berasategui is a math, finance, and statistics lecturer at Jakarta
International College. He holds a bachelor’s degree in business administration and
an MBA. He’s been using Mathematica since 1997 and in 2012 became the first
Wolfram certified-instructor in Southeast Asia. He has been training hundreds of
newcomers to Mathematica and sharing his passion for the program with them ever
since.
Acknowledgments
The author would like to express his gratitude to Ruben Garcia Berasategui for his
help in writing both editions. Most likely, I would not have written the book
without his participation.
The author would also like to thank Callum Fraser (Editor of CRC Press, Taylor &
Francis Group), for his vision in realizing the need in the market for a book about
Mathematica like this one, and Mansi Kabra ( Senior Editorial Assistant of CRC
Press , Taylor & Francis Group) for his encouragement and support during the
production process.
The author would also like to say thank you to Addlink Software Científico
(http://www.addlink.es), sponsor of the first Spanish edition of this book; Antonio
Molina and Juan Antonio Rubio for their editorial guidance; and J. M. Cascón
(USAL), J. López-Fidalgo (UCLM), S. Miranda (Solventis), R. Pappalardo (US), J.
M. Rodríguez (USAL) and C. Tejero (USAL), for their comments and corrections
to the first edition.
1
Getting Started
28. C
With very brief syntax we can build functions that in other programming languages
would require several lines of code.
In less than half a line we can write the necessary instructions to make an interactive model
of the Julia fractal set.
ManipulateJuliaSetPlot0.365 k , PlotLegends Automatic,
k, 0.385, 0.3, 0.7
The code below, generates a map with a 10-km-radius around where you are (this is usually
based on your IP address):
GeoGraphicsGeoDiskHere, Quantity10, "km"
1. Getting Started 3
1.2.1 Starting
The program can be run locally with Mathematica or Wolfram|One or over the
Internet using Wolfram Cloud or Mathematica Online.
Mathematica: https://www.wolfram.com/mathematica
Wolfram|One: https://www.wolfram.com/wolfram-one
Wolfram Cloud: https://www.wolframcloud.com
Mathematica Online: https://www.wolfram.com/mathematica/online
From here on, unless mentioned otherwise, we will assume that you have installed
Mathematica locally and activated its license. However, if you are using any of the
other two alternatives, the process is quite similar. When installing it, an assistant
will guide you through the activation process. If this is the first time, it will take
you to the user portal (https://user.wolfram.com), where you will have to sign up.
The website will ask you to register using an email address and create a Wolfram
ID and a password. It will also automatically generate an activation key. Remember
4 Mathematica beyond mathematics
your Wolfram ID and password because they will be useful later on if, for example,
you would like to install the program in a different computer or have an expired
license. In both cases, you will need a new activation key. The Wolfram ID and
password will also be necessary to access the Wolfram Cloud or Wolfram|One, and
we will give a specific example at the end of the chapter.
To start the program in Windows, under the programs menu in your system, click
on the Wolfram Mathematica icon.
Under OS X, the program is located in the applications folder, and you can launch
it either locating it with Finder, using the Dock or from the launchpad (OS X Lion
or more recent versions).
By default, a welcome screen (Figure 1.1) similar to this one will appear:
This screen contains several external links (hyperlinks). For example, by clicking
on Resources, you will be able to access a broad collection of reference materials
and tutorials. In the upper left-hand corner you will see this icon: . If
you are a registered user, chances are you already have a Wolfram ID. In that case,
click on the icon and sign in. Once the process has been completed, the icon will be
replaced with the name that you used when you signed up, as shown in the previous
image. The advantage of this is to gain access from within Mathematica to your
files in the cloud (WolframCloud) but for now, you can skip this step if desired. If
you click on New Document, below the icon shown at the beginning of the
paragraph, a new blank Notebook will be created. Notebooks are the fundamental
way to interact with Mathematica. You can also create them by clicking on File
New Notebook.
A notebook is initially a blank page. We can write on it the text and instructions
that we want. It is always a good idea to save the file before we start working on it;
as in most programs, this can be done in the menu bar: File Save As “Name of
the File” (Mathematica by default will assign it the extension .nb).
It is usually convenient to have access to the Formatting toolbar, select in the
menu bar Window and check Toolbar Formatting. Please note the drop-down
box to the left of the toolbar (Figure 1.2), that we’ll refer to as: style box. It will be
useful later on.
Once you begin to type, a new cell will be created. It will be identified by a blue
right-bracket square (]) (“cell marker”) that appears on the right side of the
notebook. Each cell constitutes a unit with its own properties. To see the cell type,
place the cursor inside or on the cell marker. The type will appear in the style box
(Figure, Input, Text, Title, Program...). We can also change the format directly
inside the box. Type in a text cell and try to change its style.
This is a cell formatted using the Program style.
When a blank notebook is created, it has a style (cell types, fonts, etc.) that assigns
to each type of cell certain properties (evaluatable, editable, etc.). The first time a
notebook is created, Mathematica assigns a style named Default (by default). Later
on we will see how to choose different styles. In the Default style, when a new cell
is created, it will be of the Input type, which is the one used normally for
calculations. When we evaluate an Input type cell, “In[n]:= our calculation” will be
shown and a new cell of type Output will be generated where you will see the
calculation result in the form of “Out[n]:= result” (n showing the evaluation order).
However, in this book we sometimes use an option to omit the symbols “In[n]:=”
and “Out[n]:=”. These and other options to customize the program can be accessed
through Edit Preferences ....
To execute an Input cell select the cell (placing the cursor on the cell marker)
and press or on the numeric keypad. You can also access the
contextual menu by right-clicking with the mouse and selecting “Evaluate Cell”.
4
When a cell is executed, a toolbar appears below its output as shown above (Figure
1.3). This bar, named the Suggestions Bar, provides immediate access to possible
next steps optimized for your results. For example, If you click on “range” you
will see that a new input is being generated and a new suggestions bar will appear
right below the result:
Range4
1, 2, 3, 4
6
The Suggestions Bar is part of the predictive interface. It tries to guide you with
suggestions to simplify entries and ideas for further calculations. It is one more step
to reduce the time it takes to learn how to use the program.
All written instructions inside a cell will be executed sequentially.
To create a new cell we just need to place the cursor below the cell where we are
and then start writing. We can also create a new cell where the cursor is located by
pressing +.
To facilitate writing, Mathematica provides Palettes. These can be loaded by
clicking on Palettes in the menu bar. There are several palettes to make it easier to
type mathematical symbols such as the Basic Math Assistant or the Other Basic
Math Input palettes. It would be useful from now on to keep one of them open.
Using a palette or the keyboard shortcuts write and execute the following expression. Please
remember: an empty space is the equivalent of a multiplication symbol: that is, instead of
45 you can write 4 5.
45
a 4 b 32 b
5
4a7b
Alternatively, you can use keyboard shortcuts to write special symbols, subscripts,
superscripts, etc. The most useful ones are: subscript + _ , superscript + ^
or + 6, fraction + / and the square root symbol + 2. Depending on your
keyboard configuration, to use some of the previous symbols you may have to
press first + (key CAPS) for example: superscript + + ^.
The previous Input cell was written in the standard format (StandardForm). It is the
one used by default in Input cells. To check it, position the cursor on the cell
marker and in the menu bar choose: Cell Convert To . InputForm will appear
with a check mark in front of it.
4 5 5 a Sqrt4 b 3 ^ 2 b;
The InputForm is very similar to the one used by other programming languages
such as C, FORTRAN or Python. For instance: multiplication = " * ", division = " /
", addition = " + ", subtraction = " - " , exponentiation = " ^ ", and square root of a
= "Sqrta". We can replace the multiplication symbol by an empty space. It is
very important to keep that in mind since Mathematica interprets differently "a2"
and "a 2": "a2" is a unique symbol and "a 2" is a 2. We have also added
semicolon “;”, thus the instruction will be executed, but its output will not be
shown. This is useful to hide intermediate results.
Initially, it was its symbolic calculation capabilities that made Mathematica popular
in the academic world.
As an example, write and execute the following in an input cell (use a palette to help you
enter the content).
cosx y 1
x sinx y
2 y 2
A customized style, to which we will refer later, has been defined in this notebook
so that the outputs are shown in a format that corresponds to traditional
mathematical notation. In Mathematica it is called TraditionalForm. We could
have defined a style so that the inputs also use the traditional notation as well, but
in practice it is not recommended. However, you can convert a cell to the
TraditionalForm style anytime you want.
Copy the input cell from above to a new cell and in the menu bar select Cell Convert To
TraditionalForm. Notice that the cell bracket appears with a small vertical dashed line.
You will be able to use the same menu to convert to other formats.
2 sin
cosx y 1
x sinx y
2 y 2
The natural language or free-form format enables us to write in plain English the
operation we want to execute or the problem we want to solve. To start using it,
press = at the beginning of the cell (input type) and you will notice that (“Free-
form Input”) will appear. After that, write your desired operation. Your instruction
will automatically be converted into standard Mathematica notation, and then the
answer will be displayed. Let’s take a look at some examples.
To add the numbers from 1 to 10 you can write “sum 1 to 10” or other similar English
expressions such as “sum from 1 to 10”.
55
If you follow the previous steps, the entry proposed may be different from the one
shown here. The reason behind this is that when using the free-form format, the
program interprets your input by connecting to Wolfram|Alpha online, and the
knowledge engine is constantly evolving.
Move the cursor over Sumi,i, 1, 10and the message “Replace cell with
this input” will appear. If you click on the symbol + located in the upper right-hand
corner, additional information about the executed operation will be shown.
If you have received any message related to connection problems, it is because the
free-form format and the use of data collections that we will refer to later on,
requires that your computer is connected to the Internet. If it is and you still have
connectivity problems it may be related to the firewall configuration in your
network. In that case you may have to configure the proxy or ask your network
8
1 1
x 1 13 , x 13 1
3 3
When you don’t know the correct function syntax, you can use the free-form
format to write what you want to do. Mathematica probably will show you the
correct syntax immediately; click on it and only the input in the correct notation
will remain with the free-form format entry disappearing.
The free-form input format generally works well when it’s dealing with brief
instructions in English. In practice it is very useful when one doesn’t know the
specific syntax to perform certain operations in Mathematica.
It’s also very useful to use Inline Free-form Input.
In this example, we assume we don’t know the syntax to define an integral. In an input cell
we click on Insert Inline Free-form Input and will appear. Inside this box we type
integrate x^2.
Integrate x^2
x3
3
The entry is automatically converted to standard WL (Wolfram Language) or Mathematica
syntax or by clicking over the symbol, the cell will go back to its free-form format.
x3
3
By clicking in the input is automatically converted to the standard syntax in
Mathematica. It can be used as a template.
Integratex ^ 2, x;
You can ask practically anything that can be computed. The below value is changing. The
distance from the Earth to the Moon varies depending on the position of the Moon in its
orbit around the Earth.
370 017. km
"
9.80
major
C4 D4 E4 F4 G4 A4 B4 C5
From the output, not shown, we selected the part that we were interested in (“Music
notation”) by clicking on the symbol and choosing the desired option (“Formatted pod”).
A new entry was generated displaying only the musical scale.
WolframAlpha"Do major",
IncludePods "MusicNotation", AppearanceElements "Pods",
TimeConstraint 30, Automatic, Automatic, Automatic
The most important difference between using the free-form input notation and
the Wolfram|Alpha one, is that in the first case the entry that we write is sent by
the program to a Wolfram server that will return the syntax in Mathematica and
it will be executed locally, while by using he the Wolfram|Alpha notation, all
the processing is done in the server, and our computer only displays the output.
If we have the program locally installed, it is better to use the free-form notation
to get the Mathematica syntax. In this way, we will be able to use the output
easily in subsequent calculations.
In the screen-shot shown above (Figure 1.4), after clicking on Data Manipulation
& Analysis Importing & Exporting, Mathematica displays the window below
(Figure 1.5). Notice that besides giving us the functions related to the chosen topic,
we can also access tutorials and even external resources such as video
presentations.
vaguely, we can use the search toolbar (Figure 1.6) with two or three words related
to what we are looking for.
Another type of help is the one given when a mistake is made. In this case we
should pay attention to the text or sound messages that we have received. For
example, if we try to evaluate the following text cell, we will receive a beep.
“This is a text entry”
With Help Why the Beep?... we will find out the reason behind the beep.
An extra source of help is the Input Assistant with its context-sensitive
autocompletion and function templates features: when you start writing a function,
Mathematica helps you automatically complete the code (similar to the system used
by some mobile phones for writing messages). For example: If you start typing Pl
Mathematica will display all the functions that start with those two letters right
below them (Figure 1.7). Once you find the appropriate entry, click on it and a
template will be added.
Sometimes, a down arrow will be shown at the end of the function offering us
several templates (Figure 1.8). Choose the desired one.
Additionally, a color code enables syntax error detection. If after typing a function
its color is blue, probably it has not been entered correctly. All Mathematica and
12 Mathematica beyond mathematics
user-defined functions will be shown in black. For example: If you write in an input
cell Nmaximize, it will be displayed with a blue font. However, once you change it
to NMaximize, the function is now shown in black and a template is displayed.
With Insert Hyperlink we can create hyperlinks to jump from one location to
another inside the same notebook, to another notebook or to an external link.
The Format menu contains options to change the font, face, size, color, etc.,
of the contents of the cells. We can even use special characters such as .
This notebook, like any other, has a predefined style. This style tells Mathematica
how to display its contents. There are many such styles. To choose one you can use
Format Stylesheet. To define the style of a particular cell use Format Style.
Additionally, inside a style, we can choose the appropriate working environment:
Format Screen Environment. Try changing the appearance of a notebook by
modifying the working environment.
1. Getting Started 13
When typing in a new notebook choose the stylesheet from the beginning with
Format Stylesheet Stylesheet Chooser... . After clicking on the upper side
of the icon representing the desired style (New), you will be able to use and
modify the template according to your needs.
If you’d like to type a sequence of formulas and align them at the equal signs, in
Writing Assistant, choose Math Cells Equal Symbol Aligned Math Cell.
ab c
a cb
Figure 1.10 Aligning equations at the equal sign.
Sometimes it may be useful to type both, the formula and its result, as part of the
text. This can be done by writing and executing the function in the same text cell.
For example: using a palette write x3 x = x3 x. Now select the second term of
the equation and press ++, or, in the menu bar, choose Evaluation
4
Evaluate in place, and the previous expression will become x3 x = x4 .
When creating a document, you may be interested in seeing only the result. In that
case you can hide the Input cell.
To hide the input just click directly on the output marker cell. The following example was
created by executing PlotSinx, x,2 Pi, 2 Pi and then hiding the
command to display only the graph.
PlotSinx, x, 2 Pi, 2 Pi
1.0
0.5
6 4 2 2 4 6
0.5
1.0
All Mathematica functions are in English, but the menus and palettes can also be
displayed in any of the additional 12 languages offered since version 11. The
alternatives include Chinese, French, German, Italian, Japanese, Korean and
Spanish among others. Users can take advantage of this functionality by going to
Edit Preferences Interfaces. Once an option is selected, every function will
be automatically annotated with a “code caption” in the chosen language. Since
Mathematica 11, the program includes a real-time spell checker that works with
any of the supported languages. Alternatively, users of previous versions can
review the English contents of a notebook with Edit Check Spelling... .
With Edit Preferences and with Format Option Inspector (see
tutorial/OptionInspector) you will be able to customize many features of
Mathematica that can be applied globally, to a notebook or to a particular cell. For
You can write your inputs in StandardForm. Once you verify that they work,
you can convert them to the traditional style.
Example: Let’s type:
2
x x x2
Limit , x 0
x
2x
We convert the cell to the traditional form by selecting it and clicking on:
Cell Convert to TraditionalForm. Then the cell above will be shown as follows:
x2 2
lim
x0 x
2x
If an entry is in the traditional style, to see how it was originally typed convert it
to the standard style by selecting the cell and choosing from the menu bar
Cell Convert to StandardForm.
In many of the styles available in Stylesheet Chooser... outputs are displayed using
the standard form. In this chapter, we have used a customized stylesheet that shows
outputs in the traditional format. In the rest of the book, we will almost always use
the standard form since what we are trying to highlight is how to create functions
with Mathematica and for that purpose, the classical format is not adequate.
If you have opened a new notebook using the Textbook style, you will probably see
(0.1) instead of (1.1). It’s possible to modify the numbering scheme, but it is
beyond the scope of this chapter. When you need to number equations, sections,
and so on, remember that it is a good idea to select the stylesheet directly from the
Writing Assistant palette: Palettes Writing Assistant Stylesheet Chooser... .
After selecting Textbook (New), a new notebook will be generated with many
different numbered options such as equations, sections, etc. You can then use that
notebook as a template.
If you wish to insert an automatic reference to the equation proceed as follows:
Write an equation in a cell with the EquationNumbered style. Then go to Cell Cell Tags
Add/Remove... and add a tag to the formula. Let’s give it the tag: “par” (we write “par”,
from parabola, although we could have used any other name). A good idea would be also to
go to Cell Cell Tags and check Show Cell Tags, keeping it checked while creating the
document, and only unchecking it after we are done. This will enable us to see the cell tags
at all times. The first equation done this way is written below:
par
x2 = 1 (1.2)
If you wish to refer to this equation, type the following: (1. ), put the cursor after the dot and
go to Insert Automatic Numbering. In the opening dialog (Figure 1.11) scroll down to
EquationNumbered, choose the tag of the equation, in our case: “par” and then press OK
(see the screen below). After this has been done, the number of the equation appears
between the parentheses: (1.2). If you click on the “2” the cursor will go immediately to the
actual equation.
1. Getting Started 17
The main advantage of this format is that it keeps the interactivity of the objects
created with Manipulate. In this case, your documents can be read with the
Wolfram CDF Player (available for download for free from
http://www.wolfram.com/cdf-player). We recommend the inclusion of the player
web link when sending the file to ensure that the intended reader can open it .
The Wolfram CDF Player is not just a reader of Mathematica documents for users
without access to the program. It can also avoid displaying the functions used in the
inputs, showing only the outputs (remember the trick that we have seen previously
to hide the inputs and only show the outputs). Furthermore, it can include dynamic
objects created with Manipulate so that readers can experiment with changing
parameters and visualizing the corresponding effects in real time. Additionally,
readers will not even know that the original document was generated in
Mathematica. To see all these capabilities in action take a look at the following cdf
document:
http://www.wolfram.com/cdf/uses-examples/BriggsCochraneCalculus/BriggsCochraneCalculus.cdf
Many of the Wolfram Research websites, such as Wolfram|Alpha
(http://www.wolframalpha.com) , allow visitors to generate and download
documents in cdf format. The same happens with the Wolfram Demonstrations
Project (http://demonstrations.wolfram.com) where the files are available in cdf
format and you can download and run them locally.
You can now create a new notebook by clicking on: Create a New Notebook, and
reproduce most of the examples discussed in the chapter. You can also save the
notebook online and when accessing the file again, no matter from what location,
continue working on it. Additionally, you have the possibility of accessing cloud
files from a local Mathematica installation: File Open from Cloud... or File
Save to Cloud.... Mathematica has specific functions related to the Wolfram
Cloud. In later chapters, we will refer to them.
When you don’t know the syntax of a function, type it, select it with the cursor,
and press the <F1> key.
sin 1
60 22 3 x2
7 y
This notebook uses a style where the outputs are shown in the traditional form.
We have assigned it the name expr1, to be able to use the result later on. To write
the transcendental numbers and , we have taken advantage of special symbols (
is not the same as e; e is a letter without any other meaning) located in the Basic
Math Assistant palette. Alternatively, we could have used the keyboard and write
Pi for and E^ or Exp[] for . Notice the use of a single equal sign "=" to indicate
sameness or equivalence in equations. For comparisons, we will type a double
equal sign "==".
The same operations can be written using the typical InputForm notation:
5 Cos3 Pi 2 ^ 1 3 x ^ 2 Pi Sin1 E ^ 7 y
sin 1
60 22 3 x2
7 y
Here x is replaced with 3 and y with 5. To do that, you apply the following syntax: “expr .
rule” that will replace expr with the contents of rule.
expr1 . x 3, y 5
sin 1
540 22 3
5 7
There is something that should draw our attention in this output. There are terms
that have not been evaluated: , sin(1), and 7 . This is because Mathematica does
not simplify or make approximations if that means losing precision. It literally
works with infinite precision. Nevertheless, we can force it to compute the decimal
approximation, the usual approach in other programs. To do that, we will use the
function N as shown in the next example. A similar result can be also obtained by
including decimals in some of the numbers.
In this example we use the % symbol to recall the last output. N or N[expr] use machine
precision in their calculations even though the output may show fewer decimals. By default,
5 decimals are shown, but with N, n we can use a precision n as large as desired.
N
857.196
If you apply several replacement rules to the same expression in succession, the
replacement takes places consecutively:
$
We first apply (/.) the rule (ab) to the expression a x b y and then the second rule (bc)
is applied to the previous result.
axby .a b . b c
cxcy
expr1
1.4.2 Functions
Mathematica contains thousands of prebuilt functions (Functions) that are always
capitalized and whose arguments are always written inside square brackets [arg].
It’s important to keep in mind that the program differentiates between lower and
upper cases. For example: NMinimize is not the same as Nminimize. However,
users can define a function using lower case.
Lists are a fundamental Mathematica concept, frequently used in many contexts. In
a list, elements are inside curly brackets {}. Later on we will refer to them.
Remember these five rules: All functions in the WL are capitalized: Log;
arguments are written inside square brackets: Sin2 x 1; list elements are
inside curly brackets: {a1 , ..., an }; ranges are written as lists: Plotx, x,
0, 2 Pi; and parentheses are used to group operations: Loga x
Sinb x a b .
Figure 1.13 shows an input type cell. If you’d like to avoid its evaluation, go to:
Cell Cell Properties and uncheck Evaluable. That is what we did in this case in
the original document since the idea is to see the cell content but not to execute it.
In the previous template, all the basic function arguments are displayed. Now you can fill
them in using the options as the next example shows. Execute the Mathematica expression
below.
22
0.5
5 10 15 20 25 30
0.5
1.0
The resulting output is a plot with the suggestion bar (Figure 1.14) appearing right below it.
Thanks to the WolframPredictiveInterface, the Image Assistant and Drawing Tools provide
point-and-click image processing and graphics editing. Click on theme... and a menu with
several choices to customize the plot will unfold (Figure 1.15).
Figure 1.15 Customizing a plot using the “theme...” option in the Suggestions Bar.
Besides arguments, WL functions also have options. One way of seeing those
options directly is Options[func]:
Solve options:
OptionsSolve
Next, we’ll show some examples. Replicate them using the Basic Math Assistant
palette that includes templates for hundreds of commands.
Definite integral example:
4 3
Sinx Cosx x
3 sin x 1 1 1
sin 3 x sin 5 x sin 7 x
64 64 320 448
Use Simplify anytime you need to simplify a complex expression.
Simplifyx8 4 x6 y2 6 x4 y4 4 x2 y6 y8
4
x2 y2
$
There are also other functions to manipulate expressions. You can find them in the
palette Other Algebraic Manipulation. Note that you can manipulate an entire
expression or just part of it.
Try to use it with this example simplifying the Sin and Cos arguments separately.
Sinx8 4 x6 y2 6 x4 y4 4 x2 y6 y8 Cosx4 2 x2 y2 y4
To get:
4 2
Sinx2 y2 Cosx2 y2
You can rotate the image to see it from different angles by clicking with the mouse
cursor inside of it.
If you haven’t used Mathematica previously, the last command may seem strange.
Let’s analyze it step by step:
Highlight RandomInteger and press <F1>. The help page for the command will be
displayed. In this case with RandomInteger5, 5, 10, 3 we are generating 10
sublists with 3 elements, each element being a random number between 5 and 5. We are
going to use them to simulate the coordinates {x, y, z}.
The previous function is inside Sphere (do the same, highlight Sphere and press <F1>) a
command to define a sphere or a collection of spheres with the coordinates
x1 , y1 , z1 , x2 , y2 , z2 , , r, where r is the radii of the spheres. If omitted, as
in this example, it’s assumed that r 1.
SphereRandomInteger5, 5, 10, 3
4 1 4
2 3 2
4 4 1
1 4 4
2 1 0
Sphere
5 4 2
2 5 5
1 5 2
4 5 4
2 3 1
Note: In this particular notebook, list outputs are sometimes shown as matrices.
24
We use the command Graphics3D to display the graphs. We have added Yellow
to indicate that the graphs, the spheres in this case, should be yellow and the option
ImageSize to fix the graph size and make its proportions more adequate for this
example. Finally we arrive at the instruction:
Graphics3DYellow, Sphere
We have iconized the argument inside Sphere (highlight the part of expression to be
iconized, right-click on it and then select Un/Iconize selection).
In the next cell, we use Rotate to show a rotated output, in this case by 1 radian, compared
to the usual display.
Rotate1 Sqrt1 x, 1
Type inside the output and you'll see that it retains its properties.
1
x
In the above evaluation cell (Input) we can include a comment in the following format: (*
comment *). Nevertheless, it is recommended to include the comments in separate cells in
text format. Remember that you can do that by choosing Text or other format in the style box.
Practically anything can be used as a symbol. For example, let’s first generate a small red
sphere:
Graphics3DRed, Sphere, ImageSize 30
Then, substitute the symbol thus obtained, , and paste it below replacing b. Finally,
evaluate the cell.
3
Expand1 b
b3 3 b2 3 b 1
$
3
Expand 1
3 2
3 3 1
Lists (List) are a fundamental concept in the WL. Lists or ranges are written
inside curly brackets. The list elements are separated by commas: {a, b, ...}. For
a description of list operations see: guide/ListManipulation. We’ll describe them
in more detail in later chapters.
We add InputForm to see the output displayed in the internal format used by the WL.
Matrices are represented as lists of lists.
mat InputForm
%%# &
9, 5, 3, 7, 3, 6, 1, 10, 10, 10, 6, 8, 8, 8, 6, 9, 7, 8, 0, 3, 10,
10
Remember that for assignments, a single equal sign "=" is used, while to
indicate an equality in an equation or a comparison, we will use a double equal
sign "==" that when typed will be automatically converted to “”.
b2 4 a c b b2 4 a c b
x , x
2a 2a
Next, we extract the second solution (note that both solutions are inside a list):
26
b2 4 a c b
x
2a
To verify that the previous result is correct, we use "expr . rule", replacing the x in the
equation with its values from the solution.
a x2 b x c 0 . sol
2
b2 4 a c b b b2 4 a c b
c 0,
4a 2a
2
b2 4 a c b b b2 4 a c b
c0
4a 2a
Although we can see that the substitution has taken place, it’s often convenient to
simplify it. For that we use Simplify[, where % enables us to call the result (Out)
of the last entry and evaluate the cell. This method is very useful in complex
situations where we want to check that Mathematica actually returns the correct
answer.
We can check that the equality holds and therefore the solution is correct.
Simplify
True, True
For further details about matrix operations you can consult the documentation:
guide/MatrixOperations.
75
$
Now we can assign a value to the independent variable and we’ll get the function value.
f3
0.180814
We could have also typed the previous two operations in the same cell (don’t forget in this
case to enter “;” at the end of each function). However, it’s better to use one cell for each
function until you have enough practice. It will help you find mistakes.
fx : 0.2 Cos0.3 x2 ;
f3
0.180814
Now we are ready to visualize the Derivative (') of fx in a specific interval.
Plotf 'x, x, 0, 10
1.0
0.5
2 4 6 8 10
0.5
1.0
The same approach can be extended to several variables. In this case, we use the previously
defined f x function.
gx, y : fx 2.3 Exp 0.3 y2
Now we can present the result in a 3D graph with Plot3D. We use the option PlotRange
All to display all the points in the range for which the function is calculated. Remove the
option to see what happens.
Plot3Dgx, y, x, 2 , 2 , y, 2 , 2 , PlotRange All
28
12 3 &
12
To find out how many times the word “evolution” appears compared to the word “selection”
we use the function StringCount, specifying the chosen word or text.
StringCounttxt, "evolution"
4
351
Apart from that, you can also use functions created by other Mathematica users
located in the Wolfram Function Repository:
https://resources.wolframcloud.com/FunctionRepository. They can be accessed
using ResourceFunction:
We look for the functions related to astronomy (the output is not shown).
ResourceSearch"Astronomy"
BlackBodyRadiation`
BlackBodyProfile MaxPower PeakWavelength TotalPower
The following package function calculates the peak wavelength of a black body for a given
temperature.
PeakWavelength5000 Kelvin
1.5.1 Graphics
One of the most remarkable aspects of Mathematica is its graphical capabilities.
The program’s tool for 2D graphics can be accessed by pressing +D or by
selecting in the menu bar Graphics Drawing Tool.
The most commonly used functions to represent graphics are: Plot, Plot3D, and
30
The names that are shown can give you a hint about the type of plot they generate.
Click on the chosen name to get detailed information. In some cases, you will be
referred to the plot options.
The graphical functions don’t end there. The ones that include the word Chart are usually
related to statistical graphs. Similarly to the previous case, you can use the expression:
? Chart
Here we use a figure known as the “Utah Teapot”, included in ExampleData, to make a 3D
bar chart.
g ExampleData"Geometry3D", "UtahTeapot";
In the following example we use the options AxesLabel to add labels to the axes and
PlotLegends “Expressions” to show the represented functions:
PlotSinx, Cosx, Tanx, x, 0, 2 Pi,
AxesLabel "x", "fx ", PlotLegends "Expressions"
fx
4
2 sin'
cos'
x
1 2 3 4 5 6
tan'
2
4
40
1500 60 000
50 000
30
1000 40 000
20
30 000
500 20 000
10
10 000
0 0 0
0 10 20 30 40
We can even find the intersection points of a circumference and a parabola and graphically
represent them. To write special symbols such as “” , the best thing to do is to use
Mathematica’s palettes, which we will discuss later on.
3
pts Solve2 2 1 2 2 0, ,
2
1 1 1 1 1 1
x 5 5, y 1 5 , x 5 5, y 1 5 ,
2 2 4 2 2 4
1 1 1 1 1 1
x 5 5, y 5 1 , x 5 5, y 5 1
2 2 4 2 2 4
Show
3
ContourPlotx2 y2 1, y 2 x2 0, x, 1.5, 1.5, y, 1.5, 1.5,
2
GraphicsRed, PointSizeMedium, Pointx, y . pts
1.5
1.0
0.5
0.0
0.5
1.0
1.5
1.5 1.0 0.5 0.0 0.5 1.0 1.5
The next cell displays the surface cos x sin y bounded by the ring 1 x2 y2 4 (To write
“” you can type “<=”). Note that to set the region where the function exists, we use the
RegionFunction option.
32
Plot3DCosx Siny, x, 2, 2, y, 2, 2,
RegionFunction Functionx, y, z, 1 x ^ 2 y ^ 2 4
You can interact with 3D graphics: Click on the graph to rotate it, with + click
(or -click) you can zoom, and with +click you can move it around.
The code below generates the famous Lissajous curves. Using Grid we can show a two-
dimensional mesh of objects, in this case graphs. We also use the Tooltip function to
display labels related to objects (not only graphics) that will be shown when the mouse
pointer hovers over the objects. Here, when the mouse pointer is over the chosen curve,
you’ll see the function that generated it.
Other functions that do not include Plot or Chart but that are also useful for
creating graphs are Graphics, Graphics3D, and ColorData, as the following
example shows.
Representation of the water molecule.
Graphics3DSpecularityWhite, 50, ColorData"Atoms", "H",
Sphere0, 0, 0, .7, Sphere1.4, 0, 0, .7,
ColorData"Atoms", "O", Sphere.7, 0, .7, Lighting "Neutral"
If you want to learn more about graphics, you can select the relevant text with the
cursor and press <F1>. For example, type: tutorial/GraphicsAndSoundOverview,
select it and press <F1>.
1.5.2 Images
Image processing has experienced a significant improvement. When clicking on an
image you will see a menu with numerous options (Figure 1.16).
The following command loads an image.
ExampleData"TestImage", "Lena"
The Image Assistant appears below the image. Click on any of available options. For
example, after clicking on more... we see the following menu (Figure 1.17):
Figure 1.17 Additional capabilities available from the “more... option” in the Image Assistant.
After selecting one of the Actions & Tools, a miniature version of the image appears where
you can check the effect on the original image. For example: When selecting invert colors,
you’ll see Figure 1.18.
After getting the desired effect, press Apply to modify the image. An alternative
way to do that is to select the function directly from the menu:
ColorNegate[image]. Copy it to an input cell and execute it. This last procedure
is the appropriate one if you want to create a program or explain how the image has
been modified. We will be using this method when we refer to image processing in
later chapters.
1.5.3 Manipulate
Mathematica has functionality to enable dynamic and interactive operations. One
of the most significant examples of this is the Manipulate function.
Type the Mathematica expression shown below. Notice how sliders are created for each of
the parameters being defined. Click on the + symbol included in the output to show the
buttons created by Manipulate.
ManipulatePlotSinfrequency x alpha, x, 2 , 2 ,
frequency, 1, 5, alpha, 0, Pi 2
download and open the files; later on you’ll be able to modify their code to suit
your needs or even make new ones. In Chapter 4, we’ll learn more details.
This example explores Coulomb’s law. This law describes the interaction between two
charges (based on ‘Potential Field of Two Charges’ created by Herbert W. Franke:
http://demonstrations.wolfram.com/PotentialFieldOfTwoCharges/).
ManipulateContourPlotq1 Normx, y p1 q2 Normx, y p2,
x, 2, 2, y, 2, 2, Contours 10,
q1, 1, 3, 3, q2, 1, 3, 3,
p, 1, 0, 1, 0, 1, 1, 1, 1, Locator, Deployed True
Move the locators around with the mouse to see how the potential fields change.
1.5.4 Gauges
Mathematica includes different types of customizable gauges that can behave
dynamically:
ThroughAngularGauge, VerticalGauge, ThermometerGauge, HorizontalGauge
42, 0, 100, ImageSize Tiny
100
40 60 80
60
20 80 , , ,
40 0 20 40 60 80 100
0 100 20
0
With just a few lines of code, we can generate two dynamic clocks synchronized with both,
our local time and a different time zone.
DynamicRefreshRow
ClockGaugeAbsoluteTime, PlotLabel Style"Local", Large, Bold,
ClockGaugeAbsoluteTimeTimeZone 9,
PlotLabel Style"Tokyo", Large, Bold, UpdateInterval 1
If you find the previous example difficult to understand, remember that you can always
access the documentation to familiarize yourself with the functions used to generate the
clocks.
3
x3
3 x3
Now assign a the value of 2 in the first cell. What happens when you re-execute the
third instruction (a b)?
9
38
f 3
If you really want to remove all the information and not only the variables, the best
way is to quit the session. This requires that you exit Mathematica (actually it
requires that you exit the program Kernel). You can do that in several ways: With
File Exit or if you don’t want to exit the notebook use either Evaluation Quit
Kernel or in an input cell type Quit[]. Once you have exited, the program removes
from memory all the assignments and definitions. When you execute the next
command the kernel will be loaded again.
Although Mathematica tries to maintain compatibility with notebooks created in
previous versions, it is not always complete so you may have to make some
modifications. When you open for the first time a notebook created in a previous
Mathematica version, the program will offer you the possibility of checking the file
automatically; Accept it and read the comments that you may receive carefully. In
many cases, Mathematica will modify expressions directly and let you know about
the changes made giving you the option to accept or reject them. In other cases it
will offer suggestions.
1.5.6 Entities
The Wolfram Language (WL) contains real-world data accessible through the
entity framework, describe in detail in Chapter 4.
For example, to find out the population of Japan, we would just ask Mathematica for the
population value corresponding to the “Japan” entity. Using (Insert Inline Free-
form Input) we write “Japan” and “Population” inside EntityValue:
EntityValue Japan , Population
1.5.7 Wikipedia
We can even obtain information from Wikipedia using the function WikipediaData.
"
4, 3, 8, 0, 4, 1, 3, 7, 6, 4,
7, 2, 7, 2, 5, 2, 0, 9, 8, 9,
8, 1, 6, 4, 8, 5, 8, 0, 6, 7,
4, 5, 8, 4, 3, 1, 5, 1, 9, 9,
9, 2, 4, 7, 3, 1, 9, 2, 9, 6;
Use the command Classify to teach the program to associate the handwriting to a certain
digit based on the examples. This function uses statistical criteria to assign a weight to each
potential match.
digits ClassifylabeledData
ClassifierFunction
0, 1, 2, 3, 4, 5, 6, 7, 6, 4
Note that the classification has been done correctly except in the case of 6 that has been
mistaken with the digit 4. The program assigns probabilities based on the stored data. You
can check that the probability assigned to the symbol is higher for 4 than for 6. Even a
person may not be sure whether the handwriting represents a 6 or a 4.
40
6 0.82
6 0.82
This chapter covers some of the concepts and functions that we need to
program in Mathematica using WL. It also explains some useful notions
for generating efficient code. In particular, we will learn how to code
using pure functions, very important to create compact programs that take
full advantage of the power of Mathematica. The upcoming sections also
include examples of some of the most commonly used programming
functions and describe the fundamental ideas behind package
development, essential to build new applications that incorporate user-
defined functionality. Finally, guidance will be provided on tools
available for large projects or to develop programs that can be run in the
cloud. This chapter may be the least fun, but it is important to have a
basic understanding of the concepts discussed in its pages to be able to
follow the rest of the book. If you are an advanced user you may skip it,
although we recommend that you at least skim through its contents in case
you find something useful.
% gives the last result. %% gives the result before last. ( times) gives the th
previous result.
AtomQ
True
Rational
Plus
Instead of x + y + z we write:
Plusx, y, z
#
With FullForm we can see the basic form of expressions. One head, Plus in this case, and
elements separated by commas inside brackets:
FullFormx y z
Plus, , #
Cells are also expressions. Shown below is a copy of this cell displaying its
contents. This was done by duplicating this cell, selecting the copy and clicking on:
.
Cell[TextData[{“Cells are also expressions. Shown below is a copy of this cell displaying its contents. This was done by selecting the
copied cell and clicking on: “,
StyleBox[“Cell”, FontWeight->”Bold”], “ “, StyleBox[“Show Expression”, FontWeight->”Bold”], “. “}], “Text AM”,
CellChangeTimes->{{3.6*^9, 3.6*^9}, {3.6*^9, 3.6396977*^9}, {3.6*^9, 3.6396978*^9}, {3.6*^9, 3.6*^9}, {3.6*^9, 3.6*^9}}]
True
SparseArray
True
Graph
We get the result of the operation but not the structure of the initial expression as we
wanted. This is because the calculation is done first and then FullForm is applied to the
result, just a number. To avoid the execution of the calculation we use HoldForm.
FullForm HoldForm1.5 Sin2 Pi 4
HoldFormTimes1.5`, SinTimes2, TimesPi, Power4, 1
The structure of any expression adopts a tree-like format. Here we represent the previous
expression in such a format using TreeForm.
expr HoldForm1.5 Sin2 Pi 4
2
1.5 sin
4
TreeForm expr
HoldForm
Times
1.5 Sin
Times
2 Times
Power
4 1
7
11
Graphics
List
RGBColor Circle
1 0 0 List 1
0 0
If you move the cursor over an individual frame you will notice that it displays the
result of the operations until that frame.
A small transformation will allow us to modify the expression so that instead of a red
circumference (Circle), we get a red circle (Disk).
gr . Circle Disk
At the core of the Wolfram Language (or ) is the foundational idea
that everything—data, programs, formulas, graphics, documents—can be
represented as symbolic expressions. And it is this unifying concept that
underlies the Wolfram Language symbolic programming paradigm.
$ are the ultimate atoms of symbolic data letters, digits, combinations of
letters and digits not starting with a digit, and special symbols such as: $, , , ,
, . Upper- and lower-case letters are distinguished. Once a symbol has been
created during a session, it will remain in memory unless explicitly removed. An
alternative is to use functions that automatically delete a symbol once it’s been
used.
If we have a list and we’d like to know the type of objects that it contains, we apply Head to
each of the list elements. This can be done using Map..
Integer, Rational, Real, Complex, Complex, Power, Symbol, List, Symbol, Graphics, Image
List
2 3 6 3 1 5 4 3 7
The previous list can be shown in traditional notation as a matrix, but we should not forget
that internally treats it as a list.
list TraditionalForm
2 3 6
3 1 5
4 3 7
If the notebook has been configured to display outputs using the traditional format (as in this
case), it’s not necessary to use //MatrixForm.
MatrixForm1, 2, 3, 4
1 2
3 4
Length gives the number of elements in the list and for list with sublists Dimensions can be
used to determine the length at each level:
Lengthlist, Dimensionslist
3, 3, 3
It’s important to distinguish operations between lists and note that they don’t
behave in the same way as operations between matrices, especially with respect to
multiplication.
This is an example of list multiplication. We write * to emphasize that it’s a multiplication,
but if we leave an empty space instead, the result would be the same.
a, b, c, d x, y
Here we multiply matrices, or if you prefer, a matrix by a vector. To indicate that this is a
matrix multiplication we use Dot or “.” which is the same). Note that “*” and “.” are
different operations.
,
In the mathematical rule for multiplying matrices a row vector is multiplied by a column
vector. is less strict and allows the multiplication of two lists that do not meet
the mathematical multiplication rule. It chooses the most appropriate way to perform the
operation.
x, y, z . a, b, c
#
#
In general, the sum of lists of equal dimension is another list of the same dimension whose
elements are the sum of the elements of the initial lists based on their position.
1, 2, 3 a, b, c x, y, z
1, 2, # 3
If we multiply a list by a constant, each element of the list will be multiplied by it. The same
happens if we add a constant to a list.
1, 2, 3, 4 k
2
3 4
1, 2, 3, 4 c
1 2
3 4
,
Or
list21 , list2 1
,
To extract a list element based on its position in the list we use Part[ ] or, in
compact form, [[]].
In this example, we show two equivalent ways of extracting the second element of a list:
Partlist2, 2, a, b, c2
,
In the case of a list of sublists, first we identify the sublist and then the element
within the sublist.
In the following example, we extract element 3 from sublist 2:
a1, a2, b1, b2, b3, c12, 3
b3
! " # $ #%
In the example below, we extract elements 2 to 4. Note that the syntax ;; means to
extract from to ".
a1, b1, c1
a1, a2, a3, b1, b2, b3, c1, c2, c31, All
a1, a2, a3
In this case, we use ReplacePart to replace a list element, specifically from sublist 2 we
replace element 2 that is b2 with c2.
ReplaceParta1, a2, a3, b1, b2, b3, 2, 2 c2
a1 a2 a3
b1 c2 b3
Next, we use to rearrange the elements in a list.
list a, b, c, d, e, f;
, , , , ,
Lists can also be used to specify intervals for graphical plots and in other functions that require
ranges as inputs.
RandomReal3, 3, 2, 3
2.95474 1.10704 2.41065
1.09333 2.03312 1.45064
The solutions ( ) are often displayed in list format. If we need a specific part of
the solution, we can use the previous command to extract it.
To extract the second solution of this second-degree equation:
sol Solve1.2 x ^ 2 0.27 x 0.3 0, x
0.4
Other functions very useful for list manipulation are Flatten, Take, and Drop.
With Flatten[ ] we flatten out nested lists. With Flatten[ ] we flatten the list to level .
Flattena, b, c, d, e
, , , ,
With Take[ ] we extract the first elements of the list, and with the last . With
Take[ {"}] we can extract elements to ". In the example below, we flatten out the list
before taking the first 5 elements:
TakeFlattena, b, c, d, e, f, g, 5
, , , ,
With Drop[ ] we remove the first elements of the list, and with the last . Drop[
%"&] removes elements to ".
Dropa, b, c, d, e, f, g, 3
, , ,
There are commands particularly useful when operating on lists. It’s easy to identify them
since they generally contain List as part of their names. You can check using the help
system:
? List
There are many more functions used to manipulate lists. We recommend you
consult the documentation:guide/ListManipulation.
1, 2, 3, 4
3
This data structure can be broken down into individual lists containing just its Keys or
Values. Keys must be unique.
To extract the value of an element, we can either use a single square bracket with the
element’s key ([key]) or Part with the position of the element in the list ([[position]]) :
assoc"sphere", assoc4
,
An alternative and more general method to extract values from association objects is to use
Lookup:
Lookupassoc, a, "sphere"
1,
With Normal, the association is converted to a normal expression (in this case a list).
Normalassoc
, ,
1, 2, 3
a 1, b 2, c 3
Common functions for list operations work directly on the keys in an association if “Key” is
added to the name:
assoc 3 c, 4 d, 1 a, 2 b
3 , 4 , 1 , 2
1 , 2 , 3 , 4
KeySelectassoc, OddQ, KeyTakeassoc, 2, 3, KeyDropassoc, 1, 2
A B C
1 3 4 2
2 4 1 3
You can extract parts from datasets in a similar way to the one used with lists. To extract the
element from “row 2” and “column C”:
data"2", "C"
3
Get the elements from an entire row and from all the rows for a particular column.
data"1", dataAll, "B"
A 3
1 4
B 4 ,
2 1
C 2
Wolfram documentation gives examples showing how to use a Dataset. If you want
to dig deeper into Mathematica’s capabilities for handling structured data, there is a
vast collection of computable datasets in the Wolfram Data Repository
(https://datarepository.wolframcloud.com/ ) that we will use later.
6 1 3 7 9
3 3 9 4 9
8 9 4 0 6
10 7 10 0 0
3 1 1 9 10
5, 1, 6, 9, 1
5, 1, 6, 9, 1
Matrix operations very frequently have matrices where only few elements have non-
zero values. These kinds of matrices are called sparse arrays. In these cases, it is
recommended to use SparseArray.
We build a tridiagonal matrix 55 whose main diagonal elements are 9s and its adjacent 1s.
s SparseArrayi, i 9, i, j ; Absi j 1 1, 5, 5
Specified elements: 13
SparseArray
Dimensions: 5, 5
1 1
2 2
3 3
4 4
5 5
1 2 3 4 5
We build a linear system of 5 equations of the form = , with and the
matrices previously defined.
We solve the equation and check the calculation time:
TimingLinearSolves, b
869 7 22 487 11
0., , , , ,
1560 520 39 520 1560
We repeat the same process but for a system of 50,000 variables and 50,000 equations, of
the form =, where and are called sLarge and bLarge, respectively. We use “;”
to avoid displaying the output since it would take too much space.
TimingbLarge RandomReal1, 50 000;
0., Null
10 000 10 000
20 000 20 000
30 000 30 000
40 000 40 000
50 000 50 000
1 10 000 20 000 30 000 40 000 50 000
We solve the system = and see that the calculation time is extremely short.
Timingxvec LinearSolvesLarge, bLarge;
0.03125, Null
We don’t show the result, but we can check that the answer is correct by verifying that
0. With Chop we eliminate the insignificant terms, unavoidable in numerical
calculations involving decimal numbers.
! " # $ #%
0
A frequently asked question is how to differentiate between "=" (Set) and ":="
(SetDelayed).
x RandomReal
y : RandomReal
0.921297
x x
0.
y y
0.129974
Symbol
Global`x
Assignment
0.921297
?y
Symbol
Global`y
Assignment
: RandomReal
If we want to remove the definition assigned to x from the global context we can use Clear.
Clearx
What has happened? When "=" is used in f exp, the result of evaluating expis
assigned tof immediately. From then on, whenever f is called, the program will
always replace f with the value of exp. In the previous example, the value of x was
stored right away, while in the case of y, what gets stored is the function that will
be executed every time it’s called. Because of that, when calculating y y,
RandomRealis executed twice, returning a different result each time.
The following expression generates a list of 5 random integers between 0 and 5. We call it
data1. These numbers have been stored and they will always be used anytime data1 is
called.
data1 RandomInteger5, 5
3, 1, 1, 0, 3
Now we type the same above expression but using “:=”. We name this new list data2.
From now on, whenever data2 is called, the expression that generates the 5 random
numbers will be executed anew. What is stored is the function RandomInteger5,5 not
the actual numbers it generates.
data2 : RandomInteger5, 5
The difference can be seen by calling and executing the above defined expressions.
data1
3, 1, 1, 0, 3
5, 3, 3, 1, 4
Remember: When using ":=" (SetDelayed) in f: exp the expression exp is not
evaluated until f is used in a posterior calculation, and it will be executed each
time f is called. However, if you use “=”, the function f exp will only be
evaluated the first time it is called; after that, every time f is evaluated it will be
replaced by exp.
The same role that “ =” y “:=” play when defining functions, “” and “: (or :>)”
play when making replacements:
Immediate replacement (Rule).
! " # $ #%
We can make dynamic assignments in which the symbol (make sure that in the
menu bar is checked) returns an entry
that changes dynamically as we make new assignments to it.
Let’s create a variable.
Dynamicdvar
dvar
If we now assign to dvar different values or expressions, you’ll see that the output above
keeps on changing.
dvar 25
5
3
3
We can also create a box with InputField and write any in it.
InputFieldDynamicdvar
dvar
Try to use any fx in the box above, for example Sinx, and you will see how the next
graph is updated. The process will repeat itself anytime you write a new function in the box.
DynamicPlotdvar, x, 0, 5, Filling Axis
though it may not be the most appropriate one. Because of this, if you are a
programmer we would like to encourage you not to replicate the procedural
paradigm when programming with . It will be worth the effort.
The usual method to define a function with a single argument in the Wolfram Language is:
fx : x2
4
f 2
4
2
Let’s see an example for creating a function to calculate the factorial of a number
using different programming styles without using the built-in factorial symbol “!”:
Using a procedural style, we could do as follows:
factorial1n :
temp 1; Dotemp temp counter, counter, 1, n; temp
factorial15
120
With we can create a function that virtually transcribes the definition of
factorial (note the use of “ ” in the first definition and of “: ” in the second one; the reason
will be explained later):
factorial20 1;
factorial2n : n factorial2n 1
factorial25
120
Notice that we combined “=” and “=:” to calculate the factorial. The use of “=” takes
precedence over “:=”.
It’s normal to use “:=”when defining functions. However, in many cases it is also
possible to use " =" or ":=" without affecting the final result.
Trace shows each step of the evaluation:
Tracefactorial22
factorial2 2 , 2 factorial2 2 1 ,
2 1, 1 , factorial2 1 , 1 factorial2 1 1 , 1 1, 0 , factorial2 0 , 1 , 1 1, 1 , 2 1, 2
We can use this example to show the difference between Unset (.) and Clear:
Definitionfactorial2
factorial2 0 1
factorial2 n : factorial2 1
! " # $ #%
factorial2 n : factorial2 1
Null
The functional method (using different instructions in each case) is much more
efficient.
We perform the same operation in three different ways and display the execution time. In
the first case we use Total, a built-in function. If available, these functions are usually the
fastest way to get things done in . Later on in the chapter, we will explain
Apply (or @@) in more detail.
AbsoluteTimingTotallist
In this example, we build a function to calculate the sum of the square roots of
consecutive integers : 1 + ...+ }, and apply it with 50.
In the functional style, we transcribe almost literally the traditional notation in
This approach is not only simpler but also more effective.
n
rootsumn : i
i 1
239.036
Why do we use “//N”? To find out, see what happens after removing it from the previous
function definition.
Another way of programming where displays its power and flexibility
is rule-based programming.
For example: To convert {{1 , 1 , 1 },..., { , , },...} into {1 1 1 , ....,
{ }, ...} we just create the corresponding replacement rule.
a1, b1, c1, a2, b2, c2, a3, b3, c3, a4, b4, c4 .
a, b, c a b c
a1 a2 a3 a4
, , ,
b1 c1 b2 c2 b3 c3 b4 c4
This rule works fine except when the list and its sublists are both of the same size. For
example, the list consists of three sublists and each of the sublists contains three elements:
{{a1, b1, c1},{a2, b2, c2},{a3, b3, c3}.
a1, b1, c1, a2, b2, c2, a3, b3, c3 . a, b, c a b c
a1 b1 c1
, ,
a2 a3 b2 b3 c2 c3
There are different ways to avoid this problem. One is to use Replace at level 1.
Replacea1, b1, c1, a2, b2, c2, a3, b3, c3,
a, b, c a b c, 1
a1 a2 a3
, ,
b1 c1 b2 c2 b3 c3
The effective use of pattern matching is very important for good programming. A pattern object
may consist of blanks: a single Blank (_) with a single underscore character, a BlankSequence
(__) with two underscores or a BlankNullSequence (___) with three underscores.
Clearf, g
1, f, fa, fa, b, gc1, c2 . f "replaced"
1, f, fa, fa, b, gc1, c2 . f "replaced"
1, f, fa, fa, b, gc1, c2 . f "replaced"
, ,
, ,
! " # $ #%
Many build-up functions are applied to different levels. Here Mapis used to show
how it works at different levels.
We have the below list:
data a, b, 1, 2, 3, 4, 5;
TreeFormdata
List
List List
List List
1 2 3 4 5
, ,
, ,
, ,
Have you realized that the previous pattern can be used to compute the factorial of a
number? For example, 5!:
ApplyTimes, Range5
120
, , , ,
, , , ,
Using @ in succession enables the creation of composite functions as shown in this example:
f g h x
f g h x
2 2 cos
Notice that f with Map is applied to each element of the expression while with Apply
actually changes the entire expression by replacing its head.
Mapf, a, b, c, Applyf, a, b, c
, , , , ,
, , ,
, , ,
Some functions, when applied to lists, operate on the individual list elements, e.g.
f[{, , ...] {f[], f[], ...}. In this case, the function is said to be Listable.
In the case of listable functions, Map is implicitly included in them as this example shows.
Sina, b, c
See also: MapAt, MapThread, Outer, Inner, Through, Convolve, and ListConvolve.
MapAtf, a, b, c, d, 2
, , ,
, , , , , #
, , , , , , ,
#
, ,
,
In the next example, we apply MapIndexed together with Labeled to identify the position of
each term.
MapIndexedLabeled, DSinx ^ 4, x, 4
, , #
In this example we use NestList to build an iterative formula for computing the future value
of an initial investment of at an annual interest rate for " years and show its growth year
by year. We assume an initial capital of 100 and an annual interest rate of 3% for 10 years.
interestn : 1 0.03 n
100, 103., 106.09, 109.273, 112.551, 115.927, 119.405, 122.987, 126.677, 130.477, 134.392
, , , ,
As mentioned previously, we will not always find the specific function for what we
want to do. However, there are versatile functions useful to know that we can use to
perform a specific computation. When operating with lists, FoldList is such a
function.
Here we use FoldList as an alternative to Accumulate.
RestFoldListPlus, 0, a, b, c, d, e
, , , ,
3
3
! " # $ #%
3
In this last case, instead of Function we have used the # symbol and to finish
defining the function we have added &at the end. Since the combination of # and
& represent dummy variables we don’t need to store them in memory, reducing the
computation time. Next we are going to see several examples using this notation.
This function squares its argument and adds 3 to it.
2 3 &x
2 3
1 3 &
3, 3
3, 3
func2 1, 2 1, 2, 17
Here is combined HoldForm and a pure function. (Remember that HoldForm holds an
expression unevaluated):
10
12 &i
i 1
385
10
HoldForm12 &i
i 1
12 22 32 42 52 62 72 82 92 102
676
1, 2, 3
Plot3Dx ^ 2 y ^ 2, x, 1, 1, y, 1, 1, ColorFunction Hue3 &
Examples of a pure function with multiples arguments (note the use of ):
1 ^ 2 2 ^ 4 &x, y
4
2
#n, ## represent, respectively, the nth and all the arguments supplied to the pure function:
Clearf
,
, ,
There are many ways to define a function in as shown below for the function
2 sin 3.
f1x : x ^ 2 Sinx 3; f11
sin 1 2
sin 1 2
sin 1 2
! " # $ #%
sin 1 2
sin 1 2
The combination of a pure function and Apply at level 1 (the short form for Apply[
,{1}] is ) gives a powerful method to operate with a list.
f 1 k 22 &;
In this example, we want to solve the equation below step-by-step expressing the result as a
function of x.
yx 2;
We want to solve for by moving to the right side. That is equivalent to subtracting from
both sides of the equal sign (note that is necessary to use parenthesis to apply the pure
function to the entire equation).
x & y x 2
2
2
Then, we draw three lines, red, green, and blue, each one representing the median, the mean,
and the geometric mean of the data, respectively. We use MapThread to define the
beginning and the end of each line.
lines
MapThread1, Line0, 2, 20, 2 &, Red, Green, Blue,
Mediandata, Meandata, GeometricMeandata;
Finally, we combine the data with the generated lines.
6
4
5 10 15 20
The previous method, with 1, can be used to calculate 2 . With Nest only the last term
is shown.
Nest1 1 1 &, 1, 10 N
1.41421
FixedPointList is similar to NestList except that it will execute until the solution converges.
FixedPointList1 1 1 &, 0.1
3.76868
In the examples below, we use different methods to simulate a random walk, also
known as the drunk man’s walk. Let’s suppose that a drunk man tries to cross the
street and each step he takes can randomly go in any direction. Many physical
phenomena, such as Brownian motion, are variations of random walks (The
built-in function is RandomWalkProcess.)
The function below is a very simple case of a random walk along a line. Each step has a
maximum length of 1 and can go toward both ends of the line with equal probability. To
simulate it, we generate random numbers between 1 and 1 (if it goes in one direction we
use + and in the opposite direction .) What will be the distance covered after steps?
RandomWalksteps : AccumulateRandomReal 1, 1, steps
We create a function to graphically represent the distance covered after steps. In this case,
we assume 500.
ListPlotRandomWalk500, Filling Axis
2
2
4
Random walks can also be created with a combination of NestList and RandomChoice. The
individual steps can be forward or backward with equal probability. We randomly generate
1s and -1s with 1 meaning a step forward and -1 indicating a step backward. Starting at an
arbitrary point, we want to know the position after steps. In this case, we have chosen
ListLinePlot instead of ListPlot.
ListLinePlotNestList RandomChoice1, 1 &, 0, 500
5
5
10
15
20
25
a, a1, a11, a2, a3, a4, assoc, b, b1, b2, b3, b4, bLarge, c, c1, c2, c3, c4, counter, d,
data, data1, data2, data3a, data3b, dvar, e, expr, f , f1, f2, f3, f4, factorial1, factorial2,
func, g, gr, h, i, interest, j, k, lines, list, list2, list3, mat, n, newton, RandomWalk,
rootsum, rootsum2, s, sLarge, sol, steps, sum, temp, u, x, x0, xvec, x$, y, z
Symbols starting with $ that you haven’t defined may appear. They correspond to internal
assignments done by the program. In addition, if you used Clear[“Global`*”] earlier during
the session, you will have deleted the previous assignments but not their names. For
example: will not have any value assigned to it, but RandomWalk will. You still have not
cleared that assignment. Furthermore, both symbols still exist in the global context.
?a
Symbol
Global`a
Symbol
Global`RandomWalk
Definitions
RandomWalksteps : AccumulateRandomReal1, 1 , steps
As usual, if we want to eliminate all the previously defined assignments we can use:
ClearAll"Global` "
Remember that there’s another way to limit the use of variables to a specific
context: ! "
# .
! " # $ #%
With ClearAll[“Global`*”] all the assignments made will be deleted but not the variable
names themselves. They can be removed as follows:
Remove"Global` "
?a
MissingUnknownSymbol, a
MissingUnknownSymbol, RandomWalk
However, a more logical way to proceed is to avoid global variables and use local
variables instead, that is, variables that only have meaning inside an expression.
Let’s define the following variable in a global context.
y 3
3
Let’s use y within a local context. To create local variables we will use Module or With. For
example, in the next expression, y appears, but in this case it’s a local variable, defined
using With, and its use is restricted to operations inside the defined expression.
f1x : Withy x 1, 1 y y ^ 2
21
21
Nevertheless, that doesn’t affect the global variable that will still be 3.
y
3
The main difference between both functions is that with Module we explicitly
indicate what variables are local while when using With, replacement rules are
assigned to the local symbols, that is: With 0 , 0 , , . In
many cases, you can use them interchangeably.
Another related function is Block, very similar to Module. The main difference is
that while the latter creates temporary assignments for the specified variables
during the evaluation, the former suspends any existing assignments for those
variables and restores the assignments once the evaluation is done.
Moduley, Expand 1 y ^ 2
y$1786902 2 y$178690 1
16
The behaviour of both is similar, but in some occasions With is faster than Module:
TimingDoModulex 5, x;, 10 ^ 5
0.140625, Null
0.046875, Null
You can find an interesting comparison between them using Inactive on:
http://www.wolfram.com/mathematica/new-in-10/inactive-objects/optimize-
code.html
Catch returns the argument of Throw:
Catcha 1; b 2; c 3; Throwc; d 4; e 5
3
1, 2, 3, ,
211
A predicate is a function that can take two values: {True, False}. This type of
function is called Boolean. Figure 2.1 shows all the built-in symbols in
used to represent Boolean functions and their corresponding Wolfram
Language function.
The Wolfram Language provides various ways to set up , which
specify that particular expressions should be evaluated only if certain conditions
hold.
! " # $ #%
Predicates are used when defining conditions. These can appear under very different
situations. They can be used to logically combine multiple predicates:
22 4
True
True
x 3;
IntegerQx && EvenQx
False
To check whether two expressions are equivalent “===” can be used. Note that 3 y 3.0 in
are different kind of numbers.
1 33
True
False
If and Which use predicates for tests. A predicate can return either True or False:
The conditional statement can be realized in the Wolfram Language by If:
x 5;
IfPrimeQx, "Prime", "Composite", Whichx 1, a, x 2, b, x 5, c
Prime, c
Which and Switch are useful when dealing with more than two branches.
Which uses predicates, while Switch uses pattern matching:
Let’s use f to define a function whose behavior will be different depending on whether we
are using 1 or 2 arguments:
fx : 0.2 x ^ 2
1.8
225.
A frequently used format when defining conditions is “_h” to indicate that the replacement
will happen for any expression whose head is h as the following examples show:
Clearf, g
4
2
2, 4, , 1.41, 2,
3
Notice that Sqrt[2] is interpreted as a integer because the condition is applied to the atomic
expression (the integer inside of Sqrt):
MapHead, 4, 4, Pi, 1.41, Sqrt2, 2 3
Here is an example of finding the square roots of the only the positive integers in a list:
4, 2, 3.14, 9, 25, 16 . x ; x 0 && IntegerQx Sqrtx
In a function, the easiest way to limit a parameter n so that the function will not be
executed unless the parameter meets the condition is by typing n:
In this example n is required to be an integer.
Clearf
! " # $ #%
0.0976562
3.2, 2.1
10.24
3.2, 2
True
In the next example, we combine several conditions that must be met to apply the
binomial coefficients formula . There is a specific function for this calculation:
Binomial, but here we want to create it ourselves.
The function below calculates the binomial coefficients if is an integer, is an integer 0,
and (implying that n0).
binomialnInteger, rInteger ? NonNegative ; n r :
n r n r
We add a message that will be displayed if the conditions are not met.
3
Symbol
In the following case, we establish the condition that has to be either integer, rational, or
complex to return 2 . The function will be returned unevaluated in any other case.
squareofnumbernInteger nRational nComplex : n^2
9
9, , squareofnumber 1.3 , 7 24 , 1, squareofnumber 2 ,
4
squareofnumber , squareofnumber , , squareofnumber
We can also define conditions with the following functions: If, Which, Switch, and
Piecewise.
In this example, the function is defined using intervals.
Plot
Piecewisex ^ 3 6, x 2, x, 2 x 0, x ^ 2, x 0, x, 5, 5
20
4 2 2 4
20
40
60
80
! " # $ #%
The use of /; combined with FreeQ is particularly useful. For example, we create a function
that will consider everything that is not as a constant.
fc x, x : c fx, x ; FreeQc, x
3 2
3 2
0
0.5
1.0 0.5 0.5 1.0 1.5 2.0
0.5
2, 1
1.0
1, 2, 4, 5, 7
1, 2, 4, 5, 7
In this example, we use Cases to select the list elements of the form .
Cases1, x, x , x ^ y, x ^ y
10 , 10
The pattern “_.” makes it possible to define an argument that can be omitted. By using
x^y. (note the dot after the underscore), is also selected since the argument of can
be omitted.
Cases1, x, x , x ^ y, x ^ y.
10, 10 , 10
We use DeleteCases to eliminate those elements of a list that meet certain criteria.
We remove the elements that are decimals (we use Real, different from Reals that refers to
the real numbers).
DeleteCases1, 1, x, 2, 3, y, 3 5, 2.7, Pi, 9, y, Real
3
1, 1, 10, 2, 3, , , , 9,
5
We eliminate from the list those sublists containing 0 as their last term.
DeleteCases
1, 1, 0, 2, 3, 1, y, 3 5, .7, Pi, 9, z, a, 0
2, 3 1
3
, 0.7
5
, 9 #
Alternatives (|) allows you to specify a number of different choices so that an expression is
declared a match for the pattern when it matches any one of those choices:
Casesa, b, 2, c, 4, 2.3, R, "Hello", Integer Symbol
, , 2, , 4, *
If you want a match where a particular kind of object is repeated a number of times in this
case Repeated (..) can be used
, a, a, a, b, a, a, a, a . a .. x
Except[] can be used with a pattern object, which represents any expression except one
that matches .
! " # $ #%
0, 25
Including a second argument in Except[] allows you to represent any expression that
matches but not . Here, all integers except 0 are returned:
Cases0, 5, "Hello", Plot, 7.5, 8, Except0, Integer
5, 8
The functions to find patterns within strings are very similar to the ones we’ve
seen so far. They usually have “String” at the beginning of them. The main
difference is the use of “~~” to represent a sequence of string expressions.
StringCases[“ ”, ] will match substrings in that match . The next example
selects all the substrings that start with “ab”:
StringCases"aby mno abc abd bac xyz", "ab"
False
StringMatchQ[“ ”, ] checks whether contains the pattern . The string
below contains the sequence “ATG” so the function returns True:
StringMatchQ"AAGCTGCGTCGTAAATGCGAGT", "ATG"
True
MachinePrecision
40.
There are several functions to convert a decimal number into an exact one.
For example: Round returns the closest integer to Floor the closest integer to
that is less than or equal to , and Ceiling the closest integer to that is greater than or
equal to .
Round3.3, Floor3.3, Ceiling3.3
3, 3, 4
In the next example, we are going to see the difference between operating with
integers (or expressions composed of integers, such as fractions) and operating with
decimal approximations.
We create the following function:
fx : 11 x a
0.1 , 0.1 , 0.1 , 0.1 , 0.1 , 0.1 , 0.1 , 0.1 , 0.1 , 0.1 , 0.1 , 0.100002 , 0.100025 ,
0.100279 , 0.103066 , 0.133729 , 0.471014 , 4.18116 , 44.9927 , 493.92 , 5432.12
This effect is not associated to the way operates. It will happen with any
program performing the same operation using machine precision. For example, try to
replicate the previous calculation, replacing with a positive integer (for example 3), in a
program such as Microsoft Excel, and you will see that the errors multiply.
fx : 11 x 3
! " # $ #%
0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.299995, 0.299949, 0.299443,
0.293868, 0.232543, 0.442028, 7.86231, 89.4854, 987.34, 10 863.7
Rationalize converts a decimal number to the nearest rational with the smallest denominator.
Rationalize0.3
3
10
When used in the previous iteration we eliminate the numerical errors since
uses infinite precision for calculations without decimal numbers.
NestListf, Rationalize0.3, 20
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
, , , , , , , , , , , , , , , , , , , ,
10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10
Let’s show another more complicated example, where we will see as well the effect
of using decimal approximations instead of exact calculations.
Consider the following system of differential equations:
1 ' 1 2.5 1 2
2 ' 2 1 2
Initial Conditions : 1 0 1 0 0
We solve it using DSolve. Depending on the machine precision of your computer, you may
find numerical problems when visualizing the solutions. It can usually solved rationalizing
the decimal coefficients, then computes with integers and rationals using
infinite precision.
ClearAllx1, x2
2
20 40 60 80 100
20
15
10
3 2 1 1 2 3
x
x 1.
x 0.75
x 0.5
x 0.25
x 0.
x 0.25
x 0.5
x 0.75
x 1.
2.35045
The next function is a piecewise function, that is, it takes different values in each of the
intervals.
ClearAll"Global` "
Sin10 x
x0
x
1
0x1
fun x ;
Sin2000 x x2 1 x 2
Cosx
2x
x
1.0
0.5
10 5 5 10
0.5
1.0
0.001275015530
0.00127502
0.001275015530
We should not worry most of the time about choosing a specific computation
method. + default usually chooses the most appropriate one:
https://www.wolfram.com/algorithmbase/
However, we can always check if that is the case for a given problem by comparing
the results of different methods.
The calculation time is faster in this last case. It seems logical, since in the first
example the integral f1 is calculated 10 times, each time applying a different
integration limit, while in the second case f2 provides the solution to the integral as
a function of the integration limit x1, and then replaces the value of x1 directly in
the solution 10 times.
We repeat the same pattern using again “=” and the calculation is even faster.
Timing f3x1 IntegrateSinx ^ 3, x, 0, x1;
Tablef3i, i, 1, 10;
0.15625, Null
The reason behind this behavior is that for functions such as: Integrate, Simplify
and others, keeps a cache of previous results that it will use later if
necessary to make calculations more efficient:
http://library.wolfram.com/infocenter/Conferences/5832/
If we now delete the cache, the calculation takes longer:
ClearSystemCache;
The code below shows a traditional definition, each time f5, 5, 5, 5, 5 is computed,
the consumed time is the same:
ClearAllfOrd
fOrda1, a2, a3, a4, a5 : NIntegrate1 x1 x2 x3 x4 x5 ,
x1, 0, a1, x2, 0, a2, x3, 0, a3, x4, 0, a4, x5, 0, a5
0.197992, 910.019
Alternatively, we can combine =: and = for a faster computation. The function definition
below incorporates memoization (a technique by which previously computed results are
stored and reused to speed up the overall evaluation). Subsequent evaluations are much
faster, since f5, 5, 5, 5, 5 is stored in the definition itself (as a downvalue):
fMemoa1, a2, a3, a4, a5 : fMemoa1, a2, a3, a4, a5
NIntegrate1 x1 x2 x3 x4 x5 , x1, 0, a1,
x2, 0, a2, x3, 0, a3, x4, 0, a4, x5, 0, a5
0.225405, 910.019
Example: We use Compile to calculate the following function that only accepts real values
as arguments:
cf Compilex, Real, x ^ 2 1 1 x Sinx
Argument count: 1
CompiledFunction
Argument types: Real
9.8696
"
"
"
"
"
CloudObject
https:www.wolframcloud.comobj4b9408f26e924419b55db959e5514c0d
The first time we execute it, will ask for our Wolfram ID and password as
shown in Figure 2.2:
Clicking on the hyperlink just created will automatically take us to the web page shown in
Figure 2.3:
! " # $ #%
We can deploy any function to the cloud. Figure 2.4 shows the deployment of
a dynamic interface:
CloudDeployManipulatePlotSinx 1 k x, x, 0, 6, k, 0, 2
CloudObject
https:www.wolframcloud.comobjbaeed00591cd413186bcbc06e4e7e75e
EndPackage[]
Binomialdistribution::usage
"Binomialdistributionn, r, p calculates the binomial probability distribu
under the conditions that n must be an integer, n 1, n r and 0 p
Begin"`Private`"
BinomialPackage`
BinomialPackage`Private`
End
EndPackage
BinomialPackage`Private`
Next, from the main menu bar select $ % . A new window in the package
environment will appear (Figure 2.5). After that, copy the previous cell to this new
notebook, turn it into an initialization cell ( % &')and, in
this case, save it as a package ($ ( % ) in the Data
folder located in our working directory. Use as the name the one defined in BeginPackage:
“Binomialpackage.wl” (remember to choose the extension wl or m).
! " # $ #%
In this environment we can test, modify, and even debug the package. Once we see it is
working correctly we will save it. A package is an ASCII file. It can also be modified using
a text editor such as Notebook (in Windows).
As the last step, we exit the session (to be precise: we exit the kernel) to start a new one.
This can be done without closing the notebook by choosing in the menu bar
)* or typing and evaluating:
Quit
Then the package will be available for all users that run Mathematica installed in this directory.
However, in this case we will define a specific directory.
Let’s make the folder Data our working directory.
SetDirectoryFileNameJoinNotebookDirectory, "Data"
F:\Mi unidad\Mathematica\MBN13\Data
To use the package we need to load it first (or, for testing purposes, execute it inside a
notebook). We can use either << or Needs, but in this case we choose the latter since it has
the advantage of only loading the package if it hasn’t been loaded previously.
Needs"BinomialPackage`"
One way to know what functions are available inside the package is as follows:
Symbol
At the time of loading the package, all its functions are executed, in this case just one:
Binomialdistribution.
Binomialdistribution5, 3, 0.1
0.0081
This chapter explores the use of dynamic functions for creating interactive
simulations (demonstrations) using the Manipulate and DynamicModule
functions. It also shows how to manipulate and process 2D and 3D
images to detect edges, remove backgrounds or identify specific image
elements. The creation of graphs and networks is also covered by giving
an overview of the Graph function. The last section puts some of the
previously discussed Mathematica’s capabilities into action by explaining
in detail the calculation of the period of a pendulum from a series of
images. All the functionality discussed in the chapter is illustrated with
relevant examples.
In the input cell above we click on the floating label “Replace cell with this input” that
appears right below our command. This way we obtain the function below that we can use
as a basic Manipulate template. Notice that a is a parameter that can take values between 0
and 10 with a default value of 1. Try moving the slide bar with the cursor.
ManipulatePlotSina x, x, 0, 10, a, 1, 0, 10
In some of the examples that follow we have added the options ContentSize and
ImageSize to reduce the output size.
The next example shows some of the many options of Manipulate. You can include a drop-
down button, but it will be automatically added when a parameter contains five or more
choices as shown below. In this case, we also have the possibility to choose several filling
alternatives for displaying a graph. We use PlotLabel as well to show the parameter value
when labeling the graph. By adding the option Appearance “Labeled”, we can display to
the right of the sliding bars the actual values of the parameters. If you’d like them to be
shown by default, you should add the option Appearance “Open”. We can also label the
parameters using the syntax: {{a, initial a value, “a label”}, minimum a value, maximum a
value}. Another interesting option is ControlType. For example, we can use Checkbox to
include check boxes. The syntax is: {a,{a1,a2}}, where {a1,a2} refers to the alternatives
available, a1 is the default option. When there are more than two choices, you can use
RadioButtonBar.
Manipulate
PlotExpa x Sinb x, x, 0, 10,
PlotLabel StyleStringForm"Expa x Sinb x ", 10,
PlotStyle Directivecolor, AbsoluteThicknessthickness,
Axes axes, a, 0.1, "Amplitude a", 0, 0.5, Appearance "Open",
b, 3, "Amplitude b", 1, 10, Appearance "Labeled",
axes, True, False,
color, Orange, Orange, Red, Yellow, Green, Blue,
thickness, 1, 3, 5, ControlType RadioButtonBar
ManipulatePlot
If we use Locator with Manipulate we will be able to move objects in the output
with the mouse. Additionally, all the information associated to those objects will be
dynamically updated. The syntax of Locator is: {{parameter, {x, y}},
Locator}.
In this example, we can move the three vertices of a polygon and see how the triangle area is
filled automatically.
Manipulate
GraphicsPink, Polygonpts, PlotRange 1.1, ImageSize Tiny,
pts, 0, 0, 1, 0, 0, 1, Locator
We are going to extend the functionality of the previous command to calculate and
display the area of the triangle, but first let’s introduce a way to call a function from
within Manipulate.
Any local variables that are used by a function should be passed into that function as an
argument. Because of that, the first example doesn’t work:
ClearAllf;
fx : Sina x;
ManipulatePlotfx, x, 0, 10, a, 1, 5
Use the option SaveDefinitions or we can use Initialization with the function that we would
like to evaluate first when Manipulate is executed. The SaveDefinitions option determines
whether external definitions required to display the content area are automatically included
in the output:
ClearAllf;
fx : x ^ 2;
Manipulatefx, x, 0, 100, SaveDefinitions True
Going back to the example of the triangle, to explicitly include the value of its area we have
to define a function that calculates it from the Cartesian coordinates of its three vertices: v 1 ,
v2 , and v3 .
areaTriangv1, v2, v3 :
AbsDetJoinTransposev1, v2, v3, 1, 1, 1 2
areaTriang3, 2, 4, 1, 7, 3
1
2
Now we include areaTriang in the previously typed command. Move the vertices with the
mouse to see how the area is dynamically updated.
ManipulateGraphicsPink, Polygonpts, PlotRange 1,
PlotLabel StringForm"Triangle Area `1`", areaTriangpts,
ImageSize Tiny,
pts, 0, 0, 1, 0, 0, 1 2, Locator,
Initialization areaTriangv1, v2, v3 :
AbsDetJoinTransposev1, v2, v3, 1, 1, 1 2
In this example, we use BSplineFunction to generate a B-Spline curve associated with all
the displayed points. If we use LocatorAutoCreate True, we will be able to add or
remove points using +click (+click in OS X).
Manipulate
ShowGraphicsPointp, Green, Linep, ImageSize Small,
ParametricPlotBSplineFunctionpt, t, 0, 1,
p, pts, Locator, LocatorAutoCreate True,
Initialization pts 1, 5, 3, 2, 6, 4
Dynamic and DynamicModule are other functions used for dynamic applications.
Add Dynamic around bits that can be updated independently of the rest:
ManipulateShowContourPlotCosa x Cosa y, x, 0, 1, y, 0, 1,
GraphicsDiskDynamicp, 0.02, a, 10, 0, 20, p, 0, 0, 1, 1
DynamicModule exhibits many of the same scoping behaviors as Module, plus a few extra
features:
DynamicModulex 0.5, SliderDynamicx, Dynamicx
, 0.5
9
The example that follows shows how to use it to display some of the different control types
available in Mathematica.
In this example, we use derivatives to show the tangent at the selected point for different
curves. This type of visualization is useful to represent the tangent to a curve and introduce
the concept of the derivative.
ManipulatePlotfx, fx1 f 'x1 x x1 , x, 2 Pi, 2 Pi,
PlotRange 2, 2, Epilog PointSize0.025, Pointx1, fx1,
x1, 2 Pi, 2 Pi, f, Sin, Cos, Tan, Sec, Csc, Cot
We can check the image size in pixels. It consists of a n m grid of triplets, each one
representing a pixel.
ImageDimensionsgiralda
635, 635
We adjust the contrast and the brightness (if you prefer, instead of copying and pasting the
image, just type “giralda”).
ImagePartition splits an image into a collection of subimages of a certain size. In this case,
we apply the function to a picture of Machu Picchu:
ImageStich enables us to stitch together a list of images by merging overlapping images into
a single one. This function is one of the latest additions to Mathematica 13. The example
below is included in the function documentation:
This example, available in the documentation, shows how to identify and highlight
sickle cells using a combination of the Binarize, ComponentMeasurements and
HighlightImage functions in Mathematica.
i ;
Then, compute centroids and the radii of disks with the same area as those centroids only for
the largest components of the image:
cells ComponentMeasurementsb, "Centroid", "EquivalentDiskRadius",
AdjacentBorderCount 0 && 50 Area 1200 &
7 196.016, 146.391, 17.0101,
8 141.599, 146.897, 16.982, 11 88.4614, 123.396, 15.7367,
12 218.991, 111.964, 18.412, 13 51.7714, 105.796, 16.9163,
14 141.656, 108.643, 16.6412, 16 178.551, 88.5622, 16.9257,
17 91.3829, 86.464, 16.8125, 18 223.054, 74.4934, 15.5843,
21 115.626, 54.4298, 14.9057, 22 78.1822, 51.4477, 15.0121,
23 197.218, 45.1391, 17.7697, 25 163.209, 31.0179, 15.4818
Finally, highlight the selected components:
Can you identify the location of the common points in these two images? (Images
available in the ImageCorrespondingPoints documentation.)
The output displays the matching positions between the two images.
matches ImageCorrespondingPoints i1, i2;
MapThread
Show 1, GraphicsYellow, MapIndexedInset 21 , 1 & , 2 &,
i1, i2, matches
3
14 1 2 14
20 8
8 37
16 3
42 35 21 42
1830 1
20 5 26 4 2
371621 19 11 24 6
19 518
7 30
35 , 40 25 13 7 11
22
26 4 39381023 34 24
13 3332 15 27 28 31
3141
6 25 34 9 413332 22
40 10 23
28 17
291236
39 38279
15 17
2912 36
3.4.1 3D Images
There are many functions (guide/3DImages) for 3D image processing, as this
example shows:
1.0
0.5
500 1000 1500 2000 2500 3000
0.5
1.0
The next function uses a filter to remove the noise component of the signal.
ManipulateWithres LowpassFilterdata, ,
GraphicsColumnSpectrogramres, ListLinePlotres,
, Pi, .01, Appearance "Labeled"
The image above corresponds to the painting “The Ambassadors” by Holbein. There’s a
skull in it. Can you see it?
With ImagePerspectiveTransformation, we can apply a transformation to see the image from
a different perspective and, combined with TransformationFunction, display the part of the
image that we want to visualize.
ImagePerspectiveTransformationambassadors,
0.5 0.87 0.05
TransformationFunction 1.34 2.7 0.43
0.62 0.2 0.525
3.4.7 Video
Mathematica has added numerous capabilities for video processing.
Generate a video of a 3D object rotating around the z axis:
g ExampleData"Geometry3D", "SpaceShuttle"1, 2 ;
A basic introduction to video and audio using the Wolfram Language can be found
at tutorial/VideoBasics and tutorial/AudioBasics:
https://reference.wolfram.com/language/tutorial/VideoBasics.html
https://reference.wolfram.com/language/tutorial/AudioBasics.html
Next we define the capacity of each edge and vertex. Finally we add labels to identify the
vertices.
a4 a3 a2 a1
We may have a graph representation in a certain format from which we would like to extract
information. We can do it as follows:
EdgeRulesg
2, 3, 4, 5
2, 3, 4, 6
a2 a3 , a3 a4 , a2 a1 , a1 a2
In the next example, we find the shortest way to link all the locations in Figure 3.1 with
fiber optic cable:
Maywood
14 28 10
Downers Oakbrook
27 17
Grove 25 Addison
23 11
20
10 21
Stickney
15
Wheaton
20 McCook
10
28
Bellwood
After creating the network graph with Mathematica (Figure 3.2, code not shown) and
naming it “g”, we find the minimum spanning tree containing all the villages (Figure 3.3):
fibernetwork FindSpanningTreeg;
Maywood
14 28 10
Downers Oakbrook
27 17
Grove 25
Addison
23 11
20
10 21
Stickney
15
Wheaton
McCook
20
10
28
Bellwood
In this example, we generate random graphs and search for cliques (Figure 3.4):
Grid TableHighlightGraphg RandomGraph8, 16,
Subgraphg, FirstFindCliqueg, 2, 4
The command below returns the edges and vertices of a dodecahedron in graph format:
Graphics3D
Opacity.8, PolyhedronData"Dodecahedron", "Faces", "Polygon",
g PolyhedronData"Dodecahedron", "SkeletonGraph"
,
Here we are trying to find the shortest path passing once through each vertex (this is the
famous Hamilton’s Icosian game):
h PathGraph FirstFindHamiltonianCycleg;
,
The example that follows is significantly more difficult than the previous ones. Our
objective is to find out the period of a pendulum by analyzing a sequence of video
images. The images were obtained with CurrentImage (if your computer has a
webcam you can use this function). The function adds a time stamp to the image,
information that we will use later on in the example:
ims , , , ,
, , , , , ,
, , , , ;
Source: Wolfram seminars for image processing, although currently, the seminar is not
available.
Here is the video sequence:
ListAnimateims
We begin by highlighting the most prominent lines to identify the ones corresponding to the
pendulum.
edges EdgeDetect ims;
In the images above, the pendulum lines are the ones close to the vertical. We can identify
them using Radon.
radons ImageAdjust Radon , Automatic, Pi 4, Pi 4 & edges ;
Binarize detects the brightest points and extracts their x-coordinates to obtain the angle.
xs ComponentMeasurements , "Centroid"1, 2, 1 &
Binarize , .9 & radons
89.5, 96., 111.5, 124., 125.5, 122.5, 112.5, 100.5, 87.5, 89., 95.5, 106., 121.5, 127.5, 124.
In the next step, we obtain the relationship between the positions and the time by converting
the x-coordinates to radians and combining them with the time stamps of the images.
2.90839
Next, we use FormulaData to find the formula to calculate the length of the pendulum:
After initially typing “ FormulaData“Pend ” then you will have the option to choose the
full function:
FormulaData"PendulumSmallOscillations", "Standard"
l 1
T 2 ,f , vmax 2 g l 1 cos0
g T
l
T 2
g
l 2.1012 m s2
WDF: https://www.wolfram.com/data-framework/
Using the Inline free-form input format (Ctrl+= in a US keyboard) we can know the type of
entity of the word written inside the box:
France
Out[ ]= France
So, France is an “Entity” of the type “Country”.
If you get an error message when evaluating the previous expression it may be due to
problems with your firewall or proxy configuration for accessing the Internet. Check your
Mathematica settings in: . If the issue is proxy-related,
access the help system for information about how to troubleshoot it:
tutorial/TroubleshootingInternetConnectivity.
A list of entities within a given type can be found using EntityList:
In[ ]:= EntityList"Country" Shallow
Out[ ]//Shallow=
We will use Shallow or Short to reduce the size of the output. Remove it to see the
complete output.
The entity type “Country” not only includes all the individual countries of the world. It also
contains classes (EntityClass ), groups of entities with a shared characteristic. For instance,
the entity class “Europe” represents all of the countries in Europe.
In[ ]:= EntityClass"Country", "Europe"
Out[ ]= Europe
Each entity has properties associated to it. Here are the ones associated to “Country”:
In[ ]:= Entity"Country""Properties" Shallow
Out[ ]//Shallow=
adjusted net national income , seasonal bank borrowings from Fed, plus adjustments , regions ,
adult population , obese adults , number of aggravated assaults , rate of aggravated assault ,
aggregate home value , aggregate home value, householder 15 to 24 years ,
aggregate home value, householder 25 to 34 years , 741
If we want to know the expression for a certain property (e.g., the adult
population) we place the mouse over “adult population” in the previous output and we see
that the exact syntax is:
In[ ]:= EntityProperty"Country", "AdultPopulation"
Since the above expression is very cumbersome to type every time we want to know the value of
a property, offers us several approaches to simplify our queries:
For example, combining just Entity and EntityProperty, we can obtain the same result:
In[ ]:= Entity"Country", "Japan"EntityProperty"Country", "AdultPopulation"
If you click on the symbol to accept the interpretation and then convert the cell to
standard form, the result will be a familiar one:
In[ ]:= Entity"Country", "Japan"EntityProperty"Country", "AdultPopulation"
WolframAlpha can help us find out the age interval of the adult population property. (You
can just write == Adult population):
In[ ]:= WolframAlpha"Adult population",
IncludePods "Input", AppearanceElements "Pods"
Input interpretation:
Out[ ]=
all countries, dependencies, and territories adult population
The following example uses EntityValue to find out the emissions of greenhouse gases
(GreenhouseEmissions) in Europe:
In[ ]:= EntityValue Europe COUNTRIES ,
In summary, the workflow for obtaining data (e.g., the diameter of Earth) is as follows:
Create the entity, using Entity:
120 Mathematica beyond mathematics
Out[ ]= Earth
Create the property, using EntityProperty:
In[ ]:= EntityProperty"Planet", "Diameter"
Out[ ]= 12 742.018 km
Summarizing :
Out[ ]= 12 742.018 km
Quite often, the last approach (with Ctrl+=) is the most convenient one because we don’t need
to worry about the exact syntax. However, for programing purposes, the standard form is
recommended to improve speed of execution and avoid ambiguity issues.
The price that we have to pay for such convenience is that sometimes may not be
able to “read our minds”.
Example: Let’s say we want to learn more about the special powers of Mercury, the Roman
god of trade, merchants, thieves, and travelers. Since mercury is an entity present in more
than one entity type (planets, chemical elements, periodicals, ...), if we just write “mercury”
in free form, ’s guess will be most probably that you are referring to a chemical
element. You can use EntityTypeName to verify it:
Out[ ]= Element
4. Accessing Scientific and Technical Information 121
To tell the program that we refer to the mythological figure, we need to press and
choose the appropriate option after clicking on “more”:
Out[ ]= Mercury
Now we can find out the actual name for that property:
In[ ]:= Mercury MYTHOLOGICAL FIGURE "Properties"
Out[ ]= alternate names , place of birth , body form , culture , place of death ,
entity classes , gender , image , locations , major temples , name , native names ,
patronages , special powers , subtype , symbols , type , literary references
And finally learn that the ancient god was famous because he could fly and move very fast:
In[ ]:= Mercury MYTHOLOGICAL FIGURE "SpecialPowers"
Sometimes, even after going through all the options, we may not see what we are looking
for. The reason for this is that not all of the potential interpretations are included. To find
them use:
In[ ]:= SemanticInterpretation"Mercury", AmbiguityFunction All InputForm
Out[ ]//InputForm=
AmbiguityList[{Entity["Element", "Mercury"], Entity["Planet", "Mercury"], Entity["Chemical",
"Mercury"],
EntityClass["MannedSpaceMission", "ProjectMercuryMannedMission"], Entity["Mineral", "Mercury"],
Entity["Mythology", "Mercury"], Entity["Periodical", "TheMercuryAUSHobart158944"],
Entity["Periodical", "TheMercuryZAFGreyvilleDurban156816"], Entity["Periodical",
"TheMercuryPAPottstown14607"]}]
Note that one of the interpretations refers to an EntityClass. A function that
represents a class of entities identified by a certain characteristic. In this case, the class is
“ProjectMercuryMannedMission” included in the “MannedSpaceMission” entity type.
“MannedSpaceMission” contains the following entity classes:
In[ ]:= EntityClassList"MannedSpaceMission" Shallow
Out[ ]//Shallow=
alternate names , callsign , command and service module callsign , operator countries , crew ,
description , disciplines , distance traveled , Earth landing date , entity classes , 20
The entities included in the class “ApolloLandings” and the graphical representation
corresponding to the “LunarLandingDate” property are:
122 Mathematica beyond mathematics
Apollo 14
Apollo 15
Out[ ]=
Apollo 11 Apollo 16
Apollo 12 Apollo 17
This mission was led by the astronaut Neil Armstrong born on:
Additional possibility: the Entity framework can also be used to construct and execute queries
for relational databases (https://reference.wolfram.com/language/tutorial/RelationalDataba-
sesQuickStart.html .)
For additional information about entity types visit: https://reference.wolfram.com/lan-
guage/guide/EntityTypes.html.
If you are interested in knowing the sources of Wolfram Knowledgebase (https://www.wolfram-
.com/knowledgebase/ ) there are several ways:
You can find them in the help for the type of entity on which you want to obtain the information. If the
information is available for that entity you will find it at the end where it says “Related links”. For example,
if you consult the Planet entity type help “Related links”, it will direct you to:
https://www.wolfram.com/knowledgebase/source-information/?page=PlanetData. However, not all entity
types include this information. If not, try the following:
Ask Wolfram Alpha (https://www.wolfram.com/knowledgebase/source-information/ ). For example: If you
want to know where the data for the MannedSpaceMission entity type comes from, write in Wolfram
Alpha: https://www.wolframalpha.com/input/?i=Manned+Space+Mission. You will notice that the sources
are indicated at the end. If you then click on the icon, you will see:
4. Accessing Scientific and Technical Information 123
By hovering the mouse over the list elements, you will see the details for each of the cities.
If you prefer to see them explicitly, just type the following:
In[ ]:= salamanca
SemanticInterpretation"Salamanca",
Entity"City", , AmbiguityFunction All
Out[ ]= AmbiguityList
Salamanca , Salamanca , Salamanca , Salamanca , Salamanca , Salamanca , Salamanca
Alternatively, we can use Interpreter (this function can be used any time there is ambiguity
risk).
In[ ]:= Interpreter"City""Salamanca, Spain"
Out[ ]= Salamanca
Notice that the previous output displays the units explicitly, in this case . To see the
output without units, use QuantityMagnitude.
In[ ]:= QuantityMagnitudeCityData
Entity"City", "Salamanca", "Salamanca", "Spain" , "Population"
Out[ ]= 159 754
If you wish to know all the information (properties) for a given entity of a computable data
function (DF) you can use the syntax:
Transpose[{DF[“Properties”], DF[entities, #]& /@ DF[“Properties”]}]
In the example below, we find all the information available in CityData for the city of
Salamanca, Salamanca province, Spain: {"Salamanca","Salamanca","Spain"}.
4. Accessing Scientific and Technical Information 125
The following function displays a table with the entities, properties, and classes available for
the specified collection:
In[ ]:= DataTablefun Symbol : Moduledata,
data
Stylefun, "Section",
FontFamily "Lucida Grande", FontSize 14, SpanFromLeft,
"", "", " ",
"Entities", Lengthfun, AppendRandomChoicefun, 2, " ",
"Properties", Lengthfun"Properties",
AppendRandomChoicefun"Properties", 2, " ",
"Classes", Lengthfun"Classes",
AppendRandomChoicefun"Classes", 2, " "
;
Griddata, Frame All
126 Mathematica beyond mathematics
" '
Entities 249 Barbados , Ireland ,
Out[ ]=
Properties 223 ForeignRegisteredShips, HIVAIDSPopulation,
Classes 337 australia extended ,
Apart from asking for information about many different entities, you can also
query the program about formulas:
For instance, if you need the formula for relativistic kinetic energy, you can type:
In[ ]:= FormulaLookup"Relativistic Kinetic Energy"
Out[ ]= KineticEnergyRelativistic
Of course, you can always use Free-form input by typing “=” at the beginning of an input
cell followed by the relevant words in plain English.
FormulaData"KineticEnergyRelativistic"
1
Out[ ]= 1 2 1 ,
1 2 %2 1
also has the capability to transcribe a given expression using SpokenString:
In[ ]:= transcribe SpokenStringSqrtx y z
In the next example, we use DictionaryLookup to search for English (other languages are
also available) words starting with “sh” and ending in “y”:
In[ ]:= DictionaryLookup"English", "sh" "y"
Out[ ]= shabbily, shabby, shadily, shadowy, shady, shaggy, shakily, shaky, shallowly, shamefacedly,
shamefully, shamelessly, shandy, shanty, shapelessly, shapely, sharply, shatteringly, shay,
sheeny, sheepishly, sherry, shiftily, shiftlessly, shifty, shimmery, shimmy, shinny, shiny,
shirty, shiveringly, shivery, shockingly, shoddily, shoddy, shortly, shortsightedly, shorty,
showery, showily, showy, shrewdly, shrilly, shrinkingly, shrubbery, shrubby, shyly
Using the program, we can also identify a language, transliterate expressions to different
alphabets, translate between languages, and even create a button that when pressed will
return a spoken representation of the argument:
In[ ]:= text ", ?";
Out[ ]= Russian
4. Accessing Scientific and Technical Information 127
Out[ ]= Press
Next, we are going to obtain the phonetic transcription (in American English) of the first
part of this sentence. We use SpokenString to split the phrase into words and the property
"PhoneticForm", from WordData, to get the phonetic transcription of each word. Since
the output is a list containing the words’ phonetic transcriptions, we apply Row[list, “ ”] to
join the elements substituting the commas with blank spaces.
In[ ]:= RowWordData , "PhoneticForm" &
StringSplit"we are going to obtain the phonetic transcription", " "
Out[ ]=
Out[ ]= Creator King John of England, his barons, and Stephen Langton,
Date Year: 1215 , Language English, Source English translation of Magna Carta
Out[ ]= Preamble:,
John, by the grace of God, king of England, lord of Ireland, duke of Normandy and Aquitaine,
and count of Anjou, to the archbishop, bishops, abbots, earls, barons, justiciaries,
foresters, sheriffs, stewards, servants, and to all his bailiffs and liege subjects, greetings.
We can perform many different type of analyses using the Wolfram Data
Repository. The example below shows the visualization of the evolution of
COVID-19 related cases and deaths in selected countries in the world.
During the pandemic, the “Epidemic Data for Novel Coronavirus COVID-19” was updated
daily with detailed information for provinces in Australia, Canada, and China and counties
and states in the US. For the rest of the world, the information was available at the national
level:
In[ ]:= ResourceUpdate"Epidemic Data for Novel Coronavirus COVID19"
Out[ ]= ResourceObject
We can also visualize the estimated deaths per 100,000 people by country (choosing the 30
countries with most deaths due to the pandemic):
In[ ]:= DateListLogPlot
CalloutTooltip10 ^ 5 2 1"Population", CommonName 1, 1 &
Normal ResourceData
"Epidemic Data for Novel Coronavirus COVID19", "WorldCountries"
TakeLargestBy Deaths"LastValue" &, 30
All, Country, Deaths &, Sequence
PlotRange All, GridLines Automatic, AspectRatio 1.5,
ImageSize 500, PlotLabel "Estimated Deaths per 100,000 People log \
scale"
With computable data we can have access to historical as well as real-time weather
information: WeatherData, WeatherForecastData, AirTemperatureData,
AirPressureData, WindSpeedData, WindDirectionData, WindVectorData.
The [“ 0, “ 0,{ }] returns the value of the
variable specified in “ ” for a given location and period (by default it
returns the most recent value). Of course you can always use the Free-form input
130 Mathematica beyond mathematics
AirTemperatureData
Out[ ]= 17. C
Notice that the function used by to get the temperature is AirTemperatureData.
By default, it will provide you with the temperature of your location (actually your IP
address location). This information is obtained from weather stations affiliated with the
World Meteorological Organization (WMO). You can use the same function to find out
from which weather station you got the air temperature reading.
In[ ]:= AirTemperatureData"StationName"
Out[ ]= LESA
Out[ ]= LESA
In this example, we get the average daily temperature in Salamanca (Spain) from January 1,
2016 to January 1, 2022 and represent it graphically. Notice the seasonality.
In[ ]:= DateListPlotAirTemperatureData
25
20
15
Out[ ]= 10
5
The graph below displays the range of daily temperatures over the past 180 days that
corresponds to that station code.
4. Accessing Scientific and Technical Information 131
30
20
Out[ ]= 10
The following function generates a series of weather-related forecasts for the week after:
In[ ]:= Manipulatedata WeatherForecastData
Entity"City", "Salamanca", "Salamanca", "Spain";
DateListPlotNormaldatag . Intervalx Meanx,
FrameLabel Automatic, g, "Temperature", "WindSpeed",
"WindDirection", "PrecipitationAmount", SaveDefinitions True
The final example of this section generates 100 pseudo-random geo position points on the
surface of Spain, obtains their most recent win vector readings from the nearest weather
station, and finally generates a stream plot:
H He
Li Be B C N O F Ne
Na Mg Al Si P S Cl Ar
K Ca Sc Ti V Cr Mn Fe Co Ni Cu Zn Ga Ge As Se Br Kr
Rb Sr Y Zr Nb Mo Tc Ru Rh Pd Ag Cd In Sn Sb Te I Xe
Out[ ]=
Cs Ba Hf Ta W Re Os Ir Pt Au Hg Tl Pb Bi Po At Rn
Fr Ra Rf Db Sg Bh Hs Mt Ds Rg Cn Nh Fl Mc Lv Ts Og
La Ce Pr Nd Pm Sm Eu Gd Tb Dy Ho Er Tm Yb Lu
Ac Th Pa U Np Pu Am Cm Bk Cf Es Fm Md No Lr
If you click on an element symbol, you will see the color code assigned by the function.
Let’s take a look at the properties available in ElementData:
4. Accessing Scientific and Technical Information 133
atomic number , atomic radius , atomic symbol , block , boiling point , 110
We can use Dataset to display all the information associated with an entity and
DeleteMissing to eliminate those properties without information. We can also include the
option MaxItems to limit the maximum number of items displayed simultaneously.
The following function tells us the abundance (as a fraction of the mass) of the different
elements in the human body:
In[ ]:= humanabundance
TableQuantityMagnitudeElementDataz, "HumanAbundance",
ElementDataz, z, 92;
We keep only those whose presence in the body is greater than 0.1% of the body mass. We
order them by comparing the first element of each sublist using a combination of Sort and a
pure function.
134 Mathematica beyond mathematics
61. oxygen
23. carbon
10. hydrogen
2.6 nitrogen
1.4 calcium
1.1 phosphorus
0.20 potassium
Out[ ]= 0.20 sulfur
0.14 sodium
0.12 chlorine
0.027 magnesium
0.026 silicon
0.0060 iron
0.0037 fluorine
0.0033 zinc
Formula: H2 O
Out[ ]= Molecule
Atoms: 3 Bonds: 2
In the example that follows, we show the caffeine molecular plot. We can move the plot
with the mouse to visualize the molecule from different perspectives:
In[ ]:= m Molecule caffeine CHEMICAL
Formula: C8 H10 N4 O2
Out[ ]= Molecule O
N
N
N
N Atoms: 24 Bonds: 25
Out[ ]=
With ChemicalReaction you can enter a reaction as a string, and then ReactionBalance is
used to balance it. Finally reaction balance equation is represented in the traditional form:
4. Accessing Scientific and Technical Information 135
Out[ ]=
A graph application below is shown. First the graph structure for a molecule is represented
and then a 6-cycle can find:
H
O H
C
H H
H
C C
N
H N C
C H
Out[ ]=
C C
N
O N
H C H
C
N C
Out[ ]=
C C
N
We can also compare the properties of two biomolecules:
In[ ]:=
Here is the list of the properties associated with the anatomical structure data:
4. Accessing Scientific and Technical Information 137
The function below returns the parts that form the lung:
This instruction returns all the available properties for dogs’ muscles:
Out[ ]//Shallow=
We can even answer questions such as: “What does the dog's femur bone connect to?”
Out[ ]=
We can inspect the properties of the DNA entity and visualize its structure:
Out[ ]=
We can obtain even more complex representations using the new functions BioSequence
and BioSequenceComplement:
In[ ]:= MoleculePlot3DMoleculeBioSequence"DNA", "CTGA"
Out[ ]=
GenomeLookup shows the chromosomes in which a specified DNA sequence appears along
with its position inside of them.
ClearAll"Global`"
140 Mathematica beyond mathematics
Out[ ]= CholesterolBiosyntheticProcess,
FattyAcidMetabolicProcess, LipidMetabolicProcess, MetabolicProcess
ProteinData[“protein”] returns the amino acids of a given protein. In this case, we display
the sequence of the protein A2M. We use Short to avoid displaying the complete output.
In[ ]:= ProteinData"A2M" Short
Out[ ]//Short=
MGKNKLLHPSLVLLLLVLLPTDASVSGKPQYMVLVPSLLHTETTEKGCVLLSYLN
LDKVSNQTLSLFFTVLQDVPVRDLKPAIVKVYDYYETDEFAIAEYNAPCSKDLGNA
ProteinData[“protein”,”property”] gives us the option to choose the property of the protein
we want to know about. As an illustration, to visualize the A2M protein we can use
“MoleculePlot” (this property is not yet available for all proteins at the time of writing).
In[ ]:= ProteinData"A2M", "MoleculePlot"
WolframAlpha (e.g., starting a input cell ==) or the Free-form input allows us to see an
overview of available results for the specified gene. (Cassel, B. J. (2018, October 16–19).
2 & " [Conference Presentation]. Wolfram Technology
Conference 2018)
4. Accessing Scientific and Technical Information 141
mus81 gene
Out[ ]=
Metabolic pathways are the key cycles by which food and fuel are converted into structures
and energy for biological systems. Here is the famous TCA cycle:
TCA cycle
Click “WolframAlpha results” choosing “PathwayTopology” and click in the upper right
corner PathwayTopolog(+) “Formatted Pod”
In[ ]:= WolframAlpha"TCA cycle",
IncludePods "PathwayTopology:MetabolicPathwayData",
AppearanceElements "Pods",
TimeConstraint 20, Automatic, Automatic, Automatic
There are many functions available for the computation of data related to earth
sciences and geography:
https://reference.wolfram.com/language/guide/EarthSciencesDataAndComputation.html
http://reference.wolfram.com/language/guide/GeographicData.html
The names of the functions enable us to identify those related to geography, almost all of
them except the ones starting with Geometric. Additionally, we can always access the help
documentation to see their syntax. Let’s take a look at some of them.
Using FindGeoLocation[] you can find your current geolocation of your computer (you can
find information about the criteria used to find your computer location in the Help) :
In[ ]:= FindGeoLocation
Next we use GeoGraphics to display a map of Salamanca (a City of Spain) and its
surroundings. Remember that in many cases you will find the Free-form input very useful as
well ( and type Salamanca).
In[ ]:= GeoGraphicsGeoDisk Salamanca CITY , Quantity50, "km"
GeoElevationData returns the elevation of a geographic site above sea level in meters,
informs us about the minimum and maximum elevations, and creates a relief plot:
In[ ]:= elevation
GeoElevationData GeoDisk Salamanca CITY , Quantity100, "km"
99.886
99.002
97.850
Out[ ]= 95.635
89.080
76.800
45.000
Out[ ]= CloudObjecthttps:www.wolframcloud.comobj047bd1e9759948e581817f69dd1561a2
We can now copy this newly created link to our website to make it accessible from any
device with web browsing capabilities:
< a href = "https://www.wolframcloud.com/objects/..." > City Map (html) < /a >
The application is now available to any user, although the first time we access it, we may
have to enter our Wolfram ID and password. However, we don't need to have
installed in our computers. Once we enter the required data (Figure 4.1) we get the
corresponding map (Figure 4.2).
The Earth, although not flat, is not really a sphere, it is a spheroid with the polar
radius being shorter than the equatorial
Terrestrial measurements are usually expressed as established by the The International Terres-
trial Reference Frame (ITRF):
https://www.iers.org/IERS/EN/DataProducts/ITRF/itrf.html
In[ ]:=
Out[ ]= ITRF00
Using the ITRF data the difference between the equatorial and polar radius is:
In[ ]:= GeodesyData"ITRF00", "Semiaxes"
Out[ ]= 21 384.7 m
In this example, we show how to calculate the distance between two locations on
the Earth: Where I am (Here) to Sydney
Out[ ]= 17 836.4 km
4. Accessing Scientific and Technical Information 147
This it is the the shortest distance (geodesic) that can be represented as follows:
In[ ]:= GeoGraphics
PointSizeLarge, PointHere, Pointloc, Arrow GeoPathHere, loc,
GeoGridLines Quantity10, "AngularDegrees",
GeoProjection "Orthographic"
Out[ ]=
Out[ ]=
GeoListPlot can also generate maps for locations outside the Earth. In the example below,
we visualize the landing of Apollo 11, the first manned lunar landing mission:
In[ ]:= GeoListPlot Apollo 11 MANNED SPACE MISSION ,
GeoRange All, GeoProjection "Orthographic",
GeoLabels True, LabelStyle White, ImageSize Small
We first define the cities that we want to travel to. It is better to add the country when typing
the city to avoid ambiguities in case there are several cities with the same name.
{"London, England", "Nuuk, Greenland", "Buenos Aires, Argentina", "Los Angeles, USA",
"Malmo, Sweden", "Ulan Bator, Mongolia", "Sidney, Australia"}
In[ ]:= cities
Out[ ]= London , Nuuk , Buenos Aires , Los Angeles , Stockholm , Ulaanbaatar , Sydney
Out[ ]=
Then I examined the magnitude of the geomagnetic field across the entire planet. The
magnitude at Valparaíso was very low indicating that there was actually no problem with
the magnetic sensor of my mobile phone.
In[ ]:= data GeomagneticModelData
90, 180., 90, 180., "Magnitude", GeoZoomLevel 2;
In[ ]:= GeoContourPlot
Flatten TableGeoPosition 16 i 45 8, 32 j 45 8
data33 i, 65 j, i, 1, 32, j, 1, 64, ContourShading True,
ColorFunction "TemperatureMap", PlotLegends Automatic
nT
62 631
53 641
Out[ ]= 44 650
35 660
26 670
The next series of commands generate a plot displaying the streamlines based on magnetic
direction fields from the South Pole to Valparaiso:
Number of points: 40
Out[ ]= GeoPosition Lat bounds: 80, 10
Lon bounds: 90, 50
4. Accessing Scientific and Technical Information 151
gm GeomagneticModelDatacoords;
norms QuantityMagnitudegmAll, "Magnitude";
declinations gmAll, "Declination";
vectors Transposenorms, declinations;
In[ ]:= GeoStreamPlotGeoVectorcoords vectors,
GeoBackground "ReliefMap", StreamStyle White
Out[ ]=
The intensity and position of the magnetic field changes slightly over time (see
GeomagneticModelData >Applications). Even the positions of the magnetic poles
themselves have interchanged several times in the past:
In[ ]:= TextSentencesWikipediaData"Geomagnetic Reversal"1 ;; 5
5.1 Importing/Exporting
A fundamental feature of Mathematica is its capability for importing and exporting
files in different formats. The commands we need for that are Import and
Export. They enable us to import or export many different types: XLS, CSV, TSV,
DBF, MDB, ...
A file can be imported or exported by typing the complete source or destination
path.
In Windows, to type the file location for import or export purposes use the following syntax:
“...\\directory\\file.ext” , as shown in the following example:
Import"...\\Mathbeyond\\Data\\file.xls"
If we want to use the working directory in which the files are located for importing
or exporting purposes, normally the easiest way will be to create a subdirectory
inside the directory where the notebook is.
The following function checks the directory of the active notebook.
In[ ]:= NotebookDirectory
We make the previous cell an Initialization Cell to ensure its evaluation before any
other cell in the notebook by clicking on the cell marker and choosing from the
menu bar: Cell Properties Initialization Cell). This means that all the data will
be imported/exported from the Data subdirectory. We may even consider “hiding”
the initialization cell by moving it to the end of the notebook. Remember that the
order of operations is not the same as it appears on screen. It is the order in which
the cells are evaluated. Therefore, even if an initialization cell is located at the end,
it will still be the first one to be executed.
The total number of available formats can be seen using $ImportFormats and
$ExportFormats.
In[ ]:= $ImportFormats Length, $ExportFormats Length
In this section, we are going to use, among others, the files available as examples in
a subdirectory named ExampleData created during the program installation. Some
of these examples are downloaded from www.wolfram.com (Internet connection
required).
We are interested in the ones related to statistics. The following function displays the name
of the example, a brief description, and the dimensions of its data matrix.
In[ ]:= Shallow
2, ExampleData , "Description", DimensionsExampleData &
ExampleData"Statistics", 5, 3
Out[ ]//Shallow=
AirlinePassengerMiles, Revenue passenger miles flown by commercial airlines., 24,
AirplaneGlass, Time to failure for airplane glass., 31,
AnimalWeights, Brain and body weights for 28 animal species., 28, 3,
AnorexiaTreatment, Anorexia data on weight change., 72, 3,
AnscombeRegressionLines, Anscombe's 4 regression line data., 11, 8, 106
5.1.1 Import
To import files use Import "pathfile.ext", "Elements" , where file is the name
of the file you want to import. If the file is in the Data directory previously
defined, you only need to type the file name. If it is located somewhere else you
will need to enter the complete path (remember that you can use
Insert File Path...). You can also import files directly from the
Internet. To do that, specify the desired URL in Import[“http://url”]. As
an option you can import only certain file elements, the ones specified by
Elements.
5. Data Analysis and Manipulation 155
Out[ ]= AtomicNumber, Abbreviation, Name, AtomicWeight 1., H, Hydrogen, 1.00793 2., He, Helium
Notice that the previous output has the following structure {{{ ...}}}. The reason is that
Mathematica assumes the file is an Excel spreadsheet that may contain several worksheets.
Using “Data”, 1, we tell the program to import the data from worksheet 1 (do not
confuse this Data with the working subdirectory Data that we have created earlier):
In[ ]:= examplexls Import"ExampleDataelements.xls", "Data", 1
AtomicNumber Abbreviation Name AtomicWeight
1. H Hydrogen 1.00793
2. He Helium 4.00259
3. Li Lithium 6.94141
4. Be Beryllium 9.01218
Out[ ]=
5. B Boron 10.8086
6. C Carbon 12.0107
7. N Nitrogen 14.0067
8. O Oxygen 15.9961
9. F Fluorine 18.9984
We show below how to extract the first 5 rows of the worksheet and display them in a table
format.
In[ ]:= TableFormTakeexamplexls, 5
Out[ ]//TableForm=
AtomicNumber Abbreviation Name AtomicWeight
1. H Hydrogen 1.00793
2. He Helium 4.00259
3. Li Lithium 6.94141
4. Be Beryllium 9.01218
The next function imports a single column. In this case, we get the fourth column containing
the atomic weights.
In[ ]:= Import"ExampleDataelements.xls", "Data", 1, All, 4
We can also visualize the atomic weights of the imported data. Tooltip is included so that
when the cursor is over a point in the graphic, its corresponding atomic weight can be seen.
In[ ]:= ListPlotTooltipdataAll, 4,
Filling Axis, PlotLabel "Atomic Weight"
Atomic Weight
20
15
Out[ ]= 10
2 4 6 8
Try to copy and paste links to any of the numerous images available in:
https://earthobservatory.nasa.gov/.
Note: The examples using files imported from the Internet may not work if their web addresses have been
changed or the files have been deleted.
In[ ]:= Import"https:eoimages.gsfc.nasa.govimagesimagerecords7800078349
arctic vir 2012147 lrg.jpg"
Out[ ]=
With Import[..., “Elements”], we can find out what elements of the file are available to
import (e.g., ImageSize).
In[ ]:= Import"https:eoimages.gsfc.nasa.govimagesimagerecords7800078349
arctic vir 2012147 lrg.jpg", "Elements"
Out[ ]= BitDepth, CameraTopOrientation, Channels, ColorMap, ColorProfileData, ColorSpace,
Comments, Data, DateTime, Exif , FlashUsed, GeoPosition, GPSDateTime,
Graphics, Image, ImageSize, IPTC, MakerNote, MetaInformation, RawData,
RawExif , RawIPTC, RawXMP, RedEyeCorrection, Summary, Thumbnail, XMP
In[ ]:= Import"https:eoimages.gsfc.nasa.govimagesimagerecords7800078349
arctic vir 2012147 lrg.jpg", "ImageSize"
Out[ ]= 1500, 1500
From the URL address of a website we can limit the information selected to only
what we are interested in. A good practice is to first open the URL in a browser and
see what information suits our needs. After importing the web page, you will notice
that it will appear in Mathematica as a list of lists. Analyze how the information is
5. Data Analysis and Manipulation 157
structured. You need to find out what lists contain the information you want and
then use ... to extract it. Alternatively, you can also use the functions:
URLExecute or URLDownload.
Importing from databases is a frequent data import operation. For example:
importing from an Access mdb file.
We import the complete structure of the database:
In[ ]:= Import "ExampleDatabuildings.mdb", "Datasets", "Buildings", "Labels"
We use Short or Shallow to display just a few data elements (this is practical if there’s
only one table and it’s not very big).
In[ ]:= Import "ExampleDatabuildings.mdb" Short
Out[ ]//Short=
1
We import the records associated to several fields and display them in a table format:
In[ ]:= TextStyleGridTransposeImport
"ExampleDatabuildings.mdb", "Datasets", "Buildings", "LabeledData",
"Rank", "Name", "City", "Height" , Frame All , Small
For big databases (ORACLE, SQLServer, etc.), with a large number of tables and
thousands of records, it’s better to access them with an ODBC connection as we
will show later.
We extract the element in the third position. Notice that with Dataset we use [...] instead of
[[...]].
In[ ]:= poverty1, 3
Out[ ]= World
If you hover the mouse over World, the following message will appear on the screen:
Entity“Country”, “World”. Remember that in the WL the object type entities
(Entity ) have associated additional information that you can access directly, e.g., the
world population (Remember that % refers to the last output).
In[ ]:= "Population"
The function below shows the fields {“SeriesDescription”, “Value”, “Time_Detail”} for
the World entity. Notice that Select[#”GeoAreaName” “World”&] would not work since
we are referring to the entity “World” and not to the word “World”.
In[ ]:= povertyworld
povertySelect"GeoAreaName" Entity"Country", "World" &,
"SeriesDescription", "Value", "Time Detail"
We now group the selected data by years (“Time_Detail”) and display only the information
of the related year by clicking on it:
In[ ]:= GroupBypovertyworld, "Time Detail"
5. Data Analysis and Manipulation 159
5.1.3 Export
Out[ ]=
Then, we export it to a video format, in this case MP4. (Normally this operation may last a
few seconds).
In[ ]:= Export"example3DAnimation.mp4", example3DAnimation
Out[ ]= example3DAnimation.mp4
Now we can either manually locate the exported file and open it or call it directly from
Mathematica and have the system open it using the default application for MP4 files.
In[ ]:= SystemOpen"example3DAnimation.mp4"
5.1.4 Metadata
As we have previously seen, data from image files usually carry additional
information about the images themselves. In this case, we download a file in the
ARC Grid format, used in topography for representing terrain elevation.
We import the following ARCGrid file.
5. Data Analysis and Manipulation 161
Out[ ]=
Notice that this is a compressed file. We can decompress it to find out what files are
included.
In[ ]:= Import "http:exampledata.wolfram.comArcGRID.zip", "ZIP"
Out[ ]=
Out[ ]= 32 732
We build a simple frequency table using BinCounts x1 , x2 , , b1 , b2 , to
count the number of xi in the intervals b1 , b2 , b2 , b3 , ..
In[ ]:= TableForm"0 102 ", "102 103 ", "103 104 ",
"104 105 ", "105 106 ", "106 107 ", BinCountsusacities,
0, 100, 1000, 10 000, 100 000, 1 000 000, 10 000 000
Out[ ]//TableForm=
0 102 102 103 103 104 104 105 105 106 106 107
2310 13 274 12 795 4039 304 10
We calculate the most common descriptive statistics:
In[ ]:= Mean, StandardDeviation, Skewness, Kurtosis,
Quantile, .6, InterquartileRange &usacities
Out[ ]= 7773.91, 65 287.1, 81.8769, 9560.86, 1775., 3718.5
104
1000
100
Out[ ]=
10
1
100 105
Out[ ]= When in the Course of human events, it becomes necessary for one people to dissolve the political bands which have connected them with another, and to assume, among the Powers of the earth, the separate and equal station to which the Laws of Nature and of Nature's God entitle them, a decent respect to the
opinions of mankind requires that they should declare the causes which impel them to the separation. We hold these truths to be selfevident, that all men are created equal, that they are endowed by their Creator with certain unalienable Rights, that among these
are Life, Liberty, and the pursuit of Happiness.That to secure these rights, Governments are instituted among Men, deriving their just powers from the consent of the governed,That whenever any Form of Government
becomes destructive of these ends, it is the Right of the People to to alter or abolish it, and to institute new Government, laying its foundation on such principles and organizing its powers in
such form, as to them shall seem most likely to effect their Safety and Happiness. Prudence, indeed, will dictate that Governments long established should not be changed for light and transient causes; and accordingly all experience
The functions below show how to create a bar chart of the frequency of characters:
In[ ]:= letters LetterCounts
ExampleData"Text", "DeclarationOfIndependence", IgnoreCase True
Out[ ]= e 928, t 686, o 578, a 540, n 537, s 519, i 495, r 489,
h 393, l 278, d 266, u 218, c 204, f 190, m 178, p 151,
g 150, w 117, b 105, y 91, v 76, j 33, k 21, x 10, q 6, z 4
5. Data Analysis and Manipulation 165
800
600
Out[ ]=
400
200
0
e t o a n s i r h l d u c f m p g w b y v j k x q z
Out[ ]= ResourceObject
The function below imports the dataset using and sets the number of items to
be displayed at once to 10. You can use the mouse to scroll through the data.
In[ ]:= Dataset ResourceData"NASA GISTEMP Global Means dTs", MaxItems 10
166 Mathematica beyond mathematics
To extract the contents of specific columns, we use the following syntax: data[All,
{Column Names}], in this case we choose “Year” and “JanToDec”.
In[ ]:= data ResourceData"NASA GISTEMP Global Means dTs"
All, "Year", "JanToDec";
The values are the deviations with respect to the “normal” , where “normal” always means
the average over the 30-year period 1951 — 1980 for that place and time of the year. The
years 1951 and 1980 correspond to rows 72 and 101, respectively. The main statistics for
that period are shown below. Remember that the use of the syntax f1, ...,
fn&data1 is equivalent to f1data1, ..., fndata1
In[ ]:= Mean, Median, StandardDeviation, Max, Min &
data72 ;; 101, "JanToDec"
Out[ ]= 0.000333333 C, 0.01 C, 0.109686 C, 0.27 C, 0.2 C
The mean should be zero; the small difference is due to rounding errors. The median is very
close to zero, and the maximum and minimum figures indicate that during those 30 years,
there was no clear trend (their absolute values are quite similar). However, when we analyze
all the available data, we can see that starting in the 1970’s the mean global temperature
displays a clear upward trend.
In[ ]:= DateListPlotdata, PlotTheme "Web", AxesOrigin Automatic, 0
0.75
0.50
0.25
Out[ ]=
0.25
0.50
1900 1925 1950 1975 2000
To insert the label Min in the graph, copy Min to the clipboard and double click inside the
plot. Then, right-click and choose “Paste into Graphic”. Finally, move the label to the
appropriate location.
5. Data Analysis and Manipulation 167
Out[ ]= 18. C
Out[ ]=
18C
Out[ ]= LESA
We can then analyze the temperature measurements for this weather station. The function
below returns all the data available for December 14, 2020 measured at 30-minutes intervals.
In[ ]:= AirTemperatureData"LESA",
DateObject2020, 12, 14, DateObject2020, 12, 15
The output is given as a time series, a format that is very useful if you want to perform
additional statistical calculations. However, we can also display the actual values using
Normal . is used in this case to show only 3 random temperature readings.
In[ ]:= RandomChoiceNormal , 3
To compute the mean temperature in a circle with a 200-km radius centered around the
weather station, we first obtain LESA’s coordinates and then use GeoDisk.
In[ ]:= AirTemperatureData"LESA", "Coordinates"1
Out[ ]= 18.4 C
168 Mathematica beyond mathematics
GeoNearest can be used to find out the name of the nearest weather station.
Out[ ]=
We visualize data1 and data2 using Histogram. If you position the cursor inside any of
the bars, its value will be displayed.
In[ ]:= Histogramdata1, data2, ChartLegends "LESA", "LEVD"
35
30
25
20 LESA
Out[ ]=
15
LEVD
10
5
0
5 10 15 20 25 30
We can also visualize the data using BoxWhiskerChart . When placing the cursor on any
of the boxes, you will see the statistical information associated with it.
5. Data Analysis and Manipulation 169
25
20
15
Out[ ]=
10
0
LESA LEVD
Out[ ]= In probability theory, a probability density function PDF, or density of a continuous random
variable, is a function whose value at any given sample or point in the sample space
the set of possible values taken by the random variable can be interpreted as providing
a relative likelihood that the value of the random variable would be close to that sample.,
In other words, while the absolute likelihood for a continuous random variable to take
on any particular value is 0 since there is an infinite set of possible values to
begin with, the value of the PDF at two different samples can be used to infer,
in any particular draw of the random variable, how much more likely it is that
the random variable would be close to one sample compared to the other sample.,
In a more precise sense, the PDF is used to specify the probability of the random variable
falling within a particular range of values, as opposed to taking on any one value.
The formal definition:
The probability density function PDF P x of a continuous distribution is defined as the derivative
of the cumulative distribution function D x,
D x P xx
P x P
Out[ ]= P x,
so
D x P X x
x
P d .
Different kinds of probability distribution can be used depending on the random processes
involved. The number of probability distributions (guide/ParametricStatisticalDistributions)
170 Mathematica beyond mathematics
available in Mathematica is substantially higher than the one available in other well-known
statistical programs. Additionally, Mathematica’s symbolic capabilities enable us to build our
own distributions using variations of the available ones.
This example shows the PDF and CDF of a Poisson distribution.
In[ ]:= pdf PDFPoissonDistribution , x
x
x0
Out[ ]= x
0 True
Looking at the output above, we can see that the Poisson distribution depends on only one
parameter: . This means that the variance is equal to the mean and therefore, the standard
deviation = .
The Poisson distribution is used to model the number of events x occurring within a given time
interval with the parameter indicating the average number of events per interval. A typical
Poisson process is the radioactive disintegration.
Example: After measuring an environmental sample of uranium over a few hours, its parameter has been
determined to be = 10 disintegrations/minute.
The PDF and CDF of measure x = {0, 1, 2, ...} disintegrations in one minute are represented
below. An option in PlotLegends is included to place the legend in the desired position.
In[ ]:= DiscretePlotEvaluatepdf, cdf . 10, x, 0, 15, PlotRange All,
PlotLegends Placed"Probability", "Cumulative Probability", Below
Out[ ]=
#$ expr, x dist (to write you can use the palettes or the shortcut: Esc
key dist Esc key) calculates the probability of expr under the assumption that x
5. Data Analysis and Manipulation 171
Out[ ]= 0.0834585
Out[ ]= 2
Out[ ]= 4 6 3 7 2
A normal distribution in a variate X with mean and variance 2 is a statistic distribution with
probability density function
1 x 2 2 2
P x e
2
Out[ ]=
on the domain x , . While statisticians and mathematicians uniformly use the term “normal
distribution” for this distribution, physicists sometimes call it a Gaussian distribution and, because of
its curved flaring shape, social scientists refer to it as the “bell curve.” Feller uses the symbol ! x for
P x in the above equation, but then switches to " x in Feller.
be talking about pseudorandom numbers since they are generated by algorithms. However,
for most practical applications they can be considered random numbers. We place the
function inside Manipulate using n, the number of data points, as the parameter. We can
clearly see that the shape of the histogram gets closer to the probability density function
(PDF) of the theoretical N(0,1) as the number of simulated data points increases.
In fact, the most commonly used distributions for modeling equipment failure are the
Gamma distribution (GammaDistribution) and the Weibull distribution
(WeibullDistribution). We can even use the function with a third argument to generate a
Dataset containing the values of all possible properties associated to them:
In[ ]:= FindDistributionpacemaker, 2, All
The function below displays the graphs of their probability density functions along with the
histogram of the data:
In[ ]:= ShowHistogrampacemaker, Automatic, "PDF",
PlotPDFgamma, x, PDF weibull, x, x, 0, 50,
Filling Axis, PlotLegends "Gamma PDF", "Weibull PDF"
Gamma PDF
Out[ ]=
Weibull PDF
The probability that the pacemaker will still be working after 20 years is:
In[ ]:= Probabilityx 20, x gamma N
Out[ ]= 0.279733
Out[ ]= Data on eruptions of Old Faithful geyser, October 1980. Variables are the
duration in seconds of the current eruption, and the time in minutes to the next
eruption. Collected by volunteers, and supplied by the Yellowstone National Park
Geologist. Data was not collected between approximately midnight and 6 AM.
174 Mathematica beyond mathematics
Out[ ]=
Out[ ]= 0.900811
They are highly correlated. To visualize it we can use PairedHistogram after rescaling the
data.
In[ ]:= sclData1
Withdat1 MinMax &duration, dat2 MinMax &waitingtime,
Rescale, dat1, dat2 & duration;
In[ ]:= PairedHistogramsclData1, waitingtime, 12, "PDF",
ChartElementFunction "GlassRectangle", ChartLabels
Style"Scaled Duration", "Text", Style"Waiting Time", "Text"
Out[ ]=
Now let’s plot together both, the original data and the mixture distribution, approximating it:
In[ ]:= ShowHistogramwaitingtime, 25, "PDF",
PlotPDFmixDist1, x, x, 41, 100, PlotStyle Red
0.05
0.04
0.03
Out[ ]=
0.02
0.01
0.00
50 60 70 80 90
The new distribution can be used for statistical computations such as:
The probability that the time between two emissions will be 80 minutes or more:
In[ ]:= Probabilityx 80, x mixDist1
Out[ ]= 0.323516
Out[ ]= LESA
We can now get the average daily wind speed, in km/h, for the area beginning in 2000 and
eliminate incomplete records. We use DeleteMissing[list, 1, 2] to remove any sublists
containing missing data within the first 2 levels. We also use “Path” to extract Time-value
pairs:
176 Mathematica beyond mathematics
We need to extract the wind speeds from the data. We use the Part function to get a list of
the daily average speeds, convert them to new units (meters/second), and visualize the result
with histograms.
In[ ]:= wndData 1000 wDataAll, 2 602 ;
GraphicsRowhistplotpdf, histplotcdf HistogramwndData, 45,
"PDF", PlotLabel Style"Daily average speed", "Title", 12,
HistogramwndData, 45, "CDF", PlotLabel Style
"Accumulated daily average speed", "Title", 12, ImageSize Large
0.30 1.0
0.25
0.8
0.20
Out[ ]= 0.6
0.15
0.4
0.10
0.05 0.2
0.00 0.0
2 4 6 8 2 4 6
5. Data Analysis and Manipulation 177
We can now fit the previous data to an existing distribution. However, we can also create a
new one based on the data. This last operation can be done automatically with the function
SmoothKernelDistribution.
In[ ]:= e SmoothKernelDistributionQuantityMagnitude wndData
Type: SmoothKernel
Out[ ]= DataDistribution
Data points: 8072
While the ability to estimate a distribution is a valuable tool in statistical analysis, one must
be careful when actually using it. It is possible for estimated distributions to have very small
negative values in the tails. Estimated distributions of this sort behave in a similar manner to
interpolating functions and can show unexpected behavior.
In[ ]:= Show
histplotpdf,
estplot PlotPDFe , x, x, 0, 13, PlotStyle Thick, Red
Daily average speed
0.30
0.25
0.20
Out[ ]=
0.15
0.10
0.05
0.00
0 2 4 6 8
Assuming that the generator we are using for our analysis requires at least a wind speed of
3.5 m/s to generate electricity, we can use the Probability function to estimate the proportion
of time the generator will be contributing to the grid.
In[ ]:= Probabilityx 3.5, x e
Out[ ]= 0.298209
An alternative method would be to estimate the distribution parameters and construct the
distribution. The graphical representation of the data suggests that the wind speed could be
approximated using a LogNormalDistribution. Zeros are eliminated because they are not
compatible with such distribution.
In[ ]:= params FindDistributionParameters
DeleteCasesQuantityMagnitudewndData, 0, LogNormalDistribution ,
Out[ ]= 0.836653, 0.7039
1.0
0.30
0.25 0.8
CramerVonMises
The null hypothesis that the data is distributed according to the
Out[ ]=
QuantityDistributionLogNormalDistribution0.836653, 0.7039, kmh
is rejected at the 5 percent level based on the Cramér-von Mises test.
Get the test statistic and p-value:
In[ ]:= tstData"TestDataTable"
Statistic P-Value
Out[ ]=
Cramér-von Mises 2.48879 1.03184 106
The test tells us that the fit is not good, but when there is a large number of data points, the
best possible test is the graphical representation of the data alongside the density function of
the estimated distribution. In that case, the fit is quite reasonable. We can recalculate the
probability of the wind speed being at least 3.5 m/s and see that the result is very close to the
one previously obtained.
In[ ]:= Probabilityx 3.5, x p
Out[ ]= 0.27721
Find the probability of counting 8 or fewer cars before seeing a black one.
In[ ]:= NProbabilityx 9, x GeometricDistribution0.1
Out[ ]= 0.651322
Three friends are standing on a road in different places counting the number of cars
until a black one appears. The winner will be the first one to spot a black car.
Assuming that 10% of the cars are black, find the expected number of cars that will
pass before one of the friends sees a black car.
We use OrderDistribution[dist, n, k], where k represents the k th -order statistics
distribution for n observations from the distribution dist.
In[ ]:= Expectationx, x OrderDistributionGeometricDistribution0.1, 3, 3
Out[ ]= 16.9006
The output indicates the average total number of cars that the friends will count before a
black one appears. Since it’s an average, the number doesn’t have to be an integer.
What is the probability that the winner will count less than 10 cars and at least 4?
In[ ]:= ProbabilityConditionedx 10, x 4,
x OrderDistributionGeometricDistribution0.1, 3, 3
Out[ ]= 0.245621
There are many more statistical functions available. You may be interested in
exploring some of the latest ones, such as: CopulaDistribution ,
MarginalDistribution, and TransformedDistribution.
with n in this case being the number of voters and p the probability of a Yes vote.
The answer will depend on our previous knowledge regarding the value of p.
If we don’t know anything about p in advance, then the probability can take any value
between 0 and 1. This is equivalent to the assumption that p follows a uniform distribution
between 0 and 1. For any value of n, the solution is:
In[ ]:= sol1n : Probabilityx n 2,
x ParameterMixtureDistribution
BinomialDistributionn, p, p UniformDistribution0, 1
With n = 3030, the probability of a tie is:
In[ ]:= sol13030
1
Out[ ]=
3031
A more direct way of calculating p is to enumerate all the different possibilities: {{3030
Yes, 0 No}, {3029 Yes, 1 No},...., {1 Yes, 3029 No},{ 0 Yes, 3030 No}}. There are a total
of 3,031 possible cases, and since we know nothing about p, we assume that all of them have
the same probability of happening. Therefore, p = 1/3,031 for any result and a tie is one of
the potential outcomes.
In the actual case, there was some previous knowledge. There had been earlier votes with
#
outcomes very close to a tie. Let’s assume that p = 0.49 with a standard deviation of 0.02.
Then we can calculate the probability assuming that p follows a Normal probability
distribution: X ~ Normal(0.49, 0.02):
In[ ]:= NIntegratePDFNormalDistribution0.49, 0.02, p
PDFBinomialDistribution3030, p, 1515,
p, 0, 1, WorkingPrecision 10
Out[ ]= 0.005402504702
If we’d like to perform a Bayesian analysis, it’s convenient to assume that p follows a Beta
distribution with parameters $ and %, whose actual values depend on our prior knowledge of
p.
In[ ]:= sol2n , , : Probabilityx n 2,
x ParameterMixtureDistribution
BinomialDistributionn, p, p BetaDistribution,
Without previous knowledge about p, then $ = 1 and % = 1 and the result is the same as the
one previously obtained.
In[ ]:= sol23030, 1, 1
1
Out[ ]=
3031
For the secret ballot scenario, we have some prior knowledge since the results of the
previous 3 or 4 votes had been very close to a tie. We can therefore assume that p is
approximately 1/2. This is equivalent to $ and % taking very large values. An extreme case:
In[ ]:= sol23030, 100 000, 100 000 N
Out[ ]= 0.0143853
If we knew for sure that p was exactly 1/2, this problem would be equivalent to computing
the probability that after tossing a fair coin 3,030 times, we would get the same number of
heads and tails.
5. Data Analysis and Manipulation 181
Out[ ]= 0.0144938
In summary, the probability that a tie could happen was not that small (keeping in
mind that earlier votes were close affairs). Highly unlikely events happen all the
time. An extreme example occurred shortly after the vote, also in Spain. The same
person won the biggest prize in the national lottery for two consecutive weeks. In
this game, the probability of winning the highest prize (“El Gordo”) is 1/100,000.
This means that a priori, the probability that a person purchasing just one ticket
each time will win twice in a row is 1/100,000 1/100,000 = 10 10 quite a small
likelihood. Actually, the odds of dying from lightning are higher. It’s not unusual
that with so much going on around us at any given moment, the small probability
events are the ones drawing our attention.
To build statistical graphs load the palette: Palettes & Chart Elements Schemes.
Many empirical data are represented by more than one random variable; we will
refer to them as multivariate data.
The function below plots the PDF Student’s t distribution for two variables.
In[ ]:= Plot3DPDFMultivariateTDistribution1, 1 2, 1 2, 1, 20, x, y,
x, 3, 3, y, 3, 3
Out[ ]=
Some statistical studies may contain simulated instead of empirical data. In those
cases, random number generators are used. Mathematica has the functions
RandomReal and RandomInteger to generate random numbers. Since these
functions use algorithms, strictly speaking they don’t provide truly random data.
However, in most situations we can assume that they do.
182 Mathematica beyond mathematics
The next function generates random numbers from a multivariate Poisson distribution. If
you remove the “;” you will notice that the result is a list of sublists, that can be interpreted
as a matrix with three columns:
In[ ]:= multidata
RandomVariateMultivariatePoissonDistribution1, 2, 3, 4, 125;
We represent each sublist with a point. We include a red point to show the mean.
In[ ]:= ShowListPointPlot3Dmultidata,
Graphics3DRed, PointSizeLarge, PointMeanmultidata
Out[ ]=
The next graph shows a histogram of the first two dimensions of the random data previously
generated (multidata) along with a histogram of 5,000 pairs of normally distributed random
numbers. (Remember that to extract list elements we use: Part or ...).
In[ ]:= GraphicsRowHistogram3DmultidataAll, 1 ;; 2, 5, Histogram3D
RandomRealNormalDistribution, 5000, 2, ImageSize Medium
Out[ ]=
An alternative way of representing the same data with the edges smoothed out is by using
SmoothHistogram3D and SmoothDensityHistogram (the change in color is associated to the
actual data value).
5. Data Analysis and Manipulation 183
Out[ ]=
Life Expectancy
Out[ ]= Female
Male
With CountryData we get not only the Life Expectancy but also the Gross Domestic
Product (GDP) per Capita and Population for all the countries in South America:
In[ ]:= data1 TableTooltipCountryDatac, 1 &
"LifeExpectancy", "GDPPerCapita", "Population",
ColumnCountryDatac, "Name", CountryDatac, "Population",
c, CountryData"SouthAmerica";
The countries that don’t have available information are deleted:
In[ ]:= data2 DeleteMissingdata1, 1, 3
Out[ ]= 78.07 yr $8441.92 per person per year 45 195 777 people ,
70.7 yr $3143.05 per person per year 11 673 029 people ,
74.98 yr $6796.84 per person per year 212 559 409 people ,
79.57 yr $13 231.7 per person per year 19 116 209 people ,
76.91 yr $5332.77 per person per year 50 882 884 people ,
77.76 yr $5600.39 per person per year 17 643 060 people ,
77.9 yr $35 400. per person per year 3483 people ,
77.121 yr $8300. per person per year 298 682 people ,
71.59 yr $6955.94 per person per year 786 559 people ,
78.13 yr $4949.75 per person per year 7 132 530 people ,
74.96 yr $6126.87 per person per year 32 971 846 people ,
73.57 yr $6491.14 per person per year 586 634 people ,
78.19 yr $15 438.4 per person per year 3 473 727 people ,
72.22 yr $16 055.6 per person per year 28 435 943 people
5. Data Analysis and Manipulation 185
The result is displayed as a bubble chart. The diameters of the bubbles are proportional to
the population size. You can see the country name and its corresponding population by
putting the mouse over each bubble:
In[ ]:= BubbleChartdata2, FrameLabel "Life expectancy", "GDPcapita"
35 000
30 000
25 000
GDP capita
20 000
Out[ ]=
15 000
10 000
5000
0
70 72 74 76 78 80
Life expectancy
In 1936 he introduced the Iris flower data set as an example of discriminant analysis. Please
read carefully the following sentences describing the experiment:
186 Mathematica beyond mathematics
Out[ ]= The Iris flower data set or Fisher's Iris data set is a multivariate data set introduced by the British
statistician and biologist Ronald Fisher in his 1936 paper The use of multiple
measurements in taxonomic problems as an example of linear discriminant analysis.,
It is sometimes called Anderson's Iris data set because Edgar Anderson collected the
data to quantify the morphologic variation of Iris flowers of three related species.,
Two of the three species were collected in the Gaspé Peninsula "all from the
same pasture, and picked on the same day and measured at
the same time by the same person with the same apparatus".,
The data set consists of 50 samples from each of three species of Iris
Iris setosa, Iris virginica and Iris versicolor.,
Four features were measured from each sample: the length and the
width of the sepals and petals, in centimeters.,
Based on the combination of these four features, Fisher developed a linear
discriminant model to distinguish the species from each other.
Fisher's paper was published in the Annals of Eugenics and includes
discussion of the contained techniques' applications to the field of Phrenology.
We use ResourceData to find out the source of the Iris data set:
In[ ]:= ResourceDatafisheriris, "Source"
Out[ ]= Fisher,R.A. "The use of multiple measurements in taxonomic problems" Annual Eugenics, 7, Part II,
179 188 1936; also in "Contributions to Mathematical Statistics" John Wiley, NY, 1950.
This paper is freely available online in the following site:
https://onlinelibrary.wiley.com/doi/epdf/10.1111/j.1469-1809.1936.tb02137.x
Here we show the three species of Iris. (At the time of this writing, the images were located
in positions 3 to 5. However, as Wikipedia is subject to frequent changes, if the function
below doesn’t return the correct images modify the numbers accordingly.)
In[ ]:= WikipediaData"Iris flower data set", "ImageList"3 ;; 5
In this case, we are going to do a simple exploratory data analysis (EDA). Later on
in the book, we will use the same data to show how to perform some machine
learning tasks.
Here we can see the elements in the file:
5. Data Analysis and Manipulation 187
Out[ ]= Sepal length in cm., Sepal width in cm., Petal length in cm., Petal width in cm., Species of iris
Now, we display the data of the 50 samples from each of three species of Iris (Iris setosa,
Iris virginica, and Iris versicolor):
In[ ]:= FisherIrisData ResourceData"Sample Data: Fisher's Irises"
To select the data only for the setosa species we proceed as follows:
In[ ]:= setosadata FisherIrisDataSelect"Species" "setosa" &
The following table contains information about some of the basic statistics used in EDA
applied to the setosa dataset.
In[ ]:= sepalpetal "SepalLength", "SepalWidth", "PetalLength", "PetalWidth";
The same statistical information for all the species based on their sepal lengths:
In[ ]:= FisherIrisDataGroupBy"Species",
Max , Min, Mean, Median, InterquartileRange,
StandardDeviation &, "SepalLength"
188 Mathematica beyond mathematics
Next, we calculate the covariances and correlations to explore the relationships between the
variables.
In[ ]:= sepalpetaldata TransposeNormalFisherIrisDataAll, & sepalpetal;
Out[ ]= 0.685694 cm2 , 0.042434 cm2 , 1.27432 cm2 , 0.516271 cm2 ,
The results indicate that sepal length is strongly correlated with both, petal length and petal
width. They also show that petal length is strongly associated to petal width. However, we
can see that the linear association between sepal length and sepal width is quite weak.
5. Data Analysis and Manipulation 189
Out[ ]= 0.0947443
Let’s supposed we don’t know all the data for that year and instead we only have
precipitation information for just 30 random days.
We can use SeedRandom to get repeatable random values:
In[ ]:= sample1 SeedRandom2514; RandomChoiceprecipitationData, 30
Out[ ]= 0.2, 0., 0., 0., 0.02, 0., 0., 0., 0., 0., 0., 0., 0.05, 0., 0.,
0.28, 0., 0.02, 0., 0.43, 0.02, 0., 0.28, 0., 0., 0., 0., 1.45, 0., 0.05
We want to estimate the daily mean precipitation during that year along with its standard
deviation based on the sample:
StandardDeviationsample1
In[ ]:= ms1, ss1 Meansample1,
Lengthsample1
We can also get directly a confidence interval around the mean as follows:
In[ ]:= MeanAroundsample1
This confidence interval indicates that 68.27% of the time the true population mean will be
within its bounds. We can make such a statement because according to the central limit
theorem, the mean statistic of the sample follows a normal distribution regardless of the
underlying population distribution.
In[ ]:= Probability x , x NormalDistribution , N
Out[ ]= 0.682689
If instead of a single weather station we have precipitation information from several stations
located in the area, even though we may not have the corresponding dates for such data, we
can still estimate the mean daily precipitation for the year. We use again SeedRandom to
make the example reproducible:
In[ ]:= samples SeedRandom1234; RandomChoiceprecipitationData, 34,
SeedRandom7341; RandomChoiceprecipitationData, 27,
SeedRandom3211; RandomChoiceprecipitationData, 27,
SeedRandom9247; RandomChoiceprecipitationData, 25,
SeedRandom3782; RandomChoiceprecipitationData, 34;
190 Mathematica beyond mathematics
We group the data to simulate a bigger single sample and find out that the mean confidence
interval gets smaller:
In[ ]:= MeanAroundFlattensamples
We can obtain a very similar result if we calculate the mean of the sample means:
In[ ]:= MeanAroundMean & samples
In[ ]:= flowTormes 68.2, 604.8, 139.4, 95.7, 327., 357.7, 134.3, 99.6, 300.2,
208.7, 48.6, 43.1, 128.5, 553.9, 45.4, 119.9, 63.5, 243.8,
274.8, 117., 134.4, 179.1, 29.6, 168.9, 23.3, 13.7, 510.2,
48.5, 420.6, 330.9, 576.9, 22.3, 73., 750.2, 33.6, 471.5, 138.2,
53.4, 67.6, 250.9, 216.7, 242.5, 188.8, 66.2, 43.1, 322.4;
Using bootstrapping, we can get a larger set to give us a better idea of how much this
measure varies. We also calculate its 95% confidence interval:
In[ ]:= flowTormes1
ResourceFunction"BootstrapStatistics"flowTormes , 10 000, Mean;
HistogramflowTormes1
1500
1000
Out[ ]=
500
0
150 200 250 300
The function to compute the maximum and minimum flows during that period is shown
below:
In[ ]:= Through Min, MaxflowTormes1
Next, we approximate the data using a smooth kernel distribution. In statistics, kernel
density estimation (KDE) is a non-parametric way to estimate the probability density
function of a random variable. KDE is a fundamental data-smoothing problem where
inferences about the population are made, based on a finite data sample.
In[ ]:= SmoothKernelDistributionflowTormes ;
We now smooth over each bootstrapped sample and obtain the confidence estimates:
In[ ]:= B SmoothKernelDistribution & bSamp;
Next, we calculate the confidence band from the bootstrapped examples (we choose a range
close to the minimum and maximum flows of the river):
In[ ]:= rng Range10., 1000;
pdf TablePDFi, rng, i, B ;
In[ ]:= High TableQuantilei, .975, i, Transposepdf;
Low TableQuantilei, .025, i, Transposepdf;
Finally, we visualize the estimate of the PDF with 95% confidence bands:
192 Mathematica beyond mathematics
In[ ]:= ShowPlotPDF, x, x, 0, 1000, PlotRange All, AxesLabel "m3s",
ListLinePlotTransposerng, High, Transposerng, Low,
PlotStyle Dashed, Gray, Filling 1 2
0.004
0.003
Out[ ]= 0.002
0.001
m3 s
200 400 600 800 1000
When designing big engineering projects such as damns, bridges, or nuclear power
stations, is necessary to take into consideration events with very low probability of
happening but high negative impact. Massive floods, big earthquakes, and tsunamis
are instances of such events, and their probabilities can be calculated using extreme
value theory.
Going back to our example, studying Tormes’ annual maximum flows since 1965
would very useful when planning and designing infrastructure (e.g., road location)
along the river path to minimize the risk of catastrophic flooding. In cases like this
one, the most frequently used distributions are generalized extreme value
distributions such as the Gumbel distribution. [Gumbel, E. J. (1958). Statistics of
Extremes. Irvington, New York: Columbia University Press.]
Before showing how to analyze the Tormes’ flow using the Gumbel distribution,
there are a couple of concepts from extreme value theory that we need to explain
first: exceedance and return period.
The concept of exceedance refers to the number of times in N future observations
that we will observe a more extreme observation m than a certain threshold
(determined from n historical observations). That number can be modelled as a
continuous random variate following a distribution w n, m, N .
Regarding the second concept, one of the most important objectives of frequency
analysis is to calculate the recurrence interval of events, a.k.a the return period. If
the variable (x), equal to or greater than an event of magnitude , occurs once in T
years, then the probability of occurrence P(X x) in a given year of the variable is:
1
Px xT
T
1
T
1 Px xT
Based on the previous two equations, we can now defined the return period as the
average length of time between events of the same magnitude or greater. This can
also be interpreted as the number of observations such that, on average, there is one
5. Data Analysis and Manipulation 193
where: x# = mean and X = standard deviation of the variate X, in our case the river
flow series.
That is:
In[ ]:= xmean, sd, size
Mean, StandardDeviation, Length &flowTormes
Out[ ]= 203.274, 181.765, 46
In this section, we are going to see some of those functions through basic examples.
We use two files containing the data we are going to fit: noisydata.xls and
pulsar1257.dat. These two files must be in a subdirectory, named “Data”, inside the
directory where the notebook is located.
Remember that in the first section of this chapter, we discussed how to create such a folder
and make it the working directory. In case you haven’t done so yet, just execute the
following function after creating the “Data” subdirectory in your computer:
In[ ]:= SetDirectoryFileNameJoinNotebookDirectory, "Data"
100
50
Out[ ]=
5 5 10 15
50
100
150
Now we graphically show both the data and the fitted function.
In[ ]:= Showdataplot,
Plotfitx, x, 5, 15, PlotStyle Red
150
100
50
Out[ ]=
5 5 10 15
50
100
150
Represent it graphically:
1
In[ ]:= dataplot ListPlot data, AspectRatio , PlotStyle Red
4
3
2
1
Out[ ]=
1.0 1.5 2.0 2.5 3.0 3.5
1
2
Out[ ]= FittedModel 1.33261 cos23.3869 0.209533 cos34.5111 19 sin18 1.29803 sin34.5111 0.0769581
We put the data and the function on the same graph to check the quality of the fit:
In[ ]:= ShowPlotpulsarfitt, t, 0.68, 3.76,
dataplot
3
Out[ ]=
1.0 1.5 2.0 2.5 3.0 3.5
1
2
Once the model has been created, we can perform all kinds of statistical analyses. However,
it would be useful to list all the properties available for the model:
In[ ]:= pulsarfit"Properties" Shallow
Out[ ]//Shallow=
AdjustedRSquared, AIC, AICc, ANOVATable,
ANOVATableDegreesOfFreedom, ANOVATableEntries, ANOVATableMeanSquares,
ANOVATableSumsOfSquares, BestFit, BestFitParameters, 40
For example, here we display the residuals:
In[ ]:= ListPlotpulsarfit"FitResiduals", GridLines None, Automatic
0.06
0.04
0.02
Out[ ]=
50 100 150
0.02
0.04
5. Data Analysis and Manipulation 197
Out[ ]=
2 4 6 8
The shape of the curves suggests that they could be approximated by functions of the form:
a b Exp k t. We use the function FindFit to find the best-fit parameters for the fitting
functions.
In[ ]:= fitpopulation1 FindFitpopulation1, a1 b1 Exp k1 t, a1, b1, k1, t
The next example, showing how to customize a graph, includes options that we
haven’t covered yet (we will explain them later in the book). As usual, if any of the
instructions are not clear, consult Mathematica help by selecting the function and
pressing F1. You can use this code as a template for similar cases. Alternatively,
you might find it easier to create the labels and plot legends with the drawing tools
(.+D).
We display the fitted curves along with the original data after having replaced the symbolic
parameters in the curves with the values obtained from FindFit. We also customize the
size of the points representing the experimental data.
In[ ]:= fitsol1 a1 b1 Exp k1 t . fitpopulation1;
fitsol2 a2 b2 Exp k2 t . fitpopulation2;
Plotfitsol1, fitsol2, t, 0, 10, AxesLabel "thours",
"Concentration", PlotLegends Placed"Population 1", "Population 2",
Center, Epilog Hue0.3, PointSize0.02, MapPoint,
population1 , Hue0, PointSize0.02, MapPoint, population2,
PlotLabel StyleFramed"Evolution of 2 cell populations",
12, Blue, Background LighterCyan
Out[ ]= 3
Population 1
Population 2
2
thours
2 4 6 8 10
The following function will take you to a website that contains the file with historical
production data for different types of energy by country and region.
In[ ]:= SystemOpen
"http:www.bp.comenglobalcorporateenergy economicsstatistical
review of world energy.html"
Download it by clicking on “Statistical Review of World Energy - all data, 1965-2021” and
save it without changing its name in the subdirectory Data. The file that we are going to use
corresponds to the 2016 (the last one available when this was written) report containing data
5. Data Analysis and Manipulation 199
With the help of the following functions, we are going to import part of the file:
We can see the labels for the worksheets by using the argument "Sheets".
In[ ]:= Import
"https:www.bp.comcontentdambpbusiness sitesenglobalcorporate
xlsxenergy economicsstatistical reviewbp stats review 2022 all
data.xlsx", "Sheets" Short
Out[ ]//Short=
Contents, Primary Energy Consumption,
Primary Energy Cons by fuel, 78, Definitions, Methodology
Out[ ]= Oil, Natural Gas, Coal, Nuclear energy, Hydro electric, Renew ables,
184.214, 145.349, 160.104, 25.3128, 40.2596, 39.9126
We now visualize the data using a pie chart showing the percentage share of the total for
each energy source. An interesting observation is the fact that fossil fuels represent the vast
majority of the world primary energy consumption with renewables (excluding
hydroelectric) playing a very small role (wind, sun, biomass, etc.).
In[ ]:= PieCharttotalcons2021 Totaltotalcons2021,
ChartStyle "DarkRainbow", ChartLabels energytype, LabelingFunction
PlacedRowNumberForm100 , 3, 1, " ", "RadialCallout" &,
PlotLabel Style"Sources of Energy ", Bold
Sources of Energy
31.0
Oil
24.4
Natural Gas
Out[ ]=
Renew ables
6.7
Hydro electric
Nuclear energy
6.8 Coal
4.3
26.9
If you’d like to customize a graph, use the information available in the help
system. For example, go to the documentation page for PieChart, and look for
an example that suits your needs.
Now we are going to analyze the evolution of the global primary energy
consumption and look for a function that will enable us to extrapolate energy
consumption over the coming years.
5. Data Analysis and Manipulation 201
Notice that the desired information is in row 3 (year) and row 111 (Total World, in
Exajoules). We also eliminate not relevant rows and columns.
In[ ]:= primaryevolution
DropDropTransposeprimaryenergyDataYear3, 112, 1, 4;
Shallowprimaryevolution
Out[ ]//Shallow=
1965., 110.01, 1966., 115.411, 1967., 119.987,
1968., 127.923, 1969., 136.364, 1970., 144., 1971., 147.92,
1972., 155.41, 1973., 164.257, 1974., 162.18, 46
We plot the data. Using Tooltip, if you click on a curve point, its value will be shown:
202 Mathematica beyond mathematics
200
150
Out[ ]=
100
50
Year
1970 1980 1990 2000 2010 2020
240
220
200
180
Out[ ]=
160
140
120
The next function fits the data to a typical time series model:
In[ ]:= tsm TimeSeriesModelFitaveragetempSalamanca
Family: ARMA
Out[ ]= TimeSeriesModel
Order: 2, 7
We can now use the fitted model to forecast for example the average daily temperature
today and one month from now:
In[ ]:= today DateValueToday, "Year", "Month"
Out[ ]= 2022, 5
Out[ ]= 10.7181 C
Out[ ]= 11.0916 C
Next, we estimate average monthly temperatures using only the data from the last 10 years:
In[ ]:= start DateValueDatePlusToday, 10, "Year", "Year", "Month";
tspec start, today, "Month";
In[ ]:= temp TimeSeriesWeatherData"Salamanca", "Temperature", tspec, "Value",
start, Automatic, "Month";
Then display them graphically. Notice the seasonality.
In[ ]:= DateListPlottemp, Joined True
20
15
Out[ ]=
10
0
2014 2016 2018 2020 2022
Family: SARIMA
Out[ ]= TimeSeriesModel
Order: 0, 0, 0 , 1, 1, 1 12
We visualize next year’s forecast for the average monthly temperature using 95%
confidence bands:
In[ ]:= fdates DateRangetoday, DatePlustoday, 1, "Year", "Month";
25
20
15
Out[ ]=
10
The data records are highly correlated (The p-values are small when the data is
autocorrelated):
In[ ]:= ListPlotTableAutocorrelationTesttemp, i, i, 1, 6, Filling Axis
8. 1024
6. 1024
24
Out[ ]= 4. 10
2. 1024
1 2 3 4 5 6
Dimension: 2
Out[ ]= SpatialEstimatorFunction SpatialTrendFunction: 1
SpatialNoiseLevel: Automatic
Out[ ]= 14.3398 C
C
20
15
Out[ ]= 10
0
206 Mathematica beyond mathematics
The number of applications in the real world that use machine learning
continues to increase unabated. The Wolfram Language has both, general
and domain-specific functions, that implement machine learning
algorithms. After briefly discussing the concept of machine learning and
showing some examples of Mathematica’s capabilities in this area for
identifying languages, estimating probability distributions and
highlighting faces, this chapter focuses on the two most important
functions for performing machine learning tasks with WL: and
. Using data from the Titanic and Fisher’s iris datasets among
others, the reader will learn how to classify data into different clusters
and predict numerical outcomes. The last section contains a short
introduction to neural networks.
Machine learning ML is the study of computer algorithms that can improve automatically through
experience and by the use of data., It is seen as a part of artificial intelligence.,
Machine learning algorithms build a model based on sample data, known as training data,
in order to make predictions or decisions without being explicitly programmed to do so.,
Machine learning algorithms are used in a wide variety of applications, such as in
medicine, email filtering, speech recognition, and computer vision, where it is
difficult or unfeasible to develop conventional algorithms to perform the needed tasks.,
A subset of machine learning is closely related to computational statistics, which focuses
on making predictions using computers; but not all machine learning is statistical learning.
solving domain-specific problems. However, we are not going to explain how those
algorithms actually work.
The two most important functions in WL for ML are Classify and Predict. They are
used for either classifying inputs into categories or class labels or predicting
numeric outputs. The next two sections will cover these functions in more detail.
Next, we show some toy examples of domain-specific functions that use ML:
The function below, identifies the language used to write text:
LanguageIdentify
"thank you", "merci", "dar las gracias", "", " "
In this case, we analyze data from patients suffering from kidney failure to find the
probability distribution that best approximates the time to infection after having the
catheter replaced:
We get the description of the data and store the time to kidney infection (months):
ExampleData"Statistics", "KidneyInfection", "LongDescription"
Next, we use FindDistribution to find the best distribution that fits the data. This function
uses ML algorithms when computing the answer.
e FindDistributionKidneyInfection
ExponentialDistribution0.108901
Finally, we compare the histogram of the data to the PDF of the estimated distribution:
ShowHistogramKidneyInfection, 0, 28, 2, "ProbabilityDensity",
PlotPDFe, x, x, 0, 28, PlotStyle Thick, PlotRange All
0.10
0.08
0.06
0.04
0.02
0.00
0 5 10 15 20 25
The following examples use images available in Mathematica help to show some
! 209
i ;
The function ImageContents tries to identify the contents of an input image and returns a
dataset of identified entities:
210
Then, we make use of the power of Interpreter to transform those entities into a dataset:
TextCasesstr,
Union LookupNormalcontent, "Type" "Interpretation" Dataset
, ,
1.5
1.0
0.5
2 4 6 8 10 12 14
6.2 Classification
In ML there are two approaches for teaching computers to classify inputs:
unsupervised learning and supervised learning.
Under the first one, an algorithm will try to identify patterns from inputs that are
neither classified nor labeled. Functions such as ClusterClassify and FindClusters
implement the cluster analysis unsupervised algorithm to classify data into clusters,
groups sharing similar characteristics. For additional information please refer to the
“Partitioning Data into Clusters” technical note available at:
https://reference.wolfram.com/language/tutorial/NumericalOperationsOnData.html#2948.
6.2.1 Classify
The supervised learning approach uses inputs that have been already labeled to
teach algorithms how to classify new data. One of the most important functions in
Mathematica for supervised learning is Classify. Let’s see it in action:
In this example, we use a set of images captured either during the day or at night to train an
algorithm to recognize when the image was taken:
daynight Classify
After generating a classifier function based on the training set, we can now ask the
algorithm to classify new images:
, ,
This function has numerous options that allow us to get additional information such
as the accuracy of the classifier or the confusion matrix:
We first generate a training list and train a classifier with it:
traininglist 1 "A", 2 "A", 3.5 "B", 3 "A", 5 "B", 6 "B";
Now, we can use test data to assess how good the classifier function is by measuring its
accuracy and uncertainty:
testset 1.2 "A", 2.1 "A", 3. "B", 6.4 "B";
0.75 0.25
Finally, we can also visualize the confusion matrix to find out how many new inputs were
misclassified:
ClassifierMeasurementsc, testset, "ConfusionMatrixPlot"
A
A 2 0 2
actual class
B 1 1 2
3
predicted class
Let’s imagine we have a wide variety of colors obtained by mixing red and blue
214
and we would like to classify them into just two categories: Red or Blue.
The first thing we need to do is to train a supervised learning algorithm with a labeled data
set. In principle, the bigger the training set, the better the resulting classifier will be. For
this step, we generate a list of colors mixing red and blue, with 1-x and x being the
proportion of red and blue, respectively. That is 100% red is x = 0, and 100% blue is x = 1.
Table x, x, 0, 1, 1 16
1 1 3 1 5
5 11 3 13 7 15 3 7 1 9
0, , , , , ,
, , , , , ,1 , , , ,
16 8 16 4 16 8 16 2 16 8 16 4 16 8 16
colorsblend TableBlendRed, Blue, x, x, 0, 1, 1 16
, , , , , , , , , , , , , , , ,
Next, we tell Classify which colors are red and which ones are blue . If the mix is 50%/50%,
we don’t assign any label to that color:
reddish colorsblend1 ;; 8
, , , , , , ,
, , , , , , ,
Just like people, the classifier may have difficulties assigning a color to mixtures close to
50%/50%. However, in this case it’s doing a great job by correctly assigning the red color to
the third element of the list (49.048% blue):
ccolorsblendtest1
, , , ,
Finally, we can check the associated probabilities for each possible class:
ccolorsblendtest1, "Probabilities"
0.000068972, 0.999931,
0.0060001, 0.994, 0.345658, 0.654342,
0.978826, 0.0211736, 0.999753, 0.000247122
In this next example, we use , the list of colors we have just created, as
an input to LearnDistribution to generate a new classifier in the form of a
probability distribution.
We create the learned distribution first:
! 215
, , , , , , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , , , , ,
Approximately half are red, half are blue, and none are green. This is the expected result
given the initial mixtures.
PDFld, , ,
Let’s see an example where multiple inputs are associated to just one output.
We are going to mix colors again using, in this case, the three basic colors: Red (R), Green
(G), and Blue (B). We can do this in the WL with the function RGBColor r, g, b
MapGraphicsRGBColor, Disk &, 1, 0, 0, 0, 1, 0,
0, 0, 1, 0.5, 0.5, 0, 0.5, 0, 0.5, 0, 0.5, 0.5
We generate 100 random lists of the form {r,g,b} to reproduce the colors.
SeedRandom1234
Method: ExtendedCA
RandomGeneratorState
State hash: 3 259 750 555 964 806 719
, , , , , , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , , , , ,
We can now label each list either Red, Green, or Blue depending on the position of its
largest element. For example, if the biggest value is the second element, we assign the color
Green to the list.
listcolor
TakeLargest "Index", 1 & list . 1 Red, 2 Green, 3 Blue ;
training Threadlist Flattenlistcolor;
216
Next, using the new classifier, we convert that image into a different one containing only the
three basic colors:
ImageMapclcolor &, TableImageDataimgi, 1 ;; 5, i, 5
256, 256
As mentioned previously, ClusterClassify can be very useful when our data is not
labeled. Let’s see an example.
First, we randomly generate 90 colors:
colors RandomColor90
, , , , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , , ,
Next, we classify them into 3 clusters:
c ClusterClassifycolors, 3
The example below shows how to find the dominant color in an image.
We choose an image of a sunflower in this case:
218
Although we often represent colors using the RGB specification, sometimes it is more
convenient to use a different color space such as LAB. The command below uses
ColorConvert to change the color specification of the image .
ColorConvertimage, "LAB"
Next, we use the classifier to assign a cluster to each pixel and find four dominant colors:
decision cimageData;
, , ,
, , ,
Now that we have our classifier, we can ask questions, such as the probability that a 50-year
old male would have either died or survived:
titanic"1st", 50, "male", "Probabilities"
We can also visualize the probability of survival for first-class males based on their ages:
Plottitanic"1st", age, "male",
"Probability", "survived", age, 0, 70
0.60
0.55
0.50
0.45
0.40
0.35
0.30
10 20 30 40 50 60 70
We can even predict the age of a passenger using the rest of the features. For example, the
predicted age of a female in 3rd class that survived is:
titanicAge Predictdata . HoldPatternclass, age, sex outcome
class, sex, outcome age;
titanicAge"3rd", "female", "survived"
21.5452
For our last example in this section, we use Fisher’s Iris dataset. The dataset
consists of 150 samples from three species of Iris (setosa, versicolor, and virginica).
Each sample contains four measurements: the length and the width of the sepals
and petals. We want to be able to correctly classify new Iris flowers based on those
four measurements:
Let’s download the data first:
irisData ExampleData"MachineLearning", "FisherIris", "Data";
Finally, using ClassifierMeasurements on the test dataset, we calculate the accuracy of the
algorithm, visualize the confusion matrix (2 samples of versicolor were misclassified as
virginica) and see what examples had the highest actual-class probabilities:
irisCm ClassifierMeasurementsirisCf, irisTestData;
0.96
virginica
setosa
setosa 15 0 0 15
actual class
versicolor 0 14 2 16
virginica 0 0 19 19
15
14
21
predicted class
7.7, 2.6, 6.9, 2.3 virginica, 7.7, 2.8, 6.7, 2. virginica,
7.7, 3.8, 6.7, 2.2 virginica, 7.2, 3.6, 6.1, 2.5 virginica,
6.7, 3.3, 5.7, 2.5 virginica, 6.3, 3.4, 5.6, 2.4 virginica,
4.4, 3.2, 1.3, 0.2 setosa, 6.8, 3.2, 5.9, 2.3 virginica,
5.5, 4.2, 1.4, 0.2 setosa, 4.3, 3., 1.1, 0.1 setosa
Evaluating the probabilities for the first element of the previous output, we can see the
classifier is 100% certain that those measurements correspond to an Iris virginica flower:
irisCf7.7, 2.6, 6.9, 2.3, "Probabilities"
6.3 Prediction
Finally, we compare visually the training data with values obtained with the predictor
function:
ShowListPlotList trainingdata, Plotpx, x, 10, 10
1.5
1.0
0.5
4 2 2 4
0.5
1.0
1.5
Let’s suppose that a person was drinking water heavily contaminated with lead for
100 days. To find out the lead concentration after the exposure, urine samples were
taken during the next 200 days. Our objective is to create a function to accurately
predict the evolution of lead concentration after the first 200 days.
222
sampleslead 1, 2.58, 2, 2.95, 3, 2.66, 4, 3.83, 5, 4.18,
6, 4.18, 7, 15.1, 8, 12.65, 9, 12.53, 10, 11.57,
11, 11.69, 12, 10.97, 13, 11.97, 14, 12.18, 15, 16.56,
16, 16.2, 17, 15.9, 18, 21.02, 19, 19.52, 20, 18.63,
21, 18.2, 22, 17.72, 23, 18.63, 24, 18.24, 25, 19.18,
26, 18.37, 27, 18.81, 28, 18.41, 29, 18.36, 30, 18.31,
31, 19.87, 32, 20.08, 33, 32.34, 34, 30.29, 35, 30.77,
36, 29.19, 37, 28.35, 38, 30.07, 39, 29.58, 40, 30.7,
41, 29.19, 42, 28.17, 43, 32.65, 44, 30.96, 45, 30.6,
46, 31.97, 47, 32.95, 48, 32.04, 49, 33.9, 50, 32.59,
51, 31.74, 52, 31.18, 53, 33.22, 54, 32.06, 55, 31.64,
56, 33.04, 57, 35.26, 58, 34.64, 59, 33.78, 60, 36.14,
61, 34.41, 62, 33.87, 63, 33.02, 64, 32.61, 65, 32.58,
66, 31.76, 67, 32.17, 68, 35.32, 69, 35.04,
70, 33.88, 71, 33.14, 72, 34.5, 73, 34.54, 74, 33.24,
75, 33.58, 76, 33.75, 77, 33.13, 78, 45.23, 79, 41.97,
80, 39.77, 81, 42.11, 82, 42.49, 83, 41.58, 84, 39.78,
85, 43.89, 86, 42.41, 87, 41.36, 88, 40.3, 89, 39.76,
90, 39.84, 91, 39.77, 92, 42.76, 93, 41.29, 94, 41.15,
95, 40.71, 96, 39.84, 97, 39.62, 98, 39.89, 99, 39.24,
100, 38.8, 101, 37.56, 102, 36.53, 103, 35.64,
104, 34.83, 105, 34.07, 106, 33.36, 107, 32.68,
108, 32.01, 109, 31.37, 110, 30.75, 111, 30.13, 112, 29.53,
113, 28.95, 114, 28.37, 115, 27.81, 116, 27.26,
117, 26.72, 118, 26.19, 119, 25.67, 120, 25.17,
121, 24.67, 122, 24.18, 123, 23.7, 124, 23.23, 125, 22.77,
126, 22.32, 127, 21.88, 128, 21.44, 129, 21.02, 130, 20.6,
131, 20.2, 132, 19.8, 133, 19.4, 134, 19.02, 135, 18.64,
136, 18.27, 137, 17.91, 138, 17.56, 139, 17.21,
140, 16.87, 141, 16.53, 142, 16.21, 143, 15.89,
144, 15.57, 145, 15.26, 146, 14.96, 147, 14.66,
148, 14.37, 149, 14.09, 150, 13.81, 151, 13.54, 152, 13.27,
153, 13.01, 154, 12.75, 155, 12.5, 156, 12.25, 157, 12.01,
158, 11.77, 159, 11.54, 160, 11.31, 161, 11.08,
162, 10.86, 163, 10.65, 164, 10.44, 165, 10.23,
166, 10.03, 167, 9.83, 168, 9.64, 169, 9.44, 170, 9.26,
171, 9.07, 172, 8.89, 173, 8.72, 174, 8.55, 175, 8.38,
176, 8.21, 177, 8.05, 178, 7.89, 179, 7.73, 180, 7.58,
181, 7.43, 182, 7.28, 183, 7.14, 184, 7., 185, 6.86,
186, 6.72, 187, 6.59, 188, 6.46, 189, 6.33, 190, 6.21,
191, 6.08, 192, 5.96, 193, 5.84, 194, 5.73, 195, 5.62,
196, 5.5, 197, 5.39, 198, 5.29, 199, 5.18, 200, 5.08;
inp, out Transposesampleslead;
Once the predictor function has been created, we can plot the predicted values against the
training sample:
ShowPlotpx, x, 1, 250, Exclusions None,
ListPlotsampleslead, PlotStyle Green
40
30
20
10
4.96642
After using the “GradientBoostedTrees” method to create a new predictor function, we can
create “SHAPPlots” to measure the impact of specific features on the final prediction. Note
that in this case we download the data set using ResourceData.
wine ResourceData"Sample Data: Wine Quality";
p Predictwine "WineQuality",
Method "GradientBoostedTrees", PerformanceGoal "Speed";
pm PredictorMeasurementsp, wine;
The three features that seem to have a significant influence in the predicted quality
according to their SHAP (Shapley additive explanations) values are: Alcohol,
FreeSulfurDioxide, and VolatileAcidity.
! 225
if we want to find the best linear fit, we can just use Fit to obtain the desired equation of the
line:
model Fitdata, 1, x, x
0.405714 x 3.15238
However, let’s use this toy example to show how easy it would be to plot the line
of best fit and visualize the errors using LeastSquaresPlot, one of the functions
available in the Wolfram Function Repository:
https://resources.wolframcloud.com/FunctionRepository/resources/LeastSquaresPlot/
226
5.0
4.5
4.0
3.5
1 2 3 4 5
Input : scalar
LinearLayer
Output : scalar
Input : scalar
LinearLayer
Output : scalar
We can now extract the values of the slope and the intercept by using the options “Weights”
and “Biases”, respectively:
NetExtracttrained, "Weights" Normal
0.407973
3.14473
4.59413
5.0
4.5
4.0
3.5
1 2 3 4 5
We then modify the data to follow this pattern: {{Class, Age, Sex} survived or {Class,
Age, Sex} died}. Missing data is represented by “—”:
dataset
RuleLookup, "Class", "Age", "Sex", Lookup, "SurvivalStatus" &
ResourceData"Sample Data: Titanic Survival";
train, test Normal TakeDropdataset, 1000;
Shorttrain
Next, we define an neural network using NetChain. In this case, we are creating a multilayer
perceptron (MLP) classifier with a "FeatureExtractor" encoder to deal automatically
with mixed and missing data:
net NetChain
DropoutLayer, LinearLayer12, ElementwiseLayerLogisticSigmoid,
LinearLayer4, LinearLayer2, SoftmaxLayer,
"Output" NetDecoder"Class", "survived", "died",
"Input" NetEncoder"FeatureExtractor"
;
We now train the feature extractor and the classifier jointly, using NetTrain:
trainedNet NetTrainnet, train
49 28
actual class
ConfusionMatrixPlot
46 186
predicted class
Accuracy 0.760518
Precision survived 0.515789, died 0.869159
We leave it as an exercise for the reader to compare this MLP with the classifier we
created previously using Classify. Which one generates better results?
! 229
8, 0, 4, 1, 6
We can also train the model with new cases using NetTrain and test it:
4, 5, 6, 7, 8, 9
7
Based on the existing net, it’s very easy to initialize a new model (NetInitialize) and train it
(NetTrain) to classify handwritten inputs as either “Arabic” or “Latin”:
languageNet NetInitialize
NetJoin
NetTakenet, 9,
NetChain2, SoftmaxLayer,
"Output" NetDecoder"Class", "Arabic", "Latin",
All
, , , , , , , , , ,
The first six sections of this chapter are dedicated to , one of the most
famous mathematical constants. What looks like a simple geometric ratio
between the length of a circle’s circumference and its diameter has
surprising properties and unexpected connections to seemingly unrelated
fields. Many famous mathematicians throughout history have created
their own formulas to calculate the largest number of decimals of in the
most efficient possible way, something that they currently still do. The last
two sections discuss the Riemann hypothesis, one of the most important
unsolved problems in mathematics, and its connection to the prime
numbers.
256
the following ratio as the value of : 81
=3.16049.
The Babylonians noticed that the length of a circle was slightly bigger than the one
of an hexagon inscribed inside it getting a value of 3 18 = 3.125, quite close to the
correct value of 3.14159....
The following example describes a function to inscribe a n-sided polygon inside a
circle.
The display of a polygon inscribed in a circle can be done using the and
functions inside the command. without arguments indicates a circle
of radius r 1 and center at x, y 0, 0. x1 , y1 , ...,xn , yn represents a
polygon whose vertices are given by the coordinates x1 , y1 , ...,xn , yn . We use to
place text inside the graphical object. In the function definition we include ? and
? to execute the function only if n is an even positive integer.
polig1n ? EvenQ ? Positive : GraphicsCircle,
LightGray, PolygonTableCos2 k n, Sin2 k n , k, n ,
Inset "Inscribed Polygon" , 0, 0 , ImageSize 130
We can add a warning message that will be displayed if the requirements for n are not met.
This function must be defined immediately after the previous one. This way, will
check first whether n is a positive even number and if not, it will display the warning
message. If we reverse the order in which we define the functions, it will not work.
polig1n : "n must be an even positive number."
Inscribed Polygon
of extensive research since 1999. He lived at the end of the Ancient Greece era, the
same period as other famous mathematicians such as Euclid (ca. 325–ca. 270 B.C.)
and Eratosthenes (ca. 284–ca. 192 B.C.). They were all located in a small
geographical region consisting of several cities around the Aegean sea, a lucky
coincidence that has not happened again in the history of humankind. Archimedes’
life is the subject of many legends of which the best-known one is probably the
story of how he was able to set on fire a large fleet of Roman galleys that were
trying to attack Syracuse. His extraordinary intelligence was acknowledged even by
his own enemies. Plutarch tells us that during the siege of Syracuse a Roman
soldier found the old man so absorbed in his geometric drawings that he ignored his
orders and was killed as a result. Marcellus, the person in charge of the Roman
troops, had given express orders to spare his life and when he heard the news he
punished the soldier, mourned Archimedes’ death, and buried him with honors.
About the The Archimedes Palimpsest: http://www.archimedespalimpsest.org/
In Archimedes’ tomb there is a cylinder enclosing a sphere, a reference to one of
his greatest findings, that the sphere has 2/3 the volume of its circumscribing
cylinder. The same 2/3 ratio applies to all surfaces.
Next we are going to show how to inscribe a sphere inside a cylinder.
To show a cylinder enclosing a sphere, we use the syntax Graphics3D[{{properties,
Cylinder}, {properties, Sphere}}, options]. We use Opacity to define the degree of
transparency in the cylinder. We can also choose the color and other relevant details using
options. Since we are not using any arguments for either Cylinder or Sphere, the
commands assume by default that r 1, x, y, z 0, 0, 0 and, in the case of the cylinder,
h 1.
Graphics3DOpacity0.2, Blue, Cylinder , Green, Sphere ,
Boxed False, ImageSize 100
The same graph can be done with ContourPlot3D, a Mathematica command used for any 3D
function for which we know its analytic expression. PlotLabel, a general graphics option,
adds a legend to the graph. In this example we use the equations of both geometric figures
as legends and display them on separate lines with TableForm.
234
The method that follows enables us to verify that the ratio between the volume of a
sphere and that of its circumscribing cylinder is 2/3 (without using the formulas for
their respective volumes).
We first compute the volume of the sphere, Vsphe , and of the cylinder ,Vcyl , with
r 1 by integrating their analytical expressions and then calculate Vsphe /Vcil .
Archimedes obviously did not use this method since he didn’t know integral
calculus.
We use Boole to indicate that the integral of the points that do not satisfy the condition is 0.
volsphe Integrate Boolex ^ 2 y^2 z ^ 2 1,
x, 1, 1 , y, 1, 1 , z, 1, 1
4
3
volcyl
Integrate Boolex ^ 2 y ^ 2 1, x, 1, 1 , y, 1, 1 , z, 1, 1
2
since the area of the circle is Ac r2 , then Ac , and from that formula we can
deduce that the value of is going to be between the value area of the inscribed
polygon, Ains and that of the circumscribed polygon Acir , that is, Ains Ac
Acir .
The previous procedure can be easily modeled in Mathematica. We use GraphicsRow to
display all the figures in the same row. The higher the number of triangles, the closer the
resemblance of the resulting graphical object to a rectangle.
Manipulate
GraphicsRowTableGraphicsEdgeFormOpacity1, TableHuei n, .5,
Diski Sin n , 1 ^ i 1 1 Cos n 2 , 1,
1 ^ i 2 1 n, 1 ^ i 2 1 n , i, n , n, 4, m ,
m, 6, "number of sectors" , 4, 20, 2, Appearance "Labeled"
The difference between using the exact value of and its approximation with a
precision is insignificant.
16
2 10 sunDist
0.0000939951143117 m
Despite this fact, the number of functions created to compute with the largest
number of decimals in the fastest possible way since Archimedes is remarkable.
Some of the best mathematicians in history have developed their own approaches.
In the following paragraphs, we’ll show some of them.
Although Archimedes’ method is inefficient, little progress was made until Newton
in 1666 described a radically new method.
(Ve)The Discovery That Transformed Pi: https://www.youtube.com/watch?v=gMlf1ELvRzc
236
Newton generated this expression for by considering the shape below. His
approach to calculate the famous constant is based on computing the area of a
semicircle using an integral.
GraphicsLightBlue, Disk0.5, 0 , 0.5, 2 Pi 3., Pi ,
LightGreen, Polygon0.25, 0 , 0.5, 0 , 0.25, Sqrt3 4. ,
Circle0.5, 0 , 0.5, 0, Pi , Line0, 0 , 0, 0.6 ,
Line0.5, 0 , 0.25, 0.5 Sqrt3 2 , 0.25, 0 , ,
TextStyle"A", 16, 0., 0. , 1, 1 ,
TextStyle"B", 16, 0.26, 0 , 1, 1 ,
TextStyle"C", 16, 0.5, 0 , 1, 1 ,
TextStyle"D", 16, 0.25, 0.5 Sqrt3. 2 , 1, 1 ,
Axes True, False , Ticks 1 4, 1 2, 1 , 1 2 ,
PlotRange 0.1, 1.1 , .1, 0.6
x x2 , x x2
3
Simplify 3 32
4
Contrary to expectations given the original definition of as the ratio between the
perimeter of a circumference and its diameter, many of its later definitions do not
imply a geometric relationships. The original one was of a practical nature. Other
non-geometrical definitions are also valid. As a matter of fact, we could take any of
them as the definition of . Next we present some examples.
The formula below is probably the easiest way (but not the most effective one) to calculate
. Notice that Mathematica is able to obtain an exact result for infinite series.
1j
4
j0
2j 1
The next one shows as the result of an integral computation. It’s due to Leibniz, whose
integration methods won over those from his contemporary fellow mathematician Newton.
1
t
0 t2 1
2
238
There are very curious methods such as the ones that compute using a series of continuous
fractions. Here’s one example:
4 1 1 3 22 5 32 7 42 9 52 11 62 13
4 1
1
4
9
3
5
16
7
25
9
36
11
13
True
It’s not by chance then that in 1914 Ramanujan found the following surprising formula:
2 2 n 4 j 26 390 j 1103
ramapin :
9801 j0 44 j j 4 994 j
1
TableFormTablen, N , 10, n, 1, 5
ramapin
1 6.395362624 1016
2 5.682423256 1024
3 5.238896280 1032
4 4.944187579 1040
5 4.741011769 1048
If we add a 6th term we’ll get an error message. The reason is that the series convergence is
so fast that we need to substantially increase the computation precision. To do that we
change the value of the variable $MaxExtraPrecision and limit the scope of the new
assignment with Block to the previous expression only; otherwise, the new value would be
applied to all calculations done after the latest definition.
Block$MaxExtraPrecision 10 000,
1
TableFormTablen, N , 10, n, 1, 10
ramapin
1 6.395362624 1016
2 5.682423256 1024
3 5.238896280 1032
4 4.944187579 1040
5 4.741011769 1048
6 4.598865016 1056
7 4.499979208 1064
8 4.433276097 1072
9 4.391477319 1080
10 4.369600809 1088
By March 14, 2019 (the Pi day) the approximation reached 31.4 trillions digits
(31.4 1012 ) using the Chudnovsky algorithm (it is the used by Mathematica). To
put that figure in perspective, if we wanted to print that many digits we would need
a library of 30 million volumes (500 km worth of shelves) .
n 1k 6 k 13 591 409 545 140 134 k
chudnovskyn : 12
k0 3 k k 3 640 3203k 32
To verify that a sequence is correct, we compare part of it with the ones obtained
by different methods. For example:
We can combine RealDigits and [[...]] to show the digits located in positions n to m + n after
the decimal point. We extract the digit n+1 since 3, the first digit, is not decimal. We then
use n m 5 to increase the precision with 5 more digits than the required ones to
calculate m n.
pinthn, m : RealDigitsN 3, n m 5 1, n ;; n m
240
For example, we can check that after position 762, the digit 9 appears six consecutive times.
pinth760, 10
3, 4, 9, 9, 9, 9, 9, 9, 8, 3, 7
But, how can a record be tested to see if it is still valid? Usually the Bailey–
Borwein–Plouffe’s formula (1996) is used.
n 1 4 2 1 1
bayley1n :
k0 16k 8k 1 8k 4 8k 5 8k 6
When throwing the needle, there are two possibilities: either the needle crosses one
of the lines or it doesn’t. The probabilities associated to each of those outcomes are,
241
The previous experiment has been performed on several occasions, and it has
always required thousands of throws to achieve a precision better than two digits.
Nowadays we use simulations where the throws are replaced by computer-
generated (pseudo)random numbers.
Simulation-based procedures, usually known as Monte Carlo methods, enable us to
calculate probabilistically in many different ways.
A very simple example to approximate the value of is to first draw a circle of
radius 1 circumscribed by a square with sides of length 2 and then to drop a ball m
times onto the square counting how many times (n) the ball falls inside the circle.
Assuming that the ball can land with equal probability on any point in the square,
the probability that it will land inside the circle is given by the ratio between the
area of the circle and that of the square, that is, mn r2 l2 , since l d 2, then
4 n/m .
The above experiment can be simulated with Mathematica. We just need to generate m pairs
of real numbers between 1 and 1 and count how many of them “land” inside a circle with
radius r 1 (we can use to check that the pairs verify x2 y2 <1 and count with
$ how many they are).
pibm : Moduledata, n ,
data RandomReal 1, 1 , m, 2 ;
n LengthCasesdata, x, y ; x ^ 2 y ^ 2 1;
4. n m
The method converges very slowly.
pib100 000
3.14408
The previous function can be extended to display the results graphically. Notice that text
needs to go between quotation marks and that inside a string of text we can use \t or \n to
insert a tab or a new line, respectively.
ManipulateModuledata, n ,
data RandomReal 1, 1 , m, 2 ;
n LengthCasesdata, x, y ; x ^ 2 y ^ 2 1;
Text StyleColumnGraphics
PointSize0.006, Orange, Disk0, 0 , 1, Black, Pointdata ,
Row"inside: ", n, "\toutside: ", m n, "\ntotal: ", m,
"\t 4 ", n, "", m, " ", 4. n m , "Label",
m, 100, "Random pairs generated" , 1, 10 000,
1, Appearance "Labeled"
242
3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, 1
The 1 at the end indicates that the first element corresponds to the integer part. If we just
want the decimal part we can subtract 3 from 3.14159... : 3.14159 ... 3 =0.14159. For
example, the first 10 decimal digits of are:
243
1, 4, 1, 5, 9, 2, 6, 5, 3, 5
The following expression enables us to find a specific digit in a short time. In the example
below we find the 10,000,000th digit.
TimingFirstRealDigitsPi, 10, 1, 10 ^ 7
9.875, 7
This short expression tells us how often numbers 0–9 appear in the first 100,000 decimals of
.
SortTallyRealDigitsN 3, 100 000 1
0 9999
1 10 137
2 9908
3 10 025
4 9971
5 10 026
6 10 029
7 10 025
8 9978
9 9902
The previous output seems to confirm that in base 10 all digits are present with
approximately the same probability. This indicates that they are distributed
following a random discrete uniform distribution.
To test this hypothesis for the first 10,000 decimals we use a combination of the functions
DistributionFitTest and DiscreteUniformDistribution.
vals RealDigitsN 3, 10 000 1 ;
P-Values above 0.05 (as in this case) suggest that the data come from a discrete uniform
distribution. If this were true then would be normal.
The command below returns the frequency of numbers 0–9 as a function of the total number
of digits used in the calculation.
Manipulate
BarChartBinCountsRealDigitsN 3, 10 000 1, 1 ;; u , 0, 10, 1 ,
ChartLabels Range0, 9,
u, 100, "Total number of digits" ,
1, 10 000, 1, Appearance "Labeled"
244
3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, 2, 3, 8,
4, 6, 2, 6, 4, 3, 3, 8, 3, 2, 7, 9, 5, 0, 2, 8, 8, 4, 1, 9,
7, 1, 6, 9, 3, 9, 9, 3, 7, 5, 1, 0, 5, 8, 2, 0, 9, 7, 4, 9,
4, 4, 5, 9, 2, 3, 0, 7, 8, 1, 6, 4, 0, 6, 2, 8, 6, 2, 0, 8,
9, 9, 8, 6, 2, 8, 0, 3, 4, 8, 2, 5, 3, 4, 2, 1, 1, 7, 0, 6, 7
Notice that some sublists contain more than one element. This happens when a digit repeats
itself once or more consecutively. With the next function we can count the size of each
sublist:
Length SplitRealDigitsPi, 10, 100 1
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1,
1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1
We see that in 6 cases a digit repeated itself once, and there were no instances of a number
appearing consecutively 3 or more times.
The next function counts the number of sublists with 1, 2, ..., n terms. Using Drop we
eliminate the first element since it corresponds to the number of empty sublists. The
function is then applied to the first 106 digits of .
data1
DropBinCountsLength SplitRealDigitsPi, 10, 1 000 000 1 , 1;
We present the results in table format: 1 indicates that a digit does not appear in consecutive
positions, 2 that the same digit appears in two consecutive positions and so on.
TableFormTablei, i, Lengthdata1 , data1 ,
TableHeadings "repetitions", "frequency" , None
repetitions 1 2 3 4 5 6 7
frequency 809 776 81 256 7976 830 77 12 1
245
By adding Position we can identify the order in which a certain repetition happens. For
example, to check the positions in which the sequence {9,9} appears within a 100 decimal
digits approximation of , we would type:
PositionSplitFirst RealDigitsPi 3, 10, 100, 9, 9
42
75
To relate the sublist position to the number of digits, we proceed as follows:
PartAccumulateLength SplitRealDigitsPi 3, 10, 100 1 ,
42, 75 1 1
44, 79
This result indicates that the sequence 99 appears first in the 44th and 45th digits and the
next one in the 79th and 80th ones. We can check it using the table below. In it, decimals
are grouped by rows containing 10 digits each.
TableFormPartitionFirst RealDigitsPi 3, 10, 100, 10,
TableHeadings Range10, Range10
1 2 3 4 5 6 7 8 9 10
1 1 4 1 5 9 2 6 5 3 5
2 8 9 7 9 3 2 3 8 4 6
3 2 6 4 3 3 8 3 2 7 9
4 5 0 2 8 8 4 1 9 7 1
5 6 9 3 9 9 3 7 5 1 0
6 5 8 2 0 9 7 4 9 4 4
7 5 9 2 3 0 7 8 1 6 4
8 0 6 2 8 6 2 0 8 9 9
9 8 6 2 8 0 3 4 8 2 5
10 3 4 2 1 1 7 0 6 7 9
The function SequencePosition can be used to find the position where the digit n will be
repeated r consecutive times.
In the example below, using the first 10,000,000 digits of , we find the cases in which the
number 9 appears consecutively exactly 7 times.
digitsPi FirstRealDigitsNPi, 10 ^ 7;
and 1 1012 , respectively. This means that, using a approximation of 1012 digits,
we’d most likely see one sequence of 12 consecutive 0s. The same could be said
for other sequences.
In the example below, we look for the pattern 3131313 in the first then million digits of Pi:
SequencePositiondigitsPi, 3, 1, 3, 1, 3, 1, 3
These types of patterns can lead us to a variant of what Borges proposed in his
famous work The Library of Babel. The text describes a library containing all the
books ever written and the ones that may be written in the future (actually, Borges
mentions the number of pages and the number of lines per page but this is
irrelevant for the purpose of our argument). Let’s suppose that we could write a
book where all its letters were a, followed by one containing only b’s and so on
until writing a final book with all z’s. This way we would cover all the possible
combinations of letters. The resulting number would be huge, immensely bigger
than the number of atoms in the universe. Nevertheless it would be a finite number.
A variant of the previous story consists of replacing the order of the letters with
probabilities. Let’s supposed that we have a roulette with 35 positions representing
the 26 letters of the English alphabet plus certain symbols such as punctuation
marks, whitespace characters, etc. If we played such roulette enough times, we
would end up not only with every possible word, sentence, and paragraph but also
with every possible book! The only problem would be the time and the space to
store the results. We can speculate that the same applies to . For example, if we
express it in base 35 and assign the digit 1 to the letter a, the digit 2 to b and so on,
with a number of digits big enough we will find the library of Babel (it doesn’t
matter how big the number needs to be since has an infinite number of digits).
This means that all books: past, present, and future, have already been written and
they are in the decimals of .
Look at: https://community.wolfram.com/groups/-/m/t/1623981
Surprisingly, in contemporary physics when we speculate about the existence of
parallel universes, we arrive at what we might consider an extreme version of the
previous speculation. If there is an infinite number of universes, all the possibilities
that don’t violate the laws of physics occur. Therefore, right now there’s someone
identical to you doing exactly the same as you’re doing. According to this theory
all the variants of your life will happen although in different universes.
2.7182818284590452353602874713526624977572470936999595749669676277240766303535475
94571382178525166427
As with , the number can be computed using several different methods (Click on + in the
next cell).
When is expressed in decimal form, something funny happens (we use
NumberForm combined with ExponentFunction to avoid the output being displayed in
scientific notation).
NumberFormNPi 163
, 30, ExponentFunction Null &
!
262537412640768743.999999999999
We can see that the first 12 decimal digits are all 9s. This could give the false impression
that all the decimal digits are 9, but that is not the case. It would be enough to increase the
precision by one more digit to check that it is only a lucky coincidence.
In a separate category we include imaginary numbers, those whose square is a
negative number as in the solution to the equation x 2 1 0.
Notice that to express imaginary numbers, Mathematica uses the symbol and not the Latin
letter i.
Solvex ^ 2 1 0, x
Numbers consisting of a real part and an imaginary one are called complex
numbers. As a matter of fact, all real numbers can be considered a special case of
complex numbers where the imaginary element is 0. Sometimes strange
connections exist among different numbers as in the case of the following equation
discovered by Euler:
x cosx sinx x
We can see that in just one expression there are two transcendental constants and
the imaginary unit connected to each other and, the most surprising thing, the result
is the integer 1. It’s hard to believe that all these strange connections between
numbers are random.
1
Pi is also connected to one of Riemann’s Zeta functions: (s) =
i1 ks
, which we
will discuss in further detail in the next section. We can see that % [2] converges
to:
248
2
6
also appears in fundamental notions in physics such as Heisenberg’s uncertainty
principle or Einstein’s general relativity theory. It seems that is much more than
the ratio between the perimeter of a circumference and its diameter!
Given that s is a complex variable, (s) is also complex. We can visualize its real component
Re s and its imaginary one Im s with the following Mathematica command:
Plot3DReZetaa b I , a, 10, 10 , b, 10, 10 , Mesh None,
Plot3DImZetaa b I , a, 10, 10 , b, 10, 10 , Mesh None
,
249
It can be shown (using expansions of analytic functions, a complex analysis concept), that
for negative even values, that is when s 2 n , s 0. We can check it for some cases
(in the example below when n ranges from 1 to 100 in steps of 10).
TableZeta 2 n , n, 1, 100, 10
0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0.03
0.02
0.01
Re
30 25 20 15 10 5 Im
0.01
0.02
0.03
Another way to visualize this is by plotting 1/ (s), with s x y . Remember that
x+y = x2 y2 . Using the inverse expression, the zeros become asymptotes that look like
columns. In the graph below, we can see these “columns” appearing when s 2 n.
(This function and several others that follow are based on the ones described by Stan Wagon
in his book Mathematica in Action).
Plot3D1 AbsZetax y, x, 20, 2 , y, 1, 1 ,
MeshFunctions 3 &, MeshStyle Blue, Mesh 10,
PlotPoints 60, MaxRecursion 3 ,
Boxed False, BoxRatios 5, 2, 2 , AxesLabel "x", "y", None ,
Ticks Automatic, Range 22, 2, Range0, 4
250
1
1 Re
2
1
Im
2
30 20 10 10 20 30
1
In Mathematica the function ZetaZerok represents the k-th zero of s in the critical
1
line s 2
i b with the smallest imaginary part. For example the first zero corresponds to:
s NZetaZero1, 15
0.5000000000000 14.1347251417347
0
We’ve seen that the first zero is approximately at 14.1347251417347 . If we would like to
visualize all the existing zeros for a given interval, we can use the following function in
terms of the imaginary component (remember that the real component is always 1/2).
ManipulatePlotRiemannSiegelZt, t, 0, k , Epilog PointSize.02, Red,
PointTableImZetaZeron, 0 , n, k , k, 50, 100
251
Next, we plot 1/ (s) below. We can see that all the zeros (columns) are clearly aligned.
Plot3D1 AbsZetax y, x, 4, 5 , y, 10, 40 ,
MeshFunctions 3 &, MeshStyle Blue, Mesh 10,
PlotPoints 60, MaxRecursion 3 , ViewPoint 8, 1, 3 ,
Boxed False, BoxRatios 5, 10, 2 , AxesLabel "x", "y", None ,
AxesEdge 1, 1 , Automatic, Automatic ,
Ticks Automatic, Range0, 30, 10, Range0, 4 ,
ClippingStyle None, PlotRange 0, 5
As of the end of 2021, all the non-trivial 1013 zeroes that have been found lie on the
critical line. However, in mathematics, this doesn’t count as a proof. Of all the
approaches that have been proposed to prove the Riemann hypothesis, one of the
most curious ones comes from D. Hilbert and G. Pólya (proposed independently).
They suggested that the zeros of the Riemann Zeta correspond to the eigenvalues of
a linear operator given by an infinite matrix. In 1972 the mathematician Hugh
Montgomery and the physicist Freeman Dyson went even further and proposed a
connection between the zeroes and heavy nuclei energy levels.
We can model these energy levels using what is known as the Spectrum of
Riemannium:
https://www.americanscientist.org/article/the-spectrum-of-riemannium
In Mathematica, we can explore these two theories using random Hermitian
252
That is:
1 1 1 1
s 1 1 1 !
s s s
2 3 5 p 1 ps
Prime numbers seem to be randomly distributed. For a given prime number, there’s no
general formula that will give us the next one.
TablePrimen, n, 45, 60
197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281
40
30
20
10
n
50 100 150 200
gives the option to choose an specific algorithms. The time of computation for
some of then are shown below
BarChartTableFirstAbsoluteTimingPrimePi10 ^ n,
FirstAbsoluteTimingPrimePi10 ^ n, Method "LMO", First
AbsoluteTimingPrimePi10 ^ n, Method "Meissel" , n, 7, 11, .5 ,
ChartLegends "Automatic", "Lagarias Miller Odlyzko", "Meissel"
0.20
0.15
Automatic
0.10 LagariasMillerOdlyz
Meissel
0.05
0.00
The trend is that the frequency of prime numbers decreases as n gets bigger.
There are several approximations to calculate (n), such as: t/Log(t), or
z
lin #0 d t log t (in Mathematica LogIntegralz), but the best one is done by
using Riemann’s Zeta function. Specifically, the program uses the function R n =
RiemannRx .
1n
For x 0, Rx
n $n li x n.
The t/Log(t) approximation can be found using the asymptotic term for PrimePi at Infinity:
AsymptoticPrimePit, t Infinity
t
logt
In the following plot, we compare the real value of (n) with all these approximations.
254
We can visualize the comparison in an even better way by representing the difference
between each of the alternative approximations and (n). In the case of R(n), the
approximation is very accurate since, at least in the chosen interval, the error never gets too
big (actually it never exceeds the real value by more than 3).
Plotn Logn PrimePin, LogIntegraln PrimePin,
RiemannRn PrimePin , n, 200, 1000 , PlotLabels "Expressions"
10 lin n
5
Rn n
400 600 800 1000
5
10
15
n
20 n
logn
25
analyze the composition of the light coming from space. Many of the objects that
looked like blurry spots under the telescope became galaxies with myriads of stars.
It was discovered that those galaxies were moving apart from each other: the
universe was expanding, an expansion that started around 14 billion years ago. It
turned out that the visible stars and galaxies contain less than 5% of the mass of the
Universe. The rest is dark energy and matter, we know it exists (at least the dark
matter) but we don’t really know what it is. The idea of the existence of dark
energy (“a riddle wrapped inside a mystery”) started with an amazing discovery
(1998): The expansion of the universe is accelerating, a discovery that was worth a
Nobel Prize in 2011. And the discovery race continues, promising new surprises:
the important thing is still the journey.
For astronomical calculations, Mathematica has several functions: Comet (or
CometData) , Exoplanet (or ExoplanetData), Galaxy (or GalaxyData), MinorPlanet
(or MinorPlanetData), MoonPosition, Sunset, Nebula (or NebulaData), Planet (or
PlanetData), Pulsar (or PulsarData), SiderealTime, StarCluster (or
StarClusterData), Star (or StarData), SunPosition, Supernova (or SupernovaData),
Sunrise and many more. These functions replace and extend AstronomicalData
although you can still use it.
The syntax of all these functions is similar to that of other computable functions.
Example: Function"name", "property" where "name" or entity (Entity) is an
object and "property" is a characteristic or parameter of the object.
For example: to refer to the solar system planets we can type either PlanetData[] or:
EntityValue"Planet", "Entities"
If we place the cursor on one of the list elements in the output, for example "Mercury",
Mathematica displays Entity"Planet", "Mercury". This tells us that "Mercury" belongs to
the "Planet" entity.
We can get the available properties for planets using the free-form input: Insert Inline
Free-format input and then type “planet” or “planets”. Also, in this example and the
following one, we use Shallow or Short to avoid getting the complete output. If you want to
see all the properties just remove Shallow when executing the command:
The next output informs us about the property "OrbitPeriod" for each one of the planets.
Notice the use of "EntityAssociation" to link names with properties.
! ! 257
Here we show Mercury’s average orbit velocity writing Mercury average velocity or
EntityValue[Entity[“Planet”,”Mercury”],”AverageOrbitVelocity”]
Mercury PLANET
47.4 kms
Sometimes, we may want to get the value without its corresponding unit. In those cases, we
can use QuantityMagnitude.
QuantityMagnitude Mercury PLANET
47.4
The result is given in light-years (ly), a common unit to express distances between stars.
The parsec (pc), and its multiples (kiloparsec or kpc, and megaparsec or Mpc), are used to
measure very long distances such as distances between galaxies. Let's see the relationship
between a parsec and a light year.
Result
3.262 ly
3.262 ly
Some of the functions related to astronomy need the precise location for which the
information is desired. By default they will use our current time and position.
Example: The following function returns the sunrise time on February 28, 2023, at a place
located 40º N and 10º W, a location that is one hour ahead of GMT as we can see.
258
Dates and hours are of crucial importance in astronomy. The existence throughout
history, even nowadays, of different calendars makes it difficult to compare dates
of astronomical and historical events. To avoid this problem we use Julian dates. A
Julian date (not to be confused with the Julian calendar: Julian year) indicates the
days that have gone by since January 1, 4713 B.C. at 12:00 PM UT (November 24,
4714 B.C., in the proleptic Gregorian calendar). The name of the Julian date comes
from Julius Scaliger (not Julius Caesar) who proposed it in 1583 after realizing that
such a day was the least common multiple of 3 calendar cycles used by the Julian
calendar. Using 12:00 PM as the starting time instead of 12:00 AM has the
advantage that astronomical observations, usually done at night, are recorded on the
same day. Starting with Mathematica 10.2 the function JulianDate is available. The
function converts dates from our calendar, the Gregorian one, to Julian dates. It
takes into account the modifications to the Julian calendar done by Gregorio XIII in
1582 in most Christian countries that improved the handling of leap years, but in
which the year 0 doesn’t exist.
In England and other countries, the Gregorian calendar was introduced at a later date. This
fact makes statements such as that Shakespeare and Miguel de Cervantes died on the same
day incorrect.
dateSh William Shakespeare PERSON "
Actually, it is not clear when Cervantes died. Although April 22 is the most frequently used
date, the parish register shows April 23. In any case, Cervantes’s and Shakespeare’s deaths
happened 10 days apart. The reason for the confusion is that in England they were still using
the Julian calendar. To compare both dates, we need to convert Shakespeare’s death date to
the Gregorian calendar.
CalendarConvertdateSh, "Gregorian"
We can alternatively find out the Spanish writer’s death date using the Julian calendar and
calculate the difference in days between both deaths.
JulianDatedateSh JulianDatedateCe
11.
We can now use the above mentioned approach to convert the current date to a Julian date.
We have added Dynamic to dynamically update the calculation with an interval defined with
UpdateInterval, in this case 1 second.
! ! 259
2459870.19238
260
You can also make analemma for other celestial objects such as Venus or Mercury.
With less patience and without having to wait for a year, we can simulate an
analemma with Mathematica from our own computer.
We need to define the location (latitude and longitude).
GeoPosition40.97, 5.67
SunPosition returns the sun position from our position (azimuth and latitude) at 9:00 AM in
GMT (choosing TimeZone0) on January 1, 2016.
SunPositionsalamanca, DateObject2016, 1, 1, 9, TimeZone 0
132.99, 10.15
We apply the same function to get the position every 5 days, at the same time, to complete a
year:
GraphicsOrange, PointSizeLarge,
Point MapQuantityMagnitude, SunPositionsalamanca,
DateRangeDateObject2016, 1, 1, 9, 0, TimeZone 0,
DateObject2016, 12, 31, 9, 0, TimeZone 0, 5
"Values", 2, Frame True
45
40
35
30
25
20
15
10
100 110 120 130
8.3 Stargazing
Clear"Global` "
enabled the measurement of time with great precision. Centuries later, telescopes
mounted on satellites and atomic clocks heralded a new revolution.
Moon
E W
S
The program uses “SkyChart”, a function from the repository. To find out more details
about its functionality, we type:
ResourceObject"SkyChart"
Name: SkyChart »
ResourceObject Type: Function
Description: Visualize the sky for a specified location, time and date
With the instruments and methods available to astronomers nowadays, the information that
we can obtain about stars is very extensive. In the next example, we show the properties
most commonly used when describing stars:
properties "AbsoluteMagnitude", "ApparentMagnitude",
"RightAscension", "Declination", "Altitude", "Azimuth",
"Constellation", "DistanceFromSun", "SpectralClass";
Let’s find those properties for Polaris, the North Star:
Here are the 10 closest stars to Earth (the Sun is excluded), many of them not visible to the
naked eye:
ThroughEntityListEntityClass"Star", "StarNearest10"
"Name", "DistanceFromEarth", "ApparentMagnitude"
Barnard's Star 5.93796 ly 9.54
Lalande 21185 8.30785 ly 7.49
Luyten 7268 A 8.56318 ly 12.57
Luyten 7268 B 8.56318 ly 12.7
Proxima Centauri 4.2465 ly 11.01
Rigil Kentaurus 4.39282 ly 0.01
Toliman 4.40311 ly 1.35
Sirius 8.59682 ly 1.44
Sun 1.01234 au 26.72
Wolf 359 7.79346 ly 13.45
We can immediately notice that most of them are not visible since their apparent magnitude
is bigger than 5. With the exception of the Sun, the star with the biggest apparent magnitude
is Sirius, a star that we can see during winter nights (in the Northern hemisphere) next to the
Orion constellation. The closest one is Proxima Centauri that is only visible in the Southern
hemisphere (Proxima Centauri rotates around the double system Centaurus A and B in a
500,000-year period. By chance, it’s currently in its orbital position closest to the solar
system).
The number of stars visible without the use of technology is very small compared to the
number of stars in our galaxy. The command below generates a plot representing the naked-
eye stars and their distances to Earth in light years.
ListPlotSortEntityClass"Star", "NakedEyeStar""DistanceFromSun",
AxesLabel "stars", "ly"
ly
1500
1000
500
stars
2000 4000 6000 8000
From the graph above we can see that almost all of the visible stars are less than 1,500 ly
away. Our galaxy, the Milky Way, has a width of approximately 100,000 ly. Therefore, with
our bare eyes we only see a very small fraction of the stars in our own galaxy, most of them
the ones closest to Earth. The previous figure refers to the number of potentially visible stars
without light pollution or moonlight. In practice, the actual number is 2,000 or 3,000, since
we only see part of the sky depending on our position.
Besides stars with apparent magnitude 5 or less, the only other celestial bodies
visible using only our eyesight are: the Moon, the 5 nearest planets and,
264
sporadically, some comets. In the Northern hemisphere, the only object visible that
doesn’t belong to our galaxy is the Andromeda galaxy. In short, we see an
insignificant fraction of our own galaxy, which in turn is just one among billions of
galaxies.
We’ve seen that among the properties of Star (or StarData) and other astronomical
functions are: “RightAscension”, “Declination”, “Altitude”, and ”Azimuth”. These
properties are commonly used to indicate the position of a celestial body. In
equatorial coordinates, “RightAscension” and “Declination” are similar to latitude
and longitude, but they refer to the celestial sphere so they are independent of the
observer . Another type of coordinates widely used are the alt-azimuthal (alt/az)
that depend on the observer: The “Altitude” (alt) is the height of the star over the
horizon. It goes from 0º to 90º and has a positive sign for stars located above the
horizon and a negative sign for the ones located below it. The ”Azimuth” (az) is the
arc in the horizon measured counterclockwise from the South point until the
object’s vertical. Its value ranges from 0º to 360º.
For further details see: The Celestial Sphere, http://demonstrations.wolfram.com/TheCelestial-
Sphere, by Jeff Bryant.
The next example shows Sirius’s displacement on the first of each month at 22:00 h using
alt/az coordinates in local time.
Planet
sirius TableQuantityMagnitudeEntityValue
DatedEntity"Star", "Sirius", DateObject2022, i, 1, 22, 0, 0,
"Azimuth", "Altitude", "AngularDegrees", i, 1, 12
118.713 6.38653
143.138 23.8375
171.697 31.9130
205.412 28.4294
231.945 14.4112
253.045 6.00471
271.772 28.0331
296.751 50.3112
346.221 65.1931
46.9011 58.3531
78.9981 37.7519
98.8455 15.5399
! ! 265
20
0
altitude
20
40
60
As we can see, there are negative values that correspond to the period of the year in which
Sirius falls below the horizon and as a consequence is not visible. As a matter of fact, the
Egyptians considered that the appearance of the star marked the beginning of a new year.
In the previous example, we didn’t specify the observer’s location and the local time zone.
They can be shown:
FindGeoLocation, $TimeZone
We can display the time in zone 0, using the Coordinated Universal Time (UTC)
DateObjectNow, TimeSystem "UTC", TimeZone 0
However, it’s important to keep in mind that the function actually gives us the position of
the server that we are using to connect to the Internet based on the IP address. Sometimes it
may be far away from our real position. This would be the case if, for example, we were
using a phone to surf the net. We can check it by showing the obtained position in a map:
GeoGraphics
GeoVisibleRegionBoundaryFindGeoLocation, GeoZoomLevel 12
266
Naked-eye stars display an amazing regularity. It’s possible to forecast the location
of each star without prior knowledge of celestial mechanics. If we observe a star at
a certain hour, for example at 24:00 h, ignoring the displacement due to the
inclination of the Earth’s axis with respect to the ecliptic, the next day it will be on
the same spot 4 minutes earlier, that is at 23:56 h, completing a cycle in a year. The
explanation that the Greek civilization and others provided for this fact was to
assume that all the stars (which they called fixed, to distinguish them from the
planets or wandering stars that did not exhibit such behavior) were glued to a dome
that rotated with a daily cycle of 23 h and 56 min, the sidereal day (or stellar day).
1 sidereal day
We can use UnitConvert to find out the actual duration. When executing the command
above, the predictive interface will do the conversion automatically.
UnitConvertQuantity1, "SiderealDays",
MixedUnit"Hours", "Minutes", "Seconds"
23 h 56 min 4.09054 s
However, over very long periods we can notice discrepancies that at first sight may
seem insignificant during a person’s lifetime but become quite obvious after several
generations. Hipparchus of Nice (c. 190 BC–c. 120 BC) compared the stellar maps
available at the time (celestial cartography is over 2,500 years old!) and realized
that there was a relative movement of the stars with respect to the ecliptic. This
movement is known today as the precession of the equinoxes. This is probably his
most famous discovery. The displacement happens when the Earth’s axis, moving
along a circumference with respect to the ecliptic, rotates with a period of 25,771
years.
http://demonstrations.wolfram.com/PrecessionOfTheEquinoxes
As a result, the North celestial pole keeps on moving. Today it is close to the North
Star but 4,800 years ago it was pointing toward Thuban (Alpha Draconis). William
Shakespeare did not realize that, when in his play Julius Caesar stated: “But I am
constant as the northern star, Of whose true-fix’d and resting quality There is no
! ! 267
11.75 masyr
means milliarc seconds per year. It’s a value that cannot be observed during a
lifetime. We need to keep in mind that a telescope on the ground can rarely see details
smaller than 1 arc second. However, during long periods even the shapes of the
constellations change.
Astrology (a pseudoscience) attributed predictive powers to the constellations. For
example, they were supposed to determine the future of people born under them.
The firmament was divided into 12 signs corresponding approximately to the
number of lunar cycles in a year. Each division was assigned a symbol called a
zodiac sign. The names of the zodiac are associated with the first constellations
named by the Greeks during the 5th century BC, even though the Babylonians were
the first ones to mention them 4,000 years ago. The starting point was the
constellation pointing toward Aries (the moment when the Sun moves from the
south celestial hemisphere to the north one coinciding with the spring equinox) in
the 5th century BC. Today, we still use the same division although the
constellations are now in a different location to where they were 2,500 years ago.
The real astronomical zodiac dates correspond to the constellation located behind
the solar disk during the spring equinox, in the direction opposite to the Earth’s
direction (the Sun actually passes through 13 constellations and not 12, the 13th
one is Ophiuchus). The zodiac signs must be adjusted by a month to adapt to the
current astronomical reality, so chances are you may have to revise your zodiac
sign (although it wouldn’t be very useful).
The natural language input below returns the location of the Ursa Minor stars in the year
100,000 compared to their current position (dashed line). The rest of the stars experience
similar changes.
268
The solar day (24 hours) is defined as the time it takes for the Sun to return to its
culmination point (its highest point in the sky). That is, the Earth in 24 hours spins more
than 360 º. In one average tropical year, the number of rotations is:
1 QuantityMagnitudeUnitConvertQuantity1, "TropicalYears", "Days"
366.2421887920
However, the periodicity of the Earth’s rotation is not constant, but it’s slowing
down. Because of that, the rotation is no longer associated to the definition of
seconds, as the base time unit. Now, the official definition is
(https://www.bipm.org/en/si-base-units/second ):
"The unperturbed groundstate hyperfine transition
frequency of the caesium133 atom, to be 9 192 631
770 when expressed in the unit Hz, which is equal to s1 ".
To keep time standards such as UT1, based on the Earth’s rotation, accurate it’s
necessary to add or subtract discrete leap seconds (normally once or twice a year).
Even though the UTC system, which is the standard “civil time”, is not based on tracking
the precise rotation speed of the Earth, there was still a discrepancy between it and the
International Atomic Time (TAI) standard on 2021-12-31:
Wed 1 Dec 2021 00:00:00 UTC Wed 1 Dec 2021 00:00:00 TAI
37. s
GeoOrientationData takes into account the measured rotation speed of the Earth. The
following code shows the deviation of the duration of the day over 86,400 seconds (24 h) for
the last 20 years:
! ! 269
0.002
0.001
0.000
0.001
largest distance from Sun 8.1608146 108 km
next apoapsis time Sat 30 Dec 2028
last apoapsis time Sat 18 Feb 2017
This function shows the mass and density for each planet:
TextGridPrependEntityValue"Planet", "Name", "Mass", "Radius",
"Name", "Mass", "Radius", Frame All, Background None,
LighterYellow, .9, White, LighterBlendBlue, Green, .8
! ! 271
The next function displays the number of moons for each planet:
Length EntityValue"Planet", "Satellites", "EntityAssociation"
Next, we display the Mars and Venus positions at 20:00 h each 5 days for the first 90 days
of 2023.
analemmamars Table
QuantityMagnitudePlanetData"Mars", EntityProperty"Planet", "Azimuth",
"Date" DateObjectDateList2023, 1, i, 20, zone,
"AngularDegrees", QuantityMagnitude
PlanetData"Mars", EntityProperty"Planet", "Altitude",
"Date" DateObjectDateList2023, 1, i, 20, zone,
"AngularDegrees", i, 1, 90, 5;
analemmavenus Table
QuantityMagnitudePlanetData"Venus", EntityProperty"Planet", "Azimuth"
"Date" DateObjectDateList2023, 1, i, 20, zone,
"AngularDegrees", QuantityMagnitude
PlanetData"Venus", EntityProperty"Planet", "Altitude",
"Date" DateObjectDateList2023, 1, i, 20, zone,
"AngularDegrees", i, 1, 90, 5;
With GraphicsRow we can display both graphs in the same row. The graph for Mars has
been modified using AspectRatio to make its visualization easier.
272
40
70 35
30
60
altitude
altitude
25
20
50
15
10
40
5
100 120 140 160 240245250
azimuth azimuth
The points with negative altitude correspond to those times when the planet is below the
horizon and therefore invisible. We cannot see the planet when it’s behind the Sun either.
We can also see that in the case of Venus, the planet is only visible a few degrees
over the horizon. This is because Venus is an interior planet and, as such, when
observed from the Earth, it will never be very high above the horizon. This means
that we will never be able to see it next to the zenith since the sunlight would blind
us. Obviously, this doesn’t happen with exterior planets such as Mars.
The graph below shows the orbits of the terrestrial planets or inner planets: Mercury, Venus,
the Earth and Mars, with the Sun (not in scale) in the center.
Graphics3DYellow, Sphere0, 0, 0, 0.02, EntityValue
EntityClass"Planet", "InnerPlanet", "OrbitPath", Boxed False
! ! 273
The next four functions, based on examples included in the Wolfram Documentation, enable
us to calculate and display graphically the distance from Earth and the apparent magnitude
of the visible planets over a year:
visibleplanetsplanet , d1 , d2 , property :
PlanetDataplanet, TableEntityProperty"Planet", property,
"Date" DateObjectdate, date, DateRanged1, d2, "Week";
tsplanet , d1 , d2 , property :
TimeSeriesTransposeDateRanged1, d2, "Week",
visibleplanetsplanet, d1, d2, property;
DateListPlot
AssociationMapts, 2021, 1, 1, 2022, 12, 31, "DistanceFromEarth" &,
"Mercury", "Venus", "Mars", "Jupiter", "Saturn",
PlotLegends "Expressions", FrameLabel Automatic
10
8 Mercury
Venus
6
au
Mars
4 Jupiter
Saturn
2
0
2021 2022 2023
Mercury
2
Venus
0
Mars
Jupiter
2 Saturn
4
century, radio telescopes and spacial astronomy would arrive taking the science to a
whole new level.
Next, we use a function to know how far away in astronomical units (au) it would be from
the Earth each year starting in 1970-01-01 until 2300
DateListPlot, 01, 01 , EntityValue
Entity"MinorPlanet", "Pluto", EntityProperty"MinorPlanet",
"DistanceFromEarth", "Date" & Range1970, 2300, 10
45
40
35
30
Notice that during our lifetimes Pluto will be further away each year. In July 2015, a probe,
New Horizons, visited it for the first time.
One of the most interesting characteristics of Pluto and other celestial bodies in the Kuiper
Belt is that their orbits are normally slanted with respect to the ecliptic plane, next to the
ones from the classical planets, as we can see with the following function:
! ! 275
136199 Eris (2003 UB313) 44.143, 136108 Haumea (2003 EL61) 28.1950,
8.3.4 Exoplanets
To obtain information about planets outside of the solar system we can use the
Exoplanet entity (or ExoplanetData).
276
According to a paper published in Nature on August 25, 2016, there’s an Earth-sized planet
orbiting our nearest neighboring star other than the sun, Proxima Centauri.
EntityClass"Exoplanet",
"DistanceFromEarth" TakeSmallest1 EntityList
Proxima Centauri b
Although the planet orbits (0.051 au) closer to its star than Mercury does to the Sun, the star
itself is far fainter than the Sun. The surface temperature would allow the presence of liquid
water. However, the conditions on the surface may be strongly affected by the ultraviolet
and X-ray flares from the star, far more intense than the Earth experiences from the Sun.
tufted evergreen perennial herb having spikes of tiny white flowers and
glossy green round to heart-shaped leaves that become coppery to
maroon or purplish in fall
Based on the 3rd definition, nebulae are just part of galaxies. With telescopes we
can only see the ones located in our own galaxy. The generic name nebulae actually
includes two types of very different structures: the planetary nebulae
(“PlanetaryNebula”) generated by the accumulation of dust left after a supernova
explosion, and nebulae (“Nebula”) star nurseries, such as the Orion Nebula. As we
can see the name is confusing as planetary nebulae have nothing to do with the
formation of planets. Other very important galaxy elements are clusters, or globular
clusters (groups of stars). Information about galaxies, clusters, and nebulae can be
found using the Galaxy entity (or GalaxyData), the StarCluster entity (or
StarClusterData), and the Nebula entity (or NebulaData).
Here is the Milky Way diameter in light-year
1.0 105 ly
A very interesting nebula is Orion (M42), visible specially during winter in the northern
hemisphere. It is an enormous gas cloud where new stars are constantly being born.
Orion Nebula
M42
We’ve seen that big distances, such as the ones between galaxies, are usually measured in
parsec (pc), the equivalent of the distance corresponding to one arc second. The closest
galaxies to our own are shown below, note the use of Dataset. They all form a group of
galaxies known as the local group. Many of them are quite small.
lggaxies Dataset Local Group of galaxies GALAXIES
"DistanceFromSun", "EntityAssociation";
Sagittarius Dwarf Elliptical Galaxy 0.020 Mpc
Sculptor Dwarf Galaxy 0.079 Mpc
Sextans Dwarf Galaxy 0.086 Mpc
Small Magellanic Cloud 0.0606 Mpc
Ursa Minor Dwarf Galaxy 0.066 Mpc
Willman I 0.037 Mpc
To check which galaxies are visible to the naked eye (Apparent Magnitude < 5), we sort the
apparent magnitudes of the local group members (remember that the smaller the magnitude
the higher the luminosity).
Dataset Local Group of galaxies GALAXIES
As mentioned earlier, with our own eyes it is very difficult to see magnitudes greater than 5.
This means that in the northern hemisphere the only visible galaxy is M31 (Andromeda), the
rest of the galaxies with high luminosity (and correspondingly low apparent magnitude) are
only visible from the southern hemisphere.
The French philosopher Auguste Comte (1798–1857) believed that the composition
of stars was going to be beyond human knowledge. In his Cours de Philosophie he
used to say that stars would only be known as specks of light in the sky since they
were very far away. However, the analysis of stellar light has enabled us to know
reasonably well stars’ composition, temperature, and evolution.
280
A star is what in physics is known as a black body and therefore has a light
spectrum whose characteristics are related to its surface temperature following
Planck’s law.
Using the free-from input we can access Planck’s law formula. First we type “Planck law”
using natural language, press + once Mathematica has understood our input, select the
equation’s pod, and after right-clicking on the subpod and choosing “Copy subpod content”,
paste the information on the cell below.
Equation
2 h c2
L
5 h c k T 1
wavelength
T temperature
The spectrum for a specific temperature, and other properties, can be represented using the
PlanckRadiationLaw.
PlanckRadiationLawQuantity8000, "Kelvins", "SpectralPlot"
1.4 1014
1.2 1014
1.0 1014
8.0 1013
W m3 sr
6.0 1013
4.0 1013
2.0 1013
0
0 5. 107 1. 106 1.5 106 2. 106 2.5 106 3. 106 3.5 106
m
2 h c2
L
h ck
5 exp 1
T
! ! 281
2 h c2
UnitConvert , SI
h ck
5 exp 1
T
Next, we build a function where the wavelength is computed as function of the temperature.
blackbodyT : plancklaw .
QuantityVariable" ", "Wavelength" Quantityk , "Meters",
QuantityVariable"T", "Temperature" QuantityT, "Kelvins"
PlotCalloutblackbody8000, "8000 K", Above,
Calloutblackbody5800, "5800 K", Above,
Calloutblackbody4000, "4000 K", Above, k, 10 ^ 7, 3 10 ^ 6 ,
AxesOrigin 0, 0, PlotRange All, MaxRecursion 0
4000 K
3000
2000
1000
5800 K 8000 K
We are going to build a dynamic representation of Planck’s law that shows the
relationship between color and temperature.
The next function associates a black body spectrum to its temperature (in degrees Kelvin).
BlackBodyT :
34
With h 6.62607 10 , c 2.99792458 10 ^ 8, k 1.38065 10 ^ 23,
Plot 2 h c ^ 2 ^ 5 Exph c k T 1,
, 10 ^ 9, 1000 10 ^ 9, MaxRecursion 0, ColorFunction
ColorData"VisibleSpectrum"10 ^ 9 &, ColorFunctionScaling False,
Filling Axis, Ticks None, PlotLabel RowT, " K"
, , , ,
Historically, the measurement of distances across the Universe was one of the
fundamental problems in astronomy. The first step toward solving it was to
measure the Earth’s size but before even that, someone had to realize that our
planet is a sphere. It is believed that it was the Pythagoreans, mysterious people
that didn’t leave any written works behind, who for the first time, circa 430 B.C.,
came up with the idea that the Earth was round, based probably on two
observations: a) Sailors traveling from Greece to Northern Africa had noticed that
several constellations, such as Ursa Major, would appear higher in the horizon in
Egypt than in Athens for a given date and time (although there are other geometric
shapes that may explain this fact, the sphere is the one that provides the simplest
explanation), b) The way the Moon hides during lunar eclipses can be easily
explained if one assumes that it’s a shadow projected by a sphere, the Earth, upon
the Moon.
Once it was clear that the Earth was round, the next step was its measurement. It
was Eratosthenes, who eventually became the head of the library of Alexandria, the
first person to measure the Earth’s size. The method he used is legendary and well-
known among astronomy enthusiasts, but it’s worth describing it in the next few
lines. He started measuring the distance between Alexandria and Syene (nowadays
Aswan), that were located approximately on the same meridian (they really differ
by 3º). He also assumed that the Sun was distant enough that its rays would hit the
Earth in a parallel manner. He knew that during the summer solstice in Syene the
light would illuminate the bottom of the wells in the city. At that moment, the
sunbeams would hit the ground perpendicularly while at the same time in
Alexandria they would form an angle of approximately 1/50th of the length of a
circumference (number deduced by the projection of the shadows). He then used a
basic trigonometric identity to figure out that the distance between Alexandria and
Syene was 1/50th of the Earth’s circumference. Since that distance was 5,000
stadia, the Earth’s circumference was 250,000 stadia. There’s no agreement about
the exact conversion rate to meters. Depending on the reference, if we take a value
ranging from 185 m to 157.2 m, the error is between 17% and 1% of the actual
size, quite an extraordinary approximation in any case. In 1492, Christopher
Columbus assumed a considerably smaller figure while trying to convince Queen
Isabella of Spain to finance his trip against the opinion of her advisors, who knew
Eratosthenes’s estimate. He persisted in his error and ended up discovering
America.
284 Mathematica beyond mathematics
Shortly before Eratosthenes’s death, Hipparchus of Nice was born (c. 190 BC).
He’s not only famous due to his discovery of the precession of equinoxes, as we
have already mentioned, but also because of his quite accurate measurement of the
distance between the Earth and the Moon using the shadow projected by the former
onto the latter during Moon eclipses. He also initiated an empirical study of the
apparent magnitude of the stars and their positions. For that purpose, he invented
the ecliptic coordinates (different from the elliptic ones) and used the astrolabe
(invention attributed to him). Many centuries later (from 1989 to 1993) a satellite,
carrying its name, repeated his work with technology from the late-20th century. Its
successor: Gaia
(http://www.esa.int/Our_Activities/Space_Science/Gaia_overview), currently in
mission, will measure the position of the objects in our galaxy with extreme
precision.
We can compute the distance to the nearest stars using geometric methods (Figure
8.2). We measure the position of the star seen from a certain location, and 6 months
later we will observe its apparent displacement. This phenomenon is known as
parallax, and its explanation is similar to what happens when we place our thumb
between and object and our eyes. When closing either eye, we will notice the
apparent movement of the object. In the case of a stellar parallax, each eye
corresponds to the vertices of the base of an isosceles triangle created when joining
the Earth, position in two vertices A and B, separated by 6 months along the orbit
of the star, with vertex C, the position of the star, considered fixed. The parallax is
half the angle ACB.
The distance, d, from the Sun to the star with parallax, p, in radians, can be
computed as follows:
1 AU
d= tan p
= {given that the angle is very small, tan p p } 1p AU.
Since the values of p are very small, it is convenient to express them in arcseconds, p s .The
parallax of Procyon is:
! ! 285
3.50 pc
Or directly in parsecs:
3.49568 pc
where M is the absolute magnitude of the star (the luminosity of the star if it was 1
pc away) and P is the average period in days.
The previous expression can be written in Mathematica as:
abmpd 2.78 Log 10, pd 1.35;
Applying a similar criterion to the one used to determine the distance between a
bulb with known brightness and the brightness experienced by an observer, we can
deduct the following relationship between M, the apparent magnitude m
(magnitude seen from the Earth, calculated as the average between the maximum
and minimum observed magnitudes), and the distance D (in pc):
mM 5 log D 10 5 log D 5
From the previous formulas, we can establish the following equation to compute the
distance using the period (pd) and the apparent magnitude (apm), both of which can be
measured empirically:
distcephpd , apm : Solveapm abmpd 5 Log10, dist 5 0, dist
This method can be applied to stars in our galaxy with the help of big ground
telescopes or space ones. We can also use it to measure the distances to our nearest
galaxies assuming we can find Cepheid variable stars in them.
In https://www.eso.org/public/products/education/ we can find a set of basic
astronomy exercises using real data. In one of them, we are given the results of the
calculations done by Hubble in several Cepheid variables located in the M100
galaxy. According to the data, one star has a period of 53.5 days and an apparent
magnitude of 24.5. Stars of such magnitude can only be observed with large
telescopes.
The absolute magnitude and distance, in pc, are:
abm53.5, dist . distceph53.5, 24.90
It’s one of the most distant objects ever measured using this method.
For remote galaxies the method used is based on measuring the apparent luminosity
of a type Ia supernovae and relating it to the real magnitude (that can be deduced
using theory).
Another key parameter related to the previous one is the redshift experienced by
light. This is an example of the Doppler effect caused by galaxy movements,
mostly associated with the expansion of the universe.
You can find information about redshift typing in a free-form input cell “Cosmological
redshift”). After expanding the cell and telling Mathematica to interpret it as a formula,
you can even do some interactive calculations (click +) .
Basic dimensions
0.485872
Astronomer Edwin Powell Hubble (1889–1953) (the famous telescope was named
after him) realized that galaxies frequently presented redshift. The amount changed
depending on the galaxy. Later on, this astronomical phenomenon became known
as Hubble’s law.
cz H0 D
with:
z (redshift) = ob e e , where ob corresponds to the observed wavelength and e to the
emitted one; z is therefore a dimensionless number.
c is the speed of light.
D is the actual distance to the galaxy (in Mpc).
H0 is the Hubble constant at the moment of observation (during our lifetime and for the
duration of our civilization, this value will be practically constant).
Once z is known, we can calculate D using basic algebra:
cz
D
H0
0.485872
Determine the distance of a comoving object, in light years, for a redshift of 0.075:
ft ? NumericQ : QuantityMagnitude
UniverseModelDataQuantityt, "LightYears", "Redshift";
FindRoot0.075 fdist, dist, 10 ^ 9
FindRoot: The line search decreased the step size to within tolerance specified by AccuracyGoal
and PrecisionGoal but was unable to find a sufficient decrease in the merit function. You may
need more than MachinePrecision digits of working precision to meet these tolerances.
0.011
We compare this value with the best estimate available in the Galaxy entity.
1.256 108 ly
We can see that there are some discrepancies. This is because galaxies are also
affected by the gravitational forces of their galactic neighbors, e.g., Andromeda
influencing the Milky Way. In fact, Andromeda and our galaxy are approaching
each other and will collide in approximately 4 billion years from now.
The possibilities of the astronomical functions in Mathematica don’t end here. Feel
free to continue exploring.
In this section, we will show how we can use Mathematica for other Astronomy
related matters. We will describe, in particular, several features developed by the
author for the study of binary systems and, by extension, the search for exoplanets.
Since 1995, when the first extrasolar planet was found, more than 400 have been
detected. NASA’s Kepler spacecraft launched on March 6, 2009, was specifically
designed to search for extrasolar planets. Probably, by the time you read these
lines, their total number will have increased significantly.
Kepler site: http://www.nasa.gov/kepler
To find exoplanets, we mainly use two methods:
288 Mathematica beyond mathematics
1) Radial velocity: A star with a planet will move in its own small orbit in response
to the planet’s gravity leading to variations in the radial velocity of the star with
respect to Earth. These movements are only detectable for giant planets next to
stars.
2) Transit photometry: When a planet crosses in front of its parent star’s disk the
observed brightness of the star drops by a small amount. This last method is
becoming the most effective one. The principle is similar to the one used when
studying binary star systems.
More than half of the stars that we see are actually systems of two or more stars.
Normally it’s not possible to distinguish between different stars in multiple systems
(not to confuse them with naked-eye binaries that are usually stars that seem close
to each other when seen from Earth but are actually located in different systems
altogether). The detection is usually done with the transit photometry method. This
approach is very effective when dealing with eclipsing binaries, systems of two
stars with coplanar orbits when observed from Earth.
Next we are going to describe how to build this kind of system using Mathematica
and study its brightness. In the example that follows, we use as reference Eclipsing
Binary Stars by Dan Bruton:
http://www.physics.sfasu.edu/astro/ebstar/ebstar.html
Figure 8.3 represents a binary system (although we discuss the case of two stars,
the method is similar if we consider a star and a planet). The coordinates of the
centers are (x1, y1, z1) for star 1 and (x1, y1, z1) for star 2 (assuming
homogeneous spheres, the centers of mass coincide with the geometric centers).
We consider {0, 0, 0} as the coordinates for the center of mass of the system. These
coordinates have been chosen so that an observer, located on Earth, is on the axis
OZ. That is: the one that goes from the center of mass to Earth. For the OY axis, we
take the perpendicular to OZ in the star 1 plane when it forms an angle = 0. This
happens when the star is closest to the observer.
With:
i = orbital inclination: angle, in radians, between the line of vision and the orbital plane axis.
! ! 289
R = distance between the centers of both stars. It’s expressed in arbitrary units, usually the
average distance between the stars. In the case of a constant orbit, R = 1.
m1, m2 = stars’ masses (we can choose arbitrary units, but it’s important that they keep the
equivalence q = m2/m1).
r1, r2 = radii. For convenient purposes we choose r1 > r2. In practice, we can choose stars 1
and 2 so that the condition is always met. It’s convenient to express the radii in fractions of
the major semi-axis. In the case of a circular orbit, obviously the major semi-axis is greater
than or equal to the minor one and identical to the radius R.
= Phase, corresponding to the angle between the star and OZ, using = 0, the angle where
the center of star 2 is the closest to the observer. In a binary system with circular orbit, is a
variable parameter (it’s time-dependent) while the previous parameters are constant
(although that is not always the case, for example: There might be some intrinsic changes to
the brightness or even the mass could change as in the case of a mass transfer between stars.
However, in the example that follows we’ll use the simplest case, which happens to be the
2 tt0
most common one). is known as the azimuthal angle and follows P
, with P being
the orbital period.
In a binary system, the coordinates {x1, y1, z1} and {x2, y2, z2} for the location of
the stars are given by:
x y z
x1 , y1 , z1 ;
1 1 q 1 1 q 1 1 q
x y z
x2 , y2 , z2 ;
1q 1q 1q
We can convert them to spherical coordinates using the formulas:
x R Sin;
y R Cosi Cos;
z R Sini Cos;
Then, the position of each star as a function of R, i, , q with q = m2/m1 can be calculated
with the following functions:
x y z
star1R , i , , q , , ;
1 1 q 1 q1 1 1 q
x y z
star2R , i , , q , , ;
1 q 1 q 1 q
We have used the previous equations to define a Mathematica function to visually display
the movement of both stars as a function of R, i, , m1, and m2:
eclbinR, i, , m2m1, r1, r2 (The condition m2 < m1 is necessary).
290
ManipulateGraphics3DControlActiveOpacity1, Opacity.6,
Spherest1R, i, , q, r1, Spherest2R, i, , q , r2,
Opacity1, RGBColor.6, .74, .36, PointSize.02,
Point0, 0, 0, Black, PlotRange 2, 2, 2, 2, 2, 2,
SphericalRegion True, Boxed False, Background Black,
ImageSize 500, 340, ViewPoint 0, 0, 20,
R, rr, "R", 0.1, 5, Appearance "Labeled",
i, ii, "i", 0, , Appearance "Labeled",
q, qq, "mass 2 mass 1", 0.1, 1, Appearance "Labeled",
r1, rr1, "radium 1", 0.1, .5, Appearance "Labeled",
r2, rr2, "radium 2", 0.1, .5, Appearance "Labeled",
, rho, " rotation", 0, 4 , Appearance "Labeled",
R Sin
Initialization st1R , i , , q : ,
1
1
q
The initial moment = 0 is the time when both stars are aligned and can be seen
from the OZ axis (where the observer is located), that is when the interposition of
the stars is at its maximum. In a real system, all the parameters would be fixed and
only would change as a function of time.
Let’s consider the binary system defined by the following parameters R = 2, i = 2 Pi 85
/360, m1 = 0.6, m2 = 0.1, R1 = 0.4, R2 = 0.3.
eclbin2, 2 Pi 85 360, 0, 0.1 0.6, 0.4, 0.3
! ! 291
The transit photometry method discussed in the previous section is based on the
study of the changes in brightness when a star or a planet interposes itself between
another star and the observer. As mentioned before, since stars are not visually
distinguishable, even less a star and a planet, what we will see is a variation in the
apparent brightness.
The method described here, with the arrival of astronomical CCD cameras, is
within the reach of Astronomy fans. There have been instances where fans have
even been able to detect exoplanets using this technique. This is a great example of
how science can be done without the need for major resources.
The luminosity l of a star is defined as the amount of energy emitted by the star per
unit of time. The flow F is the energy emitted per unit of surface and per unit of
time, that is F1 = l1 2 and F2 = l2 2 where l1 , l2 = luminosities (in arbitrary
4 R1 4 R2
units, as long as they keep the ratio l2 /l1 ).
Then an observer will see the system with a luminosity given by:
l K F1 A1 F2 A2
where A1 and A2 represent each of the areas of the stars’ disks as seen by the observer, and
K can be determined based on the observer’s detector area, on the OZ axis, and the distance
between the Earth and the binary system.
To find these areas, we need to know the apparent distance as seen by the
292
2.0
1.5
1.0
0.5
2 4 6 8 10 12
To calculate A1 and A2, it is necessary to take into account the current stage in the
cycle.
The two figures below show the star with the bigger radius (r1 ) in red, and the one with the
smaller radius (r2 ) in blue.
GraphicsRed, Disk0, 0, 2, Blue, Disk4, 0, 1
Without eclipse: Corresponds to the period in which all the light emitted by both
stars in the direction of the observer reaches him. Therefore, this is when the
luminosity is at its maximum. In this stage, the following relationship holds:
r1 r2 with:
8. Looking at the Sky 293
r2
r1
!2 "A1 !1
"A2
To calculate the amount of light that arrives from the eclipsed star, we have to
subtract the eclipsed area from the total area. This means we have to compute "A1
and "A2 corresponding to the star 1 and star 2 segments, respectively, using the
following formulas:
1 1
"A1 = R12 ( !1 - Sin[!1]); "A2 = R22 ( !2 - Sin[!2])
2 2
GraphicsBlue, Disk2, 0, 1, Red, Disk0, 0, 2,
GraphicsBlue, Disk 2, 0, 1, Red, Disk0, 0, 2,
GraphicsRed, Disk0, 0, 2, Blue, Disk 2, 0, 1,
GraphicsRed, Disk0, 0, 2, Blue, Disk2.5, 0, 1
, , ,
The two figures on the left correspond to the scenario where star 1 (red) is the
closest one, that is z1 z2. In this case, the light that reaches the observer will be:
A1 r12 and A2 r22 "A1 "A2
The rest of the figures corresponds to the case where star 1 (red) is the most distant
one, that is z1 < z2 then:
A1 r12 "A1 "A2 and A2 r22
Total or annular: Corresponds to the period when a star is shaded by the other one
completely. In this stage: 1 2 with: A1 = 21 and A2 0 for z1>z2 and A1 =
21 22 and A2 = 22 ;
Now we are going to include all the criteria described earlier in the function
areaR, i, m2m1, r1, r2, l1, l2, , where: m2 m1 and r1 r2.
The function returns the area that the observer will measure.
Without eclipse: When # r1 r2 :
areaR , i , q , r1 , r2 , l1 , l2 , :
ModuleA1, A2, A1, A2 Pi r1 ^ 2 , Pi r2 ^ 2 ;
l1 A1 r1 ^ 2 l2 A2 r2 ^ 2 ; rhoR, i, , q r1 r2
A2R , i , q , r1 , r2 , :
Module2, , phi, phi1, phi1 LastSolve
phi
r12 r22 2 2 r2 Cos , phi . rhoR, i, , q1, 2;
2
1
r22 2 Sin2 . 2 phi1;
2
In this type of eclipse, for z1 z2: A1 r12 and A2 r22 "A1 "A2 while for
z1 z2: A1 r12 "A1 "A2 and A2 r22 .
areaR , i , q , r1 , r2 , l1 , l2 , :
R Cos Sini
Modulez1, z2, a1, a2, A1, A2 , z1 ;
1
1
q
R Cos Sini
z2 ;
1 q
a1 A1R, i, q, r1, r2, ;
a2 A2R, i, q, r1, r2, ;
A1, A2
If z1 z2, Pi r1 ^ 2 , Pi r2 ^ 2 a1 a2, Pi r1 ^ 2 a1 a2, Pi r2 ^ 2;
l1 A1 r1 ^ 2 l2 A2 r2 ^ 2 ; r1 r2 rhoR, i, , q r1 r2
R Cos Sini
z2 ;
1 q
A1, A2 If z1 z2, Pi r1 ^ 2 , 0, Pi r1 ^ 2 Pi r2 ^ 2 , Pi r2 ^ 2;
l1 A1 r1 ^ 2 l2 A2 r2 ^ 2 ; rhoR, i, , q r1 r2
Let’s simulate the ideal case of an eclipsing binary. We can distinguish the
repeating phases of an eclipse: one corresponds to the period without eclipse (t1 to
t2), in t2 the eclipse starts and reaches its plenitude between t3 and t4, from t4 to t5
we are in the partial eclipse phase, from t6 to t9 is the transit phase, and when t9 =
296
2.0 t7 t8
t3 t4
1.5
1.0
0.5
t
2 4 6 8 10 12
Figure 8.5 Light curve.
1.0
0.8
0.6
0.4
0.2
Phase
0.2 0.4 0.6 0.8 1.0 1.2 1.4
! ! 297
We represent graphically the previous data. If we only had the previous data points it would
be very difficult to figure out the underlying pattern (remove Joined True), joining the
dots makes it easier to observe the cyclical nature of the data. It’s normal for many
intermediate points to be missing, making the analysis even more challenging.
ListPlotdata, Joined True
5.15
5.10
5.05
5.00
4.95
4.90
where:
! represents the phase in cycles.
t0 (normally called epoch) represents the origin of the time variable expressed as a Julian
Date (DJ).
t is the moment when the measurement is taken (normally we will measure the apparent
magnitude, m).
P is the period, the time it takes for the cycle to repeat itself.
298
An equivalent expression to the previous one and the one that we will be using is:
t t0
Mod
P
With the function below, Mathematica returns the phase given the initial data points and the
period.
philist , p : Moduledata, data list;
TransposeModdataAll, 1 p, 1, dataAll, 2;
In this example we are assuming that the period is known, but in practice that’s
what we are trying to find out. We can check that by using an approximate value of
the period the representation is more diffuse.
With real observations, the period has to be determined, and that is not always easy. The
next function will help us to compute it. We keep in mind that for period-magnitude curves,
the order of representation in the axis OY is inverted. That is, the magnitudes are sorted in
descending order not in the usual ascending one: 3 2 0 1 2.
curveLdata , per , kmax :
Modulemag, a, b, k, i, mag RoundTransposedata2, 0.1;
ManipulateListPlotJoinphidata, per k . a , b a, b,
phidata, per k . a , b 1 a, b, AxesLabel
"Period", "Magnitude", AxesOrigin 1, Maxmag 0.1,
Ticks Automatic, Tablei, i, i, Maxmag 0.1,
Minmag 0.1, Maxmag Minmag 10 ,
k, 0, "Deviation from the period in days", 0, kmax
Let’s assume a complete cycle (2). We use the previous function to simulate the
experimental measures and include a random component. We make the assumption that the
period is 20 days (the duration of a complete orbit).
simulation Table
, area2, 2 Pi 85 360, 0.1 0.6, 0.4, 0.3, 0.6, 0.2, 2 Pi 20
RandomRealNormalDistribution0, 0.005,
, 0, 60, 0.05; Quiet
We represent the data in a phase diagram:
simulation1 Sortphisimulation, 20;
2.50
2.45
2.40
2.35
2.30
2.25
0.05
0.05
0.10
0.15
0.20
0.25
0.30
All the previous functions along with other examples in the book have been put
together in a package that can be downloaded from diarium.usal.es/guillermo.
The output tells us that this isotope, whose complete symbol is 131
53 I78 , has an atomic mass of
130.09061 atomic mass units (amu), with a single type of radioactive decay, a beta decay,
302
that disintegrates into Xenon-131 with a disintegration period of and that it
doesn’t exist in natural form ().
The example below generates a dataset with information about all the properties available
for Iodine-131.
DeleteMissing iodine 131 "Dataset"
others with as many as 8. Isotopes can be stable or unstable. Stable nuclei, the
majority on earth, last “forever”, that is, they always keep the same number of
neutrons and protons. Some theories predict the disintegration of protons, and
therefore of any kind of atom, but they have not been proven yet. Furthermore,
even if they could be validated, the disintegration speed would be so slow that for
all practical purposes we could consider that stable nuclei have an almost eternal
duration. Unstable nuclei (called radioisotopes) are those that over time (depending
on the radioisotope the period can range from a fraction of a second to billions of
years) are transmuted into other elements, called daughter isotopes. The new
nucleus can in turn be stable or unstable. If it’s unstable, the process continues until
the daughter isotope is a stable one.
With [“element”, “property”] we can obtain all the known isotopes for a
given element. The element can be written as a complete word or, preferably, as a symbol
using standard notation. If we type “Symbol” in “property”, we’ll get the isotopes in A X
notation. We use Short to display only one line, but the complete output includes more
isotopes.
IsotopeData"C", "Symbol" Short
8 C, 9 C, 10
C, 11
C, 12
C, 13
C, 3, 17
C, 18
C, 19
C, 20
C, 21
C, 22
C
The decay of a type of nucleus into another one usually happens due to the
emission of particles or and radiation . In some radionuclides, there are also
emissions of neutrons and protons and even other types of reactions such as
spontaneous fission (SF), common in some heavy nuclei. Fission is the division of
a nucleus into two smaller ones, each one with approximately half the mass of the
original.
Using “DecayModes” or “DecayModeSymbols” as properties we can get the type
of decay for a certain isotope. One isotope can display more than one decay mode
although normally one of them would be the most common one. Using
“BranchingRatios” and “DecayEnergies” we can see, respectively, the probability
associated to each decay mode and its energy measured in keV (kiloelectron-volt).
The command below shows the different decay modes of Uranium–238, and the symbols,
probabilities and energies associated to them.
GridIsotopeData"Uranium238", &
"DecayModes", "DecayModeSymbols", "BranchingRatios", "DecayEnergies"
AlphaEmission SpontaneousFission DoubleBetaDecay
SF 2
1.00 5.45 107 2.2 1012
4269.75 keV — 1144.2 keV
Notice that the majority of the radioactive decays for U238 correspond to alpha emissions
(the value 1.00 indicates that the probability is close to 100%). However, spontaneous
fissions (SF), even though they have a small probability of happening, play a central role in
chain reactions since they emit neutrons, essential to start a chain reaction.
In short, we can say that a radioactive isotope is like a father who has a
daughter, who in turn may have a granddaughter and so on until eventually
there’s a descendant without offspring, the stable nucleus. The disintegration
304
You can use the free-form input to get the definition of chain reaction. Probably when you
type “chain reaction definition” you will see several definitions. Just choose the most
appropriate one depending on your needs.
We can also get information by classes. We use Shallow to avoid displaying the complete
output, which is quite large. If you want to see all the classes just remove the command from
the next input.
IsotopeData"Classes" Shallow
beta delayed deuteron emission , beta delayed fission , beta delayed four neutron emission ,
beta delayed neutron alpha emission , beta delayed neutron emission ,
beta delayed three neutron emission , beta delayed triton emission , 37
Each class includes isotopes sharing a common characteristic. For example, if we want to
know the isotopes with a beta emission associated to spontaneous fission (a very rare
occurrence, only observed in 3 isotopes out of the more the 3,000 available), the command
below gives us the answer:
IsotopeData beta delayed fission ISOTOPES
xt B
In the next example, we use and the total time t as our changing parameters. We also use
the previously defined function within Manipulate using the command Initialization.
Alternatively, we could have used SavedDefinitions.
Manipulate Plot yt, , 1 , t, 0, t1, Filling Bottom,
, 1, 0, 5, t1, 3, 1, 10,
t
Initialization yt, , x0 : x0
306
In many situations instead of the , , it’s better to use the semi-decay period
or , T12 , defined as the time that it would take for the amount x0 present in the
sample at time t 0 to become half, x0 2.
The relationship between and T12 can be calculated as follows:
cteDes . Solve x1 T, x0, x0 2 , , Reals1
log2
T
Another concept is the , the life expectation (duration) of the isotopes present
in a sample. It’s the same as calculating the arithmetic mean of the expected duration of the
individual isotopes present in a sample.
To calculate the mean of a continuous variable t we compute t = 0 t f t t 0 f t x,
and associate the result to the symbol . We specify that >0, otherwise we’ll receive a
message stating that the solution is only valid if >0.
Integratet x0 E ^ t , t, 0, Infinity,
Assumptions Reals && 0 Integratex0 E ^ t ,
t, 0, Infinity, Assumptions Reals && 0
1
In summary: the radioactive decay constant , the half-life T12 , and the mean
lifetime , are connected as follows: T12 Log 2 Log 2 , = 1/ .
With IsotopeData we can find out the half-life, mean lifetime or decay constant of
Iodine-131.
IsotopeData"Iodine131", & "HalfLife", "Lifetime", "DecayConstant"
By default the function returns the time in seconds, but we can convert it to other unit using
UnitConvert.
UnitConvert
IsotopeData"Iodine131", & "HalfLife", "Lifetime" , "days"
In the example below we can choose the isotope among several options. We use
QuantityMagnitude to get only the value (in this case, the decay constant), without the unit.
Manipulate Plot yt, 3600 , 1 , t, 0, t1, Filling Bottom,
t1, 3, "Hours", 1, 100,
, QuantityMagnitudeIsotopeData"Iodine131", "DecayConstant"
"Iodine 131", QuantityMagnitude
IsotopeData"Iodine125", "DecayConstant" "Iodine 125",
Initialization yt, , x0 : x0 t
307
The next example uses IsotopeData to show all the isotopes for iodine. We also include the
option of selecting the unit for displaying the evolution over time for the chosen isotope.
Since the decay rate for some of the isotopes is very fast we use a logarithmic scale.
Manipulate LogPlot yt, k , 1, t, 1, t1, Filling Bottom,
t1, 3, 1, 100 , k, 1 "Seconds", 3600 "Hours", 3600 24 "Days",
, "", "Isotope",
ThreadQuantityMagnitudeIsotopeData"Iodine", "DecayConstant"
IsotopeData"Iodine", Initialization yt, , x0 : x0 t
Iodine-131 emissions from the Fukushima Daiichi nuclear reactor, due to the
accident caused by the tsunami in March 2011, were estimated to be 1.51017 Bq as
308
of the end of April 2011. Given this information, let’s explore how much mass was
emitted and how the emissions evolved over time.
Activity can be turned into mass using the formula A nat = m N A ma
m ma A NA .
Iodine-131 decays into Xenon-131, a stable isotope that at room temperature is gas. We use
LayeredGraphPlot to represent the decay chain.
LayeredGraphPlotIsotopeData"I131", "Symbol"
IsotopeDataIsotopeData"I131", "DaughterNuclides"1, "Symbol",
Left, VertexLabels "Name"
131
I
131
Xe
We need to know the decay constant, 131, and the atomic mass. IsotopeData will give us
the latter (although we could have used 131 as an excellent approximation) and also return
the mean lifetime (in seconds). Afterward we can calculate =1/.
mat131, 131 IsotopeData"Iodine131", "AtomicMass",
1 IsotopeData"Iodine131", "Lifetime"
130.906124609 u, 9.9967 107 per second
N0
32.617 grams
It may seem strange at first sight that such a big radioactive activity would only result in a
very small mass. What’s actually happening is that the Bq is an extremely small unit. As a
matter of fact, for many years instead of using the Becquerel to measure radioactivity,
people used the Curie, Ci (1 Ci = 3.7 1010 Bq), unit that’s still in use in some places.
To find out in how many days the radioactivity of the Iodine-131 would become just 0.1%
of its initial figure, we use the following command:
UnitConvertt . Solvex1t, 100, 131 0.1, t, "days"
79.9774 days
is stable right away, as with Iodine-131 Xenon-131, but other isotopes, like
Radium-226, shown at the end of this section, have more complicated decay chains.
One of Radium-226’s daughter isotopes, Radon-222 (Rn222), is responsible for a
large share of the natural radiation that people are exposed to. Each time we
breathe, a portion of the air includes radon gas. The content of Rn222 changes
substantially from one area to another. For example: granitic houses with poor
ventilation usually have a high content level of radon. The reason is that granite
contains Radium-226 that becomes radon gas when it disintegrates.
An isotope can decay following different branches. For example: Radium-226 decays with a
half-life period of s into 3 radioisotopes: 222 Rn, 212 Pb and 226 Th. Almost all of
it decays into 222 Rn and an insignificant part into 212 Pb. Occasionally, there can also be
traces of Thorium-226.
ra226 IsotopeData"Ra226", &
"DaughterNuclides", "BranchingRatios", "HalfLife";
TableFormra226, TableHeadings "Isotope", "Fraction", "T s ", None
!
thorium-234 , —, plutonium-238
thorium-234 , plutonium-238
The set of all branches produced by the decay of a parent isotope (like Uranium-238) is
known as the radioactive decay chain. However, the majority of those disintegrations happen
along only one branch. IsotopeData["isotope", "BranchingRatios"] shows the results after
sorting the branches from bigger to smaller in terms of their likelihood of occurrence. When
an isotope decays into one or several isotopes, we can choose to keep just one: the one with
the highest probability. If we apply this criterion to each daughter isotope, we will end up
with a single branch named the main branch. From a practical point of view, in many decay
chains it is enough to study the main branch.
The following function lets us choose the main branch, and within it, the number of
daughter nuclides n that we are interested in. Notice the use of iso_String, and n_Integer to
limit the input type to: “isotope name” (between double quotation marks), and n integer
(number of daughter nuclides that we would like to display); in any other case, the function
will not be executed.
mainbranchisoString, nInteger :
NestListFirstIsotopeData, "DaughterNuclides" &, iso, n
We apply it to Uranium-238 and indicate 14 daughter nuclides, the entire decay chain (one
of the longest ones).
310
The function below generates a list of isotopes showing: their disintegration period,
daughter nuclides and branching ratios.
isoproisolist :
Modulevals, vals TableIsotopeData, prop, prop,
"Symbol", "HalfLife", "DaughterNuclides", "BranchingRatios" &
isolist;
TextGridPrependvals, "", "Disintegration\nperiod sec ",
"Daughter nuclides", "Branching ratios", Frame All,
Background None, LightBlue, White, 1 LightYellow
We use it to show the information related to the principal branch of Radon-226 obtained in
the previous calculation.
isoprora226
214
Bi 1.19 103 s polonium-214 , 1.0, 0.00021, 0.000030
thallium-210 , lead-210
214
Po 0.0001643 s lead-210 1.00
210
Pb 7.01 108 s bismuth-210 , mercury-206 1.00, 1.9 108
210
Bi 4.33 105 s polonium-210 , thallium-206 1.00, 1.32 106
210
Po 1.19557 107 s lead-206 1.00
206
Pb
311
The functions below are part of the documentation related to LayeredGraphPlot. Sometimes
we may get lucky and find examples in the documentation that perfectly match what we
want to do. This is one of those occasions.
The following functions (from ref/LayeredGraphPlot) let us input the parent isotope (the
first in the chain) and get as a result all the daughter nuclides from all the branches except
those that have an insignificant contribution (“Missing”).
DaughterNuclidessList :
DeleteCasesUnionApplyJoin, MapIsotopeData, "DaughterNuclides" &,
DeleteCasess, Missing, Missing
ReachableNuclidessList :
FixedPointUnionJoin, DaughterNuclides &, s
DecayNetworkisoList :
ApplyJoin,
MapThread DaughterNuclides &, ReachableNuclidesiso
DecayNetworkPlots :
LayeredGraphPlotMapIsotopeData, "Symbol" &, DecayNetworks, 2,
PerformanceGoal "Quality", VertexShapeFunction
TextFramedStyle2, 8, Black, Background LightYellow, 1 &
We use the last one to show the decay chain of Rn222, the immediate decay product of
Ra226. You can also apply the same function to see the complete chain of the latter. You
may have to adjust the output image size to see it properly. To do that just place the cursor
in one of the corners of the image and resize it accordingly.
312
222
Rn
218
Po
214 218
Pb At
214 218
Bi Rn
210 214
Tl Po
209 210
Pb Pb
206
Pb
The following expression returns all the isotopes that have the same number of neutrons:
isoneuneut :
SelectIsotopeData, IsotopeData, "NeutronNumber" neut &
isoneu14
Using the previous function we display the decay chains of all the isotopes with 4 neutrons.
313
8 6 12 10 3
Be He N C H
4 6 10
He Li B
1 2 i
1 2 3
The first circle represents the first isotope in the chain, with a decay constant ,
the second circle refers to the next isotope, with a decay constant , and so on.
Let’s consider the case of a chain of 3 isotopes, with q1 t, q2 t, and q3 t the
quantities present of each one at time t, satisfying the following set of differential
equations:
q1 ' t 1 q1 t
q2 ' t 1 q1 t 2 q2 t
q3 ' t 2 q2 t 3 q3 t
As an initial condition let’s assume that at t 0 there’s a quantity (atoms, gm, Bq) of isotope
1 in compartment 1 and nothing in the rest. This means: q1 0 b1, q2 0 q3 0 0 then:
314
Following the same approach, the previous equation can be extended for n daughter
isotopes:
t
n1 n j
Here we apply it to the parent and the first 3 daughter isotopes (of the main branch) of
Uranium-238.
u3 mainbranch"U238", 3
We need the decay constants . Remember that = 1/ , with in seconds. However, since
we prefer to measure t in days we multiply it by 24 3600.
s Threadk1 , k2 , k3 , k4 Evaluate
24 3600 QuantityMagnitudeIsotopeData, "Lifetime" & u3
k1 4.248 1013 , k2 0.029, k3 2.5, k4 7.735 109
315
Next we’re going to calculate the evolution of the radioactivity, Ai , in the previous isotopes.
We assume that at time t 0 there’s 1 gm of U238 in the sample, which is expressed as the
number of its atoms, NA (Avogadro’s number)/238. Now we can compute Ai , using the
general formula A = NA .
A1t, A2t, A3t, A4t
k1 q1, k2 q2, k3 q3, k4 q4 . s .
b UnitConvert1 NA 238 ExpandAll
13 13
1.075 109 4.24810 t
, 1.1 109 4.24810 t
1.1 109 0.029 t ,
13
1.3 107 2.5 t 1.1 109 0.029 t 1.1 109 4.24810 t
,
2.5 t 2 0.029 t 9 7.735109 t 13
0.04 3. 10 1.1 10 1.1 109 4.24810 t
We subtract 108 Bq from the activity of Pa234 with the only purpose of differentiating it
from Th234. Otherwise both graphs would be superimposed since they are in equilibrium as
we will see shortly.
PlotA1t, A2t 10 ^ 8, A3t 10, t, 0, 180,
PlotRange All, AxesLabel "t days ", "A t in Bq",
PlotLegends Placed"U238", "Th234", "Pa234", Center
At in Bq
1 109
8 108
6 108 U238
Th234
4 108
Pa234
8
2 10
tdays
50 100 150
In the graph above we can see that after 180 days, A1A2A3. This is known as secular
equilibrium. In this example, it’s enough to calculate the radioactivity of the isotope parent
to know the daughter isotopes’ radioactivity. This happens when 1 < 2 .
The next example shows that given enough time, in this case thousands of years, U238 and
U234 will reach secular equilibrium. Normally we consider that the secular equilibrium has
been reached after a period of time longer than 8 half-life periods of the daughter isotope.
316
1 109
8 108
8
6 10 U238
U234
4 108
2 108
tdays
2 108 4 108 6 108 8 108 1 109
Radiocarbon Dating
The 14 C dating method was discovered in 1947 by Willard Libby. It can be applied
to samples that are no more than 50,000 years old. This method has become the
most frequently used one for measuring the age of organic objects, especially
archeological ones.
The table that follows displays the isotopic abundance (proportion of isotopes) of the
different carbon isotopes and their half-lives (If “HalfLife”< 1 s is not shown). An isotopic
abundance of 0 indicates that the isotope doesn’t exist in nature or is present only as traces.
For example: the proportion of 14 C is very small although, as we will see, it plays a crucial
role in dating.
vals DeleteCasesTableIsotopeData, prop,
prop, "Symbol", "HalfLife", "IsotopeAbundance" &
IsotopeData"C", a, b ; b 1 s , c;
317
Text
GridPrependvals, "", "Half lives", "Isotope abundance", Frame All,
Background None, LightBlue, White, 1 LightYellow
This method is based on the following principle: 98.9% of the carbon in the Earth’s
atmosphere is Carbon-12, 1.1% Carbon-13, and the rest, 1.18 1014 % is Carbon-14. This
last isotope is the key to the method.
14
The command below returns the decay scheme of C. It decays into 14 N with a beta
emission (1 electron) and has a half-life period of s (approximately 5,700 years)
c14 IsotopeData"C14", &
"DaughterNuclides", "BranchingRatios", "DecayModes", "HalfLife"
supports the idea that after the formation of the solar system, with some exceptions,
the distribution of isotopes was homogeneous.
Among meteorites, the carbonaceous chondrite type is particularly interesting.
Chondrites are made of chondrules, molten droplets created during the collisions
that gave birth to planets, that have the isotopic composition of the planet-forming
period. Furthermore, they didn’t experience any further heating intense enough to
melt them again. It’s been possible to determine their age using dating techniques
based on radioactive isotopes with long half-lives. The most commonly used
methods are: the lead-lead method (Pb-Pb) and the rubidium-strontium one (Rb-
Sr). Next, we’re going to discuss the Pb-Pb method.
The decay chains of Uranium-238 and Uranium-235 are of particular interest among natural
206 207
isotopes. Their final nuclides, are respectively: Pb and Pb.
RowIsotopeData, "Symbol" & mainbranch1"U238", " "
238 234 234 234
U Th Pa U
230 226 222 218 214 214 214 210 210 210 206
Th Ra Rn Po Pb Bi Po Pb Bi Po Pb
RowIsotopeData, "Symbol" & mainbranch1"U235", " "
235 231 231 227 227 223 219 215 211 211 207 207
U Th Pa Ac Th Ra Rn Po Pb Bi Tl Pb
206 207
In nature, apart from Pb and Pb, we also have other stable lead isotopes in the
following proportions:
pbestable DeleteCases TransposeIsotopeData"Pb",
IsotopeData, "Stable" & IsotopeData"Pb" , , False;
vals
TableIsotopeData, prop, prop, "Symbol", "IsotopeAbundance" &
FirstTransposepbestable;
TextGridPrependvals, "Isotope", "Isotope Abundance", Frame All,
Background None, LightBlue, White, 1 LightYellow
These proportions may vary widely depending on the origin of the sample.
208 Pb is a stable daughter isotope of the decay chain of Th232, and doesn’t play any role in
the dating method we are about to describe. 204 Pb, however, is a primordial isotope. This
means that the amount of this nuclide present on Earth has not changed since the formation
of our planet. Therefore it’s useful for estimating the fraction of the other lead isotopes in a
given sample that are also primordial since their relative fractions are constant everywhere.
RowIsotopeData, "Symbol" & mainbranch1"Th232", " "
232 228 228 228 224 220 216 212 212 212 208
Th Ra Ac Th Ra Rn Po Pb Bi Po Pb
At the moment of the formation of the solar system, U238 and U235, after going
through an homogenization process, started to decay, respectively, into Pb-206 and
Pb-207 thus adding to the existing amount in the rocks.
320
The quantity from the parent isotope that decays into a daughter isotope is:
Nh t 1 Np t, that is:
t
Nh Np
with the P and I subscripts indicating, respectively, the present and initial
quantities, 235 and 238 the decay constants of U235 and U238, and t the elapsed
time.
There will also be a certain amount of Pb204 that will not change. Under these
hypotheses we obtain the following relationships:
207 207 235
Pb Pb U
235 t
1
204 204 204
Pb P Pb I Pb
206 206 238
Pb Pb U
238 t
1
204 204 204
Pb P Pb I Pb
The different lines obtained depend on the original U/Pb ratio. For samples coming from the
oldest meteorites (see the graph ), K = 0.61, meaning that t, in eons (billions of years) is:
235 t
1 1
K .
137.88 238 t
1
235 1 QuantityMagnitudeUnitConvertIsotopeData"U235",
"Lifetime", "eons", 238 1 QuantityMagnitude
UnitConvertIsotopeData"U238", "Lifetime", "eons";
FindRootK 0.61 , t, 0.5
t 4.54709
We can compare this result with the one given by Mathematica using the free-form input
and see that is quite similar.
4.54 109 yr
Orogenic phenomena have left us without remains of rocks from the time of the
formation of our planet. The oldest ones have been found in zircon crystals from
Mount Narryer, in Western Australia. They are estimated to be more than 4,000
years old, proving that at that time the Earth already had a continental crust.
The sum of the particle masses that form the iron isotope (expressed in MeV c2 ): mA = N
mn + Z (mp +me ):
fe56particlesmass Nfe56 ParticleData"Neutron", "Mass"
Zfe56 ParticleData"Proton", "Mass"
Zfe56 ParticleData"Electron", "Mass"
52 595.320 MeVc2
We calculate below the conversion factor of 1 amu (atomic mass unit) to 1 MeV:
931.494102 MeV2
We find the mass of the iron isotope Fe56 and express it in MeV:
fe56mass
52 103.0644 MeVc2
We can get approximately the same value directly with (sometimes in MeV c2 we omit c2
or in natural units we consider c 1):
IsotopeData"Fe56", "BindingEnergy"
8.790323 MeV
60.605352 MeV
The function below displays the binding energies as a function of the mass number. With
Tooltip we can see the information related to a point when placing the cursor on it:
bindingenergy TooltipIsotopeData, "MassNumber",
IsotopeData, "BindingEnergy" & IsotopeData;
323
8.5
8.0
7.5
7.0
6.5
6.0
0 50 100 150 200 250
Mass number A
The following command shows the binding energy per nucleon, as a function of the atomic
number and the number of neutrons until Z = 92 (uranium):
bindingenergyZNB Flatten
Table z, a z, IsotopeDataz, a, "BindingEnergy", z, 1, 92,
a, IsotopeData, "MassNumber" & IsotopeDataz, 1;
Here we visualize it in 3D:
ListPlot3DbindingenergyZNB, AxesLabel "A", "N", "B"
Next we show an example with 224 nuclides with less than 15 protons each (Z < 15) and
plot it using the function ColorData["Atoms"] that associates each element to one color. We
can find the value of Z and N by placing the cursor inside the graph, right clicking with the
mouse button and selecting “Get Coordinates”.
lessthan15 TakebindingenergyZNB, 224;
324
In this case, we present the same information but using a color code that associates redder
colors to higher binding energies:
ListDensityPlotlessthan15, FrameLabel "N", "Z",
InterpolationOrder 1, ColorFunction "Rainbow"
Decays and nuclear transformations are related to binding energies. There are two
types of nuclear transformations that are particularly useful: fusion and fission.
Fusion is the process of joining nuclei. This process is the most predominant one in
elements with low atomic numbers, specifically, until iron. The lower the atomic
number the lower the binding energy associated to it. Fusion is the normal method
by which stars generate energy.
Fission is the opposite process of fusion. It consists of splitting apart nuclei to turn
them into lighter ones.
Some isotopes of uranium and plutonium represent a special case where the process
accelerates when interacting with neutrons.
Some very heavy nuclei may experience spontaneous fission. The next function displays the
first five such elements:
325
Since IsotopeData includes “Spontaneous Fission” as a property, we can also get those
elements as follows (we don’t show the complete output):
IsotopeData spontaneous fission ISOTOPES Shallow
Many of them are transuranic elements obtained by artificial means, although there are also
some isotopes in nature that exhibit this property. They are largely responsible for the
presence of free neutrons that have lifetimes of just a few minutes:
ParticleData"Neutron", "Lifetime"
885.6 s
This can be explained by the fact that both, neutrons and protons, are not really elementary
particles but are made of two types of quarks:
ParticleData, "QuarkContent" & "Proton", "Neutron"
Text Grid Prepend values, "", "Charge", "Mass MeVc 2 ", Frame All,
Background None, LightBlue, White, 1 LightYellow
Charge MassMeVc2
1
d 5.0 MeV2
3
2
u 2.2 MeV2
3
Notice how small the mass of the quarks is compared to that of a neutron (two quarks down
and one quark up) or a proton (two quarks up and one quark down):
qd, qu ParticleData, "Mass" & "DownQuark", "UpQuark"
The Standard Model of particle physics is the theory describing three of the four
known fundamental forces electromagnetic, weak and strong interactions,
omitting gravity in the universe and classifying all known elementary particles.,
It was developed in stages throughout the latter half of the 20th century, through the
work of many scientists worldwide, with the current formulation being finalized
in the mid1970s upon experimental confirmation of the existence of quarks.
A few properties of the gauge bosons, quarks, and leptons are summed up in the next tables.
DatasetEntityValueEntityClass"Particle", "GaugeBoson",
EntityProperty"Particle", "Lifetime", EntityProperty"Particle",
"Width", "EntityPropertyAssociation"
327
In the design of radiation protection products it is important to take into account the mass
attenuation coefficients of the materials used. This attenuation depends on the incident
particles (protons, alpha, protons, electrons, neutrons) and their energy. Here we compare
the mass attenuation coefficient for various substances and energies:
329
materials "Water", lead ELEMENT , iron ELEMENT , aluminum ELEMENT ;
ListLogLogPlot
TableQuantityx, "Megaelectronvolts", StoppingPowerData,
"Particle" "Photon", "Energy" Quantityx, "Megaelectronvolts",
"MassAttenuationCoefficient", x, 0.01, 100, .5 & materials,
Joined True, PlotLegends materials, AxesLabel Automatic
cm2 g
100
Water
10
lead
1 iron
aluminum
0.10
MeV
0.01 0.10 1 10 100
To accurately estimate absorbed doses, we measure the amount of the energy deposited in
human tissues. The example below shows the stopping power changes, dE/dx, for an alpha
particle emitted from U234 as it travels through biological tissue:
initialKE QuantityMagnitude
IsotopeDataEntity"Isotope", "Uranium234", "BindingEnergy"
7.600708
0.25
dEdx MeV"m
0.20
0.15
0.10
0 10 20 30 40 50 60
Distance Travelled "m
Input
Flow from 1 to 2
Output
to compartment 2 and k21 from compartment 2 to 1, k20 the transfer rate coefficient
from compartment 1 to the environment (output), and x1 t and x2 t represent the
quantities in compartments 1 and 2 at time t:
b1 t
k12
1 2
x1 t x2 t
k21
k10
or in matrix notation:
x1 t k10 k12 k21 b1 t
x2 t k12 k21 0
We also need to know the initial conditions. In this case, the contents of x1 t and
x2 t at t 0.
x1 0 x10 ; x2 0 x20
Using PlotLegends
x Using Callout
0.030
0.025 xx1 t
0.04
Out[ ]= 0.020 x1 t 0.03 x t
0.015 2
x2 t 0.02
0.010
0.01
0.005
Days
Days 5 10 15 20 25 30
5 10 15 20 25 30
The flow diagram in Figure 10.2 represents the iodine model (obtained from ICRP
78) where compartment 1 is the blood, compartment 2 is the thyroid, compartment
3 is the rest of the body, and compartment 4 is the bladder. Additionally, 3 0,
i.e., a transfer from compartment 3 to the environment, represents the output to the
gastrointestinal tract (GIT) and 4 0 represents the output, via urine excretion, to
the environment.
b1 t
k12
1 2
k14
k31 k23
k30
4 3
k40
We need to know the transfer constants kij values. According to the ICRP 78, the transfer
rates for iodine, in days1 , are k12 0.3 Log2 0.25, k14 0.7 Log2 0.25,
k23 Log2 80, k30 0.2 Log2 12, k31 0.8 Log2 12 and k40 12 ;
0.3 Log2 0.7 Log2 Log2
In[ ]:= kidine k12 , k14 , k23 ,
0.25 0.25 80
1 1
k30 0.2 Log2, k31 0.8 Log2, k40 12;
12 12
In[ ]:= Aiodine A . kidine
2.77259 0 0.0462098 0
log2
0.831777 0 0
80
Out[ ]=
log2
0 0.0577623 0
80
1.94081 0 0 12
The evolution of the iodine retention in the blood (compartment 1) and in the thyroid
(compartment 2) is plotted as follows:
336 Mathematica beyond mathematics
1.0
0.8
0.6
Out[ ]= Blood
Thyroid
0.4
0.2
Days
2 4 6 8 10
Figure 10.3 shows a more complex compartmental model: The general multi-input
multi-output (MIMO) three-compartment model. We have assumed a radioactive
decay with a disintegration constant (it can be considered as an output from each
compartment):
b3 t
3
k30
k13 k32
k31 k23
b1 t b2 t
k12
1 2
k21
k10 k20
x1 (t) = - ( + k10 + k12 + k13 ) x1 (t) + k21 x2 (t) + k31 x3 (t) + b1 (t)
x2 (t) = k12 x1 (t) - ( + k20 + k21 + k23 ) x2 (t) + k32 x3 (t) + b2 (t) (10.2)
x3 (t) = k13 x1 (t) + k23 x2 (t) - ( + k30 + k31 + k32 ) x3 (t) + b3 (t)
Out[ ]=
10. Modeling: Application in Biokinetics 339
The evolution of the iodine retention in the blood (compartment 1) and in the thyroid
(compartment 2) is plotted below:
In[ ]:= Plotx1t, x2t, t, 1, 200, PlotStyle
RGBColor1, 0, 0, AbsoluteDashing4, 4, RGBColor0, 0, 1,
AxesLabel "Days", "r", PlotLegends
Placed"Blood", "Thyroid", Center, Framed, RoundingRadius 5 & ,
PlotLabel StyleFramed"b1 t 1 0.5 Cos0.3 t",
12, Blue, Background LighterYellow
3.5
3.0
2.5
Out[ ]=
2.0
Blood
1.5 Thyroid
1.0
0.5
Days
50 100 150 200
The following function computes numerically the SODE using NDSolve and returns the
solution in the interval {t, tmin , tmax }:
In[ ]:= SystemNDSolve1matrixA ? MatrixQ, incondList,
inputList, t, tmin, tmax, t1, x, opts ? OptionQ :
ModuleA, n, var, i, system, ic, X, X0, sol , A matrixA;
n DimensionsA1;
X Tablexi t, i, 1, n; X0 Tablexi 0, i, 1, n;
system DX, t A.X input;
ic ThreadX0 incond;
sol NDSolvesystem, ic, X, t, tmin, tmax, opts;
ChopExpandAllsol . t t1;
This function can be used when SystemDSolve1 does not find a solution, for instance, if
some transfer coefficients are variables:
0.3 Log2
In[ ]:= iodine131matrix2 iodine131matrix . k12 1 Cos0.2 t ,
0.25
0.7 Log2 Log2
k14 , k23 1 Cos0.3 t ,
0.25 80
1 1
k30 0.2 Log2, k31 0.8 Log2, k40 12, 131;
12 12
In[ ]:= q1t1, q2t1, q3t1, q4t1
q1 t1, q2 t1, q3 t1, q4 t1 . SystemNDSolve1 iodine131matrix2,
1, 0, 0, 0, 1 0.5 Cos0.3 t, 0, 0, 0, t, 0, 100 , t1, q1;
In[ ]:= PlotCalloutq1t, "Blood", Above, Calloutq2t, "Thyroid", Above,
t, 1, 100, AxesOrigin 0, 0, AxesLabel "Days", "r"
r
5 Thyroid
3
Out[ ]=
Blood
1
Days
20 40 60 80 100
The solution of a SODE with constant coefficients (eqn. 10(3)) can be found as
follows:
t
xt x0 A t A t b
0
where X s and Bs are the Laplace transforms of xt and bt respectively.
This equation can be written in Mathematica as:
In[ ]:= SystemLTSolve1matrixAList, initcondList, inputList, t, s, X :
ModuleB, A1, R, R1, P, r, n, A1 matrixA; n DimensionsA11;
B s IdentityMatrixn A1; R InverseB; P ApartR;
r LaplaceTransforminput, t, s;
R1 DotP, initcond DotP, r;
ThreadSubscriptX, 1s & Rangen R1;
Example: In Figure 10.4 (Godfrey, 1983, Example 6.5) the response of the central
compartment 1 to a single input of 1 unit at t 0 has been fitted (no noise is
assumed) to the function x1 exp t 0.7exp 5 t 0.2exp t 0.1exp 0.1 t.
The problem consists of determining the unknown transfer rates,
k10 , k12 , k13 , k21 , k31 , assumed to be constants.
b1
k21 k13
2 1 3
k12 k31
k10
The Laplace transform X s of an impulsive input of 1 (we can use any mass unit, so if 1 is
in mg the solution will be in mg) in compartment 1 at t 0 can be modeled using
SystemLTSolve1 with the initial conditions 1, 0, 0 and bt 0, 0, 0 . To solve the
problem, we will also need to define the compartmental matrix of the system.
In[ ]:= tricompartment CompartMatrix13,
1, 2, k12 , 2, 1, k21 , 1, 3, k13 , 3, 1, k31 , 1, 0, k10 ;
In[ ]:= X1s, X2s, X3s X1 s, X2 s, X3 s .
SystemLTSolve1tricompartment , 1, 0, 0, 0, 0, 0, t, s, X Simplify
k21 s k31 s
Out[ ]= ,
k12 s k31 s k10 k21 s k31 s s k21 s k13 k31 s
k12 k31 s
,
k12 s k31 s k10 k21 s k31 s s k21 s k13 k31 s
k13 k21 s
k12 s k31 s k10 k21 s k31 s s k21 s k13 k31 s
342 Mathematica beyond mathematics
The Laplace transform X1 exp s of x1 exp t 0.7exp5 t 0.2expt 0.1exp0.1 t is:
In[ ]:= X1exp LaplaceTransform0.7 Exp5 t 0.2 Expt 0.1 Exp0.1 t, t, s
0.2 0.7 0.1
Out[ ]=
s1 s5 s 0.1
Now, the transfer rate k10 , k12 , k13 , k21 , k31 can be obtained solving X1 exp s X1 s. The
following procedure is applied:
In[ ]:= SolveAlwaysX1exp X1s, s
Out[ ]= k10 0.746269, k12 1.25488, k13 1.70885, k21 0.324354, k31 2.06565 ,
k10 0.746269, k12 1.70885, k13 1.25488, k21 2.06565, k31 0.324354
In this case, there are two sets of possible solutions. Notice that neither set contains any
negative rate constants, which would permit the rejection of such set.
All the functions described so far are part of Sysmodel, a package included in
BIOKMOD that we will use in the next section.
In[ ]:= ClearX1, X2, X3, X1exp
Compartmental models can also be used to model pandemics. The SIR model is the
most basic one. It consists of three compartments representing three situations: (S)
Susceptible, (I) Infectious, and (R) Recovered. The model can be visualized as
follows:
t
A very simple version of this model consists of replacing S, I, and R with the
variables s, i, and r, each one representing fractions of the entire population (values
between 0 and 1).
Initially, we assume that almost all of the population is susceptible of getting
infected (i.e., s(0) 1). Over time, that fraction will go down as the infection
spreads. We also assume that the infectious population at t = 0 will be almost 0
(i.e., i(0) 0) and that at the beginning, there will be no people yet who have
recovered (i.e., r(0) = 0). At each point in time, the following equation must hold:
s i r 1. Each infectious individual is also assumed to transmit the disease to
b new people daily so the higher the value of b, the faster the epidemic will spread.
Finally, k represents the fraction of the infected population that recovers every day.
For example, if the disease lasts 7 days, k is 1/7.
The equation below describes how the percentage of the population susceptible of getting
infected changes over time. We can assume that the rate of change is going to be negative
since we start with a value close to 1 that goes down over time. Additionally, the rate of
change is proportional to b, the number of contacts, (the higher the number of contacts, the
higher the number of people becoming infectious and therefore the faster s goes down) and
to s itself, the fraction of the population that is susceptible of becoming infectious at any
time t.
10. Modeling: Application in Biokinetics 343
Out[ ]=
Imagine that we got some data {t,i} with t representing the number of days since
the beginning of the epidemic and i the fraction of infectious people, and we would
like to calculate the values for b and k.
The data has been generated assuming b = 0.5 and k = 1/14 and adding a random component
to both parameters: (SeedRandom[31415]; Evaluate[Table[{t, soli2[0.5 +
RandomReal[{-0.1, 0.1}], 1/14 + RandomReal[{-0.01, 0.01}]][t]}, {t, 10, 100, 5}]] /. {a_,
b_} -> {a, Round[b, 0.001]}
In[ ]:= sample 10, 0.007, 15, 0.108, 20, 0.188, 25, 0.471,
30, 0.516, 35, 0.456, 40, 0.395, 45, 0.153, 50, 0.168,
55, 0.158, 60, 0.071, 65, 0.048, 70, 0.036, 75, 0.015,
80, 0.011, 85, 0.012, 90, 0.015, 95, 0.006, 100, 0.002;
We use FindFit to estimate b and k.
In[ ]:= fit FindFitsample, soli2 b, kt,
b, 0.5, k, 0.01, t, PrecisionGoal 10
Out[ ]=
According to the solution, each infectious person transmits the disease to another person
every two days (b = 0.46 1/2), and an infected person will recover in 14 days (k =
344 Mathematica beyond mathematics
0.071466 1/14).
Let’s plot the fitted model against the actual data:
In[ ]:= PlotEvaluatesols2b, kt, soli2b, kt, solr2b, kt . fit,
t, 0, 100, PlotRange 0, 1.01, PlotStyle Blue, Red, Green,
PlotLegends "Susceptible", "Infectious", "Recovered",
Epilog Point sample
1.0
0.8
0.6 Susceptible
Out[ ]= Infectious
0.4 Recovered
0.2
0 20 40 60 80 100
We can now make the plot interactive to see how the model changes when we
change b and k. Their default values are the ones from the initial model. This
method is quite useful since we can visually adjust the model to improve the fit.
In[ ]:= ManipulatePlotsols2b, kt, solr2b, kt, soli2b, kt,
t, 0, 100, PlotStyle Blue, Red, Green,
PlotLegends "Susceptible", "Infectious", "Recovered",
Epilog Point sample, b, 0.46, 0.1, 1, k, 1 14, 0.01, 1
We can also analyze the sensitivity of the parameters by evaluating the derivative with
respect to them. The higher the sensitivity, the higher the impact on the model when the
parameter values change. For visualization purposes we add and subtract the derivative of
the model multiplied by 0.1 before plotting it.
10. Modeling: Application in Biokinetics 345
b0
k12
x1 x2
k21
Vm km
In our example: b0 = 1, Vm = 0.1, km = 0.3, and k12 and k21 are the unknown
parameters. They can be estimated using experimental data.
We define a model x1 t, k12 , k21 and then use the !"# function to
obtain a numerical solution as a function of k12 and k21:
In[ ]:= ClearAll"Global` "
Vm 0.1; km 0.3;
Vm x1t
eq1 x1 't k12 x1t k21 x2t ;
km x1t
eq2 x2 't k12 x1t k21 x2t;
In[ ]:= sol ParametricNDSolve
eq1, eq2, x10 1, x20 0, x1, x2, t, 0, 100, k12 , k21
Out[ ]= $
$
x1 t, k12 , k21 and x2 t, k12 , k21 can be represented as functions of k12 and k21 .
In[ ]:= ManipulatePlotEvaluatex1k12, k21t, x2k12, k21t . sol,
t, 0, 30, PlotRange All, PlotLegends
Placed"x1", "x2", Center, Framed, RoundingRadius 5 & ,
k12, 0.03, "k12", 0.01, 0.1, k21, 0.02, "k21", 0.01, 0.1
348 Mathematica beyond mathematics
Figure 10.6 Schematic cross section of the liver:(1) Blood, (2) Space of Disse, (3) The hepatocyte.
To scale the equations and make the variables dimensionless we make the
following replacement:
z D DA fub CLint
Z ; dn ; rn ; c z, t CB z, t
L vL QL Q
The coefficients C1 and C2 can be determined using the boundary conditions, which in this
case are: at z 0, c z, and at z , c 0, that is, c0, t z and c, t 0.
350 Mathematica beyond mathematics
&
Out[ ]=
Hence:
In[ ]:= sol3 Evaluatesol2 . c1 1 c2 . c2 0
& &
%
Out[ ]= & &
& %%
& %
Out[ ]= & %
&
&
4 d r t2 t2 2 t zz2
z 4dt
3
0.01
0.1
Out[ ]= 2 0.2
1
2
1
0 1 2 3 4
PS PS VTb
x2 t x1 t kon x2 t koff x3 t 10.6
VTu VTu VTu
VTu
x3 t kon x2 t koff x3 t
VTb
Q PS PS PS PS
1, 1, , 1, 2, , 2, 1, , 2, 2, kon ,
Vp Vp Vp VTu VTu
VTb VTu
2, 3, koff , 3, 2, kon , 3, 3, koff ;
VTu VTb
In[ ]:= physiomodel1
+ )) *( *(
Out[ ]= ))
*' *' *' *( *( *( *(
We have a system of ODE, where some parameters par: {kon , koff } are constants
whose values are unknown:
X t, par A t, par X t B t 10.7
Here is developed a function to solve (10.7) using ParametricNDSolve, where the inputs are
given in an easy way
10. Modeling: Application in Biokinetics 353
The mathematical expression for b1 t was unknown initially, but an experiment was made
where b1 t was given by the best fit of the input function to experimental data
t1 , b1 , , tn , bn obtained via sampling from an arterial catheter. Here are the data:
In[ ]:= dataCateter
0., 0., 0.05, 402.7, 0.1, 430.3, 0.15, 375.4, 0.2, 292.4,
0.25, 202.2, 0.3, 148.4, 0.35, 96.4, 0.4, 64.9, 0.45, 41.7,
0.5, 25.3, 0.55, 17.8, 0.6, 8.8, 0.65, 6.6, 0.7, 3.2,
0.75, 2.5, 0.8, 1.4, 0.85, 0.9, 0.9, 0.5, 1., 0.2, 1.1, 0.07,
1.2, 0.03, 1.3, 0.01, 1.4, 0.003, 1.45, 0.001, 1.5, 0.001;
In[ ]:= ListPlotdataCateter, Joined True, PlotRange All, ImageSize Small
400
300
Out[ ]= 200
100
The shape of the graph suggests that the function below could provide a good fit:
In[ ]:= bt a t Expc t . FindFitdataCateter, a t Expc t , a, c, t
Out[ ]=
400
300
Out[ ]= 200
100
In[ ]:= X1sample 0.03, 27.5, 0.08, 48.61, 0.28, 133.39, 0.33, 106.18,
0.38, 92.11, 0.48, 60.47, 0.55, 56.54, 0.65, 27.73,
0.75, 23.25, 0.85, 15.83, 0.95, 13.82, 1.05, 10.58, 1.15, 7.83,
1.25, 7.35, 1.35, 6.08, 1.45, 5.21, 1.6, 4.18, 1.8, 3.48,
2., 2.86, 2.2, 2.42, 2.4, 2.11, 2.6, 2.01, 2.8, 1.7, 3., 1.58,
3.4, 1.58, 3.75, 1.44, 4.25, 1.35, 4.75, 1.3, 5.25, 1.29,
6.75, 0.81, 7.25, 0.78, 7.75, 0.73, 8.25, 0.75, 11., 0.68;
Then, the solution for x1 where the measurements are taken is:
In[ ]:= X1a x1 . ParametricSystemNDSolve1physiomodel1,
0, 0, 0, inputcatheter1, t, 0.01, 12, x, koff, kon
Out[ ]=
Now we want obtained koff and kon for fitting x1 t samples:
In[ ]:= FindFitX1sample , X1akoff, kont, koff, 0.1, 1, kon, 0.1, 1, t
Once again, we quit the session before proceeding to the next section.
In[ ]:= Quit
the unknown parameters that need to be fitted. The objective is to choose the best
moments{t0 , ..., ti , ..., tn } based on the data, and this can be done using optimal
experimental designs (OED). Here, “best” depends on the objectives of the
practitioner, which are usually linked to a particular optimality criterion, such as
obtaining accurate estimators of the parameters, minimizing the variance of the
predicted response, or optimizing a different characteristic of the model. Many
optimality criteria are based on the Fisher information matrix (FIM). The FIM has a
simple expression for linear models and observations that come from exponential-
family distributions. For more complex models, some formulas can be obtained
from the general information matrix. The usual procedure for a nonlinear model is
to linearize it and apply the well-known toolbox for linear models. The most
widely-used criterion is D-optimality, which focuses on the determinant of the FIM.
In the following pages, we’re going to describe one example using the D-optimality
criterion to obtain the {t0 , ..., ti , ..., tn } values. For further information we refer the
interested reader to: J. Lopez-Fidalgo, J. Rodriguez-Diaz, JM. Sanchez and G.
Santos-Martin, “Optimal designs for compartmental models with correlated
observations”, Journal of Applied Statistics: 32(10), pp.1075-1088, 2005. A D-
optimal design will be a design that maximizes the determinant of the FIM.
We first define rt, . In our example: r(t,1 ,2 ) =
being 1 , 2 the unknown parameters.
In[ ]:= r1, 2 1 2.0 t 0.09 2 0.001 t 0.2 2 ; 1, 2;
rt rt
Then, we compute rt, 1 , 2 = { , }:
1 2
Next, we specify the number of points to be used in the optimal design. We assume a 3-
point design with the first one, t0 , defined by the user.
In[ ]:= n 3;
For computational purposes, we prefer to use the distance di = ti ti1 , instead of ti , then ti =
i di with d0 t0 defined by the user. That is, the points to be estimated are:
In[ ]:= dd Tabledi , i, n
Out[ ]= & & & & & & & & & &
A typical choice for the covariance matrix is = {lij } with lij = exp {|tj tj |} (meaning the
relationship between samples decays exponentially as the time-distance between them
increases), that is:
j1 i1
In[ ]:= ffi, j : Whichi j, 1, i j, ki dk , i j, kj dk ;
356 Mathematica beyond mathematics
Out[ ]= & & & & & & & & & &
& & & & & & & & & &
We assume that all the measures have approximately the same uncertainty level, that is
2 2i , then covariance matrix is = 2 . In our example, we assume = 1 and = 2.
In[ ]:= 2; 1; 2 ;
We also need to give the initial values to 1 and 2 . The ultimate purpose is to find their real
values empirically. However, the method requires an initial guess, (we can use previous
experiments, information derived from similar experiments, etc.) and to find out how
sensible our choice of values has been we can perform a sensitivity analysis. We assume the
following values:
In[ ]:= 1 100; 2 5;
The moment d0 t0 for taking the first measurement is defined by the user. In this case, we
take the measurement at t0 = 0.5.
In[ ]:= d0 0.5;
A D-optimal design will be a design that maximizes det|M|, the determinant of the
information matrix. This can be done using ,$% as follows (in the next example, we
show that usually it is better to use &,$ or ,$% ):
In[ ]:= deter1x : Detm . Threaddd x;
This indicates that the samples should be taken in {ti }. The same solution can be directly
obtained using Biokmod (Section 10.4.4):
In[ ]:= optimumt tm . sol2
In practice, since it’s very difficult to take samples at exactly the precise time, we need to
measure the robustness of our calculations: how sensitive they are to small changes. For that
purpose, we calculate their efficiency using the following formula:
!&
(N is the total number of estimated points).
!$
For example, to measure the efficiency of d3 :
In[ ]:= d3 Tablei, i, 3 d3 . sol2, 3, 3 d3 . sol2, 3, 0.1 ;
10. Modeling: Application in Biokinetics 357
1.000
0.999
Out[ ]= 0.998
0.997
0.996 10 20 30 40 50 60
The optimum point is the one with efficiency 1. In our case, this corresponds to
Det[m[max]], obtained when t:
In[ ]:= optimumt4
Out[ ]=
As usual, to avoid any problems in the next section with previously defined variables we
quit the session.
In[ ]:= fba ? NumberQ, b1 ? NumberQ, t ? NumberQ : Dx1a, b, bt . b b1 . sol
Next, we define the number of points to be used in the optimal design. We assume a 5-point
design:
In[ ]:= n 5 1;
After that, we compute = {lij } with lij = exp {|tj tj |} (remember that di = ti ti1 is used
instead of ti :
In[ ]:= dd Tabledi , i, n; tt FoldListPlus, Subscriptd, 0, dd
Out[ ]= & & & & & & & & & & & & & & &
j1 i1
In[ ]:= ffi, j : Whichi j, 1, i j, ki dk , i j, kj dk ;
The next to last step is the calculation of the information matrix M = X T 1 X . Initially, we
assign arbitrary values to k12 and k21 . As mentioned in the previous section, their real values
are unknown, but we have to make an initial estimation based on the information that we
have. In this case we assume k12 = 0.03 and k21 = 0.02:
In[ ]:= m1ti : TransposeMapX10.03, 0.02, &, ti .
Inverse.MapX10.03, 0.02, &, ti
,$% tries to find a global maximum using analytical methods that may take too long
to compute. For this reason, it may be more convenient to use ,$% or
&,$ , Mathematica functions that take a vector of numbers as their inputs. In our
example, with n = 4.
In[ ]:= objd0 ? NumericQ, d1 ? NumericQ,
d2 ? NumericQ, d3 ? NumericQ, d4 ? NumericQ :
Detm1d0, d1 d0, d0 d1 d2, d0 d1 d2 d3, d0 d1 d2 d3 d4
Finally, we find the solution. Remember that a D-optimal design is a design that maximizes
det|M|, the determinant of the information matrix. We can show the intermediate
calculations with ', :
In[ ]:= sol1 FindMaximumobjd0 , d1 , d2 , d3 , d4 ,
0 d0 10, 0 d1 10, 0 d2 10, 0 d3 10, 0 d4 10,
d0 , 3, d1 , 3, d2 , 3, d3 , 3, d4 , 3 , StepMonitor
Print"d0:", d0 , "d1:", d1 , "d2:", d2 , "d3:", d3 , "d4:", d4
&- &- &- &- &-
&- &- &- &- &-
&- &- &- &- &-
Out[ ]= & & & & &
As we have explained before, to solve D-optimal designs we need to give the initial
values to the starting points k12 and k21 . To learn more about how different values
for those points would impact the design, we need to perform a sensitivity analysis.
One way to proceed would be to calculate the efficiency, as we did in the example
of Section 10.3.1, but in this example we will do it using derivatives.
To analyze the sensibility of x1 with respect to k12 , we keep k21 constant at 0.02 and then
x1k12 , 0.02
calculate k12
, that we call st, k12 , to see how it changes over time. The closer the
derivative is to zero, the less sensitive x1 will be to changes in initial values of k12 . We can
x10.03,k21
do the same for k21 , calculating in this case st, k21 k
(Notice that Mathematica
21
actually calculates the numerical derivatives of x1[k12, 0.02][t] and x1[0.03, k21][t], since
both are numerical expressions).
In[ ]:= Plot Dx1k12, 0.02t, k12 . k12 0.03 . sol,
Dx10.03, k21t, k21 . k21 0.02 . sol,
t, 0, 20, AxesLabel "t", "s t ",
x1 k12 , 0.02 x1 0.03, k21
PlotLegends " ", " "
k12 k21
st
t
5 10 15 20
x1 k12 ,0.02
1
k12
Out[ ]=
x1 0.03,k21
k21
2
3
The derivative gives an idea of the absolute sensibility, but we are more interested in the
relative sensitivity (rs). This sensibility can be analyzed using the expressions rst, k12
x1 0.03, 0.02t st, k12 and rst, k12 x1 0.03, 0.02t st, k21 . A good choice
for is 0.1.
360 Mathematica beyond mathematics
0.8 0.8
0.6 0.6
Out[ ]=
0.4 0.4
0.2 0.2
t t
5 10 15 20 5 10 15 20
The left graph indicates that the sensitivity of the solution with respect to k12 increases with t
until t = 10. We can see the pattern by looking at the difference in the measurement of
rst, k12 between the 3 potential values of k12 around t = 10. After that, it decreases.
Basically, this means that measurements taken at the beginning or at the end of the interval
{0, 20} would not change significantly for different values of k12 . However, measurements
taken around t 10 may show large discrepancies if our initial estimate for the constant was
not the correct one. For k21 , we obtain a similar conclusion (right graph).
In this section, we are going to use the Mathematica package SysModel2 included
in the application Biokmod.
Biokmod can be downloaded at: http://diarium.usal.es/guillermo/biokmod/
To install it, extract the file "biokmodXX.zip" and copy the folder named Biokmod
to the AddOns\Applications directory in the Mathematica installation folder
($InstallationDirectory).
Load the package SysModel2 as usual:
In[ ]:= Needs"Biokmod`SysModel2`"
The ICRP has updated the biokinetic models ICRP 130, ICRP 134, ICRP 137,
ICRP 141:
https://journals.sagepub.com/doi/suppl/10.1177/ANIB_48_2-3/suppl_file/OIR_Data
_Viewer_for_P134-P137-P141.zip
The flow diagram for both, ingestion and injection, is presented in the figure below:
362 Mathematica beyond mathematics
Out[ ]= 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
If we were dealing with injection instead, the b1 coefficient would have been 1 and the rest 0.
Let’s consider the case of I131 ingestion with fA = 0.99 (the value recommended by default
for ICRP 134)
In[ ]:= lambdaIodine131
Out[ ]= 0.086371
1 105
5 104
1 104
Out[ ]=
5000
1000
500
0.5 1 5 10 50 100
equations. In this section, we show some of those capabilities using basic examples.
In[ ]:= ClearAll"Global` "
10
Out[ ]=
5 10 15 20
This next example models a ball falling down a 1-meter-wide staircase from a height of 11
m and an initial velocity of 3 m/s. The first stair is located at h = 10 m and the ball retains
80% of its velocity in each bounce.
In[ ]:= sol2
Blockc .8, NDSolvey ''t 9.81, y0 11, y '0 3, a0 10,
WhenEventyt at 0, y 't c y 't, WhenEventModt, 1,
at at 1, y, a, t, 0, 8, DiscreteVariables a ;
PlotEvaluateyt, at . sol2, t, 0, 8, Filling 2 0
10
Out[ ]=
2 4 6 8
The code below solves and visualizes the system with and without delay:
In[ ]:= lvsystem1, 2 : Y1 t Y1 t Y2 t 1 1 ,
Y1 0 1, Y2 t Y2 t 2 Y1 t 2 , Y2 0 1;
In[ ]:= lv FirstNDSolvelvsystem0, 0, Y1 , Y2 , t, 0, 25;
2.5
2.0
1.5
Out[ ]=
1.0
0.5
u 2 u u
u 0
t x2 x
Here’s an example of the equation:
In[ ]:= ufun NDSolveValue
Dut, x , t 1 100 Dut, x, x, x ut, x Dut, x, x,
ut, 0 1, ut, 1 1, u0, x Cos2 Pi x, u, t, 0, 2,
x, 0, 1, Method "MethodOfLines", "SpatialDiscretization"
"FiniteElement", "MeshOptions" "MaxCellMeasure" 0.01;
In[ ]:= list TablePlotufunt, x, x, 0, 1,
PlotRange 1.1, 1.1, ImageSize Large, t, 0, 2, 1 20;
ListAnimatelist, SaveDefinitions True
10. Modeling: Application in Biokinetics 369
Notice the use of the finite element method (FEM) to solve the equation. To learn more
about it, the “Finite Element User Guide” provides an overview of how to solve PDEs using
this method:
https://reference.wolfram.com/language/FEMDocumentation/tutorial/FiniteElementOvervie
w.html
Cart position
m
1.0
Position
0.5
t Reference
5 10 15 20
0.5
1.0
Out[ ]=
Pendulum angle
5
pendulum.angle
t
5 10 15 20
5
10
370 Mathematica beyond mathematics
Out[ ]= $60.39
Japan Germany
We repeat the same steps for “China vs US” but in this case instead of Subpod content, we
choose ComputableData. The output is shown below. The advantage of this choice is that
the output is being generated as a list allowing easy subsequent manipulation.
11. Economic and Financial Applications 373
GDP at parity, $2.42832 1013 per year , $2.0953 1013 per year ,
real GDP, $1.46318 1013 per year , $1.92945 1013 per year ,
In the WolframAlpha style you can select the information you are interested in
by choosing from the menu that appears after clicking on + Subpod content
or ComputableData in the desired frame. An input cell will be generated to get
only the desired information.
The graph below shows the evolution of the gross domestic product, GDP, of China from
1980 to 2021.
In[ ]:= DateListPlotCountryData"China", "GDP", 1980, 2021
1.4 1013
1.2 1013
1.0 1013
8.0 1012
Out[ ]=
6.0 1012
4.0 1012
2.0 1012
0
1980 1990 2000 2010 2020
We can also use Wolfram|Alpha to access data related to demographics, and make
predictions.
Here the free-form input is used to import data. In this example, we use it to forecast
China’s population.
374 Mathematica beyond mathematics
Population history
China COUNTRY
Family: ARIMA
Out[ ]= TimeSeriesModel
Order: 2, 3, 0
Now, the evolution of China’s population over the next 10 years can be estimated using
TimeSeriesForecast.
In[ ]:= DateListPlotts"TemporalData", TimeSeriesForecastts, 10,
PlotLegends "Historical", "Forecast",
PlotLabel Style"Chinese Population", Bold
Chinese Population
1.4 109
1.3 109
Historical
Out[ ]=
9
1.2 10 Forecast
1.1 109
1.0 109
11.2.1 FinancialData
The FinancialData function in many occasions is the best way to access
finance-related information. Although in theory the program can display
thousands of indicators, in reality the accessible ones are mostly those related to
the US markets. In any case, regardless of whether the information may or may not
be directly accessible from FinancialData, nowadays it would be easy to access
it. For example, many bank platforms enable their clients to download data in an
Excel format that could then be imported into Mathematica and analyzed using any
11. Economic and Financial Applications 375
If we want to see the index value (with a few minutes’ delay) we can type:
In[ ]:= FinancialData"SP500"
Out[ ]= 3677.95
We can also access historical data. In this example, we show the trajectory of the S&P 500
index.
In[ ]:= DateListPlotFinancialData"^GSPC",
"January 1 2000", "September 30 2022", Joined True
5000
4000
3000
Out[ ]=
2000
1000
0
2000 2005 2010 2015 2020
If the financial entity is trading at the time of the query, the price that we get is the
one from a few minutes earlier (usually the delay is 15 minutes). The access to
information in “real time” requires additional payments to 3rd-party providers such
as Bloomberg or Reuters. There are even people willing to pay very large amounts
of money to gain an advantage of a few milliseconds when trading:
376 Mathematica beyond mathematics
http://adtmag.com/articles/2011/07/29/why-hft-programmers-earn-top-salaries.aspx.
The following function will dynamically update the S&P500 index (as long as the
command is executed during trading hours):
In[ ]:= sp500 : FinancialData"SP500"
Out[ ]= 3677.95
Out[ ]= sp500
As mentioned earlier, if the entity is not available in FinancialData we may still be able to
import the information from specialized websites. An interesting one is Nasdaq Data Link
(formerly known as Quandl), https://data.nasdaq.com/, that provides access to numerous
free and premium economic and financial databases. We can first select the data and
download it in our chosen format: Excel, CSV or any other. Then we can use Import to
analyze them using Mathematica. Alternatively, we can access the data directly using
Import or URLFetch.
11.2.2 Visualization
The number of specialized graphs for economic and financial visualization has
increased significantly in the latest versions of Mathematica.
All the functions below end in Chart. We have already seen some of them in Chapter 6. In
this section, we’re going to cover the ones normally used for analyzing stock prices.
In[ ]:= ? Chart
These graphs complement the capabilities of FinancialData but they can also be
used with other functions as shown below:
Let’s visualize the historical closing prices for the Google using different graphs.
In[ ]:= data FinancialData"GOOGL", "Close", 2021, 1, 1, 2022, 9, 30;
If we click on any point inside the plot, we’ll see the information associated to it.
In[ ]:= GraphicsColumn
KagiChartdata, PointFigureChartdata, LineBreakChartdata
140.
120.
100.
80.
2021 2022
140.
120.
Out[ ]=
100.
80.
2021 2022
140.
120.
100.
80.
2021 2022
The next graph uses the function RenkoChart with EventLabels and Placed to add symbols
(“” y “”) indicating in what position they must be placed.
378 Mathematica beyond mathematics
100.
80.
Out[ ]=
60.
40.
2020 2021
The graph below displays the trajectory of GE in the NYSE from January 1, 2021 to
December 31, 2021. For each trading day, we can see the open, high, low, and close prices.
In[ ]:= CandlestickChart"NYSE:GE", 2021, 1, 1, 2021, 12, 31
110.
100.
Out[ ]=
90.
80.
Dec Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec Jan
In the world of professional stock market investors, there’s a group that uses chart
analysis as its main tool for making investment decisions. Its members are known
as chartists and their techniques are known as technical analysis. They closely
study the past performance of stocks or indices to find trends and support and
resistance levels. These people associate certain graph shapes to future markets ups
and downs. Although there’s no sound mathematical theory justifying their
predictions, all economic newspapers have a section dedicated to this type of
analysis.
The powerful function InteractiveTradingChart, includes practically all the functionality
that a chartist may require. In this example, we display GE’s share price history for a given
period. Notice that in the upper area of the graph, we can see a trimester at a time and using
the bottom part we can move through the entire period. We can also choose the time
interval (days, weeks or months) and even what chart type and indicators to show.
In[ ]:= InteractiveTradingChart"NYSE:GE", 2020, 01, 1, 2022, 05, 31
380 Mathematica beyond mathematics
The fit is poor on both tails. As a matter of fact, all the efforts that have been made trying to
predict financial markets have produced unsatisfactory results. These markets exhibit what
Mandelbrot (the father of fractal geometry) called “wild randomness” (single observations
can impact the total in a very disproportionate way).
11. Economic and Financial Applications 381
140
120
100
80
Out[ ]=
60
40
20
0
0 50 100 150
The following functions are included under the Time value Computations category:
TimeValue, EffectiveInterest, Annuity, AnnuityDue and Cashflow. Let’s see some
examples.
TimeValue ["s", "i", "t"] calculates the time value of a security s at time t for an
interest specified by i.
If we invest an amount c during t years at an annual interest rate of i, to find the future value
Mathematica will apply the following formula:
In[ ]:= TimeValuec, i, t
Out[ ]= c i 1t
How much do we need to invest at 3.5% annual interest rate to get 100,000 in 10 years?
In[ ]:= SolveTimeValuequantity, .035, 10 100 000, quantity
We have two options to invest 60,000 for 10 years. Option A offers a fixed annual interest
rate of 4% during the 10 years. Option B offers an interest rate of 3% during years 1 and 2,
4 % in years 3 to 5, and 4.5% until the end of the investment period. Which option is the
best one?
In[ ]:= TimeValue60 000, 0.04, 10 product A ,
TimeValue60 000, 0, 0.03, 2, 0.04, 5, 0.045, 10 product B
Out[ ]= 88 814.7, 89 229.2
very useful to avoid making mistakes when comparing nominal and effective
interest rates. Annuity"p", "t"represents an annuity of fixed payments p
made over t periods. Below we show examples using both functions.
What is the effective rate of an annual nominal interest rate of 4% compounded quarterly?
In[ ]:= EffectiveInterest.04, 1 4
Out[ ]= 0.040604
The next set of functions creates a loan amortization table for a given period and interest
rate. In this case, we borrow 150,062 and make monthly payments of 1,666 during 10
years at 6% nominal annual rate. The same approach can be used with different numbers:
In[ ]:= years 10; monthlypayments 1666; monthlyinterest 0.06 12;
150 000
100 000
Out[ ]=
50 000
Month
20 40 60 80 100 120
11.3.2 Bonds
ClearAll"Global` "
Bonds are financial assets that we can also analyze with Mathematica. When an
organization needs capital, instead of borrowing from banks, they can issue bonds.
Bonds can be considered a type of loan in which the lenders are the ones
purchasing them. Those lenders can usually be anyone. There are different types of
bonds. Normally they pay a fixed amount of interest at predetermined time
intervals: annually, half-annually, quarterly or even monthly. Some bonds issued
for short periods of time (less than one year), such as Treasury Bills, don’t pay
interest explicitly, they are sold at a discount from their par value.
For analyzing bonds, Mathematica has the function FinancialBond. It takes the
following arguments (some of them are optional):
“FaceValue” face value, par value
“Coupon” coupon rate, payment function
“Maturity” maturity or call/put date
“CouponInterval” coupon payment interval
“RedemptionValue” redemption value
“InterestRate” yield to maturity or yield rate
“Settlement” settlement date
“DayCountBasis” day count convention
Let’s see some examples.
The yield to maturity of a 1,000 30-year bond maturing on June 31, 2018, with a coupon
of 6% paid quarterly that was purchased on January 6, 2012 for 900 would be:
In[ ]:= FindRootFinancialBond "FaceValue" 1000, "Coupon" 0.05,
1
"Maturity" 2018, 6, 31, "CouponInterval" ,
4
"InterestRate" y, "Settlement" 2012, 1, 6 900, y, .1
Out[ ]= y 0.069269
A zero-coupon bond with a redemption value of 1,000 after 10 years is currently sold for
400. Find the implied yield rate compounded semiannually:
384 Mathematica beyond mathematics
Out[ ]= y 0.0937605
11.3.3 Derivatives
In[ ]:= Clear"Global` "
Out[ ]= Telefónica
We’re going to need the 12-month EURIBOR rate. This type of interest rate is published by
Euribor EBF (http://euribor-rates.eu). Let’s import the 10 most recent rates (remember that
in Chapter 2 we explained how to extract specific parts of a web page) as follows:
In[ ]:= Import"http: www.euriborrates.eu euriborrate12months.asp", "Data"
3, 2, 1
4 14 2022, 0.003 4 13 2022, 0.014 4 12 2022, 0.005 4 11 2022, 0.030 4 8 2
Out[ ]= 4 1 2022, 0.086 3 1 2022, 0.363 2 1 2022, 0.431 1 3 2022, 0.499 12 1 2
1 3 2022, 0.499 1 4 2021, 0.502 1 2 2020, 0.248 1 2 2019, 0.121 1 2 2
However, unless we’re creating a program to automate the process, probably the easiest
thing to do is to copy the values directly. Besides, it’s unusual for websites to keep their
structure for a long time so, by the time you read these lines, the instruction above may not
work. We’ll store in memory the first rate that we will use for our calculations later on.
In[ ]:= euribor12m 0.003 100
Out[ ]= 0.00003
For our computations, we need to know several indicators associated to the stock, starting
with its price (Figure 11.1). The values returned are time sensitive and depend on the time
of execution of the query. We show the commands below so readers can replicate the
example.
In[ ]:= FinancialData"NYSE:TEF", "Price" stock price,
in USD, in NYSE at the time of the query
Out[ ]= $3.26
Stock volatility during the past 50 days. (Volatility is a measure of the variability of the
stock price over a specific time horizon. It’s usually the standard deviation of the closing
stock price during the last 50 trading sessions, or an alternative chosen time horizon):
In[ ]:= FinancialData"NYSE:TEF", "Volatility50Day"
Out[ ]= 28.1187
Last year’s dividend yield, computed as the ratio between the dividends paid and the current
stock price.
In[ ]:= FinancialData"NYSE:TEF", "DividendYield"
Out[ ]= MissingNotAvailable
Now we can evaluate the price of a call option given its expiration date (in this case, in one
year), and using the current stock price as the strike price:
In[ ]:= FinancialDerivative"American", "Call",
"StrikePrice" spotPrice, "Expiration" 1,
"InterestRate" euribor12m , "Volatility" vol,
"CurrentPrice" spotPrice, "Dividend" div
Out[ ]= 0.399738
In the next graph, we can see the evolution of the value of our call spread as a function of
the underlying asset price and the remaining time. To make it easier to visualize, the solid
green line indicates the current price, our strike price; and the dashed red line, the price of
the underlying asset at which we maximize our profit. We need to keep in mind that our
starting point in the time scale is 1. Remember that our goal is to have a maximum gain of
18%.
In[ ]:= data , 1 2, FinancialDerivative"American", "Call",
"StrikePrice" spotPrice, "Expiration" 1,
"InterestRate" euribor12m , "Volatility" 0.5,
"CurrentPrice" , "Dividend" div, "ReferenceTime" 2
FinancialDerivative"American", "Call",
"StrikePrice" 1.18 spotPrice, "Expiration" 1,
"InterestRate" euribor12m , "Volatility" 0.5,
"CurrentPrice" , "Dividend" div, "ReferenceTime" 2 &
FlattenTableprice, referencetime, referencetime, 0, 1, 0.05,
price, 0.8 spotPrice, 1.4 spotPrice, 1, 1;
388 Mathematica beyond mathematics
Out[ ]=
At expiration, the profile of our profit as a function of the underlying asset price is:
In[ ]:= ListPlotCasesdata, x, 0., y x, y,
Joined True, PlotStyle DirectiveThickness0.02, Red,
AxesLabel "Underlying Price", "Profit"
Profit
0.6
0.5
0.4
Out[ ]=
0.3
0.2
0.1
Underlying Price
3.0 3.5 4.0 4.5
In this example, the warrants that we’ve used are of the American type, meaning
that they can be exercised at any time until the expiration date, although it is not
usually optimum to do so.
However, Mathematica, in addition to financial derivatives price calculations, can
also compute their sensitivities, known as Greeks. These sensitivities are the partial
derivatives of the option price with respect to different valuation parameters. For
example, the delta of an option is the partial derivative of its price with respect to
the price of the underlying asset. This measure not only gives us an idea of how
11. Economic and Financial Applications 389
sensitive the option value is to small changes in the price of the underlying asset
but also, for standard options (also known as vanilla), it gives us the probability
that the option will be exercised. Gamma, the second derivative with respect to the
underlying asset price, tells us how sensitive the delta is to changes to it. This
metric is very important for hedging purposes. Rho is the partial derivative with
respect to the interest rate and in the case of stock options is the least sensitive
Greek. Theta is the derivative with respect to time to expiration. Initially, since
options are not usually bought in the money i.e. if a ticker trades at $12, purchasing
an option for $10 is not very common, they don’t have intrinsic value. They only
get value from their time component. The final one, Vega, is the partial derivative
with respect to the volatility of the underlying asset. This is such an important
parameter in option valuation that for certain options, their prices are quoted in
terms of the volatility of their respective underlying assets.
The graph below shows how the Greeks change with respect to the remaining time to
expiration:
In[ ]:= ticksmin, max : Table
j, ToStringRound12 j 12 " Months", .03, 0, j, min, max, 0.2
In[ ]:= TabViewRule Transpose"Delta", "Gamma", "Rho", "Theta", "Vega",
ListPlot, Ticks ticks, Automatic & TransposeTablei, &
FinancialDerivative"American", "Call", "StrikePrice" 55.00,
"Expiration" 1, "InterestRate" 0.1, "Volatility" 0.5,
"CurrentPrice" 50, "Dividend" 0.05, "ReferenceTime" i,
"Greeks" All, 2 , i, 0, 0.95, 0.05
0.5
0.4
Out[ ]=
0.3
0.2
0.1
The same Greek letters are used to indicate the risks of portfolio of options due to
changes in the price of the underlying assets or market conditions.
Delta: the delta of a portfolio of options with the same underlying assets is the
weighted average of the delta of the individual options.
The delta factor = the change in the price of the option as a result of changes in the
price of the underlying assets. For an at-the-money option, the delta factor is
390 Mathematica beyond mathematics
usually close to 50%, so if the underlying price changes by 1, the price of the
option will change by 0.5.
Now, let’s do the same computation using the Black–Scholes equation (To create more
visually appealing documents you can convert the input cell to the traditional format:
Cell Convert to TraditionalForm):
In[ ]:= BlackScholesModel
, 1 2 , ,
2
, 2 0, , max , 0;
2 2
After solving the boundary value problem we get:
In[ ]:= dsol ct, s .
DSolveBlackScholesModel, ct, s, t, s
1 TraditionalForm
Out[ ]//TraditionalForm=
1 2 logk 2 r 2 t T 2 logs
r T s r T erfc
2 2 2 T t
2 logk 2 r 2 t T 2 logs
k r t erfc
2 2 T t
Finally, we calculate the price of the derivative and compare it with the one previously
obtained. We can see it’s the same:
In[ ]:= dsol . t 0, s spotPrice, k spotPrice, vol, T 1,
r euribor12m
Out[ ]= 0.397621
Ratings-Based approach, the minimum amount of capital that a bank should keep
following the standards defined by the Basel II regulatory framework on bank
capital adequacy.
http://demonstrations.wolfram.com/BaselIICapitalAdequacyInternalRatingsBasedI
RBApproach
To facilitate the understanding of the example, we also include the code:
In[ ]:= Setting defaults
DefaultEAD 1 000 000;
MinimumPD 0.0001; MaximumPD 0.25;
MinimumLGD 0.01; DefaultLGD 0.7; MaximumLGD 1.0;
MinimumMaturity 1 250;
DefaultMaturity 1;
MaximumMaturity 30;
In[ ]:= Create shorthands for Normal CDF & CDF inverse
Phix : CDFNormalDistribution0, 1, x;
PhiInversep : InverseCDFNormalDistribution0, 1, p;
In[ ]:= Correlation Function, Basel Committee's way of parameterising a 2
parameter formula PD, rho with 1 parameter PD .
RPD : 0.12 1 Exp50 PD 1 Exp50
0.24 1 1 Exp50 PD 1 Exp50 ;
ConditionalPercentageExpectedLossPD, LGD : Phi
PhiInversePD SqrtRPD PhiInverse0.999 Sqrt1 RPD LGD
UnconditionalPercentageExpectedLossPD, LGD : PD LGD;
In[ ]:= Maturity Adjustment, with a 1year maturity as a base case
bPD : 0.11852 0.05478 LogPD ^ 2;
MaturityAdjustmentMaturity, PD :
1 Maturity 2.5 bPD 1 1.5 bPD ;
In[ ]:= Capital Requirement Calculation: note as PD 1,
this quantity 0, as credit risk is taken care of via provisioning,
i.e. ELoss exposure x PD x LGD.
CapitalRequirementPD, LGD, Maturity :
ConditionalPercentageExpectedLossPD, LGD
UnconditionalPercentageExpectedLossPD, LGD
MaturityAdjustmentMaturity, PD;
392 Mathematica beyond mathematics
To learn more about financial modeling and risk management using Mathematica,
Igor Hlivka has created several excellent example notebooks. You can find them
in:
http://community.wolfram.com/web/ihcomm.
11.4 Optimization
The following tree can help us decide the most appropriate function to find the
minimum. (To find the maximum we would use the functions Maximize,
NMaximize or FindMaximum instead). Note: Since Mathematica 12,
LinearProgramming has been superseded by LinearOptimization, but the command
can still be used.
Is your problem linear?
Yes No
LinearProgramming or
L Do you want a global optimum?
Out[ ]=
Yes No
Yes No Yes No
In many cases, the problem can be solved using several functions. With the
exception of LinearProgramming and LinearOptimization, the syntax for the rest of
the functions is very similar: f[{objective function, constraints}, {variables}].
In[ ]:= Clear"`Global` "
394 Mathematica beyond mathematics
The maximum can be found using any of the previously mentioned commands:
In[ ]:= NMaximizeob,c1,c2, var, Maximizeob,c1,c2, var,
FindMaximumob,c1,c2, var
3. x 1., y 2.
Out[ ]= 3 x 1, y 2
3. x 1., y 2.
We can see that the maximum occurs when {x 1, y 2} and is 3. We can verify it
(remember that “/.” is used for substitutions).
In[ ]:= x y . x 1, y 2
Out[ ]= 3
We can interpret the problem graphically by drawing the plane x y only in the region:
{0 x 1, 0 y 2}.
In[ ]:= Plot3Dx y, x, 1, 2, y, 1, 2,
RegionFunction Functionx, y, 0 x 1 && 0 y 2,
AxesLabel Automatic, Mesh None
Out[ ]=
The graph clearly shows that the maximum is in (1,2) and the minimum in (0,0). Use the
mouse to move the graph around to see it from different perspectives.
In certain situations, we’d like the variables to take only integer values. To add this extra
requirement in Mathematica, we use the built-in symbol Integers.
Let’s consider the following model in which we want to minimize the objective function
(ob) (notice that equality when typing constraints is "=="):
In[ ]:= Clear"`Global` "
11. Economic and Financial Applications 395
Since this is a linear problem, we can also use either LinearOptimization (since
Mathematica 12) or LinearProgramming. As a matter of fact, these functions are
the most appropriate ones for linear problems, especially if the number of variables
is large.
In[ ]:= LinearOptimizationob, c1, c2, c3, c4, x Integers, y Integers
Out[ ]= x 4, y 27
The syntax is: [c, m, b] . This function finds the vector x
that minimizes the quantity c.x subject to the constraints m.x b and x 0. We
can limit the values that the variables (some or all of them) can take to just integers
with [..., Integers].
For comparison purposes, let’s solve the same problem using .
The syntax is: [c, m, b] . This function finds the vector x that
minimizes the quantity c.x subject to the constraints m.x b and x 0. We can limit the
values that the variables (some or all of them) can take to just integers with
[..., Integers].
x 2y c : 1, 2
5 x y 7 m1 : 5, 1 b1 : 7, 0
x y 26 m2 : 1, 1 b2 : 26, 1
Notice that the syntax to indicate the type of constraint is as follows: {bi , 0} if mi .x bi ;
{bi , 1} if mi .x bi and {bi , 1} if mi .x bi .
In[ ]:= LinearProgramming1, 2, 5, 1, 1, 1, 7, 0, 26, 1,
3, Infinity, 4, Infinity, Integers Quiet
Out[ ]= 4, 27
In this problem, both, the objective function that we want to minimize and the constraints
are nonlinear.
In[ ]:= var x, y;
ob x ^ 2 y 1 ^ 2;
c1 x ^ 2 y ^ 2 4;
Let’s calculate the global minimum with Minimize:
In[ ]:= Minimizeob, c1, var
NMinimize can solve this type of problem faster (although in this example we wouldn’t be
able notice any difference). We add Chop to remove, when the solution is 0, the spurious
values that may appear in the output when using numerical approximations. Execute the
command below without it and see what happens.
In[ ]:= NMinimizeob, c1, var Chop
If we are looking for a local minimum we can use FindMinimum. Although it’s not
necessary, this function is much faster if we enter an initial point from which to start the
search. It’s also a good idea to add Chop.
In[ ]:= FindMinimumob, c1, x, 0, y, 0 Chop
8
Out[ ]= 3.3699 10 , x 0, y 0.999999
We present the previous problem graphically with Plot3D and ContourPlot, very
convenient functions when trying to find optimum points visually. (The option
Contours specifies how many contours we want to display; if not specified, Mathematica
will choose the optimum number for the given function).
In[ ]:= GraphicsRowPlot3D x ^ 2 y 1 ^ 2, x, 3, 3,
y, 3, 3, RegionFunction Functionx, y, x ^ 2 y ^ 2 4,
AxesLabel Automatic, Mesh All, ContourPlotx ^ 2 y 1 ^ 2,
x, 3, 3, y, 3, 3, RegionFunction Functionx, y, x ^ 2 y ^ 2 4,
Contours 100, FrameLabel Automatic
Click inside the output from Plot3D and move the graph around to see the minimum from
different angles. In the contour plot, we can see that the minimum corresponds to the
coordinates (0, 1). By default, dark colors are associated to small values and light hues are
linked to bigger ones. If you place your cursor over the contour limits, you’ll see the value
that the function takes at that location. (All the points connected by the same line have the
same function value.)
Here is another example of nonlinear optimization, in this case for finding the global
minimum (Minimize). The objective function is Expx y with the constraint that x, y a
Circle centered in {0, 0} and with a radius r 1. We show the result, both numerically and
graphically.
In[ ]:= m MinimizeExpx y, x, y Disk, x, y
1 1 1
Out[ ]= , x , y
2 2
11. Economic and Financial Applications 397
Out[ ]=
The following example shows how the optimization algorithm approaches the optimum
(minimum) point for f(x,y) in an iterative way.
In[ ]:= Clear"Global` "
Out[ ]=
We’d like to minimize the total amount of bricks transported per unit of distance,
i,j xi,j di,j , meeting the demand without exceeding the stock available in the
warehouses.
For that purpose, we have to calculate the minimum of i,j xi,j di,j , where xi,j
corresponds to the quantities transported from the warehouses i:{1, 2} to the sites
j:{1, ..., 6} with di,j representing the distance between warehouse i and site j.
To calculate the distances between the warehouses and the sites we use the function
EuclideanDistance (in the bi-dimensional case, it’s the equivalent to the Pythagorean
theorem):
In[ ]:= EuclideanDistancex1, y1, x2, y2
The command below returns triplets, {i, j, di,j }, indicating the distance in km between the
warehouse i and site j. We use Roundvalue, 0.1 to round the distance to the nearest
first decimal.
In[ ]:= Tablei, j, RoundEuclideanDistancewarehouses i , sites j , 0.1,
i, 1, 2, j, 1, 6
1, 1, 5.8 1, 2, 1.8 1, 3, 7.5 1, 4, 4.2 1, 5, 6.8 1, 6, 6.8
Out[ ]=
2, 1, 7.8 2, 2, 10.7 2, 3, 4.5 2, 4, 5.5 2, 5, 2.7 2, 6, 5.4
Therefore, the variables and the objective function, i,j xi,j di,j , are:
In[ ]:= var x11 , x12 , x13 , x14 , x15 , x16 , x21 , x22 , x23 , x24 , x25 , x26 ;
In[ ]:= of 5.8 x11 1.8 x12 7.5 x13 4.2 x14 6.8 x15
6.8 x16 7.8 x21 10.7 x22 4.5 x23 5.5 x24 2.7 x25 5.4 x26 ;
The constraints are: i xi,j = demandj , j xi,j = stocki and xi,j 0
In[ ]:= constraints x11 x21 3 && x12 x22 5 && x13 x23 4 && x14 x24 7 &&
x15 x25 6 && x16 x26 11 && x11 x12 x13 x14 x15 x16 20 &&
x21 x22 x23 x24 x25 x26 25 && x11 0 && x12 0 && x13 0 && x14 0,
x15 0 && x16 0 && x21 0 && x22 0 && x23 0 && x24 0 && x25 0 && x26 0;
The solution is:
In[ ]:= NMinimizeof, constraints, var
Out[ ]= 149.4, x11 3., x12 5., x13 0., x14 7., x15 0.,
x16 0., x21 0., x22 0., x23 4., x24 0., x25 6., x26 11.
This result indicates that the optimum solution, 149.4 tonnes km, is obtained by
transporting 3 tonnes from warehouse 1 to site 1, 5 tonnes from warehouse 1 to site 2 and so
on.
In “Optimal Transport Scheduling”, a demonstration by Yifan Hu, you can see a dynamic
visualization of the resolution of this problem:
11. Economic and Financial Applications 399
http://demonstrations.wolfram.com/OptimalTransportScheduling
If in the demonstration you use 20 and 25 tonnes, you’ll see that the solution is the same
(there’s actually a small difference due to rounding).
The capital not invested in any of the products will be placed in government bonds
(assumed to be riskless) with a bi-annual rate of return of 3%. The objective of the
consulting firm is to allocate the capital to each of the products to meet the
following goals:
(a) Maximize the return per invested.
(b) Keep the possibility of loss to a maximum of 5% of the total amount invested.
(c) Invest at least 20% in commercial loans.
(d) Allocate to mutual funds and personal loans an amount no larger than the one
invested in mortgages.
Variables: xi is the percentage of capital invested in product i. The amount placed in
government bonds can be considered as a new product with an expected return, in
percentage, b6 = 3 and risk r6 = 0. Therefore:
In[ ]:= var x1 Mortgages , x2 Mutual funds ,
x3 Personal loans , x4 Commercial loans , x5
Certificates Bonds , x6 Government debt ;
Objective function to maximize: 6i 1 bi xi .
In[ ]:= of 9 x1 12 x2 15 x3 8 x4 6 x5 3 x6 ;
Constraint 4: The percentage invested in mutual funds x2 ) and personal loans (x3 ) cannot
be bigger than the one invested in mortgages x1 ).
In[ ]:= c4 x2 x3 x1 ;
Using NMaximize, Mathematica tells us that we’d get a return of 11.2% investing 40% in
mortgages, 40% in personal loans, and 20% in commercial loans.
In[ ]:= sol NMaximizeof, c1, c2, c3, c4, c5, var
In[ ]:= "" ToString AccountingForm10 000 000 sol 1 100, DigitBlock 3
Out[ ]= 1,120,000.
In[ ]:= NMaximizecp, i, 0.4 p 1.2 && 20 i 200, p, i
We can verify the solution visually using Plot3D and ContourPlot. If we place the cursor on
the second graph and move it around, we can see the different values of the function c r, t.
This way we can check that the upper-left corner corresponds to the largest value
(maximum), and the lower-right corner to the smallest one (minimum). The boundaries of
the curve correspond to the constraints.
In[ ]:= GraphicsRowPlot3Dcp, i, p, 4, 12, i, 0, 200,
RegionFunction Functionp, i, 4 p 12 && 20 i 200,
AxesLabel "Price", "Income", "c", Mesh None,
ContourPlotcp, i, p, 4, 12, i, 0, 200, RegionFunction
Functionp, i, 4 p 12 && 20 i 200, Mesh None,
Contours 100, FrameLabel Automatic, PlotLegends Automatic,
Frame All, AspectRatio 1 3, ImageSize 600, 300
11. Economic and Financial Applications 401
Carrefa would like to know how much to order from each supplier to minimize the
total cost.
In[ ]:= Clear"Global` "
This is the type of problem that can be solved with LinearProgramming or the
newest function LinearOptimization. With LinearProgramming, matrix notation is
required. For didactic purposes we’re going to compare both functions using
algebraic notation for LinearOptimization although matrix notation can also be
used.
With LinearOptimization the variables can be defined as follows: xij = number of
cartons from supplier i , i:{1, 2, 3}, with {1 = Pascualo, 2 = Simone, 3 = Pulova},
to center j: {1, 2, 3, 4} , with {1 = Barcelona, 2 = Madrid, 3 = Seville, 4 =
Valladolid}.
In[ ]:= var x11 , x12 , x13 , x14 , x21 , x22 , x23 , x24 , x31 , x32 , x33 , x34 ;
To define the objective function, we have to group terms by supplier. For example: the total
cost of sending x units from Pascualo to Barcelona (x11 ) would be the transportation cost
plus the purchasing price: (0.5 + pa). We use the same approach for calculating the rest of
the variables:
In[ ]:= pa, sim, pul 3, 2.8, 2.7;
402 Mathematica beyond mathematics
Out[ ]= 3.5 x11 3.6 x12 3.7 x13 3.5 x14 3.3 x21
3.4 x22 3.6 x23 3.3 x24 3.1 x31 3.2 x32 3.6 x33 3.4 x34
The constraint c1 representing the maximum number of cartons of milk that we can receive
from Pascualo is written as (the values of those variables not related to the constraint should
be 0), m1 and bi:
In[ ]:= c1 x11 x12 x13 x14 2500;
The constraint c3 or m3, dealing with the supply coming from Pulova:
In[ ]:= c3 x31 x32 x33 x34 1500;
The constraint c4, x11 x21 x31 800, indicating the demand from Barcelona:
In[ ]:= c4 x11 x21 x31 800;
The constraint c5, x12 x22 x32 1100, referring to the demand from Madrid:
In[ ]:= m5 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0; b5 1100, 0;
And finally the constraint c7, or m3 to make sure that the needs of Valladolid are being
satisfied:
In[ ]:= c7 x14 x24 x34 300;
Now we can solve the problem (LinearProgramming assumes that all the variables are
nonnegative so we don’t have to include those constraints).
In[ ]:= sol1 LinearProgrammingof,
m1, m2, m3, m4, m5, m6, m7, b1, b2, b3, b4, b5, b6, b7
Out[ ]= 0., 0., 0., 0., 400., 0., 500., 300., 400., 1100., 0., 0.
Using LinearOptimization the constraint that all the variables are nonnegative should be
included.
In[ ]:= c8 Threadvar 0
11. Economic and Financial Applications 403
Out[ ]= x11 0, x12 0, x13 0, x14 0, x21 0, x22 0, x23 0, x24 0, x31 0, x32 0, x33 0, x34 0
In[ ]:= sol2 LinearOptimizationof1, c1, c2, c3, c4, c5, c6, c7, c8, var
Out[ ]= x11 0., x12 0., x13 0., x14 0., x21 0., x22 400.,
x23 500., x24 300., x31 800., x32 700., x33 0., x34 0.
Both commands return the optimal value for each variable. If we’d like to calculate the
minimum total transportation cost, we just need to multiply the objective function by the
solution (we use "." for matrix multiplication). Although sol1 and sol2 are different , the
optimization values are the same; this happens when the problem has more than one
solution.
In[ ]:= of.sol1
Out[ ]= 8870.
Out[ ]= 8870.
all possible moves, use different criteria to choose the most appropriate ones).
For this type of problem, Mathematica has the function:
FindShortestTour[{e1, e2, ...}]. This function takes a list of coordinates: {e1,
e2, ...} and attempts to find the optimum ordering so that the total distance traveled
is minimized and all the points have been visited just once. The output returns
both, the total distance and the order of traveling.
Example: Given a set of points p = {1, 2, 3, 4, 5, 6} each with
coordinates{x,y}:{{4, 3},{1, 1}, {2, 3},{3, -5},{-1, 2},{3, 4}}, find the shortest
path that will visit all the points only once.
In[ ]:= d 4, 3, 1, 1, 2, 3, 3, 5, 1, 2, 3, 4;
Out[ ]= 2 2 5 3 10 65 , 1, 4, 2, 5, 3, 6, 1
We sort the points following the order from the output above and represent them
graphically:
In[ ]:= d order
4 3
3 5
1 1
Out[ ]= 1 2
2 3
3 4
4 3
In[ ]:= GraphicsLine
Out[ ]=
"Position", "EntityAssociation"
Next, we find out that the shortest route starts where Apollo 11 landed:
In[ ]:= order LastFindShortestTourlandingcoords
Out[ ]= 1, 6, 4, 2, 3, 5, 1
Edmonton
12
16 4 Saskatoon
10
9
In[ ]:= g ;
7 20
Vancouver 13 Calgary
14
Winnipeg
Regina
4
The previous illustration has been created using the Graph function. For simplifying
purposes, let’s just focus on 3 cities: { (1) Vancouver, (2) Calgary, (3) Edmonton}. The
flow directions (ij) and capacities (EdgeCapacity) are: {From (1) Vancouver to (2)
Calgary, 13, from (1) Vancouver to (3) Edmonton, 4, from (3) Edmonton to (2) Calgary, 10
and from (2) Edmonton to (3) Calgary, 4}. We display that information using
DirectedEdge and with the help of VertexLabels and Placed position the labels in the
desired locations. The style is defined using Style [#, “Style”]&. You can use the
documentation to learn more about the syntax for each function.
In[ ]:= g1 Graph
"Vancouver", "Calgary", "Edmonton",
1, 2, 1, 3, 2, 3, 3, 2, Null, EdgeCapacity 13, 16, 4, 10,
EdgeLabels DirectedEdge"Vancouver", "Calgary" Placed13, 0.5, 2, 1,
DirectedEdge"Vancouver", "Edmonton" Placed16, 0.5, 1.5, 2 ,
DirectedEdge"Edmonton", "Calgary" Placed10, 0.5, 0.2, 1.5,
DirectedEdge"Calgary", "Edmonton" Placed4, 0.5, 0.7, 1.5 ,
GraphStyle "BasicBlack",
VertexLabels "Edmonton" Placed" 3 Ed.", Above, Style, Red & ,
"Vancouver" Placed" 1 Vanc.", After, Style, Blue & ,
"Calgary" Placed" 2 Cal.", Above, Style, FontFamily "Helvetica" & ,
VertexSize Small, ImagePadding All, ImageSize Small
3Ed. 2Cal.
10
Out[ ]=
13
16
1Vanc.
In the next example, we add VertexCoordinates to specify the coordinates for the center of
the vertices. We also modify several options to make it easier to overlay the graph on a map
of Canada later.
11. Economic and Financial Applications 409
Graph
"Vancouver", "Calgary", "Edmonton",
1, 2, 1, 3, 2, 3, 3, 2, Null, EdgeCapacity 13, 16, 4, 10,
EdgeLabels DirectedEdge"Edmonton", "Calgary" Placed10, 0.5, 0.2, 1,
DirectedEdge"Vancouver", "Edmonton" Placed16, 0.5, 1, 0,
DirectedEdge"Vancouver", "Calgary" Placed13, 0.6, 1, 1.5,
GraphStyle "BasicBlack", ImagePadding 0, 40, 0, 10,
VertexCoordinates 0.253, 0.112, 0.1419, 0.1239, 0.122, 0.1735,
VertexLabels "Edmonton"
Placed"Edmonton", Above, Style, FontFamily "Helvetica" & ,
"Vancouver" Placed"Vancouver",
1.5, 1, 0, 0, Style, FontFamily "Helvetica" & ,
"Calgary" Placed"Calgary", Below, Style, FontFamily "Helvetica" & ,
VertexSize Small
Edmonton
16 10
Out[ ]=
Calgary
13
Vancouver
Flow value: 23
Out[ ]= OptimumFlowData
Out[ ]= 23
Here we display the result on top of the map of Canada, downloaded using CountryData
(for further details see Chapter 5).
In[ ]:= PanelShowCountryData"Canada", "Shape", F"FlowGraph",
PlotRange 0.31, 0.083, 0.04, 0.25
Edmonton
12
16 4 Saskatoon
Out[ ]= 10
9
7 20
13 Calgary
Vancouver 14
Winnipeg
Regina 4
11.7 BlockChain
Let’s assume that I’d like to transfer $500 from one of my bank accounts to
another one. This kind of transaction doesn’t actually require the movement of
dollar bills from one bank to another. It just needs a simple change in the banks’
electronic balances. Similar transactions happen every day affecting millions of
accounts without involving any physical money, just modification of electronic bits.
Until recently, this type of financial activity was mostly done using a network of
banks. However, in 2008 someone (or maybe a group of people) under the
pseudonym of Satoshi Nakamoto, sent a message to a cryptography forum
containing a 9-pages document describing a method for creating a digital coin that
could be used as a unit of account and medium of exchange. The following year,
the ideas behind that paper were implemented using a virtual coin named bitcoin:
https://bitcoin.org/bitcoin.pdf .
Although Nakamoto was talking about a concrete coin, he was really describing a
revolutionary technology named blockchain with a multitude of potential
applications.
This technology eliminates intermediaries by decentralizing the network. Users,
instead of banks or other centralized organizations, take control, and it’s them that
keep track and verify all the transactions being recorded on the blockchain. One of
the most recent applications is the recording of the virtual ownership of digital files
through something called non-fungible tokens or NFTs.
11. Economic and Financial Applications 411
Blockchains are basically decentralized digital ledgers in which the entries (blocks)
are interconnected and encrypted to protect the privacy and safety of transactions.
They are, in other words, distributed and secured databases that can contain any
kind of records, not just economic ones. These chains of blocks have a very
important requirement: There have to be users (nodes) in charge of verifying and
approving the transactions so that the blocks containing them can be added to the
ledger.
Although a detailed explanation of blockchains is beyond the scope of this book,
it’s important to mention that to design one, we would need to consider first a list
of basic requirements known as the “five pillars of blockchain”:
1. Permissioned/Private. Writing records is exclusive to members; third parties can be granted read access,
with the general public excluded. The permissions architecture goes beyond “access = everything” and
allows third-party access to specific raw data, as deemed appropriate, for interoperability and application
requirements.
2. Decentralized/P2P. Allowing for equal control over the shared database among all permissioned
participants and of equal importance; distributing the number of full copies of the ledger to maximize the
probability that there will always be a complete record in existence and available for those with permissions
to access.
3. Immutability & Data Integrity. Records are guaranteed to be cryptographically secure, with no possibility
for bad actors to threaten data integrity.
4. Scalability. The ability to secure trillions of transactions or records without compromising the networks
synchronization, security, accessibility, or data integrity.
5. Security. Support for data encryption and the management and enforcement of complex permission settings
for participants and third parties.
Mathematica includes functionality to design and manage blockchains and there
are many videos covering these topics at:
https://www.wolfram.com/broadcast/s?sx=blockchain
The functions available related to blockchains can be used for:
Configuration ($BlockchainBase)
Reading (BlockchainData, BlockchainBlockData, BlockchainTransactionData and
BlockchainAddressData)
Writing purposes (BlockchainTransaction, BlockchainTransactionSign and
BlockchainTransactionSubmit):
Here are some examples:
We can first specify the default blockchain to be used for blockchain computations with
$BlockchainBase:
In[ ]:= $BlockchainBase "Bitcoin"
Out[ ]= Bitcoin
Now we can read the information about the specified default blockchain using
BlockchainData:
In[ ]:= BlockchainData Dataset
412 Mathematica beyond mathematics
"Address" "mo9QWLSJ1g1ENrTkhK9SSyw7cYJfJLU8QH" ,
As of May 2022, Mathematica supports three virtual coins: Bitcoin, Ethereum, and
Ark (https://ark.io) . This last coin could be very useful if you were interested in
developing blockchain applications since there is a written agreement between
ARK Ecosystem and Wolfram Blockchain Labs (WBL) that integrates the ARK
public API into the Wolfram Language.
WBL: https://www.wolframblockchainlabs.com/
Notice the appearance of the word “Hash” in both, one of the properties of the
BlockchainData output and in the name of the blockchain transaction. This term
refers to a type of mathematical function that converts an input of arbitrary length
into an output of fixed length consisting of a series of unique characters. This
function is crucial to validate transactions, safely store passwords, and sign
electronic documents.
time compared to how long it would take to perform the calculation sequentially.
There are many scientific and technical fields where parallel computing is very
useful or even the only realistic way to perform certain types of calculations. For
example: in meteorology, there used to be predictive models that took longer to
evaluate than the actual prediction window.
In the past, parallel programming was a niche area only available for expensive
hardware. However, today even PCs have more than one processor. If you have an
NVidia graphics card, you can also use CUDA (included in Mathematica) for
parallel computing. This computation power gets even bigger if we connect
computers to a grid. You can even participate in collaborative projects: One of the
best examples of this approach was the well-known SETI program, another
example of a collaborative participation project is the study of protein folding:
https://fold.it/.
In summary, Mathematica has the capability to perform calculations in parallel
right out of the box. In addition, it’s possible to set up a grid to distribute
computations to several computers using either gridMathematica or Wolfram
Lightweight Grid Manager
gridMathematica: https://www.wolfram.com/gridmathematica
WLGM: https://www.wolfram.com/lightweight-grid-manager
5, 1, 7, 1, 97, 1, 101, 1, 2, 1, 3, 1, 7, 1, 10 091, 1,
2, 1, 773, 1, 2, 2, 3, 1, 14 081, 1,
3, 1, 5, 1, 8237, 1, 2, 4, 5, 1, 293, 1,
2, 1, 13, 1, 53, 1, 3, 1, 11, 1, 29, 1,
2, 2, 3, 4, 5, 2, 43, 1, 29, 1, 103, 1
The previous result is formatted as a list. Next, we display a table where we can see each
number and its corresponding prime factors.
GridTransposenumbers, CenterDot Superscript & list,
Frame All
In this case, the number of kernels that will be launched automatically is 4, the same as the
number of computer cores. Notice that the kernels are set by default to low priority, giving
preference to other computer processes.
To launch the available kernels we click on the Parallel Kernel Status... button. We can
also do the same by going the menu bar and choosing: Evaluation Parallel Kernel
Status. In both cases, a window similar to the one in Figure 12.2 will appear:
An alternative way to launch the kernels directly is to write down the following command
(for this to work, the kernels should not have been launched previously using any of the
methods described above):
LaunchKernels
If you launch more cores than the ones available in the computer, you’ll get a $Failed
message for each core not found (this can happen, for example, if you have configured your
machine for WLGM, to which we will refer later, but the application has not been activated
yet).
We can always find out our available kernels using the following command:
Kernels
System`
Parallel Parallel Parallel Parallel
Parallel Paralleli Parallel Parallel
Comb epipe ogra Subm
Array zation Map Table
ine d m it
Parallel Parallel Parallel
Parallel Paralleli Parallel Parallel Parallel
AxisP Evalu Produ
Do ze Needs Sum Try
lot ate ct
To factorize the numbers previously defined, we put the command we used earlier inside
Parallelize (Instead of Parallelize[Map[f,expr]] we can also use the equivalent
expression ParallelMap [f,expr]).
ParallelizeMapFactorInteger, numbers
5, 1, 7, 1, 97, 1, 101, 1, 2, 1, 3, 1, 7, 1, 10 091, 1,
2, 1, 773, 1, 2, 2, 3, 1, 14 081, 1,
3, 1, 5, 1, 8237, 1, 2, 4, 5, 1, 293, 1,
2, 1, 13, 1, 53, 1, 3, 1, 11, 1, 29, 1,
2, 2, 3, 4, 5, 2, 43, 1, 29, 1, 103, 1
In this case, we have used all the available kernels to simultaneously factorize the
numbers.
We can try to use this command with other functions, although as mentioned
before, the execution may not be faster. That will depend on many factors, such as
the type of calculation, the number of kernels available, etc. If the computation
time is short (a few seconds or less) we will not notice any difference or the
parallel calculation may even take longer.
In the example below, we show the computation time with and without parallelization.
When measuring computation times for comparison purposes, it’s recommended to start a
new session (you can use Quit [] to exit the current one) since, as we have seen in previous
chapters, Mathematica will automatically reuse previous calculations reducing the
execution time significantly. We use AbsoluteTiming instead of Timing because
AbsoluteTiming takes into account the entire calculation time while Timing only
considers the CPU time. The “;” tells Mathematica not to print the output of the command
since we’re only interested in its computation time.
10 000
i ; AbsoluteTiming
i1
0.928053, Null
10 000
Parallelize i ; AbsoluteTiming
i1
0.415421, Null
x2
Logx
2
10, 7, 0, 6, 6, 6
Once a function has been parallelized, it will remain so even after closing the kernels. In
this example, we close the kernels and open them again.
6, 2, 6, 4, 9, 2
An alternative way to execute the previous command is by using With to give values to the
local variables:
WaitAllTableWithi i, ParallelSubmit2 ^ i, i, 6
KernelObject
!" #
True
12.2.3 ParallelTry
LaunchKernels;
With ParallelTry we can evaluate a command in different kernels and get the
result from the kernel that executes it first. This can be useful, for example, when
comparing different computation methods.
Here we compare different methods for evaluating the minimum of a function. The output
shows the fastest one:
Sin50 x
1
, NMinimize Sin10 x y x2 y2 , y 0 && x 0,
4
x, y, Method &, "DifferentialEvolution",
12.2.4 ParallelCombine
ParallelCombine distributes part of a computation among all the available
kernels combining the partial results. It’s normally used with list elements. Its
syntax is: ParallelCombine[f, {e1 , ..., en }] that applies f to the elements of a list
and groups the partial results.
The function below calculates the first 9 primes. It’s the same as Prime[ {1, ..., 9}]
except that the calculation in this case is done in parallel:
ParallelCombinePrime, 1, 2, 3, 4, 5, 6, 7, 8, 9
12.2.5 ParallelEvaluate
The function ParallelEvaluate evaluates an expression in different kernels
and returns the result from each one of them. By default it uses all the available
kernels but we can specify which ones to use. This function is commonly
employed in calculations involving Monte Carlo simulation. The next example
shows how to calculate through simulation (an illustrative but quite inefficient
method).
First, we generate m pairs of real numbers {x, y} between 0 and 1. Then we find out which
ones fall inside the circle with radius r 1 with Norm[x y] < 1 equivalent to x2 y2 < 1.
Finally, we multiply by 4 since we’re only generating positive {x, y}, simulating just 1/4 of
the circle:
Withn 10 ^ 5, ParallelEvaluate
4. CountRandomReal0, 1, n, 2, xy ; Normxy 1 n
3.13396, 3.14492, 3.13684, 3.13684, 3.14632, 3.14472
The result is an estimate from each kernel. We calculate the mean to get a final value:
Mean
3.1406
12.2.6 ParallelMap
ParallelMap is another commonly used function that, as previously mentioned,
is equivalent to Map for sequential computations.
We are going to solve again the previous example of approximating . We generate pairs of
points {x, y} with values between -1/2 and 1/2. Next we select those points inside a circle
with radius 1/2 centered at (0,0) using again Norm[r] < r. We then proceed to calculate
using the output given by the command below:
Solvepi r ^ 2 1 interiorPoints total, pi
interiorPoints
pi
r2 total
r 1 2;
r
1.0
0.5
2 4 6 8 10
0.5
1.0
If we only give the function one initial guess, we will usually get a solution close to it:
FindRootCost ^ 2 Expt .3, t, 3
t 2.26454
We are going to find the solution with and without parallelization. We just want to
compare the computation times so we’ll not show the results.
Using multiple initial guesses, we can find all the equation roots for a given domain. We
have used Map since what we’re really doing is solving the equation for different starting
points:
initval Tablex, x, 0, 4 Pi, .1;
vals MapFindRootCost ^ 2 Expt .3, t, &, initval;
AbsoluteTiming
0.0277301, Null
The command below solves the problem replacing Map with ParallelMap.
pvals ParallelMapFindRootCost ^ 2 Expt .3, t, &,
initval; AbsoluteTiming
0.0536711, Null
In this case, the difference is negligible since, as mentioned before, there are many
factors that may influence the computation time.
We visually represent the solutions:
ShowPlotCost ^ 2, Expt .3, t, 0, 12, Graphics
Red, PointMapThreadt . 1, 2 &, pvals, Cost ^ 2 . pvals
1.0
0.5
2 4 6 8 10 12
0.5
1.0
z0 C
zn1 zn2 C
With C = 1, the series diverges (in the example we display only the first 6 terms). It tends to
infinity in absolute terms, so it doesn’t belong to it.
FixedPointList ^ 2 1 &, 0, 5
However if C 1, the series is bounded and therefore belongs to the set.
FixedPointList ^ 2 1 &, 0, 5
0, 1, 0, 1, 0, 1
We define a function that implements the previous approach for a given complex number.
In this case we use a maximum of 20 iterations:
mf1z :
LengthFixedPointList ^ 2 z &, z, 20, SameTest Abs 2 &
We apply it to find out what points in the complex plane x y belong to it. We add
AbsoluteTiming to compare the computation times using different methods.
pts1 Tablemf1x I y, x, 2, 1, 0.002, y, 1, 1, 0.002;
AbsoluteTiming
22.5427, Null
12.3.3 Faster
In this section, we show several techniques, apart from parallelization, to reduce
computation time.
These techniques require the presence of a C compiler in the computer.
(CCompilerDriver/tutorial/Overview). For 32-bit systems, an option is to install
Visual C++ Express, a Microsoft application that can be downloaded for free from:
http://www.microsoft.com/visualstudio
In case of 64-bit systems, one option is to install the 64-bit version of MinGW . In
our case, we have downloaded mingw-w64-bin_i686-mingw_20111220 and
copied it to the folder C:\mingw.
Mingw-w64: http://mingw-w64.sourceforge.net
To run the C compiler we need to load the following package first:
Needs"CCompilerDriver`"
The command below will display all the C compilers available in your computer:
CCompilers
If the function above returns an empty list it may be that either you don’t have any compiler
installed or, as in our case, you have installed MinGW in folder: C:/MinGW. To let
Mathematica know that you have the open-source software in your computer, execute the
following commands:
Needs"CCompilerDriver`GenericCCompiler`"
Now we can launch the available kernels as explained before. To use kernels over
a grid we need to make sure that we have access to them through WLGM.
Here we repeat the calculation from the previous section but in parallel:
mf2z :
LengthFixedPointList ^ 2 z &, z, 20, SameTest Abs 2 &
DistributeDefinitionsmf2;
We can still significantly reduce the computation time with the command Compile (not
limited to parallel calculations). We use it to indicate that we’re going to evaluate a function
only for numeric inputs. This way the program saves time by not performing the checks
associated to symbolic calculations and can apply its own routines optimized for numerical
calculations.
mf3 Compilec, Complex,
LengthFixedPointList2 c &, c, 50, SameTest Abs2 2.0 & ;
We can perform the computation even faster by taking advantage of some of the options of
Compile . Using this function we can convert the Mathematica-defined function into C
code that is compiled and dynamically linked:
cfun Compile x, x Sinx ^ 2,
CompilationTarget "C", RuntimeAttributes Listable;
cfun3, 4
1.23636, 1.15161
pts4 mf4Tablex y, x, 2, 1, 0.002, y, 1, 1, 0.002;
AbsoluteTiming
1.46059, Null
We display the results from the three functions using different color templates and compare
them with the built-in function MandelbrotSetPlot :
If you are a programmer you can even generate C code that can be converted into
an executable file (CCodeGenerator/tutorial/Overview) or you can call external
programs (guide/CallingExternalPrograms).
To generate C code we need to load the CCodeGenerator package. In the example below,
we load it after clearing all the variables from memory:
Clear"Global` "
We use Panel in the following example to show the output of the previous instruction
inside a frame displaying the generated code along with all the required headings and
libraries necessary to compile an executable file.
z include "math.h"
include "WolframRTL.h"
DLLEXPORT int Initialize m 4a3e4639 9687 41a0 8159 98ba6a7d8dc2 WolframLibraryData libData
if initialize
funStructCompile libData compileLibraryFunctions;
initialize 0;
return 0;
DLLEXPORT void Uninitialize m 4a3e4639 9687 41a0 8159 98ba6a7d8dc2 WolframLibraryData libData
if initialize
initialize 1;
DLLEXPORT int
m 4a3e4639 9687 41a0 8159 98ba6a7d8dc2 WolframLibraryData libData, mreal A1, mreal Res
mreal R0 0;
mreal R0 1;
mreal R0 2;
R0 0 A1;
R0 1 R0 0 R0 0;
R0 2 sin R0 1;
Res R0 2;
funStructCompile WolframLibraryData cleanUp libData, 1;
return 0;
3
Let’s apply this metric to compare the genomes of three organisms using the files
located in the Data directory that, along with other examples in this book, can be
downloaded from http://diarium.usal.es/guillermo.
We first set the working directory to the one where the files are located (Data). This way we
can use Import directly without the need to state the entire path:
SetDirectoryFileNameJoinNotebookDirectory, "Data"
G:\Mi unidad\Mathematica\MBN13\Data
ATGAATTCGGATAACTTTCAATCCTATTTAGGTTATTATTTAACCTGGATTTCCTTGGCCATATTTGC
CGAGATACTATACTTTCAATCCTATTTAGGTTATTATTTAACATATATCGGATTTCAAACCCT
TATAGATAGACTAGACTTTCAATCCTATTTAGGTTATTATTTAACGCCAGAGGCATTTCTCCG
TGCGTGGTCCTTTAATGACTTTCAATCCTATTTAGGTTATTATTTAACACAACAGGCAAAAAC
AGAATTAAGACTGGCCATTGAGCTTTCAATCCTATTTAGGTTATTATTTAACCCGTTTCTAAT
AAACAATATAATAAATGATTTTTAA, 1596 ,
ATGTTCACATCAATAGATCATAATAATAGCG CAATCTTATAGGTGCCTTAAGATGGATATAA
These three organisms are very simple and their genome size (total number of genes) is,
respectively:
Length picrogenes, acidogenes, volcaniumgenes
The command below shows the data for one gene that will be used as an input for the first
kernel:
ParallelEvaluatepicro1, Kernels1
ATGAATTCGGATAACTTTCAATCCTATTTAGGTTATTATTTAACCTGGATTTCCTTGGCCATATTTGCCGAGA
TACTATACTTTCAATCCTATTTAGGTTATTATTTAACATATATCGGATTTCAAACCCTTATAGATAGACT
AGACTTTCAATCCTATTTAGGTTATTATTTAACGCCAGAGGCATTTCTCCGTGCGTGGTCCTTTAATGAC
TTTCAATCCTATTTAGGTTATTATTTAACACAACAGGCAAAAACAGAATTAAGACTGGCCATTGAGCTTT
CAATCCTATTTAGGTTATTATTTAACCCGTTTCTAATAAACAATATAATAAATGATTTTTAA
For the next example, we want to compare the first eight genes of thermoplasma
acidophilum with those of thermoplasma volcanium. However, before doing it,
let’s discuss a simpler case first. In the example below we compare the initial 4
bases of the first 2 genes of volcaniumgenes, with their respective counterparts in
thermoplasma acidophilum.
We start by extracting the relevant bases and genes from both organisms:
vo1, vo2, aci1, aci2
StringTakevolcaniumgenes1 , 4, StringTakevolcaniumgenes2, 4,
StringTakeacidogenes1, 4, StringTakeacidogenes2, 4
ATGA, ATGA, ATGT, ATGG
Notice how the first two bases of thermoplasma volcanium are actually the same.
To make the comparison we’ll use a combination of the EditDistance and Min
functions. However, in the command below we use f1 and f2 first to gain a better
understanding of how the actual instruction works. We show the logic in four steps:
f2Mapf1x, & , aci1, aci2
Finally we apply the previous pattern using ParallelMap to compare the first eight genes
of thermoplasma acidophilum with those of thermoplasma volcanium:
acidovolcanium
ParallelMapFunctionx, MinMapEditDistancex, & , acido,
volcaniumgenes1 ;; 8; AbsoluteTiming
20.9208, Null
Depending on the computer being used, the calculation time may take up to several minutes.
We proceed the same way to compare the genes between picrophilus torridus and
thermoplasma volcanium:
picrovolcanium
ParallelMapFunctionx, MinMapEditDistancex, & , picro,
volcaniumgenes1 ;; 8; AbsoluteTiming
22.301, Null
The graph below shows the genetic proximity of the first eight genes in both cases:
thermoplasma acidophilum vs. thermoplasma volcanium and picrophilus torridus vs.
thermoplasma volcanium:
ListLinePlotacidovolcanium, picrovolcanium,
PlotLegends "acido vs. volcanium", "picro vs. volcanium"
1000
800
600
400
200
2 4 6 8
307.75, 407.
307.75
407.
Ones the Wolfram Workbench plugin is installed, to verify it, choose Help About
Eclipse and identify the Wolfram Workbench logo toward the bottom of the window.
After clicking on Next, we can assign a name to the application (in this example: Project1).
12. Faster, Further 433
Click on Finish. You’ll see in the Package Explorer tab the projects that you have. Select
Project1 and press on PacletInfo.m. Figure 12.6 will appear with basic details about your
project: Author, project version, Mathematica version, and a brief description.
Figure 12.7 Filling in the application details for the new project.
True
If CUDAQ returns False , then CUDALink will not work. For more information, read
"CUDALink Setup".
For CUDALink to work, you need to have an up-to-date driver. You can check this for your
system by running CUDADriverVersion .
CUDAInformation
Now we can use the CUDA functions. For further information, please see the
tutorial: CUDALink/tutorial/Overview.
8
https://reference.wolfram.com/language/ref/APIFunction.html
! _ Annuity
See Factorial See Blank 382
# __ Apart
See Slot See BlankSequence 341
## ___ Append
See SlotSequence See BlankNullSequence 125
$InstallationDirectory ` Apply(@, @@, @@@)
89 177, 122 61, 62
% (Out[n]) <= or ArrayPlot
42 See Condition 424, 426
& = Arrow
28 See Set 77, 147
&& (or ) == Association
See Condition See Condition 50, 51
* === AssociationMap
See Times See SameQ 51
> AssociationThread
See Dot See Condition 51
/ -> AtomQ
See Replace See Rule 42
// >= Or Attributes
15, 20 See Condition 63
/; AutocorrelationTest
See Condition See Ceiling 204
/@ AbsoluteTiming BarChart
See Map 59 165
:= Accumulate BarcodeRecognize
See SetDelayed 64 108
:-> or :> AirTemperatureData BetaDistribution
See RuleDelayed 129, 130, 167 180
@ Anatomic Data Binarize
See Apply 136 104
@@ AnatomyPlot3D BinCounts
See Apply 137 163
@@@ And (&& o ") Binomial
See Apply See Condition 75
[[…]] AngularGauge Biokmod
See Part 36 360
438 Mathematica beyond mathematics
Pi Position Radiation
231-248 130, 142, 147 328
Piecewise Positive RadioButton
76 74, 232 392
PieChart Power RadioButtonBar
200 43, 44, 45 92, 96, 97
PieChart3D Precision Radon
378 79 114
Placed PrecisionGoal Radon (gas)
144, 170, 184 79, 82 309
PlanckRadiationLaw Predict Random
280 208, 219, 221 23
PlanetData PredictorMeasurements RandomChoice
256, 271, 273 224 69, 125, 167
Plot Prepend RandomFunction
11, 14, 21 270, 310,317 98
Plot3D Prime RandomGeoPosition
11, 27, 31 72, 73, 252 131
PlotLabel PrimePi RandomGraph
36, 82, 125 253, 254 112
PlotLegends Print RandomInteger
128, 144, 150 74, 83 23, 25, 52
PlotPoints Probability RandomReal
249, 368 98, 169, 170 49, 54, 55
PlotRange Probability density function RandomSample
27, 93, 100 See PDF 220, 223
PlotStyle PropertyValue RandomVariate
92, 172, 175 111 171, 172, 182
PlotTheme ProteinData RandomWalk
166 140 69
Plus (+) Pruning RandomWalkProcess
42, 45, 59 108 98
Point Pure function Range
31, 92, 100 28, 64 5, 59, 62
PointFigureChart Quantile Rational
377 163, 191 42, 45, 46
PointSize QuantilePlot Rationalize
31, 100, 147 381 81
PoissonDistribution Quantity Rationals
170 2, 143, 257 42
Polygon QuantityMagnitude ReactionBalance
232, 236 9, 124, 133 135
PolyhedronData QuantityVariable Real
113 116, 281 42, 46, 78
PopupMenu Quiet RealDigits
97 296, 298, 366 239, 242, 243
444 Mathematica beyond mathematics
Variance
170
VerticalGauge
36
ViewPoint
251, 290
WaitAll
419, 430
WeatherData
131, 175, 176
WeatherForecastData
131
WhenEvent
329, 365
Which
73, 74, 355
WikipediaData
38, 151, 169
WindVectorData
131
With
71, 72, 107
Wolfram Cloud
18, 86
Wolfram Data Framework
117
Wolfram Data Repository
52, 127, 165
WolframAlpha
9, 119, 114
WordData
127
WorkingPrecision
84, 180
Zeta
247-253
ZetaZero
250
ZoomFactor
100