Wednesday, May 27, 2020


What are Quaternions and why would anyone care?

Quaternions are the next step up in complex numbers. Complex numbers are real numbers with an 'imaginary' part. 'imaginary' is in quotes as that's what we call it, but it's not fiction, it's the square root of -1, often represented by i (or j in electrical engineering). Written differently i2=-1. In early days, people considered this strange and useless so imaginary is fitting. By the way, if you go back far enough people didn't see any point in 0 either.

A real number is represented by a - any real number like 2, 1.5781739, 10000, etc. A complex number is represented by a + bi where a and b are real numbers and i2=-1. Complex numbers are very good at represented rotations (as in a circle around 0) which is why they're important in many scientific and engineering activities.

Quaternions are the next step up: a + bi + cj + dk or a0 + ia1 + ja2 + ka3. Here i2 = j2 = k2 = -1. All good so far, but it gets more complex in that ij = k = -ji, jk = i = -kj, and ki = j = -ik. That leads to ijk = -1 which can be seen from ij=k and k*k = -1. The fact that ij = k and not -1 is a little confusing, but these are more than just the square root of -1, but directions in 'quaternion space'. What these look like is the outcome of a standard vector cross product of i x j = k and j x i = -k.

What's interesting about quaternions is that they have almost all the properties of real and complex numbers in terms of addition, subtraction, multiplication, division, and inverses, except that multiplication is not commutattive. In other words, for normal numbers a*b = b*a, but as we saw for quaternions ij = -ji.

In terms of their use, quaternions are useful for rotations in higher dimensions that planes where complex numbers are good. In many ways the 4 dimensional approach resembles relativity theory where time plus the three spatial coordinates are linked or common differential equations where time and space are linked - especially in Schrodinger's equation where time and space are linked via a multiplier of i. For fun purposes, quaternions can replace complex numbers to create higher dimensional Mandelbrot sets. Don't stop there - there are also Tessarines another 4-dimensional complex number were j2 = 1 and Octonions - an 8th dimensional complex number.

The story is that Hamilton was trying to solve a difficult problem and came up with the idea of triplets of complex numbers while out on a walk with his wife by the Royal Canal in Dublin. Supposedly, he was so struck by the idea that he carved i2 = j2 = k2 = -1 and ijk = -1 into the stone of a bridge.

Sunday, February 16, 2020

Other Useful Linux Commands

A collection of other bash and Linux commands ... or solutions

There's obviously no point in detailing all of the Linux/Unix/Bash commands available. Here are a few commands or solutions, I wanted to remember.

Get motherboard or DIMM info:
dmidecode reads information from the DMI (desktop management interface) table which is closely related to the SMBIOS (system management BIOS). Need sudo to run:

dmidecode -t 4 # for CPU info
dmidecode -t 2 # for motherboard info
dmidecode -t memory # for all memory
dmidecode -t 17 # for sodimm information
dmidecode -t 16 # for motherboard info on memory 

There's also lshw, but it wasn't installed on my system.
Related commands: lspci, lsusb, lscpu, lsscsi, lsblk

(Other ls* commands of interest: lsmem, lslocks, lsns, lsipc, lslogins)

Weather on the command line:

While loop failing:
I had a problem where I wanted to check ssh on a number of hosts. Simple - cat the file, pipe into while, use ssh with timeout to do the check like this:
cat host_list.txt | while read h; do timeout 3 ssh $h; done

It didn't work. It just hung on the ssh command until timeout cut the command. ssh seemed to be grabbing standard input (and maybe stdout) and interfering with the while loops input. Switching to a for loop helped with the input. Then ssh didn't seem to be the best choice nor did telnet 22. Netcat in a for loop worked best:
nc -v $host 22 

Problem solved and the hosts with ssh running were found....

ssh starting remote processes:
Here's another one that might be easy or not... With ssh, you can run a remote command:
ssh "ls -l"
What if you want to start a remote process and want it to keep running - something like this:
ssh "nohup java example.jar &"

The problem with the above is that it often won't work - the remote service won't be running. This mostly has to do with terminal control. Despite nohup saying that it is redirecting output to nohup.out, there's still a problem. Two easy solutions:
ssh "nohup java example.jar > ./output_file.out 2>&1 &"

This grabs both standard out and standard error and puts them into output_file.out. Of course, another option is to create a systemd (or init.d) script and use that.

Specifically, inotifywait which will watch file system objects for events that you specify.
inotifywait -r -e access,modify /var/log
This will watch for access or modification updates on files in /var/log.
Combine inotifywait with a while loop as in
while inotifywait -e access /var/log
    some shell commands here

The only downside to inotifywait is that inotify-tools often needs to be installed (yum/dnf install or apt install).