Monday, May 29, 2017

Mass MOCA and combinatorics




Consider the installation on the right in the picture above. If we label each color with a number, then each square represents a permutation. Each square is now grouped into a group of 4 squares, as shown below. Each group has an interesting property. If you superimpose any two of the squares in the group, none of the colors will collide.

 

Now if we represent each permutation as a vertex of a graph such that two vertices are adjacent iff they collide. Then the largest independent set in the graph is of size 4.  There are 4! = 24 such possible independent sets (order dependent). And all 24 groups are shown in the art installation.

Friday, June 26, 2015

Random Conversations

Watched this speech by MP Hukumdev Narayan.



It reminded me of this conversation between me and my friend K. K is an economist studying for his PhD.

We were at a bar near the university.

Me : Why do you think Americans spend so much money on alcohol

K :  Americans spend money on travel, alcohol and girls. Indians on the other hand like to save up.

Me : But..  that saved money has to be spent eventually...

K : You have to save for emergencies  in India.

Me : hmm... but I don't think, medical or other emergencies are that big of an expenditure most of the time. Where does the money go ?

K : Food ? ... Nope.

Me : Weddings!!

K (laughs) : Yeah. Fat Indian Weddings.

Me : Yes. There should be a law on expenses in weddings.

K : There should be a law imposing restrictions on expenses in general.

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.