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)
  (plot
   (lines
    (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

TA DA!

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:

https://gogs.librecmc.org/pi31415/librecmc-package-feed/commit/90ee4c484bfd46ff41358ee6668dab72985b061d

A build for for mips24_kc architecture is available here:

ftp://lavender.qlfiles.net/tinyscheme/tinyscheme-extensions_1.1-1_mips_24kc.ipk

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)
 (begin
 (display "Unable to open home directory!! Check value of HOME environment var.")
 (quit)))

(let listentry ((entry (read-dir-entry dirstream)))
 (if (eof-object? entry)
 #t
 (begin
 (display entry)
 (newline)
 (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
.
..
shellinabox_2.10-1_mips_24kc.ipk
luci-app-shellinabox_0.1-4_mips_24kc.ipk
tinyscheme-extensions_1.1-1_mips_24kc.ipk
tinyscheme-embedded_1.41-2_mips_24kc.ipk
srepl.scm
tinyscheme_1.41-2_mips_24kc.ipk
exercises
script.scm
zlib_1.2.11-1_mips_24kc.ipk
listhome.scm

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/
tsx.so

christopher@evenstar:~$ tinyscheme 
TinyScheme 1.41

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

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

One annoying detail here is that TinyScheme 1.41 does not use ld.so.conf 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.

TinyScheme FFI

I started to experiment with using TinyScheme to control a libreCMC system, but then discovered that TinyScheme doesn’t actually come built-in with a system interface, other than basic input/output (from stdio.h). This is not really a deficiency, as TinyScheme was intended to be embedded in a variety of systems and applications. TinyScheme has a Foreign Function Interface (FFI) allowing us to turn C functions into scheme functions, so adding in more system functions should in principle be fairly simple.

One difficulty, though, is that the examples in the documentation did not actually match the FFI in the release. So, I had to spend some quality time in the header files. I was then able to modify the examples to work. This C program loads and runs a scheme file called myscheme.scm, while importing a C function called square, which, of course, squares a number:

#include "tinyscheme/scheme.h"
#include "tinyscheme/scheme-private.h"

pointer square(scheme *sc, pointer args) {
  if(args!=sc->NIL) {
    if(sc->vptr->is_number(sc->vptr->pair_car(args))) {
      double v=sc->vptr->rvalue(sc->vptr->pair_car(args));
      return sc->vptr->mk_real(sc,v*v);
    }
  }
  return sc->NIL;
}

int main() {
  scheme * sc;

  FILE *fptr;
  // initialize the scheme invironment
  sc = scheme_init_new();
  // set output to go to STDOUT by default
  scheme_set_output_port_file(sc, stdout);
  // Load the init file
  fptr = fopen("/usr/lib/tinyscheme/init.scm", "r");
  scheme_load_file(sc, fptr);
  fclose(fptr);

  // Import square fn
  sc->vptr->scheme_define(sc,
  sc->global_env,
  sc->vptr->mk_symbol(sc,"square"),
  sc->vptr->mk_foreign_func(sc, square));
 
  // Load my scheme program
  fptr = fopen("myscheme.scm", "r");
  scheme_load_file(sc, fptr);
  fclose(fptr);
  // de-initialize the scheme environment
  scheme_deinit(sc);
  return 0;
}

And here is the scheme program:

(display (square 4))
(newline)

And here is execution:

christopher@evenstar:~/Devel/embedded-ts$ gcc -o loader -DUSE_DL loader.c -ltinyscheme
christopher@evenstar:~/Devel/embedded-ts$ ./loader 
16.0

Next I should be able to add functions from <stdlib.h>, e.g., system() to call shell commands.