## Monday, 1 December 2014

### train of thought

Now I have explained supported-ops, apply(), and create inverse, I can show you "train of thought".
Way back when I first started this project, it looked like this:
```-- simple code to approximately represent the concept of "train of thought"
train-of-thought[|X>,n] ::= [
|ket> => |X>
repeat[n][
|op> => pick-elt:supported-ops|ket>
|ket> => pick-elt:apply(|op>,|ket>)
Output:|ket>
]
return |ket>
]```
`(yeah, for a while operators were separated by colons, now by spaces) `

`Now, in python it looks like:`
```# train-of-thought[n] some-superposition
# eg: train-of-thought[20] |colour: red>
#
# where n is an int.
def console_train_of_thought(one,context,n):
try:
n = int(n)
except:
return ket("",0)

print("context:",context.name)
print("one:",one)
print("n:",n)
X = one.pick_elt()
print("|X>:",X)
print()
result = superposition()

for k in range(n):
op = X.apply_op(context,"supported-ops").pick_elt()  #   |op> => pick-elt supported-ops |X>
X = X.apply_op(context,op).pick_elt()                #   |X> => pick-elt apply(|op>,|X>)
result.data.append(X)
print(X.display())
return result                             # return a record of the train-of-thought ```
` `
I guess in words, start with a ket.
Look up the list of supported-ops.
Pick one (that's what pick-elt does).
Apply that operator to your starting ket.
Pick one element from that resulting superposition.
Loop.

The note is that for it to work well, you first need lots of data, and you need to have run create inverse.
The reason that is important is, if you don't have inverses you rapidly run into |>, and your train stops!
With inverses, it happily goes round in circles.

Let's give an example. Let's use the early US presidents data:
```sa: load early-us-presidents.sw
sa: train-of-thought[20] |Washington>
context: sw console
one: |Washington>
n: 20
|X>: |Washington>
|year: 1793>
0.000|>
0.000|>
0.000|>
...
```
Doh! We forgot to run "create inverse" and our train of thought died.

Let's try again:
```sa: create inverse
sa: train-of-thought[20] |Washington>
context: sw console
one: |Washington>
n: 20
|X>: |Washington>

|early US Presidents: _list>
|party: Federalist>
|number: 2>
|US President: John Adams>
|early US Presidents: _list>
|early US Presidents: _list>