Voder-Vocoder

The Log of Hal Canary

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

Archive for 2010-03

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)

life

I should blog something interesting about my life, but I am busy living. I’m working on a school assignment having to do with applying algorithms to graph theory. The grad-level graph theory class I failed comes in handy there. I’m going to go hang out with friends in a few minutes. Work is still there; the days are just as long as ever, and the constant barrage of paper-cuts is bothering me today.

Hal Canary | Life | 2010-03-26 19:21:24 UTC
Permanent Link |
Comments Off (but feel free to email)

Judging a Book by its Cover

_Kaboom: Embracing The Suck In A Savage Little War_ by Matthew Gallagher (0306818809).  The subtitle is excellent.

Hal Canary | Judging a Book by its Cover | 2010-03-23 08:09:19 UTC
Permanent Link |
Comments Off (but feel free to email)

Judging a Book by its Cover

_American Adulterer_ by Jed Mercurio (1439116253). Read the cover text.

Hal Canary | Judging a Book by its Cover | 2010-03-23 08:06:58 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)

millimeter-wave

In the future, everyone will have a millimeter-wave camera built into their smartphone. We may as not wear clothes at all then. You’re going to want to get started getting in shape now.

Hal Canary | Life | 2010-03-04 20:45:28 UTC
Permanent Link |
Comments Off (but feel free to email)

Judging a Book by its Cover

[Nasty, Brutish, and Long: Adventures in Eldercare by Ira Rosofsky (1583333770)]

Hal Canary | Books, Judging a Book by its Cover | 2010-03-03 10:09:37 UTC
Permanent Link |
Comments Off (but feel free to email)

Copyright 1997-2011 by Hal Canary.
mailto: h3 at halcanary dot org
http://halcanary.org