More Fun with Scheme and Complex Numbers

This will be a quick post, as I should be working on some volunteer treasurer work which, I must confess, is infinitely more boring than playing around with Scheme and complex numbers. :) I just wanted to post this screenshot of complex numbers in Dr. Racket:

Screenshot from 2019-01-24 20-57-52.png

Dr. Racket provide a really easy interactive interface, for manipulating plots (and a lot of other graphical things). I just started playing around with it today. Here is a quick explanation of the code:

Preamble stuff:

#lang racket

(require plot)

The plot interface (so far as I know) takes vectors but not straight-up complex numbers, so I needed a conversion function:

(define (complex2vec c) (vector (real-part c) (imag-part c)))

A function to generate a list of numbers is handy:

(define (range m n)
  (if (>= m n) '()
    (cons m (range (+ 1 m) n))))

So, now we generate the plot object:

(define hexspiral
      (map (lambda (n)
             (complex2vec (expt (+ (exp 0+1i) 0+0.1i) n)))
        (range 0 100)))))

What I’m doing here is taking the e^i complex number, and raising it to successive powers, which causes it to rotate around the complex plane origin. Of course, that would simple move around the unit circle over and over again, which would be boring, so I actual started with e^i plus a small complex number which nudges the first point slightly outside the unit circle. This causes the new points to spiral outwards.

Dr. Racket makes a fun scheme playground. Next I want to try some more complex complex-number functions.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s