<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"><channel><atom:link rel="hub" href="http://tumblr.superfeedr.com/" xmlns:atom="http://www.w3.org/2005/Atom"/><description>Hi. I enjoy writing code and surfing the internet. Most of this blog will be personal code snippets that I find interesting.

And I refuse to post any PHP code on this site.</description><title>I'm James and I write code. Sometimes I blog.</title><generator>Tumblr (3.0; @jcchurch)</generator><link>http://jcchurch.tumblr.com/</link><item><title>Grabbing all of the links to a Youtube Playlist in Python</title><description>&lt;p&gt;My friend Jesse wanted a way to grab all of the links to videos on a YouTube playlist. That&amp;#8217;s no problem. I can easily cycle through links and spit them out into an HTML page.&lt;/p&gt;

&lt;p&gt;There&amp;#8217;s just one bug that I can&amp;#8217;t seem to fix. Each and every link in the output goes to &lt;a href="http://www.youtube.com/watch?v=dQw4w9WgXcQ"&gt;Rick Astley - Never Gonna Give You Up&lt;/a&gt;. I guess some bugs were never meant to be solved.&lt;/p&gt;

&lt;script src="https://gist.github.com/4684667.js"&gt;&lt;/script&gt;</description><link>http://jcchurch.tumblr.com/post/41954032899</link><guid>http://jcchurch.tumblr.com/post/41954032899</guid><pubDate>Thu, 31 Jan 2013 11:41:55 -0600</pubDate><category>youtube</category><category>python</category></item><item><title>Mandelbrot Set in Matlab</title><description>&lt;p&gt;I wanted to see if I could create a Mandelbrot Fractal image in Matlab. After quickly looking at the &lt;a href="http://en.wikipedia.org/wiki/Mandelbrot_set"&gt;Mandelbrot Set&lt;/a&gt; entry on Wikipedia, I coded up the following and ran it:&lt;/p&gt;

&lt;script src="https://gist.github.com/4636499.js"&gt;&lt;/script&gt;&lt;p&gt;I&amp;#8217;m kinda surprised that it took so little code to create the image. I&amp;#8217;m pleased with the results.&lt;/p&gt;

&lt;p&gt;&lt;img src="http://media.tumblr.com/94d0b4a864cf54a7ef262e389174587a/tumblr_inline_mh70unWoJC1qaga17.png" alt=""/&gt;&lt;/p&gt;</description><link>http://jcchurch.tumblr.com/post/41451150158</link><guid>http://jcchurch.tumblr.com/post/41451150158</guid><pubDate>Fri, 25 Jan 2013 12:06:00 -0600</pubDate><category>matlab</category><category>mandelbrot</category></item><item><title>Finding a 3D Rotation Matrix for two vectors in Matlab</title><description>&lt;p&gt;Update: The formulation of the input has been updated to allow two matrices of 3 by n column vectors and return a 3 by 3 by n matrix of rotation matrices.&lt;/p&gt;

&lt;p&gt;Here&amp;#8217;s the problem:&lt;/p&gt;

&lt;p&gt;I have a 3 by 1 vector and I need to know the rotation matrix to manipulate that vector to another 3 by 1 vector.&lt;/p&gt;

&lt;p&gt;Let&amp;#8217;s call the first vector &amp;#8216;u&amp;#8217; and the second vector &amp;#8216;v&amp;#8217;. I&amp;#8217;m looking for a 3 by 3 rotation matrix &amp;#8216;R&amp;#8217; that satisfies this simple property:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;R*u=v&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;That&amp;#8217;s it!&lt;/p&gt;

&lt;p&gt;I&amp;#8217;m sure that you&amp;#8217;ve encourtered solving systems of linear equations in an algebra class. They are pretty painful, right? In those cases, you are given &amp;#8216;R&amp;#8217; and &amp;#8216;v&amp;#8217; and you are told to solve for vector &amp;#8216;u&amp;#8217;. By comparison, those are easy. Here we go with the solution:&lt;/p&gt;

&lt;script src="https://gist.github.com/3782637.js"&gt; &lt;/script&gt;</description><link>http://jcchurch.tumblr.com/post/32265687290</link><guid>http://jcchurch.tumblr.com/post/32265687290</guid><pubDate>Tue, 25 Sep 2012 09:46:00 -0600</pubDate><category>matlab</category></item><item><title> 	 Solving Many, Many 3 by 3 Eigenvalue Problems in Matlab</title><description>&lt;p&gt;My research is requiring me to solve many, many small eigenvalue problems, all of which are 3 by 3 matrices. Rather than use the built-in &amp;#8220;eig&amp;#8221; function, I wrote my own.&lt;/p&gt;

&lt;script src="https://gist.github.com/3419717.js"&gt; &lt;/script&gt;</description><link>http://jcchurch.tumblr.com/post/29921193842</link><guid>http://jcchurch.tumblr.com/post/29921193842</guid><pubDate>Tue, 21 Aug 2012 15:54:00 -0600</pubDate><category>matlab</category></item><item><title>Determining the Circumcircles of a list of Triangles in Matlab.</title><description>&lt;p&gt;Imagine that we have a set of two dimensional points scattered across a space. For each point, the X coordinates are in variable X, and the Y coordinates are in variable Y. We can passed these coordiantes into the &amp;#8220;delaunay&amp;#8221; function and get &amp;#8220;TRI&amp;#8221;, which is a list of triangles. We can view the triangle mesh of the Delaunay triangulation by passing it to the MATLAB function &amp;#8220;trimesh&amp;#8221;, but there is no function for viewing the circumcirlces produced by this function. That&amp;#8217;s what I&amp;#8217;ve created today.&lt;/p&gt;

&lt;script src="https://gist.github.com/3025455.js"&gt; &lt;/script&gt;&lt;p&gt;This second block isn&amp;#8217;t my code. I found it on the Mathworks website. I&amp;#8217;m posting it here to demonstrate that the above code works. It&amp;#8217;s just used for plotting the circles.&lt;/p&gt;

&lt;script src="https://gist.github.com/3025482.js"&gt; &lt;/script&gt;&lt;p&gt;Let&amp;#8217;s make some triangle meshes!&lt;/p&gt;

&lt;script src="https://gist.github.com/3025492.js"&gt; &lt;/script&gt;&lt;p&gt;This produces the elegant picture below.&lt;/p&gt;

&lt;p&gt;&lt;img src="http://media.tumblr.com/tumblr_m6g7q11Bda1qaga17.png" alt=""/&gt;&lt;/p&gt;</description><link>http://jcchurch.tumblr.com/post/26231011215</link><guid>http://jcchurch.tumblr.com/post/26231011215</guid><pubDate>Sat, 30 Jun 2012 15:04:00 -0600</pubDate><category>matlab</category></item><item><title>Distance from point to the nearest point along a line segment in Matlab</title><description>&lt;p&gt;&lt;script src="https://gist.github.com/1961989.js"&gt; &lt;/script&gt;&lt;/p&gt;</description><link>http://jcchurch.tumblr.com/post/18625635659</link><guid>http://jcchurch.tumblr.com/post/18625635659</guid><pubDate>Fri, 02 Mar 2012 16:44:00 -0600</pubDate><category>matlab</category></item><item><title>Examining Exit Codes of 4 simple programs with bugs: Python, Ruby, Perl, PHP</title><description>&lt;p&gt;Strict type checking is an aspect of the compiler to make sure that your programs have properly declared all of the necessary variable names prior to their use. I believe it is essential to all modern languages to implement proper errors in the event that an undefined variable is used. Let&amp;#8217;s look at 4 popular scripting languages to see how they handle this problem.&lt;/p&gt;

&lt;p&gt;For each program, we will examine three things: the source code, the execution, and the returned exit code. In modern operating systems, a program that concludes execution successfully returns a code of 0. If any problems arise in the course of the program, it should return something other than 0. I used the UNIX command &amp;#8220;echo $?&amp;#8221; to check the exit code.&lt;/p&gt;

&lt;p&gt;All 4 programs will print the word &amp;#8220;Hi&amp;#8221; to the screen, then it will try to print an undefined variable &amp;#8220;thisDoesNotExist&amp;#8221;. After that, it will print &amp;#8220;Hi again&amp;#8221;. We would hope that the program will abort some time during the execution of the program and tell us that &amp;#8220;thisDoesNotExist&amp;#8221; is undefined. We would also hope that the program would never return an exit code of 0.&lt;/p&gt;

&lt;h2&gt;First, my favorite little language: Python&lt;/h2&gt;

&lt;h3&gt;badName.py:&lt;/h3&gt;

&lt;pre&gt;&lt;code&gt;print "Hi"
print thisDoesNotExist
print "Hi again"
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Execution and return code:&lt;/h3&gt;

&lt;pre&gt;&lt;code&gt;jcchurch@mccarthy:~/tmp/exitCodes$ python badName.py
Hi
Traceback (most recent call last):
  File "badName.py", line 2, in &amp;lt;module&amp;gt;
    print thisDoesNotExist
NameError: name 'thisDoesNotExist' is not defined
jcchurch@mccarthy:~/tmp/exitCodes$ echo $?
1
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Python gives me a good error message &amp;#8220;NameError: name &amp;#8216;thisDoesNotExist&amp;#8217; is not defined&amp;#8221;. It also returns a status code of 1. This is what we should expect from a modern language.&lt;/p&gt;

&lt;h2&gt;Second: Ruby&lt;/h2&gt;

&lt;h3&gt;badName.rb:&lt;/h3&gt;

&lt;pre&gt;&lt;code&gt;puts "Hi"
puts thisDoesNotExist
puts "Hi again"
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Execution and return code:&lt;/h3&gt;

&lt;pre&gt;&lt;code&gt;jcchurch@mccarthy:~/tmp/exitCodes$ ruby badName.rb
Hi
badName.rb:2: undefined local variable or method `thisDoesNotExist' for main:Object (NameError)
jcchurch@mccarthy:~/tmp/exitCodes$ echo $?
1
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Ruby also gives a good error message &amp;#8220;undefined local variable or method `thisDoesNotExist&amp;#8217;&amp;#8221; and an exit code of 1.&lt;/p&gt;

&lt;h2&gt;Third: Perl&lt;/h2&gt;

&lt;p&gt;Not every language designer agrees with me that strict type checking is essential. Perl requires us to use the statement &amp;#8220;use strict;&amp;#8221; to use strict type checking. I wish this were turned on by default.&lt;/p&gt;

&lt;h3&gt;badName.pl:&lt;/h3&gt;

&lt;pre&gt;&lt;code&gt;use strict;

print "Hi\n";
print $thisDoesNotExist;
print "Hi again\n";
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Execution and return code:&lt;/h3&gt;

&lt;pre&gt;&lt;code&gt;jcchurch@mccarthy:~/tmp/exitCodes$ perl badName.pl
Global symbol "$thisDoesNotExist" requires explicit package name at badName.pl line 4.
Execution of badName.pl aborted due to compilation errors.
jcchurch@mccarthy:~/tmp/exitCodes$ echo $?
255
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Perl doesn&amp;#8217;t even print &amp;#8220;Hi&amp;#8221;. It gives the error message &amp;#8220;Global symbol &amp;#8220;$thisDoesNotExist&amp;#8221; requires explicit package name&amp;#8221;, which is a tad cryptic, but it does the job we want it to do. It also returns with a exit code of 255.&lt;/p&gt;

&lt;h2&gt;Fourth: PHP&lt;/h2&gt;

&lt;p&gt;I&amp;#8217;m breaking my rule of never posting PHP code on this site. Like Perl, strict type checking is turned off by default. We can turn it on with the statement &amp;#8220;error_reporting(E_ALL);&amp;#8221;.&lt;/p&gt;

&lt;h3&gt;badName.php:&lt;/h3&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;?
error_reporting(E_ALL);
print "Hi\n";
print $thisDoesNotExist;
print "Hi again\n";
?&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Execution and return code:&lt;/h3&gt;

&lt;pre&gt;&lt;code&gt;jcchurch@mccarthy:~/tmp/exitCodes$ php badName.php 
Hi

Notice: Undefined variable: thisDoesNotExist in /home/jcchurch/tmp/exitCodes/badName.php on line 4
Hi again
jcchurch@mccarthy:~/tmp/exitCodes$ echo $?
0
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;PHP gives us a good error message: &amp;#8220;Undefined variable: thisDoesNotExist&amp;#8221;. But wait! It keeps going! We see &amp;#8220;Hi again&amp;#8221; AFTER the error message, which means this isn&amp;#8217;t really an error message but a warning. When the program concludes, the exit code returned to the operating system is 0, which tells me that nothing went wrong in the program.&lt;/p&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;Python and Ruby demonstrate the same behavior: Execute as much of the program as possible until an error is reached. Once the error is reached, print an error and abort the program.&lt;/p&gt;

&lt;p&gt;Perl compiles the entire program and notices the undefined variable at compile time. It prints the error without executing any of the code.&lt;/p&gt;

&lt;p&gt;PHP executes as much of the code as possible. When it encounters the undefined variable, it happily prints a warning message and keeps on going. Finally, it tells us that there were no problems in the code with an exit code of 0.&lt;/p&gt;

&lt;p&gt;Oh PHP. Maybe you will grow up as a language one of these days.&lt;/p&gt;</description><link>http://jcchurch.tumblr.com/post/14266516236</link><guid>http://jcchurch.tumblr.com/post/14266516236</guid><pubDate>Thu, 15 Dec 2011 11:09:00 -0600</pubDate><category>python</category><category>ruby</category><category>perl</category><category>php</category></item><item><title>Top 10 List for Categorical Data in MySQL</title><description>&lt;p&gt;Let&amp;#8217;s say that you have a table with some categorical data. You want to make a Top 10 list of the most frequently visited categories. Here is an example table:&lt;/p&gt;

&lt;p&gt;In this case, we have a table called &amp;#8220;example&amp;#8221; with one column called &amp;#8220;label&amp;#8221;. This is a bad example, in that it only has 10 items in the whole table.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;br/&gt;
mysql&amp;gt; SELECT * FROM example;&lt;br/&gt;
+-------+&lt;br/&gt;
| label |&lt;br/&gt;
+-------+&lt;br/&gt;
| A     |&lt;br/&gt;
| B     |&lt;br/&gt;
| C     |&lt;br/&gt;
| D     |&lt;br/&gt;
| A     |&lt;br/&gt;
| B     |&lt;br/&gt;
| C     |&lt;br/&gt;
| A     |&lt;br/&gt;
| B     |&lt;br/&gt;
| A     |&lt;br/&gt;
| E     |&lt;br/&gt;
| E     |&lt;br/&gt;
| E     |&lt;br/&gt;
| E     |&lt;br/&gt;
| E     |&lt;br/&gt;
+-------+&lt;br/&gt;
15 rows in set (0.00 sec)&lt;br/&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We wish to create a Top 10 List (err&amp;#8230; Top 5 List) of the most frequently seen categories (or labels) in our table. Here is the code:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;br/&gt;
mysql&amp;gt; SELECT label, n FROM (SELECT label, count(*) AS n FROM example GROUP BY label) AS T1 ORDER BY T1.n DESC LIMIT 10;&lt;br/&gt;
+-------+---+&lt;br/&gt;
| label | n |&lt;br/&gt;
+-------+---+&lt;br/&gt;
| E     | 5 |&lt;br/&gt;
| A     | 4 |&lt;br/&gt;
| B     | 3 |&lt;br/&gt;
| C     | 2 |&lt;br/&gt;
| D     | 1 |&lt;br/&gt;
+-------+---+&lt;br/&gt;
5 rows in set (0.00 sec)&lt;br/&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;There are two SELECT statements here! Let&amp;#8217;s look at the inner SELECT statement. Here it is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;br/&gt;
mysql&amp;gt; SELECT label, count(*) AS n FROM example GROUP BY label;&lt;br/&gt;
+-------+---+&lt;br/&gt;
| label | n |&lt;br/&gt;
+-------+---+&lt;br/&gt;
| A     | 4 |&lt;br/&gt;
| B     | 3 |&lt;br/&gt;
| C     | 2 |&lt;br/&gt;
| D     | 1 |&lt;br/&gt;
| E     | 5 |&lt;br/&gt;
+-------+---+&lt;br/&gt;
5 rows in set (0.00 sec)&lt;br/&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This bit of code groups labels together, pulls the label column and the count of each label (and names this count as &amp;#8216;n&amp;#8217;). It almost does the job, except the data isn&amp;#8217;t sorted. You might wonder why we don&amp;#8217;t use &amp;#8220;ORDER BY n DESC&amp;#8221; here. At this point, we&amp;#8217;ve called a new column &amp;#8216;n&amp;#8217;, but we cannot use that new column in filtering or ordering.&lt;/p&gt;

&lt;p&gt;Let&amp;#8217;s move to the outer SELECT statement. I&amp;#8217;ve replace the inner SELECT statement with &amp;#8220;&amp;#8230;&amp;#8221;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;br/&gt;
mysql&amp;gt; SELECT label, n FROM (...) AS T1 ORDER BY T1.n DESC LIMIT 10;&lt;br/&gt;
+-------+---+&lt;br/&gt;
| label | n |&lt;br/&gt;
+-------+---+&lt;br/&gt;
| E     | 5 |&lt;br/&gt;
| A     | 4 |&lt;br/&gt;
| B     | 3 |&lt;br/&gt;
| C     | 2 |&lt;br/&gt;
| D     | 1 |&lt;br/&gt;
+-------+---+&lt;br/&gt;
5 rows in set (0.01 sec)&lt;br/&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We are using the inner SELECT statement as a brand new temporary table for the outer SELECT statement. You can see the &amp;#8220;AS T1&amp;#8221;, which means that we are naming our inner SELECT statement &amp;#8220;T1&amp;#8221;. We order by T1&amp;#8217;s column &amp;#8216;n&amp;#8217; in descending order, limit the output to 10 records and again pull the columns &amp;#8216;label&amp;#8217; and &amp;#8216;n&amp;#8217;.&lt;/p&gt;

&lt;p&gt;In summary, creating a sorted &amp;#8216;Top 10 List&amp;#8217; in MySQL requires a nested SELECT statement.&lt;/p&gt;</description><link>http://jcchurch.tumblr.com/post/13867765033</link><guid>http://jcchurch.tumblr.com/post/13867765033</guid><pubDate>Wed, 07 Dec 2011 03:39:00 -0600</pubDate><category>mysql</category></item><item><title>Linear Regression in Matlab</title><description>&lt;p&gt;&lt;script src="https://gist.github.com/1284580.js"&gt; &lt;/script&gt;&lt;/p&gt;</description><link>http://jcchurch.tumblr.com/post/11396973721</link><guid>http://jcchurch.tumblr.com/post/11396973721</guid><pubDate>Thu, 13 Oct 2011 09:49:14 -0600</pubDate><category>matlab</category></item><item><title>Determining if a graph is connected in Matlab</title><description>&lt;p&gt;I needed a quick script to tell me if a graph was connected or not. My adviser usually tells me &amp;#8220;MATLAB has everything!&amp;#8221; No, it doesn&amp;#8217;t, and that&amp;#8217;s why we need to write simple code like this just to get more complicated jobs done.&lt;/p&gt;

&lt;p&gt;&lt;script src="https://gist.github.com/1029396.js"&gt; &lt;/script&gt;&lt;/p&gt;

&lt;p&gt;Since I haven&amp;#8217;t posted in a while, let&amp;#8217;s do another script. This one will determine if a graph is symmetrical, which is another simple problem that MATLAB doesn&amp;#8217;t have the code to do. As you can see, the code is very short.&lt;/p&gt;

&lt;p&gt;&lt;script src="https://gist.github.com/1029414.js"&gt; &lt;/script&gt;&lt;/p&gt;</description><link>http://jcchurch.tumblr.com/post/6588027722</link><guid>http://jcchurch.tumblr.com/post/6588027722</guid><pubDate>Thu, 16 Jun 2011 08:56:00 -0600</pubDate><category>matlab</category></item><item><title>Rotating Points around 3 axes in Matlab</title><description>&lt;p&gt;I wrote this based on the content found on the Wikipedia page for &lt;a href="http://en.wikipedia.org/wiki/Rotation_matrix"&gt;Rotation Matrix&lt;/a&gt; and it worked on the first try. I fell like I need a victory lap.&lt;/p&gt;

&lt;p&gt;&lt;script src="https://gist.github.com/955898.js"&gt; &lt;/script&gt;&lt;/p&gt;</description><link>http://jcchurch.tumblr.com/post/5200162448</link><guid>http://jcchurch.tumblr.com/post/5200162448</guid><pubDate>Wed, 04 May 2011 16:25:02 -0600</pubDate><category>matlab</category></item><item><title>Simplified Plotting (At least for me) in Matlab</title><description>&lt;p&gt;Graph plotting in Matlab is rich with features that allow the user to create fantastic, beautiful plots. The downside to all of this is that when the user wants to create something simple, they must first return to the Matlab manuals.&lt;/p&gt;

&lt;p&gt;I don&amp;#8217;t care for any of this. I want to create simple 2D and 3D plots and I don&amp;#8217;t really care about the current shape of the vector used in my plotting. I want a function which can accept multiple vectors filled with data and it figures out how the data should be plotted.&lt;/p&gt;

&lt;p&gt;If I want a plot for a research paper, I&amp;#8217;ll return to the manual to figure out something. For now, just throw the data on the screen as quick as Matlab can.&lt;/p&gt;

&lt;p&gt;This function allows you to string together multiple vectors containing either 2D or 3D (or a combination of the two), with vectors in row-order or column-order (again, I don&amp;#8217;t care), and throws it on the screen.&lt;/p&gt;

&lt;p&gt;This supports up to 5 colors. More can be plotted, but once you&amp;#8217;ve used your five colors, it loops back to the beginning.&lt;/p&gt;

&lt;p&gt;&lt;script src="https://gist.github.com/930267.js"&gt; &lt;/script&gt;&lt;/p&gt;</description><link>http://jcchurch.tumblr.com/post/4449480214</link><guid>http://jcchurch.tumblr.com/post/4449480214</guid><pubDate>Fri, 08 Apr 2011 15:59:00 -0600</pubDate><category>matlab</category></item><item><title>Solving the Monty Hall Problem with a Monte Carlo approach in Java</title><description>&lt;p&gt;&lt;script src="https://gist.github.com/930274.js"&gt; &lt;/script&gt;&lt;/p&gt;</description><link>http://jcchurch.tumblr.com/post/4372715285</link><guid>http://jcchurch.tumblr.com/post/4372715285</guid><pubDate>Tue, 05 Apr 2011 14:48:00 -0600</pubDate><category>java</category></item><item><title>mpipypi.py - Computing Pi using MPI Python bindings</title><description>&lt;p&gt;&lt;script src="https://gist.github.com/930276.js"&gt; &lt;/script&gt;&lt;/p&gt;</description><link>http://jcchurch.tumblr.com/post/4194300133</link><guid>http://jcchurch.tumblr.com/post/4194300133</guid><pubDate>Tue, 29 Mar 2011 17:00:00 -0600</pubDate><category>python</category><category>mpi</category></item><item><title>Dijkstra's Algorithm in Matlab</title><description>&lt;p&gt;&lt;script src="https://gist.github.com/930280.js"&gt; &lt;/script&gt;&lt;/p&gt;</description><link>http://jcchurch.tumblr.com/post/3645300461</link><guid>http://jcchurch.tumblr.com/post/3645300461</guid><pubDate>Fri, 04 Mar 2011 17:19:00 -0600</pubDate><category>matlab</category></item><item><title>Splitting Pages of a PDF in Python</title><description>&lt;p&gt;For the past month, I&amp;#8217;ve programmed in nothing but Matlab. Kinda sad, because I don&amp;#8217;t even care for Matlab. I prefer Python.&lt;/p&gt;
&lt;p&gt;I was going through a store called Tuesday Morning to look at the stuff for sale. It&amp;#8217;s a bit of a junk store. They had a DVD for sale containing every Fantastic Four comic book on PDF from 1961 to 2004 for only $15. Being a comic fan, I had to get it.&lt;/p&gt;
&lt;p&gt;Each issue is scanned into PDF format. Each 2 pages of the paper issue is combined into 1 page of the PDF. I didn&amp;#8217;t like this, so I decided to write a Python script to cut each page of the PDF down the middle into two pages and stitch the document back together. This means that 1 page of a comic book equals 1 page of PDF.&lt;/p&gt;
&lt;p&gt;I use is the handy &lt;a href="http://pybrary.net/pyPdf/"&gt;pyPdf&lt;/a&gt; library. It&amp;#8217;s doing all of the magic in this script. To execute this script:&lt;/p&gt;
&lt;p&gt;$ python splitPages.py InputDocument.pdf OutputDocument.pdf&lt;/p&gt;
&lt;p&gt;(Since the comics are intellectual property owned by Marvel, I&amp;#8217;m going to not post screen shots.)&lt;/p&gt;
&lt;pre&gt;from pyPdf import PdfFileWriter, PdfFileReader&lt;br/&gt;import sys&lt;br/&gt;&lt;br/&gt;print "Reading", sys.argv[1]&lt;br/&gt;&lt;br/&gt;output = PdfFileWriter()&lt;br/&gt;&lt;br/&gt;left = PdfFileReader(file(sys.argv[1], "rb"))&lt;br/&gt;right = PdfFileReader(file(sys.argv[1], "rb"))&lt;br/&gt;&lt;br/&gt;left.decrypt('')&lt;br/&gt;right.decrypt('')&lt;br/&gt;&lt;br/&gt;pages = left.getNumPages()&lt;br/&gt;&lt;br/&gt;for i in range(0, pages):&lt;br/&gt;        # Grab the left page&lt;br/&gt;        p = left.getPage(i)&lt;br/&gt;        p.mediaBox.upperRight = (&lt;br/&gt;            p.mediaBox.getUpperRight_x() / 2,&lt;br/&gt;            p.mediaBox.getUpperRight_y()&lt;br/&gt;        )&lt;br/&gt;        output.addPage(p)&lt;br/&gt;&lt;br/&gt;        # Grab the right page&lt;br/&gt;        p = right.getPage(i)&lt;br/&gt;        p.mediaBox.upperLeft = (&lt;br/&gt;            p.mediaBox.getUpperRight_x() / 2,&lt;br/&gt;            p.mediaBox.getUpperRight_y()&lt;br/&gt;        )&lt;br/&gt;        output.addPage(p)&lt;br/&gt;&lt;br/&gt;print "Writing", sys.argv[2]&lt;br/&gt;&lt;br/&gt;outputStream = file(sys.argv[2], "wb")&lt;br/&gt;output.write(outputStream)&lt;br/&gt;outputStream.close()&lt;br/&gt;&lt;/pre&gt;</description><link>http://jcchurch.tumblr.com/post/3419020291</link><guid>http://jcchurch.tumblr.com/post/3419020291</guid><pubDate>Sun, 20 Feb 2011 23:44:00 -0600</pubDate><category>python</category></item><item><title>My O'Reilly Wish List</title><description>&lt;p&gt;&lt;a href="http://www.oreillynet.com/oreilly/new-year-2011-enter.csp"&gt;O&amp;#8217;Reilly is currently holding a $500 giveaway.&lt;/a&gt; My entry form is a blog about the books, ebooks, and videos that I would purchase with $500. Beside every title is the dollar value of the product rounded up to the nearest dollar. After rounding, my wishlist totals $499. I think I hit the mark pretty close.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Paperback Books:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The Definitive ANTLR Reference - 37&lt;/p&gt;
&lt;p&gt;Language Implementation Patterns - 35&lt;/p&gt;
&lt;p&gt;Manga Guide to Calculus - 20&lt;/p&gt;
&lt;p&gt;Hacking: The Art of Exploitation, Second Edition - 50&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Ebooks:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Beautiful Data - 36&lt;/p&gt;
&lt;p&gt;Beautiful Security - 32&lt;/p&gt;
&lt;p&gt;Beautiful Architecture - 36&lt;/p&gt;
&lt;p&gt;Beautiful Code - 36&lt;/p&gt;
&lt;p&gt;jQuery Cookbook - 28&lt;/p&gt;
&lt;p&gt;Data Analysis with Open Source Tools - 32&lt;/p&gt;
&lt;p&gt;Statistics in a Nutshell - 35&lt;/p&gt;
&lt;p&gt;Hadoop - 39&lt;/p&gt;
&lt;p&gt;Bioinformatics Programming Using Python - 48&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Videos:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Great R: Level 1 - 35&lt;/p&gt;</description><link>http://jcchurch.tumblr.com/post/2946060502</link><guid>http://jcchurch.tumblr.com/post/2946060502</guid><pubDate>Wed, 26 Jan 2011 15:43:00 -0600</pubDate></item><item><title>3D Visualization of an Adjacency Matrix in Matlab</title><description>&lt;p&gt;&lt;img src="http://i.imgur.com/K9G0v.png" alt="Bucky Ball" width="520" height="511"/&gt;&lt;/p&gt;
&lt;p&gt;An adjacency matrix is a data model to represent an undirected graph. Most adjacency matrices are two dimensional, which means you can use the built-in Matlab function &amp;#8220;gplot&amp;#8221; to visualize it. This function &amp;#8220;gplot3&amp;#8221; allows you to visualize adjacency matrices in 3D.&lt;/p&gt;
&lt;p&gt;I created the above demonstration using Matlab&amp;#8217;s built in Bucky Ball creator.&lt;/p&gt;
&lt;pre&gt;» [B XY] = bucky;
» gplot3(B, XY);
&lt;/pre&gt;
&lt;p&gt;Here&amp;#8217;s the code!&lt;/p&gt;
&lt;script src="https://gist.github.com/930284.js"&gt; &lt;/script&gt;</description><link>http://jcchurch.tumblr.com/post/2863221131</link><guid>http://jcchurch.tumblr.com/post/2863221131</guid><pubDate>Fri, 21 Jan 2011 17:30:00 -0600</pubDate><category>matlab</category></item><item><title>A Slinky in Matlab</title><description>&lt;p&gt;&lt;img height="525" width="595" src="http://i.imgur.com/3cZgu.png"/&gt;&lt;/p&gt;
&lt;p&gt;I made this because I need to learn about 3D graphics, but I&amp;#8217;m too lazy to learn Blender. This was made with 1 line of Matlab.&lt;/p&gt;
&lt;pre&gt;&amp;gt;&amp;gt; x = linspace(0, 200*pi, 10000); y = sin(x/100)+sin(x); z = cos(x); plot3(x, y, z)&lt;/pre&gt;</description><link>http://jcchurch.tumblr.com/post/2614607971</link><guid>http://jcchurch.tumblr.com/post/2614607971</guid><pubDate>Wed, 05 Jan 2011 17:30:07 -0600</pubDate><category>matlab</category></item><item><title>Comparing Stock Volatility Using R</title><description>&lt;p&gt;In my last post, I wrote a quick Python script to download 1 years worth of a stock&amp;#8217;s history into a CSV file for given a list of stocks. Today, we are going to take those stocks an compare the volatility between each stock&amp;#8217;s daily price change.&lt;/p&gt;
&lt;p&gt;Measuring volatility is a simple calculation of the standard deviation of the percentage of the stock&amp;#8217;s price change over a given year. The higher the standard deviation of percent change means higher volatility (and conversely, lower standard deviation means lower volatility).&lt;/p&gt;
&lt;p&gt;This program takes a list of ticker symbols, opens up their corresponding CSV files (see yesterday&amp;#8217;s post for more information), computes the standard deviation based on percent change, sorts that list, and prints.&lt;/p&gt;
&lt;p&gt;In the results, you can see that I&amp;#8217;m tracking 26 stocks. The three stocks with the lowest volatility are Proctor &amp;amp; Gamble (0.85%), Coca-Cola (0.99%) and Kraft (1.08%). The three stocks with the highest volatility are Micron Technology (3.24%), Netflix (3.44%), and Crosstex Energy Inc (3.62%).&lt;/p&gt;
&lt;p&gt;Disclaimer: I&amp;#8217;m not a broker and I haven&amp;#8217;t been investing for very long. This information should not be used as investment advice, but it should be used as a tutorial on how to pull information out of a CSV file in the R language. How you interpret this data is up to you.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Here&amp;#8217;s the data.&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;R version 2.7.1 (2008-06-23)
Copyright (C) 2008 The R Foundation for Statistical Computing
ISBN 3-900051-07-0

&amp;gt; source('stockSD.R')
          sdList
PG   0.008494452
KO   0.009928666
KFT  0.010838839
PFE  0.013281879
MSFT 0.013841909
YUM  0.014198831
SAP  0.014843072
FTR  0.015125052
ORCL 0.015317628
INTC 0.015906689
CPNO 0.016704188
AAPL 0.016787813
EMR  0.016905197
GE   0.017227925
GOOG 0.017538399
BIG  0.018467030
YHOO 0.018651226
GS   0.019989476
AMZN 0.020597942
BAC  0.023268540
C    0.023951356
F    0.023993128
RIMM 0.024404948
MU   0.032412592
NFLX 0.034474349
XTXI 0.036222946
&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Here&amp;#8217;s the code.&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;symbols &amp;lt;- c("BAC", "YHOO", "RIMM", "MU", "MSFT", "GOOG", "PFE", "INTC", "C", "GE", "SAP", "FTR", "BIG", "ORCL", "AMZN", "XTXI", "GS", "KO", "F", "PG", "EMR", "AAPL", "KFT", "NFLX", "CPNO", "YUM")

sdList &amp;lt;- 1:length(symbols)

for (s in 1:length(symbols)) {
    infile &amp;lt;- paste(symbols[s], ".csv", sep="")
    df &amp;lt;- read.csv(infile)
    percent_change &amp;lt;- ((df$Close - c(0, df$Close[1:length(df$Close)-1]))/ c(0, df$Close[1:length(df$Close)-1]) )[-1]
    sdList[s] &amp;lt;- sd(percent_change)
}

# Create a frame for our standard devation list
sdDF &amp;lt;- data.frame(symbols, sdList)

# Order the standard devation list by standard devations in ascending order
sorted.sdDF &amp;lt;- sdDF[order(sdDF$sdList), ]

# Apply names to the standard devation list
rownames(sorted.sdDF) &amp;lt;- sorted.sdDF$symbols

# Take out the names 
sorted.sdDF &amp;lt;- sorted.sdDF[-1]

print(sorted.sdDF)
&lt;/pre&gt;</description><link>http://jcchurch.tumblr.com/post/2572994531</link><guid>http://jcchurch.tumblr.com/post/2572994531</guid><pubDate>Sun, 02 Jan 2011 17:32:07 -0600</pubDate><category>r</category></item></channel></rss>
