开发者

Algorithm for functions permutating integers

I want to write some functions as follows

y = f(x) and another function,

x = g(y) that acts as a reversible, where

y = f(g(y)) and where x and y are permutated integers.

For very simple example in the range of integers in 0 to 10 it would look like this:

0->1  
1->2  
2->3  
...  
9->10  
10->0 

but this is the simplest method by adding 1 and reversing by subtracting 1.

I want to have a more sofisticated algorithm that can do the following,

234927773-&开发者_运维问答gt;4299  
34->33928830  
850033->23234243423  

but the reverse can be obtained by conversion

The solution could be obtained with a huge table storing pairs of unique integers but this will not be correct. This must be a function.


You could just XOR.

y = x XOR p
x = y XOR p

Though not my area of expertise, I think that cryptography should provide some valuable answers to your question.


If the domain of your permutation is a power of 2, you can use any block cipher: 'f' is encryption with a specific key, and 'g' is decryption with the same key. If your domain is not a power of 2, you can probably still use a block cipher: see this article.


You could use polynomial interpolation methods to interpolate a function one way, then do reverse interpolation to find the inverse function.

Here is some example code in MATLAB:

function [a] = Coef(x, y)
    n = length(x);

    a = y;
    for j = 2:n
        for i = n:-1:j
            a(i) = (a(i) - a(i-1)) / (x(i) - x(i-j+1));
        end
    end
end

function [val] = Eval(x, a, t)
    n = length(x);

    val = a(n);
    for i = n-1:-1:1
       val = a(i) + val*(t-x(i)); 
    end
end

It builds a Divided Difference table and evaluates a function based on Newtons Interpolation.

Then if your sets of points are x, and y (as vectors of the same length, where x(i) matches to y(i), your forward interpolation function at value n would be Eval(x, Coef(x, y), n) and the reverse interpolation function would be Eval(y, Coef(y, x), n).

Depending on your language, there are probably much cleaner ways to do this, but this gets down and dirty with the maths.

Here is an excerpt from the Text Book which is used in my Numerical Methods class: Google Book Link

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜