jemnotesversion 2 / featuring use-this-code or see all/search

Jan 14
Yep, it’s totally trivial, but I always forget how to create a new command in latex that has arguments. Here it is once and for all.
\newcommand{\err}[1]{\textcolor{red}{#1}}
Jan 4
ZeroC comes recommended as a way to communicate between different applications / processes.
Dec 10
Here’s a nice patch to the string class in Ruby to test if a string represents a valid floating-point number:
class String
  def valid_float?
    true if Float self rescue false
  end
end
Oct 20
Ruby 1.9 has some nice new methods on the Enumerable class; for example, each_cons, which iterates through consecutive slices of an array. You can add these to Ruby 1.8 with
require 'enumerator'
Jul 30
Short, simple and sweet. Found here, which reports that it’s attributable to Sam Stephenson.
IO.read('image.png')[0x10..0x18].unpack('NN')
May 21
I wrote down below about enumerate, which lets you access (index, item) pairs within a loop. I guess I thought it was a good idea, because I was about to write it here again. If you need both the index and item from a list, use enumerate:
for index, item in enumerate(items):
    pass
I haven’t used it in the intervening two months, but oh well.
Jul 31
Sampling from a discrete distribution, where p is a vector of probabilities:
min(find(rand() < cumsum(p)))
Jul 21
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;