Inverse function

31

1

Wouldn't it be neat if programming functions could be inverted, just like the mathematical function they implement?

Write a function (or program) that takes one input x in any form, that outputs ln(x).
When the bytes of the program are reordered/reversed so that the first byte now is the last byte, it should take one input x in any form and output e^x instead.

  • Your answer must have at least 3 correct significant figures.
  • Approximations are fine, as long as they have at least 3 correct significant figures.
  • Your code must be in the same programming language both forwards and backwards.

Let's say this program implements ln(x):

abc你好

Then this program has to implement e^x:

\xBD\xA5\xE5\xA0\xBD\xE4cba

Gold star if you use a language without float support.

This is a weird form of code-golf, so the shortest program wins.

Filip Haglund

Posted 2016-02-06T11:26:25.383

Reputation: 1 789

4"Wouldn't it be neat if programming functions could be inverted, just like the mathematical function they implement?" Some languages (e.g. J and Mathematica) can actually do this for some functions. – Martin Ender – 2016-02-06T11:29:16.743

Additionally, K2 could approximate an inverse for an arbitrary monadic pure function via its "function inverse" overload of dyadic and triadic ?, which used the secant method. – JohnE – 2016-02-06T15:19:31.983

1"at least 3 correct significant figures" - over what range? – TLW – 2016-02-06T16:48:07.137

4I realize it's far too late now, but I think this would have been a really nice challenge had comments been disallowed. – Alex A. – 2016-02-06T21:45:54.207

I actually thought of that when I came up with this challenge @AlexA. but forgot about it while writing the post :P Also that would've made "normal" languages like java, c++ etc basically impossible. – Filip Haglund – 2016-02-07T12:59:00.897

@TLW a big enough range, say up to 10k? I want you to have to handle printing floats, and a 32bit int should be enough for storage. – Filip Haglund – 2016-02-07T13:00:39.703

Answers

75

Haskell, 11 bytes

f=log
pxe=f

and in reverse order:

f=exp
gol=f

This works without the "comment" trick. Instead each version defines an additional, but unused function (pxe/ gol).

nimi

Posted 2016-02-06T11:26:25.383

Reputation: 34 639

49+1 for gol=f. – Leif Willerts – 2016-02-06T13:22:49.080

2This is also a valid solution in Julia. – Rainer P. – 2016-02-06T23:28:49.220

44

APL, 3 bytes

*⊣⍟

This is a function train. Monadic * returns e^x, monadic returns ln(x). is a dyadic function that returns its left argument. Thus, *⊣⍟ is equivalent to just *, and the reverse ⍟⊣* is equivalent to just .

marinus

Posted 2016-02-06T11:26:25.383

Reputation: 30 224

22

Jelly, 5 4 bytes

Yay, my first Jelly answer. :) Input is via command-line argument.

Jelly has its own code page so each character is one byte.

eÆÆl

Try it online!

Reversed:

lÆÆe

Try it online!

Explanation

The Æ on its own is an unrecognised token, so it acts the same as a linefeed. That means in either case the main link is only Æl or Æe which is the 2-character built-in for exp() or ln() and is by default performed on the first command-line argument.

Martin Ender

Posted 2016-02-06T11:26:25.383

Reputation: 184 808

9

Javascript, 18 bytes

Math.log//pxe.htaM

Neil

Posted 2016-02-06T11:26:25.383

Reputation: 95 035

Don't you need a return() or console.log() around it? – OldBunny2800 – 2016-02-06T20:32:32.033

2@OldBunny2800 It evaluates to a function, which should be permissible. – Neil – 2016-02-06T20:50:50.437

5Math.ln||pxe.htaM will probably also work. – SuperJedi224 – 2016-02-07T12:35:12.750

@SuperJedi224 Thanks, that helped me spot the error in my answer! – Neil – 2016-02-07T12:50:57.260

@Neil I hadn't even noticed that – SuperJedi224 – 2016-02-07T18:09:42.847

@EᴀsᴛᴇʀʟʏIʀᴋ That's not how it works. Try assigning it to a function f: f=Math.log//pxe.htaM, then calling it with input. – Conor O'Brien – 2016-04-28T18:55:25.423

7

Seriously, 5 bytes

,_.e,

Input, ln, output, then exp on an empty stack (does nothing), and input (does nothing since input is exhausted). Try it online!

Reversed:

,e._,

Try it online!

Mego

Posted 2016-02-06T11:26:25.383

Reputation: 32 998

5

Julia, 7 bytes

log#pxe

This is an anonymous function. Assign it to a variable to call it. Evaluates to builtins log or exp plus a comment.

Rainer P.

Posted 2016-02-06T11:26:25.383

Reputation: 2 457

1Same answer works for R – Dason – 2016-02-06T20:30:36.613

5

Mathematica, 19 bytes

1&#@pxE+0&0+Log@#&1

Reversed:

1&#@goL+0&0+Exp@#&1

This was interesting to golf! Mathematica has no line comments / implicit string endings, so I couldn't take the simple route. Instead, I used the fact that 0 + x == x, 0 x == 0, and that 1 x == x, no matter what x is! Testing:

In[1]:= (1&#@pxE+0&0+Log@#&1)[x]

Out[1]= Log[x]

In[2]:= (1&#@goL+0&0+Exp@#&1)[x]

         x
Out[2]= E

LegionMammal978

Posted 2016-02-06T11:26:25.383

Reputation: 15 731

4

Python2, 73 bytes

io: stdin/stdout

from math import*;print log(input())#))(tupni(pxe tnirp;*tropmi htam morf

inverse:

from math import*;print exp(input())#))(tupni(gol tnirp;*tropmi htam morf

Filip Haglund

Posted 2016-02-06T11:26:25.383

Reputation: 1 789

You can shave 10 characters off by using __import__("math"). instead of – TLW – 2016-02-06T16:43:48.830

3

Brachylog, 3 bytes

*₁≡

Try it online!

Initially, I had hoped to use ~*, but although *~ computes e^x and successfully ignores the trailing tilde, ~* fails for all integer inputs and hits a float overflow on most non-integer inputs.

Forwards:

       The output
  ≡    is
*₁     the natural logarithm of
       the input.

Backwards:

       The output is
  *    Euler's number to the power of
       the input
≡      passed through the identity predicate
 ₁     with an extraneous subscript.

This uses the identity predicate because, although trailing tildes are tolerated, leading subscripts are not. (If they were, the Brachylog answer would be *₁ alone, which is just the normal builtin for natural log.)

Unrelated String

Posted 2016-02-06T11:26:25.383

Reputation: 5 300

3

CJam, 11 bytes

rdmle#eemdr

Test it here.

Reversed:

rdmee#elmdr

Test it here.

Basically the same comment-trick as the OP's Python answer. e# starts a comment. rd reads the input and ml or me computes the logarithm or exponential.

Martin Ender

Posted 2016-02-06T11:26:25.383

Reputation: 184 808

2

Vitsy, 5 bytes

This is a program that exits on an error.

EL^rE
E   E  Push java.lang.Math.E
 L     Push log_(top) (input) (ln(input))
  ^    Push (top)^(input)  (e^(input))
   r   Reverse the stack

This program exits on an error with ln(input) on the stack.

Try it online! (note that I have put N to have visible output)

Then it's inverse:

Er^LE

This program exits on an error with e^(input) on the stack.

Try it online!

Addison Crump

Posted 2016-02-06T11:26:25.383

Reputation: 10 763

2

Fuzzy Octo Guacamole, 7 bytes

non-competing, FOG is newer than the challenge

EZO@pZE

This is the equivalent of a function in FOG. It assumes the input is on the stack. This can be assigned to a function by the code "EZO@pZE""f"o, where f is any single-char name you want to assign. Then use it like any other command. Example: "EZO@pZE"'f'o^f.

Explanation:

EZO@pZE
E       # Push E (2.718....)
 Z      # Reverse stack (it is now [e, input])
  O     # log(x, y) which is ln(input)
   @    # Exit. (implicit output) Nothing after this gets run.
    p   # x^y (power function)
     Z  # Reverse stack
      E # Push E.

Reversed:

EZp@OZE
E       # Push E (2.718....)
 Z      # Reverse stack (it is now [e, input])
  O     # x^y (power function)
   @    # Exit. (implicit output) Nothing after this gets run.
    p   # log(x, y) which is ln(input)
     Z  # Reverse stack
      E # Push E.

Rɪᴋᴇʀ

Posted 2016-02-06T11:26:25.383

Reputation: 7 410

1

Matl, 5 bytes

Yl%eZ

Yl: log Ze: exp %: comment

Rainer P.

Posted 2016-02-06T11:26:25.383

Reputation: 2 457

Thanks to Dennis, there's an online compiler for MATL

– Luis Mendo – 2016-02-06T17:34:07.360

1

Pyth, 12 bytes

Finds ln(input())

.lQ) " Q1n.^

Finds e^input()

^.n1Q " )Ql.

Spaces stop implicit printing of strings, each version calculates it then creates a string with the remaining characters.

ln(x) mode here

e^x mode here

Blue

Posted 2016-02-06T11:26:25.383

Reputation: 26 661

1

, 8 chars / 10 bytes

МŬï//ïŦМ

Try it here (Firefox only).Try reverse here (Firefox only).

Just 2 builtins separated by a comment.

Mama Fun Roll

Posted 2016-02-06T11:26:25.383

Reputation: 7 234

1

Jolf, 9 bytes

Program 1: exp of input

amoj"jOma
a         print
 moj      e^j
    "jOma  the rest of the line is captured as a string; implicit printing is restricted.

Program 2: ln of input

amOj"joma
a         print
 mOj      ln(j)
    "joma  the rest of the line is captured as a string; implicit printing is restricted.

Bonus points for being a case-insensitive palindrome? Try it here!

Conor O'Brien

Posted 2016-02-06T11:26:25.383

Reputation: 36 228

1

J, 8 bytes

The natural logarithm is ^., and exponential ^. The problem is, . can only modify a valid verb, otherwise, a spelling error will occur. Thus, we can't use the left argument trick in the APL answer, becuase ^.[^ would cause an error when reversed, as ^[.^ creates an invalid verb. So, we must use comments; but NB. is so long :( Fortunately, they both end with ., so&ldots; there's that.

Logarithm:

^.NB.BN^

Exponential:

^NB.BN.^

You can enter them for yourself online!

Conor O'Brien

Posted 2016-02-06T11:26:25.383

Reputation: 36 228

0

Runic Enchantments, 9 bytes

i'lA@Ae'i

Try it online!

An ungodly uninteresting program. @ insures termination of the implied entry point at the left, everything after is unexecuted. I tried really hard to re-use the ' or A instructions, but to no avail, even at larger program sizes. The required explicit entry point for multi-line programs essentially precludes it.

Draco18s no longer trusts SE

Posted 2016-02-06T11:26:25.383

Reputation: 3 053

0

Java 8, 198 182 30 bytes

d->Math.log(d)//)d(pxe.htaM<-d

Try it online.

and reversed:

d->Math.exp(d)//)d(gol.htaM<-d

Try it online.

Uses the comment trick (//) with built-ins for Math.log and Math.exp.

Kevin Cruijssen

Posted 2016-02-06T11:26:25.383

Reputation: 67 575