Skip to content

Instantly share code, notes, and snippets.

View shane5ul's full-sized avatar

Sachin Shanbhag shane5ul

View GitHub Profile
@shane5ul
shane5ul / Gerschgorin.py
Last active June 8, 2021 07:48
demoGershgorin(A) plots the Gershgorin discs that bound the eigenvalues of a matrix A, and also the actual eigenvalues.
import numpy as np
import matplotlib
from matplotlib.patches import Circle
from matplotlib.collections import PatchCollection
import matplotlib.pyplot as plt
from numpy import linalg as LA
def demoGerschgorin(A):
n = len(A)
@shane5ul
shane5ul / ipythonToBlogger
Last active August 29, 2015 14:24
In iPython LaTeX markdown uses the $ and $$ tags for math mode. My blogger template likes \( \) and \[ \] instead. This sed script lets me quickly do the conversion.
sed -r 's/\$\$([^$$]*)\$\$/\\[\1\\]/g' INPUTFILE | sed -r 's/\$([^$]*)\$/\\(\1\\)/g'
@shane5ul
shane5ul / buffettkelly.py
Created July 1, 2015 03:39
These are helper functions for this blog post.
def integrand(r, f, rmin, rmax, mu, sigma):
"""defines the part inside the integral sign"""
from scipy.stats import truncnorm
a, b = (rmin - mu)/sigma, (rmax - mu) /sigma
rv = truncnorm(a, b, loc=mu, scale=sigma)
return rv.pdf(r) * np.log(1 + f*(r-1))
def logS(f, rmin, rmax, mu, sigma):
"""numerically solves the integral"""
from scipy.integrate import quad
@shane5ul
shane5ul / MySymPyCheat
Last active May 15, 2018 21:56
My SymPy Cheat Sheet
{
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
@shane5ul
shane5ul / splitup.py
Last active January 4, 2020 18:13
Given a set of contributions (and optionally expemptions) creates a list of pair-wise transactions which "settles" all accounts
#!/usr/bin/env python3
#
# (c) Sachin Shanbhag, 2015
# Last Modified: Jan 5, 2020 (input change to space separated; python3 compliance)
#
# Given a set of contributions (and optionally expemptions) creates a list of pair-wise
# transactions which "settles" all accounts
#
# example : python splitup.py INPUTFILENAME
#
@shane5ul
shane5ul / logWeightedAverage.py
Created January 29, 2015 14:08
Given the logarithm of the weights, L, and observation, R, this computes the weighted average
def logWeightedAverage(L, R):
n = len(L)
Lscale = 0.
sumNumerator = 0.
sumDenominator = 0.
for j in range(n):
@shane5ul
shane5ul / random_number_demo.f90
Created December 17, 2014 18:01
Using Fortran90's intrinsic random number generator, using either a specified seed or using the system clock
!
! Program to demonstrate the use of Fortran 90's intrinsic random_number()
!
program main
integer :: values(8), k, nseed
integer, dimension(:), allocatable :: seed
real(8) :: r(10)
@shane5ul
shane5ul / DCS2.m
Created June 9, 2014 20:08
Discrte Cosine Series Tutorial 2
n = 15; % number of collocation points is (n+1)
nmodes = 5; % number of modes of the DCS
xi = [0:n]'/(n+1); % collocation points for discrete cosine series
V = zeros(n+1, nmodes);
%
% V: stack the nmodes DCS basis functions columnwise
%
for j = 1:nmodes
@shane5ul
shane5ul / DCS1.m
Created June 9, 2014 19:41
Discete Cosine Series Tutorial: Snippet 1
n = 15; % number of collocation points is (n+1)
nmodes = 5; % number of modes of the DCS
xi = [0:n]'/(n+1); % collocation points for DCS
V = zeros(n+1, nmodes);
%
% V: stack the nmodes DCS basis functions columnwise
%
@shane5ul
shane5ul / KahanSum.m
Created May 12, 2014 19:19
An Octave program to compute a compensated sum.
function s = KahanSum(x)
s = 0; % the sum
r = 0; % the error or remainder
for i = 1:length(x)
tmp = s;
y = x(i) + r;
s = tmp + y; % s = s + x_i
r = (tmp - s) + y;