二维码的纠错码原理及如何纠错(2)

下面进一步介绍二维码纠错相关的编码矩阵

1 范德蒙德(Vandermonde)矩阵

1.1 定义及特性

法国数学家 Alexandre-Théophile Vandermonde 在十八世纪提出了行列式的概念, 用来解决线性方程组问题, 其中一个关键是范德蒙德(Vandermonde) 矩阵, Vandermonde 矩阵具有如下的形式:
A n = [ 1 1 1 … 1 x 1 1 x 2 1 x 3 1 … x n 1 x 1 2 x 2 2 x 3 2 … x n 2 ⋮ ⋮ ⋮ ⋱ ⋮ x 1 n − 1 x 2 n − 1 x 3 n − 1 … x n n − 1 ] (1) \mathrm{A_n} = \left[ \begin{matrix} 1 &1 & 1 &\dots & 1 \\ x_1^1 & x_2^1 &x_3^1 & \dots &x_n^1 \\ x_1^2 & x_2^2 &x_3^2 & \dots &x_n^2 \\ \vdots & \vdots &\vdots & \ddots &\vdots \\ x_1^{n-1} & x_2^{n-1} &x_3^{n-1} & \dots &x_n^{n-1} \\ \end{matrix} \right] \tag1 An=1x11x12x1n11x21x22x2n11x31x32x3n11xn1xn2xnn1(1)
它的任意的子方阵均为可逆方阵。一个 n n n n n n列的范德蒙德矩阵定义如下,其中 x i x_i xi 均不相同,且不为0。Vandermonde 矩阵有一个很重要的特性:
∣ A n ∣ = ∣ A n T ∣ = ∏ 1 ≤ i ≤ j ≤ n ( x j − x i ) (2) |\mathrm{A_n}| = |\mathrm{A_n^T}| = \prod_{1 \leq i \leq j \leq n}(x_j - x_i) \tag2 An=AnT=1ijn(xjxi)(2)
例子1:
如下范德蒙矩阵
A n = [ 1 1 1 2 3 5 2 2 3 2 5 2 ] \mathrm{A_n} = \left[ \begin{matrix} 1 &1 & 1 \\ 2 & 3 &5 \\ 2^2 & 3^2 &5^2 \\ \end{matrix} \right] An=122213321552
那么它的行列式为 ∣ A n ∣ = ( 3 − 2 ) ∗ ( 5 − 2 ) ∗ ( 5 − 3 ) = 6 \left|A_n\right|=(3-2)*(5-2)*(5-3)=6 An=(32)(52)(53)=6.
例子2:
如下范德蒙矩阵
A n = [ 1 1 1 1 2 3 5 7 2 2 3 2 5 2 7 2 2 3 3 3 5 3 7 3 ] \mathrm{A_n} = \left[ \begin{matrix} 1 &1 & 1 &1\\ 2 & 3 &5 &7\\ 2^2 & 3^2 &5^2 &7^2\\ 2^3 & 3^3 &5^3 &7^3\\ \end{matrix} \right] An=122223133233155253177273
那么它的行列式为 ∣ A n ∣ = ( 3 − 2 ) ∗ ( 5 − 2 ) ∗ ( 7 − 2 ) ∗ ( 5 − 3 ) ∗ ( 7 − 3 ) ∗ ( 7 − 5 ) = 240 \left|A_n\right|=(3-2)*(5-2)*(7-2)*(5-3)*(7-3)*(7-5)=240 An=(32)(52)(72)(53)(73)(75)=240.
这个特性可以用数学归纳法证明出来。
【证明】
D n D_n Dn n n n阶Vandermonde行列式( n ≥ 2 n \geq 2 n2),则有
D n = ∏ 1 ≤ i ≤ j ≤ n ( x j − x i ) = ( x n − x n − 1 ) ( x n − x n − 2 ) … ( x n − x 1 ) ( x n − 1 − x n − 2 ) ( x n − 1 − x n − 3 ) … ( x n − 1 − x 1 ) … ( x 3 − x 2 ) ( x 3 − x 1 ) ( x 2 − x 1 ) . D_n = \prod_{1 \leq i \leq j \leq n}(x_j - x_i) = (x_n - x_{n-1})(x_n - x_{n-2})\dots(x_n - x_1)(x_{n-1} - x_{n-2}) \\ (x_{n-1} - x_{n-3})\dots(x_{n-1} - x_1)\dots(x_3 - x_2)(x_3 - x_1)(x_2 - x_1). Dn=1ijn(xjxi)=(xnxn1)(xnxn2)(xnx1)(xn1xn2)(xn1xn3)(xn1x1)(x3x2)(x3x1)(x2x1).
(1) 当 n = 2 n = 2 n=2时, D 2 = ∣ 1 1 x 1 x 2 ∣ = ( x 2 − x 1 ) D_2 = \left|\begin{matrix}1 &1 \\ x_1 &x_2\end{matrix}\right| = (x_2 - x_1) D2=1x11x2=(x2x1),结论成立;
(2) 假设结论对 n − 1 n - 1 n1阶范德蒙德行列式成立,即
D n − 1 = ∣ 1 1 … 1 x 2 1 x 3 1 … x n 1 x 2 2 x 3 2 … x n 2 ⋮ ⋮ ⋱ ⋮ x 2 n − 2 x 3 n − 2 … x n n − 2 ∣ = ∏ 2 ≤ i ≤ j ≤ n ( x j − x i ) D_{n-1} = \left| \begin{matrix} 1 & 1 &\dots & 1 \\ x_2^1 &x_3^1 & \dots &x_n^1 \\ x_2^2 &x_3^2 & \dots &x_n^2 \\ \vdots &\vdots & \ddots &\vdots \\ x_2^{n-2} &x_3^{n-2} & \dots &x_n^{n-2} \\ \end{matrix} \right| = \prod_{2 \leq i \leq j \leq n}(x_j - x_i) Dn1=1x21x22x2n21x31x32x3n21xn1xn2xnn2=2ijn(xjxi)
考虑 n n n阶范德蒙行列式的情形:
D n = ∣ 1 1 1 … 1 x 1 1 x 2 1 x 3 1 … x n 1 x 1 2 x 2 2 x 3 2 … x n 2 ⋮ ⋮ ⋮ ⋱ ⋮ x 1 n − 1 x 2 n − 1 x 3 n − 1 … x n n − 1 ∣ D_{n} = \left| \begin{matrix} 1 &1 & 1 &\dots & 1 \\ x_1^1 & x_2^1 &x_3^1 & \dots &x_n^1 \\ x_1^2 & x_2^2 &x_3^2 & \dots &x_n^2 \\ \vdots & \vdots &\vdots & \ddots &\vdots \\ x_1^{n-1} & x_2^{n-1} &x_3^{n-1} & \dots &x_n^{n-1} \\ \end{matrix} \right| Dn=1x11x12x1n11x21x22x2n11x31x32x3n11xn1xn2xnn1
从第 n n n行开始,自下而上依次的由下一行减去它上一行的 x 1 x_1 x1倍 ,有:
D n = ∣ 1 1 1 … 1 0 x 2 − x 1 x 3 − x 1 … x n − x 1 0 x 2 2 − x 2 x 1 x 3 2 − x 3 x 1 … x n 2 − x n x 1 ⋮ ⋮ ⋮ ⋱ ⋮ 0 x 2 n − 1 − x 2 n − 2 x 1 x 3 n − 1 − x 3 n − 2 x 1 … x n n − 1 − x n n − 2 x 1 ∣ = ∣ 1 1 1 … 1 0 x 2 − x 1 x 3 − x 1 … x n − x 1 0 x 2 ( x 2 − x 1 ) x 3 ( x 3 − x 1 ) … x n ( x n − x 1 ) ⋮ ⋮ ⋮ ⋱ ⋮ 0 x 2 n − 2 ( x 2 − x 1 ) x 3 n − 2 ( x 3 − x 1 ) … x n n − 2 ( x n − x 1 ) ∣ D_{n} = \left| \begin{matrix} 1 &1 & 1 &\dots & 1 \\ 0 & x_2- x_1 &x_3 - x_1 & \dots &x_n-x_1 \\ 0 & x_2^2-x_2x_1 &x_3^2-x_3x_1 & \dots &x_n^2-x_nx_1 \\ \vdots & \vdots &\vdots & \ddots &\vdots \\ 0 & x_2^{n-1}-x_2^{n-2}x_1 &x_3^{n-1}-x_3^{n-2}x_1 & \dots &x_n^{n-1}-x_n^{n-2}x_1 \\ \end{matrix} \right|\\ =\left| \begin{matrix} 1 &1 & 1 &\dots & 1 \\ 0 & x_2- x_1 &x_3 - x_1 & \dots &x_n-x_1 \\ 0 & x_2(x_2-x_1) &x_3(x_3-x_1) & \dots &x_n(x_n-x_1) \\ \vdots & \vdots &\vdots & \ddots &\vdots \\ 0 & x_2^{n-2}(x_2-x_1) &x_3^{n-2}(x_3-x_1) & \dots &x_n^{n-2}(x_n-x_1) \\ \end{matrix} \right| Dn=10001x2x1x22x2x1x2n1x2n2x11x3x1x32x3x1x3n1x3n2x11xnx1xn2xnx1xnn1xnn2x1=10001x2x1x2(x2x1)x2n2(x2x1)1x3x1x3(x3x1)x3n2(x3x1)1xnx1xn(xnx1)xnn2(xnx1)
按第一列展开后提取公因式,得
D n = ( x 2 − x 1 ) ( x 3 − x 1 ) … ( x n − x 1 ) ∣ 1 1 … 1 x 2 x 3 … x n ⋮ ⋮ ⋱ ⋮ x 2 n − 2 x 3 n − 2 … x n n − 2 ∣ = ( x 2 − x 1 ) ( x 3 − x 1 ) … ( x n − x 1 ) ∏ 2 ≤ i ≤ j ≤ n ( x j − x i ) = ∏ 1 ≤ i ≤ j ≤ n ( x j − x i ) D_{n} = (x_2 - x_1)(x_3 - x_1)\dots(x_n - x_1) \left| \begin{matrix} 1 &1 &\dots & 1 \\ x_2 & x_3 & \dots &x_n \\ \vdots & \vdots & \ddots &\vdots \\ x_2^{n-2} & x_3^{n-2} & \dots &x_n^{n-2} \\ \end{matrix} \right| \\ = (x_2 - x_1)(x_3 - x_1)\dots(x_n - x_1)\prod_{2 \leq i \leq j \leq n}(x_j - x_i) =\prod_{1 \leq i \leq j \leq n}(x_j - x_i) Dn=(x2x1)(x3x1)(xnx1)1x2x2n21x3x3n21xnxnn2=(x2x1)(x3x1)(xnx1)2ijn(xjxi)=1ijn(xjxi)
得证。

1.2 Vandermonde矩阵的理解

在差值(interpolation)问题中, 假设在二维空间有 n n n 个点 ( x 1 , y 1 ) , ( x 2 , y 2 ) , … , ( x n , y n ) (x_1,y_1), (x_2,y_2), \dots, (x_n,y_n) (x1,y1),(x2,y2),,(xn,yn), 希望得到一个多项式解:
p ( x ) = d n x n − 1 + d n − 1 x n − 2 + ⋯ + d 2 x + d 1 (3) p(x) = d_{n}x^{n-1} + d_{n-1}x^{n-2} + \dots + d_2x+ d_1 \tag3 p(x)=dnxn1+dn1xn2++d2x+d1(3)
这个多项式可以满足我们的当前条件:
p ( x 1 ) = y 1 = d n x 1 n − 1 + d n − 1 x 1 n − 2 + ⋯ + d 2 x 1 + d 1 p ( x 2 ) = y 2 = d n x 2 n − 1 + d n − 1 x 2 n − 2 + ⋯ + d 2 x 2 + d 1 … p ( x n ) = y n = d n x n n − 1 + d n − 1 x n n − 2 + ⋯ + d 2 x n + d 1 p(x_1) = y_1 = d_{n}x_1^{n-1} + d_{n-1}x_1^{n-2} + \dots + d_2x_1 + d_1 \\ p(x_2) = y_2 = d_{n}x_2^{n-1} + d_{n-1}x_2^{n-2} + \dots + d_2x_2 + d_1 \\ \dots \\ p(x_n) = y_n = d_{n}x_n^{n-1} + d_{n-1}x_n^{n-2} + \dots + d_2x_n + d_1 p(x1)=y1=dnx1n1+dn1x1n2++d2x1+d1p(x2)=y2=dnx2n1+dn1x2n2++d2x2+d1p(xn)=yn=dnxnn1+dn1xnn2++d2xn+d1
实际上, 可以推演如下:
y = [ y 1 y 2 ⋮ y n ] = [ d 1 + d 2 x 1 + d 3 x 1 2 ⋯ + d n x 1 n − 1 d 1 + d 2 x 2 + d 3 x 2 2 ⋯ + d n x 2 n − 1 ⋮ d 1 + d 2 x n + d 3 x n 2 ⋯ + d n x n n − 1 ] = [ 1 1 1 … 1 x 1 1 x 2 1 x 2 1 … x n 1 x 1 2 x 2 2 x 2 2 … x n 2 ⋮ ⋮ ⋮ ⋱ ⋮ x 1 n − 1 x 2 n − 1 x 2 n − 1 … x n n − 1 ] T ∗ [ 1 d 2 ⋮ d n ] = A T ∗ [ y 1 y 2 ⋮ y n ] (4) y = \left[ \begin{matrix} y_1 \\ y_2 \\ \vdots \\ y_n \end{matrix} \right] = \left[ \begin{matrix} d_1 + d_2x_1 + d_3x_1^2 \dots + d_nx_1^{n - 1} \\ d_1 + d_2x_2 + d_3x_2^2 \dots + d_nx_2^{n - 1} \\ \vdots \\ d_1 + d_2x_n + d_3x_n^2 \dots + d_nx_n^{n - 1} \\ \end{matrix} \right] \\ = \left[ \begin{matrix} 1 &1 & 1 &\dots & 1 \\ x_1^1 & x_2^1 &x_2^1 & \dots &x_n^1 \\ x_1^2 & x_2^2 &x_2^2 & \dots &x_n^2 \\ \vdots & \vdots &\vdots & \ddots &\vdots \\ x_1^{n-1} & x_2^{n-1} &x_2^{n-1} & \dots &x_n^{n-1} \\ \end{matrix} \right]^\mathrm{T} * \left[ \begin{matrix} _1 \\ d_2 \\ \vdots \\ d_n \end{matrix} \right] = \mathrm{A^\mathrm{T} } * \left[ \begin{matrix} y_1 \\ y_2 \\ \vdots \\ y_n \end{matrix} \right] \tag4 y=y1y2yn=d1+d2x1+d3x12+dnx1n1d1+d2x2+d3x22+dnx2n1d1+d2xn+d3xn2+dnxnn1=1x11x12x1n11x21x22x2n11x21x22x2n11xn1xn2xnn1T1d2dn=ATy1y2yn(4)
还可以表示如下:
[ d 1 d 2 ⋮ d n ] = ( A T ) − 1 y (5) \left[ \begin{matrix} d_1 \\ d_2 \\ \vdots \\ d_n \end{matrix} \right] = (\mathrm{A^T})^{-1} y \tag5 d1d2dn=(AT)1y(5)
x 1 = 1 , x 2 = 2 , x 3 = 3 , … , x n = n x_1 = 1, x_2 = 2, x_3 = 3, \dots, x_n = n x1=1,x2=2,x3=3,,xn=n, 则有:
A = [ 1 1 1 … 1 1 2 3 … n 1 2 2 2 3 2 … n 2 ⋮ ⋮ ⋮ ⋱ ⋮ 1 n − 1 2 n − 1 3 n − 1 … n n − 1 ] (6) \mathrm{A} = \left[ \begin{matrix} 1 &1 & 1 &\dots & 1 \\ 1 & 2 &3 & \dots &n \\ 1^2 & 2^2 &3^2 & \dots &n^2 \\ \vdots & \vdots &\vdots & \ddots &\vdots \\ 1^{n-1} & 2^{n-1} &3^{n-1} & \dots &n^{n-1} \\ \end{matrix} \right] \tag6 A=11121n112222n113323n11nn2nn1(6)

1.3 Vandermonde矩阵在最小二乘拟合中的应用

假设对 n n n个采样点进行拟合,那么方差可以表示为:

R 2 = ∑ i = 1 n [ y i − ( d 1 + d 2 x i + d 3 x i 2 ⋯ + d n x i n − 1 ) ] 2 R^2 = \sum_{i=1}^{n}[y_i - (d_1 + d_2x_i + d_3x_i^2 \dots + d_nx_i^{n - 1})]^2 R2=i=1n[yi(d1+d2xi+d3xi2+dnxin1)]2

为求得方差的最小值,对 d 1 , … , d n d_1, \dots, d_n d1,,dn求偏导:
∂ ( R 2 ) ∂ ( d 1 ) = − 2 ∑ i = 1 n [ y − ( d 1 + d 2 x + d 3 x 2 ⋯ + d n x n − 1 ) ] = 0 ∂ ( R 2 ) ∂ ( d 2 ) = − 2 ∑ i = 1 n [ y − ( d 1 + d 2 x + d 3 x 2 ⋯ + d n x n − 1 ) ] x = 0 … ∂ ( R 2 ) ∂ ( d n ) = − 2 ∑ i = 1 n [ y − ( d 1 + d 2 x + d 3 x 2 ⋯ + d n x n − 1 ) ] x n − 1 = 0 \frac{\partial(R^2)}{\partial(d_1)} = -2\sum_{i=1}^{n}[y - (d_1 + d_2x + d_3x^2 \dots + d_nx^{n - 1})] = 0\\ \frac{\partial(R^2)}{\partial(d_2)} = -2\sum_{i=1}^{n}[y - (d_1 + d_2x + d_3x^2 \dots + d_nx^{n - 1})]x = 0 \\ \dots \\ \frac{\partial(R^2)}{\partial(d_n)} = -2\sum_{i=1}^{n}[y - (d_1 + d_2x + d_3x^2 \dots + d_nx^{n - 1})]x^{n-1} = 0 \\ (d1)(R2)=2i=1n[y(d1+d2x+d3x2+dnxn1)]=0(d2)(R2)=2i=1n[y(d1+d2x+d3x2+dnxn1)]x=0(dn)(R2)=2i=1n[y(d1+d2x+d3x2+dnxn1)]xn1=0
移项:
d 1 n + d 2 ∑ i = 1 n x i + d 3 ∑ i = 1 n x i 2 + ⋯ + d n ∑ i = 1 n x i n − 1 ) ] = ∑ i = 1 n y i d 1 ∑ i = 1 n x i + d 2 ∑ i = 1 n x i 2 + d 3 ∑ i = 1 n x i 3 + ⋯ + d n ∑ i = 1 n x i n ) ] = ∑ i = 1 n x i y i … d 1 ∑ i = 1 n x i n − 1 + d 2 ∑ i = 1 n x i n + d 3 ∑ i = 1 n x i n + 1 + ⋯ + d n ∑ i = 1 n x i 2 n − 2 = ∑ i = 1 n x i n − 1 y i d_1n + d_2\sum_{i=1}^{n}x_i + d_3\sum_{i=1}^{n}x_i^2 + \dots + d_n\sum_{i=1}^{n}x_i^{n - 1})] = \sum_{i=1}^{n}y_i \\ d_1\sum_{i=1}^{n}x_i + d_2\sum_{i=1}^{n}x_i^2 + d_3\sum_{i=1}^{n}x_i^3 + \dots + d_n\sum_{i=1}^{n}x_i^{n})] = \sum_{i=1}^{n}x_iy_i \\ \dots \\ d_1\sum_{i=1}^{n}x_i^{n-1} + d_2\sum_{i=1}^{n}x_i^{n} + d_3\sum_{i=1}^{n}x_i^{n+1} + \dots + d_n\sum_{i=1}^{n}x_i^{2n-2} = \sum_{i=1}^{n}x_i^{n-1}y_i \\ d1n+d2i=1nxi+d3i=1nxi2++dni=1nxin1)]=i=1nyid1i=1nxi+d2i=1nxi2+d3i=1nxi3++dni=1nxin)]=i=1nxiyid1i=1nxin1+d2i=1nxin+d3i=1nxin+1++dni=1nxi2n2=i=1nxin1yi
用矩阵表示如下
[ n ∑ i = 1 n x i ∑ i = 1 n x i 2 … ∑ i = 1 n x i n − 1 ∑ i = 1 n x i ∑ i = 1 n x i 2 ∑ i = 1 n x i 3 … ∑ i = 1 n x i n ⋮ ⋮ ⋮ ⋱ ⋮ ∑ i = 1 n x i n − 1 ∑ i = 1 n x i n ∑ i = 1 n x i n + 1 … ∑ i = 1 n x i 2 n − 2 ] ∗ [ d 1 d 2 ⋮ d n ] = [ 1 1 1 … 1 x 1 1 x 2 1 x 2 1 … x n 1 x 1 2 x 2 2 x 2 2 … x n 2 ⋮ ⋮ ⋮ ⋱ ⋮ x 1 n − 1 x 2 n − 1 x 2 n − 1 … x n n − 1 ] ∗ [ y 1 y 2 ⋮ y n ] \left[ \begin{matrix} n &\sum_{i=1}^{n}x_i & \sum_{i=1}^{n}x_i^2 &\dots & \sum_{i=1}^{n}x_i^{n-1} \\ \sum_{i=1}^{n}x_i & \sum_{i=1}^{n}x_i^2 & \sum_{i=1}^{n}x_i^3 & \dots & \sum_{i=1}^{n}x_i^{n} \\ \vdots & \vdots &\vdots & \ddots &\vdots \\ \sum_{i=1}^{n}x_i^{n-1} & \sum_{i=1}^{n}x_i^n & \sum_{i=1}^{n}x_i^{n+1} & \dots & \sum_{i=1}^{n}x_i^{2n-2} \\ \end{matrix} \right] * \left[ \begin{matrix} d_1 \\ d_2 \\ \vdots \\ d_n \end{matrix} \right] = \\ \left[ \begin{matrix} 1 &1 & 1 &\dots & 1 \\ x_1^1 & x_2^1 &x_2^1 & \dots &x_n^1 \\ x_1^2 & x_2^2 &x_2^2 & \dots &x_n^2 \\ \vdots & \vdots &\vdots & \ddots &\vdots \\ x_1^{n-1} & x_2^{n-1} &x_2^{n-1} & \dots &x_n^{n-1} \\ \end{matrix} \right] * \left[ \begin{matrix} y_1 \\ y_2 \\ \vdots \\ y_n \end{matrix} \right] ni=1nxii=1nxin1i=1nxii=1nxi2i=1nxini=1nxi2i=1nxi3i=1nxin+1i=1nxin1i=1nxini=1nxi2n2d1d2dn=1x11x12x1n11x21x22x2n11x21x22x2n11xn1xn2xnn1y1y2yn
对其进行变形,得到:
[ 1 1 1 … 1 x 1 1 x 2 1 x 2 1 … x n 1 x 1 2 x 2 2 x 2 2 … x n 2 ⋮ ⋮ ⋮ ⋱ ⋮ x 1 n − 1 x 2 n − 1 x 2 n − 1 … x n n − 1 ] [ 1 1 1 … 1 x 1 1 x 2 1 x 2 1 … x n 1 x 1 2 x 2 2 x 2 2 … x n 2 ⋮ ⋮ ⋮ ⋱ ⋮ x 1 n − 1 x 2 n − 1 x 2 n − 1 … x n n − 1 ] T [ d 1 d 2 ⋮ d n ] = [ 1 1 1 … 1 x 1 1 x 2 1 x 2 1 … x n 1 x 1 2 x 2 2 x 2 2 … x n 2 ⋮ ⋮ ⋮ ⋱ ⋮ x 1 n − 1 x 2 n − 1 x 2 n − 1 … x n n − 1 ] [ y 1 y 2 ⋮ y n ] \left[ \begin{matrix} 1 &1 & 1 &\dots & 1 \\ x_1^1 & x_2^1 &x_2^1 & \dots &x_n^1 \\ x_1^2 & x_2^2 &x_2^2 & \dots &x_n^2 \\ \vdots & \vdots &\vdots & \ddots &\vdots \\ x_1^{n-1} & x_2^{n-1} &x_2^{n-1} & \dots &x_n^{n-1} \\ \end{matrix} \right] \left[ \begin{matrix} 1 &1 & 1 &\dots & 1 \\ x_1^1 & x_2^1 &x_2^1 & \dots &x_n^1 \\ x_1^2 & x_2^2 &x_2^2 & \dots &x_n^2 \\ \vdots & \vdots &\vdots & \ddots &\vdots \\ x_1^{n-1} & x_2^{n-1} &x_2^{n-1} & \dots &x_n^{n-1} \\ \end{matrix} \right]^{\mathrm{T}} \left[ \begin{matrix} d_1 \\ d_2 \\ \vdots \\ d_n \end{matrix} \right]=\\ \left[ \begin{matrix} 1 &1 & 1 &\dots & 1 \\ x_1^1 & x_2^1 &x_2^1 & \dots &x_n^1 \\ x_1^2 & x_2^2 &x_2^2 & \dots &x_n^2 \\ \vdots & \vdots &\vdots & \ddots &\vdots \\ x_1^{n-1} & x_2^{n-1} &x_2^{n-1} & \dots &x_n^{n-1} \\ \end{matrix} \right] \left[ \begin{matrix} y_1 \\ y_2 \\ \vdots \\ y_n \end{matrix} \right] 1x11x12x1n11x21x22x2n11x21x22x2n11xn1xn2xnn11x11x12x1n11x21x22x2n11x21x22x2n11xn1xn2xnn1Td1d2dn=1x11x12x1n11x21x22x2n11x21x22x2n11xn1xn2xnn1y1y2yn
上面的公式可以简写为:
A A T d = A y \mathrm{A}\mathrm{A}^\mathrm{T}d = \mathrm{A}y AATd=Ay
A \mathrm{A} A为竖着的范德蒙矩阵。此处向量 d d d等于:
d = ( A A T ) − 1 A y d = (\mathrm{A}\mathrm{A}^\mathrm{T})^{-1} \mathrm{A}y d=(AAT)1Ay

1.4 基于Vandermonde的编码

编码矩阵就是单位矩阵和范德蒙德矩阵的组合。输入数据(D)和编码矩阵的乘积就是编码后的数据。
[ 1 0 0 … 0 0 1 0 … 0 ⋮ ⋮ ⋮ ⋱ ⋮ 0 0 0 … 1 1 1 1 … 1 1 2 3 … n 1 2 2 2 3 2 … n 2 ⋮ ⋮ ⋮ ⋱ ⋮ 1 n − 1 2 n − 1 3 n − 1 … n n − 1 ] [ d 1 d 2 ⋮ d n ] = [ d 1 d 2 ⋮ d n c 1 c 2 ⋮ c m ] (7) \left[ \begin{matrix} 1 &0 & 0 &\dots & 0 \\ 0 &1 & 0 &\dots & 0 \\ \vdots & \vdots &\vdots & \ddots &\vdots \\ 0 &0 & 0 &\dots & 1 \\ 1 &1 & 1 &\dots & 1 \\ 1 & 2 &3 & \dots &n \\ 1^2 & 2^2 &3^2 & \dots &n^2 \\ \vdots & \vdots &\vdots & \ddots &\vdots \\ 1^{n-1} & 2^{n-1} &3^{n-1} & \dots &n^{n-1} \\ \end{matrix} \right] \left[ \begin{matrix} d_1 \\ d_2 \\ \vdots \\ d_n \end{matrix} \right] = \left[ \begin{matrix} d_1 \\ d_2 \\ \vdots \\ d_n \\ c_1 \\ c_2 \\ \vdots \\ c_m \\ \end{matrix} \right] \tag7 10011121n101012222n100013323n10011nn2nn1d1d2dn=d1d2dnc1c2cm(7)
算法复杂度: 采用这种方法的算法复杂度还是比较高的,编码复杂度为 O ( m n ) O(mn) O(mn),其中 m m m为校验数据个数, n n n为输入数据个数。解码复杂度为 O ( n 3 ) O(n^3) O(n3)

2 柯西( Cauchy)矩阵

柯西矩阵的任意一个子方阵都是奇异矩阵,存在逆矩阵。而且柯西矩阵在迦罗华域上的求逆运算,可以在 O ( n 2 ) O(n^2) O(n2)的运算复杂度内完成。
使用柯西矩阵,比范德蒙德矩阵的优化主要有两点:

  1. 降低了矩阵求逆的运算复杂度。范德蒙矩阵求逆运算的复杂度为 O ( n 3 ) O(n^3) O(n3),而柯西矩阵求逆运算的复杂度仅为 O ( n 2 ) O(n^2) O(n2)
  2. 通过有限域转换,将GF( 2 w 2^w 2w)域中的元素转换成二进制矩阵,将乘法转换为逻辑与,降低了乘法运算复杂度。(二进制的加法即XOR,乘法即AND)

2.1 柯西( Cauchy)矩阵介绍

柯西矩阵的描述如下:
[ 1 x 0 + y 0 1 x 0 + y 1 1 x 0 + y 2 … 1 x 0 + y n 1 x 1 + y 0 1 x 1 + y 1 1 x 1 + y 2 … 1 x 1 + y n 1 x 2 + y 0 1 x 2 + y 1 1 x 2 + y 2 … 1 x 2 + y n ⋮ ⋮ ⋮ ⋱ ⋮ 1 x m + y 0 1 x m + y 1 1 x m + y 2 … 1 x m + y n ] (8) \left[ \begin{matrix} \frac{1}{x_0 + y_0} &\frac{1}{x_0 + y_1} & \frac{1}{x_0 + y_2} &\dots & \frac{1}{x_0 + y_n} \\ \frac{1}{x_1 + y_0} &\frac{1}{x_1 + y_1} & \frac{1}{x_1 + y_2} &\dots & \frac{1}{x_1 + y_n} \\ \frac{1}{x_2 + y_0} &\frac{1}{x_2 + y_1} & \frac{1}{x_2 + y_2} &\dots & \frac{1}{x_2 + y_n} \\ \vdots & \vdots &\vdots & \ddots &\vdots \\ \frac{1}{x_m + y_0} &\frac{1}{x_m + y_1} & \frac{1}{x_m + y_2} &\dots & \frac{1}{x_m + y_n} \\ \end{matrix} \right] \tag8 x0+y01x1+y01x2+y01xm+y01x0+y11x1+y11x2+y11xm+y11x0+y21x1+y21x2+y21xm+y21x0+yn1x1+yn1x2+yn1xm+yn1(8)
x i x_i xi y i y_i yi 都是迦罗华域GF( 2 w 2^w 2w)中的元素。
基于柯西矩阵的编码矩阵为:
[ 1 0 0 … 0 0 1 0 … 0 ⋮ ⋮ ⋮ ⋱ ⋮ 0 0 0 … 1 1 x 0 + y 0 1 x 0 + y 1 1 x 0 + y 2 … 1 x 0 + y n 1 x 1 + y 0 1 x 1 + y 1 1 x 1 + y 2 … 1 x 1 + y n 1 x 2 + y 0 1 x 2 + y 1 1 x 2 + y 2 … 1 x 2 + y n ⋮ ⋮ ⋮ ⋱ ⋮ 1 x m + y 0 1 x m + y 1 1 x m + y 2 … 1 x m + y n ] [ d 1 d 2 ⋮ d n ] = [ d 1 d 2 ⋮ d n c 1 c 2 ⋮ c m ] (9) \left[ \begin{matrix} 1 &0 & 0 &\dots & 0 \\ 0 &1 & 0 &\dots & 0 \\ \vdots & \vdots &\vdots & \ddots &\vdots \\ 0 &0 & 0 &\dots & 1 \\ \frac{1}{x_0 + y_0} &\frac{1}{x_0 + y_1} & \frac{1}{x_0 + y_2} &\dots & \frac{1}{x_0 + y_n} \\ \frac{1}{x_1 + y_0} &\frac{1}{x_1 + y_1} & \frac{1}{x_1 + y_2} &\dots & \frac{1}{x_1 + y_n} \\ \frac{1}{x_2 + y_0} &\frac{1}{x_2 + y_1} & \frac{1}{x_2 + y_2} &\dots & \frac{1}{x_2 + y_n} \\ \vdots & \vdots &\vdots & \ddots &\vdots \\ \frac{1}{x_m + y_0} &\frac{1}{x_m + y_1} & \frac{1}{x_m + y_2} &\dots & \frac{1}{x_m + y_n} \\ \end{matrix} \right] \left[ \begin{matrix} d_1 \\ d_2 \\ \vdots \\ d_n \end{matrix} \right] = \left[ \begin{matrix} d_1 \\ d_2 \\ \vdots \\ d_n \\ c_1 \\ c_2 \\ \vdots \\ c_m \\ \end{matrix} \right] \tag9 100x0+y01x1+y01x2+y01xm+y01010x0+y11x1+y11x2+y11xm+y11000x0+y21x1+y21x2+y21xm+y21001x0+yn1x1+yn1x2+yn1xm+yn1d1d2dn=d1d2dnc1c2cm(9)

2.2 柯西编解码过程优化

在范德蒙编码的时候,我们可以采用对数/反对数表的方法,将乘法运算转换成了加法运算,并且在迦罗华域中,加法运算转换成了XOR运算。
柯西编解码为了降低乘法复杂度,采用了有限域上的元素都可以使用二进制矩阵表示的原理,将乘法运算转换成了迦罗华域“AND运算”和“XOR逻辑运算”,提高了编解码效率。
从数学的角度来看,在迦罗华有限域中,任何一个GF( 2 w 2^w 2w)域上的元素都可以映射到GF(2)二进制域,并且采用一个二进制矩阵的方式表示GF( 2 w 2^w 2w)中的元素。
例如,GF( 2 3 2^3 23)域中的元素可以表示成GF(2)域中的二进制矩阵:

上图中,黑色方块表示逻辑1,白色方块表示逻辑0。通过这种转换,GF( 2 w 2^w 2w)域中的阵列就可以转换成GF(2)域中的二进制阵列。生成矩阵的阵列转换表示如下:

在GF( 2 w 2^w 2w)域中的编码矩阵为 k ∗ ( k + m ) k *(k+m) k(k+m),转换到GF(2)域中,使用二进制矩阵表示,编码矩阵变成了 ( w ∗ k ) ∗ ( w ∗ ( k + m ) ) (w*k)* (w*(k+m)) (wk)(w(k+m))二进制矩阵。

采用域转换的目的是简化GF( 2 w 2^w 2w)域中的乘法运算。在GF(2)域中,乘法运算变成了逻辑与运算,加法运算变成了XOR运算,可以大大降低运算复杂度。
和范德蒙编解码中提到的对数/反对数方法相比,这种方法不需要构建对数/反对数表,可以支持 w w w为很大的GF域空间。采用这种有限域转换的方法之后,柯西编码运算可以表示如下:

算法复杂度:使用柯西矩阵要优于范德蒙德矩阵的方法,柯西矩阵的运算复杂度为 O ( n ∗ ( n − m ) ) O(n *(n - m)) O(n(nm)),解码复杂度为 O ( n 2 ) O(n^2) O(n2)

2.3 参数 w w w影响

选择GF( 2 w 2^w 2w)中的 w w w参数,需要满足 k + n ≤ 2 w k+n \leq 2^w k+n2w
对于柯西矩阵的RS编码,还需要满足coding Block size % ( w w w * packet) == 0。(具体参数设置和意义见 Jerasure实现,jerasure开源实现。)
关于Erasure Code,有一个开源的实现Jerasure,是由James S. Plank教授开发。还有一个开源项目FECpp,也是关于EC code的。地址为:FECpp 开源实现

3 RS编码升级

RS编码后的数据,如果丢失了一块,恢复丢失的数据需要最少读取n块数据。在生产环境中,硬盘故障经常发生,恢复数据对网络IO和CPU都会有较大的消耗。
因此有些公司在EC编码的基础上做了一些改进,使用LRC或SEC替换RS编码。

3.1 LRC - Locally Repairable Code 本地副本存储

LRC编码与RS编码方式基本相同,同时增加了额外的数据块副本。
LRC编码本质上是RS编码+2副本备份。
LRC编码步骤如下:

  1. 对原始数据使用RS编码,例如编码为4:2,编码结果为4个数据块:D1、D2、D3、D4,2个编码块C1、C2;
  2. 原始数据做2副本,将4个数据块的前2个数据块和后2个数据块,分别生成2个编码块,即R1=D1D2,R2=D3D4;
  3. 如果某一个数据块丢失,例如D2丢失,则只需要R1和D1即可恢复D2;

LRC的编码矩阵中增加了步骤2的2副本编码,样子如下:

LRC增加了副本数量,使用了更多的存储空间,来减少恢复数据块时读取块的数量,节省恢复数据使用的网络IO和CPU。

3.2 SEC - Sparse Erasure Code 稀疏纠删码

LRC编码中只对数据块做了2副本,当编码块丢失时,仍然需要读取n块数据来重新计算编码块。
SEC编码中对数据块和编码块都做增加了校验块。
SEC编码本质上是RS编码+奇偶校验块。
SEC编码步骤如下:

  1. 对原始数据使用RS编码,例如编码为4:2,编码结果为4个数据块:D1、D2、D3、D4,2个编码块C1、C2;
  2. 生成D1D2的校验块X1,D3D4的校验块X2,C1C2的校验块X3;
  3. 当数据块或编码块中的某一个丢失时,例如C2丢失,通过C1和校验块X3即可恢复C2;

SEC同样通过增加存储块,减少了恢复数据是的网络和CPU开销。

4 FEC 介绍

在信息中按照某种规则加上一定的冗余位,构成一个码字,称为差错控制编码过程。
在接收端接收到码字,或从存储设备中读取码字后,查看信息位和冗余位,并检查他们之间的关系是否正确,以确定是否有差错发生,称为校验。
Forward Error Correction,FEC- 前向纠错编码技术通过在传输码列中加入冗余纠错码,在一定条件下,通过解码可以自动纠正传输误码。这种编码的译码设备较复杂。

除FEC之外,还有两种差错控制编码:Automatic repeat request(ARQ)检错重发(或自动请求重传),Hybrid Error Correction(HEC)混合纠错。

检错重发由发送端送出能够发现错误的码,接收端如果发现错误,通过反向信道把这一判决结果反馈给发送端。然后,发送端把接收端认为错误的信息再次重发。其特点是需要反馈信道,译码设备简单。

混合纠错是 ARQ和 FEC方式的混合。发送端同时送出具有检错和纠错能力的码,如果接收端收到信码在纠错能力以内,则自动进行纠正。如果超出纠错能力,则经过反馈信道请求发送端重发。

参考

  1. A Tutorial on Reed-Solomon Coding for Fault-Tolerance in RAID-like Systems
  2. Optimizing Cauchy Reed-Solomon Codes for Fault-Tolerant Storage Applications
  3. Tutorial: Erasure Coding for Storage Applications
  4. http://jerasure.org/jerasure/jerasure
  5. http://jerasure.org/jerasure/gf-complete
  6. http://alanwu.blog.51cto.com/3652632/1406312
  7. http://alanwu.blog.51cto.com/3652632/1410132
  8. http://blog.163.com/yandong_8212/blog/static/13215391420143281143547/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HenrySmale

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值