If you only need it to work on positive numbers, this is easy:
def gs(a):
return a**2 + (1/a**2)
That result is shared by any positive number with its own multiplicative inverse, and with no other positive number. For example, gs(2) == gs(0.5) == 4.25
.
Unfortunately, it's also shared with its arithmetic inverse: gs(2) == gs(0.5) == gs(-2) == gs(-0.5) == 4.25
.
But we can fix that just by copying the sign:
def gs(a):
return (a**2 + (1/a**2)) * abs(a)/a
Now, gs(2) == gs(0.5) == 4.25 != gs(-2) == gs(0.5) == -4.25
.
If you don't like the abs
because there's a hidden if
in there… well, there isn't. Sticking with floats, you can obviously do a**2**.5
.
So that gets us all non-zero floats. And zero is obviously outside the domain, because gs(0)
should be equal to gs(1/0)
, which is not a number.
It still doesn't work for complex numbers, however—gs(i) == 2i == gs(-1/i)
, but gs(1/i) == -2i == gs(-i)
. For that, you just need multiply by the conjugate instead of squaring.