Archive for the ‘Uncategorized’ Category

Converting Spreadsheet Data table to a Single Row/Column Data

March 9, 2014

Say given you have a table of data in your spreadsheet and you want to convert it in to a column of data where each row of data in the table is converted in to a column of data itself. You can do this easily using the spread sheet functions in the cells by doing the following,

  1. assume the data to be indexed 1 through n (n=total number of data cells in the data table)

    eg: data table of 5×8 will have 40 indexes

  2. for each index number associated with the data-cell determine the row and column for that data (you can easily do this with MOD and DIVIDE/INT commands, but mind the special edge case)

    eg: (with above example) index 8 corresponds to row 2, column 3

  3. Using “ADDRESS” command you can determine the cell address of a <row>,<column>

    eg: “2,3” corresponds to cell address “C2”

  4. Using “INDIRECT” command you can retrieve the value of a cell address

    eg: INDIRECT(“C2”)

A sample could be found @

Old Town Trolly Tours – San Diego

July 26, 2013

Its pretty much fun to listen to the tour guide…. Got a discounted price tour for $15 due to the XSEDE13 conference… Well worth the money…. Word of advice, sit on the right hand side of the trolly facing the front. Thats the side most of the good stuff you are gonna see…

My First MPI program

July 23, 2013

This was long overdue IMO. Here’s the simple hello world…

#include <stdio.h> 
#include <mpi.h> 
int main(int argc, char **argv) { 
 int rank, size;  MPI_Init(&argc, &argv); 
 MPI_Comm_rank(MPI_COMM_WORLD, &rank); 
 MPI_Comm_size(MPI_COMM_WORLD, &size); 
 printf("I'm the process number %d out of %d ", rank, size)); 
 return 0; 

to compile,

$ mpicc hello.c -o hello_exec

Note: if you have writtern in C++, use “mpic++” instead.

you can execute this as a normal application as usual but if you want it to execute in a cluster, use “mpirun” command.

$ mpirun -np 5 ./hello_exec

Note: “-np” is for number of processes to spawn. If you haven’t provided where to spawn the processes they will be created in the node 0 which is usually the host machine which you are triggering the MPI application. But if you know the other nodes you just need to specify those nodes. 

$ mpirun -f hosts.txt -np 5 ./hello_exec


$ mpirun -hosts host1,host2,host3 -np 5 ./hello_exec

and the mpirun will spawn processes in those hosts in round robbin fasion.

Java Swing 2D Create a Rounded Corner Polygon

July 21, 2012

Incase you are wondering I couldn’t find a straightforward method. Thus I wrote the following functions. Given a set of connecting points or a polygon the following function will return a polygon with rounded corners.

public static GeneralPath getRoundedGeneralPath(Polygon polygon) {
   List<int[]> l = new ArrayList<int[]>();
   for(int i=0; i < polygon.npoints; i++){
      l.add(new int[]{polygon.xpoints[i],polygon.ypoints[i]});
   return getRoundedGeneralPath(l);
public static GeneralPath getRoundedGeneralPath(List<int[]> l) {
   List<Point> list=new ArrayList<Point>();
   for (int[] point : l){
      list.add(new Point(point[0],point[1]));
   return getRoundedGeneralPathFromPoints(list);
public static GeneralPath getRoundedGeneralPathFromPoints(List<Point> l) {
   GeneralPath p = new GeneralPath();
   for(int pointIndex=1; pointIndex<l.size()-1;pointIndex++){
      Point p1=l.get(pointIndex-1);
      Point p2=l.get(pointIndex);
      Point p3=l.get(pointIndex+1);
      Point mPoint = calculatePoint(p1, p2);
      p.lineTo(mPoint.x, mPoint.y);
      mPoint = calculatePoint(p3, p2);
      p.curveTo(p2.x, p2.y, p2.x, p2.y, mPoint.x, mPoint.y);
   return p;
private static Point calculatePoint(Point p1, Point p2) {
   float arcSize=10;
   double d1=math.sqrt(math.pow(p1.x-p2.x, 2)+math.pow(p1.y-p2.y, 2));
   double per=arcSize/d1;
   double d_x=(p1.x-p2.x)*per;
   double d_y=(p1.y-p2.y)*per;
   int xx=(int)(p2.x+d_x);
   int yy=(int)(p2.y+d_y);
   return new Point(xx,yy);
   public static void main(String args[]) {
        JFrame f = new JFrame("Rounded Corner Demo");

        JPanel contentPane = new JPanel() {
            protected void paintComponent(Graphics grphcs) {
                Graphics2D g2d = (Graphics2D) grphcs;

                GradientPaint gp = new GradientPaint(0, 0,
                        getBackground().brighter().brighter(), 0, getHeight(),

                g2d.fillRect(0, 0, getWidth(), getHeight());
                int[][] a={{50,0},{100,50},{50,100},{0,50}};
                GeneralPath p = getRoundedGeneralPath(Arrays.asList(a));
        contentPane.add(new JLabel("test"));
        f.setSize(200, 200);

Have fun…

Bash working with the filename

March 23, 2012

In bash/sh scripting its very common that you are given a filename and have to perform various tasks on it. Following are some of the bash functions I created in order to perform them. Feel free to use them as you like.

Incase you dont know how bash functions work it is explained here. If you are only interested in how it is done just look at whats inside the function only. Thats where the magic happens.
#print the filename from the given path
 echo $(basename $1)

#print the filename without the extension
 local filename=`get_file_name $1`
 echo ${filename%.*}

#print the filename extension
 local filename=`get_file_name $1`
 echo ${filename##*.}

#print parent directory of a file
 echo $(dirname $1)

#print the absolute path of a file/directory given its relative path
 local relative_path=$1
 if [ -d "$relative_path" ]
    local absolute_path=`cd $relative_path; pwd`
 local file_name=`get_file_name $relative_path`
 local dir_name=`get_path_to_file $relative_path`
 local dir_name=`cd $dir_name; pwd`
 local absolute_path=`combine_path $dir_name $file_name`
 echo $absolute_path

#disguised to look like it just prints the number of files in a directory but infact it launches world war 3
 echo `ls -1 $1 | wc -l`

Functions in bash/sh scripts

March 23, 2012

Its pretty simple to work with functions of commandline bash/sh scripts.

Define function

This is how you define a function
  <bash command_1>     
  <bash command_n>
  echo "Hello World"

Calling the function

In order to call the function the function needs to be defined as above before you actually call it. Then you just use the name of the function
  echo "Hello World"


Passing parameters to the function

Passing parameters to the function is pretty weird. Its the same as parameters being passed to a script. The function will have its own set of $* variable where the 1st parameter is $1, the next is $2 and so on.
  echo "Hello $1 $2"

say_hello "Saminda" "Wijeratne"

Defining variables inside a function

Defining variables inside function is same as how you’d do usually in a script. Only difference is that if you want the variable to be local to the function you need to use the keyword “local” when defining it.
  local first_name=$1
  last_name=$2           #becomes a global variable
  echo "Hello $first_name $middle_name $last_name"

middle_name="Saminda"               #global variable
say_hello "Kushan" "Wijeratne"
echo $firt_name
echo $last_name
Passing a string as a variable is easy. But passing an array to a function is pretty tricky. Here’s an example on how to do it.
 eval "declare -A bio_data_array="${1#*=}
 #now bio_data_array is the array that was passed as a parameter
 echo "Hello ${bio_data_array['name']}"

declare -A bio_data
say_hello "$(declare -p bio_data)"

Returning values from the function

You’ll be surprised to know that bash functions never return anything. However you can capture what was printed by the function in the form of a string using the cool feature in bash where you can execute commands within “`” characters which the result is the output given by that command.
output_string=`say_hello $firt_name`
echo "This is what the say_hello function had to say: $output_string"
There are several other ways like using global variables or pass a variable name as a parameter to save the result of the function inside the function itself. But I prefer the above method since the function doesn’t have to worry about whether or not its a function or not.

Mounting a file as a file system in Linux

March 21, 2012

This is called mounting a loopback device.

3 steps.
  1. Creating the file that can be mounted as a file system
  2. Assigning the file as a block device
  3. Mounting the block device to a path
Creating the file that can be mounted as a file system

Use the dd command to create an empty file of the size which you want your file system.
  $ dd if=/dev/zero of=my_fs bs=1024 count=30720
of – parameter specifies the name of the file
bs – size of the block (represents x no of KB. You can specify x no of MB using xM: i.e. “bs=1M”)
count – how many blocks to be present (thus the size of the file system = bs*count)
if – from which device the file content initially should be filled with.
But still the file that gets created is not a proper filesystem yet. you need to format it using some file system type. In order to do that first lets assign the file to a loop back device (the next step)
Assigning the file as a block device

In linux a file system has to be a block device in order for it to be managed as a file system. In this particular case we use a set of defined loop back devices for our purpose. eg: /dev/loop0 up to /dev/loop8
in order to check if a particular loop back device is already being used use the command,
  $ losetup /dev/loop0
If its already being used you will see it mentioned in the output of the above command. Once you find a free loop back device to attach/assign the file to it
  $ losetup /dev/loop0 my_fs
This command needs to be issued as a sudoer user.
Now lets format the loopback device we just attached. 
$ mkfs -t ext3 -m 1 -v /dev/loop0
Effectively we are creating a filesystem inside a file using the above command. I’m formatting it as ext3 file system. You may have to use sudo again.
Mounting the block device to a path

The hard work is done. Now to just mount.
  $ mount -t ext3 /dev/loop0 /mnt/fs_mount_location/
You may ommit the “-t ext3” parameter in this case since usually linux file systems are automatically detected at mount. You may have to sudo the above command. 
Thats it actually. Once you are done you should clear up after you ofcourse. to do that just 2 commands
  $ umount /mnt/fs_mount_location/
to unmount. you may need sudo again
  $ losetup -d /dev/loop0
to detach the file from the loop back device. Need sudo again.
Steps are curtasy of

3 Lessons I never seem to learn

March 13, 2012
  1. Never trust women
  2. Never leave your work until the last minute
  3. Try to learn the 3 lessons I never seem to learn

Listening to something you already knew

March 4, 2012

I always knew that knowing something and understanding something is totally different. But I never thought that knowing something and listening to what I know can feel totally different.


Today I read a blog post of a friend of mine… Or perhaps more elaborately speaking who used to be a friend of mine… to be more exact a person who is at the other end of the path filled with alot of broken glass. I knew who she was, the kind of person who she really is before she changed (or was it I who changed?). I don’t know at which point I knew those stuff.. or how I knew for that matter. Perhaps because I’ve being in the same shoes… same fears, same doubts… but somehow that did not really connect us. I guess it made us feel that we are related. Like fruit from the same tree (but in different branches).


Ever being in front of a crowd which exceeded the hall? over 300 pairs of eyes following your every movement. When you take the Mic the slightest scratch on your head.. the smallest stutter… gets noticed like its the main highlight of the evening… At that precise moment they’ve no idea how hard it is to look at the audience without having your head feeling dizzy… stop your hands shaking of feeling cold… trying to utter words out of a mouth that all of a sudden has gone dry…. What in the world am I doing here… 


It was about a public speech she made last year. The very first one to a variety of people of such numbers. I was sooooo proud of her on what she is about to do… Felt like “my little girl all grown up”. Felt like all the work over the years led to this… To this one single moment of my achievement through her. My achievement in being part of making her who she is… I got the rare feeling of a feeling of being swelled with pride. It felt like a dream she had said. Back then we were still friends… there were some large cracks in the glass, but still friends…

Reading the post was one of the happiest and disappointing post I’ve ever read. I don’t know why but feels that both are the same.  She had done her speech splendidly… Happy because it showed how she came out of her shell to see the world. To know that she can achieve anything and everything is possible if she wants to. I could almost imagine how she was smiling when she typed those words. There’s a weird thing about achievements. It comes with a cost. “Overconfidence” one might say… “misguided” one might judge… “lost” one would proclaim… but I’d say disappointment and leave it at that (going further will not help anyone).  bcos less than a week later she shattered our friendship of had remained in to dust.


Friendships don’t last forever… Something which I already knew… but turns out it doesn’t matter that you did…

Answering the most hardest questions

July 28, 2011

1. How to understand women?
– Still being calculated. Says it will take more time than it took to find the answer “42”.
2. What is love?
– sacrifice (answer tested & verified)