Skip to content

Instantly share code, notes, and snippets.

View tinybike's full-sized avatar

Jack Peterson tinybike

  • Merica
View GitHub Profile
@tinybike
tinybike / prettyerrors.m
Created September 1, 2014 04:35
convert a number and its error to a more readable version
function [u_sig,u_paren] = pretty_errors(u,u_err)
% Convert a number and its error to a more readable version:
% Ex:
% u = 1.234567, u_err = 0.012345 -> u_sig = '1.23', u_paren = '1'
% (for use with notation u = 1.23(1))
n = -1;
u_paren = 0;
while ~u_paren
@tinybike
tinybike / commatic.m
Created September 1, 2014 05:18
insert commas into numbers for display
function comma_N = commatic(N)
% COMMATIC Insert commas into integers, e.g. 1234567 -> '1,234,567'.
% (c) Jack Peterson, 4/29/2013
N = num2str(N);
num_digits = length(N);
comma_N = '';
for j = 1:num_digits
comma_N = strcat(N(num_digits-j+1),comma_N);
@tinybike
tinybike / perceptron.py
Created September 1, 2014 07:20
perceptron neural network
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Perceptron neural network. Can learn any linearly separable logic.
(c) Jack Peterson, 6/12/2008
"""
from random import random
def hardlimit(row, col, bias):
@tinybike
tinybike / chomp.pl
Created September 1, 2014 21:15
because it's there
#!/usr/bin/perl
foreach(<>){
chomp;
}
exit;
@tinybike
tinybike / sockjsio.js
Created September 3, 2014 04:40
sockjs wrapper with some semantic sugar to make it look like socket.io
/**
* SockJS wrapper with some semantic sugar to make it look more like
* socket.io. Supports socket.emit and socket.on. Based on:
* http://simplapi.wordpress.com/2013/09/22/sockjs-on-steroids/
*
* @author jack@tinybike.net (Jack Peterson)
*/
var SOCKJSIO = (function (my, $) {
// Module exports
var _exports = my._exports = my._exports || {};
CC = mpic++
CXXFLAGS = -g -W -Wall -Werror
LIBS = -lboost_mpi -lboost_serialization -lboost_system -lboost_filesystem -lboost_graph_parallel -lboost_iostreams
all: Boost
Boost: Boost.o
$(CC) $(CXXFLAGS) -o Boost Boost.o $(LIBS)
Boost.o: Boost.cpp
@tinybike
tinybike / mssql_connect.jl
Created September 14, 2014 23:32
connect julia to mssql using the magic of pycall
# connect julia to mssql using the magic of pycall
# @author Jack Peterson (jack@tinybike.net)
using PyCall
@pyimport pymssql
# Connect to MSSQL Server
conn = pymssql.connect(server="SERVER:PORT",
user="USER",
@tinybike
tinybike / weighted_median.py
Last active July 4, 2023 15:03
calculate a weighted median
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
calculate a weighted median
@author Jack Peterson (jack@tinybike.net)
"""
from __future__ import division
import numpy as np
def weighted_median(data, weights):
@tinybike
tinybike / weighted_median.jl
Last active August 29, 2015 14:06
weighted medians for julia
using Base.Test
function weighted_median{T<:Real,W<:Real}(data::Array{T,1}, weights::Array{W,1};
checknan::Bool=true)
isempty(data) && error("median of an empty array is undefined")
if length(data) != length(weights)
error("data and weight vectors must be the same size")
end
if checknan
for (i, x) in enumerate(data)
# "Find a full-rank matrix with smallest singular value less than X."
# Question: is this a good way to do proof-of-work?
using Winston
using Distributions
function svd_work(N, target)
tic()
while true
A = rand(N, N)