Voder-Vocoder

The Log of Hal Canary

Navigation: Home | THE LOG | Log Archives | Resume | Contact Info | Public Key | SSL | Math Applets | Site Map | WP Backend | RSS2 | Atom

Archive for the “Computers & Code” Category

« Previous Entries

gravatar

<!-- Easy Gravatar implementation for
     Wordpress comment page: -->
<?php
$gravlink = "http://www.gravatar.com/avatar/";
$gravlink .= md5(strtolower(trim(get_comment_author_email())));
$gravlink .= "?d=".urlencode("http://halcanary.org/images/q80.png");
echo $gravlink; ?>

Hal Canary | Computers & Code | 2010-07-17 09:25:23 UTC
Permanent Link |
Comments Off (but feel free to email)

gnuplot question

Here's your basic LaTeX document:

%% FILE: basic.tex
\documentclass[letterpaper,12pt]{article}
\begin{document}
\input{graph}
\end{document}

The file graph.tex is generated using Gnuplot like this:

#!/usr/bin/gnuplot
## FILE: graph.gnuplot
set xrange [-3:3]
set yrange [0:.42]
set ytics nomirror autofreq 0, .1
set terminal latex size 3,1.5
set output 'graph.tex'
f(x) = .39894228040143267794 * exp(-0.5 * (x ** 2))
set style line 1 linecolor rgb "black" linewidth 1
plot f(x) ls 1 notitle

Here's the execution:

$ gnuplot graph.gnuplot
$ pdflatex basic.tex > /dev/null
$ evince basic.pdf &

This makes a really professional-looking graph I can put in a document.

[]

Now here's what I want to see:

[]

How do I do this with Gnuplot?

Hal Canary | Computers & Code | 2010-07-16 22:10:14 UTC
Permanent Link | 2 Comments

filling an ebook reader

This week, I finally broke down and bought an ebook reader — B&N's $150 WiFi Nook (ISBN 9781400532629). It's a beautiful little device.

I got the Jonathan Adler Punctuation Cover (978161560062) since it's both very sturdy and cheaper than most.

I've already side-loaded 90 free books onto the device. These books have come from several places. In no particular order:

  • The Lee County Public Library lets you borrow ebooks via Adobe Digital Editions (ADE) software. You can use ADE to side-load ebooks onto a nook.
  • Project Gutenberg — the first place to go for books from the public domain. All of their books are now availible in ePub versions.
  • Google Books, Some of the scans availible are in the public domain, and are therefore downloadable. Generally, the Gutenberug editions are a higher quality than the Google versions, since The Gutenberg tries to be an ideal textual copy of the book while Google tries to be a good representation a particular physical book.
  • Many of the works of Cory Doctorow are availible as ePubs on his website, Craphound. I especially recommend Makers and Little Brother.
  • The Baen Free Library — All copyrighted works which you may read for free, and all are availible as ePubs.
  • The Baen CD Mirror — These files are also copyrighted but free to redistribute. The newer CDs have ePub files on them. The older CDs do not, but you can use a program like Calibre to covert file formats.

Places to buy eBooks:

Hal Canary | Books, Computers & Code | 2010-06-27 14:39:03 UTC
Permanent Link |
Comments Off (but feel free to email)

simple mergesort

Even though I've studied this algorithm a couple of times, I've never had to implement it before. So I assigned it to myself.

/** should have a time-complecity of O(N×log(N))
    and a space-compelcity of O(N) **/
void mergesort(int N, int array[]) {
  int k; // k is the block size.
  int x; //x is which block we are at.
  int i,j; //indices in old[]
  int p; // index in new[]
  int ilimit,jlimit; //end of blocks.
  int *hold = malloc(sizeof(hold) * N);
  if (hold == NULL) {
    fprintf(stderr,"malloc failed\n");
    exit(2);
  }
  int *new = hold;
  int *old = array;
  int *tmp;
  for (k = 1; k < N; k *= 2) {
    p = 0;
    for (x = 0; x < N; x += (2*k)) {
      i = x;
      ilimit = i + k;
      j = ilimit;
      if (ilimit >= N) {
	while (i < N)
	  new[p++] = old[i++];
 	break; //out of for-loop
      }
      jlimit = j + k;
      if (jlimit >= N)
	jlimit = N;
      while (1) {
	if (old[i] < old[j]) {
	  new[p++] = old[i++];
	  if (i == ilimit) {
	    while (j < jlimit)
	      new[p++] = old[j++];
	    break; //out of while-loop
	  }
	} else {
	  new[p++] = old[j++];
	  if (j == jlimit) {
	    while (i < ilimit)
	      new[p++] = old[i++];
	    break; //out of while-loop
	  }
	}
      } // End while loop.
    } // End inner for loop.
    tmp = old; old = new; new = tmp;
  }// End outer for loop.
  if (old != array)
    for (i = 0; i < N; i++)
      array[i] = old[i];
  free(hold);
  return;
}

Next step is to translate to Java and use .compareTo() with arrays of references:

  public static void mergeSort(Comparable array[]) {
    int N = array.length;
    int k; // k is the block size.
    int x; // x is which block we are at.
    int i,j; //indices in old[]
    int p; // index in new[]
    int ilimit,jlimit; // end of blocks.
    Comparable hold [] = new Comparable [N];
    Comparable neww [] = hold;
    Comparable old [] = array;
    Comparable tmp [];
    for (k = 1; k < N; k *= 2) {
      p = 0;
      for (x = 0; x < N; x += (2*k)) {
        i = x;
        ilimit = i + k;
        j = ilimit;
        if (ilimit >= N) {
          while (i < N)
            neww[p++] = old[i++];
          break; //out of for-loop
        }
        jlimit = j + k;
        if (jlimit >= N)
          jlimit = N;
        while (true) {
          if (old[i].compareTo(old[j]) < 0) {
            neww[p++] = old[i++];
            if (i == ilimit) {
              while (j < jlimit)
                neww[p++] = old[j++];
              break; //out of while-loop
            }
          } else {
            neww[p++] = old[j++];
            if (j == jlimit) {
              while (i < ilimit)
                neww[p++] = old[i++];
              break; //out of while-loop
            }
          }
        } // End while loop.
      } // End inner for loop.
      tmp = old; old = neww; neww = tmp;
    }// End outer for loop.
    if (old != array)
      for (i = 0; i < N; i++)
        array[i] = old[i];
  }

Hal Canary | Computer Science, Computers & Code | 2010-05-08 07:55:23 UTC
Permanent Link |
Comments Off (but feel free to email)

bourne shell absolute path

#!/bin/sh
abspath () {
    D7636=`/usr/bin/dirname "$1"`;
    D7636=`(cd "$D7636"; pwd -P)`;
    B7636=`/usr/bin/basename "$1"`;
    echo "${D7636}/${B7636}";
}
PATH=$1
echo "path = \"${PATH}\""
ABSPATH=`abspath "$PATH"`
echo "absolute path = \"${ABSPATH}\""

UPDATE: I just realised that readlink -f "$FILE" will do the trick.

Hal Canary | Computers & Code | 2010-03-31 10:20:12 UTC
Permanent Link |
Comments Off (but feel free to email)

dynamic arrays

About ten years ago, I wrote a C++ program to print out all the prime numbers less than a given number using trial division. I recently went back and looked at the program and realized how little I knew at the time. Even though my first CS class covered object-oriented programming in C++, we never really talked the about simply using the new keyword on arrays to make use of dynamic arrays. The topic was covered in my second CS class, which I took three years later.

int *array;
int array_size = 128;
array = new int[array_size];

/* do somthing to fill the array */

int *temparray = new int[(array_size * 2)];
for (int i = 0; i < array_size; i++)
    temparray[i] = array[i];
array_size = array_size * 2;
delete [] array;
array = temparray;

In the last few years, I have realized that for the simplest progrmas, C is often more efficient and straightforward than C++. In C, the code looks exactly the same, except that new is replaced by malloc() and delete is replaced by free().

int *array;
int *temparray;
int array_size = 128;
int i;
array = malloc(array_size * sizeof(*array));

/* do somthing to fill the array */

temparray = malloc(array_size * 2 * sizeof(*temparray));
for (i = 0; i < array_size; i++)
    temparray[i] = array[i];
array_size = (array_size * 2);
free(array);
array = temparray;

Hal Canary | Computer Science, Computers & Code | 2010-03-17 09:00:38 UTC
Permanent Link |
Comments Off (but feel free to email)

algorithms matter

This example of why the right algorithm matters comes directly from my textbook. Here's the C implementation:

Bad:

#include <stdio.h>
#include <stdlib.h>
long int fib(long int n) {
  if (n==0)
    return 1;
  if (n==1)
    return 1;
  return fib(n-1) + fib(n-2);
}
int main(int argc, char *argv[]) {
  if (argc <= 1) {
    fprintf(stderr, "argument?\n\n");
    exit(1);
  }
  long int n = atol(argv[1]);
  printf("f(%li) = %li\n",n,fib(n));
  return 0;
}

Good:

#include <stdio.h>
#include <stdlib.h>
long int fib(long int n) {
  long a=1, b=1, c;
  int i;
  for (i = 1;i < n; i++){
    c = a + b; a = b; b = c;
  }
  return b;
}
int main(int argc, char *argv[]) {
  if (argc <= 1) {
    fprintf(stderr, "argument?\n\n");
    exit(1);
  }
  long int n = atol(argv[1]);
  printf("f(%li) = %li\n",n,fib(n));
  return 0;
}

Output:

$ time ./fib2 38 ; time ./fib1 38
f(38) = 63245986

real	0m0.002s
user	0m0.000s
sys	0m0.004s
f(38) = 63245986

real	0m1.492s
user	0m1.428s
sys	0m0.004s

And you can show how nicely the good algorithm scales up by pulling out a bigint library, like Java's BigInteger:

public class fib3 {
  public static String fib(int n) {
    java.math.BigInteger a,b,c;
    int i;
    a = b = java.math.BigInteger.ONE;
    for (i = 1;i < n; i++) {
      c = a.add(b); a = b; b = c;
    }
    return b.toString();
  }
  public static void main(String[] args) {
    if (args.length < 1) {
      System.err.println("argument?");
      System.exit(1);
    }
    int n = Integer.parseInt(args[0]);
    System.out.print("f(" +
      Integer.toString(n) + ") = ");
    System.out.println(fib(n));
  }
}

Hal Canary | Computer Science, Computers & Code | 2010-03-11 18:48:25 UTC
Permanent Link |
Comments Off (but feel free to email)

fullscreen without distractions

Step 1. Set a hotkey to make applications run full-screened in Gnome:

gconftool-2 --type string --set \
  /apps/metacity/window_keybindings/toggle_fullscreen \
  '<Ctrl><Alt>f'

Step 2. Run your text editor and terminal window fullscreen.

Step 3. Code without distractions.

Hal Canary | Computers & Code | 2010-02-27 20:23:17 UTC
Permanent Link |
Comments Off (but feel free to email)

zip-one-file

#!/bin/sh
## zip-one-file
## DTPD
for x in "$@"; do
  zip "${x}.zip" "$x"
done

Hal Canary | Computers & Code | 2010-02-24 08:47:50 UTC
Permanent Link |
Comments Off (but feel free to email)

private static String itoa(int i)

I like Java's verbosity; it is consistant and clear. But sometimes you want succinct things.

/** "Returns a String object representing the specified integer." */
private static String itoa(int i){
	return Integer.toString(i);
}
/** "Returns a String object representing this Integer's value." */
private static String itoa(Integer i){
	return i.toString();
}

Also, I realized I had been using assert statements without -enableassertions for the last month, which did me very little good. A good habit, I suppose.


ALSO:

private static void printl(String s) {
	System.out.println(s);
}
private static void error(String s) {
	System.err.println(s);
}

Hal Canary | Computers & Code | 2010-02-21 11:19:27 UTC
Permanent Link |
Comments Off (but feel free to email)

Mplayer and Gnome's screensaver.

#!/bin/sh
#DTPD
gnome-screensaver-command --inhibit &
INHIBIT_PID=$!
mplayer -dvd-device /dev/scd0 -ontop \
	-fs dvdnav:// -nocache 2> /dev/null
kill $INHIBIT_PID

This is how to use gnome-screensaver's inhibit command with a video player such as mplayer.

Hal Canary | Computers & Code | 2010-02-17 21:05:06 UTC
Permanent Link |
Comments Off (but feel free to email)

time-wasting methods.

/* verbose java masturbation: */
public class Foo {
  private int foobar;
  public void setFoobar(int value) {
    foobar = value;
  }
  public int getFoobar(){
    return foobar;
  }
}
/* much less annoying: */
public class Foo {
  public int foobar;
}

Hal Canary | Computers & Code | 2010-01-17 22:23:38 UTC
Permanent Link |
Comments Off (but feel free to email)

« Previous Entries

Copyright 1997-2007 by Hal Canary.
mailto: h3 at halcanary dot org
xmpp:halcanary@jabber.org
aim:halwcanary
http://halcanary.org