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 :  Mechanize. Below 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.

Saturday, February 16, 2013

Points on a semi-circle


Suppose you choose $N$ points uniformly randomly on a circle. What is the probability that all the points lie on a semi-circle.

Random walk on a circle



Let $x_1, x_2, ..., x_n$ be n points (in that order) on the circumference of a circle. Dana starts at the point $x_1$ and walks to one of the two neighboring points with probability $1/2$ for each. Dana continues to walk in this way, always moving from the present point to one of the two neighboring points with probability 1/2 for each.  $P_i$ is the probability that the point $x_i$ is the last of the n points to be visited for the first time.


1. Show that $P_i$ is the same for all i.


2. Find  $P_i$