## Obsolation note:

Thanks to Dave Coleman’s comment I found out that glpk is available through homebrew now! So you just need these 2 steps to get glpk now:

1. homebrew
2. `brew install glpk`

If you still want to read on, the old way is still here…

So you want copy-paste instructions to install GLPK on your Macbook? Here are the steps:

2. Optional: Follow the instructions to verify the download (you might need to get GNU Privacy Guard or gpg for this. You can get it at http://gnupg.org)

4. At this point, you should have GLPK installed. Verify it: which glpsol /usr/local/bin/glpsol

5. … and try help: glpsol —help

Now that you are all set-up, read up this excellent introduction using GLPK: http://www.ibm.com/developerworks/linux/library/l-glpk1

Notes:

## Russian Peasant Multiplication in Ruby

Russian Peasant Multiplication: a very simple and elegant way to multiple.

So here’s my simple implementation: (or as a gist on github)

``````module RussianPeasantMultiplication
def russian_peasant_multiply(b)
a, b = [self, b].sort #So we have the smaller number as the first

negative_operands = [a, b].select { |n| n < 0 }
result_should_be_negative = negative_operands.size.odd? # or negative_operands.size == 1

# Now get the absoultes
a, b = [a, b].map { |n| n.abs }

while( a > 1 )
a = a >> 1 # halv it
b = b < < 1 # double it
if a.odd? # or (a % 2 == 0 )
else
end
end
result = numbers_to_add.inject(0) { |sum, n| sum += n }
result = result * -1 if result_should_be_negative
result
end
end

class Integer
include RussianPeasantMultiplication
end

# Tests
require "test/unit"

class TestInteger < Test::Unit::TestCase
def test_russian_peasant_multiply
assert_equal(22 * 70, 22.russian_peasant_multiply(70))
end

def test_russian_peasant_multiply_for_negative_numbers
assert_equal(-22 * 70, -22.russian_peasant_multiply(70))
end

def test_russian_peasant_multiply_for_negative_arguments
assert_equal(22 * -70, 22.russian_peasant_multiply(-70))
end

def test_russian_peasant_multiply_for_negative_numbers_and_arguments
assert_equal(-22 * -70, -22.russian_peasant_multiply(-70))
end

def test_russian_peasant_multiply_for_zero
assert_equal(0 * -70, 0.russian_peasant_multiply(-70))
end

def test_russian_peasant_multiply_for_zero_arguments
assert_equal(-22 * 0, -22.russian_peasant_multiply(0))
end

def test_russian_peasant_multiply_for_zero_numbers_and_arguments
assert_equal(0 * 0, 0.russian_peasant_multiply(0))
end
end
``````

Pics from the Colorado trip back in November, 2009 (with Ujwala and Maa). We spent two days in Rocky Mountain National Park and a day over in Colorado Springs and around (Manitou Springs, Pikes Peak – highest peak in that area).

It’s heavenly – have to go there again in Spring 2010 )along with the parks in Wyoming, Montana).

Click on the photo to get to my SmugMug page – feel free to leave a comment [![]]

## Rails - Table Join With Specified Fields in Select

Figured this out after a lot of monkeying-around (I mean `script/console`).

Situation:

• I have two tables (Revisions `has_many` Inputs)
• I can load Revisions and then for each I can find Inputs, but quickly found out that for my situation, it leads to a lot of queries. So I want to load the required fields from both tables together

`:joins` is the only way to do this, using `:includes` does NOT respect the select clause. Here a gist:

Admittedly, this is hacky, too hacky for my comfort. Comment/suggest a better/cleaner solution?

Note: Found out that there is a gem to do this: ar-select-with-include

## A Beautiful Foggy Autumn Morning 