Archive for October, 2009

Ironically, despite 10 years of working on a database, my work has been so far in the guts that I rarely have to do any SQL myself. At one point when I’d been drafted for some some project management related work I did some fancy stuff with it making forms with python to compare regression results, but I’ve already forgotten all that. Here’s a couple notes to myself of the very simplest stuff.

— create a table. Note the backwards “prototype” compared to a C function;)

create table b (x int)

— how many rows?

select count(*) from b

— double table size

insert into b select * from b

–touch the whole thing to force IO and general activity in the database engine

Something funny is happening in the kernel, since my session does appear to have sufficient root-ish behaviour (even the linux filesystem fsuid is set right). Turns out that this is some kind of clash between the clearcase version control virtual filesystem and the /proc virtual filesystem. When I am in my view, even as root:

Line element.

The line element for the particle moving on a spherical surface can be calculated by calculating the derivative of the spherical polar unit vector

than taking the magnitude of this vector. We can start either in coordinate form

or, instead do it the fun way, first grouping this into a complex exponential form. This factorization was done above, but starting over allows this to be done a bit more effectively for this particular problem. As above, writing , the first factorization is

The unit vector lies in the plane perpendicular to , so we can form the unit bivector and further factor the unit vector terms into a form

This allows the spherical polar unit vector to be expressed in complex exponential form (really a vector-quaternion product)

Now, calculating the unit vector velocity, we get

The last two lines above factor the vector and the quaternion to the left and to the right in preparation for squaring this to calculate the magnitude.

This last term () has only grade two components, so the scalar part is zero. We are left with the line element

In retrospect, at least once one sees the answer, it seems obvious. Keeping constant the length increment moving in the plane is , and keeping constant, we have . Since these are perpendicular directions we can add the lengths using the Pythagorean theorem.

Line element using an angle and unit bivector parameterization.

Parameterizing using scalar angles is not the only approach that we can take to calculate the line element on the unit sphere. Proceding directly with a alternate polar representation, utilizing a unit bivector , and scalar angle is

For this product to be a vector must contain as a factor ( for some vector m.) Setting for now, the deriviate of is

This is

Alternately, we can take derivatives of , for

Or

Together with 23, the line element for position change on the unit sphere is then

Starting with cyclic reordering of the last term, we get zero

The line element (for constant ) is therefore

This is essentially the same result as we got starting with an explicit . Repeating for comparision that was

The bivector that we have used this time encodes the orientation of the plane of rotation from the polar axis down to the position on the sphere corresponds to the angle in the
scalar parameterization. The negation in sign is expected due to the negative bivector square.

Also comparing to previous results it is notable that we can explicitly express this bivector in terms of the scalar angle if desired as

The inverse mapping, expressing the scalar angle using the bivector representation is also possible, but not unique. The principle angle for that inverse mapping is

Allowing the magnitude to vary.

Writing a vector in polar form

and also allowing to vary, we have

The squared unit vector derivative was previously calculated to be

Picturing the geometry is enough to know that since is always tangential to the sphere. It should also be possible to algebraically show this, but without going through the effort we at least now know the general line element

(NOTE: the process of converting from standalone latex to wordpress got messed up in a few places. This have been fixed manually, but if anything seems wrong, check first in the standalone PDF above.)

In the following section I started off with the goal of treating two connected pendulums moving in a plane. Even setting up the Hamiltonians for this turned out to be a bit messy, requiring a matrix inversion. Tackling the problem in the guise of using a more general quadratic form (which works for the two particle as well as particle cases) seemed like it would actually be simpler than using the specifics from the angular velocity dependence of the specific pendulum problem. Once the Hamiltonian equations were found in this form, an attempt to do the first order Taylor expansion as done for the single planar pendulum and the spherical pendulum was performed. This turned out to be a nasty mess and is seen to not be particularily illuminating. I didn’t know that is how it would turn out ahead of time since I had my fingers crossed for some sort of magic simplification once the final substuitions were made. If such a simplification is possible, the procedure to do so is not obvious.

Although the Hamiltonian equations for a spherical pendulum have been considered previously, for the double pendulum case it seems prudent to avoid temptation, and to first see what happens with a simpler first step, a planar double pendulum.

Setting up coordinates axis down, and axis to the left with we have for the position of the first mass , at angle and length

If the second mass, dangling from this is at an angle from the axis, its position is

We need the velocities, and their magnitudes. For this is

For the second mass

Taking conjugates and multiplying out we have

That’s all that we need for the Kinetic terms in the Lagrangian. Now we need the height for the terms. If we set the reference point at the lowest point for the double pendulum system, the height of the first particle is

For the second particle, the distance from the horizontal is

So the total distance from the reference point is

We now have the Lagrangian

Dropping constant terms (effectively choosing a difference reference point for the potential) and rearranging a bit, also writing , we have the simpler Lagrangian

The conjugate momenta that we need for the Hamiltonian are

Unlike any of the other simpler Hamiltonian systems considered so far, the coupling between the velocities means that we have a system of equations that we must first invert before we can even express the Hamiltonian in terms of the respective momenta.

That is

While this is easily invertible, doing so and attempting to substitute it back, results in an unholy mess (albeit perhaps one that can be simplified). Is there a better way? A possibly promising way is motivated by observing that this matrix, a function of the angular difference , looks like it is something like a moment of inertia tensor. If we call this , and write

Then the relation between the conjugate momenta in vector form

and the angular velocity vector can be written

Can we write the Lagrangian in terms of ? The first Kinetic term is easy, just

For the second mass, going back to 183, we can write

Writing for this matrix, we can utilize the associative property for compatible sized matrices to rewrite the speed for the second particle in terms of a quadratic form

The Lagrangian kinetic can all now be grouped into a single quadratic form

It is also clear that this generalize easily to multiple connected pendulums, as follows

In the expression for above, it is implied that the matrix is zero for any indexes , so it would perhaps be better to write explicitly

Returning to the problem, it is convenient and sufficient in many cases to only discuss the representative double pendulum case. For that we can calculate the conjugate momenta from 201 directly

Similarly the conjugate momentum is

Putting both together, it is straightforward to verify that this recovers 193, which can now be written

Observing that , and thus , we now have everything required to express the Hamiltonian in terms of the conjugate momenta

This is now in a convenient form to calculate the first set of Hamiltonian equations.

So, when the velocity dependence is a quadratic form as identified in 200, the first half of the Hamiltonian equations in vector form are just

This is exactly the relation we used in the first place to re-express the Lagrangian in terms of the conjugate momenta in preparation for this calculation. The remaining Hamiltonian equations are trickier, and what we now want to calculate. Without specific reference to the pendulum problem, lets do this calculation for the general Hamiltonian for a non-velocity dependent potential. That is

The remaining Hamiltonian equations are , and the tricky part of evaluating this is going to all reside in the Kinetic term. Diving right in this is

For the two particle case we can expand this inverse easily enough, and then take derivatives to evaluate this, but this is messier and intractable for the general case. We can however, calculate the derivative of the identity matrix using the standard trick from rigid body mechanics

Thus the derivative of the inverse (moment of inertia?) matrix is

This gives us for the Hamiltonian equation

If we introduce a phase space position gradients

Then for the second half of the Hamiltonian equations we have the vector form

The complete set of Hamiltonian equations for 210, in block matrix form, describing all the phase space change of the system is therefore

This is a very general relation, much more so than required for the original two particle problem. We have the same non-linearity that prevents this from being easily solved. If we want a linear expansion around a phase space point to find an approximate first order solution, we can get that applying the chain rule, calculating all the , and derivatives of the top rows of this matrix.

If we write

and the Hamiltonian equations 215 as

Then the linearization, without simplifying or making explicit yet is

For brevity the constant term evaluated at is expressed in terms of the original angular velocity vector from our Lagrangian. The task is now evaluating the derivatives in the first order term of this Taylor series. Let’s do these one at a time and then reassemble all the results afterward.

So that we can discuss just the first order terms lets write for the matrix of first order derivatives in our Taylor expansion, as in

First, lets do the potential gradient.

Next in terms of complexity is the first order term of , for which we have

The over all rows and columns is the identity matrix and we are left with

Next, consider just the dependence in the elements of the row vector

We can take derivatives of this, and exploiting the fact that these elements are scalars, so they equal their transpose. Also noting that , and , we have

Since we also have , for invertable matrixes , this reduces to

Forming the matrix over all rows , and columns , we get a trailing identity multiplying from the right, and are left with

Okay, getting closer. The only thing left is to consider the remaining dependence of 222, and now want the theta partials of the scalar matrix elements

There is a slight asymmetry between the first and last terms here that can possibly be eliminated. Using , we can factor out the terms

Is this any better? Maybe a bit. Since we are forming the matrix over all indexes and can assume mixed partial commutation the transpose can be dropped leaving us with

We can now assemble all these individual derivatives

We have both and derivatives above, which will complicate things when trying to evaluate this for any specific system. A final elimination of the derivatives of the inverse inertial matrix leaves us with

Single pendulum verification.

Having accumultated this unholy mess of abstraction, lets verify this first against the previous result obtained for the single planar pendulum. Then if that checks out, calculate these matrixes explicitly for the double and multiple pendulum cases. For the single mass pendulum we have

So all the partials except that of the potential are zero. For the potential we have

and for the angular gradient

Putting these all together in this simplest application of 226 we have for the linear approximation of a single point mass pendulum about some point in phase space at time zero:

Excellent. Have not gotten into too much trouble with the math so far. This is consistent with the previous results obtained considering the simple pendulum directly (it actually pointed out an error in the earlier pendulum treatment which is now fixed (I’d dropped the term)).

Double pendulum explicitly.

For the double pendulum, with , and , we have

The derivative is the same but inverted in sign, so we have most of the constant term calculated. We need the potential gradient to complete. Our potential was

So, the gradient is

Putting things back together we have for the linear approximation of the two pendulum system

Where is still to be determined (from 226).

One of the elements of are the matrix of potential derivatives. These are

We also need the inertial matrix and its inverse. These are

Since

We have

and the matrix of derivatives becomes

For the remaining two types of terms in the matrix we need . The derivative of the inertial matrix is

Computing the product

We want the matrix of over columns , and this is

Very messy. Perhaps it would be better not even bothering to expand this explicitly? The last term in the matrix is probably no better. For that we want

With a sandwich of this between and we are almost there

we have a matrix of these scalars over , and that is

Putting all the results for the matrix together is going to make a disgusting mess, so lets summarize in block matrix form

where these are all related by the first order matrix equation

Wow, even to just write down the equations required to get a linear approximation of the two pendulum system is horrendously messy, and this isn’t even trying to solve it. Numerical and or symbolic computation is really called for here. If one elected to do this numerically, which looks pretty much mandatory since the analytic way didn’t turn out to be simple even for just the two pendulum system, then one is probably better off going all the way back to 215 and just calculating the increment for the tragectory using a very small time increment, and do this repeatedly (i.e. do a zeroth order numerical procedure instead of the first order which turns out much more complicated).

There were some other uninitialized variables, and I gave these all explicit invalid or zero values to make sure they didn’t have stack garbage in them. For the loop counter i, I just moved it into the for () declaration like so:

Did you spot my mistake? I didn’t when I made the change, and it was so minor that I forgot I’d even made it by the time I tried out the code.

After the loop termination my member variable m_confused was mysteriously set to zero, despite the loop termination condition in the non-error codepath requiring that a value be assigned.

Turns out that the compiler (g++ 4.2.3 in this case) after I “fixed” the code to remove the possibility of referencing the loop counter inappropriately interpreted this as me declaring a loop scoped variable m_confused. That, and not this->m_confused got my update, so once it went out of scope I was left with zero in my class. Unfriendly of the compiler to not give me a warning about this even with -Wall (the older gcc 4.1.2 compiler does).

What are the lessons to be learned?

don’t fix unbroken code. This unfortunately takes much more self discipline than I have!

no more than one statement per line. Keeping the code simple, should give less chance for you to misinform the compiler what you want to do. There wasn’t any good reason that the original code needed to initialize both of these in the for loop. The ‘m_confused = 0’ statement could have just as easily been on it’s own line. Ironically, this particular value was already zeroed out in other code, so it didn’t even have to be in the for loop explicitly at all.

always initialize all stack variables no matter what at the point of declaration. Even the simple change to make it obvious that it is initialized by restricting the usage scope can be screwed up.

have good code coverage testing. If I had been unlucky enough to have made this “maintainability-fix” in a non-mainline codepath, I’d have made a very unfriendly alteration for somebody else (or myself) to later debug.

For the spherical rigid pendulum of length , we have for the distance above the lowest point

(measuring down from the North pole as conventional). The Lagrangian is therefore

We can drop the constant term, using the simpler Lagrangian

To express the Hamiltonian we need first the conjugate momenta, which are

We can now write the Hamiltonian

Before going further one sees that there is going to be trouble where . Curiously, this is at the poles, the most dangling position and the upright. The south pole is the usual point where we solve the planar pendulum problem using the harmonic oscillator approximation, so it is somewhat curious that the energy of the system appears to go undefined at this point where the position is becoming more defined. It seems almost like a quantum uncertainty phenomena until one realizes that the momentum conjugate to is itself proportional to . By expressing the energy in terms of this momentum we have to avoid looking at the poles for a solution to the equations. If we go back to the Lagrangian and the Euler-Lagrange equations, this point becomes perfectly tractable since we are no longer dividing through by .

Examining the polar solutions is something to return to. For now, let’s avoid that region. For regions where is nicely non-zero, we get for the Hamiltonian equations

These now expressing the dynamics of the system. The first two equations are just the definitions of the canonical momenta that we started with using the Lagrangian. Not surprisingly, but unfortunate, we have a non-linear system here like the planar rigid pendulum, so despite this being one of the most simple systems it does not look terribly tractable. What would it take to linearize this system of equations?

Lets write the state space vector for the system as

lets also suppose that we are interested in the change to the state vector in the neighborhood of an initial state

The Hamiltonian equations can then be written

Getting away from the specifics of this particular system is temporarily helpful. We have a set of equations that we wish to calculate a linear approximation for

Our linear approximation is thus

Now, this is what we get blindly trying to set up the linear approximation of the state space differential equation. We see that the cyclic coordinate leads to a bit of trouble since no explicit dependence in the Hamiltonian makes the resulting matrix factor non-invertible. It appears that we would be better explicitly utilizing this cyclic coordinate to note that , and to omit this completely from the state vector. Our equations in raw form are now

We can treat the dependence later once we have solved for . That equation to later solve is just this last

This integrates directly, presuming is known, and we have

Now the state vector and its perturbation can be redefined omitting all but the dependence. Namely

We can now write the remainder of this non-linear system as

and make the linear approximation around as

This now looks a lot more tractable, and is in fact exactly the same form now as the equation for the linearized planar pendulum. The only difference is the normalization required to switch to less messy dimensionless variables. The main effect of allowing the trajectory to have a non-planar component is a change in the angular frequency in the dependent motion. That frequency will no longer be , but also has a and other more complex trigonometric dependencies. It also appears that we can probably have hyperbolic or trigonometric solutions in the neighborhood of any point, regardless of whether it is a northern hemispherical point or a southern one. In the planar pendulum the unambiguous sign of the matrix terms led to hyperbolic only above the horizon, and trigonometric only below.

I’ve been listening to Estonian podcasts. The most recent series concerned the guilds, merchants and so on starting in the Middle Ages and going into the 17th Century. I had heard a couple of these, and the subject material was of very little interest to me until I hit a couple of them which sheds some light about the origin of our family name.

I don’t necessarily want to transcribe the episodes, but will give a summary of what I heard.

Basically a “joot” was a festive occasion ( the use of the word in this sense is not current used in the language.) Strictly speaking these “joots” were feasts usually held in guild ( trade or merchant associations )halls, where there was plenty to eat drink. Sometimes dancing was an element in these “joots”.

There were three major events during the year where “ joots ” played an important part in festivities. These were the times around Christmas, Easter, and the coming of summer in the month of May. The Christmas and Easter events were usually two weeks long, and the one in May one week. There were other events during the year which were not as long, but all of them ended with a “joot”.

The two week events involved parades, musicians ( a lot of drummers and (bag-pipers )dancing, singing, presentations of all kinds of events – street dancing and singing, sword, spear and torch dances done by men, costumes ( one favourite one was that of the Devil). These were much in the spirit of a carnival and the festivities were always followed by a “joot”

The May celebrations which usually lasted for a week, where a “prince of spring” was chosen always ended up with a “joot”. Some of these involved burning of effigies etc.

Some of the shorter celebrations or events involved target shooting ( first with cross-bows and later with rifles ), horse races, and contests of various kinds always followed by a “joot” where there was plenty to eat and drink.

Here’s a little more info about the word “joot”

1.)Although the word “joot” is not today used in the mediaeval sense of a gala “state” celebration, it still today carries on the connotation of a festive event which consists of a get-together of which drinking is a definite part. I have never heard the word or seen the word in print with this meaning, but it does exist in a BIG dictionary.( The action word of “jooma” means to drink, and a “joot” would describe the event where drinking took place.

2.)There is another word “jootma” which means to solder. A jootraud would be a soldering iron. ( Raud means iron )

3.) There is a combining word form “jootraha” (“raha” means money) which means essentially a tip such that you would give for good service in a restaurant. There are apparently some dictionaries in Estonian which trace back the words to their source, but I don’t own one of them. I would suspect that the derivation of “jootraha” goes back to number 1.). This would probably go to the custom of tipping of what I would assume to be the serving wenches for good service or the hope of good future service. Alternately and more probably, the giving of tips had its origin in the participants of “joots” giving money to the organizers of these in the way of fixed “donations” or donations in accordance to their ability to pay. So its joining in to pay for the celebrations.

The line element for the particle moving on a spherical surface can be calculated by calculating the derivative of the spherical polar unit vector

than taking the magnitude of this vector. We can start either in coordinate form

or, instead do it the fun way, first grouping this into a complex exponential form. Writing , the first factorization is

The unit vector lies in the plane perpendicular to , so we can form the unit bivector and further factor the unit vector terms into a form

This allows the spherical polar unit vector to be expressed in complex exponential form (really a vector-quaternion product)

Now, calcuating the unit vector velocity, we get

The last two lines above factor the vector and the quaternion to the left and to the right in preparation for squaring this to calculate the magnitude.

This last term () has only grade two components, so the scalar part is zero. We are left with the line element

In retrospect, at least once one sees the answer, it seems obvious. Keeping constant the length increment moving in the plane is , and keeping constant, we have . Since these are perpendicular directions we can add the lengths using the Pythagorean theorem.