Friday, February 8, 2013

The science behind meditation - is it brain training?

An interesting article on Matthieu Ricard, a researcher turned monk, suggests that meditation might act as a brain training exercise, helping the human mind to achieve superior levels of happiness and decreased propensity for negativity. One hopes more research is done in this area so that the exact mechanism by which meditation acts to bring peace of mind is understood, thereby helping the whole of mankind lead a more content life.

Thursday, February 7, 2013

Converting twips to pixels and vice-versa

Here is a Ruby program to convert between twips and pixels:

#!/bin/ruby

# As per PostScript definition
TWIPS_PER_INCH = 1440

def twips_to_pixels(twips, dpi, round = true)
  raise "Invalid DPI" if (dpi <= 0)
  raise "Invalid twips" if (twips < 0)

  num_pixels = ((twips / TWIPS_PER_INCH.to_f) * dpi)
  return round ? num_pixels.round : num_pixels.floor
end

def pixels_to_twips(pixels, dpi, round = true)
  raise "Invalid DPI" if (dpi <= 0)
  raise "Invalid number of pixels" if (pixels < 0)

  twips = ((pixels / dpi.to_f) * TWIPS_PER_INCH)
  return round ? twips.round : twips.floor
end

def numeric?(str)
  Float(str) != nil rescue false
end

def print_usage_and_exit
  puts "Usage: #{$0} [-t twips | -p pixels] [-d dpi]"
  exit(1)
end

if __FILE__ == $0
  print_usage_and_exit if ARGV.length != 4

  dpi = nil
  twips = nil
  pixels = nil

  [0, 2].each do |option_index|
    option = ARGV[option_index]
    option_val = ARGV[option_index + 1]
    print_usage_and_exit unless (numeric?(option_val))

    case option
      when "-d" then dpi = option_val.to_f
      when "-t" then twips = option_val.to_f
      when "-p" then pixels = option_val.to_f
      else print_usage_and_exit
    end
  end

  print_usage_and_exit if ((pixels.nil? && twips.nil?) || dpi.nil?)
  res = twips.nil? ? pixels_to_twips(pixels, dpi) : twips_to_pixels(twips, dpi)
  puts res
end

Sunday, February 3, 2013

1/-1 matrix - problem #29 from Problem solving strategies

Here is the solution to one of the problems mentioned in the earlier blog post:

Problem 29: Suppose contrary to the claim a1 + b1 + … + a25 + b25 = 0. Then:
(a1 + a2 + … + a25) = - (b1 + … + b25)
Now note that ai= -1,if number of elements having value -1 in row i is even, and 1 otherwise.

Thus (a1 + a2 + … + a25) = -x + y,  where x is the number of rows having odd number of -1s and y is the number of rows having even number of -1s

Similarly, (b1 + … + b25) = -m + n, where m is the number of columns having odd number of -1s and n is the number of columns having even number of -1s

Combining ①, ② and ③ results in the equation (y – x ) = (m - n)

But (y + x) = 25, the total number of rows in the matrix, and (m + n) = 25, the total number of columns in the matrix. Thus (y + x) = (m + n)

Combining ④ and ⑤ results in the equality y = m, which implies that the number of rows with even number of -1s equals the number of columns with odd number of -1s.

Define z = number of -1s in the matrix.

Suppose y is odd. Then, x must be even and by original definition of x and y, z must be even (even number of rows with odd number of -1s plus odd number of rows with even number of -1s, resulting in even + even). If y is even, z must be odd. Thus parity(z) ≠ parity(y)

On the other hand, by definition of m and n, it can be shown that if m is odd, then z must be odd and if m is even then z must be even. Thus parity(z) = parity(m). But this contradicts since y = m as per an earlier equation. Thus the assumption a1 + b1 + … + a25 + b25 = 0 is wrong.