# What might this function be?

The problem:

I'm looking for a particular function $f(x, y)$—this isn't "homework" in the sense that I have no idea if such a function exists. It has a continuous domain $-1 \lt x \lt 1$ and $-1 \lt y \lt 1$, and a continuous range $-1 \le f(x, y) \le 1$ (EDIT: I have no idea how to word these requirements. See comments.) Additionally:

$f(n, -n) = 0$

$f(n, 0) = f(0, n) = n$

$f(n, 1) = f(1, n) = 1$

$f(n, -1) = f(-1, n) = -1$

I did work out the following cubic function $g$ which comes close:

$g(x, y) = (-\frac{x^2}{1-x^2})y^3 - xy^2 + (\frac{1 - 2x^2}{1-x^2})y + x$

$g(n, -n) = 0$

$g(n, 0) = g(0, n) = n$

$g(n, 1) = 1$

$g(n, -1) = -1$

However:

$g(\lim \limits_{x \to 1}, |n|) = \infty$

What I'm trying to achieve:

In my code, $x$ and $y$ are two "match" percentages between elements $a$ and $b$ (for $x =$ +100%, $a$ and $b$ are perfect matches; for $x =$ 0%, $a$ and $b$ have no relation; for $x =$ -100%, $a$ and $b$ are exact opposites). I'm trying to (elegantly) combine these two match values such that $x$ & $y$ with the same sign pull each other toward |100%| and $x$ & $y$ with opposite signs pull each other toward 0% (cancel each other out).

If I can't find an elegant mathematic solution, I'll just use linear interpolation in the three ranges $(sign(x), 0), (0, -x), (-x, -sign(x))$.

EDIT: Also worth noting that function $g$ doesn't work well for this purpose for $|x| > \frac23$ or $|y| > \frac23$, since the cubic function grows a peak & valley beyond that threshold.

• As long as your requirements aren't contradicting each other or the definition of a function (and your's doesn't) a function will exist. In your case a lot of functions exist, you might want to add some requirements, like should it be continuous? – Henrik May 23 '16 at 15:58
• Your "however" doesn't make any sense. You have a limit of nothing? – Henrik May 23 '16 at 16:05
• f(-1,1) would have to be equal to -1 and to 1. Such a function is impossible..... Oh, I guess not. – fleablood May 23 '16 at 16:06
• @fleablood: I thought so to for a moment, but the requirements are only for $-1<n<1$, so the function just isn't continuous at $(-1,1)$ (or at $(1,-1)$). - And that means that my suggested requirement above is not valid. – Henrik May 23 '16 at 16:08
• @yeah, I realize that now. So anyway... many functions. f(x,y) = i)ii) iii) iv) if conditions met. f(x) = $\sqrt{\pi}*x + e^y$ otherwise will do. – fleablood May 23 '16 at 16:11

What about $$f(x, y) = \begin{cases} x + y & -1 \leq x + y \leq 1 \\ 1 & x + y > 1 \\ -1 & -1 > x + y \end{cases}$$
OK, so it's a piecewise function, but it does what (I think) you intended. Bear in mind that I guessed most of it based on the fact that $x$ and $y$ represent percentages and you are probably trying to combine two aspects of similarity into a single number.
For $-1 \leq n \leq 1$, $$f(n, 0) = f(0, n) = n$$ For $n \geq 0$: $$f(n, 1) = f(1, n) = 1$$ (If there's one 100% correlation and the other one is negative, then that isn't a 100% result, is it?) For $n \leq 0$: $$f(n, -1) = f(-1, n)$$ (A similar observation as above applies.)