Peeter Joot's (OLD) Blog.

Math, physics, perl, and programming obscurity.

Archive for August, 2012

C++ syntax, nested loops and continue — an embarrassing experiment?

Posted by peeterjoot on August 28, 2012

For whatever reason, the C++ continue keyword is not used often in the portions of the code I normally deal with.  I was moving code in a function that had gotten waaay to big into a helper function. This changed the scope of some continue and break usage, and thought I’d double check that these only impact the inner loop:

#include <stdio.h>

int main()
   for ( int i = 0 ; i < 5 ; i++ )
      for ( int j = 0 ; j < 5 ; j++ )
         if ( j % 2 )
            continue ;

         printf( "i,j = %d, %d\n", i, j ) ;

   printf( "\n\n" ) ;
   for ( int i = 0 ; i < 5 ; i++ )
      for ( int j = 0 ; j < 5 ; j++ )
         if ( 3 == j )
            break ;

         printf( "i,j = %d, %d\n", i, j ) ;

   return 0 ;

I see:

i,0 = 0, 0
i,2 = 0, 2
i,4 = 0, 4
i,0 = 1, 0
i,2 = 1, 2
i,4 = 1, 4
i,0 = 2, 0
i,2 = 2, 2
i,4 = 2, 4
i,0 = 3, 0
i,2 = 3, 2
i,4 = 3, 4
i,0 = 4, 0
i,2 = 4, 2
i,4 = 4, 4

i,0 = 0, 0
i,1 = 0, 1
i,2 = 0, 2
i,0 = 1, 0
i,1 = 1, 1
i,2 = 1, 2
i,0 = 2, 0
i,1 = 2, 1
i,2 = 2, 2
i,0 = 3, 0
i,1 = 3, 1
i,2 = 3, 2
i,0 = 4, 0
i,1 = 4, 1
i,2 = 4, 2

This makes logical sense, and protects me from breaking code containing the inner loop in question should I move it to a helper function (where there will no longer be an outer loop in scope).

Should it be embarrassing that I actually did this experiment after so many years of programming?

Posted in C/C++ development and debugging. | Tagged: , , , | Leave a Comment »

Scotiabank iTrade RESP … they are sneaky little buggers with their hidden fees.

Posted by peeterjoot on August 21, 2012

Watch out if you are considering opening a Scotiabank iTrade RESP account.  I got the email below from Scotiabank where I currently have a very minimal RESP account.  I’d opened this account with the intention of increasing it, assuming I’d eventually have the funds to do so, but will now likely close it out of principle and look elsewhere:

“As a valued Scotia iTRADE® client, your continued satisfaction is important to us.
That’s why we’re writing to you today to remind you that your combined assets with Scotia iTRADE need to reach certain levels as of August 31st, 2012 for you to avoid an annual fee charged to your registered account(s) on September 15th, 2012 (covering a one year period to September 14th, 2013).
Here is the breakdown for meeting asset levels to avoid the fee:
• If you hold an RESP account with Scotia iTRADE, you need to have $15,000 or greater in combined assets with Scotia iTRADE to avoid a $25 annual fee (plus HST) charged to your RESP account(s) on September 15th.
• For registered accounts, other than RESP accounts, you need to have $25,000 or greater in combined assets with Scotia iTRADE to avoid a $100 annual fee (plus HST) charged to your registered account(s) on September 15th.
With our powerful trading platforms, easy-to-use trading and investment tools, and friendly customer service, consolidating more of your assets with Scotia iTRADE is a wise decision.
We appreciate your business and will continue to offer you robust investment solutions to help you achieve your investment needs.”

Somewhere in the fine print of the Scotia iTrade agreement must be some statement of fees for less than this minimum “$15,000”.  When I read the fine print, I saw the fees schedule for the per trade fees, but not this one.

I can summarize this email from Scotiabank, “As a valued iTrade customer, we will charge you fees that will encourage you to look into our competitors banking products.”

Posted in Incoherent ramblings | Tagged: , , , , | 5 Comments »

Geometry of general Jones vector (problem 2.8)

Posted by peeterjoot on August 9, 2012

[Click here for a PDF of this post with nicer formatting]

Another problem from [1].


The general case is represented by the Jones vector

\begin{aligned}\begin{bmatrix}A \\ B e^{i\Delta}\end{bmatrix}.\end{aligned} \hspace{\stretch{1}}(1.1.1)

Show that this represents elliptically polarized light in which the major axis of the ellipse makes an angle

\begin{aligned}\frac{1}{{2}} \tan^{-1} \left( \frac{2 A B \cos \Delta }{A^2 - B^2} \right),\end{aligned} \hspace{\stretch{1}}(1.1.2)

with the x axis.


Prior to attempting the problem as stated, let’s explore the algebra of a parametric representation of an ellipse, rotated at an angle \theta as in figure (1). The equation of the ellipse in the rotated coordinates is

Figure 1: Rotated ellipse


\begin{aligned}\begin{bmatrix}x' \\ y'\end{bmatrix}=\begin{bmatrix}a \cos u \\ b \sin u\end{bmatrix},\end{aligned} \hspace{\stretch{1}}(1.2.3)

which is easily seen to have the required form

\begin{aligned}\left( \frac{x'}{a} \right)^2+\left( \frac{y'}{b} \right)^2 = 1.\end{aligned} \hspace{\stretch{1}}(1.2.4)

We’d like to express x' and y' in the “fixed” frame. Consider figure (2) where our coordinate conventions are illustrated. With

Figure 2: 2d rotation of frame


\begin{aligned}\begin{bmatrix}\hat{\mathbf{x}}' \\ \hat{\mathbf{y}}'\end{bmatrix}=\begin{bmatrix}\hat{\mathbf{x}} e^{\hat{\mathbf{x}} \hat{\mathbf{y}} \theta} \\ \hat{\mathbf{y}} e^{\hat{\mathbf{x}} \hat{\mathbf{y}} \theta}\end{bmatrix}=\begin{bmatrix}\hat{\mathbf{x}} \cos \theta + \hat{\mathbf{y}} \sin\theta \\ \hat{\mathbf{y}} \cos \theta - \hat{\mathbf{x}} \sin\theta\end{bmatrix},\end{aligned} \hspace{\stretch{1}}(1.2.5)

and x \hat{\mathbf{x}} + y\hat{\mathbf{y}} = x' \hat{\mathbf{x}} + y' \hat{\mathbf{y}} we find

\begin{aligned}\begin{bmatrix}x' \\ y'\end{bmatrix}=\begin{bmatrix}\cos \theta & \sin\theta \\ -\sin\theta & \cos\theta\end{bmatrix}\begin{bmatrix}x \\ y\end{bmatrix},\end{aligned} \hspace{\stretch{1}}(1.2.22)

so that the equation of the ellipse can be stated as

\begin{aligned}\begin{bmatrix}\cos \theta & \sin\theta \\ -\sin\theta & \cos\theta\end{bmatrix}\begin{bmatrix}x \\ y\end{bmatrix}=\begin{bmatrix}a \cos u \\ b \sin u\end{bmatrix},\end{aligned} \hspace{\stretch{1}}(1.2.7)


\begin{aligned}\begin{bmatrix}x \\ y\end{bmatrix}=\begin{bmatrix}\cos \theta & -\sin\theta \\ \sin\theta & \cos\theta\end{bmatrix}\begin{bmatrix}a \cos u \\ b \sin u\end{bmatrix}=\begin{bmatrix}a \cos \theta \cos u - b \sin \theta \sin u \\ a \sin \theta \cos u + b \cos \theta \sin u\end{bmatrix}.\end{aligned} \hspace{\stretch{1}}(1.2.8)

Observing that

\begin{aligned}\cos u + \alpha \sin u = \text{Real}\left( (1 + i \alpha) e^{-i u} \right)\end{aligned} \hspace{\stretch{1}}(1.2.9)

we have, with \text{atan2} = \text{atan2}(x, y) a Jones vector representation of our rotated ellipse

\begin{aligned}\begin{bmatrix}x \\ y\end{bmatrix}=\text{Real}\begin{bmatrix}( a \cos \theta - i b \sin\theta ) e^{-iu} \\ ( a \sin \theta + i b \cos\theta ) e^{-iu}\end{bmatrix}=\text{Real}\begin{bmatrix}\sqrt{ a^2 \cos^2 \theta + b^2 \sin^2 \theta } e^{i \text{atan2}(a \cos\theta, -b\sin\theta) - i u} \\ \sqrt{ a^2 \sin^2 \theta + b^2 \cos^2 \theta } e^{i \text{atan2}(a \sin\theta, b\cos\theta) - i u}\end{bmatrix}.\end{aligned} \hspace{\stretch{1}}(1.2.10)

Since we can absorb a constant phase factor into our -iu argument, we can write this as

\begin{aligned}\begin{bmatrix}x \\ y\end{bmatrix}=\text{Real}\left(\begin{bmatrix}\sqrt{ a^2 \cos^2 \theta + b^2 \sin^2 \theta } \\ \sqrt{ a^2 \sin^2 \theta + b^2 \cos^2 \theta } e^{i \text{atan2}(a \sin\theta, b\cos\theta) -i \text{atan2}(a \cos\theta, -b\sin\theta)} \end{bmatrix} e^{-i u'}\right).\end{aligned} \hspace{\stretch{1}}(1.2.11)

This has the required form once we make the identifications

\begin{aligned}A = \sqrt{ a^2 \cos^2 \theta + b^2 \sin^2 \theta }\end{aligned} \hspace{\stretch{1}}(1.2.12)

\begin{aligned}B = \sqrt{ a^2 \sin^2 \theta + b^2 \cos^2 \theta } \end{aligned} \hspace{\stretch{1}}(1.2.13)

\begin{aligned}\Delta =\text{atan2}(a \sin\theta, b\cos\theta) - \text{atan2}(a \cos\theta, -b\sin\theta).\end{aligned} \hspace{\stretch{1}}(1.2.14)

What isn’t obvious is that we can do this for any A, B, and \Delta. Portions of this problem I tried in Mathematica starting from the elliptic equation derived in section 8.1.3 of [2]. I’d used Mathematica since on paper I found the rotation angle that eliminated the cross terms to always be 45 degrees, but this turns out to have been because I’d first used a change of variables that scaled the equation. Here’s the whole procedure without any such scaling to arrive at the desired result for this problem. Our starting point is the Jones specified field, again as above I’ve using -iu = i (k z - \omega t)

\begin{aligned}\mathbf{E} = \text{Real}\left( \begin{bmatrix}A \\ B e^{i \Delta}\end{bmatrix}e^{-i u}\right)=\begin{bmatrix}A \cos u \\ B \cos ( \Delta - u )\end{bmatrix}e^{-i u}\end{aligned} \hspace{\stretch{1}}(1.2.15)

We need our cosine angle addition formula

\begin{aligned}\cos( a + b ) = \text{Real} \left( (\cos a + i \sin a)(\cos b + i \sin b)\right) =\cos a \cos b - \sin a \sin b.\end{aligned} \hspace{\stretch{1}}(1.2.16)

Using this and writing \mathbf{E} = (x, y) we have

\begin{aligned}x = A \cos u\end{aligned} \hspace{\stretch{1}}(1.2.17)

\begin{aligned}y = B ( \cos \Delta \cos u + \sin \Delta \sin u ).\end{aligned} \hspace{\stretch{1}}(1.2.18)

Subtracting x \cos \Delta/A from y/B we have

\begin{aligned}\frac{y}{B} - \frac{x}{A} \cos \Delta = \sin \Delta \sin u.\end{aligned} \hspace{\stretch{1}}(1.2.27)

Squaring this and using \sin^2 u = 1 - \cos^2 u, and 1.2.17 we have

\begin{aligned}\left( \frac{y}{B} - \frac{x}{A} \cos \Delta \right)^2 = \sin^2 \Delta \left( 1 - \frac{x^2}{A^2} \right),\end{aligned} \hspace{\stretch{1}}(1.2.27)

which expands and simplifies to

\begin{aligned}\left( \frac{x}{A} \right)^2 +\left( \frac{y}{B} \right)^2 - 2 \left( \frac{x}{A} \right)\left( \frac{y}{B} \right)\cos \Delta = \sin^2 \Delta,\end{aligned} \hspace{\stretch{1}}(1.2.27)

which is an equation of a rotated ellipse as desired. Let’s figure out the angle of rotation required to kill the cross terms. Writing a = 1/A, b = 1/B and rotating our primed coordinate frame by \theta degrees

\begin{aligned}\begin{bmatrix}x \\ y\end{bmatrix}=\begin{bmatrix}\cos \theta & -\sin\theta \\ \sin\theta & \cos\theta\end{bmatrix}\begin{bmatrix}x' \\ y'\end{bmatrix},\end{aligned} \hspace{\stretch{1}}(1.2.22)

we have

\begin{aligned}\begin{aligned}\sin^2 \Delta &=a^2 (x' \cos \theta - y'\sin\theta)^2+b^2 ( x' \sin\theta + y' \cos\theta)^2 \\ &- 2 a b (x' \cos \theta - y'\sin\theta)( x'\sin\theta + y'\cos\theta)\cos \Delta \\ &=(x')^2 ( a^2 \cos^2 \theta + b^2 \sin^2 \theta - 2 a b \cos \theta \sin \theta \cos \Delta ) \\ &+(y')^2 ( a^2 \sin^2 \theta + b^2 \cos^2 \theta + 2 a b \cos \theta \sin \theta \cos \Delta ) \\ &+ 2 x' y' ( (b^2 -a^2) \cos \theta \sin\theta + a b (\sin^2 \theta - \cos^2 \theta) \cos \Delta ).\end{aligned}\end{aligned} \hspace{\stretch{1}}(1.2.27)

To kill off the cross term we require

\begin{aligned}\begin{aligned}0 &= (b^2 -a^2) \cos \theta \sin\theta + a b (\sin^2 \theta - \cos^2 \theta) \cos \Delta \\ &= \frac{1}{{2}} (b^2 -a^2) \sin (2 \theta) - a b \cos (2 \theta) \cos \Delta,\end{aligned}\end{aligned} \hspace{\stretch{1}}(1.2.27)


\begin{aligned}\tan (2 \theta) = \frac{2 a b \cos \Delta}{b^2 - a^2} = \frac{2 A B \cos \Delta}{A^2 - B^2}.\end{aligned} \hspace{\stretch{1}}(1.2.27)

This yields 1.1.2 as desired. We also end up with expressions for our major and minor axis lengths, which are respectively for \sin \Delta \ne 0

\begin{aligned}\sin\Delta/ \sqrt{ b^2 + (a^2 - b^2) \cos^2 \theta - a b \sin (2 \theta) \cos \Delta }\end{aligned} \hspace{\stretch{1}}(1.2.27)

\begin{aligned}\sin\Delta/\sqrt{ b^2 + (a^2 - b^2)\sin^2 \theta + a b \sin (2 \theta) \cos \Delta },\end{aligned} \hspace{\stretch{1}}(1.2.27)

which completes the task of determining the geometry of the elliptic parameterization we see results from the general Jones vector description.


[1] G.R. Fowles. Introduction to modern optics. Dover Pubns, 1989.

[2] E. Hecht. Optics. 1998.

Posted in Math and Physics Learning. | Tagged: , , , , , | Leave a Comment »

Complex form of Poynting relationship

Posted by peeterjoot on August 2, 2012

[Click here for a PDF of this post with nicer formatting]

This is a problem from [1], something that I’d tried back when reading [2] but in a way that involved Geometric Algebra and the covariant representation of the energy momentum tensor. Let’s try this with plain old complex vector algebra instead.

Question: Average Poynting flux for complex 2D fields (problem 2.4)

Given a complex field phasor representation of the form

\begin{aligned}\tilde{\mathbf{E}} = \mathbf{E}_0 e^{i (\mathbf{k} \cdot \mathbf{x} - \omega t)}\end{aligned} \hspace{\stretch{1}}(1.0.1)

\begin{aligned}\tilde{\mathbf{H}} = \mathbf{H}_0 e^{i (\mathbf{k} \cdot \mathbf{x} - \omega t)}.\end{aligned} \hspace{\stretch{1}}(1.0.2)

Here we allow the components of \mathbf{E}_0 and \mathbf{H}_0 to be complex. As usual our fields are defined as the real parts of the phasors

\begin{aligned}\mathbf{E} = \text{Real}( \tilde{\mathbf{E}} )\end{aligned} \hspace{\stretch{1}}(1.0.3)

\begin{aligned}\mathbf{H} = \text{Real}( \tilde{\mathbf{H}} ).\end{aligned} \hspace{\stretch{1}}(1.0.4)

Show that the average Poynting vector has the value

\begin{aligned}\left\langle{{ \mathbf{S} }}\right\rangle = \left\langle{{ \mathbf{E} \times \mathbf{H} }}\right\rangle = \frac{1}{{2}} \text{Real}( \mathbf{E}_0 \times \mathbf{H}_0^{*} ).\end{aligned} \hspace{\stretch{1}}(1.0.5)


While the text works with two dimensional quantities in the x,y plane, I found this problem easier when tackled in three dimensions. Suppose we write the complex phasor components as

\begin{aligned}\mathbf{E}_0 = \sum_k (\mathbf{E}_{kr} + i \mathbf{E}_{ki}) \mathbf{e}_k = \sum_k {\left\lvert{\mathbf{E}_k}\right\rvert} e^{i \phi_k} \mathbf{e}_k\end{aligned} \hspace{\stretch{1}}(1.0.6)

\begin{aligned}\mathbf{H}_0 = \sum_k (\mathbf{H}_{kr} + i \mathbf{H}_{ki}) \mathbf{e}_k = \sum_k {\left\lvert{\mathbf{H}_k}\right\rvert} e^{i \psi_k} \mathbf{e}_k,\end{aligned} \hspace{\stretch{1}}(1.0.7)

and also write \phi_k' = \phi_k + \mathbf{k} \cdot \mathbf{x}, and \psi_k' = \psi_k + \mathbf{k} \cdot \mathbf{x}, then our (real) fields are

\begin{aligned}\mathbf{E} = \sum_k {\left\lvert{\mathbf{E}_k}\right\rvert} \cos(\phi_k' - \omega t) \mathbf{e}_k\end{aligned} \hspace{\stretch{1}}(1.0.8)

\begin{aligned}\mathbf{H} = \sum_k {\left\lvert{\mathbf{H}_k}\right\rvert} \cos(\psi_k' - \omega t) \mathbf{e}_k,\end{aligned} \hspace{\stretch{1}}(1.0.9)

and our Poynting vector before averaging (in these units) is

\begin{aligned}\mathbf{E} \times \mathbf{H} = \sum_{klm} {\left\lvert{\mathbf{E}_k}\right\rvert} {\left\lvert{\mathbf{H}_l}\right\rvert} \cos(\phi_k' - \omega t) \cos(\psi_l' - \omega t) \epsilon_{klm} \mathbf{e}_m.\end{aligned} \hspace{\stretch{1}}(1.0.10)

We are tasked with computing the average of cosines

\begin{aligned}\left\langle{{ \cos(a - \omega t) \cos(b - \omega t) }}\right\rangle=\frac{1}{{T}} \int_0^T \cos(a - \omega t) \cos(b - \omega t) dt=\frac{1}{{\omega T}} \int_0^T \cos(a - \omega t) \cos(b - \omega t) \omega dt=\frac{1}{{2 \pi}} \int_0^{2 \pi}\cos(a - u) \cos(b - u) du=\frac{1}{{4 \pi}} \int_0^{2 \pi}\cos(a + b - 2 u) + \cos(a - b) du=\frac{1}{{2}} \cos(a - b).\end{aligned} \hspace{\stretch{1}}(1.0.11)

So, our average Poynting vector is

\begin{aligned}\left\langle{{\mathbf{E} \times \mathbf{H}}}\right\rangle = \frac{1}{{2}} \sum_{klm} {\left\lvert{\mathbf{E}_k}\right\rvert} {\left\lvert{\mathbf{H}_l}\right\rvert} \cos(\phi_k - \psi_l) \epsilon_{klm} \mathbf{e}_m.\end{aligned} \hspace{\stretch{1}}(1.0.12)

We have only to compare this to the desired expression

\begin{aligned}\frac{1}{{2}} \text{Real}( \mathbf{E}_0 \times \mathbf{H}_0^{*} )= \frac{1}{{2}} \sum_{klm} \text{Real}\left({\left\lvert{\mathbf{E}_k}\right\rvert} e^{i\phi_k}{\left\lvert{\mathbf{H}_l}\right\rvert} e^{-i\psi_l}\right)\epsilon_{klm} \mathbf{e}_m = \frac{1}{{2}} \sum_{klm} {\left\lvert{\mathbf{E}_k}\right\rvert} {\left\lvert{\mathbf{H}_l}\right\rvert} \cos( \phi_k - \psi_l )\epsilon_{klm} \mathbf{e}_m.\end{aligned} \hspace{\stretch{1}}(1.0.13)

This proves the desired result.


[1] G.R. Fowles. Introduction to modern optics. Dover Pubns, 1989.

[2] JD Jackson. Classical Electrodynamics Wiley. John Wiley and Sons, 2nd edition, 1975.

Posted in Math and Physics Learning. | Tagged: , , | 2 Comments »

Ease of screwing up C string operations.

Posted by peeterjoot on August 2, 2012

Here’s a new way to mess up a strncpy that I hadn’t seen before. I found about 50 instances of this in the component I was working on today:

   ossStrNCopy( pszTarget, pszSource, strlen( pszSource ) + 1 ) ;

Our ossStrNCopy function is a lot like strncpy, but unlike the library interface, ours is explicitly truncating and won’t leave a dangling non-null terminated string. It’s supposed to be called like:

   ossStrNCopy( pszTarget, pszSource, sizeof(pszTarget) ) ;

(where pszTarget is a char [] array and not a char *). The idea is to avoid buffer overflows that would occur if you were to copy more bytes than the target can hold.

This incorrect use is an excellent example of what was probably clueless cut and paste. Perhaps the thought process was: “I don’t know what this last parameter is for. It’s too much work to read the documentation … I’ll just put something.”

The caller is requesting to have one plus the null terminator of the source string copied into the target. If you are going to do that, why not just call strcpy?

Posted in C/C++ development and debugging. | Tagged: , , , | 2 Comments »