jemnotes

2008-11-30

interesting blog

amix.dk. Has some code segments, which might be interesting to look at if nothing else.

2008-11-18

readline on mac

This page pointed out a bizarre but perfect way of fixing readline on mac. Apparently the GNU license doesn’t play nice for Apple so it doesn’t work by default. I did this:

sudo easy_install ipython
sudo easy_install -f http://ipython.scipy.org/dist/ readline

And suddenly readline worked in ipython on mac.

interesting design

Very odd, but also quite interesting industrial design. Reading this made me realise that sorting these entries by date is silly. I’ll fix this sometime.

2008-11-06

stack size

I was shown an optimization solver that ran fine with certain parameter sizes, yet started segfaulting with larger problem sizes. It turned out that a desired single large, amorphous array could not be allocated within the available stack size limit. Increasing the stack size limit via ulimit -s 50000 (for example) fixed the problem.

You can view a shell’s resource limits using, for example,

monolith> ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) 6144
file size               (blocks, -f) unlimited
max locked memory       (kbytes, -l) unlimited
max memory size         (kbytes, -m) unlimited
open files                      (-n) 256
pipe size            (512 bytes, -p) 1
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 266
virtual memory          (kbytes, -v) unlimited

coffee

Peregrine espresso in Washington, DC (map). The best espresso I’ve had in the US.

2008-10-19

git configuration

Useful page with git configuration details.

2008-10-05

nice airline booking website

travelocity done right: ITA software.

vpn on mac

After adding a VPN on mac, you might find it doesn’t really work. Add a new (ethernet, say) interface with network settings, and pick an address on the VPN’s subnet. Then you’ll be able to use services on that server by explicitly referring to that IP.

Sadly, mac’s ‘seemless user interface’ doesn’t seem to carry over to leopard server…

2008-09-08

python

(Only) somewhat ok page about re-raising exceptions.

2008-09-03

git

Set up a new tracked repository.

git remote add remotename username@server:~/repo/

2008-09-02

passwd

Expire a user’s password, requiring them to change it immediately upon their next login:

passwd -e username

free fonts

Nice looking free fonts: exljbris and Gentium.

samba over ssh on mac

Leopard irritatingly prevents you from creating an ssh tunnel to another computer and then connecting a samba share to your localhost. Get around this by first creating an alias for your localhost interface:

sudo ifconfig lo0 alias 127.0.0.2 up

Then, forward the ports via ssh, eg with

sudo ssh -N -L 127.0.0.2:139:localhost:139 -L 127.0.0.2:445:localhost:445 user@server.com

Finally, connect using Finder to smb://127.0.0.2/sharename.

2008-09-01

vim

Paste last executed command in vim: ":p.

vim

Nice way of having two different kinds of syntax highlighting within one file, with vim.

2008-08-25

openvpn

Great setup guide for openvpn. Goes well with Tunnelblick.

2008-08-21

valgrind

Check for common memory errors in C with valgrind. Also profile cache misses and branch mispredictions.

Sample usage:

# Check memory accesses.
valgrind --tool=memcheck ./prog

# View cache and branch profiling information.
valgrind --tool=cachegrind ./prog

For more detailed output from valgrind, add the -g tag to your gcc compilation flags to add debugging information to the binary.

2008-08-16

psfrag

The key component of psfrag is the position specification. Here is the relevant syntax:

\psfrag{tag}[position]{Replacement text}

The position is up to two letters; one from {t, b, B, c} (top, bottom, baseline, center) and one from {l, r, c} (left, right, center). Center is assumed if either is omitted.

mac

Infinite labs: excellent little applications for mac, including PlugSuit and Afloat.

vim

Nice color scheme for vim: ir_black.

2008-08-14

vim

Disable highlighting of spurious _ and ^ symbols in LaTeX files as errors (say, when a macro is used to start equation environments).

autocmd Filetype tex syntax clear texOnlyMath

2008-08-13

make

Use multiple processes (and hence cores) with make by using make -j.

ssh

Disconnect quickly, even when ^d is not allowed, with ~..

python

A quick way of finding out where a Python package lives:

>>> import pylab
>>> print pylab
<module 'pylab' from '/usr/lib/python2.5/site-packages/pylab.pyc'>

2008-08-11

python

Run a Python script through a profiler, then interpret the output. I don’t think it’s worth the bother going through iPython; I couldn’t get %run -p to work properly.

At the shell:

> python -m cProfile -o outprof.tmp examples/bb30.py

Then, in iPython:

>>> import pstats
>>> p = pstats.Stats('outprof.tmp')
>>> p.strip_dirs().sort_stats('cumulative').print_stats(50)

bash

A reminder of how tee works comes from hunterdavis.com.

./prog | tee >(gzip > logfile.tgz)

ipe on mac

From this blog, where there is an installer for ipe for mac, it seems that to convert back from a pdf to an eps the following is required.

gs -dBATCH -dNOPAUSE -dNOPROMPT -sDEVICE=epswrite -sOutputFile=new.eps old.pdf

find and sed on mac

find and sed operate slightly differently on a mac. Here’s some code that replaces 4.3 with 4.4 in all Makefiles, recursively.

find . -name 'Makefile' -exec sed -i -e 's/4\.3/4.4/g' {} \;

2008-08-06

vim

The numbered register "0 contains the text from the most recent yank command, while the numbered register "1 contains the text from the most recent delete or change command. If the delete or change was ‘small’, though, (ie, less than one line), the small delete register "- is used instead.

This means that if you yank some text to move it elsewhere, then rearrange some stuff (including deleting, say, newlines), you can still easily retrieve the text that you yanked with "0p.

2008-08-05

latex

I made some aliases for use when writing mbox{LaTeX} documents. Add these (perhaps unwieldly) lines to your .bashrc:

# Next command on a single line.
alias lr='latex -file-line-error-style -interaction=nonstopmode $LR.tex >
  /tmp/latexoutput || cat /tmp/latexoutput ;
  grep -e "\(LaTeX Warning\)\|\(Overfull\)\|\(Underfull\)" /tmp/latexoutput |
  grep -v "There were undefined references";
  killall -SIGUSR1 xdvi.bin 2> /dev/null'

alias xdj='xdvi -paper us -expert -keep $LR.dvi 2>/dev/null >/dev/null&'

Note: depending on your system, you made need to replace xdvi.bin with xdvi-xaw3d.bin.

Then, when you are preparing a document, you can do this:

> LR=myfilename # set filename. The suffix .tex is added automatically.
> lr            # compiles your latex document.
> xdj           # displays your latex document.

This sounds pretty ordinary, but these aliases take advantage of these things:

2008-08-04

gcc

To see the assembly instructions generated for a piece of C code, use this:

gcc -c -g -Wa,-ahl src.c

gcc

Find out the macros, and values, predefined by gcc (includes useful things like __INT_MAX__).

gcc -dM -E - < /dev/null

2008-08-02

google chart api

At http://code.google.com/apis/chart/. Nice charts, like this one.

line chart

You generate these by going to a specially written url such as http://chart.apis.google.com/chart?cht=lc&amp;chs=200x125&amp;chd=t:50.0,5.0,4.0,93.0,17.0,18.0,13.0,15.0 for the chart above. It looks like it would be a bit of work to translate from a particular application to this api, though.

2008-07-31

matlab

Sampling from a discrete distribution, where p is a vector of probabilities:

min(find(rand() < cumsum(p)))

2008-07-29

skype

Woah, typing s/search/repl/ in a Skype conversation actually performs a replacement for both parties on your last message (only). You can also remove the last messages with s///. It turns out you can also do the same by right clicking.

matlab

Have a look at this bizarre Matlab behaviour:

>> 1 / [1 2 3]'
ans =
         0         0    0.3333

Apparently, this is how they wanted it. Excusing the use of backslash as if it were mathematics, we have the equivalence:
 A / B = (B^T backslash A^T)^T,
leading to the bizarre conclusion that in Matlab,
 / = backslash^T.

Of course, the syntax that I really wanted was 1 ./ [1 2 3]'. This is fully consistent with Matlab’s elementwise operations, but would it be so bad if 1 / [1 2 3]' just did not work, especially rather than silently giving you something totally weird but the same shape?

find

I’ve often tried to use find, but have never really got over its syntax. It seems that it’s one of those powerful tools that’s not necessarily obvious to use at first. Here’s one way to use it:

find . -name '*.sw*' -print

prints filename which have .sw in them (say, leftover vim swapfiles).

You can combine this with grepping:

find . -name "*.bib" -exec echo {}; grep "tribute to" '{}'

looks for the words ‘tribute to’ in files with the extension .bib.

2008-07-22

python & vim

Nice page describing enhancements vim has for Python. Will try the omni-complete from this page soon.

vim

Exceedingly useful for cutting and pasting from the clipboard in vim without indentation problems: :put +. (More info.) Depending on your system, :put * might be what you’re after instead.

gcc

Version 4.3 of gcc, apart from significant improvements, has this nice feature:

> gcc -c -Q -Os --help=optimizers
The following options control optimizations:
  -falign-jumps                         [disabled]
  -falign-labels                        [disabled]
  -falign-loops                         [enabled]
# ---8<---
  -fwhole-program                       [disabled]
  -fwrapv                               [disabled]

2008-07-21

C

A few code snippets for timing in C. First, tic() and toc() functions. These work like Matlab: tic() starts the timer, and toc() stops it and gives formatted output. There’s also a tocq() function which just returns the number of elapsed seconds.

#include <time.h>
static clock_t tic_timestart;

void tic(void) {
    tic_timestart = clock();
}

float toc(void) {
    clock_t tic_timestop;
    tic_timestop = clock();
    printf("time: %8.2f.\n", (float)(tic_timestop - tic_timestart) / CLOCKS_PER_SEC);
    return (float)(tic_timestop - tic_timestart) / CLOCKS_PER_SEC;
}

float tocq(void) {
    clock_t tic_timestop;
    tic_timestop = clock();
    return (float)(tic_timestop - tic_timestart) / CLOCKS_PER_SEC;
}

A bit of code for counting flops: first, a statistics structure, then some defines.

/* CFDIV    divides             1/a[3]
 * CFADD    adds                c[12] + d[12]
 * CFMUL    multiplies          b[4] * h[7]
 * CFASN    assigns             A[5] = 0
 * CFSTO    stores              A[5] = (...)
 * CFTFR    transfers           A[5] = b[7]
 * CFNTF    negated transfers   A[5] = -b[7]
 * CFCOM    compares            c[5] < b[7]
 */
typedef struct statistics_t *statistics;
typedef struct statistics_t {
    long div;
    long add;
    long mul;
    long asn;
    long sto;
    long tfr;
    long ntf;
    long com;
} Statistics;

#define CFDIV(i) if (countflops) stats->div += i
#define CFADD(i) if (countflops) stats->add += i
#define CFMUL(i) if (countflops) stats->mul += i
#define CFASN(i) if (countflops) stats->asn += i
#define CFSTO(i) if (countflops) stats->sto += i
#define CFTFR(i) if (countflops) stats->tfr += i
#define CFNTF(i) if (countflops) stats->ntf += i
#define CFCOM(i) if (countflops) stats->com += i