Z Index Vectors
Graphs are beautiful. I had the good fortune to run just the right python command to build the vector field graph above. Let’s run through what it’s describing.
This article was inspired by this article (https://aws.amazon.com/blogs/database/z-order-indexing-for-multifaceted-queries-in-amazon-dynamodb-part-1/). I recommend checking it out!
z-index ordering
My understanding is that z-index ordering is a technique used for making database queries more efficient. It allows the creation of a special “z-index” value for each database entry that can be queried, instead of querying the data in the entry itself.
But that’s not what we are going to talk about.
There is a neat little math trick that z-index uses that I wanted to explore. It also happens to be the backbone of what makes the z-index ordering technique work.
what is it
When you create a z-index value from a set of values, you are basically condensing multiple dimensions down into a single dimension. It’s like taking a set of points and projecting them on a line. n numbers go into the function and 1 value comes out.
how it works
Let’s work with a set of two numbers.
(12, 65)
To get the z-index value of this point, we need to convert those numbers into binary.
(1100, 1000001)
We then combine these binary numbers by combining them in an alternating fashion. I will display 12 (1100) as italic and 65 (1000001) as bold.
111000000001
We start by alternating between the two numbers, then we just add on the rest of the remaining value. This creates a new binary number! Let’s convert it back to decimal.
3585
^^ That is the z-index value of (12, 65).
examples
Let’s consider the following example.
l is a set of random points between 0 and 100 (eg. (2, 4), (67, 54), …).
Plotted, it looks like this.
Now, let’s derive the z-index value for each point, and plot the in orange.
Note: I started scaling the z-index values down to the range 0 -100. I achieved this in the following way
scale = 100 / largest z-index
z-index = z-index(x, y) * scale
This process effectively normalizes the z-index values to 0–100.
This is kind of hard to visualize, let’s plot a line between each point and it’s corresponding z-index.
This is a little better, we can start to see how these points map to z-index values.
I noticed that there seems to be an interesting pattern for this mapping system, and I wanted to see the larger pattern. Vectors are a good way of describing mapping functions such as this, so I built a vector field for the z-index values.
This shows points between 0–100, and the vector that describes it’s z-index value. It essentially shows us what direction and how far the z-index value is.
Immediately, this is striking. Squares seem to pop out of nowhere, grouping certain clusters together.
Let’s look at a larger range, 0–1000.
The patterns are even more pronounced at this scale.
I decided to keep scaling up these vector fields by 1000.
Neat.
It may not be useful, but it is beautiful.