Emulating vi’s % command in Emacs

In vi/vim, The % command will match any parenthesis/bracket for its counterpart. The same thing can be achieved in emacs by one of the following ways:

  1. In all language modes, the following keys work:
       M-C-b          : Goto starting bracket ({, (, < etc.)
       M-C-f          : Goto ending bracket (}, ), > etc.)
       M-C-a          : Goto beginning of function
       M-C-e          : Goto end of function
       
  2. If you want the exact behavior of vi “%” command, matching all parens, use the following elisp code:
    (defun match-parenthesis ()
      "Go to the matching parenthesis if on parenthesis else insert %."
      (interactive)
      (cond ((looking-at "[([{]") (forward-sexp 1) (backward-char))
    		  ((looking-at "[])}]") (forward-char) (backward-sexp 1))
    		  (t (self-insert-command 1))))
    (global-set-key "%" 'match-parenthesis)
        

    It matches the parenthesis if the point is on a parenthesis, otherwise inserts the character ‘%’.

Emacs
vi/vim

Comments (0)

Permalink

How to catch a lion

(Got from an e-mail forward)

Newton ’s Method:

Let, the lion catch you.
For every action there is an equal and opposite reaction.
Implies you caught lion.

Einstein Method:

Run in the direction opposite to that of the lion.
Due to higher relative velocity, the lion will also run faster and will
get tired soon.
Now you can trap it easily.

Software Engineer Method:

Catch a cat and claim that your testing has proven that its a Lion.
If anyone comes back with issues tell that you will upgrade it to Lion.

Indian Police Method:

Catch any animal and interrogate it & torture it to accept that its a
lion .

Rajnikanth Method :

Keep warning the lion that u may come and attack anytime.
The lion will live in fear and die soon in fear itself.

Jayalalitha Method:

Send Police commissioner Muthukaruppan around 2AM and kill it, while
it’s sleeping !

Manirathnam Method (director):

Make sure the lion does not get sun light and put the lion in a dark
room with a single candle lighted.
Keep murmuring something in its ears.
The lion will be highly irritated and commit suicide.

Karan Johar Method (director):

Send a lioness into the forest.
Our lion and lioness fall in love with each other.
Send another lioness in to the forest, followed by another lion.
First lion loves the first lioness and the second lion loves the 2nd
lioness.
But 2nd lioness loves both lions.
Now send another lioness (third) into the forest.
You don’t understand right… ok….read it after 15 yrs, then also u
wont!

Yash Chopra method (director):

Take the lion to Australia or US.. and kill it in a good scenic
location.

Govinda method:

Continuously dance before the lion for 5 or 6 days.

Menaka Gandhi method:

Save the lion from a danger and feed him with some vegetables
continuously.

George bush method:

Link the lion with Osama bin laden and shoot him!!!

Ravi Shastri method:

Ask the lion to bowl at u.
U bat for 200 balls and score 1 run
Lion tired and surrenders

Fun
Indian Jokes
Jokes

Comments (0)

Permalink

History test answers

(Got from an e-mail forward)

The following are actual answers given on history tests and in Sunday school quizzes by children between fifth and 6th grade ages. They were collected over a period of three years by two teachers. Read carefully for grammar, misplaced modifiers, enough misinformation to satisfy anyone… and, of course, spelling!:

  1. Ancient Egypt was old. It was inhabited by gypsies and mummies who all wrote in hydraulics. They lived in the Sarah Dessert. The climate of the Sarah is such that all the inhabitants have to live elsewhere.
  2. Moses led the Hebrew slaves to the Red Sea where they made unleavened bread, which is bread made without any ingredients. Moses went up on Mount Cyanide to get the ten commandos. He died before he ever reached Canada but the commandos made it.
  3. Solomon had three hundred wives and seven hundred porcupines. He was actual hysterical figure as well as being in the bible.
  4. The Greeks were a highly sculptured people, and without them we wouldn’t have history. The Greeks also had myths. A myth is a young female moth. Socrates was a famous old Greek teacher who went around giving people advice.They killed him. He later died from an overdose of wedlock which is apparently poisonous. After his death, his career suffered a dramatic decline.
  5. In the first Olympic games, Greeks ran races, jumped, hurled biscuits, and threw the java.
  6. Julius Caesar extinguished himself on the battlefields of Gaul. The Ides of March murdered him because they thought he was going to be made king. Dying, he gasped out: “Same to you, Brutus.”
  7. Joan of Arc was burnt to a steak and was canonized by Bernard Shaw for reasons I don’t really understand. The English and French still have problems.
  8. Queen Elizabeth was the “Virgin Queen,” As a queen she was a success. When she exposed herself before her troops they all shouted “hurrah!” and that was the end of the fighting for a long while.
  9. It was an age of great inventions and discoveries. Gutenberg invented removable type and the Bible. Another important invention was the circulation of blood.
  10. Sir Walter Raleigh is a historical figure because he invented cigarettes and started smoking.
  11. Sir Francis Drake circumcised the world with a 100 foot clipper which was very dangerous to all his men.
  12. The greatest writer of the Renaissance was William Shakespeare. He was born in the year 1564, supposedly on his birthday. He never made much money and is famous only because of his plays. He wrote tragedies, comedies, and hysterectomies, all in Islamic pentameter.
  13. Romeo and Juliet are an example of a heroic couple. They lived in Italy. Romeo’s last wish was to be laid by Juliet but her father was having none of that I’m sure. You know how Italian fathers are.
  14. Writing at the same time as Shakespeare was Miguel Cervantes. He wrote Donkey Hote. The next great author was John Milton. Milton wrote Paradise Lost. Since then no one ever found it.
  15. Delegates from the original 13 states formed the Contented Congress. Thomas Jefferson, a Virgin, and Benjamin Franklin were two singers of the Declaration of Independence. Franklin discovered electricity by rubbing two cats backward and also declared, “A horse divided against itself cannot stand.” Franklin died in 1790 and is still dead.
  16. Abraham Lincoln became America’s greatest Precedent. Lincoln’s mother died in infancy, and he was born in a log cabin which he built with his own hands. Abraham Lincoln freed the slaves by signing the Emasculation Proclamation.
  17. On the night of April 14, 1865, Lincoln went to the theater and got shot in his seat by one of the actors in a moving picture show. They believe the assinator was John Wilkes Booth, a supposing insane actor. This ruined Booth’s career.
  18. Johan Bach wrote a great many musical compositions and had a large number of children. In between he practiced on an old spinster which he kept up in his attic. Bach died from 1750 to the present. Bach was the most famous composer in the world and so was Handle. Handle was half German, half Italian, and half English. He was very large.
  19. Beethoven wrote music even though he was deaf. He was so deaf that he wrote loud music and became the father of rock and roll. He took long walks in the forest even when everyone was calling for him. Beethoven expired in 1827 and later died for this.
  20. The nineteenth century was a time of a great many thoughts and inventions. People stopped reproducing by hand and started reproducing by machine. The invention of the steamboat caused a network of rivers to spring up.
  21. Louis Pasteur discovered a cure for rabbits but I don’t know why.
  22. Charles Darwin was a naturalist. He sort of said God’s days were not just 24 hours but without watches who knew anyhow? I don’t get it.
  23. Madman Curie discovered radio. She was the first woman to do what she did. Other women have become scientists since her but they didn’t get to find radios because they were already taken.
  24. Karl Marx was one of the Marx Brothers. The other three were in the movies. Karl made speeches and started revolutions. Someone in the family had to have a job, I guess.

Fun
Jokes

Comments (2)

Permalink

Circle from three points

The problem

Given three points , and . Find the center and radius of the circle passing through it.

Discussion

Three non-collinear points represent a unique circle on a unique plane. It is easy to construct the circle from 3 points by drawing perpendicular bisectors of any two chords. If the 3 points are not collinear, the bisectors should meet at the center of the circle.

Using vectors, it is possible to devise an algorithm to do this. However, an easier method will be substituting the three points into the general equation of the circle

and solving for , and . Center is and the radius is
.

Starightforward algorithm

This can be solved by substituting the three values in the above equation and solving the set of three simultaneous equations.

The solution to the above set of simultaneous equations yields that the equation of the circle passing through , and is given by equating the value of the following determinanat to zero:

A simpler algorithm

A modified algorithm is given below:

The task will be much simpler if we apply a linear transformation so that one of the points (say ) is , and now the constant term vanishes, and now there are only two variables and two unknowns. We can transform the co-ordinates back after solving.

So, transform , , to

and now the equations are

where

Now, we can solve for g and f here.

Finally, applying the tranformations back, the center is .

The following C function illustrates this algorithm.

#include <math.h>
#define TRUE 1
#define FALSE 0

int  /* Returns TRUE if it is a valid circle, FALSE if the points are collinear */
solve_3_points_circle (
   double  x1, double  y1, /* Point 1 */
   double  x2, double  y2, /* Point 2 */
   double  x3, double  y3, /* Point 3 */
   double* cx, double* cy, /* Center  */
   double* r               /* Radius  */
)
{
   double z1, z2, d;

   /* Apply the transforms */
   x1 -= x3;
   y1 -= y3;
   x2 -= x3;
   y2 -= y3;

   Temporary variables to avoid some computations */
   z1 = x1 * x1 + y1 * y1;
   z2 = x2 * x2 + y2 * y2;
   d = 2.0 * (x1 * y2 - x2 * y1);
   if (fabs(d) < 0.00000001) {
      return FALSE;
   }

   /* Calculate the center of the transformed circle */
   *cx = (y2 * z1 - y1 * z2)/d;
   *cy = (x1 * z2 - x2 * z1)/d;
   r = sqrt(*cx * *cx + *cy * *cy);

   /* Apply the transforms back */
   *cx += x3;
   *cy += y3;
   return TRUE;
}

Algorithm given by Plastock and Kalley

Plastock and Kalley gives the following formulae for solving this problem:

The center and the radius of a circle passing through the points ,
and , are given by:

where

These formulae can be used to find the parameters of the circle.

Solution by following the geometric construction

The line passing through and is

where

The perpendicular bisector of this line passes through and has a slope of .

So, the perpendicular bisector is

Similarly, the perpendicular bisector of the side passing through and is

where

Now, the center of the circle is the point of intersection of these two perpendicular bisectors. So, equating the right hand sides of the above two equations, and solving for x,

Algorithms
C/C++
Mathematics
Programming

Comments (0)

Permalink

Limeric: Another formula

By Chris Boyd:

Four plus the difference between
The factorial of six and the mean
   Of twelve squared and four
   Hundred three (plus one more)
Equals double the square of fifteen.

Limericks
Mathematics

Comments (1)

Permalink

Limerick: Is circle round?

A conjecture both deep and profound
Is whether the circle is round;
   In a paper by Erdo”s,
   Written in Kurdish,
A counterexample is found.

Limericks
Mathematics

Comments (2)

Permalink

Limerick: Cube of infinity

A graduate student from Trinity
Computed the cube of infinity;
   But it gave him the fidgets
   To write down all those digits,
So he dropped math and took up divinity.

Limericks
Mathematics

Comments (0)

Permalink

Limerick: Modular arithmetic and climate

In arctic and tropical climes,
The integers, addition, and times,
   taken (mod p) will yield
   A full finite field,
As p ranges over the primes.

Limericks
Mathematics

Comments (0)

Permalink

Limerick: A formula

This poem was written by John Saxon (an author of math textbooks).

A Dozen, a Gross and a Score,
plus three times the square root of four,
   divided by seven,
   plus five times eleven,
equals nine squared and not a bit more.

Limericks
Mathematics

Comments (0)

Permalink

Limerick: Peeling a Moebius strip

A burleycque dancer, a pip
Named Virginia, could peel in a zip;
   But she read science fiction
   And died of constriction
Attempting a Moebius strip.

Limericks
Mathematics

Comments (0)

Permalink