Rotating Doughnut Using Matrix Multiplication

My copy of Escher’s artwork got some attention and quite some people asked me, “what kind of programmer are you?”, well, this Youtube video explains it well.

Actually, Joma’s video basically depicted me when in real dev mode. Upon watching this video, I couldn’t wait to re-implement it in Python. However, the initial version is basically a trans-coding of the original C code and was pretty hard to read. So here is a version that actually uses Matrix multiplication in Python (In the mean time, I still have to fix the syntax highlighter…):


import time

import numpy as np

screen_size = 40
theta_spacing = 0.07
phi_spacing = 0.02

illumination = np.fromiter(".,-~:;=!*#$@", dtype="<U1")

A = 1
B = 1
R1 = 1
R2 = 2
K2 = 5
K1 = screen_size * K2 * 3 / (8 * (R1 + R2))


def render_frame(A: float, B: float) -> np.ndarray:
"""
Returns a frame of the spinning 3D donut.
Based on the pseudocode from: https://www.a1k0n.net/2011/07/20/donut-math.html
"""
cos_A = np.cos(A)
sin_A = np.sin(A)
cos_B = np.cos(B)
sin_B = np.sin(B)

output = np.full((screen_size, screen_size), " ") # (40, 40)
zbuffer = np.zeros((screen_size, screen_size)) # (40, 40)

cos_phi = np.cos(phi := np.arange(0, 2 * np.pi, phi_spacing)) # (315,)
sin_phi = np.sin(phi) # (315,)
cos_theta = np.cos(theta := np.arange(0, 2 * np.pi, theta_spacing)) # (90,)
sin_theta = np.sin(theta) # (90,)
circle_x = R2 + R1 * cos_theta # (90,)
circle_y = R1 * sin_theta # (90,)

x = (np.outer(cos_B * cos_phi + sin_A * sin_B * sin_phi, circle_x) - circle_y * cos_A * sin_B).T # (90, 315)
y = (np.outer(sin_B * cos_phi - sin_A * cos_B * sin_phi, circle_x) + circle_y * cos_A * cos_B).T # (90, 315)
z = ((K2 + cos_A * np.outer(sin_phi, circle_x)) + circle_y * sin_A).T # (90, 315)
ooz = np.reciprocal(z) # Calculates 1/z

xp = (screen_size / 2 + K1 * ooz * x).astype(int) # (90, 315)
yp = (screen_size / 2 - K1 * ooz * y).astype(int) # (90, 315)

L1 = (((np.outer(cos_phi, cos_theta) * sin_B) - cos_A * np.outer(sin_phi, cos_theta)) - sin_A * sin_theta) # (315, 90)
L2 = cos_B * (cos_A * sin_theta - np.outer(sin_phi, cos_theta * sin_A)) # (315, 90)
L = np.around(((L1 + L2) * 8)).astype(int).T # (90, 315)

mask_L = L >= 0 # (90, 315)
chars = illumination[L] # (90, 315)

for i in range(90):
mask = mask_L[i] & (ooz[i] > zbuffer[xp[i], yp[i]]) # (315,)

zbuffer[xp[i], yp[i]] = np.where(mask, ooz[i], zbuffer[xp[i], yp[i]])
output[xp[i], yp[i]] = np.where(mask, chars[i], output[xp[i], yp[i]])

return output


def pprint(array: np.ndarray) -> None:
"""Pretty print the frame."""
print(*[" ".join(row) for row in array], sep="\n")


if __name__ == "__main__":
for _ in range(screen_size * screen_size):
A += theta_spacing
B += phi_spacing

# clear screen using ANSI control code
print("\x1b[H")
time.sleep(0.05)
pprint(render_frame(A, B))

 

溺水的抢救办法

又看到溺水的新闻,想起来几年前Dr. Atul Gawande的讲座中提到的溺水救治案例。这个案例发生于1999年,一个三岁的小女孩溺水超过半个小时,被救上来之后没有呼吸,没有心跳,瞳孔对光照没有反应,但是由于大家都不愿意放弃,经过努力,孩子被救活了,而且后来发展正常。

So this was a case report that laid out the treatment from following a case of a 3 year old girl.

下面是案例报告,描述了对一个三岁女孩儿的救治。

She’d been in a small town in Austria and there her parents – it had been a winter day – her parents had gone out on a walk with her. And it was one of those terrible things: the parents lost sight of their little girl just for a moment and the next thing, they looked and she was out on the surface of this icy fishpond and then she fell through the ice under the water and was gone.

她一直住在奥地利的一个小镇上。在一个冬日里,父母陪着她散步。然后可怕的事情发生了:父母只是一小会儿没看到她,再看到的时候她已经在鱼塘的冰面上了,紧接着她掉下冰面不见了。

继续阅读溺水的抢救办法

《文明、现代化、价值投资与中国》读后

拿到李录的新书,心里有些激动。我已经很久没有主动想起来要看一本投资类的书了,现在决定买一本认真研读,当然是因为李录的传奇性。

我是大概三四年前听说李录,作为31年前通缉令上的人物,如今在美国做起了投资。听说做的挺成功,却不知道有多么成功。直到我看到《穷查理宝典》的中文版里面,李录做的序言,《书中自有黄金屋》。

序言当然并不是在夸耀自己的成功,却让我看到李录在一方面投资事业已经做到了如此高度,另一方面仍然保持着传统中国读书人的特质,从未停止思考中国现代化的问题,会在现代化问题下谈孔子和儒家,谈《大学》,谈士大夫精神和五四精神。

继续阅读《文明、现代化、价值投资与中国》读后

2019年BBC睿思讲座 – 法律的扩张与政治的衰落

终于翻译完了这个系列,的主体部分,问答部分还没有完成,另外也还需要一些注释。

Commentary on Lord Sumption’s Reith Lectures

但是阶段性工作完成,可以稍微总结一下。

讲座一共五个部分,第一部分,法律帝国中的扩张(Law’s Expanding Empire),探讨在公共生活中,法律裁决越来越成为常态,留给个体选择的空间越来越小。主讲人指出,有两个原因导致这种现象:其一是人们在很多问题上寻求法律所产生的一致性;其二是对安全感的需求越来越高,导致人们主动要求立法,让渡自己的自主权。主讲人认为法律已经侵入公共和私人空间太多,对民主造成伤害。

继续阅读2019年BBC睿思讲座 – 法律的扩张与政治的衰落

Shifting the Foundations – 改变基础

本文是第五部分,其它部分:

THE REITH LECTURES 2019: LAW AND THE DECLINE OF POLITICS

TX: 18.06.2019   0900-1000

Reith Lecturer: Jonathan Sumption

Lecture 5: Shifting the Foundations 改变基础

BBC页面在这里:https://www.bbc.co.uk/programmes/m00060vc

音频下载

Podcast链接

 

ANITA ANAND: Welcome to the fifth and final BBC Reith Lecture of 2019 with the former Supreme Court Judge, Jonathan Sumption. 

欢迎来到第五次也是2019年最后一次睿思讲座来听前最高法院法官Jonathan Sumption的演讲。

We’re at Cardiff University’s School of Journalism, Media and Culture, housed in a shiny new building which opened only last September, for the last of this series examining the relationship between the law and politics. 

今天,我们在Cardiff大学的新闻媒体和文化学院,坐落在一座去年9月刚刚开放的崭新建筑中,听这个探讨法律与政治关系的系列讲座的最后一次演讲。

So far, Jonathan has questioned what he calls law’s expanding empire and the mission creep of the European Convention of Human Rights. He has discussed how best democracy can accommodate political difference and has warned the UK against going down the American constitutional road. Now, he is going to offer some suggestions to try and re-energise political participation, both in our institutions and political processes. The lecture is called “Shifting the Foundations”.

到目前为止,Jonathan对他称之为法律帝国的扩张和欧洲人权公约的使命扩张进行了质疑,讨论了民主制可以如何容纳政治分歧并警告英国不要走美国的宪政之路。现在他将提出一些建议,以尝试重新激活制度和政治进程中的政治参与。讲座题目是“调整基础”。

Please will you welcome the 2019 BBC Reith Lecturer, Jonathan Sumption.

欢迎2019 BBC 睿思讲座主讲人,Jonathan Sumption。

(AUDIENCE APPLAUSE)

 

继续阅读Shifting the Foundations – 改变基础

Rights and the Ideal Constitution – 权利与理想中的宪法

本文是第四部分,其它部分:

THE REITH LECTURES 2019: LAW AND THE DECLINE OF POLITICS

TX: 11.06.2019   0900-1000

Reith Lecturer: Jonathan Sumption

Lecture 4: Rights and the Ideal Constitution 权利与理想中的宪法

BBC页面在这里:https://www.bbc.co.uk/programmes/m0005t85

音频下载

Podcast链接

ANITA ANAND: Welcome to Washington DC and the fourth BBC Reith Lecture with the former UK Supreme Court Judge, Jonathan Sumption. 

欢迎来到华盛顿特区参加英国前最高法院法官Jonathan Sumption的第四场睿思讲座。

We’re at George Washington University, home to 26,000 students. Former alumni include Jacqueline Kennedy Onassis and the former director of the FBI, J Edgar Hoover.  

我们在有26000名学生的华盛顿大学。其校友包括杰奎琳·肯尼迪和前FBI主任,J Edgar Hoover。

In his series, Jonathan has been interrogating the complex relationship between politics and the law, suggesting that the Courts have become too powerful. Now he compares the constitutional models of the US and the UK. This lecture is called Rights and the Ideal Constitution. 

在系列讲座中,Jonathan反复追问政治运作与法律之间的复杂关系,指出法院已变得过于强大。这次讲座,他会比较英国和美国的宪政模式,标题是权利与理想中的宪法。

Please welcome the BBC 2019 Reith Lecturer, Jonathan Sumption.

请欢迎BBC 2019睿思讲座主讲人,Jonathan Sumption。

(AUDIENCE APPLAUSE)

(掌声)

继续阅读Rights and the Ideal Constitution – 权利与理想中的宪法

Human Rights and Wrongs -人权及其滥用

本文是第三部分,其它部分:

THE REITH LECTURES 2019: LAW AND THE DECLINE OF POLITICS

TX: 04.06.2019   0900-1000

Reith Lecturer: Jonathan Sumption

Lecture 3: Human Rights and Wrongs  人权及其滥用

BBC节目介绍:https://www.bbc.co.uk/programmes/m0005msd

音频:

Podcast链接

ANITA ANAND: Welcome to the third of this year’s Reith Lectures with the former Supreme Court Justice Jonathan Sumption. 

欢迎来到今年睿思第三次讲座听前最高法院法官Jonathan Sumption演讲。

We are in Edinburgh’s Parliament House, a building which dates back to the 16th century. This place has long been home to the Court of Sessions, the highest court in Scotland, and here in the great hall we are dominated by a stunning stained glass window depicting the moment King James V confirmed the Court of Sessions right here in 1532. This is a place, therefore, steeped in regal and legal history, an entirely suitable setting for Jonathan Sumption to continue his series of lectures on the role of the law in our public and private life.

今天我们在爱丁堡议会大厦,始建于16世纪,这里长期以来一直是苏格兰最高法院——苏格兰高等民事法院所在地。在大厅里,令人惊叹的彩色玻璃窗俯视着我们,上面是国王詹姆斯五世于1532年在此确认高等法院的画面。因此,这饱含法律和法律历史的地方,非常适合Jonathan Sumption继续他关于法治与生活公私两面的讲座。

So far Jonathan has questioned what he calls “law’s expanding empire” and discussed how best democracy can accommodate political difference. Today he will be taking a look at human rights, in particular the role of the European Convention on Human Rights and the Strasbourg Court. The lecture is called Human Rights and Wrongs. 

迄今为止,Jonathan质疑了他所谓法律帝国的扩张,并讨论了民主应该如何容纳政治差异。今天,他将着眼于人权,特别是欧洲人权公约和斯特拉斯堡法院的角色。讲座的题目是“人权及其滥用”。

Please welcome the BBC 2019 Reith Lecturer, Jonathan Sumption.

有请BBC 2019睿思讲座主讲人,Jonathan Sumption。

(AUDIENCE APPLAUSE)

(掌声)

继续阅读Human Rights and Wrongs -人权及其滥用

Slow SSH Login

SSH login is supposed to be fast – All it needs are a key exchange to establish a secure channel and then an authentication process, which are both trivial tasks to modern computers. However, as it often happens, a simple login might take no less than 15 seconds.

I ran into this problem recently and had a investigation. As it turned out, there are multitude of factors that might affect the speed of the SSH login process. From the most obvious the to most inconspicuous, here are them:

  • The sshd_config file.

This is the main configuration file that controls the behavior of the ssh daemon. In this file, you want to make sure the following features are disabled:

继续阅读Slow SSH Login