質問

I'm trying to write two inverse functions. If we pass to function A value 10 it returns for example 4,86 and if we pass that number to function B it should give us back original 10.

I'm using something like this in function A:

output = sqrt(inputForA / range) * inputForA;

So is it possible to reverse it in function B and calculate inputForA only knowing output and range.

役に立ちましたか?

解決

You just need to factor that equation to single out inputForA. Here are the algebraic steps:

output = sqrt(inputForA / range) * inputForA

output / inputForA = sqrt(inputForA / range)

sq(output) / sq(inputForA) = inputForA / range

range * sq(output) / sq(inputForA) = inputForA

range * sq(output) = cube(inputForA)

So the cube root of range times the output squared should give you your original input. I don't have a good way of showing that in here though...

他のヒント

You just have to use basic math. output = pow(inputForA, 3. / 2.) * pow(range, 1. / 2.) so inputForA = pow(output * pow(range, 1. / 2.), 2. / 3.). This only works if inputForA and scale have the same sign, but the first function is only defined on that interval, so this is okay (since sqrt is only defined for positive values).

In Python:

scale = 7.

def f(x):
    return math.sqrt(x / scale) * x

def g(y):
    return math.pow(y * math.pow(scale, 1. / 2), 2. / 3)

print g(f(10))
10.0

print f(g(10))
10.0

You could also have used Wolfram alpha to get the answer: enter image description here

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top