Skip to content

Instantly share code, notes, and snippets.

View jcchurch's full-sized avatar

James Church jcchurch

View GitHub Profile
@jcchurch
jcchurch / birthdays.py
Created October 17, 2011 15:07
Coupon Collector's Problem applied to Birthdays
# This is not the actual Birthday Paradox Problem.
# This is something similar suggested by Taylor Smith.
#
# How many people do you need in a room to have a 50%
# chance of having every possible birthday represented
# within the room?
#
# After typing all of this out, I realized that it's
# the same as the Coupon Collector's Problem.
@jcchurch
jcchurch / linear_regression.m
Created October 13, 2011 15:45
Linear Regression in Matlab
function [m b] = linear_regression(X, Y)
n = length(Y);
EX = sum(X);
EY = sum(Y);
EX2 = sum(X.*X);
EXY = sum(X.*Y);
m = (n*EXY - EX*EY) / (n*EX2 - EX*EX);
b = (EY - m*EX) / n;
end
import java.util.*;
public class Tribute {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int datasets = scan.nextInt();
scan.nextLine();
for (int i = 0; i < datasets; i++) {
String original = scan.nextLine();
@jcchurch
jcchurch / symmetric.m
Created June 16, 2011 14:59
Determine if a graph is symmetric
function yes = symmetric(g)
% Determins if an adjacency matrix
% is symmetric or not.
yes = 1;
[height width] = size(g);
if height ~= width
yes = 0;
end
for i = 1:height
@jcchurch
jcchurch / connected.m
Created June 16, 2011 14:53
Quickly determine if a graph is connected
function result = connected(A)
% connected(A)
%
% This function takes an adjacency matrix A
% and returns if the graph is connected.
% 1 means connected.
% 0 means not connected.
% We assume that the starting result is connected.
% In lawyer terms, the graph is connected until proven
@jcchurch
jcchurch / rotationMatrix.m
Created May 4, 2011 19:55
Perform the 3 axis rotation on a set of points.
function V = rotationMatrix(P, x, y, z)
% rotationMatrix performs the 3 axis rotation on a
% matrix P, which is a 3-by-p set of points, where
% d is equal to 3 and represents the dimensionality
% of the data and p represents the total number of
% observations.
%
% x represents the amount of rotation along the x axis in radians
% y represents the amount of rotation along the y axis in radians
% z represents the amount of rotation along the z axis in radians
@jcchurch
jcchurch / spline3d.m
Created April 21, 2011 16:55
This function computes the 3D spline interpolation of a 3D dataset. I did not write this code.
function newCurve = spline3d(curve, dt)
% interpote a 3d curve using spline
% path 3*x
% newPath 3*x
x = curve(1, :);
y = curve(2, :);
z = curve(3, :);
t = cumsum([0;sqrt(diff(x(:)).^2 + diff(y(:)).^2 + diff(z(:)).^2)]);
sx = spline(t,x);
sy = spline(t,y);
@jcchurch
jcchurch / gplot3.m
Created April 20, 2011 03:32
3D Version of Matlab's GPLOT
function gplot3(A, xyz)
% GPLOT3(A, xyz) is nearly the same as GPLOT(A, xy) except
% that the xyz variable requires a third dimension.
% This function takes an adjacency matrix and visualizes it
% in 3D.
[d e] = size(A);
if d ~= e
error('A matrix must be square.');
end
@jcchurch
jcchurch / diskstra.m
Created April 20, 2011 03:22
Dijkstra's Algorithm in Matlab
function D = dijkstra(G, pairs)
% This function takes an adjacency matrix called G
% and a p-by-2 matrix called pairs.
% The pairs matrix will contain pairs of indices.
% This function will determine the shortest distance from
% the first index in the pair to the second index for
% every pair in matrix pairs.
%
% The function will only return a p-by-1 matrix of shortest
% distances. I could use it to also return the shortest path,
@jcchurch
jcchurch / mpipypi.py
Created April 20, 2011 03:20
Compute Pi using Python and MPI4PY
"""
This code computes pi. It's not the first python
pi computation tool that I've written. This program
is a good test of the mpi4py library, which is
essentially a python wrapper to the C MPI library.
To execute this code:
mpiexec -np NUMBER_OF_PROCESSES -f NODES_FILE python mpipypi.py