# 在新加坡的这五年—学术篇

### 基础书籍：

(1) Complex Analysis, 3rd Edition, Lars V. Ahlfors

(2) Complex Analysis, Elias M. Stein

(1) Lectures on Riemann Surfaces (GTM 81), Otto Forster

(2) Lectures on Quasiconformal Mappings, Lars V. Ahlfors

(1) Real Analysis and Complex Analysis, Rudin

(2) Real Analysis, Elias M. Stein

### 专业书籍：

(1) One Dimensional Dynamics, Welington de Melo & Sebastian VanStrien

(2) Mathematical Tools for One-Dimensional Dynamics (Cambridge Studies in

Advanced Mathematics), Edson de Faria / Welington de Melo

(3) Dynamics in One Complex Variable, John Milnor；Milnor 的教材总是写的清晰明确，容易上手，推荐初学者可以读这本书。

(4) Complex Dynamics, Lennart Carleson；Carleson 的教材偏向于分析学，读起来其实也有点难度，还是读 Milnor 的教材相对容易。

(5) Complex Dynamics and Renormalization, Curtis T. McMullen；McMullen 的书适合当做查阅，也不太适合从头到尾读下去。

(6) Renormalization and 3-Manifolds Which Fiber over the Circle, Curtis T. McMullen

(7) Iteration of rational functions (GTM 132), Alan F. Beardon

(8) An Introduction to Ergodic Theory (GTM 79), Walters Peter

### 论文

Question. 是否存在 $\ell\geq 4$ 的偶数和复数 $c\in\mathbb{C}$ 使得 $f(z)=z^{\ell}+c$ 的 Julia 集合 $J(f)$ 是正测度？

1. Combinatorics, geometry and attractors of quasi-quadratic maps，Pages 345-404 from Volume 140 (1994), Issue 2 by Mikhail Lyubich

2. Wild Cantor attractors exist，Pages 97-130 from Volume 143 (1996), Issue 1 by Hendrik Bruin, Gerhard Keller, Tomasz Nowicki, Sebastian van Strien

3. Quadratic Julia sets with positive area，Pages 673-746 from Volume 176 (2012), Issue 2 by Xavier Buff, Arnaud Chéritat

4. Polynomial maps with a Julia set of positive measure，Nowicki, Tomasz, and Sebastian van Strien，arXiv preprint math/9402215(1994).

### 参考资料：

1. 科研这条路
2. 维基百科：Julia 集合

# 一个数学家的辩白

《一个数学家的辩白》是数学大师 Godfrey Harold Hardy 在 1940 年左右的作品，可以称之为 Hardy 本人的自传和内心独白。Hardy 通过自己多年从事数学科研的经验，对数学，文学，哲学，美学等诸多学科的理解，将其写成了一本小册子。给其余的数学工作者，数学爱好者，以及不了解数学的人一个了解数学家内心的机会。

G.H.Hardy（1877 年 2 月 7 日 – 1947 年 12 月 1 日）是一代数论大师，英国数学家，先后在牛津大学（Oxford）和剑桥大学（Cambridge）担任数学教授，与另一位英国数学家 Littlewood 共同研究数学，其研究领域包括解析数论，三角级数，不等式等诸多方向。对数论领域和分析学领域的贡献巨大，是二十世纪英国分析学派的代表人物之一。

  在这平坦的沙滩上，
海洋与大地间，
我该建起或写些什么，
来阻止夜幕的降临?
告诉我神秘的字符，
去喝退那汹涌的波涛，
告诉我时间的城堡，
去规划那更久的白昼。

 倾江海之水，洗不净帝王身上的膏香御气。

1. 《一个数学家的辩白》；
2. 《黎曼猜想漫谈》。

# 一代数学大师 Rota 的经验与忠告

### Ten Lessons I wish I Had Been Taught

#### 讲座（Lecturing）

（a）每次讲座都应该只有一个重点。（Every lecture should make only one main point.）

Every lecture should state one main point and repeat it over and over, like a theme with variations. An audience is like a herd of cows, moving slowly in the direction they are being driven towards. If we make one point, we have a good chance that the audience will take the right direction; if we make several points, then the cows will scatter all over the field. The audience will lose interest and everyone will go back to the thoughts they interrupted in order to come to our lecture.

（b）不要超时。（Never run overtime.）

Running overtime is the one unforgivable error a lecturer can make. After fifty minutes (one micro-century as von Neumann used to say) everybody’s attention will turn elsewhere even if we are trying to prove the Riemann hypothesis. One minute overtime can destroy the best of lectures.

As you enter the lecture hall, try to spot someone in the audience with whose work you have some familiarity. Quickly rearrange your presentation so as to manage to mention some of that person’s work. In this way, you will guarantee that at least one person will follow with rapt attention, and you will make a friend to boot.

Everyone in the audience has come to listen to your lecture with the secret hope of hearing their work mentioned.

（d）给听众一些值得回忆的东西。（Give them something to take home.）

Most of the time they admit that they have forgotten the subject of the course and all the mathematics I thought I had taught them. However, they will gladly recall some joke, some anecdote, some quirk, some side remark, or some mistake I made.

#### 板书技巧（Blackboard Technique）

（a）开讲前保持黑板干净（Make sure the blackboard is spotless.）

By starting with a spotless blackboard you will subtly convey the impression that the lecture they are about to hear is equally spotless.

（b）从黑板的左上角开始书写（Start writing on the top left-hand corner.

What we write on the blackboard should correspond to what we want an attentive listener to take down in his notebook. It is preferable to write slowly and in a large handwriting, with no abbreviations.

When slides are used instead of the blackboard, the speaker should spend some time explaining each slide, preferably by adding sentences that are inessential, repetitive, or superfluous, so as to allow any member of the audience time to copy our slide. We all fall prey to the illusion that a listener will find the time to read the copy of the slides we hand them after the lecture. This is wishful thinking.

#### 多次公布同样的结果（Publish the Same Result Several Times）

The mathematical community is split into small groups, each one with its own customs, notation, and terminology. It may soon be indispensable to present the same result in several versions, each one accessible to a specific group; the price one might have to pay otherwise is to have our work rediscovered by someone who uses a different language and notation and who will rightly claim it as his own.

#### 说明性的工作反而更有可能被记得（You Are More Likely to Be Remembered by Your Expository Work）

When we think of Hilbert, we think of a few of his great theorems, like his basis theorem. But Hilbert’s name is more often remembered for his work in number theory, his Zahlbericht, his book Foundations of Geometry, and for his text on integral equations.

#### 每个数学家只有少数的招数（Every Mathematician Has Only a Few Tricks）

You admire Erdös’s contributions to mathematics as much as I do, and I felt annoyed when the older mathematician flatly and definitively stated that all of Erdös’s work could be “reduced” to a few tricks which Erdös repeatedly relied on in his proofs. What the number theorist did not realize is that other mathematicians, even the very best, also rely on a few tricks which they use over and over. But on reading the proofs of Hilbert’s striking and deep theorems in invariant theory, it was surprising to verify that Hilbert’s proofs relied on the same few tricks. Even Hilbert had only a few tricks!

There are two kinds of mistakes. There are fatal mistakes that destroy a theory, but there are also contingent ones, which are useful in testing the stability of a theory.

#### 使用费曼的方法（Use the Feynman Method）

You have to keep a dozen of your favorite problems constantly present in your mind, although by and large they will lay in a dormant state. Every time you hear or read a new trick or a new result, test it against each of your twelve problems to see whether it helps. Every once in a while there will be a hit, and people will say, “How did he do it? He must be a genius!”

#### 不要吝啬你的赞美（Give Lavish Acknowledgments）

I have always felt miffed after reading a paper in which I felt I was not being given proper credit, and it is safe to conjecture that the same happens to everyone else.

#### 写好摘要（Write Informative Introductions）

If we wish our paper to be read, we had better provide our prospective readers with strong motivation to do so. A lengthy introduction, summarizing the history of the subject, giving everybody his due, and perhaps enticingly outlining the content of the paper in a discursive manner, will go some of the way towards getting us a couple of readers.

#### 为老年做好心理准备（Be Prepared for Old Age）

You must realize that after reaching a certain age you are no longer viewed as a person. You become an institution, and you are treated the way institutions are treated. You are expected to behave like a piece of period furniture, an architectural landmark, or an incunabulum.

### Ten Lessons for the Survival of a Mathematics Department

#### 不要在其他系讲自己系同事的坏话（Never wash your dirty linen in public）

Departments of a university are like sovereign states: there is no such thing as charity towards one another.

Your letter will be viewed as evidence of disunity in the rank and file of mathematicians. Human nature being what it is, such a dean or provost is likely to remember an unsolicited letter at budget time, and not very kindly at that.

#### 不要进行领域评价（Never Compare Fields）

You are not alone in believing that your own field is better and more promising than those of your colleagues. We all believe the same about our own fields. But our beliefs cancel each other out. Better keep your mouth shut rather than make yourself obnoxious. And remember, when talking to outsiders, have nothing but praise for your colleagues in all fields, even for those in combinatorics. All public shows of disunity are ultimately harmful to the well-being of mathematics.

#### 别看不起别人使用的数学（Remember that the grocery bill is a piece of mathematics too）

The grocery bill, a computer program, and class field theory are three instances of mathematics. Your opinion that some instances may be better than others is most effectively verbalized when you are asked to vote on a tenure decision. At other times, a careless statement of relative values is more likely to turn potential friends of mathematics into enemies of our field. Believe me, we are going to need all the friends we can get.

#### 善待擅长教学的老师（Do not look down on good teachers）

Mathematics is the greatest undertaking of mankind. All mathematicians know this. Yet many people do not share this view. Consequently, mathematics is not as self-supporting a profession in our society as the exercise of poetry was in medieval Ireland. Most of our income will have to come from teaching, and the more students we teach, the more of our friends we can appoint to our department. Those few colleagues who are successful at teaching undergraduate courses should earn our thanks as well as our respect. It is counterproductive to turn up our noses at those who bring home the dough.

#### 学会推销自己的数学成果（Write expository papers）

When I was in graduate school, one of my teachers told me, “When you write a research paper, you are afraid that your result might already be known; but when you write an expository paper, you discover that nothing is known.”

It is not enough for you (or anyone) to have a good product to sell; you must package it right and advertise it properly. Otherwise you will go out of business.

When an engineer knocks at your door with a mathematical question, you should not try to get rid of him or her as quickly as possible.

#### 不要把提问者拒之门外（Do not show your questioners to the door）

What the engineer wants is to be treated with respect and consideration, like the human being he is, and most of all to be listened to with rapt attention. If you do this, he will be likely to hit upon a clever new idea as he explains the problem to you, and you will get some of the credit.

Listening to engineers and other scientists is our duty. You may even learn some interesting new mathematics while doing so.

#### 联合阵线（View the mathematical community as a United Front）

Grade school teachers, high school teachers, administrators and lobbyists are as much mathematicians as you or Hilbert. It is not up to us to make invidious distinctions. They contribute to the well-being of mathematics as much as or more than you or other mathematicians. They are right in feeling left out by snobbish research mathematicians who do not know on which side their bread is buttered. It is our best interest, as well as the interest of justice, to treat all who deal with mathematics in whatever way as equals. By being united we will increase the probability of our survival.

#### 把科学从不可靠中拯救出来（Attack Flakiness）

Flakiness is nowadays creeping into the sciences like a virus through a computer, and it may be the present threat to our civilization. Mathematics can save the world from the invasion of the flakes by unmasking them and by contributing some hard thinking. You and I know that mathematics is not and will never be flaky, by definition.

This is the biggest chance we have had in a long while to make a lasting contribution to the well-being of Science. Let us not botch it as we did with the few other chances we have had in the past.

#### 善待所有人（Learn when to withdraw）

Let me confess to you something I have told very few others (after all, this message will not get around much): I have written some of the papers I like the most while hiding in a closet. When the going gets rough, we have recourse to a way of salvation that is not available to ordinary mortals: we have that Mighty Fortress that is our Mathematics. This is what makes us mathematicians into very special people. The danger is envy from the rest of the world.

When you meet someone who does not know how to differentiate and integrate, be kind, gentle, understanding. Remember, there are lots of people like that out there, and if we are not careful, they will do away with us, as has happened many times before in history to other Very Special People.

### 参考资料：

1. Rota, Gian-Carlo. “Ten lessons I wish I had been taught.” Indiscrete thoughts. Birkhäuser, Boston, MA, 1997. 195-203.
2. Rota, Gian-Carlo. “Ten Lessons for the Survival of a Mathematics Department.” Indiscrete Thoughts. Birkhäuser, Boston, MA, 1997. 204-208.

# 素数之美

### 哥德巴赫猜想（Goldbach’s Conjecture）

1. [Theorem] 每一个大于 7 的奇数都可以写成三个素数之和；
2. [Conjecture] 每一个大于 6 的偶数都可以写成两个素数之和。

[Theorem (Vinogradov)] 假设 $N$ 是一个奇数，令 $r(N) = \sum_{p_{1}+p_{2}+p_{3}=N}1$ 表示关于 $N$ 的计数函数，其中 $p_{1}, p_{2}, p_{3}$ 都是素数。则存在一个一致有界的函数 $\Omega(N) \in (c_{1},c_{2})$$c_{2}>c_{1}>0$）对于充分大的奇数 $N$，有以下式子成立

$r(N) = \Omega(N)\cdot \frac{N^{2}}{(\ln(N))^{3}}\cdot\bigg\{1+O\bigg(\frac{\ln\ln(N)}{\ln(N)}\bigg)\bigg\}.$

[Theorem (Chen)] 假设 $N$ 是一个偶数，令 $r(N)=\sum_{p+n=N}1$ 表示关于 $N$ 的计数函数，其中 $p$ 是素数，$n$ 表示最多为两个素数的乘积。则当 $n$ 充分大的时候，有以下式子成立：

$r(N) >> \Omega(N)\cdot \frac{2N}{(\ln(N))^{2}},$

1. 在哥德巴赫猜想的研究过程中，通常数学家把偶数可表示为 $a$ 个素数的乘积与 $b$ 个素数的乘积之和这个问题，简称为 $a + b$ 问题。所以，陈景润证明的 “1+2” 并不是指 1+2 = 3，而指的是对于每一个充分大的偶数，要么是两个素数之和，要么是一个素数加上两个素数之积。其实可以简单的理解为 $p_{1}+p_{2}$ 或者 $p_{1}+p_{2}\cdot p_{3}$，在这里 $p_{1},p_{2},p_{3}$ 都是素数。从以上公式可以看出，$\lim_{N\rightarrow \infty} r(N) = +\infty.$
2. 1920 年，挪威数学家 V.Brun 证明了 “9+9″，开启了数学家研究哥德巴赫猜想之路；1966 年，中国数学家陈景润证明了 “1+2″，把素数的筛法推向了顶峰。

### 孪生素数猜想（Twin Primes Conjecture）

1. 1940 年，Paul Erdos 证明 $\exists c>0$ 使得 $\liminf_{n\rightarrow\infty} \frac{p_{n+1}-p_{n}}{\ln(p_{n})}
2. 2005 年，Daniel Goldston，Janos Pintz 和 Cem Yildirim 证明 $\liminf_{n\rightarrow\infty}\frac{p_{n+1}-p_{n}}{\ln(p_{n})}=0.$
3. 2007 年，上述结果被改进为 $\liminf_{n\rightarrow\infty}\frac{p_{n+1}-p_{n}}{\sqrt{\ln(p_{n})}\cdot (\ln\ln(p_{n}))^{2}}=0.$
4. 2013 年，张益唐证明了 $\liminf_{n\rightarrow\infty}(p_{n+1}-p_{n}) < 7 * 10^{7}$，随后这个结果被改进到 246。

1. 1931 年，Erik Westzynthius 证明 $\limsup_{n\rightarrow\infty}\frac{p_{n+1}-p_{n}}{\ln(p_{n})} =\infty.$
2. 2014 年，Kevin Ford, Ben Green, Sergei Konyagin, Terence Tao 和 James Maynard 证明 $p_{n+1}-p_{n}>c\cdot \frac{\ln(n)\cdot \ln\ln(n) \cdot \ln\ln\ln\ln(n)}{\ln\ln\ln(n)}$ 对于某个 $c>0$ 和无穷个 $n$ 成立。

### 素数定理

[素数定理] 假设 $\pi(x)$ 表示不大于 $x$ 的所有素数的个数，那么 $\lim_{x\rightarrow \infty}\pi(x)/(x/\ln(x)) = 1.$

[孪生素数个数的上界] 假设 $\pi_{2}(x)$ 表示不大于 $x$ 的所有孪生素数个数，那么存在常数 $C>0$ 使得 $\pi_{2}(x)\leq C\cdot x/(\ln(x))^{2}.$

## 素数的性质

[Theorem (Euclid)] 素数有无穷多个。

$S(x) = \sum_{1\leq n\leq x} \frac{1}{n}.$

$\sum_{n=1}^{\infty}\frac{1}{n} = \prod_{p\text{ prime}}\bigg(1+\frac{1}{p}+\frac{1}{p^{2}}+\cdots\bigg)= \prod_{p\text{ prime}} \frac{1}{1-\frac{1}{p}},$

$\ln\bigg(\sum_{n=1}^{\infty}\frac{1}{n} \bigg)= \sum_{p\text{ prime}}\frac{1}{p} + O\bigg(\sum_{p\text{ prime}}\frac{1}{p^{2}}\bigg).$

1. $\sum_{p\text{ prime}, p\leq x} \frac{1}{p} = \ln\ln(x) + O(1);$
2. $\prod_{p\text{ prime}, p\leq x}\bigg(1-\frac{1}{p}\bigg)^{-1} = c\cdot\ln(x)+o(1),$ 这里，$c>0$ 是一个常数。

1. [Theorem] 所有正整数的倒数和是发散的；
2. [Theorem] 所有素数的倒数和是发散的。

[Theorem] 所有孪生素数的倒数和是收敛的。

$\pi_{2}(x) \leq C\cdot \frac{x}{(\ln(x))^{2}}$

$\frac{1}{p_{n}'} \leq C\frac{1}{n\cdot (\ln(n))^{2}}$

[Theorem] 对于充分大的 $x$ 而言，在 $[1,x]$ 内，素数之间的最小间隔 $\min_{p_{n}\leq x} (p_{n}-p_{n-1})\leq (1+o(1))\ln(x);$ 同时，素数之间的最大间隔 $\max_{p_{n}\leq x}(p_{n}-p_{n-1})\geq (1+o(1))\ln(x).$

### Eratosthenes 筛法（Eratosthenes Sieve Method)

Eratosthenes 筛法是数学家 Eratosthenes 提出的一种筛选素数的方法，其思路比较简单：想要筛选出 $[2,n]$ 中的所有素数，则首先把 $[2,n]$ 中的所有正整数按照从小到大的顺序 $2, \cdots, n$ 来排列，然后按照如下步骤执行：

1. 读取数列中当前最小的数 2，然后把 2 的倍数全部删除；
2. 读取数列中当前最小的数 3，然后把 3 的倍数全部删除；
3. 读取数列中当前最小的数 5，然后把 5 的倍数全部删除；（4 已经被第一步去掉了）
4. 读取数列中当前最小的数 7，然后把 7 的倍数全部删除；（6 已经被第一步去掉了）
5. 循环以上步骤直到 $[2,n]$ 中所有的数被读取或者被删除。

### Brun 筛法（Brun Sieve Method）

Question. 研究素数究竟有什么用？

### 参考文献：

1.  Small and Large Gaps Between Primes, Terence Tao, Latinos in the Mathematical Sciences Conference, 2015.
2.  Bounded Gaps Beween Primes, Yitang Zhang, 2013.
3.  Additive Number Theory, Melvyn B.Nathanson, GTM 164.
4.  http://mathworld.wolfram.com/TwinPrimes.html.

# 拉格朗日四平方和定理

### 拉格朗日四平方和定理

Every positive integer is the sum of four squares.

• $1=1^{2}+0^{2}+0^{2}+0^{2}$
• $2 = 1^{2}+1^{2}+0^{2}+0^{2}$
• $7 = 2^{2}+1^{2}+1^{2}+1^{2}$

$(x_{1}^{2}+x_{2}^{2}+x_{3}^{2}+x_{4}^{2})\cdot(y_{1}^{2}+y_{2}^{2}+y_{3}^{2}+y_{4}^{2}) = z_{1}^{2}+z_{2}^{2}+z_{3}^{2}+z_{4}^{2}$，其中

$\begin{cases} z_{1}=x_{1}y_{1}+x_{2}y_{2}+x_{3}y_{3}+x_{4}y_{4} \\ z_{2}=x_{1}y_{2}-x_{2}y_{1}-x_{3}y_{4}+x_{4}y_{3} \\ z_{3}=x_{1}y_{3}-x_{3}y_{1}+x_{2}y_{4}-x_{4}y_{2} \\ z_{4}=x_{1}y_{4}-x_{4}y_{1}-x_{2}y_{3}+x_{3}y_{2}\end{cases}$

Claim. $m=1$

proof of the claim. 反证法，假设 $1 成立。令 $y_{i}=x_{i}(\mod m)$ 对于 $i\in\{1,2,3,4\}$ 成立，并且 $-m/2。因此，$y_{1}^{2}+y_{2}^{2}+y_{3}^{2}+y_{4}^{2}\equiv(x_{1}^{2}+x_{2}^{2}+x_{3}^{2}+x_{4}^{2})\equiv mp \equiv 0(\mod m)$。令 $mr = y_{1}^{2}+y_{2}^{2}+y_{3}^{2}+y_{4}^{2}$。因此，$mr\leq 4(m/2)^{2}=m^{2}$

### 参考文献

1. GTM 164, Additive Number Theory, Melvyn B.Nathanson, 1996.

# 传染病的数学模型

• 易感者（susceptible）：用符号 $S(t)$ 来表示；
• 感染者（infective）：用符号 $I(t)$ 来表示；
• 康复者（Recoverd）：用符号 $R(t)$ 来表示；

• $r$ 表示在单位时间内感染者接触到的易感者人数；
• 传染率：$\beta$ 表示感染者接触到易感者之后，易感者得病的概率；
• 康复率：$\gamma$ 表示感染者康复的概率，有可能变成易感者（可再感染），也有可能变成康复者（不再感染）。

Claim. 假设 $x=x(t)$ 是关于 $t$ 的一个方程，且满足 $\frac{dx}{dt} + a_{1}x + a_{2}x^{2}=0$$x(0)=x_{0}$，那么它的解是：$x(t) = \frac{e^{-a_{1}t}}{\frac{1}{x_{0}}-\frac{a_{2}}{a_{1}}(e^{-a_{1}t}-1)}$.

Proof. 证明如下：

### SI 模型（Susceptible-Infective Model）

$\begin{cases}\frac{dS}{dt} = -\frac{r\beta I}{N} S \\ \frac{dI}{dt}=\frac{r\beta I}{N}S \end{cases}$

$I(t) = \frac{NI_{0}}{I_{0}+(N-I_{0})e^{-r\beta t}}$.

### SIS 模型（Susceptible-Infectious-Susceptible Model）

$\begin{cases} \frac{dS}{dt} = -r \beta S\frac{I}{N} + \gamma I \\ \frac{dI}{dt}=r\beta S \frac{I}{N} - \gamma I \end{cases}$，其初始条件就是 $S(0)=S_{0}$$I(0)=I_{0}$.

$I(t) = \frac{N(r\beta-\gamma)}{r\beta}/\bigg(\bigg(\frac{N(r\beta-\gamma)}{I_{0}r\beta}-1\bigg)e^{-(r\beta-\gamma)t}+1\bigg)$.

### 参考文献

1. Introduction to SEIR Models, Nakul Chitnis, Workshop on Mathematical Models of Climate Variability, Environmental Change and Infectious Diseases, Trieste, Italy, 2017

# SymPy 的简单介绍

SymPy 是一个符号计算的 Python 库，完全由 Python 写成，为许多数值分析，符号计算提供了重要的工具。SymPy 的第一个版本于 2007 年开源，并且经历了十几个版本的迭代，在 2019 年已经基于修正的 BSD 许可证开源了 1.4 版本。SymPy 的开源地址和官方网站分别是：

1. GitHub 链接：https://github.com/sympy/sympy
2. SymPy 官方网站：https://www.sympy.org/en/index.html

SymPy 的 1.4 版本文档中，可以看出，SymPy 可以支持很多初等数学，高等数学，甚至研究生数学的符号计算。在初等数学和高等数学中，SymPy 可以支持的内容包括但不限于：

1. 基础计算（Basic Operations）；
2. 公式简化（Simplification）；
3. 微积分（Calculus）；
4. 解方程（Solver）；
5. 矩阵（Matrices）；
6. 几何（geometry）；
7. 级数（Series）；

1. 范畴论（Category Theory）；
2. 微分几何（Differential Geometry）；
3. 常微分方程（ODE）；
4. 偏微分方程（PDE）；
5. 傅立叶变换（Fourier Transform）；
6. 集合论（Set Theory）；
7. 逻辑计算（Logic Theory）。

# SymPy 的工具库介绍

## SymPy 的基础计算

$e^{i\pi}+1 = 0$

sympy.exp(1), sympy.I, sympy.pi, sympy.oo

>>> sympy.exp(sympy.I * sympy.pi) + 1
0

>>> sympy.E.evalf(10)
2.718281828
>>> sympy.E.evalf()
2.71828182845905
>>> sympy.pi.evalf(10)
3.141592654
>>> sympy.pi.evalf()
3.14159265358979

>>> expr = sympy.sqrt(8)
>>> expr.evalf()
2.82842712474619

>>> x, y= sympy.symbols("x y")
>>> x + y
x + y
>>> x - y
x - y
>>> x * y
x*y
>>> x / y
x/y

>>> x1, y1, x2, y2 = sympy.symbols("x1 y1 x2 y2")
>>> z1 = x1 + y1 * sympy.I
x1 + I*y1
>>>  z2 = x2 + y2 * sympy.I
x2 + I*y2
>>> z1 + z2
x1 + x2 + I*y1 + I*y2
>>> z1 - z2
x1 - x2 + I*y1 - I*y2
>>> z1 * z2
(x1 + I*y1)*(x2 + I*y2)
>>> z1 / z2
(x1 + I*y1)/(x2 + I*y2)

>>> sympy.expand((x+1)**2)
x**2 + 2*x + 1
>>> sympy.expand((x+1)**5)
x**5 + 5*x**4 + 10*x**3 + 10*x**2 + 5*x + 1
>>> sympy.factor(x**3+1)
(x + 1)*(x**2 - x + 1)
>>> sympy.factor(x**2+3*x+2)
(x + 1)*(x + 2)
>>> sympy.simplify(x**2 + x + 1 - x)
x**2 + 1
>>> sympy.simplify(sympy.sin(x)**2 + sympy.cos(x)**2)
1


>>> expr = x*y + x - 3 + 2*x**2 - x**2 + x**3 + y**2 + x**2*y**2
>>> sympy.collect(expr,x)
x**3 + x**2*(y**2 + 1) + x*(y + 1) + y**2 - 3
>>> sympy.collect(expr,y)
x**3 + x**2 + x*y + x + y**2*(x**2 + 1) - 3
>>> expr.coeff(x, 2)
y**2 + 1
>>> expr.coeff(y, 1)
x

$expr = \frac{x^{2}+3x+2}{x^{2}+x}$

>>> expr = (x**2 + 3*x + 2)/(x**2 + x)
>>> sympy.cancel(expr)
(x + 2)/x
>>> sympy.together(expr)
(x**2 + 3*x + 2)/(x*(x + 1))

expr = (x**2 + 3*x + 2)/(x**2 + x)
>>> sympy.factor(expr)
(x + 2)/x
>>> expr = (x**3 + 3*x**2 + 2*x)/(x**5+x)
>>> sympy.factor(expr)
(x + 1)*(x + 2)/(x**4 + 1)
>>> expr = x**2 + (2*x+1)/(x**3+1)
>>> sympy.factor(expr)
(x**5 + x**2 + 2*x + 1)/((x + 1)*(x**2 - x + 1))

>>> expr = (x**4 + 3*x**2 + 2*x)/(x**2+x)
>>> sympy.apart(expr)
x**2 - x + 4 - 2/(x + 1)
>>> expr = (x**5 + 1)/(x**3+1)
>>> sympy.apart(expr)
x**2 - (x - 1)/(x**2 - x + 1)

>>> expr = sympy.sin(x)**2 + sympy.cos(x)**2
>>> sympy.trigsimp(expr)
1
>>> sympy.expand_trig(sympy.sin(x+y))
sin(x)*cos(y) + sin(y)*cos(x)
>>> sympy.expand_trig(sympy.cos(x+y))
-sin(x)*sin(y) + cos(x)*cos(y)
>>> sympy.trigsimp(sympy.sin(x)*sympy.cos(y) + sympy.sin(y)*sympy.cos(x))
sin(x + y)
>>> sympy.trigsimp(-sympy.sin(x)*sympy.sin(y) + sympy.cos(x)*sympy.cos(y))
cos(x + y)

>>> sympy.powsimp(x**z*y**z*x**z)
x**(2*z)*y**z
>>> sympy.simplify(x**z*y**z*x**z)
x**(2*z)*y**z
>>> sympy.expand_power_exp(x**(y + z))
x**y*x**z
>>> sympy.expand_power_base(x**(y + z))
x**(y + z)

$\ln(xy) = \ln(x) + \ln(y)$

$\ln(x/y) = \ln(x) - \ln(y)$

>>> sympy.expand_log(sympy.log(x*y), force=True)
log(x) + log(y)
>>> sympy.expand_log(sympy.log(x/y), force=True)
log(x) - log(y)

## SymPy 的微积分工具

>>> import sympy
>>> x = sympy.Symbol("x")
>>> f = 1 / x
1/x
>>> y = sympy.Symbol("y")
>>> f = f.subs(x,y)
1/y
>>> f = f.subs(y,1)
1

>>> f = 1/x
>>> sympy.limit(f,x,0)
oo
>>> sympy.limit(f,x,2)
1/2
>>> sympy.limit(f,x,sympy.oo)
0
>>> g = x * sympy.log(x)
>>> sympy.limit(g,x,0)
0

>>> f = 1/x
>>> sympy.diff(f,x)
-1/x**2
>>> sympy.diff(f,x,2)
2/x**3
>>> sympy.diff(f,x,3)
-6/x**4
>>> sympy.diff(f,x,4)
24/x**5

sympy.series.series.series(expr, x=None, x0=0, n=6, dir='+') >>> g = sympy.cos(x) >>> sympy.series(g, x) 1 - x**2/2 + x**4/24 + O(x**6) >>> sympy.series(g, x, x0=1, n=10) cos(1) - (x - 1)*sin(1) - (x - 1)**2*cos(1)/2 + (x - 1)**3*sin(1)/6 + (x - 1)**4*cos(1)/24 - (x - 1)**5*sin(1)/120 - (x - 1)**6*cos(1)/720 + (x - 1)**7*sin(1)/5040 + (x - 1)**8*cos(1)/40320 - (x - 1)**9*sin(1)/362880 + O((x - 1)**10, (x, 1))

$\int\frac{1}{x}dx = \ln(x)+C$

$\int_{1}^{2}\frac{1}{x}dx = \ln(2)$

>>> f = 1/x
>>> sympy.integrate(f,x)
log(x)
>>> sympy.integrate(f, (x,1,2))
log(2)

$\int_{-\infty}^{0}e^{-x^{2}}dx=\frac{\sqrt{\pi}}{2}$

$\int_{0}^{+\infty}e^{-x}dx = 1$

$\int_{-\infty}^{+\infty}\int_{-\infty}^{+\infty}e^{-x^{2}-y^{2}}dxdy = \pi$

>>> g = sympy.exp(-x**2)
>>> sympy.integrate(g, (x,-sympy.oo,0))
sqrt(pi)/2
>>> g = sympy.exp(-x)
>>> sympy.integrate(g, (x, 0, sympy.oo))
1
>>> h = sympy.exp(-x**2 - y**2)
>>> sympy.integrate(h, (x,-sympy.oo, sympy.oo), (y, -sympy.oo, sympy.oo))
pi

## SymPy 的方程工具

$\{x\in\mathbb{R}: x^{3}-1=0\}$

$\{x\in\mathbb{C}:x^{3}-1=0\}$

$\{x\in\mathbb{R}:e^{x}-x=0\}$

$\{x\in\mathbb{R}:e^{x}-1=0\}$

$\{x\in\mathbb{C}:e^{x}-1=0\}$

>>> sympy.solveset(sympy.Eq(x**3,1), x, domain=sympy.S.Reals)
{1}
>>> sympy.solveset(sympy.Eq(x**3,1), x, domain=sympy.S.Complexes)
{1, -1/2 - sqrt(3)*I/2, -1/2 + sqrt(3)*I/2}
>>> sympy.solveset(sympy.Eq(x**3 - 1,0), x, domain=sympy.S.Reals)
{1}
>>> sympy.solveset(sympy.Eq(x**3 - 1,0), x, domain=sympy.S.Complexes)
{1, -1/2 - sqrt(3)*I/2, -1/2 + sqrt(3)*I/2}
>>> sympy.solveset(sympy.exp(x),x)
EmptySet()
>>> sympy.solveset(sympy.exp(x)-1,x,domain=sympy.S.Reals)
{0}
>>> sympy.solveset(sympy.exp(x)-1,x,domain=sympy.S.Complexes)
ImageSet(Lambda(_n, 2*_n*I*pi), Integers)

$\begin{cases}x+y-10=0 \\ x-y-2=0\end{cases}$

>>> sympy.solve([x+y-10, x-y-2], [x,y])
{x: 6, y: 4}

$\begin{cases} \sin(x-y)=0 \\ \cos(x+y)=0 \end{cases}$

>>> sympy.solve([sympy.sin(x-y), sympy.cos(x+y)], [x,y])
[(-pi/4, 3*pi/4), (pi/4, pi/4), (3*pi/4, 3*pi/4), (5*pi/4, pi/4)]

## SymPy 的矩阵工具

>>> sympy.eye(3)
Matrix([
[1, 0, 0],
[0, 1, 0],
[0, 0, 1]])
>>> sympy.eye(3,2)
Matrix([
[1, 0],
[0, 1],
[0, 0]])
>>> sympy.eye(2,3)
Matrix([
[1, 0, 0],
[0, 1, 0]])

>>> sympy.ones(2,3)
Matrix([
[1, 1, 1],
[1, 1, 1]])
>>> sympy.zeros(3,2)
Matrix([
[0, 0],
[0, 0],
[0, 0]])

>>> sympy.diag(1,1,2)
Matrix([
[1, 0, 0],
[0, 1, 0],
[0, 0, 2]])

$A = \left(\begin{array}{cc} 1 & 1 \\ 0 & 2 \end{array}\right)$

$B = \left(\begin{array}{cc} 1 & 0 \\ 1 & 1 \end{array}\right)$

>>> A = sympy.Matrix([[1,1],[0,2]])
>>> B = sympy.Matrix([[1,0],[1,1]])
>>> A
Matrix([
[1, 1],
[0, 2]])
>>> B
Matrix([
[1, 0],
[1, 1]])
>>> A + B
Matrix([
[2, 1],
[1, 3]])
>>> A - B
Matrix([
[ 0, 1],
[-1, 1]])
>>> A * B
Matrix([
[2, 1],
[2, 2]])
>>> A * B**-1
Matrix([
[ 0, 1],
[-2, 2]])
>>> B**-1
Matrix([
[ 1, 0],
[-1, 1]])
>>> A.T
Matrix([
[1, 0],
[1, 2]])
>>> A.det()
2

>>> A
Matrix([
[1, 1],
[0, 2]])
>>> A.row_insert(1, sympy.Matrix([[10,10]]))
Matrix([
[ 1, 1],
[10, 10],
[ 0, 2]])
>>> A.col_insert(0, sympy.Matrix([3,3]))
Matrix([
[3, 1, 1],
[3, 0, 2]])
>>> A.row_del(0)
>>> A
Matrix([[0, 2]])
>>> A.col_del(1)
>>> A
Matrix([[0]])

>>> A
Matrix([
[1, 1],
[0, 2]])
>>> A.eigenvals()
{2: 1, 1: 1}
>>> A.eigenvects()
[(1, 1, [Matrix([
[1],
[0]])]), (2, 1, [Matrix([
[1],
[1]])])]
>>> A.diagonalize()
(Matrix([
[1, 1],
[0, 1]]), Matrix([
[1, 0],
[0, 2]]))

>>> A = sympy.Matrix([[1,1],[0,2]])
>>> A
Matrix([
[1, 1],
[0, 2]])
>>> b = sympy.Matrix([3,5])
>>> b
Matrix([
[3],
[5]])
>>> A**-1*b
Matrix([
[1/2],
[5/2]])
>>> sympy.linsolve((A,b))
{(1/2, 5/2)}
>>> sympy.linsolve((A,b),[x,y])
{(1/2, 5/2)}

## SymPy 的集合论工具

I = sympy.Interval(0,1)
J = sympy.Interval.open(0,1)
K = sympy.Interval(0.5,2)

>>> I.start
0
>>> I.end
1

>>> I.measure
1

>>> I.closure
Interval(0, 1)

>>> I.interior
Interval.open(0, 1)

>>> I.left_open
False
>>> I.right_open
False

I = sympy.Interval(0,1)
K = sympy.Interval(0.5,2)
>>> I.intersect(K)
Interval(0.500000000000000, 1)
>>> I.union(K)
Interval(0, 2)
>>> I-K
Interval.Ropen(0, 0.500000000000000)
>>> K-I
Interval.Lopen(1, 2)
>>> I.symmetric_difference(K)
Union(Interval.Ropen(0, 0.500000000000000), Interval.Lopen(1, 2))

>>> sympy.S.Reals
Reals
>>> sympy.S.Reals-I
Union(Interval.open(-oo, 0), Interval.open(1, oo))
>>> I.complement(sympy.S.Reals)
Union(Interval.open(-oo, 0), Interval.open(1, oo))
>>> sympy.S.Reals.complement(I)
EmptySet()
>>> I.complement(K)
Interval.Lopen(1, 2)
>>> I.complement(sympy.S.Reals)
Union(Interval.open(-oo, 0), Interval.open(1, oo))

## SymPy 的逻辑工具

A, B, C = sympy.symbols("A B C")
>>> sympy.simplify_logic(A | (A & B))
A
>>> sympy.simplify_logic((A>>B) & (B>>A))
(A & B) | (~A & ~B)
>>> A>>B
Implies(A, B)

## SymPy 的级数工具

SymPy 的级数工具有一部分放在具体数学（Concrete Mathematics）章节了。有的时候，我们希望计算某个级数是发散的，还是收敛的，就可以使用 is_convergence 函数。考虑最常见的级数：

$\sum_{n=1}^{\infty}\frac{1}{n} = +\infty$

$\sum_{n=1}^{\infty}\frac{1}{n^{2}} = \frac{\pi^{2}}{6}$

>>> n = sympy.Symbol("n", integer=True)
>>> sympy.Sum(1/n, (n,1,sympy.oo)).is_convergent()
False
>>> sympy.Sum(1/n**2, (n,1,sympy.oo)).is_convergent()
True

>>> sympy.Sum(1/n**2, (n,1,sympy.oo)).evalf()
1.64493406684823
>>> sympy.Sum(1/n**2, (n,1,sympy.oo)).doit()
pi**2/6
>>> sympy.Sum(1/n**3, (n,1,sympy.oo)).evalf()
1.20205690315959
>>> sympy.Sum(1/n**3, (n,1,sympy.oo)).doit()
zeta(3)

$\prod_{n=1}^{+\infty}\frac{n}{n+1}$

$\prod_{n=1}^{+\infty}\cos\left(\frac{\pi}{n}\right)$

>>> sympy.Product(n/(n+1), (n,1,sympy.oo)).is_convergent()
False
>>> sympy.Product(sympy.cos(sympy.pi/n), (n, 1, sympy.oo)).is_convergent()
True

## SymPy 的 ODE 工具

$df/dx + f(x) = 0$,

$d^{2}f/dx^{2} + f(x) = 0$

$d^{3}f/dx^{3} + f(x) = 0$

>>> f = sympy.Function('f')
>>> sympy.dsolve(sympy.Derivative(f(x),x) + f(x), f(x))
Eq(f(x), C1*exp(-x))
>>> sympy.dsolve(sympy.Derivative(f(x),x,2) + f(x), f(x))
Eq(f(x), C1*sin(x) + C2*cos(x))
>>> sympy.dsolve(sympy.Derivative(f(x),x,3) + f(x), f(x))
Eq(f(x), C3*exp(-x) + (C1*sin(sqrt(3)*x/2) + C2*cos(sqrt(3)*x/2))*sqrt(exp(x)))

>>> sympy.classify_ode(sympy.Derivative(f(x),x) + f(x), f(x))
('separable', '1st_exact', '1st_linear', 'almost_linear', '1st_power_series', 'lie_group', 'nth_linear_constant_coeff_homogeneous', 'separable_Integral', '1st_exact_Integral', '1st_linear_Integral', 'almost_linear_Integral')
>>> sympy.classify_ode(sympy.Derivative(f(x),x,2) + f(x), f(x))
('nth_linear_constant_coeff_homogeneous', '2nd_power_series_ordinary')
>>> sympy.classify_ode(sympy.Derivative(f(x),x,3) + f(x), f(x))
('nth_linear_constant_coeff_homogeneous',)

## SymPy 的 PDE 工具

$\partial f/\partial x + \partial f/\partial y =0$

$\partial f/\partial x + \partial f/\partial y + f = 0$

$\partial f/\partial x + \partial f/\partial y + f + 10 = 0$

>>> f = sympy.Function("f")(x,y)
>>> sympy.pdsolve(sympy.Derivative(f,x)+sympy.Derivative(f,y),f)
Eq(f(x, y), F(x - y))
>>> sympy.pdsolve(f.diff(x)+f.diff(y)+f,f)
Eq(f(x, y), F(x - y)*exp(-x/2 - y/2))
>>> sympy.pdsolve(f.diff(x)+f.diff(y)+f+10,f)
Eq(f(x, y), F(x - y)*exp(-x/2 - y/2) - 10)

>>> sympy.classify_pde(f.diff(x)+f.diff(y)+f)
('1st_linear_constant_coeff_homogeneous',)
>>> sympy.classify_pde(f.diff(x)+f.diff(y)+f+10,f)
('1st_linear_constant_coeff', '1st_linear_constant_coeff_Integral')
>>> sympy.classify_pde(f.diff(x)+f.diff(y)+f+10,f)
('1st_linear_constant_coeff', '1st_linear_constant_coeff_Integral')

## SymPy 的数论工具

>>> sympy.sieve._reset()
>>> sympy.sieve.extend_to_no(100)
>>> sympy.sieve._list
array('l', [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631])

>>> [i for i in sympy.sieve.primerange(10,100)]
[11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]

search() 函数是为了计算某个数附近是第几个素数：

>>> sympy.sieve.search(10)
(4, 5)
>>> sympy.sieve.search(11)
(5, 5)

>>> sympy.ntheory.generate.prime(10)
29
>>> sympy.ntheory.generate.nextprime(10)
11
>>> sympy.ntheory.generate.nextprime(11)
13
>>> sympy.ntheory.generate.isprime(11)
True
>>> sympy.ntheory.generate.isprime(12)
False

## SymPy 的范畴论工具

SymPy 还支持范畴论（Category Theory）的一些计算方法，在这里简要地列举一下。

>>> A = sympy.categories.Object("A")
>>> B = sympy.categories.Object("B")
>>> f = sympy.categories.NamedMorphism(A,B,"f")
>>> f.domain
Object("A")
>>> f.codomain
Object("B")

## 参考文献：

1. Meurer A, Smith C P, Paprocki M, et al. SymPy: symbolic computing in Python[J]. PeerJ Computer Science, 2017, 3: e103.
2. GitHub：https://github.com/sympy/sympy
3. SymPy：https://www.sympy.org/en/index.html
4. Sympy 维基百科：https://en.wikipedia.org/wiki/SymPy
5. GreatX’s Blog：数值 Python：符号计算：https://vlight.me/2018/04/01/Numerical-Python-Symbolic-Computing/
6. SymPy 符号计算-让Python帮我们推公式：https://zhuanlan.zhihu.com/p/83822118
7. Python 科学计算利器—SymPy库：https://www.jianshu.com/p/339c91ae9f41