Peeter Joot's (OLD) Blog.

Math, physics, perl, and programming obscurity.

Archive for November, 2012

A condition for divisibility by three

Posted by peeterjoot on November 26, 2012

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


My daughter told me that any number that any number who’s digits add up to a multiple of three are divisible by three. I’d never heard of such a thing, and was suprised by it. In the spirit of a true geek dad, I had to figure out why it works.


Let’s represent our number by a sum of digits

\begin{aligned}n = \sum_{k = 0}^N a_k 10^k,\end{aligned} \hspace{\stretch{1}}(1.2.1)

and the condition for digits adding up to a multiple of three (say m times 3) is

\begin{aligned}\sum_{k = 0}^N a_k = 3 m.\end{aligned} \hspace{\stretch{1}}(1.2.2)

We can pull that out of the sum, and sure enough, the remainder (in base 10) is divisible by three

\begin{aligned}\begin{aligned}n&= \sum_{k = 0}^N a_k +\sum_{k = 1}^N a_k (10^k - 1) \\ &=3m +9 a_1 + 99 a_2 + 999 a_3 + \cdots \\ &= 3 ( m + 3 a_1 + 33 a_2 + 333 a_3 + \cdots )\end{aligned}\end{aligned} \hspace{\stretch{1}}(1.2.3)

Aurora also mentioned that this works for 9 too, which we can see by inspection. Pretty cool (just like my daughter).

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

Planck blackbody summation

Posted by peeterjoot on November 21, 2012

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


Here’s a silly exercise. I’m so used to seeing imaginaries in e^{\cdots \omega \cdots} expressions, when I looked at the famous blackbody summation for an exponentially decreasing probability distribution

\begin{aligned}\left\langle{{n_\omega}}\right\rangle &= \sum_{n = 0}^\infty n P(n) \\ &= \frac{\sum_{n = 0}^\infty n e^{-\hbar \omega n/ k T}}{\sum_{n = 0}^\infty e^{-\hbar \omega n/ k T}},\end{aligned} \hspace{\stretch{1}}(1.1.1)

I imagined (sic) an imaginary in the exponential and thought “how can that converge?”. I thought things must somehow magically work out if the limits are taken carefully, so I derived the finite summation expressions using the old tricks.


If we want to sum a discrete power series, say

\begin{aligned}S_N(x) = 1 + x + x^2 + \cdots x^{N-1} = \sum_{n = 0}^{N-1} x^n,\end{aligned} \hspace{\stretch{1}}(1.2.3)

we have only to take the difference

\begin{aligned}x S_N - S_N = x^N - 1,\end{aligned} \hspace{\stretch{1}}(1.2.4)

so we have, regardless of the magnitude of x

\begin{aligned}\boxed{S_N(x) = \frac{1 - x^N}{1 - x}.}\end{aligned} \hspace{\stretch{1}}(1.2.5)

Observe that the derivative of S_N is

\begin{aligned}\frac{dS_N}{dx} &= \sum_{n=1}^{N-1} n x^{n-1} \\ &= \frac{1}{{x}} \sum_{n=1}^{N-1} n x^n,\end{aligned} \hspace{\stretch{1}}(1.2.6)

but we also have

\begin{aligned}\frac{dS_N}{dx} &= S_N(x)  \\ &= \frac{- N x^{N-1}}{1 - x} + \frac{1 - x^N}{(1 - x)^2} \\ &=\frac{1}{{(1-x)^2}} \left( -N x^{N-1} (1-x) + 1 - x^N \right) \\ &=\frac{1}{{(1-x)^2}} \left( -N x^{N-1} +N x^{N} + 1 - x^N \right) \\ &=\frac{1}{{(1-x)^2}} \left(  1 -N x^{N-1} +(N -1) x^{N} \right)\end{aligned} \hspace{\stretch{1}}(1.2.8)

We expect this and 1.2.6 to differ only by a constant. For 1.2.6, or dS_N/dx = 1 + 2 x + 3 x^2 + \cdots, we have 1 at the origin, the same as 1.2.8. Our conclusion is

\begin{aligned}\boxed{\sum_{n=1}^{N-1} n x^n=\frac{x}{(1-x)^2} \left(  1 -N x^{N-1} +(N -1) x^{N} \right),}\end{aligned} \hspace{\stretch{1}}(1.2.13)

a result that applies, no matter the magnitude of x. Now we can form the Planck summation up to some discrete summation point (say N-1)

\begin{aligned}\frac{\sum_{n = 0}^{N-1} n e^{-\hbar \omega n/ k T}}{\sum_{n = 0}^{N-1} e^{-\hbar \omega n/ k T}}=\frac{x}{1-x} \left(  1 -N x^{N-1} +(N -1) x^{N} \right)\frac{1}{1 - x^N}\end{aligned} \hspace{\stretch{1}}(1.2.15)

I got this far and noticed there’s still an issue with N \rightarrow \infty. Taking a second look, I see that we have a plain old real exponential, something perhaps like \cref{fig:negativeExponentialPlot:negativeExponentialPlotFig1}.

\imageFigure{negativeExponentialPlotFig1}{Plot of e^{-x/5}}{fig:negativeExponentialPlot:negativeExponentialPlotFig1}{0.3}

It doesn’t really matter what the value of \hbar \omega/k T is, it will be greater than zero, so that we have for our sum

\begin{aligned}\frac{\sum_{n = 0}^{\infty} n e^{-\hbar \omega n/ k T}}{\sum_{n = 0}^{\infty} e^{-\hbar \omega n/ k T}}&=\frac{e^{-\hbar \omega/k T}}{1-e^{-\hbar \omega/k T}}  \\ &=\frac{1}{e^{\hbar \omega/k T} - 1},\end{aligned} \hspace{\stretch{1}}(1.2.15)

which is the Planck result.

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

Update to course notes (so far) for Fall 2012, UofT Modern Optics (PHY485H1F), taught by Prof. Joseph H. Thywissen

Posted by peeterjoot on November 15, 2012

As mentioned previously, I’m batching any postings of my course notes and problems from University of Toronto PHY485H1F Advanced Classical Optics. These are now updated with the following additional content:

November 15, 2012 Multiple Interference

November 10, 2012 Cosine Transforms

November 08, 2012 Lasers

November 07, 2012 Multiple interference (cont.)

November 07, 2012 Proof of Fourier transform convolution theorem

November 02, 2012 Cavity as an oscillator

October 31, 2012 Fabry-Perot interferometry

October 29, 2012 Possible content for formula sheet

October 24, 2012 Multiple interference

October 23, 2012 Diffraction and Coherence

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

New faucet installation

Posted by peeterjoot on November 11, 2012

We’d started preparing the little main floor bathroom for a makeover, which got put on hold for a while.  With the aim of getting this finished, I got the old faucet ripped out last week, and was hoping to at least get through the new faucet and drain installation today, before returning to my optics homework


Today was installation day.  The new faucet will match the new towel bar and toilet paper dispenser nicely.  First step was installing the new drain and sink plug



The plug insert needs some plumbers putty, which I applied liberally


Then the drain portion goes in from below


I’d made the mistake of pressing the sink plug above in first then trying to tighten the drain below, but ended up with the lifter hole in the wrong orientation, and I disturbed too much of the plumbers putty trying to reorient it.  I took it all apart, reapplied the putty, then assembled, taking care to ensure the lifter hole was properly facing backwards.  By the time I was done and everything was all screwed in from below, lots of the putty had squeezed out


but it wipes off easily.  Next in was the old trap


Last time I did replaced a sink I had to completely replumb the trap and all the rest of the drain fittings, since the positioning was all off.  This time with just a faucet replacement, life was surprisingly easy.  Next in is the faucet from the top.  This model is nicely engineered, just plunk it in and then tighten with a screwdriver.  The clip in the bottom then snugs up nicely


Next is the installation of the lifter, and the drain plug.  I thought it would go like this


but it seems to like being lifted from the bottom better.  This plug has lots of adjustment potential, so if we have trouble with it pulling out, I’ll have to adjust it later and try with the pole through the hole instead of sitting under.  A little bit of teflon tape later for the hot and cold connections


and I’m ready to leak test.  The installation instructions say to do this with the aerator off




(not sure why, but I do).  The sink also holds water well


Next steps … installation of the new cabinet, lights, painting, and then putting in the new toilet paper and hand towel holder.

Posted in Home improvement | Tagged: , , , | 3 Comments »

macro expansion order, and evil macros producing commas.

Posted by peeterjoot on November 7, 2012

I was looking at a bit of code, the problematic portion, after wading through many layers of nested macro hell, was roughly of the form:

    void traceFunction( int n, ... )

    int main()
       #define TUPLE( size, arg )   (size), (arg)

       #define traceInt( sz1, ptr1 )    traceFunction( 1, sz1, ptr1 )
       #define trace( arg1 )            traceInt( arg1 )

       int x = 3 ;

       trace( TUPLE( sizeof(x), &x ) ) ;

       return 0 ;

Observe that one of the macros produces a comma separated list of arguments, and that the `trace` macro, which ends up actually called with two parameters (the result of the `TUPLE` macro), is declared as if it has a single parameter.

Somewhat mysteriously, this actually compiles on many different platforms and compilers (about 16 different combinations), but breaks with the intel 13 compiler beta (although that compiler does have a compatibility mode that I’d prefer not to depend on)

I figured I could fix this with:

    #define trace( sz1, ptr1 )       traceFunction( 1, sz1, ptr1 )

eliminating the middle man, but this gives me, for C++:

t.C(23): error: identifier “trace” is undefined

and for C compilation:

t.c(23): error #54: too few arguments in invocation of macro “trace”

error, indicating that the attempt to expand the macro `trace` occurs before the attempt to expand the `TUPLE` macro. I think I can fix this provided I rely on C99 macro varargs like so:

    #define traceInt( sz1, ptr1 )    traceFunction( 1, sz1, ptr1 )
    #define trace( ... )             traceInt( __VA_ARGS__ )

That’s likely an acceptable solution, given that we’ve now got other dependencies on C99 __VA_ARGS__ in the code.

It appears that, rather luckily, I never needed to know exactly what order nested macro expansion happens in before this.

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

Oops. Fixing a drill hole in PEX (plastic) plumbing.

Posted by peeterjoot on November 3, 2012

I was putting up a laundry room (really closet) shelf.  I had some trouble finding the stud, but thought I had it in the end.  Guess what?  I didn’t    Drilling a pilot hole for the screw, the drill punched through so fast that I was into the space beyond.  Guess what else?  There was some plastic (PEX) piping behind that and it offered negligible resistance, so before I knew it I wasn’t just punching through air, but the hot water pipe that supplies the washing machine.  Damn.

That tiny little hole produced a lot of water very fast.  I got the water main shut off fast and got the still producing pinhole sealed temporarily, first with my finger, then with some electrical tape, then was off to Home Depot for a something to patch with.  Here my temporary patch job, right as I was preparing for a more permanent fix


With a bucket in hand, I drained the pipe of the remaining water


It doesn’t look like it’s coming out steadily, due to the fast capture of the camera, but it was still producing nicely.  Next was cutting the pipe.  I used a plain old copper cutter, since I figured it would do a tiny job, and this worked out nicely


I’d tried to cut right at the position of the hole, but ended up missing by about a 1/4 inch, so took off one more little bit


You can actually see that I almost managed to go through the back end of the pipe.  It really was like going through air.  Through drywall, then bam, water going everywhere!  After being advised initially to get a special tape to “fix” this, I opted to get a mechanical coupler instead.  I’d measured the pipe before I left and had trouble finding something.  It turns out that the 5/8” that I measured is what they call 1/2” pipe, so was able to use a 1/2 coupler



This turns out to be about the easiest thing you could imagine to install, just basically requiring pushing the two ends in firmly


I’ve got a little $2 decoupler, should I need to remove the coupler for any reason (was advised to buy this just in case).  Hopefully, I’ll never need it for anything.

Posted in Home improvement | 2 Comments »