# Tag Info

## Hot answers tagged complex-numbers

42

Python 2, 98 97 91 84 bytes s=input();L=1 for _ ins*8:s+=1098*int(str(s).translate('0011'*64));L*=10 print s%L This does I/O in decimal. The integers have to be separated by the non-alphanumeric character +. Thanks to @xnor for golfing off 2 bytes! Try it on Ideone. How it works In Arithmetic in Complex Bases, the author shows how to add and multiply ...

29

APL (NARS), 2 bytes √ NARS has built-in support for quaternions. ¯\_(⍨)_/¯

22

MATL, 7 bytes BJ*Y'^s Try it online! How it works Consider input 4538 for example. B % Implicit input. Convert to binary % STACK: [1 0 0 0 1 1 0 1 1 1 0 1 0] J* % Multiply by 1i % STACK: [1i 0 0 0 1i 1i 0 1i 1i 1i 0 1i 0] Y' % Run-length encoding % STACK: [1i 0 1i 0 1i 0 1i 0], [1 3 2 1 3 1 1 1] ^ % Power, element-wise %...

22

TI-Basic, 26 21 bytes ~3→Xmin 3→Xmax Prompt N DrawF N^Xcos(πX Output for N=2:

20

Charcoal, 32 29 27 bytes ≔⁰θＦＳ≔⁺×θＸ³℅ι℅ιθ‹³﹪θ⁸§1ijkθ Try it online! Link is to verbose version of code. Explanation: The values are encoded to integers equivalent to 0..7 (modulo 8) in the order 1, i, j, k, -1, -i, -j, -k. The multiplications by i, j and k have the following effect on the integer: Multiplying by i is equivalent to tripling the integer and ...

19

Ruby, score -2 (13 bytes, -15 bonus) ->n{[1,90*n]} Features include: no rounding errors! (if you pass the input as a Rational) posted by the author, Kezz101 If you support any real number you can output in any valid complex form. Negatives scores make my adrenaline rush forth. Thus the rules get abused are made use of to achieve this noble goal. ...

16

Pyth, 34 bytes _shM.u,%J/eMeN\12-+PMeNm.B6/J2k,kQ Try it online: Demonstration or Test Suite (takes quite a while). It should satisfy the time restriction though easily, since the online compiler is quite slow in comparison with the normal (offline) compiler. Explanation: My algorithm is basically an implementation of addition with carrying. But instead ...

15

Bash + Gnuplot, 56 45 bytes (-11 bytes thanks to Noiralef!) gnuplot -e "se t png;p[-3:3]real((-$1)**x)">A Saves the resulting graph as a png image named A in the current working directory. Example Outputs For n = 1: For n = 2: 14 CJam, 12 characters - 5 = 7 1'iW"-i"]li= Test it here. Supports negative inputs. 1 "Push 1."; 'i "Push the character i."; W "Push -1."; "-i" "Push the string -i."; ] "Wrap all four in an array."; li "Read STDIN and convert to integer."; = "Access array. The index is ... 14 Python 2 - (24-5)=19 lambda n:'1i--'[n%4::-2] Most credit belongs to @user2357112, I just golfed his answer from the comments on this answer a bit more. Explanation: Starts at the index n%4 in the string '1i--'. Then iterates backwards in steps of two over each letter in the string. So, for example, n=6 would start at index 2, the first -, then skip the i ... 14 Python 2, 62 bytes def f(s):b,a=[eval(s)/2.for j in-1,1];print'%d+j*%d'%(a+b,a-b) We simply evaluate the expression s with j=1 and j=-1, and output half their sum and half their difference as the coefficients of 1 and j. This works because both j=1 and j=-1 satisfy the defining equation defining equation j*j==1. So, the original and simplified expressions ... 14 Python 3 with matplotlib, 103 72 bytes -12 bytes thanks to DSM (a module is installed alongside matplotlib called pylab with the necessary functionality "making Python in a repl more like Matlab" - odd, but true!) -18 more as a result (pylab has many numpy functions too!) -1 byte thanks to Ajasja (replacing arange(-60,61)/20+0j with arange(121)/20-3+0j) ... 13 Jelly, 29 28 26 24 21 20 bytes DBḅ1100ḌµDL+8µ¡Dṣ2ṪḌ This does I/O in decimal. The integers have to be separated by the non-alphanumeric character +. Try it online! or verify all test cases. Background In Arithmetic in Complex Bases, the author shows how to add and multiply complex numbers in bases of the form -n + i. For base -1 + i, addition is done ... 13 Python 2, 69 67 bytes f=lambda a,b:a*a+b*b^58and 2*f(a*b%2*6,f(a/2,b/2))|a+b&1if a else b I/O is done with base 2 integers. -2 thanks @Dennis. 13 Jelly, 8 bytes BŒgaıP€S Try it online! How it works BŒgaıP€S Main link. Argument: n (integer) B Convert to binary. If n = 4538, this yields [1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0]. Œg Group equal elements. This yields [[1], [0, 0, 0], [1, 1], [0], [1, 1, 1], [0], [1], [0]]. aı Logical AND with the imaginary ... 13 Wolfram Language (Mathematica), 67 58 bytes Volume@BoundingRegion[Rest/@PowersRepresentations[#,4,2]]& Try it online! ...& Pure function: PowersRepresentations[#,4,2] Get the sorted reprs. of # as sums of 4 2nd powers Rest/@ Drop the first coordinate of each BoundingRegion[...] Find ... 12 Mathematica, 95 bytes Another implementation of the Cooley–Tukey FFT with help from @chyaong. {n=Length@#}~With~If[n>1,Join[+##,#-#2]&[#0@#[[;;;;2]],#0@#[[2;;;;2]]I^Array[-4#/n&,n/2,0]],#]& Ungolfed FFT[x_] := With[{N = Length[x]}, If[N > 1, With[{a = FFT[ x[[1 ;; N ;; 2]] ], b = FFT[ x[[2 ;; N ;; 2]] ] * Table[E^(-2*I*... 12 Retina, 100 bytes r+(.*)(\d|(?!\4))( .*)(.?)$2$4:$1$3 T 0 +1:11(1*:1*)11 :$1 ^:* ::: }:(1*:1*:)11 1:1$1 (1)*:$#1 This takes the input separated with a comma. The output always starts with three leading zeroes. Try it online! I really wonder if there's a shorter solution for the first stage...

12

MATL, 22 18 16 bytes Thanks @LuisMendo for additional -2 bytes! I_.01I3$:i_y^&XG I_ push 3 and negate .01 push 0.01 I push 3 3$: generate the list [-3,-2.99,-2.98,...,3] i_y^ calculate (-input)^(list) ...

12

R, 29 bytes curve(Re((0i-scan())^x),-3,3) n is provided through stdin. Result for n=1: And for n=2:

11

TI-BASIC (NSpire) - 5 (20 characters-15) cos(nπ/2)+sin(nπ/2)i If you want to recieve a complex return value, replace the i at the end with (complex i).

11

MATLAB, 35 30 bytes x=-3:.01:3;@(n)plot(x,(-n).^x) This defines an anyonmous function. The output is via a new window with a resizable vector graphic output. MATLAB's plot automatically ignores the imaginary part of the y-coordinates as long as your provide corresponding x-coordinates.The following output is for n=3.

11

Mathematica, 41 bytes Plot[Re[(-#)^x],{x,-3,3},PlotRange->All]& Output looks exactly as shown in the challenge except for the font of the numbers (which I suspect was created with Wolfram Alpha).

10

Python 2, 53 bytes f=lambda n,k=0:(n and f(n/2,n%2*(k or 1)*1j))+~n%2*k Been trying to golf this and it seems golfable but I'm out of ideas atm...

10

R, 30 bytes plot(Re((0i-n)^seq(-3,3,.05))) n = 1 n = 2

10

R, 71 48 47 bytes function(A)all(Conj(t(B<-A[,,1]+A[,,2]*1i))==B) Takes a 3D array of real numbers, make a 2D array of imaginary numbers, transpose, conjugate and compare. Thanks to @Giuseppe for reducing the byte count by an astounding 23 bytes, and to @Vlo for the final 1! Try it online! Example: > A <- array(c(2,2,4,2,3,0,4,0,1,0,-1,0,1,0,-...

10

Wolfram Language (Mathematica), 30 bytes (h=2/--√5;h^#-(1-h)^#)√.2& Try it online! Uses $\frac{\sqrt 5+1}{2}=\frac{2}{\sqrt 5-1}$. Mathematica's pre-increment/decrement operators still return the desired value when used on (non-variable) non-atoms. The built-in Fibonacci is a continuous real function, the real part of the Binet formula: \\$F_n=\frac{\...

10

BQN, 14 bytesSBCS ⌊+·(⍋×1<+´)1⊸| Try it here. ⌊+·(⍋×1<+´)1⊸| # Tacit function which takes input as an array of 2 numbers ⌊ # floor + # plus ( ) # the parenthesized train: ⍋ # grade × # times 1<+´ # comparison 1 less than the sum · # applied monadically to ...

9

C, 149 118 characters Edited version (118 characters): int G(int a,int b){a=abs(a);b=abs(b);int n=a*b?a*a+b*b:a+b, d=2;for(;n/d/d&&n%d;d++);return n/d/d|n<2?0:(a+b&3)>2|a*b;} This is a single function: G(a,b) returns nonzero (true) if a+bi is a Gaussian prime, or zero (false) otherwise. It folds the integer primality test into an ...

9

Python, 166 151 150 characters This uses the radix-2 Cooley-Tukey FFT algorithm from math import* def F(x):N=len(x);t=N<2or(F(x[::2]),F(x[1::2]));return N<2and x or[ a+s*b/e**(2j*pi*n/N)for s in[1,-1]for(n,a,b)in zip(range(N),*t)] Testing the result >>> import numpy as np >>> x = np.random.random(512) >>> np.allclose(F(x), ...

Only top voted, non community-wiki answers of a minimum length are eligible