我正在尝试使用以下公式为N * N图像实现Arnold的Cat图
for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { desMatrix[(i + j) % N][(i + 2 * j) % N] = srcMatrix[i][j]; } }要反转我做的过程:
for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { srcMatrix[(j-i) % N][(2*i-j) % N] = destMatrix[i][j]; } }实施是否正确?
在我看来,对于j和i的某些值,我可能从(ji)和(2 * ij)获得负指数; 我应该如何处理这些情况,因为矩阵指数只是正数?
I'm trying to implement Arnold's Cat map for N*N images using the following formula
for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { desMatrix[(i + j) % N][(i + 2 * j) % N] = srcMatrix[i][j]; } }To invert the process I do:
for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { srcMatrix[(j-i) % N][(2*i-j) % N] = destMatrix[i][j]; } }Is the implementation correct?
It seems to me that for certain values of j and i I might get negative indexes from (j-i) and (2*i-j); how should I handle those cases, since matrix indexes are only positive?
最满意答案
通常,当模数(%)操作需要处理负索引时,您可以根据需要简单地添加模数参数。 以来
x % N == ( x + a*N ) % N对于所有自然a,并且在这种情况下你有i和j约束在[0,N],那么你可以写(N + i - j)并确保即使我是0而j是N-1(甚至是N就此问题而言,结果总是非负的。 出于同样的原因,(2 * N + i - 2 * j)或等效地(i + 2 *(Nj))总是非负的。
但是,在这种情况下,这不是必需的。 要反转地图,您将重复前进步骤以反转分配 。 由于矩阵具有一元行列式并且是面积保持的,因此您可以确保最终获得所有积分(即覆盖M(i + 1)将产生M(i)的覆盖)。
for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { newMatrix[i][j] = desMatrix[(i + j) % N][(i + 2 * j) % N]; } }在这一点上,newMatrix和srcMatrix应该完全相同。
(实际上,你已经在运行逆向转换作为前向转换了。我设置的反向转换是前向转换的一种常用形式)。
In general, when a modulo (%) operation needs to work on negative indexes, you can simply add the modulo argument as many times as it's needed. Since
x % N == ( x + a*N ) % Nfor all natural a's, and in this case you have i and j constrained in [0, N), then you can write (N + i - j) and ensure that even if i is 0 and j is N-1 (or even N for that matter), the result will always be non-negative. By the same token, (2*N + i - 2*j) or equivalently (i + 2*(N-j)) is always non-negative.
In this case, though, this is not necessary. To invert your map, you would repeat the forward step reversing the assignments. Since the matrix has unary determinant and is area-preserving, you're assured that you'll get all your points eventually (i.e. covering M(i+1) will yield a covering of M(i)).
for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { newMatrix[i][j] = desMatrix[(i + j) % N][(i + 2 * j) % N]; } }At this point newMatrix and srcMatrix ought to be identical.
(Actually, you're already running the reverse transformation as your forward one. The one I set up to reverse yours is the one commonly used form for the forward transformation).
更多推荐
发布评论