dimanche 12 juin 2016

Editing array to ensure strictly increasing values


Consider a sorted vector x that is bounded between min and max. Below is an example of such x where min could be 0 and max could be 12:

x = c(0.012, 1, exp(1), exp(1)+1e-55, exp(1)+1e-10,
       exp(1)+1e-3, 3.3, 3.33333, 3.333333333333333, 3+1/3, 5, 5, 10, 12)

5 and 5 as well as exp(1) and exp(1)+10^(-55) have exactly the same value (to the level of accuracy of a float number). Some other entry differ largely and some others differ only by a small amount. I would like to consider an approximation to equality test

ApproxEqual = function(a,b) abs(a-b) < epsilon

, where epsilon could be 1e-5 for example.

Goal

I would like to modify the values of the variable x "as little as possible" to ensure that no two values in x are "approximatively equal" and x is still bounded between min and max.

I am happy to let you decide what "as little as possible" really mean. One could for example minimize the sum of square deviations between the original x and the expected variable output.

Example 1

x_input = c(5, 5.1, 5.1, 5.1, 5.2)
min=1
max=100

x_output = c(5, 5.1-epsilon, 5.1, 5.1+epsilon, 5.2)

Example 2

x_input = c(2,2,2,3,3)
min=2
max=3

x_output = c(2, 2+epsilon, 2+2*epsilon, 2+3*epsilon, 3-epsilon,3)

Of course, in the above case if (3-epsilon) - (2+3*epsilon) < epsilon is TRUE, then the function should throw an error as the problem has no solution.

Side Note

I would love if the solution is quite performant. answer could make could use of Rcpp for example.


Aucun commentaire:

Enregistrer un commentaire