# Mathematics in action: affine transformations and PostScript

Here’s the next in the mathematics in action series.

An important group of geometric transformations are the affine transformations, such as translations, reflections, and rotations. They are defined by a combination of a matrix multiplication and a translation:

Such transformations are an important part of the PostScript page description language. For example, the PostScript code below, after the initial header, specifies a black letter F, and another in blue, translated by (50, 50):

``````%!PS-Adobe-3.0
%%BoundingBox: 0 0 196 118
%%Pages: 0
%%DocumentFonts: Times-Roman
%%DocumentNeededFonts: Times-Roman
%%Orientation: Portrait
%%EndProlog
gsave newpath 100 dict begin /Times-Roman findfont 50 scalefont setfont

gsave 0 setgray 0 0 moveto (F) show grestore

gsave 0 0 1 setrgbcolor 50 50 translate 0 0 moveto (F) show grestore``````

The output is included in this picture:

But of course that is using only the (ef) part of the transformation. More interesting is a rotation through an angle θ, where the matrix is:

PostScript allows this to be specified using a translation and a rotation angle in degrees (red F) or as an (abcdef) matrix which combines the rotation matrix with a (180, 0) translation (grey F):

``````gsave 1 0 0 setrgbcolor 100 0 translate 30 rotate 0 0 moveto (F) show grestore

gsave 0.5 setgray 30 [0.8660 0.5 -0.5 0.8660 180 0] concat 0 0 moveto (F) show grestore``````

A diagonal matrix provides scaling in the x and y directions, with negative scaling factors giving a reflection. Again, PostScript allows the scaling factors to be provided directly (green F), or as an (abcdef) matrix (brown F):

``````gsave 0 0.7 0 setrgbcolor 100 100 translate -2 0.5 scale 0 0 moveto (F) show grestore

gsave 0.5 0.3 0 setrgbcolor [-2 0 0 0.5 175 75] concat 0 0 moveto (F) show grestore

end grestore showpage
%%Trailer
%%EOF``````

Copying and pasting the three blocks of PostScript into a text file with a .EPS extension will give the coloured image above, which can be viewed by printing it, or by inserting it into a Microsoft Word document. Experimentation with other, more complex, affine transformations is easy that way.

# Mathematics in action: averaging angles

This post begins a (potential) new series: mathematics in action. Let’s see how it goes.

There are many phenomena in science and engineering which one would like to describe using angles. With a pendulum, for example, one might use 0° to describe the state with the bob at the right, 90° for the bob at the centre moving left, 180° for the bob at the left, 270° for the bob at the centre moving right, and 360° = 0° for the bob at the right again:

This gets even more interesting when we have multiple pendulums (or metronomes, which are just pendulums upside down):

A more practical example of this sort of thing might be a bank of electric generators, which each generate an alternating current as they spin through their cycles:

There are even medical applications (see pages 83–84 of this thesis).

In studying this kind of thing, it is sometimes necessary to find the average of a set of angles. For example: 300°, 330°, 360°, and 30°. This is more complicated than it seems at first sight, since 330° = −30°, 300° = −60°, and so forth.

The solution is to find the average sine: in this case a = −0.2165 (the average of −0.8660, −0.5, 0, and 0.5), and the average cosine: in this case b = 0.8080 (the average of 0.5, 0.8660, 1, and 0.8660).

The average angle is the one whose sine and cosine are a and b, which in this case is: arctan(a/b) = −15° = 345° (Some care is needed in handling the case of negative or zero cosines).

We can define the extent to which the angles are the same by r = sqrt(a2 + b2). This will give r = 0 for angles evenly spaced around a circle, and r = 1 for identical angles. For the set of 300°, 330°, 360°, and 30° (which are quite similar), we get r = 0.8365. To see an example of r defined in this way being used, look at this paper of mine. Having identical or near-identical angles is also important for sets of electric generators.

For those mathematics students that have studied complex numbers, this can all be made a lot easier. Each angle θ is represented as the complex number cos(θ) + i sin(θ) = eiθ. We then average the complex numbers, giving a + b i. Conversion to polar coordinates then gives us r and the average angle φ, since a + b i = r eiφ. This helps to explain why complex numbers are a compulsory part of every engineering course – especially when studying harmonic motion, alternating current, or anything that vibrates.