# new functions union op and top k

Just a couple of small new function operators:

union[op] (|w> + |x> + |y> + |z>)
returns
union(op|w>,op|x>,op|y>,op|z>)
(well, it would, but currently we only have a 2 and 3 parameter union)
A quick example:
```sa: load fred-sam-friends.sw
sa: dump
----------------------------------------
|context> => |context: friends>

friends |Fred> => |Jack> + |Harry> + |Ed> + |Mary> + |Rob> + |Patrick> + |Emma> + |Charlie>

friends |Sam> => |Charlie> + |George> + |Emma> + |Jack> + |Rober> + |Frank> + |Julie>
----------------------------------------

sa: union[friends] split |Fred Sam>
|Jack> + |Harry> + |Ed> + |Mary> + |Rob> + |Patrick> + |Emma> + |Charlie> + |George> + |Rober> + |Frank> + |Julie>
```
Next, is: top[k]
returns the top k kets in a superposition, without changing the order. Though if several kets have the same coeff, then it will return more than k results.
A quick example:
```sa: top[0] (|a> + 3|b> + 2|c> + 9.3|d> + 0.5|e>)
|>

sa: top[1] (|a> + 3|b> + 2|c> + 9.3|d> + 0.5|e>)
9.3|d>

sa: top[3] (|a> + 3|b> + 2|c> + 9.3|d> + 0.5|e>)
3|b> + 2|c> + 9.3|d>

sa: top[10] (|a> + 3|b> + 2|c> + 9.3|d> + 0.5|e>)
|a> + 3|b> + 2|c> + 9.3|d> + 0.5|e>
```
And an example where more than 1 ket has the same coeff:
```sa: top[1] (3.2|a> + |b> + 3.2|c> + 3.2|d> + 3|e> + |f> + |g> + 3.1|h> + 3.2|i>)
3.2|a> + 3.2|c> + 3.2|d> + 3.2|i>

sa: top[2] (3.2|a> + |b> + 3.2|c> + 3.2|d> + 3|e> + |f> + |g> + 3.1|h> + 3.2|i>)
3.2|a> + 3.2|c> + 3.2|d> + 3.2|i>

sa: top[3] (3.2|a> + |b> + 3.2|c> + 3.2|d> + 3|e> + |f> + |g> + 3.1|h> + 3.2|i>)
3.2|a> + 3.2|c> + 3.2|d> + 3.2|i>

sa: top[4] (3.2|a> + |b> + 3.2|c> + 3.2|d> + 3|e> + |f> + |g> + 3.1|h> + 3.2|i>)
3.2|a> + 3.2|c> + 3.2|d> + 3.2|i>

sa: top[5] (3.2|a> + |b> + 3.2|c> + 3.2|d> + 3|e> + |f> + |g> + 3.1|h> + 3.2|i>)
3.2|a> + 3.2|c> + 3.2|d> + 3.1|h> + 3.2|i>

sa: top[6] (3.2|a> + |b> + 3.2|c> + 3.2|d> + 3|e> + |f> + |g> + 3.1|h> + 3.2|i>)
3.2|a> + 3.2|c> + 3.2|d> + 3|e> + 3.1|h> + 3.2|i>

sa: top[7] (3.2|a> + |b> + 3.2|c> + 3.2|d> + 3|e> + |f> + |g> + 3.1|h> + 3.2|i>)
3.2|a> + |b> + 3.2|c> + 3.2|d> + 3|e> + |f> + |g> + 3.1|h> + 3.2|i>
```
Note, it doesn't give the expected result when more than 1 ket has the same coeff. I guess we could tweak it to give exactly k results, but then we would have to pick randomly from the set of kets with the same coeff. We could do that in code easy enough, but I'm not sure we want that as the default behaviour.

Home
previous: representing nuclear decay
next: shopping with process reacion

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