# jemdoc: addcss{jacob}{exp}{notes}, showsource, title{jemnotes}, eqsize{120} = [http://stanford.edu/~jacobm jem]notes == 2008-12-06 === logging javascript error messages In Safari (and possibly other browsers), you can log a small message to the debug console with the javascript command %window.console.log(message);%. This is helpful for debugging. == 2008-11-30 === interesting blog [http://amix.dk/ amix.dk]. Has some code segments, which might be interesting to look at if nothing else. == 2008-11-18 === readline on mac [http://www.brianberliner.com/2008/04/18/ipython-on-mac-os-x-105-leopard/ 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: ~~~ {}{sh} 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 [http://www.artlebedev.com/ 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, ~~~ {}{sh} 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 [http://peregrineespresso.com/ Peregrine espresso] in Washington, DC ([http://maps.google.com/maps?q=660+Pennsylvania+Ave+SE+Washington+DC+20003&ie=UTF8&oe=UTF-8&z=16&g=660+Pennsylvania+Ave+SE+Washington+DC+20003&iwloc=addr map]). The best espresso I've had in the US. == 2008-10-19 === git configuration [http://arthurkoziel.com/2008/05/02/git-configuration/ Useful page] with git configuration details. == 2008-10-05 === nice airline booking website travelocity done right: [http://matrix.itasoftware.com/ 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 [http://blog.ianbicking.org/2007/09/12/re-raising-exceptions/ re-raising exceptions]. == 2008-09-03 === git Set up a new tracked repository. ~~~ {}{sh} 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: ~~~ {}{sh} passwd -e username ~~~ === free fonts Nice looking free fonts: [http://www.josbuivenga.demon.nl/ exljbris] and [http://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&item_id=Gentium 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: ~~~ {}{sh} sudo ifconfig lo0 alias 127.0.0.2 up ~~~ Then, forward the ports via ssh, eg with ~~~ {}{sh} 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 [http://vim.wikia.com/wiki/Syntax_highlighting Nice way] of having two different kinds of syntax highlighting within one file, with vim. == 2008-08-25 === openvpn Great [http://openvpn.net/index.php/documentation/howto.html setup guide] for openvpn. Goes well with [http://code.google.com/p/tunnelblick/ Tunnelblick]. == 2008-08-21 === valgrind Check for common memory errors in C with [http://valgrind.org/docs/manual/quick-start.html valgrind]. Also profile cache misses and branch mispredictions. Sample usage: ~~~ {}{sh} # 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 [http://www.ctan.org/tex-archive/macros/latex/contrib/psfrag/ psfrag] is the position specification. Here is the relevant syntax: ~~~ {}{latex} \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 [http://infinite-labs.net/ Infinite labs]: excellent little applications for mac, including PlugSuit and Afloat. === vim Nice color scheme for vim: [http://blog.infinitered.com/entries/show/8 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: ~~~ {}{pyint} >>> import pylab >>> print pylab ~~~ == 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: ~~~ {}{sh} > python -m cProfile -o outprof.tmp examples/bb30.py ~~~ Then, in iPython: ~~~ {}{pyint} >>> 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 [http://hunterdavis.com hunterdavis.com]. ~~~ {}{sh} ./prog | tee >(gzip > logfile.tgz) ~~~ === ipe on mac From this [http://naranja.umh.es/~atg/blog.html 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. ~~~ {}{sh} 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. ~~~ {}{sh} 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%: ~~~ {}{sh} # 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: ~~~ {}{sh} > 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: - +lr+ will only show the output if there was a problem. This avoids polluting your screen. - +lr+ will /never/ stop with an error while running $\mbox{\LaTeX}$ and leave you punching keys in the hopes of getting back to the command line. - $\mbox{\LaTeX}$ will tell you the line number of any errors. - +lr+ will automatically kick +xdvi+ after compiling the document to show your changes. No need to click on +xdvi+ or wait for it to notice the file changed. I really like the way +xdvi+ permits such updates without flickering. == 2008-08-04 === gcc To see the assembly instructions generated for a piece of C code, use this: ~~~ {}{sh} gcc -c -g -Wa,-ahl src.c ~~~ === gcc Find out the macros, and values, predefined by gcc (includes useful things like +__INT_MAX__+). ~~~ {}{sh} 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&chs=200x125&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: ~~~ {}{matlab} 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: ~~~ {}{matlab} >> 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: ~~~ {}{sh} find . -name '*.sw*' -print ~~~ prints filename which have +.sw+ in them (say, leftover +vim+ swapfiles). You can combine this with grepping: ~~~ {}{sh} 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 [http://blog.sontek.net/2008/05/11/python-with-a-modular-ide-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 +%. ([http://gcc.gnu.org/gcc-4.3/changes.html 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: ~~~ {}{sh} > 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. ~~~ {}{c} #include 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. ~~~ {}{c} /* 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 ~~~