Tuesday, October 7, 2014

The War on Education

We'll get to the war in a moment; first some background.

Introduction
I had a conversation with a high school student this week about logarithms and the cost of college tuition.  She asked her math teacher for some uses of logarithms, a question he could not answer. That's a shame, since they are so useful.  One way that everyone uses them without being consciously aware of it is when dealing with large sums of money.

Think of millionaires.  They have a lot of money.  When you think of billionaires, how do you think of them?  I think "they are one step beyond millionaires."  And if there were trillionaires, they'd be one step beyond billionaires.  You may also think "a trillion has 3 more zeros than a billion, which has 3 more zeros than a million."  But this hides the real scale.  A single billionaire has as much money as a town full of millionaires, and a trillion dollars is unfathomably large.  However, counting the "number of steps" in an exponentially growing value (like the number of decimal places in a number) is a logarithm.

The Question
But what if we stopped thinking of large numbers in terms of logarithms?  How large is a trillion dollars?

Let's start small.  Most people believe that a college education is expensive, that it's a lot of money, that it is expensive to send your kid to college.  Let's see if that's true.

The Math
And now for the war.  It doesn't matter if you are for the American wars or against them, the math is agnostic.  And we can compare war costs with college tuition.  I read recently that the US cost of the Iraqi and Afghan wars total 4.4 trillion dollars (http://costsofwar.org/article/economic-cost-summary).

Here are the statistics:

The US spends $4.4 trillion * 3.25% = $143 billion per year on interest for the previous two wars.
And $143 billion per year / $10 thousand tuition = 14 million college tuitions per year.
And 14 million per year / 4 years = 3.3 million four year college tuitions.
And there are 3.3 million high school graduates per year.

That is, we can pay the tuition cost of a four year in-state college degree for every high school graduate in the US (for the rest of time!) with the interest on our war debt (assuming a stable population, interest rate, and tuition cost, and that the US does not pay down its national debt).

Note that tuition cost might not rise like it does now if it were prepaid by the government.  This can be verified by looking at the cost of public school in the US or the cost of post-secondary school in countries that pay for every child to get a college education.

And said another way, because 1 / 3.25% = 30, the US citizens, as a whole, believe that it is 30 times more important to fight people in Iraq and Afghanistan than giving every child in the country a college degree.

Addendum
We use logarithms every time we measure an exponential function in terms of steps.  Here are some examples:
  1. Number of digits in a number.
  2. Scientific notation.
  3. Number of years before the word population hits 8 billion.
  4. Number of years before the rain forests are cut down.
  5. Number of years before your investment reaches $100,000.
  6. Half-life of a radioactive isotope.
  7. How loud your stereo is (in Decibels).
  8. How strong an earthquake is (in Richters).
  9. Notes on the musical scale (frequency increases exponentially).
  10. The height of a binary search tree.
  11. Whatever you're talking about when you say "order of magnitude".

Friday, September 5, 2014

What babies know

I've been postponing this post for a long time since I was expecting the list to grow.

There are many things a baby doesn't know, like how to walk, sing, and do long division.  Here is the list of things that my baby didn't know how to do which I wasn't expecting:

1. Sleep.  I kinda knew this one.  But my kid had to be taught how to sleep.  I suspect he would of learned on his own given more time, however.

2. See moving object.  When my son started to be able to see me, he could recognize faces.  If I was in front of him, he'd pay attention.  However, if I moved my head by a few inches, his eyes stayed affixed to my previous location for at least a second.  Once I stopped moving, he would notice out of the corner of his eye a face and then turn to my direction and look again.  It's like I didn't exist when moving, but once stopped a new me materialized.

3. Binocular vision.  This one was cool and it was exciting to see him change.  Even when my son could see me, he often only looked with one eye, or perhaps one eye first and then the other.  While one eye looked at me, the other was free to wander and see other things.  Over time, he learned to move both at the same time and converge on the me he wanted to see.  I don't know if he can see in stereo yet, but he can certainly judge distance.  And I suspect that he could have learned to use both eyes independently like a chameleon, but the advantages of stereopsis and binocular summation are too enticing.

4. Left/right independence.  When my son could first grab things, he used his right arm.  Then he tried his left.  I could see him intently trying to acquire the toy but every time he tried to move his left arm, the right one would go instead.  If his left hand was in the right position, his right hand would make a grasping motion.  It's like his brain had learned how to do a "grasp" but didn't know there was a "left grasp" vs. "right grasp" yet.  The little guy got it eventually; he did try really hard.

And that's it.  Everything else you'd expect.

My son never really cried until last week when he saw a baby cry and get lots of attention; now he does it frequently.  So I guess learning to cry is another one.

Monday, June 9, 2014

C# Resize Image / Fit Image

After spending a good 10 minutes trying to find a program that can batch resize images from the command line by someone that looks trustworthy (enough to install on a shared server), I gave up and decided to write my own.

Here is the code in case it helps anyone.  This is C# and it compiles for .NET 2.0.  You'll need to add The assembly reference System.Drawing to your project.
ResizeImage.exe <filename> <maxWidth> <maxHeight> 
For example, calling "ResizeImage.exe hello.jpg 100 100" will decrease the width of hello.jpg to be 100 pixel or less and the height to be 100 pixels or less, maintaining its aspect ratio.  If hello.jpg is already smaller than 100x100, there will be no change.

You can batch resize many images by using a batch file.  For example, this will resize all images in a specified directory, including all subdirectories:
@FOR /F "delims=" %%A IN ('dir %1 /s /b') DO @"ResizeImage.exe" "%%A" %2 %3

For example, "BatchResize.bat c:\images 100 100" will resize all images in c:\images\...


using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

using System.IO;


namespace ResizeImage
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length != 3)
            {
                Console.WriteLine("Please use arguments: ResizeImage myfile.jpg 640 480");
                return;
            }
            string fileName = args[0];
            int width = int.Parse(args[1]);
            int height = int.Parse(args[2]);
            if( width <= 0 || height <= 0 )
            {
                Console.WriteLine("Invalid width ("+width+") or height ("+height+")");
                return;
            }
            try
            {
                System.IO.FileStream fs = new FileStream(fileName, FileMode.Open);
                Image originalImage = Image.FromStream(fs);
                fs.Close();
                Image resizedImage = ResizeImage(originalImage, width, height);
                if (resizedImage != null)
                {
                    originalImage = null;
                    resizedImage.Save(fileName);
                    Console.WriteLine("Converted: " + fileName);
                }
            }
            catch( System.IO.FileNotFoundException )
            {
                Console.WriteLine("File not found: " + fileName);
            }
            catch (System.Exception e)
            {
                Console.WriteLine("Exception: " + e.ToString());
            }
        }

        static Image ResizeImage(Image image, int maxWidth, int maxHeight)
        {
            // Get the image's original width and height
            int originalWidth = image.Width;
            int originalHeight = image.Height;

            // To preserve the aspect ratio
            float ratioX = (float)maxWidth / (float)originalWidth;
            float ratioY = (float)maxHeight / (float)originalHeight;
            float ratio = Math.Min(ratioX, ratioY);

            // New width and height based on aspect ratio
            int newWidth = (int)(originalWidth * ratio);
            int newHeight = (int)(originalHeight * ratio);
            if (newWidth >= originalWidth || newHeight >= originalHeight)
                return null;

            return (Image)(new Bitmap(image, new Size(newWidth, newHeight)));
        }
    }
}

Sunday, May 18, 2014

Schrödinger's Poo

Definition: the state of the interior of a child's diaper immediately before being changed, where it is in the quantum superposition of being both clean and poo-filled.

This deviation from my regular highbrow material should make it obvious that I recently had a child.