Monday, August 9, 2010

using tmpnam is dangerous. Use mkstemp

Whenever I used tmpnam my g++ compiler cribbed that its dangerous to use tmpnam and must use mkstemp. But mkstemp returned file id and as my language is C++, i never knew how to use the C file ids with C++ fstreams. Then I found out from the man page of fstream that,


explicit basic_ofstream(int fd);
explicit basic_ofstream(const char *s,
ios_base::openmode mode =
ios_base::out,
long protection = 0666);

I was familiar with the second usage but the first usage was new to me...So one can easily use ofstream with mkstemp as shown below.

char *tmpFileName= strdup("/tmp/tmpfileXXXXXX");
if(mkstemp(tmpFileName) == -1)
throw runtime_error("temporary file cannot be created");
ofstream out(tmpFileName);

Enjoy C++

Thursday, August 5, 2010

C/C++ interface for gnuplot

I think the C interface by N.Devillard is great! I just used it in my C++ code and it worked like a charm!

Please checkout gnuplot interfaces in ANSI C.

I also tried Gnuplot-iostream interface but somehow that did not work for me....

git

It was a big relief to see git. At last, I need not maintain a central repository and the git commands seemed so simple....

Once I went back to an older version - alas, all the later versions were lost. After reading some books in git, I realised that I should have branched from an older version. Thus if you are planning to experiment, it is better to branch to a new branch and experiment. If the experiment is successful, you can merge back into main stream. So easy!

The branch command is:

git branch branch_name tag_name(or commit hash)

to checkout the branch

git checkout branch_name

It is extremely comfortable to checkout branch and then merge into...

git merge for merging and for visualization of log, one can use
git log --graph --pretty=oneline --abbrev-commit

Now the eclipse git plugin provides branche/merge etc...making life more comfortable!

Thank you Linus Torvalds

Wednesday, January 13, 2010

How to display mathematica on a local ubuntu machine

When I first ran mathematica, I got the following errors in the server machine.
xset: bad font path element (#23), possible causes are:
Directory does not exist or has wrong permissions
Directory missing fonts.dir
Incorrect font server address or syntax

This problem can be solved very easily by doing the following in your local linux machine. This is what I did.


  1. Download and install mathematica fonts from
    here on your local machine.

  2. execute the following commands in local machine.
    xset fp+ /your_mathematica_fonts_directory/Fonts/Type1
    xset fp rehash

  3. ssh to your mathematica server using -X option and type mathematica at the command prompt.


  4. You can stop reading this blog at this point, because this is enough for you to go. However some interesting stuff is available below.

  5. Steps 2 and 3 must be executed every time you want to run mathematica.

  6. May be, you would prefer to write a script to do all this so that in one click you get into the server. My script (which I have named mathematica) is given below


  7. #!/bin/bash
    xset fp+ /your_fonts_directory/Fonts/Type1
    xset fp rehash
    ssh your_login_id@your_mathematica_server -X mathematica

  8. You may be wondering that you will need to type the server account password every time. You need not do that. If you follow the instructions in this link, you can set up an rsa public key and avoid typing password.

  9. Thus with the above steps, with a single command or click you can run mathematica on your ubuntu desktop !

  10. Enjoy

Friday, January 1, 2010

A student's creativity

The following concerns a question in a physics degree exam at the University of Copenhagen:

"Describe how to determine the height of a skyscraper using a barometer."

One student replied:

"You tie a long piece of string to the neck of the barometer, then lower the barometer from the roof of the skyscraper to the ground.

The length of the string plus the length of the barometer will equal the height of the building."

This highly original answer so incensed the examiner that the student was failed.

The student appealed on the grounds that his answer was indisputably correct, and the university appointed an independent arbiter to decide the case.

The arbiter judged that the answer was indeed correct, but did not display any noticeable knowledge of physics.

To resolve the problem it was decided to call the student in and allow him six minutes in which to provide a verbal answer which showed at least a minimal familiarity the basic principles of physics.

For five minutes the student sat in silence, forehead creased in thought.

The arbiter reminded him that time was running out, to which the student replied that he had several extremely relevant answers, but couldn't make up his mind which to use.

On being advised to hurry up the student replied as follows:

"Firstly, you could take the barometer up to the roof of the skyscraper, drop it over the edge, and measure the time it takes to reach the ground.

The height of the building can then be worked out from the formula H = 0.5g x t squared. But bad luck on the barometer."

"Or if the sun is shining you could measure the height of the barometer, then set it on end and measure the length of its shadow.

Then you measure the length of the skyscraper's shadow, and thereafter it is a simple matter of proportional arithmetic to work out the height of the skyscraper."

"But if you wanted to be highly scientific about it, you could tie a short piece of string to the barometer and swing it like a pendulum, first at ground level and then on the roof of the skyscraper.

The height is worked out by the difference in the gravitational restoring force T = 2 pi sq root (l / g)."

"Or if the skyscraper has an outside emergency staircase, it would be easier to walk up it and mark off the height of the skyscraper in barometer lengths, then add them up."

"If you merely wanted to be boring and orthodox about it, of course, you could use the barometer to measure the air pressure on the roof of the skyscraper and on the ground, and convert the difference in millibars into feet to give the height of the building."

"But since we are constantly being exhorted to exercise independence of mind and apply scientific methods, undoubtedly the best way would be to knock on the janitor's door and say to him, 'If you would like a nice new barometer, I will give you this one if you tell me the height of this skyscraper'."

The student was Niels Bohr from Denmark who won the Nobel prize for Physics.