188 points · by andromeduck · 8y ago · 140 comments
Four 5-bit values in order have 20 bits of entropy, so cannot be stored in 16 bits.
Four 5-bit values without order have 20 - log2(4!) =~ 20 - 4.59 = 15.41 bits of entropy (corresponding to log(2^20/4!) possible configurations), and thus can fit in 16 bits of data if you're clever about it.
If two of the numbers happen to be the same, order no longer matters for those numbers, so your log2(4!) needs to be larger...
log2((32*31*30*29)/(4*3*2) + (32*31*30)/2 + (32*31) + (32*31)/2) = 15.675
which is still smaller than 16.If you want to optimize storage of duplicates, you still have to store the number of duplicate numbers, and you are back where you started.
http://www.inference.org.uk/itprnn/book.html
The intro chapters cover the topic of measuring informational entropy.
Aside: One interesting application is to the problem of “use a balance scale only three times to determine which of twelve balls has the wrong weight (too heavy or light)”. You choose the weighings so as as to maximize the entropy of the outcome i.e. so there are many possible outcomes of equal probability.
4! is the number of permutations of a list - it's commonly seen throughout CS theory and other forms of discrete math.
log_2(n) is the number of bits needed to store a integer from 0 to n - it's reasonably commonly used in CS theory education.
But I might just recommend going to the source! which is Claude Shannon's seminal masters thesis on coding: http://affect-reason-utility.com/1301/4/shannon1948.pdf . It's surprisingly readable and worth at least skimming if you like this stuff.
Or MSAA4x ARGB5555 in 64-bit (instead of 80 bit)
Slightly adrift of the topic...
I have a HAMT (hash array map trie) implementation that uses a 32-bit unsigned int as a bitfield to indicate which of the 32 possible children nodes are populated. With this trick I could encode any node with 5 or fewer bits flagged with a 16-bit unsigned int instead.
I just checked and 1 million keys created roughly 1.3 million nodes (1 million keys, 300k internal nodes). Nearly 90% of the 300k internal nodes have 5 or fewer bits set. This trick would make the HAMT overhead nearly half the size.
Very useful!
Now, how about 6-bit values in one 32-bit value? I'd like to try this with a 64-bit HAMT. Can this be easily generalized?
Edit: Oops, that should be nodes with 4 or fewer bits set, there are only 4, 5-bit values. Interestingly that apparently doesn't make much of a difference for HAMTs only about 2k internal nodes out of 300k have 5 children. So the results remain the same.
Let's assume that our set is (a, b, c, d). It is ordered, therefore a <= b <= c <= d. For example, a valid set is (2, 4, 4, 10).
Consider all possible numbers that may be in our set, laid out in order.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
For each element in the set, we mark its position in this order by placing a marker to the left of the corresponding number.
For our example, we first mark element 2:
0 1 m 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Then 4:
0 1 m 2 3 m 4 5 6 7 8 9 10 11 12 13 14 15
Then, 4 again:
0 1 m 2 3 m m 4 5 6 7 8 9 10 11 12 13 14 15
And finally, 10:
0 1 m 2 3 m m 4 5 6 7 8 9 m 10 11 12 13 14 15
Now, out of these 16+4 places, only 19 of them could be a valid placement for a marker, as a marker should always be placed to the left of a number (or said differently, "15" will always be in the end.)
Therefore, there are 19 choose 4 ways to pick the locations of the markers.
make main
without any makefile at all?If you haven't done so, delete the makefile now (you have it in version control anyway) and give it a try.
https://www.gnu.org/software/make/manual/html_node/Catalogue...
Note: I was initially annoyed with parent's "do this and see what happens" post and its lack of substantive communication, and responded poorly. This comment is substantially edited.
The parent suggests removing the makefile entirely and running `make main`, and observing the result. That would be a learning experience (and much better than your "RTFM")! But, if you don't want to humor that, the thing to say is: The result is that it works, and correctly builds the program, without a makefile at all.
The explanation is that GNU Make has a catalog of built-in rules (which you linked to); but it's quite a leap from knowing that fact, and even making use of them, to realizing that it means in some cases you don't need a makefile at all.
Exactly my intention. Thank you, I was worried after the first response from the other person that I'd worded myself too poorly and that more people would think I was being "self-impressed and coy" when that was not the intention at all.
That's an odd thing to read into my comment. I am merely passing on information that was at one point given to me when I myself had a Makefile that was actually not needed because the program it was for was a single source file project like OP.
Also in what way is it easier to read a long document without reference to a specific section than it is to just delete the makefile and run make and the stem of the name of the source file?
Edit: My second paragraph of this comment was written prior to parent commenter editing their comment.
You may feel differently. That's fine. But differing opinions being downvoted to oblivion is ... problematic, at best.
EDIT: After reading your response below, you were right and I was wrong. Editing.
The first trick is to make a function that can calculate the nth set with k elements from some universe U (in this case U = {0..2^5-1} without order (no duplicates) directly. This is done using the https://en.wikipedia.org/wiki/Combinatorial_number_system. This is very efficient.
Then, to encode duplicates you use the stars and bars trick.
Out of curiosity, do you know of an efficient way to calculate this without sorting first? Or even better, without knowing k ahead of time? If you can sort your input first it's trivial, but even though it seems like a super efficient way to store a set of numbers I don't know how to use it to solve, for example, the other problem in these comments of sorting 1M 32-bit integers in 2MB of RAM (you can store them with this mechanism in about 1.7MB of RAM, but only if they were already sorted).
Here's an example with numbers dumped from a quick prototype encoder. The exact range numbers aren't that important, but note how the range gets smaller for every symbol we encode:
Say we want to encode the ordered numbers 10, 15, 31, 31.
We start with the full range [0, 65536)
There are C(32 - 10 + 2, 3) = 2024 combinations that start with 10, out of 52360 possible, so that narrows it down to around 4% of the full range, we'll use [49702, 52236)
For the next number we only allow numbers 10 and above, 15 has 153 out of 2024 possibilities, range [51022, 51214)
For the next number, 31 is only 1 of the 153 possibilities, so it gets a tiny range, [51212, 51214).
And now 31 is the next number in 1 out of 1 of cases, so the range is again [51212, 51214)
We can code the sequence as either 51212 or 51213, since we used a range that is slightly larger than needed some combinations have multiple codes. We could have started with the smaller range [0, 52360) instead to get a bijection.
Ten people are standing in a line. Each person is wearing a black or a white hat and can only see the colours of the hats of the people in front of them. The goal is to guess the colour of one’s own hat. First, the last person in the line shouts out their guess (‘black’ or ‘white’), then the person in front of them shouts out their guess, etc. No other form of communication is allowed (no clapping, no touching, no texting – and no tricks involving, e.g., encoding information in the delay between it being ‘your turn’ and actually shouting out your guess). The group ‘wins’ if at most one person guesses wrong.
The group is allowed to discuss a strategy before taking part in this puzzle (i.e., before being given their hats). Which strategy should they choose to maximise the chance of winning?
They’re only allowed one wrong guess, so this is basically trying to encode 9 bits of information in 1 bit. And here the ordering of the bits actually matters. Still, it’s possible to choose a strategy which gives the group 100% chance of winning! Good luck trying to solve this one. :)
In the popular, solvable version of the puzzle, the group also gets the information if each guess was wrong or right. It's not at all about encoding "9 bits of information in 1 bit".
I got a worst case of exactly 2 MB (1.907 MiB) (all deltas being 4294, so the list is 0, 4294, 8588...), but maybe it's possible to get better than that.
It would be uber slow though, probably n^2.
(edit: better link)
For (I=0; I<1<<31; I++) For (J=0; J<1e6; J++) If (input[J]==I) print(I);
Shoddy runtime, but hey...
https://en.wikipedia.org/wiki/Sorting_algorithm#Comparison_o...
More relevant to the problem, the order for four five bit values has about 4.59 bits of information (log_2(4!)=4.59). Not all of the sets actually have four numbers- many have duplicate numbers, like the list [1 1 2 3] is a set of numbers {1 2 3}- and that gets us down to 4.08 bits of information which is small enough to make this example work.
At least that's how I understand this. I could be wrong.
http://www.cryptomuseum.com/covert/bugs/selectric/index.htm
Quotes:
"The data from the 6 magnetometers (i.e. 6 bits) was somehow digitally compressed into 4-bit words and then stored in a magnetic-core buffer that could hold up to 8 such 4-bit data words." "It is unknown why and how the data was compressed from 6 to 4 bits, and the NSA report is very vague on this point. It is possible that the Soviets used 4-bit logic and had to spread the 6-bit data over more than one 4-bit data word, but it is more likely that they used frequency analysis"
"According to the NSA report, the Russians compressed the 6-bit data into a 4-bit frequency select word. Although the report doesn't explain what they mean by this, we can make a few educated guesses. The reason for compressing it into 4-bits, was probably the fact that the Russians only had access to 4-bit digital technology at the time. The problem with 4 bits however, is that each data word has just 16 possible combinations"
...
Maybe a similar "hack"?
While developing a CAD system we saved a lot of RAM compared to our competitor's product. They were storing each (X1,Y1)(X2,Y2) as four integers. We figured out how to the same thing in just 16-bits.
Yes, you guessed it (wait for it....) we could fit a edge in word-wise!
(Disclaimer: I didn't say it was a GOOD joke!)
Each 4 bit number has 16 possible values. And you can order them in the set in 4! = 24 ways. So I thought you would get 24 * 16 = 384. Can someone explain?
So it's \binom{2^4 + 4 - 1}{4} = 3876
In other words, there are 3876 multisets of cardinality 4 with elements taken from the set containing all 4 bit values.
What I fail to understand is how this helps to compress the data in this case. The code isn't really that nice, and the comments seem to assume prior knowledge is this field, which I don't have.
So can someone give a ELI-Noob?
https://en.wikipedia.org/wiki/Bag_(mathematics)#Counting_mul...
1, 2, 3, 4 has to be stored differently than 4, 3, 2, 1, as well as 1, 3, 2, 4, and so on.
If you don't care about the order all of those can be stored in exactly the same way. Think of it like lossy compression, if you don't care about some of the detail you an ignore it (the order of the numbers in this case) and save some space.
If you have three bits and you care about the order, that's a 3-bit number -- it can take 8 different values.
If you have those same bits but you don't care about the order, the operation is a "bit count" -- you can only have 4 different values.
If you have two values, you can arrange them two ways. Three values you can arrange six ways. Four values you can arrange 20 ways. 0-20 takes a little over four bits to represent.
If you have a set, you don’t care about order. So if you save the data without an order to it, it takes a little less space to hold it. About four bits worth.
With five values you can save almost seven bits (120) and with six it’s over 9 bits (720).
As I said in another comment I just tested this with a 32-bit hash array mapped trie, and it would result in a significant reduction in the storage overhead of the trie (which is already more efficient than typical hash tables for map like data structures).