Saturday, October 11, 2014

Air Play using a raspberry pi

Instructions specific to OS X:

1. Download the image for Raspbian from : raspberrypi.org/downloads.

2. Install this image on the SD card using the app : app for macbook

3. Connect the raspberry pi (RPi) to your router with an ethernet cable and you should be able to ssh into it with the ip form the router's config page, which is 10.0.0.9 for me.

3. Enable wifi so that the RPi can use the WiFi Dongle. Also setup the RPi to receive DLNA content : Stephen Philips Blog

4. Connect the RPi to you speakers and install BubbleUPnP app on android to be able to play music form your phone including from mobile apps like Google Music.

5. Install a media server that streams media over the network. Now to be able to discover your RPi on the network and push DLNA content to it you would have to install another software. I Installed Plex and LINNOSS (Kinsky) to play music from my computer.

Enjoy wire-free music!


Wednesday, October 8, 2014

Vandermonde Matrix

I was recently trying to prove that a Vandermonde type of matrix is full rank and a quick google search did not give me the the proof I had in mind. So here it is,

Consider a matrix of the form,
\begin{equation}
\begin{bmatrix}
1& \alpha_1& \alpha_1^2& \ldots& \alpha_1^{r-1}\\
1& \alpha_2& \alpha_2^2& \ldots& \alpha_2^{r-1}\\
\vdots \\
1& \alpha_r& \alpha_r^2& \ldots& \alpha_r^{r-1}\\
\end{bmatrix} = [\mathbf{a_0} \ldots \mathbf{a_{r-1}}]
\end{equation}

If this matrix is not full rank then there exists coefficients $c_0, c_1, c_2, \ldots, c_{r-1}$ such that $\sum_i c_i \mathbf{a_i} = \mathbf{0}$. Therfore  there exist $r$ roots $\alpha_1 \ldots \alpha_r$  of the polynomial $\sum c_i x^i$ of degree less than $r$.

Saturday, August 23, 2014

Macbook, Sublime Text and IPython : the best combo for a coding project

I recently switched to a mac so I may be a a bit over-zealous, but my macbook is the first computer that I like to work on. It doesn't get stuck, has extensive command line features, is highly customizable, and has a great retina display.

In the past whenever I got a new computer the enthusiasm lasted not more than a week. It may be that I have discovered the right set of tools to work with, this time, but I never really had so much ease in coding in a language other than MATLAB and it's not that I haven't done projects in other programming languages. The combination of IPython's great debugging features, OS X's superb command line iterm and Sublime Text's easy text manipulation and great plugin's has made my life easier by a great deal.

So I'll try to make a quick reference for my future self who has a tendency to forget things quite a lot.

OS X tools 

Iterm :

  • You can divide the terminal into mutiple windows like terminator. Use Cmd+d and Cmd+Shift+d to divide the current tab.
  • You can re-assign keys to move quickly on a line  : link
I have re-assigned the left option key to Esc from Preferences > Profiles > Keys > Left Option Key Acts as. Now, in addition to the usual bash line navigation Ctrl+a, Ctrl+e, etc. , I can move one word left and right using Option+f and Option+b respectively. I can also delete word by word forward and backwards using Option+d and option+delete without trying to stretching my hand across the keyboard.

Caffeinate:
With windows, I was extremely irritated when the following happened. You are just about to head out but you suddenly remember you haven't checked your email. You open up your laptop to quickly find out your venue (because you are too lazy to remember everything), so that you can catch the bus arriving in two minutes outside your apartment. But Windows decides that there's a dangerous virus that you cannot be not-protected from and you have to wait for an hour for the software updates to finish before you can check your email.

Thankfully this doesn't happen with my macbook, because in addition to not being extremely irritating, it's battery lasts long enough  (atleast for now) for you to check your email and not have to look for a power socket, which is partly because OS X has a very aggressive power save mode. Which is fine until you are want to connect your laptop to your TV and watch a TV show without having to get up and move the mouse every few minutes.

caffeinate <command> is helpful in this scenario.

iBooks and the default OS X dictionary:
I like to have a dictionary around whenever I'm reading anything, which is too much effort for when you are reading a soft copy.


Menu Search:
Looking through the online documentation of a software to find out exactly where the option you want to set resides, is too much trouble especially considering the fact that it's easier for you to go through Petabytes of data on another computer (google's server farms) than to look at a mere 100s of bytes you have on your computer. That is why the help menu search keyboard shortcut Cmd+Shift+/ is one of my frequently used key strokes.



Sublime Text

There's nothing to document since the text editor's documentation is easy to find and it's easy to configure. Here are the plugins that I have installed in my Sublime Text 3, in decreasing frquency of usage (in addition to the in-built command palette features)-

  • Origami
  • Bracket Highlighter
  • SublimeCodeIntel
  • Text Pastry
  • WhoCalled
  • Advanced New File


IPython debugging 

pdb is a great debugging tool, but one flaw of debugging with these tools is that to set a breakpoint to get to the one case that isn't working you have to go step through the hunderds of cases that are  and it get too tedious. And that's why I preferred to use print statements to debug.

Now, enter the python modules logging. So here's what every piece of code I write now looks like -

try:
function_to_debug()
except Exception:
logging.exception("here")
_breakpoint()  # 0fa1ab74
The  logging module let's me know where exactly the exception was raised and the breakpoint let's me examine the variables values at that point. So I write my code without too much worrying about the extreme cases, and handle them as and when  they arrive.

People have also extended the logging module to print colored output. I'm too lazy to google for that now, but that would be a killer tool for any debugging task.

There's also the %pdb IPython magic feature which is essentially the same thing, except for the logging.exception("here") line above.

Note to self : keep adding rambling and features to this.







Git : Quick Reference

Get Started

To get started with git read the first two chapter in git documentation. I think after the first two chapters it would become useless to  keep reading, instead just keep learning as you go. Use git help <command> to learn more about a command or just google. If you still want to keep reading look at the reference URLs at the end.

To create a local git repository read git-basics. You can use any remote computer as a server very easily with git. Just create a git repository and then clone it from another computer using
git clone <username>@<IP>:<path/to/.git>
Git will use ssh to login and copy the files so no extra setup is needed. The instructions at  git-basics show how to create a git repository without a name, but if you want to give a name to you git repository i.e. instead of just a .git folder you want myrepo.git you can do :
mkdir myrepo.git 
cd myrepo.git 
git init --bare
Add remote repositories with
git remote add [shortname] [url]

Configurations

  • color your diffs
git config --global color.ui auto
  • more configuration options git-scm

Working with git

Working with Remotes

Use the following command to create and track remote branches (ref)
git checkout --track -b <local branch> <remote>/<tracked branch>
OR
git checkout -t <remote>/<tracked branch>
The second command keeps the same name as the remote tracked branch. To avoid the default merging with a remote branch with git pull  use git fetch 
git fetch <remote>OR
git fetch --all
to fetch from all remotes. You can then checkout a branch and fast forward using
git merge --ff-only <remote>/branch

Reverting to old commits

Look at the reset command.
git checkout <branch>~n checkout the commit n commits before the latest commit
git checkout <commit> <file1> <file2>...  checkout files form commit
Note: <commit> can be a branch name or the checksum of any commit.

Looking at the commit logs


Look at diff and status.
git diff <branch old/commit checksum>:[<file>] <branch new/commit checksum>:[<file>]
to see the difference in the files. To see commit log with only the names of the changed files do
git log --name-only
Useful : git log -p -n shows the changes introduced in the last n commits. Use gitk --all to see everything in a gui.


Branching

Show all branches
git branch -a
Create branch on the current state
git branch <name>

Git Stash

Use git stash to quickly save your current state without messing up your commit history
git stash save "<message>"
You can also do multiple layers of stashes. So list all the stashes you have done using

git stash list
stash@{0}: On shoulda: Updating instructions
stash@{1}: On master: started merge but need to fix #104 first
stash@{2}: On feature1: Adding some stuff

Pop the stash from your commit and get back to the state before "git stash save".
git stash pop/apply
A note with this command, it deletes that stash for good, while "git stash apply" does not. You can manually delete stashes with:
git stash drop <id>

and delete stashes with
git stash drop <id>
or delete all of the stored stashes with:
git stash clear
URLs :
a. http://gitready.com/beginner/2009/03/13/smartly-save-stashes.html
b. http://gitready.com/beginner/2009/01/10/stashing-your-changes.html

Renaming branches

Refer to here

Now to rename the master branch you have first change the default head to something else on the remote. You cannot do this from the client. First ssh onto your server and do 

git symbolic-ref HEAD refs/heads/new_master

Now, you can do as you want.

Frequently Used Commands 

  • git add    add files to git tracking
  • git commit   commit the files to the local git
  • git push    update the remote with the current branch
  • git pull   fetch and merge current branch from remote origin
  • git fetch    fetches named heads or tags from one or more other repositories, along with the objects necessary to complete them.
    • git fetch --all   fetch from all remotes
  • git merge  incorporates changes from the named commits (since the time their histories diverged from the current branch) into the current branch.
    • use "git merge --ff-only <remote>/branch" to fetch and merge from remote 
  • git branch <name>  creates a branch at the current checkout
  • git log  show commit logs for the current branch
  • git checkout <file OR branch OR branch:file> Checkout files and branches

Reference URLs

http://git-scm.com/doc
http://gitready.com

Tuesday, May 6, 2014

Simple explanation of cryptography

Suppose you want somebody to send you a number $x \in [1,n-1]$ such that any eavesdropper does not know what the message is. The simplest way to do that is have them send $f(x)$ to you, where the function $f(.)$  is injective.

But if the function is easily invertible then anybody who knows what $f(.)$ was used can easily eavesdrop and decode the secret message. So, you can either keep $f(.)$ secret, or you can make the inversion of $f$ really difficult except for you. The latter technique is the one modern crypto-systems use and the former one was used in ancient times starting with Caesar. The problem with Caesar's technique if that sender and the receiver have to agree on what $f(.)$ has to be used and that communication has to be secure, thus the problem remains.

The main idea in modern cryptography is that you should find a function that only you can invert. This is based upon the difficulty of factoring large primes. Suppose that the function $f_n(.)$ can be inverted easily only when the factorization of $n$ is known. Now given such a function $f_n(.)$ you can secretly choose large primes and compute a number $n$ as the product of those primes. Now, since you know the factorization of $n$ the inversion is easy for you, but not for anybody else.

Since the sender does not need to know the factorization of $n$ to encode, your communication becomes secure.

Saturday, April 26, 2014

Richard Hamming : Intro to art of doing science and engineering, Lecture 1

History has taught us much about the progress of science. Human knowledge has been growing at an exponential rate since Newton. There are about 10,000 different field of science now, about that much more than there were in the 1700's during Newton's time. Continuing in this fashion and assuming that the number of scientists grows with the amount of knowledge in 300 years there should be about a billion different fields of science and every man on earth would be a scientist. Obviously, this cannot happen. History is not a perfect predictor of the future. Consider how different history would have been if either of Napoleon, Einstein or Hitler had died young. There are too many little things that change too much.

Although the number of scientific fields may not grow exponentially, human knowledge would continue to grow. You would have to learn much more than your ancestors had to do to be able to contribute to anything. And therefore you have to learn how to learn. To adapt your style and not just depend upon history. Having a good style or approach towards learning or knowledge  is the best way to keep up. But you should keep in mind that there is no unique style that fits all. A good criterion is  to cling to fundamentals. But, you have to know how to define fundamentals which is difficult, because they change frequently.

Another important thing to know is, without a vision of where you are headed you are no better than a wandering drunk. The first ingredient of great work is having a goal and working towards it. It doesn't matter what that goal is as long as you believe in it and work hard towards it. Otherwise you would just keep drifting through life. To make your life's contribution add up to something a goal is very important.

To be able to contribute, you should keep an open mind. Knowledge is not fundamentally divided into tight departments. It is a homogenous thing. And knowing the relations between different fields can help you a lot.

You should also note that computing is the future of science, because of the following fundamental reasons -

  • speed - neurons function at about a 100m/s vs light which travels at 3e8m/s
  • precision 
  • reliability - we trip and stumble even after walking for decades
  • rapidity and control - we can only process only about 50bits per second
  • boredom
  • cost - humans are getting expensive and machines are getting cheaper

Finally, you have only one life. You ought to do more than just get by. You have to have a goal. Although, achieving a goal is not the best part, the struggle is. The struggle to achieve excellence is worth the struggle.

Your  life is not the sum total of the pleasant moments in it. You cannot just get up and say to yourself "I shall be happy today", and get on with your day. The way to make your life truly happy is to struggle to be the kind of person you wish to be, to struggle for the goals you want to achieve and to be more articulate than just idle drifting like a drunken sailor. As Socrates once said, "An unexamined life is not worth living".

Thursday, January 16, 2014

Primality Testing and importance of the twin prime conjecture

I have not studied the literature on primality testing or prime factorization but still I often wonder about the complexity of the algorithm that keeps the whole world secure.

Consider any interval of length $N$ on the real line. For $N=2$ we know that  we would not encounter more than one prime in any interval of length $N$. Now, intuitively I feel that this should be true for any number $N$, eventually i.e. there should exist a limit $L$  such that for all intervals $[n,n+N]$ for every $n>L$ there can exist only one prime in that range. Thus primes should become increasingly sparse (in this strong sense). Even though this result does not guarantee to reduce the complexity of finding prime numbers, it feels that it should make my job easier.

But alas, the twin prime conjecture lays fail to all of the above. Recently a professor proved a weaker version of the twin prime conjecture. He showed that for a given finite number $M$ (~ 7 million ) there are infinitely many prime pairs with difference less than $M$. Now people have improved upon his results and brought down the number $M$ to ~ 600, but it seems unlikely that the twin prime conjecture could be proven using his methods. Nevertheless, even this result re-inforces the difficulty of finding prime numbers.