Exponent of complex numbers

10

1

Given two integers, which may be negative, zero, or positive, a and b (taken in any reasonable format, including inputting a plain complex number), convert it to a + bi where i is the imaginary number (square root of negative one). Then, raise it to the power of a third (positive integer) input variable, c as to (a + bi)c. You should then end up with something like d + ei. You must then output, or return, d and e in any reasonable format (including outputting a plain complex number).

Input and output may be taken or outputted in any order.

Examples:

5, 2, 2 -> 21, 20
1, 4, 2 -> -15, 8
-5, 0, 1 -> -5, 0

Okx

Posted 2017-10-14T07:43:40.743

Reputation: 15 025

If we use de Moivre's formula, is floating point imprecison allowed? – Giuseppe – 2017-10-14T12:03:04.180

@Giuseppe Yes, that's okay. – Okx – 2017-10-14T13:12:59.857

4FWIW I think the change to the rules (allowing a fully flexible I/O) made a fairly interesting challenge pretty dull. – Jonathan Allan – 2017-10-15T20:50:10.017

@JonathanAllan at least for languages with native complex number support -- which are quite many :( – Felix Palmen – 2017-10-17T11:22:54.497

@JonathanAllan I can't please everyone :( – Okx – 2017-10-17T12:08:03.163

@Okx I know, just pointing out to others that I (and it seems from upvotes, others) prefered your original idea :) – Jonathan Allan – 2017-10-17T12:43:12.497

Answers

7

Mathematica, 17 bytes

ReIm[(#+#2I)^#3]&

Try it online!

-8 bytes from alephalpha

but........ rules have changed...... so

Mathematica, 5 bytes

Power

J42161217

Posted 2017-10-14T07:43:40.743

Reputation: 15 931

5{Re@#,Im@#}& -> ReIm – alephalpha – 2017-10-14T08:24:03.750

117 bytes. You can simply remove @#&. – Mr. Xcoder – 2017-10-14T08:31:49.220

haha, yes, my mistake – J42161217 – 2017-10-14T08:33:00.650

You can now do #^#2& or just Power. – totallyhuman – 2017-10-15T12:25:21.793

6

Python 3, 3 bytes

pow

Try it online!

Input and output as complex numbers.


Python 3, 47 bytes

def f(a,b,c):n=(a+b*1j)**c;return n.real,n.imag

Try it online!

Input and output as integers


Python 2, 62 60 bytes

-2 bytes thanks to @Leonhard

a,b,c=input();d=1;e=0
exec'd,e=a*d-b*e,a*e+b*d;'*c
print d,e

Try it online!

does not use complex number type

ovs

Posted 2017-10-14T07:43:40.743

Reputation: 21 408

4

Javascript (ES6), 51 50 bytes

a=>b=>g=c=>c?([x,y]=g(c-1),[x*a-b*y,a*y+b*x]):"10"
  • Takes input in currying form: f(a)(b)(c)
  • Returns the result as an array: [d, e]

Explanation

a=>b=>g=c=>               // Input in currying syntax
    c?(                   // If `c` != 0:
        [x,y]=g(c-1),     //     Set [x, y] to the result of f(a)(b)(c-1)
        [x*a-b*y,a*y+b*x] //     Return (a + bi) * (x + yi)
    ):                    // Else: (when c = 0)
        "10"              //     Return [1, 0] (to end the recursion)

f=a=>b=>g=c=>c?([x,y]=g(c-1),[x*a-b*y,a*y+b*x]):"10"
<div oninput="o.innerText=f(a.value)(b.value)(c.value)"><input id=a type=number value=0><input id=b type=number value=0><input id=c type=number value=1 min=1><pre id=o>

Herman L

Posted 2017-10-14T07:43:40.743

Reputation: 3 611

3

Jelly, 1 byte

*

Try it online!

Thanks to Mr. Xcoder for alerting me of rule updates (-6 as a result).
Thanks to someone for alerting me of rule updates (-2 as a result).

First argument: (a+bj)
Second argument: c
Returns: (d+ej)

Erik the Outgolfer

Posted 2017-10-14T07:43:40.743

Reputation: 38 134

8 bytes – user202729 – 2017-10-14T08:43:41.780

7 bytes – Jonathan Allan – 2017-10-14T12:03:35.737

In fact Jonathan's 3 byter would suffice; ḅı*, as the rules have changed and you are now allowed to output a plain complex number. – Mr. Xcoder – 2017-10-15T05:10:19.883

@Mr.Xcoder was sleeping when that happened – Erik the Outgolfer – 2017-10-15T07:54:14.710

1It seems like a * onebyter is ok now as you can take input as a complex – my pronoun is monicareinstate – 2017-10-15T11:29:27.117

@someone thanks – Erik the Outgolfer – 2017-10-15T12:22:34.503

Works in Dyalog APL too. – Adám – 2017-10-17T08:01:06.427

3

Actually, 1 byte

Try it online!

Note that the rules have changed and complex numbers are valid I/O types (unfortunately this turns the post into a "perform this exponentiation" challenge). Original answer below.

Actually, 3 bytes

Çⁿ╫

Try it online!

Returns the values separated by a newline. Takes the inputs in reverse order and returns the results in reverse order (See the tio link).

Çⁿ╫  - Full program. Reversed inputs.

Ç    - Return a+bi.
 ⁿ   - Exponentiation.
  ╫  - Pushes the real and imaginary parts of a.

Mr. Xcoder

Posted 2017-10-14T07:43:40.743

Reputation: 39 774

3

Pari/GP, 36 bytes

f(a,b,c)=divrem((a+b*x)^c%(x^2+1),x)

Try it online!


Pari/GP, 36 bytes

f(a,b,c)=[real(d=(a+b*I)^c),imag(d)]

Try it online!

alephalpha

Posted 2017-10-14T07:43:40.743

Reputation: 23 988

3

R, 3 bytes

This is becoming boring. If input and output is allowed as a complex number, there is a builtin for a power function.

`^`

For example:

> (5+2i)^2
[1] 21+20i
> (1+4i)^2
[1] -15+8i
> (-5+0i)^1
[1] -5+0i

or

> `^`(5+2i,2)
[1] 21+20i
> `^`(1+4i,2)
[1] -15+8i
> `^`(-5+0i,1)
[1] -5+0i

djhurio

Posted 2017-10-14T07:43:40.743

Reputation: 1 113

2

Pyth, 5 12 5 2 bytes

^E

Takes in c first, followed by a+bj.

7 bytes of boilerplate because apparently output as an imaginary number is disallowed. It's been re-allowed! Hurrah! And with taking in a complex number being a reasonable input, we can cut out an additional 3 bytes!

Previous solutions:

^.jEE

When complex numbers were not reasonable inputs.

m,edsd]^.jEE

When complex numbers were not reasonable outputs.

Test Suite.

Steven H.

Posted 2017-10-14T07:43:40.743

Reputation: 2 841

2

05AB1E, 1 byte

m

Try it online!

Input: c\n(a+bj)
Output: (d+ej)

Erik the Outgolfer

Posted 2017-10-14T07:43:40.743

Reputation: 38 134

Same tip as Neil, 'jì+³m is a different way to calculate the number.

– Adnan – 2017-10-14T10:58:38.167

@Adnan at least for me it does matter indeed :p – Erik the Outgolfer – 2017-10-14T12:35:34.877

2

C# (.NET Core), 62 38 bytes

a=>c=>System.Numerics.Complex.Pow(a,c)

Try it online!

my pronoun is monicareinstate

Posted 2017-10-14T07:43:40.743

Reputation: 3 111

You should include the .Real and .Imaginaryin your answer.. According to the rule "_You must then output, or return,dande` in any reasonable format (not including outputting a plain complex number)_" you're not allowed to just output the Complex number. – Kevin Cruijssen – 2017-10-14T14:02:57.880

2

05AB1E, 20 19 17 16 bytes

‚UTSsFXâP`(‚RŠ‚+

Try it online! Takes three separate inputs in the order b, a, c and outputs an array [d, e]. Edit: Saved 2 bytes thanks to @Datboi. Saved 1 byte thanks to @Adnan. Explanation:

‚                   Join a and b into a pair
 U                  Store in variable X
  T                 Push 10 to the stack
   S                Split into the pair [d, e] = [1, 0]
    s               Swap with c
     F              Repeat the rest of the program c times
      X             Get [a, b]
       â            Cartesian product with [d, e]
        P           Multiply each pair together [da, db, ea, eb]
         `          Push each product as a separate stack entry
          (         Negate eb
           ‚        Join with ea into a pair [ea, -eb]
            R       Reverse the pair [-eb, ea]
             Š      Push under da and db
              ‚     Join them into a pair [da, db]
               +    Add the two pairs [da-eb, db+ea]

Neil

Posted 2017-10-14T07:43:40.743

Reputation: 95 035

Input and output may be taken or outputted in any order. - That means that you can take the first two inputs in reverse order. – Mr. Xcoder – 2017-10-14T10:27:20.983

@Mr.Xcoder Thanks, I hadn't noticed that. – Neil – 2017-10-14T10:33:35.140

I'm not sure if it matters or not, but calculating the number can also be done with 'jì+³m.

– Adnan – 2017-10-14T10:53:27.927

You can replace 1 0‚ with TS for -2 bytes :) – Datboi – 2017-10-14T22:32:25.027

And P automatically vectorizes, so you don't need the . – Adnan – 2017-10-15T08:18:34.943

@Adnan so what's "total product" supposed to mean then? – Neil – 2017-10-15T09:26:31.633

Yes, docs are crap, but I'm planning on changing this when I have some free time (with examples etc.). – Adnan – 2017-10-15T09:29:26.070

2

TI-BASIC, 25 22 8 bytes

Takes the complex number and the exponent as input, and stores output in Ans as a complex number. Drastic drop in bytes due to loosened restrictions on input/output.

Prompt C,E
C^E

kamoroso94

Posted 2017-10-14T07:43:40.743

Reputation: 739

You can save 2 bytes with imag({iAns,Ans in the last line (by i I mean the complex number i). – Misha Lavrov – 2017-10-14T20:57:14.827

1And I guess then one more byte by just combining the two lines into imag({i,1}(A+Bi)^C. – Misha Lavrov – 2017-10-14T21:01:20.590

1Rules have changed, now you can take input and return complex numbers, if that is of any help. – Erik the Outgolfer – 2017-10-15T12:11:33.667

2

J, 10, 7, 1 bytes

^

Takes c as the right argument and the complex number ajb (how you represent a + bi in J) as the left argument.

Try it online!

Other Solutions

7 bytes

Takes the complex number input as a list.

^~j./@]

10 bytes

This outputted the a + bi in the list a b.

+.@^~j./@]

I wanted to try something cute like ^~&.(j./) but the inverse of j./ is obviously not defined. Actually, ^~&.(+.inv) works and you can make that ^&.(+.inv) which is also 10 bytes if you reverse the order in which you take the args.

cole

Posted 2017-10-14T07:43:40.743

Reputation: 3 526

2

6502 machine code subroutine, 199 187 185 bytes

A2 03 B5 FB 95 26 CA 10 F9 88 D0 01 60 A5 26 85 61 A5 27 85 62 A5 FB 85 63 A5
FC 85 64 A9 20 85 6F D0 36 18 A5 6D 65 65 85 28 A5 6E 65 66 85 29 A5 4B 85 26
A5 4C 85 27 50 CF 38 A5 6D E5 65 85 4B A5 6E E5 66 85 4C A5 28 85 61 A5 29 85
62 A5 FB 85 63 A5 FC 85 64 06 6F A9 00 85 65 85 66 A2 10 46 62 66 61 90 0D A5
63 18 65 65 85 65 A5 64 65 66 85 66 06 63 26 64 CA 10 E6 A9 FF 24 6F 70 B9 30
02 F0 9E A5 65 85 6D A5 66 85 6E 24 6F 30 14 A5 28 85 61 A5 29 85 62 A5 FD 85
63 A5 FE 85 64 06 6F D0 B4 A5 26 85 61 A5 27 85 62 A5 FD 85 63 A5 FE 85 64 06
6F B0 A0
  • -12 bytes with improved "spaghetti" structure
  • -2 bytes changing the register to pass the exponent, so we can make use of zeropage addressing mode in the initial copy loop

This is position-independent code, just put it somewhere in RAM and call it with a jsr instruction.

The routine takes the (complex) base as two 16bit signed integers (2's complement, little-endian) in $fb/$fc (real) and $fd/$fe (imaginary), and the exponent as an unsigned 8bit integer in the Y register.

The result is returned in $26/$27 (real) and $28/$29 (imaginary).


Explanation

This is still an interesting challenge on the 6502 CPU as there are no instructions for even multiplying. The approach is straight forward, implementing a complex multiplication and executing it as often as required by the exponent. Golfing is done by avoiding subroutines, instead creating kind of a "branch spaghetti", so the code for doing a simple 16bit multiplication that's needed multiple times is reused with the lowest possible overhead. Here's the commented disassembly:

 .cexp:
A2 03       LDX #$03            ; copy argument ...
 .copyloop:
B5 FB       LDA $FB,X
95 26       STA $26,X
CA          DEX
10 F9       BPL .copyloop       ; ... to result
 .exploop:
88          DEY                 ; decrement exponent
D0 01       BNE .mult           ; zero reached -> done
60          RTS
 .mult:                         ; multiply (complex) result by argument
A5 26       LDA $26             ; prepare to multiply real components
85 61       STA $61             ; (a*c)
A5 27       LDA $27
85 62       STA $62
A5 FB       LDA $FB
85 63       STA $63
A5 FC       LDA $FC
85 64       STA $64
A9 20       LDA #$20            ; marker for where to continue
85 6F       STA $6F
D0 36       BNE .mult16         ; branch to 16bit multiplication
 .mult5:
18          CLC                 ; calculate sum (a*d) + (b*c)
A5 6D       LDA $6D
65 65       ADC $65
85 28       STA $28             ; and store to imaginary component of result
A5 6E       LDA $6E
65 66       ADC $66
85 29       STA $29
A5 4B       LDA $4B             ; load temporary result (a*c) - (b*d)
85 26       STA $26             ; and store to real component of result
A5 4C       LDA $4C
85 27       STA $27
50 CF       BVC .exploop        ; next exponentiation step
 .mult3:
38          SEC                 ; calculate difference (a*c) - (b*d)
A5 6D       LDA $6D
E5 65       SBC $65
85 4B       STA $4B             ; and store to temporary location
A5 6E       LDA $6E
E5 66       SBC $66
85 4C       STA $4C
A5 28       LDA $28             ; prepare to multiply real component of result
85 61       STA $61             ; with imaginary component of argument
A5 29       LDA $29             ; (a*d)
85 62       STA $62
A5 FB       LDA $FB
85 63       STA $63
A5 FC       LDA $FC
85 64       STA $64
06 6F       ASL $6F             ; advance "continue marker"
 .mult16:
A9 00       LDA #$00            ; initialize 16bit multiplication
85 65       STA $65             ; result with 0
85 66       STA $66
A2 10       LDX #$10            ; bit counter (16)
 .m16_loop:
46 62       LSR $62             ; shift arg1 right
66 61       ROR $61
90 0D       BCC .m16_noadd      ; no carry -> nothing to add
A5 63       LDA $63             ; add arg2 ...
18          CLC
65 65       ADC $65
85 65       STA $65
A5 64       LDA $64
65 66       ADC $66
85 66       STA $66             ; ... to result
 .m16_noadd:
06 63       ASL $63             ; shift arg2 left
26 64       ROL $64
CA          DEX                 ; decrement number of bits to go
10 E6       BPL .m16_loop
A9 FF       LDA #$FF            ; check marker for where to continue
24 6F       BIT $6F
70 B9       BVS .mult3
30 02       BMI .saveres        ; have to save result to temp in 2 cases
F0 9E       BEQ .mult5
 .saveres:
A5 65       LDA $65             ; save result to temporary
85 6D       STA $6D
A5 66       LDA $66
85 6E       STA $6E
24 6F       BIT $6F             ; check "continue marker" again
30 14       BMI .mult4
 .mult2:
A5 28       LDA $28             ; prepare to multiply imaginary components
85 61       STA $61             ; (b*d)
A5 29       LDA $29
85 62       STA $62
A5 FD       LDA $FD
85 63       STA $63
A5 FE       LDA $FE
85 64       STA $64
06 6F       ASL $6F             ; advance "continue marker"
D0 B4       BNE .mult16         ; branch to 16bit multiplication
 .mult4:
A5 26       LDA $26             ; prepare to multiply imaginary component of
85 61       STA $61             ; result with real component of argument
A5 27       LDA $27             ; (b*c)
85 62       STA $62
A5 FD       LDA $FD
85 63       STA $63
A5 FE       LDA $FE
85 64       STA $64
06 6F       ASL $6F             ; advance "continue marker"
B0 A0       BCS .mult16         ; branch to 16bit multiplication

Example program using it (C64, assembly source in ca65-syntax):

.import cexp

CEXP_A          = $fb
CEXP_AL         = $fb
CEXP_AH         = $fc
CEXP_B          = $fd
CEXP_BL         = $fd
CEXP_BH         = $fe

CEXP_RA         = $26
CEXP_RAL        = $26
CEXP_RAH        = $27
CEXP_RB         = $28
CEXP_RBL        = $28
CEXP_RBH        = $29

.segment "LDADDR"
                .word   $c000

.segment "MAIN"
                jsr     $aefd           ; consume comma
                jsr     $ad8a           ; evaluate number
                jsr     $b1aa           ; convert to 16bit int
                sty     CEXP_AL         ; store as first argument
                sta     CEXP_AH
                jsr     $aefd           ; ...
                jsr     $ad8a
                jsr     $b1aa
                sty     CEXP_BL         ; store as second argument
                sta     CEXP_BH
                jsr     $b79b           ; read 8bit unsigned into X
                txa                     ; and transfer
                tay                     ; to Y

                jsr     cexp            ; call our function

                lda     CEXP_RAH        ; read result (real part)
                ldy     CEXP_RAL
                jsr     numout          ; output
                ldx     CEXP_RBH        ; read result (imaginary part)
                bmi     noplus
                lda     #'+'            ; output a `+` if it's not negative
                jsr     $ffd2
noplus:         txa
                ldy     CEXP_RBL
                jsr     numout          ; output (imaginary part)
                lda     #'i'
                jsr     $ffd2           ; output `i`
                lda     #$0d            ; and newline
                jmp     $ffd2

numout:
                jsr     $b391           ; convert to floating point
                jsr     $bddd           ; format floating point as string
                ldy     #$01
numout_loop:    lda     $ff,y           ; output loop
                bne     numout_print    ; until 0 terminator found
                rts
numout_print:   cmp     #' '            ; skip space characters in output
                beq     numout_next
                jsr     $ffd2
numout_next:    iny
                bne     numout_loop

Online demo

Usage: sys49152,[a],[b],[c], e.g. sys49152,5,2,2 (Output: 21+20i)

Felix Palmen

Posted 2017-10-14T07:43:40.743

Reputation: 3 866

1

MATL, 1 byte

^

Inputs are a+jb, c.

Try it online!

Old version: non-complex input and output, 8 bytes

J*+i^&Zj

Input order is b,a, c.

Try it online!

Explanation

J           Push imaginary unit
 *          Multiply by implicit input b
  +         Add implicit input a
   i        Take input c
    ^       Power
     &Zj    Push real and imaginary parts. Implicitly display

Luis Mendo

Posted 2017-10-14T07:43:40.743

Reputation: 87 464

*Multiply by implicit input b* - *Add implicit input b*. Did you mean a in either of those? – Mr. Xcoder – 2017-10-14T17:13:04.767

@Mr.Xcoder Yes, thanks. Corrected – Luis Mendo – 2017-10-15T02:42:05.610

You can take input in the form of a complex number now, and output in the form of a complex number. You can probably cut out a lot of boilerplate from this answer because of that. – Steven H. – 2017-10-15T11:48:41.790

@StevenHewitt Thanks! Edited now – Luis Mendo – 2017-10-15T12:36:53.467

1

Dyalog APL, 10 bytes

⎕*⍨⊣+¯11○⊢

Try it online!

a is left argument, b is right argument, and c via input prompt.

Returns a complex number in the format dJe.

Uriel

Posted 2017-10-14T07:43:40.743

Reputation: 11 708

Rules have changed, now you can take input and return complex numbers, if that's of any help. – Erik the Outgolfer – 2017-10-15T12:13:17.630

1

C (gcc), 34 bytes

#define f(a,b,c)cpow((a)+1i*(b),c)

Try it online!

ceilingcat

Posted 2017-10-14T07:43:40.743

Reputation: 5 503

0

Casio-Basic, 6 bytes

a^b

Change to the rules to allow input and output as complex numbers made this significantly shorter.

3 bytes for the function, +3 to enter a,b in the parameters box.

numbermaniac

Posted 2017-10-14T07:43:40.743

Reputation: 639

0

Octave / MATLAB, 6 bytes

@power

Anonymous function that inputs two numbers and outputs their power.

Try it online!

Old version: non-complex input and output, 30 bytes

@(a,b,c)real((a+j*b)^c./[1 j])

Anonymous function that inputs three numbers and outputs an array of two numbers.

Try it online!

Luis Mendo

Posted 2017-10-14T07:43:40.743

Reputation: 87 464

0

Perl 6,  29 26 20 19  11 bytes

{$_=($^a+$^b*i)**$^c;.re,.im}

Try it

{(($^a+$^b*i)**$^c).reals}

Try it

((*+* *i)** *).reals

Try it

((*+* *i)***).reals

Try it

With the change of output restrictions it can be further reduced:

(*+* *i)***

Try it

The *** part is parsed as ** * because the ** infix operator is longer than the * infix operator.

Expanded:

#     __________________ 1st parameter
#    /   _______________ 2nd parameter
#   /   /         ______ 3rd parameter
#  /   /         /
# V   V         V
( * + * * i) ** *
#       ^    ^^
#        \     \________ exponentiation
#         \_____________ multiplication

Brad Gilbert b2gills

Posted 2017-10-14T07:43:40.743

Reputation: 12 713

You can now do (*+* *i)***. – totallyhuman – 2017-10-15T12:19:31.830

0

8th, 38 bytes

Code

c:new dup >r ( r@ c:* ) rot n:1- times

SED (Stack Effect Diagram) is: c a b -- (a + bi) ^ c

Warning: a + bi is left on r-stack, but this doesn't affect subsequent computations.

Ungolfed version with comments

needs math/complex

: f \ c a b  -- (a + bi) ^ c
    c:new                      \ create a complex number from a and b
    dup                        \ duplicate a + bi
    >r                         \ store a + bi on r-stack
    ( r@ c:* ) rot n:1- times  \ raise ( a + bi ) to c
;

Example and usage

: app:main
    \ rdrop is not strictly required
    2 5 2 f . cr rdrop
    2 1 4 f . cr rdrop 
    1 -5 0 f . cr rdrop 
    bye
;

Output of the previous code

c:1:data:{"real":21,"imag":20}
c:1:data:{"real":-15,"imag":8}
c:2:data:{"real":-5,"imag":0}

Chaos Manor

Posted 2017-10-14T07:43:40.743

Reputation: 521

0

R, 25 bytes

simplest - since outputting complex is allowed.

function(a,b,c)(a+b*1i)^c

Zahiro Mor

Posted 2017-10-14T07:43:40.743

Reputation: 371