July 10, 2016

Useful shortcuts and commands

A documentation of my most used Visual Studio keyboard shortcut commands at work and git commands - I'm sure there are many more useful shortcuts/commands I could be using but here are the ones I use almost daily for now:

Visual Studio

Ctrl + G - Go to line number
Ctrl + . - Autocompletes a lot of stuff (this is our principal dev's favorite shortcut so that means something)
Ctrl + L - Cut line (delete and copy into clipboard)
Ctrl + K, Ctrl + C - Comment selected block of code
Ctrl + K, Ctrl + U - Uncomment selected block of code
Ctrl + R, Ctrl + R - Rename a variable
Ctrl + K - Toggle bookmark
Ctrl + K, Ctrl + W - Open bookmark window
Alt + (up arrow OR down arrow) - Moves current line of code up or down
Ctrl + Shift + B - Builds solution
Alt + B, U - Build current project
Ctrl + Shift + F - Find/replace in entire solution, project, or document
Ctrl + 0, E - Navigate to Team Explorer -> Branches
Ctrl + 0, G - Navigate to Team Explorer -> View changes in workspace
Ctrl + F12 - View implementation definition (just F12 will take you to interface definition which can be annoying)
Ctrl + [, S - Sync Solution Explorer with active document
Ctrl + ; - Search Solution Explorer for file name

Not including basic things like copy/paste, selection of entire word/line, move cursor to end/beginning of line because those are not VS-specific and most programmers already know about those

Git

git checkout, pull, add, commit, push - because duh
git stash - saves your workspace and moves it out of the way so you can checkout another branch
git stash pop - puts what you stashed back into your workspace
git push -u origin <branch> - publishes your local branch
git log --pretty=format:"%h, %an - %ar: %s" - logs the most recent commits in one line, where %h is the commit hash, %an is author name, %ar is author date (relative), %s is the commit message (subject)
git rebase -i HEAD~N - squash commits, where N is the number of commits you'd like to squash
git reset --hard origin/master - what i use when i screw up my branch and have to set it back to remote
git reset --soft HEAD^ - undoes last commit and puts changed files back into workspace.  I use this (in conjunction with previous command) to glean a comprehensive view of my entire changes before I submit for PR
git rebase master - rebase master onto local branch, after you have pulled from master
git diff --name-status branch_1 branch_2 - view diff of two branches, but only the file names that contain the diff
git diff branch_1 branch_2 - view details of diff
git push origin <branch name> -f - force pushes to local branch (which is necessary after having pushed your branch and then rebased, commonly happens during code review).  I only use force push on my own local branch.
git fetch origin, git reset --hard origin/master - set branch to exactly match remote

Interactive git cheat sheet - A neat little interactive diagram of how each stage of git interacts with each other

Random

Ctrl + Shift + T - (Chrome) Reopen closed tab
Ctrl + J - (Chrome) Open list of downloads
Ctrl + U - (Fiddler) Copy URL
R - (Fiddler) Reissue request
Ctrl + 2 - (Fiddler) Mark request to blue.  Can also use different numbers for other colors
Ctrl + Shift + L - (VsCode) Selects all words equivalent to the current one highlighted, and performs the same action on all.  Very useful for parsing stack traces.

June 01, 2016

Basins of Attraction for Newton's Method

Basins of Attraction - cool sounding term I've heard before but never really knew what it meant.  I think the pendulum-magnet example is the clearest way to intuitively understand it.

Pendulum - magnet


Picture three magnets at the same distance from each other, arranged in a triangle.  And there is a magnetic pendulum that swings above them.  It will eventually come to rest above one of the magnets. But it's the starting point from which the pendulum is let go that determines where the pendulum will eventually end up.


Above image is an awesome visualization from this awesome article that shows the basin of attraction for this magnetic pendulum - basically, it shows how the initial starting point affects the end result of the simulation.  Any point in the image gives you two pieces of information: the starting point of the pendulum, and which magnet it ended up on.  Some other simulations go on to alter the initial parameters - increasing/decreasing the damping factor, strength of the magnets, number of magnets all go on to produce different graphs of differing complexity.

Newton's Method

I am not awesome enough to make a simulation of the magnetic pendulum but graphing the basin of attraction for newton's method is a bit simpler.

Newton's method is a numerical method for finding an approximation to the root of a function for things not as simple as (x - 5)(x - 2) = 0.  What I mean by numerical method is that you take a starting guess, run it through a function to get a better guess, and keep applying the same function to each next guess until the answer is good enough.

And by doing this on the complex plane, you can get some cool graphs.  I decided to try this out for z^5 = 1 on shadertoy.  By taking each pixel and assigning it a real value based on its x coordinate and an imaginary value based on its y coordinate and applying Newton's Method to it, the pixel will eventually land on a root and be assigned a color based on the root it ended up on.  Similarly to the magnetic pendulum graph, the starting point of the pixel determines what its color will end up being.

Basin of Attraction for z^5 = 1 using Newton's Method
(link to shadertoy) You can get this graph by taking an initial z value, applying z -> z - (z^5 - 1)/(5*z^4) a bunch of times, and assigning it a color based on its last calculation. (I think the black circles mean that I needed more iterations for the value to converge, I probably need better software if I wanna do that)

Basins of attractions have other real world examples - in ecology, when a population reaches a stable equilibrium, that equilibrium is thought of as an attractor for the ecosystem.  A ball rolling on a bunch of little hills (remember those wire Fischer-Price toys at the dentists office?) will have several equilibrium points and therefore resulting basins of attraction.

March 15, 2016

Lorenz Attractor

It's unfortunate that I haven't been able to keep up with graphics much lately.  Before I left for China I'd been dabbling a little bit with WebGL, as I find it easier to set up and less error prone than OpenGL on Visual Studio.

I was reading the Black Swan (not the ballerina movie) and I got pretty happy when I saw the author start talking about fractals.  He mentioned the Lorenz system as well so I looked it up.

The Lorenz system, when plotted, looks like a butterfly or a figure eight.  I'm not sure but this might be where the "butterfly effect" comes from, because it looks like a butterfly and it basically illustrates how, if you slightly change the input parameters to the system of differential equations that make up the Lorenz system, you get a very different plot.  Other websites do a much better job of explaining than I do, I just wanted to show off my picture:


Without all the shader and WebGL code, here is the main logic to plotting this system:

  var sigma =  10.0;
  var rho = 28.0;
  var beta = 8.0/3.0;
  var x = 1.0, y = 1.0, z = 1.0;

  var colors = [];
  var verts = [];

  var I = 100000;
  for (var i = 0; i < I; i++) {
    var xnew = sigma*(y - x);
    var ynew = x*(rho - z) - y;
    var znew = x*y - beta*z;
   
    x = x + xnew*0.001;
    y = y + ynew*0.001;
    z = z + znew*0.001;
   
    var r = 0.5 - ((i/I) * 0.3);
    var g = 0.0;
    var b = r - 0.05;
    colors.push(r, g, b);
    var scale = 0.25;
    verts.push(x*scale, y*scale, z*scale);
  }

The parameters sigma, rho, and beta are the things you can change to make the plot different: values of sigma = 10, rho = 28, beta = 8/3 give the prettiest picture.

In the live version the system turns different colors of reds and purples and fades ever so slightly only to burn bright again.  Next I want to have the camera rotate around the system which I completely forget how to do, time to pull up old rendering notes from three years ago.