# Inverse Ax+B/Cx+D sized function

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:

1. 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}$$

1. 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.
• 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 , so shortest code in bytes wins!

(Some terminology might be incorrect, feel free ask me if you have problems)

• The expression string I/O in this challenge seems like a bad idea for many of the usual reasons. It's further confusing here that 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).
– xnor
Mar 22, 2021 at 10:07
• @xnor I have loosened output format, you can give input in either of the two formats Mar 22, 2021 at 10:11
• the expression is (ax+b)/(cx+d) Mar 22, 2021 at 10:13

sub{@_[0,3]=map-$_,@_[3,0]}  Try it online! # Retina 0.8.2, 48 bytes ^- + ^\d -$&
(.\d+)(.*)([-+].+)
$3$2$1 ^- ^\+ -  Try it online! Link includes test cases. Assumes that 1x is always written as such. Explanation: ^- + ^\d -$&


Negate and enforce the sign on A.

(.\d+)(.*)([-+].+)
$3$2$1  Swap A and D. ^- ^\+ -  Negate D, removing the sign if it was negative. # Vyxal, 9 bytes ÷↭⫙'N^N^W  Try it Online! ## Explained ÷↭⫙'N^N^W ÷ # Push every item of the input onto the stack ↭ # And rotate the top three items: [a, b, c, d] → [a, d, b, c] ⫙' # Rotate the entire stack left N^N^W # Negate the top and bottom of the stack and wrap into a list  • Makes me think of the ^H jokes – user100947 Mar 23, 2021 at 3:50 # brainfuck, 31 29 bytes ,[-<->],>,>,[->-<]>.<<<.>.<<.  Commented: , #input A into cell 0 [-<->] #decrement cells 0 and -1 until cell 0 contains 0 and cell -1 contains -A ,>,>, #input B,C,D into cells 0,1,2 [->-<] #decrement cells 2 and 3 until cell 2 contains 0 and cell 3 contains -D >.<<<.>.<<. #output -D,B,C and -A  Try it online! Very rarely, a challenge comes along where Brainfuck is somewhat competitive. Takes 4 bytes as input, gives 4 bytes as output. The normal form of interface for Brainfuck is an ASCII terminal, so the input ABCD in the TIO link is intrepreted as [65,66,67,68]. Outputs are per the table below. Note that signed negative numbers are treated as their unsigned 8 bit counterparts, and give extended ASCII output in the range 128-255, in accordance with https://en.wikipedia.org/wiki/Extended_ASCII#/media/File:Table_ascii_extended.png Input Signed Output Unsigned equivalent ASCII output A=65 -65 191 ¿ B=66 66 66 B C=67 67 67 C D=68 -68 188 ¼  After swapping A and D, we arrive at the required output [-68,66,67,-65], which in ASCII looks like ¼BC¿ # Keg, 7 6 bytes (SBCS) Input as a list. Outputs by values on the stack. Footer outputs entire stack joined by newlines. ÷±'±$"


Try it online!

## Explanation

        # Input as a list    (e.g. [4, 6, 8, 7])
÷       # Dump it onto stack (Stack = [ 4, 6, 8, 7])
±      # Negate TOS         (Stack = [ 4, 6, 8,-7])
'     # Shift left         (Stack = [ 6, 8,-7, 4])
±    # Negate TOS         (Stack = [ 6, 8,-7,-4])
$# Exchange TOS (Stack = [ 6, 8,-4,-7]) " # Shift right (Stack = [-7, 6, 8,-4])  ### Footer explanation ^ # Reverse the stack (So that the first item of TOS is outputted first) ∑ # Apply to entire stack: . # Output TOS as integer 19+, # Print a trailing newline  ## Credits • Saved 1 byte thanks to @Lyxal # PowerShell, 43 bytes Takes input as and outputs as an array. $n=$args;-1,1,2,-4|%{$n[$_]*(2*($_-gt0)-1)}


Try it online!

### Less Fun PowerShell, 31 bytes

param($a,$b,$c,$d)-$d,$b,$c,-$a


Try it online!

-$args[3],$args[1,2],-$args[0]  Try it online! ### Least Useful PowerShell, 26 bytes "-{3},{1},{2},-{0}"-f$args


Try it online!

# Polyglots for Bash and OSH, 19 bytes

Thanks, 2x-1!

$[-$4] $2$3 $[-$1]


Try it online!

## What the heck

$[ ... ] is historically equivalent to $(( ... )), which is not adopted for POSIX.

OBTW adding a space betwee hyphen and dollar (which is +2 bytes), the code would be polyglot for Zsh, too.

• Port in Bash is 19 bytes
– user100947
Mar 23, 2021 at 2:13
• I thought my code would work on every other POSIX-compliant shells, but it worked on Bash and Dash only; failed on OSH, bosh, ksh, yash, Zsh. This is because $((--1)), for example, is parsed as if 1 were a variable although not. I found another point to write portable shell scripts. – user100411 Mar 23, 2021 at 5:35 # convey, 16 + 6 = 22 or 53 bytes Version 1: 16 (but takes extra input) {?>>>>*} >>>>>^  After the four numbers, also input -1 1 1 -1 (+6 bytes). I can't figure out how to do it shortly any other way. Try it online! Version 2, 53 bytes {?>*}v<0 v ^<<^-1 ?>>*} v< ^v<1 v 0>^v v1-^<< >>*}  It's complicated. Multiplies the first and last numbers by -1. I'm a noob at convey and wait was crashing the program. Try it online! # dc, 19 bytes 0?0r-sbscsd-lcldlbf  Try it online! ## Explanation  # Say the input is 4 6 8 7. # Effect: 0 # [0] ? # [0, 4, 6, 8, 7] 0r- # [0, 4, 6, 8,-7] sbscsd # [0, 4] (B=-7, C=8, D=4) - # [-4] (B=-7, C=8, D=4) lcldlb # [-4, 8, 4, -7] f # Output in reverse (i.e. -7 6 8 -4)  # MMIX, 44 bytes (11 instrs) Inverts mobius *a into mobius *b. This is with typedef double mobius[4];. Note that NaNs and zeroes have the sign negated; to not do so would cost an extra instruction (zeroing out $2 at start and replacing INCL $255,#8000 with FSUB$255,$2,$255).

(jxd)

00000000: 8fff0018 e4ff8000 afff0100 8fff0010  Ɓ”¡ðỵ”⁰¡Ḥ”¢¡Ɓ”¡Ñ
00000010: afff0108 8fff0008 afff0110 8fff0000  Ḥ”¢®Ɓ”¡®Ḥ”¢ÑƁ”¡¡
00000020: e4ff8000 afff0118 f8000000           ỵ”⁰¡Ḥ”¢ðẏ¡¡¡


Disassembled:

invmob  LDOU $255,$0,24
INCH $255,#8000 STOU$255,$1 LDOU$255,$0,16 STOU$255,$1,8 LDOU$255,$0,8 STOU$255,$1,16 LDOU$255,$0 INCH$255,#8000
STOU $255,$1,24
POP  0,0


# Python 3, 26 bytes

lambda a,b,c,d:[-d,b,c,-a]


Try it online!

4 input. Output is list.