Skip to content

Instantly share code, notes, and snippets.

View samstewart's full-sized avatar

Sam Stewart samstewart

  • New York, New York
View GitHub Profile
@samstewart
samstewart / plot_vector_field.m
Created October 6, 2016 22:34
Plot vector field in Matlab
% make a grid from [0, 2] x [0, 2]
[x, y] = meshgrid(0:.1:2, 0:.1:2);
% now we construct the vector field
u = -y;
v = x;
% why do they call it quiver?
quiver(x,y,u,v)
@samstewart
samstewart / fourier_linspace.m
Created October 6, 2016 18:51
Improvement over matlab linspace command that uses funky spacing
% The linspace command in matlab has spacing which is strange (x2 - x1)/(n-1). This is incorrect for the Fourier commands. This command fixes the problem.
function x = fourier_linspace(x1, x2, N)
% we can use the matlab linspace command and just ignore the right endpoint.
x = linspace(x1, x2, N + 1);
x = x(1:end - 1);
end
@samstewart
samstewart / plot_fourier_modes.m
Created October 6, 2016 18:50
Plots the Fourier modes with K sample points.
% simple script that allows one to plot the Fourier modes. % The modes should be given in Terry Tao's notation so that
% u = \sum_{i = 1}^N exp(2 \pi n xi) \hat{u}_n
% For example, cos would be given as [0 1/2]
function plot_fourier_modes(K, modes)
plot(fourier_linspace(0, 2*pi, 2*K + 1), ifft(full_fourier_spectrum(K, modes))); grid on;
end
@samstewart
samstewart / heatmap.m
Created September 30, 2016 18:03
Heatmap in matlab
% generate a simple heatmap in matlab
colormap('hot');
% randomly generate some noise
data = rand(100, 100);
% plot "image with scaled color".
imagesc(data)
@samstewart
samstewart / vec2ind.m
Created September 23, 2016 19:25
Vector indexing in matlab. use a vector to index a multidimensional array.
% simple command that converts a vector to a multidimensional index given the size of the array
% you should ignore any singleton dimensions (don't provide an element of the vector)
% TODO turn this into a Gist
% size_of_matrix: vector of dimensions of the matrix (can include singleton dimensions)
% vec_index: a vector whose nth component corresponds to the nth index.
function indx = vec2ind(size_of_matrix, vec_index)
% We convert this to a linear index. Not that matlab takes the full n dimensional integer
% lattice and converts it to a one dimensional integer lattice by reading off successive columns.
% the formula (really bijection between countable sets) between Z^n_1 x Z^n_2 x Z^n_3 --- Z^n_k is given by
% (a_0, a_1, ..., a_n) -> a_0 + (b_0)(a_1 - 1) + (b_0 b_1) (a_2 - 1) + (b_0 b_1 b_2) (a_3 - 1) + ....
@samstewart
samstewart / java_Makefile.make
Created June 13, 2016 15:49
Simple makefile for java
# A simple makefile for java programs
JCC = javac
LIBS = /usr/bin/apache-commons.jar:/usr/bin/pdfbox.jar
JFLAGS = -g -cp $(LIBS):.
# we clear any default suffixes
.SUFFIXES: .java .class
@samstewart
samstewart / photo_import.py
Created January 11, 2014 05:50
Photo import script
#!/usr/bin/python
# awesome python script that takes all files from the camera and archives them.
# it imports from the temp directory and builds a large archive in the Photo folders.
# The current iteration is quite functional in an effort for conciseness.
import os, time, shutil, calendar, subprocess, sys, fnmatch, shutil, re
from os import path
class PhotoArchiver:
@samstewart
samstewart / Makefile
Last active January 1, 2016 22:49
Template for CPP makefiles (correction to http://www.cs.colby.edu/maxwell/courses/tutorials/maketutor/)
# g++ -mmacosx-version-min=10.8 -o bin/kepler kepler.cpp image_compare.cpp s3_conn.cpp report_parser.cpp `GraphicsMagick++-config --cppflags --cxxflags --ldflags --libs` -L 'reference source/jsoncpp-src-0.5.0/libs/linux-gcc-4.2.1' -ljson -I 'reference source/jsoncpp-src-0.5.0/include/json' -L 'reference source/libs3-2.0/build/lib' -I 'reference source/libs3-2.0/inc' -I include -lcurl -lxml2 -lz -ls3
GMLINKER=`GraphicsMagick++-config --cppflags --cxxflags --ldflags --libs`
IDIR = include
CC = g++
CFLAGS =-I$(IDIR) -mmacosx-version-min=10.8 -Wall -I/usr/local/include/GraphicsMagick -g
ODIR = bin
LDIR =../lib
@samstewart
samstewart / output.json
Created December 26, 2013 23:56
sample cucumber output
[
{
"description" : "",
"elements" :
[
{
"description" : "",
@samstewart
samstewart / retina.js
Created August 30, 2013 19:14
Simple JQuery plugin that replaces all images in a webpage according to the iOS device and resolution. Expects files named according to Apple's naming scheme. I'd like to add -Landscape and -Portrait modifiers in the future.
(function() {
var root = (typeof exports == 'undefined' ? window : exports);
var config = {
// Ensure Content-Type is an image before trying to load @2x image
// https://github.com/imulus/retinajs/pull/45)
check_mime_type: true
};