On train sets and quaternions

I was helping my two year old son build a train track yesterday. (He is unbelievably cute and has an inexplicable fascination with all things to do with transport.) Anyway as I was attempting to configure all the track sections to make a closed loop, a question occurred to me.

I know that for a polygon such as might be required for an orienteering course, each leg of the journey can be defined using a vector. The direction of the vector has an absolute frame of reference. The condition for forming a closed polygon is that the sum of all the vectors is zero. Simple.

Not so with the train set. The magnitude of each section is fixed but the direction is in relation to the direction being faced at the end of the previous track section. As well as this there is an angle of turning associated with each track section.

Whether the track forms a closed loop is dependent not only on the track pieces used but also the order in which they are connected. The situation is non-commutative.

Now I am sure that I could work out some system for representing the track pieces in some kind of vector or matrix notation but it occurs to me that it has probably been done before. Is there a simple standard notation that would work for such a system? Under this system, what are the conditions for forming a closed loop? It seems to me that quaternions might be useful here but I have no real experience with them beyond an introduction to the idea.

Thoughts? Ideas?

Not an expert by any means, but would the notation of converting orthogonal/rotational matrices to quaternions be in the ballpark?

Why can’t you just represent each piece of the track by the vector running from its start to its end?

It seems you want to understand each track piece’s orientation as being specified only in terms of its angle relative to the previous piece, rather than its absolute orientation, and then to understand when a sequence of pieces [all of the same magnitude] makes a closed loop. Well, it makes a closed loop when converting it into the sequence of “absolute” vectors it represents yields a sequence of vectors which sum to zero… And the conversion is straightforward: if you know that piece such-and-such is turned at an angle of so-and-so from the previous piece, well, then, the “absolute” vector it corresponds to is the one which is turned at an angle of so-and-so from the previous piece’s.

So if you’re given a sequence of rotations r1, r2, r3, r4, …, rN [which you can think of as complex numbers of magnitude 1], they come from a closed loop just in case A) r1 * r2 * r3 * … * rN = 1 [this indicates that after all the turns, you face the same way you started], and B) r1 + r1 * r2 + r1 * r2 * r3 + … + r1 * … * rN [equivalently, r1 * (1 + r2 * (1 + r3 * … * (1 + rN)…))] = 0 [this indicates that after all the turns, you are at the same position where you started].

For instance, a square (not that trains generally make such sharp terms, I suppose…) amounts to four right turns; that is, r1 = r2 = r3 = r4 = i. And, indeed, i[sup]4[/sup] = 1, while i * (1 + i * (1 + i * (1 + i))) = 0.

(Complex numbers because I’ve assumed that you are interested in rotations within a 2d plane. Quaternions only come into use when discussing 3d or 4d rotations.)

Got that. But there must be a tidy way of stating it.

It seems to me that each piece of track has three variable quantities.
[ul]
[li]a magnitude[/li][li]a direction or angle. And this direction is referenced back to the orientation of the previous section (rather than referring to an absolute reference frame.)[/li][li]an orientation or more specifically a change in orientation. In other words, a C shped piece of track is different from an S shaped piece of track. They might both be 15cm long and have the end at an angle of 30° to the start, but the C involves a rotation of the train while the S has the train exiting that section parallel to its orientation when it entered that section. (clear as mud I am sure.)[/li][/ul]

So, while a conventional vector is defined by two quantities, it seems to me that track pieces are defined by three quantities. (And furthermore are quantised lego-style. But we don’t need to consider that now.)

I realise that I can convert to a regular vector by choosing a reference frame (logically the first section of track laid) and doing a summation of the angles involved. But this is a bit cumbersome. I was wondering if there wasn’t some tidier way of doing it. Quaternions occurred to me because they are non-commutative under multiplication. I thought there might be a way to convert the three quantities I have described into four (the fourth having some kind of dependence on the other three and absorbing that extra degree of freedom.) With the track pieces thus described, they could then be multiplied out in the sequence in which one wished to lay. If the result was some kind of identity then it could be interpreted as a circuit being completed.

It was an idea and I think one out of reach of my mathematical capacity at the moment. I wondered if anyone had encountered anything like this before. Where better to ask?

Well whatever you mean about the orienteering, it is the same for the train set.

The vectors will add to zero.
You will find the sum of the turning is 360 degrees… or 0 degrees for a figure of 8 (and other shapes) , and n *360 degrees, for the most general statement.

I think you were confused by curved sections of track… they didn’t appear in your orienteering case, so in orientierring you didnt have to deal with X degrees per Y distance.

You can model the curved track section as

  • a vector, from one end to the other
  • two turns - one at each end, each turn is half the total turn of the curved section…

Ah, ok. I think I now understand the pieces of data which characterize a track segment for you. Essentially, there’s the starting point, the ending point, the starting train orientation, and the ending train orientation, but translating or rotating this data is considered not to affect it. So, really, there’s the rotation from the starting orientation to the ending orientation (let’s call this rotation r), and the combination of magnitude and rotation from the starting orientation to the vector from the starting point to the ending point (let’s call this complex number D).

When connecting train pieces, I suppose the rule is that both the position and the orientation must match. Accordingly, when following piece (r[sub]1[/sub], D[sub]1[/sub]) with piece (r[sub]2[/sub], D[sub]2[/sub]), the cumulative effect is that of (r[sub]1[/sub] r[sub]2[/sub], D[sub]1[/sub] + r[sub]1[/sub] D[sub]2[/sub]). Let’s call this the “track product”.

The track product is a group operation [in irrelevant jargon, it’s the semidirect product of the rotation group and the additive group of complex numbers], but unfortunately, as you noticed, not a commutative one [the action on the first component is commutative, but not on the second]. Still, it’s associative, which is nice. In particularly, this means you can efficiently parallelize the computation of the track product of a sequence of track pieces, should you care about such things.

The condition for a sequence of track pieces to form a closed loop is that their track product is the identity track (1, 0), which is to say A) the product of the r[sub]i[/sub] is 1, and B) the value D[sub]1[/sub] + r[sub]1[/sub](D[sub]2[/sub] + r[sub]2[/sub](D[sub]3[/sub] + r[sub]3/sub)) = 0.

Alas, I don’t see any “tidier” way to express condition B) here… But, it’s not particularly untidy, is it? It’s pretty clean to my eyes.

So, to rewrite the example from before, an L-shaped piece would be represented by (i, 1 + i), and four of these in a row would make a closed loop because (i[sup]4[/sup], (1 + i) + i((1 + i) + i((1 + i) + i(1 + i)))) = (1, 0).

Further illustration: A C-shaped piece would be (-1, v) for some v, while an S-shaped piece starting and ending at the same point would be (1, v). Two C-shaped pieces make a closed loop, as (-1, v) * (-1, v) = (1, -1 * v + v) = (1, 0) using the track product, but two S-shaped pieces do not make a closed loop, as (1, v) * (1, v) = (1, 1 * v + v) = (1, 2v).

Okay, so this is the Dope.
And I opened a thread about train sets.
And I learned that:

All of which is derived from the OP’s original observation that:

Damn, I love this place… :slight_smile: :slight_smile:

(hey, it makes a math phobe like me want to learn what a quaternion is.)

Er, rather, the S-shaped piece of course would not start and end at the same point as the C-shaped piece does. But the demonstration still holds; two C-shaped pieces make a loop, and two S-shaped pieces don’t.

Or how about this demonstration that an I (i.e., a straight line), a C traversed from top to bottom, a C traversed from bottom to top, and another I combine to the same effect as an S traversed from top to bottom, if the dimensions are appropriate: An I of length L is (1, L), a downwards C of height H is (-1, Hi) [with i thought of as counterclockwise rotation], an upwards C is (-1, -Hi), and a downwards S of width W and height H is (1, W + Hi). And, indeed, we have that (1, L) * (-1, Hi) * (-1, -Hi) * (1, L) = (1, L) * (1, 2Hi) * (1, L) = (1, 2L + 2Hi). So the resulting S is twice as wide as the Is used, and twice as high as the Cs used.

Put another way, this demonstrates that an I, a downwards C, an upwards C, another equally long I, and an upwards S (twice as wide as the Is and twice as tall as the Cs) combine to make a closed loop.

It’s all quite nice algebra, really…

In fairness, you opened a thread about train sets and quaternions. :slight_smile:

Mistake corrected in bold. To undo a track segment, you first reverse your direction (represented by (-1, 0)), then traverse it backwards, then reverse your direction again.

I should also have noted that while a downwards C and an upwards C aren’t the same, a downwards S and an upwards S are…

Well, when you write it like that it does look rather tidy. And that is the clarity of thought that I was asking for so thank you. Not commutative but associative.

Agreed.

For those reading who haven’t quite got a visual on the problem…
My son’s train set has two kinds of curved pieces. Both are arcs one eighth of a circle. Both can be flipped so that they either turn right or left as needed. But the radii of the two circles are different – one is for a tight corner and the other is for a more gentle bend. Let’s call these small(S) and big(B) for convenience.

Let’s say I take four of each.
I can make a loop by connecting BSBSBSBS. It is kind of a distorted square.
I can also make a loop by connecting BBSSBBSS. A bit of a rhombus shape.
But if I try BBBSBSSS or BBBBSSSS I get something of a spiral where the ends don’t meet.

Having ends meet is not sufficient however. Let’s say I take six of the big curves and two straight lengths (L). I connect LBBBBBBL and I have kind of a teardrop shape with two ends meeting but not orientated correctly for a join.

Shuffling the sections of a ready made track also illustrates the problem. Suppose my track has an S bend made by joining B and S with one turning left and the other right. I can in this case change the order and it won’t make a difference to the track’s connectivity. I can swap BS for SB and the train will still run.
However, If I attempt the same with a curved section and a straight section the pieces won’t join. I cannot swap BL for LB. I will be pointing the right way but the tracks are misaligned.

Playing around with how I might represent the situation mathematically I realised that each track was a vector quantity combined with an extra angle for the orientation. The vector required two parameters to define it – either rectangular or polar or a single complex number. The orientation required another quantity – either an angle or a unit complex number. Furthermore, in converting these quantities to a conventional vector, the angular components were dependent on all the track sections previously laid. I did not immediately see a simple representation.

So thanks Indistinguishible. Kind of a cool problem in my opinion.

I should add that I love my world.

I can be playing with my toddler son and having fun and at the same time be pushing back the boundaries of my mathematical knowledge and learning a new tool for solving problems.

:slight_smile:

I may be mistaken but at first sight and without thinking too deeply about it I think the problem may be much simpler than you are trying to make it.

Let us start with a simplified scenario restricted to a plane and where sections can only be used in one direction (imagine they have male and female ends).

I believe each section can be represented by a vector (a,b,c) where a is the distance between ends, b is the conventional plane vector angle and c is the change in angle which that section of track introduces in the track or, what is the same, the angle formed by both ends of the track.

We start at the origin (0,0) with orientation 0 and we try several sections of track independently.

A straight track of L=100 will have (100, 0, 0)
A length of track which ends in the same place but pointing right (100, 0, 45)
A length of track which ends in the same place but pointing left (A, 0, -45)
A section which ends to the right but pointing in the same original direction (A, 45, 0)
1/8 circle (A, 22.5, 45)
1/4 circle (A, 45, 90)
A half circle (A, 90, 180)
3/4 circle (A, 135, 270)
So each section of track is defined by a vector of 3 dimensions (first is length, second is angle of direction and third is change in angle). You get the idea.

Now, we are going to assemble sections. We start with one section and we know we are now at a certain point defined by A1,B1 and pointing in a certain direction defined by C1.

Now we add a second section(A2,B2,C2). Where will this take us to? A distance A2 and angle B2+C1

In other words, to the conventional two dimensional vector we need to add the angle introduced by the change in direction of the previous section of track. This can be continued with any number of sections.

Now, assume the sections can be used in reversed direction. How does this change the parameters? A will, obviously, remain unchanged while B and C will be reversed.

I think this is correct and it should be easy to calculate the number of possible combinations with a certain number of sections.

That is about as far as I got. A length and two angles define a track section.

When you start putting them together though you find that the final location depends on the order in which the pieces are connected.

How, under your system sailor do you determine whether a closed loop is formed?
As expected, Indistinguishible presented an answer elegantly with his track product ( the semidirect product of the rotation group and the additive group of complex numbers).
So, next question. Does this have a name? Anyone used this structure before?

Off the top of my head - straight sections must have corresponding return straight pieces in the same direction (orientation) to “match up” the track ends. Or… at 90 degrees to each other.

Consider the layout as connecting sections of a grid of points. Consider the simplest - a set of 45-degree turns. Obviously, 8 will do a circle. If you turn 45 degrees from your original oriention any track straight section will now be connecting grid points on a grid oriented 45 degrees. With the same straight sections, you are traversing the original grid in increments of SQRT(2). This is an irrational number, no integer number of straight sections at the 45-degree angle will get you lined up back on that grid.

Not necessarily. A 90 degree right, followed by two 90 degree lefts, followed by another 90 degree right will produce a total that’s equivalent to a straight segment.

And there’s no reason that the straight track segments can’t come in multiple lengths, that are an irrational ratio to each other.

If you are back at the starting point and facing in the same direction then you have closed the loop.