Notes to the Core Language of Mathematica I (Introduction & Basic Grammar)

This is a note to the course "Core Language of Mathematica" given by Siqi Liu in Bilibili. There is also a PDF version in Chinese.

Introduction

Why Mathematica?

Firstly, we talk about why we use Mathmatica. The question is quite open and people in different areas will give different answers. So we begin with the way of a scientific worker workes:

  1. Find a question which seems solvable;
  2. Try to use some examples to understand the question;
  3. Come up with a scheme which seems to be able to solve the question;
  4. If the scheme succeeds, then the problem is solved;
  5. If the scheme fails, return to 2;
  6. If all the schemes fail, return to 1.

So, scientific workers would use plenty of simulation and experiments. In order to enhance the efficiency of work, we get the machine to share some of our work. Specifically, for people who need computation, we should get the computer to help us. So we need programming.

And then we need to choose the most efficient way of programming. The efficiency of programming is decided by the time we spend writing and using it. In most cases, the time of running a program for once is far shorter than the time we need to write it. And usually, we scientists would run the program only once. As a result, the most efficient way is just the fastest way to write a program.

The language of Mathematica is close to natural language and it has many built-in functions and well developed documents, which makes it very suitable and easy to write a program, thus shortening the time we need to develop a program.

The History of Mathematica

We could learn about the history of Mathematica in the web site Mathematica—Three Decades of Contributions, Invention, Discovery, and Education (wolfram.com), and as Mathematica is developed by C, it is similar to C in some respects. When Wolfram developed the Mathematica, he got many inspirations from Lisp which lead to the similarity between them.

The Mathematica function as calculator

The calculation in Mathematica will always be performed in high precision and it could function as a calculator itself. Like:

1
2
3
4
In: 1+1
Out: 2
In: Sin[Pi/2]
Out: 1

But its capacity is far more than the arithmetic operation and call simple functions. We could use Expand to perform expansion, Factor to perform factorization, Solve to solve equations. Some functions of advanced mathematics are also equipped, like Dsolve to solve differential equations, D to perform derivation, Limit to get the limit of a function, Sum to calculate the series.

When you are performing a series of computations, if you worry about the contamination of variables, you could reopen the kernel.

Apart from the simple functions we have noted above, Mathematica has a lot of advanced built-in functions. For example, we could use Import to get data from the internet. And we could also deal with pictures and music with Mathematica, capture stock data from the Internet to achieve quantitative trading.

Notations

In Mathematica both the functions and constants use the Upper Camel Case. As a result, to avoid using the reserved words, we could use the Lower Camel Case. The comments are written in the form of (* comments *). If an element has got an value, it would be black and if not it would be blue.

Some Other Information about This notes

The notes is from the course "Core Language of Mathematica" taught by Siqi Liu, Tsinghua University. And as the document of Mathematica is so strong, we don't talk about the details of many functions unless we need to do that. We mention the usage of the function and readers could just read the details of the function in the document of Mathematica.

We highly recommend our readers to use genuine software. The price is not so unaffordable. If you think it is quiet costive, there are still a way to use the Mathematica legally. In May 2019, Wolfram put forward Wolfram Engine, and it is free of charge. And there many ways to add a notebook to the kernel. I recommend this passage 在 VS Code 中使用 Wolfram 笔记本 - 知乎 (zhihu.com).

The Principle of Mathematica

In Mathematica, there are two fundamental principles. Firstly, everything is a expression. Secondly, computation is just rewriting.

In Mathematica, numbers, strings and symbols are called atom. An atom is a expression and if are expressions, is a expression.

And when we perform computation in Mathematica, what we do is just get a repeatable regime in what we need to compute and interpret it to Mathematica. Thus, rather than variables, what we actually care about is the functions. i.e. Mathematica's zeroth principle: functions matter, not variables.

Basic Grammar

In this section, we will talk about the basic grammar of writing a Mathematica programm.

Type Checking

The type checking for Mathematica is not strict, for example:

1
2
In: Sin["I'm a String!"]
Out: Sin[I'm a String!]

The less stricter the type checking, the more flexible the programming. Of course, it may incur errors. But usually a Mathematica program is short, so developers could check the error by themselves.

We will see some skills base on the loose type checking of Mathematica in later sections.

Condition

The If of Mathematica take the form of If[conditional judgement,True's opertaions, Else's operations, Unevaluated' operations].

For example:

1
2
If[a==b,Print["Then"],Print["Else"],Print["Unevaluated"]]
If[a === b, Print["Then"], Print["Else"], Print["Unevaluated"]]

In Mathematica, == means equal while === means sames.

Circle

Subroutine