sorting in the bko scheme

Currently there are only two ways to sort superpositions in the BKO scheme. coeff-sort that sorts by the float coefficients of kets, and ket-sort that does a natural sort (though occasionally the natural sort breaks!) on the ket-labels. So today, some examples showing how to sort a superposition/list.

Let's consider the data here, and tweak it to this (noting they are all op-self operators. ie, they only change the coeff of a ket, not the ket label):
area-self |Adelaide> => 1295|Adelaide>
population-self |Adelaide> => 1158259|Adelaide>
annual-rainfall-self |Adelaide> => 600.5|Adelaide>

area-self |Brisbane> => 5905|Brisbane>
population-self |Brisbane> => 1857594|Brisbane>
annual-rainfall-self |Brisbane> => 1146.4|Brisbane>

area-self |Darwin> => 112|Darwin>
population-self |Darwin> => 120900|Darwin>
annual-rainfall-self |Darwin> => 1714.7|Darwin>

area-self |Hobart> => 1357|Hobart>
population-self |Hobart> => 205556|Hobart>
annual-rainfall-self |Hobart> => 619.5|Hobart>

area-self |Melbourne> => 1566|Melbourne>
population-self |Melbourne> => 3806092|Melbourne>
annual-rainfall-self |Melbourne> => 646.9|Melbourne>

area-self |Perth> => 5386|Perth>
population-self |Perth> => 1554769|Perth>
annual-rainfall-self |Perth> => 869.4|Perth>

area-self |Sydney> => 2058|Sydney>
population-self |Sydney> => 4336374|Sydney>
annual-rainfall-self |Sydney> => 1214.8|Sydney>
And I guess now, jump into some examples:
-- sort by area:
sa: coeff-sort area-self "" |city list>
5905.000|Brisbane> + 5386.000|Perth> + 2058.000|Sydney> + 1566.000|Melbourne> + 1357.000|Hobart> + 1295.000|Adelaide> + 112.000|Darwin>

-- tidy that up (by applying the clean sigmoid):
sa: clean coeff-sort area-self "" |city list>
|Brisbane> + |Perth> + |Sydney> + |Melbourne> + |Hobart> + |Adelaide> + |Darwin>

-- sort by population:
sa: clean coeff-sort population-self "" |city list>
|Sydney> + |Melbourne> + |Brisbane> + |Perth> + |Adelaide> + |Hobart> + |Darwin>

-- sort by annual rainfall:
sa: clean coeff-sort annual-rainfall-self "" |city list>
|Darwin> + |Sydney> + |Brisbane> + |Perth> + |Melbourne> + |Hobart> + |Adelaide>
So, until today (more in the next post) this along with ket-sort was the only way to sort superpositions. So I suppose I should give a ket-sort example too.
-- let's create a shuffled list:
|shuffled list> => shuffle "" |city list>

-- let's take a look:
sa: "" |shuffled list>
|Hobart> + |Brisbane> + |Melbourne> + |Adelaide> + |Darwin> + |Sydney> + |Perth>
-- yup. Looks sufficiently shuffled.

-- now ket sort:
sa: ket-sort "" |shuffled list>
|Adelaide> + |Brisbane> + |Darwin> + |Hobart> + |Melbourne> + |Perth> + |Sydney>
I guess the only thing left to mention in this post is that they can be applied just before the table operator:
sa: table[city-name,area,population,annual-rainfall] clean coeff-sort population-self "" |city list>
+-----------+------+------------+-----------------+
| city-name | area | population | annual-rainfall |
+-----------+------+------------+-----------------+
| Sydney    | 2058 | 4336374    | mm: 1214.8      |
| Melbourne | 1566 | 3806092    | mm: 646.9       |
| Brisbane  | 5905 | 1857594    | mm: 1146.4      |
| Perth     | 5386 | 1554769    | mm: 869.4       |
| Adelaide  | 1295 | 1158259    | mm: 600.5       |
| Hobart    | 1357 | 205556     | mm: 619.5       |
| Darwin    | 112  | 120900     | mm: 1714.7      |
+-----------+------+------------+-----------------+
Finally, a NB. This is the mess you get if you forget to clean the incoming superposition (ie, your incoming coeffs are not all 1):
sa: table[city-name,area,population,annual-rainfall] coeff-sort population-self "" |city list>
+----------------------+-----------------+--------------------+-----------------------+
| city-name            | area            | population         | annual-rainfall       |
+----------------------+-----------------+--------------------+-----------------------+
| 4336374.00 Sydney    | 4336374.00 2058 | 4336374.00 4336374 | 4336374.00 mm: 1214.8 |
| 3806092.00 Melbourne | 3806092.00 1566 | 3806092.00 3806092 | 3806092.00 mm: 646.9  |
| 1857594.00 Brisbane  | 1857594.00 5905 | 1857594.00 1857594 | 1857594.00 mm: 1146.4 |
| 1554769.00 Perth     | 1554769.00 5386 | 1554769.00 1554769 | 1554769.00 mm: 869.4  |
| 1158259.00 Adelaide  | 1158259.00 1295 | 1158259.00 1158259 | 1158259.00 mm: 600.5  |
| 205556.00 Hobart     | 205556.00 1357  | 205556.00 205556   | 205556.00 mm: 619.5   |
| 120900.00 Darwin     | 120900.00 112   | 120900.00 120900   | 120900.00 mm: 1714.7  |
+----------------------+-----------------+--------------------+-----------------------+
Kind of hard to explain to others what is going on here. A couple of pieces that partly explain it are:
-- first noting that it is not a "clean superposition", ie, it has coeffs with value other than 1:
sa: coeff-sort population-self "" |city list>
4336374.000|Sydney> + 3806092.000|Melbourne> + 1857594.000|Brisbane> + 1554769.000|Perth> + 1158259.000|Adelaide> + 205556.000|Hobart> + 120900.000|Darwin>
And this code in the ket class:
  def readable_display(self):
    if self.label == '':
      return ""
    if self.value == 1:
      return self.label
    else:
      return "{0:.2f} {1}".format(self.value,self.label)
That's it for this post, more sorting in the next one.

Update: Tweaks on the pretty print table code means that it no longer matters if the incoming superposition is a clean superposition or not. Code auto-runs set-to[1] sigmoid on the incoming superposition. I can't think of a use case where you would want otherwise. It is just one line of code to comment out if we do want to switch this feature off though.


Home
previous: another pretty print table example
next: new function sort by

updated: 19/12/2016
by Garry Morrison
email: garry -at- semantic-db.org