Polk Surroundbar 3000 Remote Control Programming


This is all pretty much copied from page 9 of the manual, which can be found in full here.

In a nutshell:

  1. Turn on the bar. (light is green)
  2. Press and hold the learn button for about two seconds. (light blinks orange)
  3. Press the button on the bar that you want to program. (light goes solid orange)
  4. Hold your remote one or two feet away from the bar and tap (don’t hold) the button on the remote 4-8 times at a rate of once per second until the the light blinks green. As you tap the button on your remote once per second the orange light on the unit will blink once. (light now becomes solid green).
  5. Rinse and repeat.

Extension of Least Common Multiple to Rationals

Let r and s be two rational numbers. We want to find the smallest integer k such that both k/r and k/s are integers. In other words we are looking for the least common multiple (denominator) of two rationals (fractions).

We can write and as m/p and n/q respectively, where m, p, n, and q are integers.

We have:

\displaystyle \text{lcm}(r,s) = \text{lcm} \Bigg(\frac{m}{p},\frac{n}{q}\Bigg) = \text{lcm}(m,n)

The second equality merits some explanation, and requires that we show two things. First that r and s both divide lcm(p,q), and second that lcm(m,n) is the smallest such integer with this property.

For the first property we can see that,

\displaystyle \frac{\text{lcm}(m,n)}{\frac{m}{p}} = \text{lcm}(m,n)\frac{p}{m}

must be an integer since must divide any multiple of itself. A similar argument can be made for s, and so lcm(m,n) divides both and s.

For the second property let’s assume there is an integer smaller than lcm(m,n) that  and both divideThen there must also be an integer smaller than lcm(m,n) that both and divide, but this contradicts the definition of lcm(m,n)

To summarize. The least common integer multiple of two rationals is the least common multiple of their numerators.

This all seems a bit trite, but strangely I could not find it anywhere when I looked for it, so here it is…

R Code to Compute GCD and LCM


Some code to compute the greatest common divisor and least common multiple of two numbers using R.

gcd <- function(a, b)
  stopifnot(!(a==0 && b==0))
  if ( a == 0 )

  if ( b == 0 )

  return(abs(gcd(b, a-b*floor(a/b))))

lcm <- function(a, b)

Matrix and Vector Operations in R


To create a vector [0,1,3]:

> c(0,1,3)

Append to it:

> c=append(c,4)

Assume we have some matrix “mat” then we can index an element with:

> mat[ROW,COL]

All indexing is 1-based.

To extract a row:

> mat[ROW,]

And a column:

> mat[,COL]

The variables ROW and COL are integers.

To extract part of a column:


Also see:

> help(“Extract”)

To loop from 1 to 10 inclusive:

> for ( i in 1:10 ){…}

Bloom Filter Exploration



I recently read a post about a set of probabilistic data structures for dealing with, among other things, set membership. In particular this was the first time I had read about a Bloom Filter, and so I decided to craft myself an exercise where I could simultaneously explore a new data structure and C++ 11 syntax, which I am still adapting to.

Code can be found here. Supporting files are here.

This paper provides an analysis of false positive rates. The Wikipedia article might also help.

SQL Query to Generate Data for Histogram/Frequency Plot



Say we have two MySQL relations, User and Checkin, where each User can have zero or more Checkins. Furthermore, the User table has two attributes: id and name, while the Checkin table has three attributes: id, user_id, and date.

We would like to generate a plot of the number of users versus the number of checkins. That is, how many users have checked in one time, two times, etc?

We arrive at our answer by first calculating the number of checkins per user, which can be generated using the following query:

 SELECT user_id, count(user_id) AS cicount FROM Checkin GROUP BY user_id 

This gives us a table with two attributes: user_id, and cicount, where cicount means “checkin count”. Let’s call this table “PerUserCount”, which we can query to get the counts we really want.

 SELECT cicount, count(user_id) AS nusers FROM PerUserCount GROUP BY cicount 

Of course we can’t write the query above as is. We have to use a derived table. The complete query is:

SELECT cicount, count(user_id) as nusers FROM (SELECT user_id, count(user_id) AS cicount FROM Checkin GROUP BY user_id ) as PerUserCount GROUP BY cicount

This Stack Overflow post was helpful in deriving this solution.

Dealing with Daylight Savings Time Using pytz


, ,

As a bit of background, I have a bunch of UTC datetimes in a database, but for reporting purposes, I would like to see those times in a local time. I was curious as to whether pytz would know to make adjustments for daylight savings time automatically, so I wrote this little test program.


import datetime
import pytz

def report(time):
    detroittz = pytz.timezone('America/Detroit')
    easterntz = pytz.timezone('US/Eastern')

    dtformat = "%Y-%m-%d %H:%M"
    t = datetime.datetime.strptime(time, dtformat).replace(tzinfo=pytz.utc)
    print "*****"
    print t
    print detroittz.normalize(t)
    print easterntz.normalize(t)

report("2011-12-21 00:00")
report("2011-06-21 00:00")

It produces the output:

2011-12-21 00:00:00+00:00
2011-12-20 19:00:00-05:00
2011-12-20 19:00:00-05:00
2011-06-21 00:00:00+00:00
2011-06-20 20:00:00-04:00
2011-06-20 20:00:00-04:00

And so the answer is, yes. The date which is outside of the DST period is reported as UTC-5, and the date inside the DST period is reported as UTC-4. Very nice.

Building Boost with Visual Studio 2008 for AMD64


, , ,

I just spent a ridiculous amount of time trying to build boost date_time from source using boost 1.47 and Visual Studio 2008 in 64-bit mode.

The basic instructions consisted in executing these two commands:

> bootstrap.bat
> bjam toolset=msvc address-model=64 --with-date_time

No matter what I did, bjam insisted on building x86 binaries. Frustrated and lacking other options I downloaded the latest version, 1.49. Everything worked as expected.

I’m not sure if there are issues in 1.47 related to this. Whatever the case I didn’t see anything in the 1.48 release notes.

SVN Command Line to Remove Unversioned Files



The following command line will remove all of the unversioned files in a subversion sandbox.

svn status | grep ^\? | sed -e 's/^.......\(.*\)$/\1/g' | sed -e 's/\\/\//g' | xargs -Ifile rm -rf file

The call to svn gives us the status of our sandbox. From that we use grep to select the lines that begin with a question mark. Then using sed we strip off the leading seven characters, which svn uses to indicate the status of files. This leaves us with just the file name. Next, we use sed to translate back slash characters to front slash characters. Finally the output, which is now just a list of file or directory names is sent to xargs which calls remove on each of them.

Note that the second call to sed, which substitutes back slashes for front slashes, is due to my use of subversion for Windows in a Cygwin bash shell. It can be safely omitted if your environment does not require it.

Also note that we are making an assumption about the output of svn status that is not 100% correct. Sometimes every line does not refer to a file, nor are there always seven leading characters before a line that does refer to a file. The main place I have noticed this is when there are merge conflicts. Nonetheless, for most cases this assumption is workable.

Finally, a simple variant of this command can be used to copy all of the modified files in one sandbox to another. In that case we would grep on “^M”, which would select the modified files, and then have xargs execute “cp –parents” with an appropriate destination argument.

On the other hand, if you’re on Windows and have Tortoise SVN a “Shift-Right Click” will get you a menu option for doing this.