# Complex Exponentiation [closed]

As the title says! Raise one complex number to the power of another. (a+bi)^(c+di)
(using the principal branch of the complex logarithm)

Requires four inputs a, b, c, d. You can combine a, b, c, d into respective vectors/arrays (e.x. C([a, b], [c, d]))

Built-in complex arithmetic is not allowed. Code the solution yourself!
(Built-in functions are any functions that perform arithmetic on complex vectors. e.x. quaternions, complex libraries, languages where complex numbers are a type. If your language does not have an only-real type then arithmetic operators can only be used on the reals.)
Functions are treated as the program:

function x(arg){
...
return output;
}

is the same as

arg = getInput();
...
print(output);

Smallest byte length wins.

### EXAMPLES:

• (1+1.5i)^(2+2.5i) ≈ −0.266-0.082i
• (e+1i)^(π+1i) ≈ −11.213 + 16.386i
• (1+1i)^(1+1i) ≈ 0.274 + 0.584i
• (-1+i)^(1+i) ≈ -0.121 + 0.057i
• Banning built-ins is usually frowned upon (it used to be acceptable in the past); see here or here Commented May 21 at 18:17
• Updated the question to use the principal branch of the complex logarithm. (this is what Wolfram Alpha uses for it's primary answer) Commented May 21 at 18:41
• I recommend adding tests with bases in quadrants other than the first to check that the solution is using atan2 instead of normal arctangent, for example (-1+i)^(1+i) Commented May 21 at 18:53
• In regards to Banning Built Ins, I'd personally recommend allowing them, but encourage users to also post a non-built-in answer in the same language. Banning Built-ins is otherwise a Non-observable restriction. Commented May 21 at 22:19
• I think it is very clear. Even if your language supports complex arithmetic, at no point in your computation may any value be complex.
Commented May 23 at 20:40

# JavaScript (ES7), 94 bytes

Returns [real, img].

This is essentially the formula used by doubleunary, except I'm computing $$\\sqrt{a^2+b^2}\$$ (with hypot) instead of $$\a^2+b^2\$$ to get rid of both divisions by 2.

with(Math)f=(a,b,c,d,p=atan2(b,a))=>[cos,sin].map(g=>g(c*p+d*log(h=hypot(a,b)))*h**c/exp(d*p))

Try it online!

=let(s,A1^2+B1^2,a,atan2(A1,B1),r,s^(C1/2)/exp(D1*a),u,C1*a+D1*ln(s)/2,{r*cos(u),r*sin(u)})

Put the four coefficients in cells A1:D1 and the formula in cell E1.

Returns the real and imaginary parts as a two-element array. To get an actual complex number, replace the final { array expression } with complex() (98 bytes) or text() (119 bytes, see the edits). The latter gives negative imaginary part with unary plus, as in -0.266+-0.082i, which is a valid complex number format in Google Sheets as well.

Ungolfed:

=let(
s, A1^2 + B1^2,
a, atan2(A1, B1),
r, s^(C1 / 2) / exp(D1 * a),
u, C1 * a + D1 * ln(s) / 2,
{ r * cos(u), r * sin(u) }
)

-5 bytes thanks to Arnauld.

The same in JavaScript (non-competing, 102 bytes):

with(Math)f=(a,b,c,d,s=a*a+b*b,p=atan2(b,a),r=s**(c/2)/exp(d*p),u=c*p+d*log(s)/2)=>[r*cos(u),r*sin(u)]
• Or just /exp(D1*a) -- unless I'm missing something. Commented May 21 at 23:10
• @Arnauld sorry, there's meta consensus that auto-completed bits like that zero and closing parentheses & quotes still count (as does the leading =. Interestingly, the results of the two formulas differ in the 16th decimal. Commented May 22 at 0:35
• Thank you for pointing this out. I've added another answer to the first linked meta topic, as I think the currently accepted one is not clear enough. Commented May 22 at 10:25
• @Arnauld did a quick test to find whether Google Sheets autocompletion takes place in the formula editor or the formula parser. range.setFormula('=sum(1, 2') works but gives a parse error in the cell. If you add the missing closing paren in the code or the cell it works fine. So there: autocompletion is an "IDE" feature in Google Sheets. Commented May 22 at 12:59
• Did you test "0.5" abbreviated to ".5"? That's the one we're interested in here. Commented May 22 at 13:00

# APL (Dyalog Unicode), 87 71 bytes

Anonymous infix lambda taking [a,b] as left argument, [c,d] as right argument, and returning [x,y] for $$\x+yi\$$.

{(*-/⍵×⌽p)×2 1○+/⍵×p←({0=⍺:○.5××⍵⋄(¯3○⍵÷⍺)+○(0>⍺)×¯1*0>⍵}/⍺),.5×⍟+/⍺×⍺}

Try it online!

{} is an anonymous lambda with as left argument and as right argument.

The only operations used (and only on reals) are:

• × multiplication
• prefix natural logarithm
• infix × multiplication
• infix , concatenation
• f/ f-reduction
• :… ternary (if … then … else …)
• infix = equality
• prefix × sign
• prefix pi times
• infix > greater than
• infix * exponentiation
• infix ÷ division
• prefix ¯3○ arctangent
• infix assignment
• prefix 2 1○ cosine and sine
• prefix reverse list
• infix - subtraction
• prefix * exponential

Note that there's no atan2 on the list. I implement it as $$\\text{atan2}(a,b)=\big(\tan^{-1}\frac{b}a\big)+\pi\Big(\frac{(1-2[0>b])\,\text{sgn}\,a}2-[0>b]\Big)\$$

• doesn't agree with * (nor wolframalpha) for ¯1 1 1 1, i think the problem is that you're using arctan instead of atan2 Commented May 21 at 18:50
• @RubenVerg Implemented atan2 now.
Commented May 22 at 4:49

Expects $$\a\$$, $$\b\$$, $$\c\$$, $$\d\$$ in $$\A1\$$, $$\B1\$$, $$\C1\$$, $$\D1\$$ respectively and it returns the result in two horizontally adjacent cells.

=LET(a,LN(A1^2+B1^2)/2,b,ATAN2(A1,B1),c,EXP(C1*a-D1*b),d,C1*b+D1*a,{c*COS(d),c*SIN(d)})

# Python, 107 bytes

lambda a,b,c,d:[g(c*(p:=atan2(b,a))+d*log(h:=hypot(a,b)))*h**c/exp(d*p)for g in[cos,sin]]
from math import*

Attempt This Online!

A port of Arnaulds JS answer

# Python 3, 89 bytes

import math
e=lambda a,b,c,d:math.e**((c+1j*d)*(math.log(a**2+b**2)/2+1j*math.atan2(b,a)))

Try it online

• -10 chars: lambda a,b,c,d:e**((c+1j*d)*(log(a**2+b**2)/2+1j*atan2(b,a))) (\n) from math import* Commented May 22 at 12:43
• Nice, but c+1j*d looks like it violates "Built-in complex arithmetic is not allowed"? Commented May 22 at 12:45
• Looking back, it does. Commented May 22 at 12:46
• I see, that's unfortunate :(. I found a 108 bytes, but I don't think less than 107 is doable. Commented May 22 at 13:45

# Python + numpy, 115 bytes

-9 bytes thanks to corvus_192

lambda x,y:[(e:=exp(linalg.det([y,t:=[arctan2(*x[::-1]),log(hypot(*x))]])))*cos(y@t),e*sin(y@t)]
from numpy import*

Attempt This Online!

If someone has a trick to remove a few of these 12 pairs of brackets...

### Explanation

To compute (a + i b) ** (c + i d), it is helpful to have the two quantities

c log(norm(a,b)) - d atan2(b,a)
c atan2(b,a) + d log(norm(a,b))

It turns out that withy = (c,d) and t = (atan2(b,a), log(norm(a,b))):

• the first is the determinant det([y,t]), and
• the second is the dot product y@t.
• 115 bytes: lambda x,y:[(e:=exp(linalg.det([y,t:=[arctan2(*x[::-1]),log(hypot(*x))]])))*cos(y@t),e*sin(y@t)];from numpy import* Commented May 22 at 16:10
• @corvus_192 Thanks! 2 pairs down, 12 to go!
– Stef
Commented May 22 at 16:16