-2

For example, I have a three variables: "x", "y" and "z". They are all from certain range {min, max}. Now, I want to compute a new variable, let's say p = x + F*(y-z), where F is some constant between 0 and 1. This new computed variable "p" needs to be mapped into above {min, max} range. How do I do that?

EDIT 1

Generating numbers into array:

population[D*id]=0;
    population[D*id+N]=0;
    population[D*id +2*N]=0;

    population[D*id+1]=rndFloat(globalState,threadIdx.x,4);
    population[D*id+N+1]=0;
    population[D*id +2*N+1]=0;

    for(int i=2; i<N; i++){
        float min= -4 - 1/4*abs((int)((i-4)/3));
        float max= 4 + 1/4*abs((int)((i-4)/3));
        if(i==2)
        {
            population[D*id+2]=rndFloat(globalState,threadIdx.x,3.14159265359);
            population[D*id+N+2]=rndFloat(globalState,threadIdx.x,min,max);
            population[D*id +2*N+2]=0;
        }
        else
        {
            population[D*id +i]=rndFloat(globalState,threadIdx.x,min,max);
            population[D*id+N+i]=rndFloat(globalState,threadIdx.x,min,max);
            population[D*id +2*N+i]=rndFloat(globalState,threadIdx.x,min,max);
        }
    }

Computing a new variable:

for(int i=0; i<D-1; i++)
    {
        pop[D*id+i]= population[D*a +i] + F*(population[D*b +i]-population[D*c +i]);
    }

Indices a, b and c are picked randomly. The important thing to notice is min and max range and its dependance by given indices:

float min= -4 - 1/4*abs((int)((i-4)/3));
        float max= 4 + 1/4*abs((int)((i-4)/3));

where i is replaced by an a, b and c for each range.

EDIT 2

to simplify, let's just say that there are 3 variables x, y and z which are in the certain range. Each variable has its own range. I want to compute new variable p = x + F*(y-z) and it needs to be mapped appropriately into its own range. How do I do that?

user755974
  • 49
  • 6
  • Can you be more precise? Do you want to add p to the existing range/array? What is the code you already have? – RST Jun 21 '13 at 17:32
  • 3
    The question is you asked it can't be answered because it's very unclear. What do you mean when you say "p needs to be mapped"? What kind of mapping do you want? – interjay Jun 21 '13 at 17:33
  • 1
    It's called [linear interpolation](http://en.wikipedia.org/wiki/Linear_interpolation). If you don't know maths (**Why?** Programmers need maths!), then try coming up with the solution with the help of your brain cells. Just a little bit of effort, and you'll find out quickly that `new_x = new_min + (old_x - old_min) / (old_max - old_min) * (new_max - new_min)`. –  Jun 21 '13 at 17:41
  • it's a little more complicated than that. I have just edited the question. My apologies for unspecified question. – user755974 Jun 21 '13 at 18:08
  • @H2CO3: That formula maps an x into another value. The question asks to map an x, y, and z into one value. – Eric Postpischil Jun 21 '13 at 19:26

2 Answers2

0

If you just want to take some variable x which is in the range [A, B] and scale it to a different range [C, D], then you should note that:

  1. x - A is in the range [0, B - A].
  2. Scaling a number in the range [0, B - A] is effectively the same problem being solved in this question: How do I scale down numbers from rand()?.

Note that if you use some really naieve technique like the modulus operator, you will have a non-uniform distribution in your mappings. The linked question contains some reasonable techniques for achieving a mostly-uniform mapping.

In your case, p is in the range [min - max + min, max + max - min].

Community
  • 1
  • 1
CmdrMoozy
  • 3,870
  • 3
  • 19
  • 31
  • it's a little more complicated than that. I have just edited the question. My apologies for unspecified question. – user755974 Jun 21 '13 at 18:08
  • I think this solution will still work for you, you just need to compute the range of `p` if `x`, `y` and `z` all have different ranges. Generally, if `p = x + F * (y - z)`, and `F` is in `[0, 1]`, then `p` is in `[MIN(x), MAX(x) + MAX(y) - MIN(z)]`. Note that I'm using `[` and `]`, indicating that the ranges are **inclusive**. – CmdrMoozy Jun 21 '13 at 18:47
  • @CmdrMoozy Not sure your potential range for `p` is quite right - consider counter example: `min = 1`, `max = 10`, `x = 1`, `y = 1`, `z = 10`, `F = 1` -> `p = -8`, which is rather less than min... – twalberg Jun 21 '13 at 19:00
  • Ah, of course, it for some reason didn't occur to me that `y - z < 0` could occur. The correct range should be more like `[MIN(x) - MAX(y) + MIN(z), MAX(x) + MAX(y) - MIN(z)]` – CmdrMoozy Jun 21 '13 at 19:18
  • I think MIN(p) and MAX(p) also needs to be somehow involved. Again, I know all ranges for all variables: x,y,z and p. When computing p, does not mean that will fall appropriately into it's own range. Indices for x, y, z and p are different, so the ranges are also computed differently. – user755974 Jun 21 '13 at 19:33
  • The range of `p` can be expressed in terms of the ranges of `x` `y` and `z` since `p` is defined as a linear function in terms of those variables. – CmdrMoozy Jun 24 '13 at 16:34
0

It depends what your min and max values are. For example, to map p between 0 and 1:

if (p < 10)
    p /= 9.0f;

else if (p < 100)
    p /= 99.0f;

else if (p < 1000)
    p /= 999.0f;

And so on...

Oleksiy
  • 37,477
  • 22
  • 74
  • 122
  • 2
    Another way to map `p` between 0 and 1 is `p = 0.5`. I don't see how what you're doing is any more useful. – interjay Jun 21 '13 at 17:48