Archive for November, 2007

vi key bindings in R

Wednesday, November 28th, 2007

I am working on learning R. For those that don’t know, R is a programming language and program a bit similar to Matlab, but is particularly designed for statistics. It is free and open source, and pretty fast and flexible. It has a pretty nice library interface, with lots of user contributed libraries at CRAN (The comprehensive R archive network — modeled after CTAN and CPAN (Tex and Perl respectively)).

On *nix systems, R uses the readline library to give a command history. So I can simply type the up arrow, and find the last command I used. I just discovered that it can also search the command history in the same way as in my BASH shell. Since I like vi, I use vi key bindings in bash. I was pleasantly surprised when R automatically used these as well. I am not sure if it gets them from my .bashrc file or my .inputrc file, since I have vi keymaps set in both.
.bashrc:

set -o vi

.inputrc

set editing-mode vi set keymap vi

So when I am using R, I can search the history for “foo” by typing:
<esc> /foo

and then use ‘n’ and ‘N’ to go to the next or previous match.

What fun!!

  • FriendFeed
  • Reddit
  • del.icio.us
  • Digg
  • Slashdot
  • Technorati
  • Facebook
  • Fark
  • TwitThis
  • LinkedIn

Kubuntu Gutsy with Compiz

Saturday, November 10th, 2007

I upgraded my home computer from kubuntu Feisty to Gutsy yesterday. This was actually my second attempt. I tried once a couple weeks ago, but my satellite internet connection crapped out after downloading about 95% of the packages, and the upgrade process quit. So I decided to download a .iso at work and burn it. The first thing I tried was putting in the DVD and booting the computer. After playing around with the install a bit, it became clear that upgrading was not going to be an option. It was going to reformat my root partition. I figured there had to be another way, so I exited the installer and searched on the internet. It turns out all I had to do was boot up normally in Feisty, and then insert the DVD. There was an upgrade shell script on the DVD which I ran with sudo, and about 40 minutes later, my system was upgraded.

compiz cube
screenshot using compiz with the following plugins: desktop cube (with skydome), cube gears, cube reflection, rotate cube

One thing that keeps getting better and easier in the Linux world, and especially in the Ubuntu world is OpenGL graphics. There are a couple different alternate window managers that take advantage of this, including beryl, compiz, compiz-fusion, kwin. I have tried beryl a couple times, but have never gotten it to work correctly. And when it doesn’t work correctly, Xwindows crashes, so you have to hit “ctrl-alt-f2″ to get to a command line, kill X, and then login again. Not too fun, and probably pretty scary to many linux newbies. Compiz seems a bit more stable than beryl, and is actually included in the Ubuntu distribution, and can be enabled very easily. Kubuntu seems to lag behind Ubuntu in some respects, and compiz (desktop-effects) is not included by default, but is now included in the repositories, and is very easy to install. I simply did:

sudo apt-get install compiz compiz-kde emerald compizconfig-settings-manager

Then to start compiz I simply did

compiz --replace &

And then I suddenly had all sorts of neat eye candy to play with. To configure different settings, one must look in the Settings menu from the KMenu. The first thing that most people will want to enable is the desktop cube. For this to work correctly, you should set the number of desktops in the KDE control center to 1, and then set the number of workspaces in the compiz settings to 4. This is a bit confusing at first, but is a pretty cool. From there, it just seems fun to play around with the different settings.

Desktop effects are not practical by any means, but they are fun, and I agree with others who say that people promoting linux should be showing all the “cool” things that linux offers, besides a free operating system with all sorts of built in programming tools, and a stable and secure environment. With that mind, I have also included a screenshot for others to enjoy.

  • FriendFeed
  • Reddit
  • del.icio.us
  • Digg
  • Slashdot
  • Technorati
  • Facebook
  • Fark
  • TwitThis
  • LinkedIn

numbered footnotes on titlepage

Saturday, November 10th, 2007

The new lab I work in at Indiana University has an annual progress report, with its own stylesheet. There is a template available for Microsoft Word, but I much prefer LaTeX, so I am trying to write my own class file for that. One of the quirks of the stylesheet is that footnotes from the title are numbered. To clarify what I mean, consider the fact that many journal articles include a footnote or two in the title or author listing which may be an acknowledgment or a clarification of what institutions the authors are working at. These footnotes are usually not numbered, but instead are marked with a symbol such as an asterisk or a dagger, and this is what LaTeX does by default, when one uses the thanks command. Since the reports I will be writing are very similar to articles, I decided to try to just hack the article.cls file into srl.cls. After some experimenting, I found the line in the maketitle declaration which gives the symbols instead of numbers.

\renewcommand\thefootnote{\@fnsymbol\c@footnote}%

I simply commented this line out.

I also commented out the line which sets the footnote numbers to 0 after maketitle is finished typesetting the title

\setcounter{footnote}{0}%

Presto! One part of the stylesheet is done.

  • FriendFeed
  • Reddit
  • del.icio.us
  • Digg
  • Slashdot
  • Technorati
  • Facebook
  • Fark
  • TwitThis
  • LinkedIn

100 yootles bounty for solution to nested loop rounding error

Monday, November 5th, 2007

I am working on doing some monte carlo simulations. I want to do a particular manipulation n times, but I want to constrain what I do based on three parameters, x, y, and z, which are probability distributions coded as arrays. For example, if I want to run this simulation 1000 times, then 24 should be xayaza, 24 xayazb, 72 xaybzc and so on. My code seems to work right when everything is integers, but not when some of the numbers are non-integers reals (always positive). I have tried a couple different strategies of rounding, ceiling, and floor, but it seems to always be off by a few. The correct solution should output $total = $trials;

I will offer 100 yootles to the first person who finds a solution for me. (I am including the perl code I have been playing around with, but my problem lies in the algorithm, not in the syntax.)

#!/usr/bin/perl -w
use strict;
use POSIX qw(floor ceil);
my $trials=795;
$trials = shift;
my @x = (.4,.4,.2);
my @y = (.3,.5,.2);
my @z = (.2,.2,.6);

my $trial =0;
my $total =1;
my $a=0;
while ($a<scalar @x && $trial <= round($trials*$x[$a])) {
  my $b=0;
  while ($b< scalar @y && $trial <= round($trials*$x[$a]*$y[$b])) {
    my $c=0;
    while ($c< scalar @z && $trial <= round($trials*$x[$a]*$y[$b]*$z[$c])) {
      $total++;
      $trial++;
      if ($trial >= round($trials*$x[$a]*$y[$b]*$z[$c])) {
        $c++;
        $trial=0;
      }
    }
    $b++;
    $trial=0;
  }
  $a++;
  $trial=0;
}
print "trials = $trials, total = $total\n";

sub round {
    my($number) = shift;
    return int($number + .5 * ($number <=> 0));
}

Update

My friend Danny Reeves, along with some help from David Yang solved my problem in a completely different way. Here is Danny’s solution:

#!/usr/bin/perl
# Rob's monstronsity that is surely the solution to the wrong problem.
# But for 100 yootles, we'll just do as we're told.
# This would be much nicer in a properly functional-style language!

my $trials = 795;

my @x = (.4,.4,.2);
my @y = (.3,.5,.2);
my @z = (.2,.2,.6);

@tuples = cross(\@x,\@y,\@z);

# compute idealized tuple counts:
@counts = map { $trials*prod(@$_) } @tuples;

@ic = map { int($_) } @counts# floors of counts.
@fc = map { $_-int($_) } @counts# fractional parts.
$f = sum(@fc)# sum of fractional parts, to redistribute.

# redistribute...
for($i=1; $i<=$f; $i++) {
   $ic[posmax(deltas(\@counts,\@ic))]++;
}

$total = 0;
for($i=0; $i<scalar(@ic); $i++) {
   ($x, $y, $z) = @{$tuples[$i]};
   for($j=0; $j<$ic[$i]; $j++) {
     print "do something with ($x, $y, $z)\n";
     $total++;
   }
}

print "trials = $trials, total = $total\n";


# Return a cross product from its arguments. Arguments are array refs.
# Result is a list of array refs.  [found this on the web; damn slick]
# (note that this returns the tuples in not quite canonical order)
sub cross {
   my @r = [];
   @r = map {my $s = $_; map {[@$_ => $s]} @r} @$_ for @_;
   @r
}

# Return sum of arguments.  Ie, reduce(+, args, 0).
sub sum { my $x = 0for(@_) { $x += $_; }  $x }

# Return product of arguments.  Ie, reduce(*, args, 1).
sub prod { my $x = 1for(@_) { $x *= $_; }  $x }

# Return a list of differences between 2 lists, passed as refs.
# (assumes the lists have the same length)
sub deltas {
   my @ans;
   for(my $i=0; $i<scalar(@{$_[0]}); $i++) {
     push(@ans, $_[0]->[$i] - $_[1]->[$i]);
   }
   @ans
}

# Takes list, return the position of the largest element.
sub posmax {
   if (scalar(@_)==0) { return -1; }
   my $x = 0# index of best so far.
   for(my $i=0; $i<scalar(@_); $i++) {
     if($_[$i] > $_[$x]) { $x = $i; }
   }
   $x
}

And because Danny is a huge fan of Mathematica, he also included a Mathematica version

trials = 795;
x = {.4, .4, .2};
y = {.3, .5, .2};
z = {.2, .2, .6};

(* index of the largest element; i'm sure there's an adorable one-liner
    for this if i thought hard enough *)
posmax[{}] = -1;
posmax[l_] := Module[{i, x = 1}, (* x is index of best so far *)
   For[i = 1, i < Length[l], i++, If[l[[i]] > l[[x]], x = i]];
   x]

tuples = Tuples[{x, y, z}];
counts = (trials*Times @@ # &) /@ tuples;
ic = IntegerPart /@ counts;
fc = FractionalPart /@ counts;
f = Total[fc];
For[i=1, i<=f, i++, ic[[posmax[counts - ic]]]++];
total = 0;
MapThread[Do[{"do something with ", #1}; total++, {#2}]&, {tuples,ic}];
total
  • FriendFeed
  • Reddit
  • del.icio.us
  • Digg
  • Slashdot
  • Technorati
  • Facebook
  • Fark
  • TwitThis
  • LinkedIn