Processing the capillary force video

Posted on Tue 30 May 2017 in Computer

Frames Export

Get frame rate information

videoname=T-L\ _\ 1-50\ tip-tip.avi
ffmpeg -i $videoname 2>&1 |grep -o '[0-9]\+ fps'

The output is 30 fps

ffmpeg -i $videoname -r 30 output_%04d.png

Edges Detection

The Canny edge detector is used in this step.

In [1]:
from capillary …

Continue reading

Covariant Linear Fit

Posted on Mon 29 May 2017 in Computer

Aim

Minimize $$\sum_i \mathrm{distance}^2(\vec r_i, \mathrm{line})=\sum_i (\vec r_i\cdot \hat n-\rho)^2$$ for line $\vec r\cdot \hat n-\rho=0$. It is equivalent to

  • The principle axis with least moment of inertia
  • The eigenvector with largest eigenval for the covariance matrix

When is …


Continue reading

Convert str(array) back to numpy array

Posted on Sun 21 May 2017 in Physics

If we print a numpy array, which actually use str(), we will find it almost irreversible.

In [5]:
l=arange(16).reshape(4,4)
print('l is printed as:\n', l)
l is printed as:
 [[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 …

Continue reading

又双叒叕——汉字的堆叠

Posted on Tue 16 May 2017 in Statistics

你可能会有这样的问题:类似“又双叒叕”这样子的汉字有多少?本文就是对此问题的分析。

数据来源

原始数据来源于参考维基百科条目二叠字三叠字四叠字

格式化后的的数据,参见源文件

本页面含有Unihan新版用字。有关字符可能会错误显示,详见Unicode扩展汉字。

数据分析

使用pandas进行数据表连接查询,可以找到同时具有多种叠字的字。

In [1]:
import pandas as pd
from functools import reduce
In [2]:
data=[pd.read_table('%d.txt'%i) for i in range(2, 5)]
In [3]:
def merge_out(x …

Continue reading

带负权有向无环图的最短路径

Posted on Mon 15 May 2017 in Computer

最短路径

正权图

用广度优先,先到达的点一定是最短路径。因此不需要考虑后续更新问题

带负权

由于先到达的某条路可能被后面带负权的路所代替,需要考虑后续更新问题。为了防止某节点的最短路径更新导致的后续步骤重算,任何节点必须等待他的依赖做完,此时给出的路径才是真正的最短路径。这个过程需要使用拓扑排序(考虑依赖的任务排序)来实现。

每次某个事情做完的时候,计算假设走该路径它的后继到达所需要的时间。如果该时间更短的话,给它的后继节点更新时间并记录该路径。

拓扑排序

初始时有一系列任务待处理,构成一个任务树。这些任务有的不需要依赖(入度为零)即可以完成,称为树叶;另一些是出度为零的树根;其他的称为树干。每次剪除任意一个枝叶,此次剪除可能产生新的枝叶,加入枝叶队列。以此不断循环。

此方法应用于一个任意有向/无向图可以剪除所有树枝,剩余的皆是非平凡的,或者连接环路的


Continue reading

Reduced density matrix and partial trace

Posted on Wed 03 May 2017 in Physics

The reduced matrix is defined as the partial trace of the density matrix. In linear algebra and functional analysis, the partial trace is a generalization of the trace. Whereas the trace is a scalar valued function on operators, the partial trace is an operator-valued function. The partial trace has applications in quantum information and decoherence which is relevant for quantum measurement and thereby to the decoherent approaches to interpretations of quantum mechanics, including consistent histories and the relative state interpretation.


Continue reading

对换钱悖论的贝叶斯分析

Posted on Tue 04 April 2017 in Statistics

问题的提出

现在邀请你和一位路人甲来做一个游戏:我拿出两个信封分别递给你们,并告诉你 们一个装着的钱是另一个的两倍(但不知道哪个多哪个少)。你们有一次互相交换 的机会,想交换吗?然后打开信封,看一下自己拿到的钱数(但不要让对方知道),现在还想交换吗?

——引自0x01.me

这段话似乎可以引出一个悖论:

换钱后,有一半的概率钱变成原来的两倍,一半概率钱减少到原来的一半,因此期望收益是大于零的。然而另一方面,两份钱显然是对称的,因此不可能总是由换钱带来收益。

下面我们用贝叶斯来分析这个悖论。

贝叶斯分析

假设发钱时,钱少的那个钱数取$y$的先验分布为$\rho(y)$。设某人抽到了钱数是$x$,则此时$y$的取值可以是$x$或者$x/2$。

参数$y$下$x$的分布是 $$p …


Continue reading

Blogging with Jupyter and Pelican

Posted on Thu 30 March 2017 in Computer

This articles tells how to build this blog site.

Installation and Setup

  • Jupyter, and an article telling what is jupyter notebook
  • Pelican, pythonic blog system supports markdown, rst, asciidoc, ipynb etc.
  • ipynb2pelican, a plugin enables ipynb support by metacell
  • ghp-import is needed for github page import. It can be installed …

Continue reading

ipynb2pelican Plugin released!

Posted on Mon 27 March 2017 in Computer

Yet another Pelican Plugin for blogging with Jupyter Notebooks using MetaCell to store metadata.

Thanks to super cow power of python, we can finally publish ipynb easily! Below is the README.md from the project at this time.

The ipynb2pelican plugin provides markup for Jupyter/IPython notebooks in pelican, so …


Continue reading

Next Permutation

Posted on Fri 24 March 2017 in Puzzles

Aim: Find a larger permutation

We must move a larger number forward. So we should go from the tail to find any new number is smaller than a number behind it. If no next permutation, reverse list.

In [ ]:
def next_permutation(l):
    '''Give out the next permutation of list l
    >>> next_permutation …

Continue reading