Thursday, August 26, 2010

Trim whitespace

The one that worked for me:

echo ' foo bar ' | sed 's/^ *//;s/ *$//'
foo bar

Source: trim whitespace?

Thursday, September 10, 2009

New Dad Checklist

  1. Choose name
    1. Muslim baby names
  2. Pre-register at hospital
  3. Check out the route from your home to the Emergency room at the chosen hospital -- if water breaks or labor pain starts this is where you go.
  4. Talk with health insurance company for details of coverage for baby. Usually mom's insurance covers baby for a certain number of days.
  5. Choose pediatrician, call and ask if accepting new patients and insurance plan
  6. Parents' education, video will suffice.
    1. "Laugh and learn about childbirth"
    2. Also "laugh and learn about newborn baby care", at least learn to swaddle the baby
    3. Usually these videos are available at the public libraries
  7. Car seat
  8. Car seat installation inspection: see NHTSA website and SEATCHECK.ORG for location/schedule
  9. Baby clothes:
    1. body suits
    2. onesies
    3. sleep sacks
    4. socks
    5. mittens
    6. caps
    7. burp cloth
  10. Feeding:
    1. feeder bottles (recommend Playtex Drop-Ins® Original Nurser Bottle)
    2. feeder bottle brushes
    3. breast pump (a small manual one usually suffices, recommend Medela Harmony)
  11. Cleanliness:
    1. bathtub (preferably with newborn sling, recommend The First Years "Sure Comfort" Deluxe Newborn to Toddler Tub with Sling)
    2. soap
    3. shampoo
    4. lotion
    5. comb (preferably brush type)
    6. nail cutter
    7. bath towel
  12. Diapers:
    1. diaper
    2. wipe
    3. diaper changing station (static)
    4. diaper changing station (portable)
    5. diaper bag
  13. Copy Quran into cell-phone.
    1. Mobile app
    2. MP3
  14. Check out baby soothing methods

Wednesday, May 6, 2009

postgresql commands

Useful postgresql commands:

login [assuming no password required]:

postgresql -d <database name> -h <host name> -U <user name>

List schemas:

\dn

List tables in schema THE_SCHEMA:

\dt THE_SCHEMA.*

Describe table THE_SCHEMA.THE_TABLE:

\d THE_SCHEMA.THE_TABLE

Tuesday, July 22, 2008

useful mkdir variant

You can actually make a whole path of directories with one single mkdir command:

mkdir -p a/b/c/d/e

will create a folder a, inside it folder b, ..., inside it folder e. If part of the path exists, it will not raise any error, it will simply create the part that does not yet exist.

To see how it proceeds, use:

mkdir -pv a/b/c/d/e

Thursday, July 10, 2008

Mallet command to generate topic models

To generate topic model:

First, split the data into individual files:

split -l 1 -d -a 6 ../data.txt data-

Then convert the split data to mallet format:

text2vectors --input data --remove-stopwords --output data-mallet.txt --keep-sequence TRUE --keep-sequence-bigrams TRUE

Next generate the topics:

vectors2topics --input data-mallet.txt --num-topics 250 --num-top-words 100 --output-doc-topics doc-topics.txt --num-iterations 100 --show-topics-interval 1000 > topic-words.txt

To generate at phrase level, use the N-gram option:

vectors2topics --input data-mallet.txt --num-topics 250 --num-top-words 100 --output-doc-topics doc-topics.txt --num-iterations 100 --show-topics-interval 1000 --use-ngrams true > topic-phrases.txt

If any source file is modified, run "make clean", "make", "make jar". if "make jar" is skipped, the change will not be seen.

Tuesday, July 17, 2007

Boolean Blues

All the boolean functions of two variables...

The color of each pixel is calculated as X OP Y, where X is the X coordinate of the pixel and Y is the Y coordinate of the pixel, and OP represents the boolean function that's being implemented. The code follows the images...




















The main generation loop:


for(int b = 0; b < 16; b++)
{
sprintf(filename, "out%d.bmp", b);
Renderer *r = new BMPRenderer(g, filename);
TruthTable *tt = new TruthTable(b);
for(int y = 0; y < g->getHeight(); y++)
{
for(int x = 0; x < g->getWidth(); x++)
{

int color = tt->getResultByte(x, y);
int R = (color & 0x00FF0000) >> 16;
int G = (color & 0x0000FF00) >> 8;
int B = (color & 0x000000FF);
g->putPixel(x, y,
(unsigned char)R,
(unsigned char)G,
(unsigned char)B);
}
}
r->render();
}


The TruthTable class:


#include <iostream>

using namespace std;

#define TABLE_SIZE 4

class TruthTable
{
int theTable[TABLE_SIZE];
public:
TruthTable(int table);
virtual ~TruthTable();
int getResultBit(int x, int y);
unsigned char getResultByte(unsigned char x, unsigned char y);
};

TruthTable::TruthTable(int table)
{
int mask = 0x01;
for(int i = 0; i < TABLE_SIZE; i++)
{
int result = (table & mask) >> i;
this->theTable[i] = result;
mask = mask << 1;
}
}

TruthTable::~TruthTable()
{
}

int TruthTable::getResultBit(int x, int y)
{
int mask = 0x01;
int index = ((x & mask) << 1) | (y & mask);
return this->theTable[index];
}

unsigned char TruthTable::getResultByte(unsigned char x, unsigned char y)
{
int mask = 0x01;
int result = 0;
for(int i = 0; i < 8; i++)
{
int bit_x = (x & mask) >> i;
int bit_y = (y & mask) >> i;
int bit_result = this->getResultBit(bit_x, bit_y);
result = result | (bit_result << i);
mask = mask << 1;
}
return result;
}

Friday, July 13, 2007

Random Sine Curves...

Randomly generated Sine curves with Cosine thickness and random amplitude, period and displacements. Used the standard fire color gradients: first from white to yellow, then from yellow to red, and then red "fades to black" ;) The code follows the images...

Images:








The main generation code:


int y = g->getHeight() / 2;
int y_period = 1 + rand() % 512;
int y_disp = 1 + rand() % g->getHeight();
int width = 128;
int width_period = 1 + rand() % 256;
int width_amp = 1 + rand() % 128;
int width_disp = 1 + rand() % 128;
for(int x = 0; x <>getWidth(); x++)
{
y = (int)(y_disp + width * sin(M_PI / y_period * x));
width = (int)(width_disp + width_amp * cos(M_PI / width_period * x));
drawVerticalFireLine(g, x, y, width);
}


The code for the line:


void drawVerticalFireLine(Graphics *g, int cx, int cy, int radius)
{
int seg_len = radius / 3;
//
// white to yellow
//
for(int i = 0; i <= seg_len; i++) { int intensity = (int)((1 - (double)i / (double)seg_len) * 255); g->putPixel(cx, cy + i, 255, 255, intensity);
g->putPixel(cx, cy - i, 255, 255, intensity);
}
//
// yellow to red
//
for(int i = 0; i <= seg_len; i++) { int intensity = (int)((1 - (double)i / (double)seg_len) * 255); g->putPixel(cx, cy + seg_len + i, 255, intensity, 0);
g->putPixel(cx, cy - seg_len - i, 255, intensity, 0);
}
//
// red to black
//
for(int i = 0; i <= seg_len; i++) { int intensity = (int)((1 - (double)i / (double)seg_len) * 255); g->putPixel(cx, cy + 2 * seg_len + i, intensity, 0, 0);
g->putPixel(cx, cy - 2 * seg_len - i, intensity, 0, 0);
}
}


More Images: