Let's assume that
$$ f(x) = \frac{Ax+B}{Cx+D} $$
Where, \$x\$ is a variable and \$A\$,\$B\$,\$C\$,\$D\$ are constants.
Now we have to find out the inverse function of \$f(x)\$, mathematically \$f^{-1}(x)\$, To do this first we assume,
$$ y = f(x) \\\rightarrow y=\frac{Ax+B}{Cx+D} \\\rightarrow Cxy+Dy=Ax+B \\\rightarrow Cxy-Ax=-Dy+B \\\rightarrow x(Cy-A)=-Dy+B \\\rightarrow x=\frac{-Dy+B}{Cy-A} $$
Then, we know that
$$ y=f(x) \\\rightarrow f^{-1}(y)=x \\\rightarrow f^{-1}(y)=\frac{-Dy+B}{Cy-A} ..... (i) $$
And from \$(i)\$ equation, we can write \$x\$ instead of \$y\$
$$ \\\rightarrow f^{-1}(x)=\frac{-Dx+B}{Cx-A} $$
So, \$\frac{-Dx+B}{Cx-A}\$ is the inverse function of \$f(x)\$
This is a very long official mathematical solution, but we have a "cool" shortcut to do this:
- Swap the position of the first and last constant diagonally, in this example \$A\$ and \$D\$ will be swapped, so it becomes:
$$ \frac{Dx+B}{Cx+A} $$
- Reverse the sign of the replaced constants, in this example \$A\$ is positive (\$+A\$) so it will be negative \$-A\$, \$D\$ is positive (\$+D\$) so it will be negative \$-D\$
$$ \frac{-Dx+B}{Cx-A} $$
And VOILA!! We got the inverse function \$\frac{Ax+B}{Cx+D}\$ in just two steps!!
Challenge
(Input of \$\frac{Ax+B}{Cx+D}\$ is given like Ax+B/Cx+D
)
Now, let's go back to the challenge.
Input of a string representation of a function of \$\frac{Ax+B}{Cx+D}\$ size, and output its inverse function in string representation.
I have just shown two ways to that (Second one will be easier for programs), there may be other ways to do this, good luck!
Test cases
(Input of \$\frac{Ax+B}{Cx+D}\$ is given like Ax+B/Cx+D
)
4x+6/8x+7 -> -7x+6/8x-4
2x+3/2x-1 -> x+3/2x-2
-4x+6/8x+7 -> -7x+6/8x+4
2x+3/2x+1 -> x+3/2x+2
Or you can give it using list of A,B,C,D
4,6,8,7 -> -7x+6/8x-4
Or you can output -7,6,8,-4
Rules
- Input is always in \$\frac{Ax+B}{Cx+D}\$ size, and is guaranteed to be valid.
- Standard loopholes are forbidden.
- Trailing/Leading whitespace in output is allowed.
- If possible, please link to an online interpreter (e.g. TIO) to run your program on.
- Please explain your answer. This is not necessary, but it makes it easier for others to understand.
- Languages newer than the question are allowed. This means you could create your own language where the empty program calculates this number, but don't expect any upvotes.
- This is code-golf, so shortest code in bytes wins!
(Some terminology might be incorrect, feel free ask me if you have problems)
ax+b/cx+d
represents(ax+b)/(cx+d)
. I'd suggest loosening it, though it seems that not much remains in mapping(A,B,C,D) -> (-D,B,C,-A)
. \$\endgroup\$(ax+b)/(cx+d)
\$\endgroup\$