Speed of Light – in Vedas

[This article is reproduction of an exciting content found elsewhere.]

Speed of light in Rigveda: (Originally from http://on.fb.me/IpQAF2, a long commentary at http://bit.ly/I55raN)

Ancient Vedic science “Nimisharda” is a phrase used in Indian languages of Sanskrit origin while referring to something that happens/moves instantly, similar to the ‘blink of an eye’. Nimisharda means half of a Nimesa, (Ardha is half).

In Sanskrit ‘Nimisha’ means ‘blink of an eye’ and Nimisharda implies within the blink of an eye. This phrase is commonly used to refer to instantaneous events.

Below is the mathematical calculations of a research done by S S De and P V Vartak on the speed of light calculated using the Rigvedic hymns and commentaries on them.

The fourth verse of the Rigvedic hymn 1:50 (50th hymn in book 1 of rigveda) is as follows:
तरणिर्विश्वदर्शतो जयोतिष्क्र्दसि सूर्य | विश्वमा भासिरोचनम |
taraNir vishvadarshato jyotishkrdasi surya | vishvamaa bhaasirochanam ||

which means:
“Swift and all beautiful art thou, O Surya (Surya=Sun), maker of the light, Illumining all the radiant realm.”

Commenting on this verse in his Rigvedic commentary, Sayana who was a minister in the court of Bukka of the great Vijayanagar Empire of Karnataka in South India (in early 14th century) says:
” tatha ca smaryate yojananam. sahasre dve dve sate dve ca yojane ekena nimishardhena kramaman.” which means “It is remembered here that Sun (light) traverses 2,202 yojanas in half a nimisha”
NOTE: Nimisharda= half of a nimisha.

Read more …

User Experience and Application on Google App Engine

So, we have been working on this application… web based, data driven, to be used in “social context”. I think today there’s nothing sexier than using the word social.

We designed this application nicely, fantastic by many ways. We were sure of putting this up on a public cloud, just that we were not sure of which one: EC2 or GAE. One of the most riskiest questions to keep open… but really, we were not sure. The only thing we were sure of was Java + jQuery + HTML5 (as much as possible).

EC2 gives you IaaS but then you must manage all the servers, load balancing, failovers. Even if you happen to choose BeanStalk, a lot of work has to be done, specifically database. Thankfully, we now have ElastiCache (launched around August 2011, IIRC) otherwise managing a cluster of Memcache was yet another task for the admins.

GAE provides you PaaS, so you don’t need to worry about doing all the stuff in previous paragraph yourself. Except that you have strong restrictions… a very small subset of Java SE library – specifically, not threads or direct sockets, no JDBC and Datastore, though amazing, is not an easy meat to digest for those not used to. And above all, the server side execution must complete in 30 seconds (now on what are known are front-end servers).

So, we designed our own data-access abstraction so that we can switch EC2 <=> GAE without any hassles. It took a while, but it’s a great thing that we have.

After writing core entities and business logic layer, it was time to start pushing it all up to the user interface. The dashboard – the landing page after login – has to get updates from various “components” in the system. All components worked great, except that the dashboard took somewhere around 1200ms to load for the first time. Note that most of the public resources (images, JavaScript, CSS) were loaded from GAE CDNs.

Just not acceptable!

Read more …

Card and Deck – Deal and Shuffle

There are a lot of websites showing how do you write code for the classes Card and Deck implementing two key operations – deal and shuffle. And almost all of them have an O(N) solution for shuffling the cards (N = 52, the number of cards). (For example, http://math.hws.edu/javanotes/c5/s4.html and awesome discussion at http://www.codinghorror.com/blog/2007/12/shuffling.html, or just search on Google for “card deck deal shuffle code” or even “card deck deal shuffle code optimized“)

Recently, somebody asked me – if there’s a better solution.

Now let me just generalize this problem – we have a collection of N items which need to dealed and shuffled. The order in which the items are dealed should not be predictable (that’s what randomness means, right?). Think… can we do it in O(1) with minimal memory? I did it!

For the sake of simplicity, let’s number the cards 1-52 and define the interface for the IDeck as follows:

interface IDeck
{
    void Shuffle();          //Shuffles so that the "fresh" deal can start
    int Deal();              //Gives me the next card
    int Remaining { get; }   //Returns the number of cards remaining in the deck
}

Now, let’s look at the following solution…

We need to have an array that will hold these cards, in this case – an array of int – int[] cards.
Then we need an index/count indicating how many cards have been dealt or remaining – int index.

Now, dealing is simple – start picking up the items from the start / end and update value of index appropriately. For example, we can initially have index = cards.length - 1 and decrement it each time deal is done.

But the problem is that during shuffle, we need to randomly put all the cards in the array cards. Now, to randomize and shuffle, we’ll need another temporary array so that we can start picking up the cards randomly and place them across.

Well, almost all of the solutions that Google gave me do the same.

Let’s approach is slightly differently… focus on what we need.

Read more …

July with 5 weekends once in 823yrs – Know the truth

For last few days, I have been getting spam about the awesomeness and rarity of July with 5 weekends… something that is being claimed to happen once in 823 yrs. It’s all bullshit. Search on Google for “823 years July five weekends” something around it, and you’ll see great results. Once such URL is http://www.google.com/search?q=823+years+july+five+weekends. Even Times of India (the ad and sex newspaper of India) write this in their article here.

I happen to be a mathematician at heart. Reading these news, I got reminded of a calendar that I given to my father by an MR when I was in VIIth standard – that’s about 18-19yrs ago. The calendar was of some 150+ years. I was excited to get such a calendar. And then observing it closely and using some maths – though very trivial now – I realized the Gregorian Calendar repeats itself every 28yrs.

This is how… if there were no leap years, each year would have been for 365 days, that means, the calendar will advance by 1 day a week every year (365 mod 7 = 1), and hence will repeat every 7 yrs. Because every 4th year is a leap year, the reptition happens every LCM(4, 7) = 28yrs.

And in may be around 15-20minutes, I created a infinitely-usable calendar.

Ok. I admit, I have not taken into account 100yr factor… but it can be taken up.

Coming back to this 823yrs crap, and to verify my stats, I just went to TimeAndDate.com and this is what I got: http://www.timeanddate.com/calendar/?year=1983 as the calendar of 1983!(2011 – 28 = 1983).

Check it out closely… the month of July. What does it read — 5 weekends.

And so goes the calendars of 2011 +/- 28*n where n can be any number. Let’s just checkout the calendar of 2011 – 28 * 10 = 1973. Visit this: http://www.timeanddate.com/calendar/?year=1731 and you’ll know the truth.

Thanks for being enlightened! Yet another reason why I hate media… these uneducated people write news that impacts the masses. IMHO, these newspapers and media houses should be banned… donno about any criminal cases of misinformation or anything can be put against them!

Android

I have always wanted to a start a series on Android, noting that I have now spent over 3yrs working on Android, mainly at the application level (what you touch and play with on the device) and at times at the middleware level (drivers and codecs, that runs without your knowledge in the background).

So, I finally I have a go… as and when I get time, I will be writing articles on various application development aspects relating to Android. And no, I am definitely not planning to write another “Hello World” tutorial. You can search on Google for several such tutorials on the web.

I will be writing the stuff on the aspects “Beyond Hello World”.

CodeMirror port for Google Web Toolkit

I had been hunting for a good web based code editor when I came across CodeMirror at http://www.codemirror.net.

Great project and a lot of hard and smart work indeed.

I am a great fan of GWT and try to move anything and everything that comes my way to GWT :)

The result is gCodeMirror project at Google Code and GitHub.

If you are interested in directly jumping to a demo, browse to this test URL.

Love your work but do live life

It is 8am and I am just done watching Indira Nooyi, a lady that I admire, one-on-one with Barkha Dutt at NDTV Profit.

There were a lot of takeaways from the discussion but there is something that I want to take home and wish that you do too.

She writes mail, composes on Tuesday locking herself for 15 minutes and sends on Thursday, to 200 executives once every
fortnight on various topics ranging from business to career to personal.

Here’s the crux of one of her mails.

Indira and her husband would see-off their daughter daily for her school. One the first day of every grade, they would
take the photos and capture some precious moments.

One fine day, carrying forward the tradition, they were to take the photos, the daughter says, “Hey! I’m driving to college”.
And suddenly it was a reality check for them… their “little daughter” has grown up, time has passed by swiftly.

So, while it is good to be in business, great to be involved full heart and mind in work, do live your life to fullest and give
time to family and friends because time flies quickly and it never turns back.

And it reminds me of… “If only I could turn back time”.

Google App Engine and GWT – Multi-Project Application

While working with Google Web Toolkit (GWT), it is a very common scenario to split the main application into multiple sub-projects and then use IDE to do a combined build and compile. However, the problem arises in scenarios where the GWT-RPC services and their implementations are kept in separate projects.

Recently working on an application, I structured it as follows:

  • MainApp: The final GWT+GAE application that will be deployed
  • AppCommon: Common classes to be used across projects
  • AppServices: Service definitions. It comprises of the GWT-RPC service definitions and their Async counterparts.
  • MainApp project contains the implementations of the services defined in the AppServices project.

When I “GWT Compile Project” or “Deploy App Engine Project“, the “shared” or “server” code of the dependencies is never copied to the war/WEB-INF/classes folder. Only the compiled JavaScript code is compiled.

So, I thought of automating all the stuff using Ant files.

GWT app-creator tool creates the “build.xml” for Ant and for GAE, there are guidelines on the web, for example here.

At an outline, the following needs to be done during the project build:

  • Enlist all the dependencies in order.
    If there are five projects – P1, P2, P3, P4, P5 – my script cannot identify which one depends on the other. It’s the developer’s responsibility to write them in the order so that independent or least dependent projects are listed and hence compiled first.
  • Compile all the projects in order with appropriate class-path…
  • Make a jar comprising of all the compiled class files and push it to war/WEB-INF/lib folder.
  • Compile the GWT module, as usual
  • Publish the application to GAE, as usual

I have made used of the following Ant tasks:

  • foreach from ant-contrib. You can download the jar from Sourceforge. Project home page is at http://ant-contrib.sourceforge.net.
  • Standard property, mkdir, delete, javac, copy etc tasks
  • App Engine task appcfg.

First step is to define a property, I named it projects, enlisting all projects in order.

<property name="projects" value="AppCore,AppServices,AppOne,AppTwo" />

Next is to expand the default task javac to compile all the dependencies as well. To take care of dynamic classpath entries arising because of the multiple projects, I copy all the class files to the <main-app>/war/WEB-INF/classes folder and that’s my standard classpath.

<target name="javac" depends="libs" description="Compile java source">
  <foreach list='${projects}' param="project.dir" target='compileDeps' />
  <mkdir dir="war/WEB-INF/classes" />
  <javac srcdir="src" includes="**" encoding="utf-8" destdir="war/WEB-INF/classes"
    source="1.5" target="1.5" nowarn="true" debug="true" debuglevel="lines,vars,source">
    <classpath refid="project.class.path" />
  </javac>
  <copy todir="war/WEB-INF/classes">
    <fileset dir="src" excludes="**/*.java" />
  </copy>
</target>

What this target does is compile all the dependencies before compiling the main application project.

Defining the compileDeps target is more straightforward now, noting that we have a helping hand from foreach task. Here’s how it looks like.

<target name='compileDeps' description='Compiles dependencies'>
  <property name="pdir" location="../${project.dir}" />
  <property name="cdir" location="." />
  <mkdir dir="${pdir}/war/WEB-INF/classes" />
  <javac srcdir="${pdir}/src" includes="**" encoding="utf-8" destdir="${pdir}/war/WEB-INF/classes"
    source="1.5" target="1.5" nowarn="true" debug="true"
    debuglevel="lines,vars,source">
    <classpath refid="project.class.path" />
  </javac>
  <copy todir="${pdir}/war/WEB-INF/classes">
    <fileset dir="${pdir}/src" excludes="**/*.java" />
  </copy>
  <copy todir="${cdir}/war/WEB-INF/classes">
    <fileset dir="${pdir}/war/WEB-INF/classes" excludes="**/*.java" />
  </copy>
</target>

This task compiles the dependencies, and copies all resources (non-java files) from the corresponding src folder and all binaries from the corresponding war/WEB-INF/classes folder to the <main-app>/war/WEB-INF/classes folder.

I further expanded the gwtc target to make a jar file rather than individual class files in the WEB-INF folder. Here’s the code for that.

<target name="gwtc" depends="javac" description="GWT compile to JavaScript">
  <jar destfile="war/WEB-INF/lib/main-app.jar">
    <fileset dir='war/WEB-INF/classes' />
  </jar>
  <delete dir="war/WEB-INF/classes" failonerror="false" />
  <java failonerror="true" fork="true" classname="com.google.gwt.dev.Compiler">...</java>
</target>

And that’s it!

Publishing the application to GAE requires no special change. All files are right there! Just use the <appcfg> task.

Google App Engine – Dealing with large number of files

One of the most common problem that one has to deal while deploying a large scale application at Google App Engine is the file limit – in terms of number of files.

As per the issue reported at http://code.google.com/p/googleappengine/issues/detail?id=161, the limit has been increased from 1000 to 3000, but there’s catch. Any static file (non-JSP files outside the WEB-INF folder, unless explicitly excluded in appengine-web.xml) are counted twice. Why? They’re pushed into a special folder __static__ and all such files are counted twice.

I looked at deploying the SmartGWT showcase on GAE and the “appcfg upload” gave an error about having over 9000 files. I was shocked! Literally.

And then I came across a simple yet powerful idea – zip all the static files and use a servlet to extract and serve on the fly. The solution servlet was uploaded by sindhom at http://code.google.com/p/googleappengine/issues/detail?id=161#c68.

Take that as a base, I made an update – again simple yet probably very useful. Look at the “If-Modified-Since” header and respond with with a 304 or 200 depending upon if the entry in the zip file is updated or an old one. There’s one thing hardcoded in the my solution – the Cache-Control / Expires timeline is hardcoded to 365 days, and it can be easily moved into the configuration.

The solution is mentioned at http://code.google.com/p/googleappengine/issues/detail?id=161#c88. The SmartGWT showcase is available at http://1.latest.mastergaurav.appspot.com, along with the complete code of the servlet at http://1.latest.mastergaurav.appspot.com/UnzipperServlet.java.txt.