-6
\$\begingroup\$

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

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

\$\endgroup\$
3
  • 7
    \$\begingroup\$ 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). \$\endgroup\$
    – xnor
    Commented Mar 22, 2021 at 10:07
  • \$\begingroup\$ @xnor I have loosened output format, you can give input in either of the two formats \$\endgroup\$
    – Wasif
    Commented Mar 22, 2021 at 10:11
  • 1
    \$\begingroup\$ the expression is (ax+b)/(cx+d) \$\endgroup\$
    – Wasif
    Commented Mar 22, 2021 at 10:13

17 Answers 17

4
\$\begingroup\$

Perl 5, 27 bytes

sub{@_[0,3]=map-$_,@_[3,0]}

Try it online!

\$\endgroup\$
4
\$\begingroup\$

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.

\$\endgroup\$
4
\$\begingroup\$

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
\$\endgroup\$
1
  • \$\begingroup\$ Makes me think of the ^H jokes \$\endgroup\$
    – user100947
    Commented Mar 23, 2021 at 3:50
4
\$\begingroup\$

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¿

\$\endgroup\$
4
\$\begingroup\$

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
\$\endgroup\$
2
4
\$\begingroup\$

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!

Most Unfun PowerShell, 30 bytes

-$args[3],$args[1,2],-$args[0]

Try it online!

Least Useful PowerShell, 26 bytes

"-{3},{1},{2},-{0}"-f$args

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ unfun $args[1,2]. useful is useful! :) \$\endgroup\$
    – mazzy
    Commented Mar 23, 2021 at 18:16
3
\$\begingroup\$

05AB1E, 7 bytes

À`(s()Á

Try it online!

         # implicit input                [A, B, C, D]
À        # rotate the input left         [B, C, D, A]
 `       # dump all values on the stack  B, C, D, A
  (      # negate top of stack           B, C, D, -A
   s     # swap top two values           B, C, -A, D
    (    # negate top of stack           B, C, -A, -D
     )   # collect all values in a list  [B, C, -A, -D]
      Á  # rotate the list right         [-D, B, C, -A]
\$\endgroup\$
3
\$\begingroup\$

APL(Dyalog Unicode), 9 7 bytes SBCS

-∘⌽@1 4

Try it on APLgolf!

A function submission which accepts an array.

-2 bytes from rak1507.

\$\endgroup\$
1
  • \$\begingroup\$ Don't need parens \$\endgroup\$
    – rak1507
    Commented Mar 22, 2021 at 22:50
3
\$\begingroup\$

Ruby, 26 bytes

->n{a,b,c,d=n
[-d,b,c,-a]}

Try it online!

Shorter than my brainfuck answer by a few bytes, but not as interesting.

\$\endgroup\$
3
\$\begingroup\$

jq, 22 bytes

[-.[3],.[1:3][],-.[0]]

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Java, 31 bytes

(a,b,c,d)->new int[]{-d,b,c,-a}

Try it online!

\$\endgroup\$
3
\$\begingroup\$

JavaScript (Node.js), 22 bytes

(a,b,c,d)=>[-d,b,c,-a]

Try it online!

\$\endgroup\$
4
  • \$\begingroup\$ don't think it can get shorter than this... \$\endgroup\$
    – user100690
    Commented Mar 23, 2021 at 8:53
  • \$\begingroup\$ @ophact It's not a very interesting challenge. \$\endgroup\$
    – emanresu A
    Commented Mar 23, 2021 at 8:57
  • \$\begingroup\$ I know, definitely not the best challenge \$\endgroup\$
    – user100690
    Commented Mar 23, 2021 at 9:03
  • \$\begingroup\$ @ophact See xnor's comment above. \$\endgroup\$
    – emanresu A
    Commented Mar 23, 2021 at 9:05
3
\$\begingroup\$

Dash, 23 bytes

$((-$4)) $2 $3 $((-$1))

Try it online!

Usage

Requires positional parameters for each of A, B, C, and D. Before evaluating the command. Also the code evaluates to just four strings, so run echo command with those code (may not portable when D is originally positive, as hyphen-parameter may be treated as an option).

How it works

  • dollar-double-parentheses is arithmetic expansion.

Polyglot for Bash, OSH, bosh, Dash, ksh, yash, and Zsh, 25 bytes

$((- $4)) $2 $3 $((- $1))

Try it online!

Differences

  • A space between hyphen and dollar. This is a workaround for negative values; if one of them was expanded to $((--1)), for example, Dash, OSH and Bash parse as minus of minus one, but other four don't; they parse to decrement a variable named one, although one is a constant.

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.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Port in Bash is 19 bytes \$\endgroup\$
    – user100947
    Commented Mar 23, 2021 at 2:13
  • \$\begingroup\$ 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. \$\endgroup\$
    – user100411
    Commented Mar 23, 2021 at 5:35
2
\$\begingroup\$

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!

\$\endgroup\$
2
\$\begingroup\$

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)
\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

Python 3, 26 bytes

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

Try it online!

4 input. Output is list.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.