-1

I have a function with two inputs x and y which returns z based on some conditions (which rely on the values of x and y). Something similar to the following pseudocode.

f(x, y)
    if x < a && y < b
        return z=x+y;
    else if x >= a && x < c && y >= b && y < d
        return z=x-y;
    else x >= c && y >= d
        return z=x*y;

Now I want to write a function which almost acts as the inverse function for f (Let's call it g). The pseudocode should look like something similar to this:

g(z)
    return x1, x2, y1 and y2; //x's, y's are corresponding boundaries for given z

Assume that f is very very simple and based only on a few conditions and the return value is just as simple as some arithmetic statements (exactly similar to what is provided above). How do you write g? Do you simply store boundaries and corresponding return values from f and then traverse between them with the given z to find the appropriate boundaries? I'm trying to see how other people hack this. Note that f is also written by yourself and you know everything about it.

NOTE: Assume that return values are distinct at all times and don't have any overlap in any situations

AstroCB
  • 12,337
  • 20
  • 57
  • 73
Ali Abbasinasab
  • 382
  • 3
  • 13
  • 1
    why would it return two pairs of (x, y)? – user2963623 Jul 08 '14 at 17:57
  • In some cases, e.g the second condition, it should return two pairs to distinguish the intervals. For the first and the second cases, one of the pairs are basically -infinity or +infinity. – Ali Abbasinasab Jul 08 '14 at 18:03
  • 3
    This question appears to be off-topic because it is about maths – Rachel Gallen Jul 08 '14 at 18:04
  • 2
    From the looks of it f is a many to one mapping. I doubt it will have a small number of inverses! – user2963623 Jul 08 '14 at 18:07
  • Under the assumptions you're stating: full knowledge of `f`'s implementation, nothing but arithmetic in `f`, and `f` is injective, I'd try to determine `g` by analysis rather than trying to automate the inverse function search. I strongly suspect you'll have a harder time designing the inverse search method than you will with a pen and paper analysis. – Aaron Golden Jul 08 '14 at 18:08
  • 1
    @RachelGallen there are math and algorithm sections and tags in SO! – Ali Abbasinasab Jul 08 '14 at 18:11
  • 1
    @AaronGolden I very much simplified my problem. That's why I want to know how people com up with a generic solution for these types of problems. – Ali Abbasinasab Jul 08 '14 at 18:12
  • A function of two variables usually doesn't have any inverse function. Are you trying to compute an [inverse image](http://en.m.wikipedia.org/wiki/Preimage) of a point? – n. m. could be an AI Jul 08 '14 at 18:14
  • @user3761870 there is a site devoted to maths at math.stackexchange.com – Rachel Gallen Jul 08 '14 at 18:16
  • In the general case, you can't write `g()` if `f()` can return the same value for two different calls. That is, if `f(1, 5)` and `f(4, 2)` return the same result, then what is `g(6)` supposed to return? – Jim Mischel Jul 08 '14 at 19:09
  • I guess the more pertinent question is what are you trying to reverse engineer? – Jim Mischel Jul 08 '14 at 19:10
  • There are math and algorithm tags on this site, yes. But the tag for algorithm says use it "when your issue is related to algorithm design", and the tag for math says your question "should be programming related". Since your question is about how to find an inverse, it is better suited to math.stackexchange.com. If it were about translating the inverse to code, then the math tag would be appropriate. As it is, the algorithm and hacking tags have nothing to do with your question. – Teepeemm Jul 08 '14 at 19:26
  • @user3761870 added answer, I am not good at math but just a silly math note your z=f(x,y) function map many x,y pairs to single z which means inverse x,y=g(z) is not function but mapping or projection if you like !!! (just so you know what to search for if some more info is needed) – Spektre Jul 09 '14 at 08:00

2 Answers2

1

You would start by looking at each of the conditions. What are values of x and y if they were to meet the bare minimum of the condition. For example, if x and y were just barely less than a and b respectively, will the equation x+y ever be able to satisfy z. That is will x+y be greater than z. If not then there could be no way any of the numbers smaller than the current x and y can be ruled out. Then you can go to the next condition and so on. If the values for x and y can satisfy the condition, then you would start with the lower boundaries and iterate over the possible values in that range or maybe use algebra to solve it.

jax
  • 728
  • 1
  • 11
  • 31
1
  1. your function

    double f(double x,double y) // z=f(x,y)
     {
           if ((x<a)&&(y<b))                return x+y;
     else if ((x>=a)&&(x<c)&&(y>=b)&&(y<d)) return x-y;
     else if ((x>=c)&&(y>=d))               return x*y;
                                            return ???;
     }
    

    if you lack the imagination like me then for better understanding draw a map

    fmap

    now it is more obvious that this is one to many mapping

    z=x+y -> y=z-x , x=z-y
    z=x-y -> y=x-z , x=z+y
    z=x*y -> y=z/x , x=z/y
    

    also these 3 defined intervals of x,y can give the same z values

  2. inverse function

    it is not a good idea to return intervals of x,y either you make g() a 2D function like x=g2d(y,z) or y=g2d(x,z).

    Or you return list of all valid x,y pairs not intervals !!! Which can be also done through your 2D g() function

    example of 2D g function:

    double g2d(double x,double z) // y=g(x,z)
     {
          if (x<a) return z-x;
     else if (x<c) return x-z;
     else          return z/x;
     }
    

    you should add also computed y range check for valid solution before return but I am too lazy for that.

    example of 1D g function:

    void g(double *x,double *y,int &n,double z) // x[n],y[n]=g(z)   ... x,y should be allocated big enough or use some list/vector template
     {
     double xx;
     const double min =-1e+3;
     const double max =+1e+3;
     const double step=+1e-3;
     n=0; // reset found solution count
     for (xx=min;xx<=max;xx+=step) // go through valid x axis
      {      
      x[n]=xx;
      y[n]=g2d(xx,z);
      // here add check if solution valid and continue if not
      // also can add check for max n to avoid overrun of arrays x[],y[]
      n++; // add new valid solution to list
      }
     }
    

[notes]

all code is in C++

Spektre
  • 49,595
  • 11
  • 110
  • 380
  • I was not very meticulous about the details, as I assumed readers will make some reasonable assumptions. Plus, you answer is only another way for elaboration of what I said add the end of my question `Do you simply store boundaries and corresponding return values from f and then traverse between them with the given z to find the appropriate boundaries? ` – Ali Abbasinasab Jul 09 '14 at 14:58