Beautiful Complex Number Traces

I got a lot of work done on the bookkeeping, but I couldn’t go to bed without playing with the complex number plots some more. I found an a way to produce some attractive plots. First, I needed this additional function, to make experimentation easier:

(define (complexplot f m n)
    (map (lambda (n)
           (complex2vec (f n)))
         (range m n)))))

The idea I came across was taking the standard unit circle rotation, and multiplying that by samples of the cosine wave:

screenshot from 2019-01-24 23-31-35


If I change the “sampling rate” to various fractions of Pi, I get other interesting geometry. Here is Pi/4:

Screenshot from 2019-01-24 23-32-22.pngAnd Pi/7:

screenshot from 2019-01-24 23-32-12

Complex Numbers and Programming

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:
screenshot from 2019-01-03 13-08-46
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:
screenshot from 2019-01-03 21-03-29

TinyScheme Extensions Package

I created a package for LibreCMC / OpenWRT which seems to work well, though I have not had the chance yet to submit it to the official LibreCMC package repository. Here is my package commit:

A build for for mips24_kc architecture is available here:

That should install onto TPE-R1100, GL-AR150, GL-AR300M (NAND), and GL-AR300M16.

Here is a (trimmed down) sample program which comes with the TSX source code:

; load TinyScheme extension
(load-extension "/usr/lib/tinyscheme/tsx")

; get user's home dir from HOME environment var
(define homedir (getenv "HOME"))
(display "Listing contents of ") (display homedir) (newline)

; create directory stream to read dir entries
(define dirstream (open-dir-stream homedir))
(if (not dirstream)
 (display "Unable to open home directory!! Check value of HOME environment var.")

(let listentry ((entry (read-dir-entry dirstream)))
 (if (eof-object? entry)
 (display entry)
 (listentry (read-dir-entry dirstream)))))

(close-dir-stream dirstream)

It lists the files in the home directory:

root@pathos:~# tinyscheme listhome.scm
Listing contents of /root

Of course, this is a bit simpler:

(load-extension "/usr/lib/tinyscheme/tsx")
(system "ls ${HOME}")

As I mentioned previously, TinyScheme does not currently utilize ld.conf, so it is necessary to run (load-extension "/usr/lib/tinyscheme/tsx") with the full path to the TSX extension library.


TinyScheme Extensions

In my previous post I contemplated writing my own interface to more system functions, but then I discovered this had already been done:

TinyScheme Extensions

After installation of the library:

christopher@evenstar:~$ ls /usr/local/lib/ts/

christopher@evenstar:~$ tinyscheme 
TinyScheme 1.41

ts> (load-extension "/usr/local/lib/ts/tsx")

ts> (system "grep 'model\ name' /proc/cpuinfo | head -n 1")
model name : Intel(R) Core(TM) i5-3320M CPU @ 2.60GHz

One annoying detail here is that TinyScheme 1.41 does not use paths to find the library (even though the documentation says it does). This means you must specify the full path to the extension library. Perhaps I could get this added into my own release of TinyScheme.