I had a little bit of fun today interacting with the complex number interface in Guile Scheme. I had little interest in complex numbers before watching this great video, which I highly recommend:

It is about an hour long, but worth every minute for the basic insight gained into the workings and value of complex numbers.

In my Guile Scheme exploration, I first defined *pi* and *tau* (*tau *= 2*pi):

scheme@(guile-user)> (define pi 3.141592) scheme@(guile-user)> pi $1 = 3.141592 scheme@(guile-user)> (define tau (* 2 pi)) scheme@(guile-user)> tau $2 = 6.283184

The complex numbers are represented in format a+bi:

scheme@(guile-user)> 1-0i $6 = 1 scheme@(guile-user)> 0-1i $7 = 0.0-1.0i

They can be constructed from the real number components:

scheme@(guile-user)> (make-rectangular 0 1) $8 = 0.0+1.0i

Or from the angle and magnitude:

scheme@(guile-user)> (make-polar 1 (/ pi 2)) $10 = 3.2679489653813835e-7+0.9999999999999466i

The components can be extracted back out:

scheme@(guile-user)> (real-part 2+1i) $11 = 2.0 scheme@(guile-user)> (imag-part 2+1i) $12 = 1.0 scheme@(guile-user)> (magnitude 0+1i) $13 = 1.0

scheme@(guile-user)> (magnitude 0+2i) $14 = 2.0 scheme@(guile-user)> (magnitude 1+1i) $15 = 1.4142135623730951 scheme@(guile-user)> (angle 1+0i) $16 = 0.0 scheme@(guile-user)> (angle 0+1i) $17 = 1.5707963267948966

The interesting operation is exponentiation, which rotates and scales the vector:

scheme@(guile-user)> (expt 1+0i 2)

$20 = 1

scheme@(guile-user)> (expt 2+0i 3)

$21 = 8

scheme@(guile-user)> (expt 1+1i 2)

$22 = 0.0+2.0i

Oh, and Guile allows us to represent a complex number in polar format (magnitude@angle):

scheme@(guile-user)> 1@6.283184 $25 = 0.9999999999991457-1.3071795861522044e-6i scheme@(guile-user)> (make-polar 1 tau) $26 = 0.9999999999991457-1.3071795861522044e-6i scheme@(guile-user)> (expt $26 3) $27 = 0.9999999999923108-3.921538758447679e-6i

Those familiar with complex numbers would be expecting 1+

*τi*raise to the power of three to*be*1-0i, but instead we get an ugly approximation with lots of digits. I find it we get much more simple answers if we round to three digits past the decimal. There is not a function in guile scheme that does exactly that, so far as I know, so we have to multiply, round, and then multiply back again:scheme@(guile-user)> (define (signif x) (* 0.001 (round (* 1000 x)))) scheme@(guile-user)> (signif pi) $36 = 3.142 scheme@(guile-user)> (signif tau) $37 = 6.283

Then we integrate that into a complex number approximation function:

scheme@(guile-user)> (make-polar 1 (/ tau 8)) $32 = 0.7071068967259818+0.7071066656470943i

scheme@(guile-user)> (define (appr z) (make-rectangular (round (real-part z)) (round (imag-part z)))) scheme@(guile-user)> (appr-z $32) $38 = 0.707+0.707i scheme@(guile-user)> (appr-z (expt $32 2)) $39 = 0.0+1.0i

It discovered I could do most of the same things in a LibreOffice Calc spreadsheet, which also happens to have a library of complex number functions, and also allows easy visual representation of the exponentiation of a complex number:

The author considers the programming codes in this blog post to be trivial, and therefore they can be reused without an explicit license.

Oh, by the way, ever heard of this very simple recursive complex number function?:

fc(z) = z^2+c

You might recognize one particular visual representation: