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

Patched Marble Packages for Debian Stretch

The Marble program in Debian stretch has a problem: the API for the interface to Open Street Map search changed, but the Debian version of Marble has not been updated, so the search functionality is broken. I reported this bug in Debian package bug 903491. One individual submitted a patch to the bug report. I downloaded the deb source for this package, added that patch into the deb, and rebuilt the package. For those who do not want to wait for the official fix, the patched marble debs are available here:

ftp://lavender.qlfiles.net/marble/stretch-search-bar-fix/

(WordPress.com mysteriously does not allow ftp links to be rendered, so you must copy and paste that into you Web browser address bar or your ftp client.)

You’ll need to download all the files that end in “deb”, except the ones with the word “dbgsym” or “dev” which are not required. Then run the command

dpkg -i <packages>

where <packages> is a list of all those package file names exactly, separated by spaces. dpkg -i *.deb might work for you.

When I ran the command, the marble-maps deb did not install, because of a missing dependency. But since the marble-maps package was already installed from the original installation, this did not matter. I started the program, and was able to do searches as normal.

screenshot from 2019-01-07 20-09-03The reason this is important to me is it allows me to do map searches without having to use Web browser application code that I do not trust. (I think though, to submit OSM contributions, you would still need to use the Web browser application.) Also Marble has some nice features like the various map sources that are available.

 

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

Snipe-IT Asset Management

I want to promote a free software asset management system (marketed as Open Source). This is of interest to free software advocates who need an asset management system, and want to support a free software platform. Home page is:

https://snipeitapp.com/

homepageYou could download and install your own server, complete with source code. But if you are busy running a business, you will probably want to use the paid hosted service. Here is what your dashboard looks like, once you have some inventory:

dashboard

I’ve pixel-ized out a lot of data, not wanting to get in trouble for revealing internal company data. But I use this every day for management of my enterprise sales inventory. Mainly I use it for tracking assets and consumables. Consumables are batches of stuff that get used up, but the individual items in the batch don’t need to be tracked (other than decrementing the batch count).

Consumable View

Assets are items that are tracked individually and have some unique asset tag assigned to each unit (e.g., serial no can be used). You create the model numbers separately, and then just assign the model no to each asset when you create the asset. Since the product image only needs to be assigned to the model no once, it is easy to visualize your asset inventory with product images:

asset-view

By default, there isn’t a Status category for “sold” items (you could add one) — personally I just use the Deployed status to show it has been taken out of inventory. And if I need an inventory report, I just generate a report of “Ready to Deploy” items.

Not saying it is the perfectly ideal inventory system in every imaginable way, but it has been very useful to me for everyday practical inventory management, and it is also a free software project. I use it at my desk with a barcode scanner, and also we set up a tablet with another barcode scanner to use out in the warehouse. My experience with the hosted service has been very stable, and I’ve received good support from the company behind it.