Compute a complex power

10

1

The Rundown

Given any input x and y, perform a complex operation, and print a corresponding result.

How your program should work

  1. Given an input x and y in the form z = x+yi, find zi-z

  2. If the absolute real value of zi-z is larger than the absolute imaginary part, print the real part; vice versa for the other way around. If both values are equal, print one of the values.

Example

x: 2
y: 0

Therefore:

z = 2
z^(i-z) ~= 0.192309 + 0.159740i

Since the real part has a larger absolute value than the imaginary part, the program returns

0.192309

More examples

z = 1+i >> 0.5
z = i >> 1
z = 0.5 >> 1.08787
z = -2+8i >> 2.22964E7
z = -10i >> 3.13112E7

Graviton

Posted 2017-04-17T21:40:36.160

Reputation: 2 295

10Pro-tip: get rid of the bonus! – Stewie Griffin – 2017-04-17T21:44:19.827

3Things to avoid when writing challenges - bonuses in code-golf – mbomb007 – 2017-04-17T21:47:39.387

It would be nice if you gave more input-output pairs. – user1502040 – 2017-04-17T21:57:14.653

@user1502040 Sure thing – Graviton – 2017-04-17T21:58:49.480

7Raising a complex numbers to a complex power is discontinuous and depends on the branch cut used. Can you specify that? Though I guess everyone will just be using built-in math operations and those probably all use the same convention. – xnor – 2017-04-17T22:16:33.353

Do we have to take X and Y as separate inputs? Some languages can take complex numbers as inputs. – Scott Milner – 2017-04-17T22:29:07.923

@ScottMilner, No you can take z as an entire input. – Graviton – 2017-04-17T22:35:24.030

2Does "larger" mean to pick the value with greatest absolute value, rather than (what most have assumed) to pick the maximal value? A test case of -2+i could be used for that (z^(i-z)=3-4i so 3>-4 vs abs(-4)>abs(3)). – Jonathan Allan – 2017-04-18T05:44:23.920

1Will the real and imaginary parts of the input always be integers? Your test cases seem to suggest that. – Martin Ender – 2017-04-18T06:55:43.620

what is the problem? why [on hold]? larger means larger and not abosolute larger... possible there is to clear if z=a+i*b with a and b in R or integers, but it is better in the set R – RosLuP – 2017-04-18T09:52:10.153

5The "absolute value" clarification/change has invalidated most of the answers. – xnor – 2017-04-18T22:32:49.257

@YamB: https://github.com/DaveJarvis/sequential -- cheers from Victoria.

– None – 2017-04-25T07:14:55.630

Answers

7

Jelly, 8 11 bytes

Thanks Johnathan Allan for updating the answer with the rules change.

ı_*@µĊ,ḞAÞṪ

Try it online!

ı_*@        z^(i-z)
    µ       new monadic link
     Ċ,Ḟ    pair real and imaginary parts
        AÞṪ sort by absolute value and take last value

layagyasz

Posted 2017-04-17T21:40:36.160

Reputation: 111

Make it ı_*@µĊ,ḞAÞṪ and you may well have the only valid entry (given the change to require the maximal value in absolute terms, such that, for example, -2+1j returns -4.0 rather than 3.0). – Jonathan Allan – 2017-04-19T06:47:57.790

6

Python 2, 45 bytes

def f(z):z=z**(1j-z);print max(z.real,z.imag)

Try it online - all test cases

Programming languages often use j instead of i. That is the case in Python. See this SO question for more information on why.

mbomb007

Posted 2017-04-17T21:40:36.160

Reputation: 21 944

5

Mathematica, 21 22 bytes

Edit: Thanks to JungHwan Min for saving 3 btyes

Max@ReIm[#^(I-#)]&

Pure function which expects a complex number as an argument. If an exact number is passed, then an exact number will be returned (e.g. 1/2 gives Sqrt[2] Cos[Log[2]]). The problem spec was edited after I posted my solution to specify that the absolute value should be used. The best I can come up with for that is MaximalBy[ReIm[#^(I-#)],Abs][[1]]& or Last@MaximalBy[Abs]@ReIm[#^(I-#)]&, both 34 bytes.

ngenisis

Posted 2017-04-17T21:40:36.160

Reputation: 4 600

1Max does not need to be the head. It returns the max value no matter how deep the input List is (e.g. Max[1, {2, {3}}] returns 3). Also, the question only specifies that you print the values, so I don't think you would need N: Max@ReIm[#^(I-#)]& would work. – JungHwan Min – 2017-04-18T01:59:11.250

3

MATL, 10 bytes

Jy-^&ZjhX>

Try it online! Or verify all test cases.

Explanation

Consider input -2+8i as an example.

J     % Push i (imaginary unit)
      % STACK: i
y     % Implicit input. Duplicate from below
      % STACK: -2+8i, i, -2+8i
-     % Subtract
      % STACK: -2+8i, 2-7i
^     % Power
      % STACK: 3168271.58+22296434.47i
&Zj   % Real and imaginary parts
      % STACK: 3168271.58, 22296434.47
h     % Concatenate
      % STACK: [3168271.58 22296434.47]
X>    % Maximum. Implicitly display
      % STACK: 22296434.47

Luis Mendo

Posted 2017-04-17T21:40:36.160

Reputation: 87 464

3

Octave, 29 bytes

@(z)max(real(z^(i-z)./[1 i]))

This defines an anonymous function. It works in MATLAB too.

Try it online!

Explanation

Element-wise dividing (./) the number z^(i-z) by the array [1 i] and taking the real part gives an array with the real and imaginary parts of z^(i-z).

Luis Mendo

Posted 2017-04-17T21:40:36.160

Reputation: 87 464

2

TI-BASIC, 40, 32, 31 29 bytes

Saved a byte thanks to @Conor O'Brien

Z^(i-Z→A                   #Perform operation, store as A, 8 bytes
:real(A)>imag(A            #Test if real part is greater than imaginary, 9 bytes
:Ansreal(A)+imag(Anot(Ans  #Determine output and print, 12 bytes

Takes input as a complex number on the Z variable.

TI-BASIC uses its own encoding, you can find it here.

Scott Milner

Posted 2017-04-17T21:40:36.160

Reputation: 1 806

1

Pyth, 16 Bytes

=b^Q-Q.j;eS,ebsb

user1502040

Posted 2017-04-17T21:40:36.160

Reputation: 2 196

1

C (GCC), 93 79 + 4 (-lm) = 97 83 bytes

Saved 14 bytes thanks to @ceilingcat!

float f(_Complex z){z=cpow(z,csqrt(-1)-z);return cimag(z)>creal(z)?cimag(z):z;}

Including the header complex.h is longer than that ¯\_(ツ)_/¯

Try it online!

betseg

Posted 2017-04-17T21:40:36.160

Reputation: 8 493

Why +4 bytes? I count 3, -, l, and m. – Rɪᴋᴇʀ – 2017-04-18T03:44:18.847

@Riker normal compiling is gcc file.c -o exe, so this flag adds 4 bytes: space, -, l, and m. (At least that's how I see it counted when compiling.) – betseg – 2017-04-18T04:45:41.897

@ceilingcat oh didn't know that was possible. Thanks! – betseg – 2017-04-19T09:01:29.113

54 bytes – ceilingcat – 2018-12-09T09:29:06.200

1

Ruby, 25 35 bytes

EDIT: Fixed to comply with new absolute value rule.

->z{(z**(1i-z)).rect.max_by(&:abs)}

Try it online!

This creates an anonymous function.

Nick Clifford

Posted 2017-04-17T21:40:36.160

Reputation: 1 184

1

Perl 6, 24 bytes

{($_**(i-$_)).reals.max}

$_ is the possibly complex argument; $_ ** (i - $_) is the expression to be computed; .reals is a Complex method that returns a list of the real and imaginary parts; and finally .max returns the larger of the two.

Sean

Posted 2017-04-17T21:40:36.160

Reputation: 4 136

1

TI-Basic, 19 16 bytes

Ans^(i-Ans
max(real(Ans),imag(Ans

real( and imag( are two-byte tokens.

Run with 5+3i:prgmNAME (5+3i being the argmuent, NAME being the program name.)

pizzapants184

Posted 2017-04-17T21:40:36.160

Reputation: 3 174

0

R, 38 bytes

pryr::f({z=z^(1i-z);max(Re(z),Im(z))})

Anonymous function. Takes a (possibly) complex number z, takes it to the specified power, and then returns the max of the Real and Imaginary parts.

BLT

Posted 2017-04-17T21:40:36.160

Reputation: 931

0

Axiom, 60 bytes

f(z:Complex Float):Float==(y:=z^(%i-z);max(real(y),imag(y)))

test code and results; i follow as the other the precedent version of the question...

(28) -> [[k,f(k)] for k in [1+%i,%i,1,-2+8*%i,-10*%i]]
   (28)
   [[1.0 + %i,0.5], [%i,1.0], [1.0,1.0],
    [- 2.0 + 8.0 %i,22296434.4737098688 53],
    [- 10.0 %i,31311245.9804955291 66]]

RosLuP

Posted 2017-04-17T21:40:36.160

Reputation: 3 036

0

C# - 189 Bytes

double f(double x, double y){double r,t,m,c;r=Math.Sqrt(x*x+y*y);t=Math.Atan2(y,x);m=Math.Pow(r,-x)*Math.Exp(y*t-t);c=Math.Cos((1-y)*Math.Log(r)-t*x);return m*(2*c*c<1?Math.Sqrt(1-c*c):c);}

Readable:

double f(double x, double y){
double r, t, m, c;
r = Math.Sqrt(x * x + y * y);
t = Math.Atan2(y, x);
m = Math.Pow(r, -x) * Math.Exp(y * t - t);
c = Math.Cos((1 - y) * Math.Log(r) - t * x);
return m * (2 * c * c < 1 ? Math.Sqrt(1 - c * c) : c); }

Explanation: Decided not to use any Complex libraries.

$$ \begin{align} z & = x + iy \\ & = r e^{it} \\ z^{i-z} & = (re^{it})^{(-x +i(1-y))} \\ & = r^{-x} r^{i(1-y)} e^{-xit} e^{t(y-1)} \\ & = r^{-x} e^{t(y-1)} e^{i((1-y)\ln(r)-xt)} \text{ (as }r^i = e^{i\ln(r)}\text{)} \end{align} $$

Let this be equal to \$me^{ia}\$ where

$$m = r^{-x} e^{t(y-1)}$$ $$a = (1-y)\ln(r)-xt$$

Then \$\Re(z^{i-z})=m \cos a\$ and \$\Im(z^{i-z})=m \sin a\$

The maximum absolute value can be determined by the \$\cos a\$ and \$\sin a\$ terms, with these being equal at \$\frac{1}{\sqrt{2}}\$ (hence the test \$2c^2 < 1\$).

As mentioned, raising to a complex exponent is dependent on choosing a particular branch cut ( e.g. \$z = 1\$ could be \$e^{i\pi}\$ or \$e^{3i\pi}\$ - raising this to \$i\$ gives a real part of \$e^{-\pi}\$ or \$e^{-3\pi}\$ respectively), however, I have just used the convention of \$t \in [0,2\pi)\$ as per the question.

Jack P.

Posted 2017-04-17T21:40:36.160

Reputation: 1

0

APL (Dyalog Unicode), 18 bytes

18 bytes (https://github.com/abrudz/SBCS/)
28 bytes (UTF-8)

⍵ is a complex number ajb = a+bi

{⌈/9 11∘.○⍵*0j1-⍵}

Try it online!

Richard Park

Posted 2017-04-17T21:40:36.160

Reputation: 21