Skip to main content
deleted 11 characters in body
Source Link
Kevin Cruijssen
  • 131.4k
  • 13
  • 144
  • 384

05AB1E (legacy), 7 + 22 = 29 bytes

Polar to Cartesian (7 bytes):

.¾¹.½‚*

Loose inputs in the order \$\phi,r\$; outputs as a pair \$[x,y]\$.

Try it online or verify all test cases.

Cartesian to Polar (22 bytes):

nOtI`’£×.aÇâ2(ÿ,ÿ)’.E‚

Input as a pair in the order \$[x,y]\$; outputs as a pair \$[r,\phi]\$.

Try it online or verify all test cases.

Explanation:

.¾      # Calculate the cosine of the first (implicit) input ϕ
  ¹.½   # Also push the sine of the first input ϕ
     ‚  # Pair them together
      * # Multiply them to the second (implicit) input r
        # (after which the pair is output implicitly as result)

Although 05AB1E does have tangent, sine, and cosine builtins, it lacks arctan; arcsin; and arccos builtins (both with 1 or 2 arguments). So we'll use a Python eval for it instead (hence the use of the legacy version of 05AB1E)†:

n       # Get the square of the values in the (implicit) input-pair [y,x]
 O      # Sum them together
  t     # Take the square root of that
I       # Push the input-pair [x,y]
 `      # Pop and push both separated to the stack
  ’£×.aÇâ2(ÿ,ÿ)’
        # Push dictionary string "math.atan2(ÿ,ÿ)",
        # where the two `ÿ` are replaced with `y,x` respectively
   .E   # Evaluate and execute it as Python code
‚       # Pair it with the earlier calculated sqrt(y²+x²)
        # (after which it is output implicitly as result)

See this 05AB1E tip of mine (section How to use the dictionary?) to understand why ’£×.aÇâ2(ÿ,ÿ)’ is "math.atan2(ÿ,ÿ)".

I don't have 05AB1E installed on my work laptop. I'll see if an Elixir eval in theThe new version of 05AB1E is possible as well, but (which.E is disabled on TIOeval as Elixir instead of Python, unlikein which case the Python eval) is shorter or notmath.atan2(a,b) would be :math.atan2(a,b), later on at homewhich is 1 byte longer.

05AB1E (legacy), 7 + 22 = 29 bytes

Polar to Cartesian (7 bytes):

.¾¹.½‚*

Loose inputs in the order \$\phi,r\$; outputs as a pair \$[x,y]\$.

Try it online or verify all test cases.

Cartesian to Polar (22 bytes):

nOtI`’£×.aÇâ2(ÿ,ÿ)’.E‚

Input as a pair in the order \$[x,y]\$; outputs as a pair \$[r,\phi]\$.

Try it online or verify all test cases.

Explanation:

.¾      # Calculate the cosine of the first (implicit) input ϕ
  ¹.½   # Also push the sine of the first input ϕ
     ‚  # Pair them together
      * # Multiply them to the second (implicit) input r
        # (after which the pair is output implicitly as result)

Although 05AB1E does have tangent, sine, and cosine builtins, it lacks arctan; arcsin; and arccos builtins (both with 1 or 2 arguments). So we'll use a Python eval for it instead (hence the use of the legacy version of 05AB1E)†:

n       # Get the square of the values in the (implicit) input-pair [y,x]
 O      # Sum them together
  t     # Take the square root of that
I       # Push the input-pair [x,y]
 `      # Pop and push both separated to the stack
  ’£×.aÇâ2(ÿ,ÿ)’
        # Push dictionary string "math.atan2(ÿ,ÿ)",
        # where the two `ÿ` are replaced with `y,x` respectively
   .E   # Evaluate and execute it as Python code
‚       # Pair it with the earlier calculated sqrt(y²+x²)
        # (after which it is output implicitly as result)

See this 05AB1E tip of mine (section How to use the dictionary?) to understand why ’£×.aÇâ2(ÿ,ÿ)’ is "math.atan2(ÿ,ÿ)".

I don't have 05AB1E installed on my work laptop. I'll see if an Elixir eval in the new version of 05AB1E (which is disabled on TIO, unlike the Python eval) is shorter or not, later on at home.

05AB1E (legacy), 7 + 22 = 29 bytes

Polar to Cartesian (7 bytes):

.¾¹.½‚*

Loose inputs in the order \$\phi,r\$; outputs as a pair \$[x,y]\$.

Try it online or verify all test cases.

Cartesian to Polar (22 bytes):

nOtI`’£×.aÇâ2(ÿ,ÿ)’.E‚

Input as a pair in the order \$[x,y]\$; outputs as a pair \$[r,\phi]\$.

Try it online or verify all test cases.

Explanation:

.¾      # Calculate the cosine of the first (implicit) input ϕ
  ¹.½   # Also push the sine of the first input ϕ
     ‚  # Pair them together
      * # Multiply them to the second (implicit) input r
        # (after which the pair is output implicitly as result)

Although 05AB1E does have tangent, sine, and cosine builtins, it lacks arctan; arcsin; and arccos builtins (both with 1 or 2 arguments). So we'll use a Python eval for it instead (hence the use of the legacy version of 05AB1E)†:

n       # Get the square of the values in the (implicit) input-pair [y,x]
 O      # Sum them together
  t     # Take the square root of that
I       # Push the input-pair [x,y]
 `      # Pop and push both separated to the stack
  ’£×.aÇâ2(ÿ,ÿ)’
        # Push dictionary string "math.atan2(ÿ,ÿ)",
        # where the two `ÿ` are replaced with `y,x` respectively
   .E   # Evaluate and execute it as Python code
‚       # Pair it with the earlier calculated sqrt(y²+x²)
        # (after which it is output implicitly as result)

See this 05AB1E tip of mine (section How to use the dictionary?) to understand why ’£×.aÇâ2(ÿ,ÿ)’ is "math.atan2(ÿ,ÿ)".

The new version of 05AB1E is possible as well, but .E is eval as Elixir instead of Python, in which case the math.atan2(a,b) would be :math.atan2(a,b), which is 1 byte longer.

Source Link
Kevin Cruijssen
  • 131.4k
  • 13
  • 144
  • 384

05AB1E (legacy), 7 + 22 = 29 bytes

Polar to Cartesian (7 bytes):

.¾¹.½‚*

Loose inputs in the order \$\phi,r\$; outputs as a pair \$[x,y]\$.

Try it online or verify all test cases.

Cartesian to Polar (22 bytes):

nOtI`’£×.aÇâ2(ÿ,ÿ)’.E‚

Input as a pair in the order \$[x,y]\$; outputs as a pair \$[r,\phi]\$.

Try it online or verify all test cases.

Explanation:

.¾      # Calculate the cosine of the first (implicit) input ϕ
  ¹.½   # Also push the sine of the first input ϕ
     ‚  # Pair them together
      * # Multiply them to the second (implicit) input r
        # (after which the pair is output implicitly as result)

Although 05AB1E does have tangent, sine, and cosine builtins, it lacks arctan; arcsin; and arccos builtins (both with 1 or 2 arguments). So we'll use a Python eval for it instead (hence the use of the legacy version of 05AB1E)†:

n       # Get the square of the values in the (implicit) input-pair [y,x]
 O      # Sum them together
  t     # Take the square root of that
I       # Push the input-pair [x,y]
 `      # Pop and push both separated to the stack
  ’£×.aÇâ2(ÿ,ÿ)’
        # Push dictionary string "math.atan2(ÿ,ÿ)",
        # where the two `ÿ` are replaced with `y,x` respectively
   .E   # Evaluate and execute it as Python code
‚       # Pair it with the earlier calculated sqrt(y²+x²)
        # (after which it is output implicitly as result)

See this 05AB1E tip of mine (section How to use the dictionary?) to understand why ’£×.aÇâ2(ÿ,ÿ)’ is "math.atan2(ÿ,ÿ)".

† I don't have 05AB1E installed on my work laptop. I'll see if an Elixir eval in the new version of 05AB1E (which is disabled on TIO, unlike the Python eval) is shorter or not, later on at home.