Saturday, July 20, 2013

A chess post

To improve one's chess play, reading books and famous games is an established but boring way to improve your skill. So I have resorted to playing continuously instead  which is far more entertaining but also far slower. Since the last month I started playing I have played 500 (!!) 10 min games on chess.com .

Occasionally I look at famous games. My amateur chess expert has advised me to look at the games of Paul Morphy first. I started looking at the games but then I realized that although improving is only possible when you analyze games, it is far more rewarding and easier to analyze your own games. I played this interesting chess game  recently. I am going to analyze my own and my opponent's mistakes from the principles I have gathered. Please post improvements/suggestions in comments.

Also since its turning out to be very difficult to include a dynamic chess board on blogger, I am just including the link to the game here : game?id=561308873 .

I am playing white in the above game. The winner makes the last mistake here, as always.

Black's second move seems intimidating. The bishop on b4 pins my d4 pawn. I don't know what the best defense to this is, so I move my knight and hope to quietly slip my bishop in d2 as in move 5. (I don't want the bishop to take my knight and destroy my pawn structure).

Normal development follows till move 9. White's 9. o-o is weakening. Black could have played 9. .. Bxc3, and after that 10 .. Bxe2. To defend that white should have played 9. Nd5 forking the Queen and the bishop.

Black's Queen remains on the open e-file for too long for white to play tactics such as 11. Bxa6 . Black's move 16 is also restricting. Qf5 would be better since it maintains pressure on the d4 square while providing the Queen room to move. Similarly white 17. Qe1 should have been Qe2, since it backs both the white knight and bishop.

A better move for white instead of 18. d5 would be Bd5.

Now 24. Ne5 leaves black  with little choice. The only move to protect a mate would be Qf5 or Qg5. Both of which is countered by g4.

Sunday, July 7, 2013

Experiments with Neodymium magnets

I bought a set of neodymium to play with. When you are bored, without a computer, they come in handy.

The set consisted of 216 small 4-5mm balls of magnets. The magnets came in a 6x6x6 shaped cube. That shape was very unstable and stuck to the metallic box. On trying to break apart the cube the cube disintegrated into chains.

Trying to put back the magnets into a cube is not very easy. I can say that, because my friend, a physicist, also failed at the first attempt. Here's my approach and the difficulty I face along with the tricks involved.

My approach was to create a stack of 6 rings of the spheres each ring consisting of twelve balls.
Three stacks are required to make a cube. Here what the stacks looked like.
Now I flattened out all the stacks and formed sheets of 6x6x2,
Now I just stuck the stacks together and I had my cube.


The problem

The problem was to get a stack to align properly. Actually, there are two possible ways,

Proper (left) and improper (right) alignment
The one on the left is the desired one.

Proper (top) and improper (bottom) alignment

Also all the three stacks must align properly, although you don't have to combine those stacks. Because otherwise the 6x6x2 sheets would not stick together to form a cube. One of the sheets would slide over the other similar to the bottom chains in the above figure.

The trick
Suppose you are trying to join two stacks of rings and just would not align properly. The trick is to flip one stack and then try again. There's a catch here. If both of the stacks consist of even number of rings, flipping won't work. So adding rings one by one to a stack always works.

Suppose now you have formed three stacks of rings, all properly aligned. But all the three stacks do not align properly and so you cannot stick the respective sheets together. Here, atleast two stacks must be aligning properly and the third one would not align with any of the other two. So instead of  trying to re-create the third stack ad hoc, if all the rings of the third stack are inverted, it works. 

Here's the underlying diagrams.
Magnets aligning to form rings
The spheres colored regions inside the spheres represent the north and south poles of the magnet. So if two different configurations are superimposed they align properly, otherwise flipping one of them works. 
But suppose instead of a ring we have stacks of even number of rings ( both aligned properly ), flipping does not change the configurations of the stack and hence they will always align wrong. So inverting a ring, as follows, is required.
Inverting a ring
Inverting a ring (or stack of rings)

Here are some more configurations. Interestingly, the hexagon is more stable than the cube.





Monday, June 24, 2013

Reverting to google chrome's old "new tab" page

This happened to me after a chrome update. The following new tab page was enabled by default.

The problem was also that my omnibox was not in focus every time I opened a new tab. Fortunately I found a solution online. This feature can be disabled by going to "chrome://flags" and selecting the "disabled" option for Enable Instant extended API 




Monday, May 13, 2013

Speeding up MATLAB code using matlab distributed computing toolbox

MATLAB's distributed computing toolbox is a great tool to fully utilize your multi-core processors. If you have the distributed computing toolbox installed, the setup is quite easy. The steps to start parallel processing on your localhost are given below. To use a cluster of computers through the LAN, the setup is similar.

The steps given are for Windows 7, though the steps for linux are the same.
  1. First open a command prompt using "Run as Administrator"
  2. Go to %matlabroot% directory which is usually "C:\Program Files\MATLAB\R2011a"
  3. Now cd to "toolbox\distcomp\bin"
  4. Install the mdce service using "mdce install"
  5. run admincenter
  6. Add hosts (your computer is localhost)
  7. start mdce service
  8. start jobmanager
  9. start workers

Now open matlab go to "Parallel" menu. Manage Configurations and create a new job manager. 

In the properties, in the Jobs tab, job manager hostname should be "localhost". Job manager's name should be the name you used earlier. 

In the Jobs tab, input the minimum and maximum number of workers. The number of workers should be less than the number of processors in your computer. Click OK and you are done.

Now that the distributed computing toolbox is set up you can use the "matlabpool" command and the "parfor" loop to speed up code.

If the mdce service, the jobmanager and the workers keep running, the system gets slow. Therefore, to stop the workers, in the command prompt run :
%matlabroot%\toolbox\distcomp\bin\admincenter
Now stop the workers and the job manager. To stop the mdce service use the command :
mdce stop
Note that you must have the mdce service, the job manager and the workers running to be able to use matlabpool effectively.

Tuesday, April 30, 2013

Equivalent definitions of continuity of functions


This may seem trivial but, the following definitions of continuity of a function $f(x)$ on a metric space are equivalent -

  1. $\forall \epsilon > 0$, $\exists \delta > 0$ such that for every point $x$,  $d(x,y) < \delta \implies d(f(x), f(y)) < \epsilon $
  2. $x_n \rightarrow x  \implies f(x_n) \rightarrow f(x)$
I will just give the proof for $2) \implies 1)$, since the other way round is easier.

To find the $\delta$ for any $\epsilon > 0$ in $1)$, Consider the set $\mathcal{A}$ of all sequences $x_n \rightarrow x$ . Now, from the definition of continuity in $2)$ we know that for all sequences $f(x^i_n)  \rightarrow f(x)$ for $\{x^i_n\} \in \mathcal{A}$. Hence, $\exists N^i$ such that $\forall n>N^i$ , $d(f(x^i_n), f(x)) < \epsilon$. Let 

$ \delta = \arg_{x^i_N} \min_{x^i_n \in \mathcal{A}} d(x,x^i_N) $

Thus this value of $\delta$ can be used to find the neighbour-hood in $2)$.

Saturday, April 27, 2013

Windows equivalents for Linux


One of the major factors that prevented me from using ubuntu regularly was the absence of a software as versatile as IDM for speeding up content download from the internet. Recently I found a great equivalent to IDM in ubuntu : axel

Here's how to use the axel command :
axel -N -n <number of connection> "<url>"
where the -N option is for not using the proxy server.

To download videos in linux its easy to use vlc and record the stream. A great pdf editor for linux is described here

Saturday, March 23, 2013

Web Scraping using Python

Python is a powerful and versatile programming language. Its versatility is increased by the various modules developed for wide applications. It even has a MATLAB library module NumPy.

Many times I needed to download multiple files from a webpage, say wallpapers, and all the usual methods don't work. Here I list the "usual" methods :
  1. wget :  download webpage content from the linux terminal
  2. Download them all plugins in firefox and their counterparts in google chrome
Many times I tried to extract the jpg links  in a webpage using python regular expression (regexp) . Recently I needed to download a large database provided on a webpage which would not yield to any of the above methods. The webpage contained a javascript form to provide the download link  and it used some validation methods which rendered creating the download links using simple regexp useless.

Here is a powerful alternative to avoiding the drudgery of manually downloading files, using a python module :  MechanizeBelow is the code I developed to download a webpage.

First, this is the webpage that I wanted to download. Note that the download link is provided using a javascript form. Also note the onSubmit attribute in the form tag which is the validation mechanism. So if you simply create the download link using the data provided in the html page, the downloaded file would be corrupted.


The following code first sets the appropriate variable to their appropriate values and then uses mechanize to download the file.


Now,  to create the list of pages containing the download links, I used the BeautifulSoup and the re (regexp) module to parse HTML.

To save the data structure (lists) used to store the download links pickle is a useful library. It can also be made searchable as shown here.

Mechanize has some major disadvantages that I quickly realized. The biggest is that you cannot download webpages that are dynamically loaded i.e. downloaded in multiple tries using javascript i.e. When you first download a webpage you do not download the complete webpage but HTML+javascript code. The browser then executes that javascript code to download the rest of the webpage. Now mechanize does not automatically execute the javascript code to complete the download, so you may not get the content that you want using mechanize.

Another option is easy to use is windmill . Windmill is a powerful library but it is not properly documented and hence difficult to use. The limited documentation can be found here .

Another python module for web scraping is scrapy . This one I have yet to use.

A few good beginner tutorials for python :
  1. MIT OCW : Introduction to programming
  2. Python for beginners
Usually, its faster to just dive in, choose a project and complete it using online documentation and user forums. In this regard stackoverflow.com is a particularly good place to post your queries.