Skip to content

Instantly share code, notes, and snippets.

@kersulis
kersulis / dbode.jl
Last active April 21, 2016 19:54
LTC model described in section 5.3.2 of http://deepblue.lib.umich.edu/handle/2027.42/113584
"""
LTC model used by [Baghsorkhi2015](http://deepblue.lib.umich.edu/handle/2027.42/113584)
(see section 5.3.2).
"""
type DBODE
"index of bus whose voltage is regulated"
bus::Int64
"index of line whose tap is influenced"
line::Int64
"voltage setpoint [pu]"
@kersulis
kersulis / .block
Last active August 16, 2018 23:01
Force-directed graph: injection shift factors
heigh: 800
@kersulis
kersulis / pvec2mat.jl
Last active December 7, 2015 22:45
Convert a permutation vector to its corresponding matrix.
"""
Given a permutation vector, return the corresponding permutation matrix such that
`A[p,:] = P*A`.
See [Wikipedia](https://en.wikipedia.org/wiki/Permutation_matrix#Definition).
"""
function pvec2mat(p)
n = length(p)
P = zeros(n,n)
for i in 1:n
P[i,:] = ej(n,p[i])
@kersulis
kersulis / naive_lu.jl
Created December 7, 2015 22:39
Naive LU decomposition.
"""
Naive LU decomposition (no pivoting) from
[Bindel's CS6210 class notes](http://www.cs.cornell.edu/~bindel/class/cs6210-f12/notes/lec09.pdf).
Beware A[1,1] = 0...
"""
function mylu(A)
m,n = size(A)
for j = 1:n-1
A[j+1:n,j] = A[j+1:n,j]/A[j,j]
@kersulis
kersulis / eigenimages.py
Last active November 20, 2020 11:36
Interactive Eigenimages. Live version on Binder: http://mybinder.org/repo/kersulis/SVD-eigenimages
import ipywidgets as ipw
from pylab import *
def classify_image(tst,trn,k):
""" Input:
tst: a n x T vector corresponding to T images that are to be classified
trn: n x m x 10 matrix containing m training samples for each digit
k: integer between 1 and n
"""
n,T = tst.shape
@kersulis
kersulis / createY.jl
Last active November 12, 2015 21:23
Generate an admittance matrix from a set of real vectors.
"""
createY(f,t,x [,r,b]) -> Y
Create an admittance matrix for AC power flow.
All inputs are real. The output matrix is real if no line
resistances are provided (DC case), and complex otherwise.
* `f`,`t`: vectors encoding all lines (fi,ti)
* `x`: per-unit reactance xi for all lines
* `r`: per-unit resistance ri for all lines
* `b`: per-unit susceptance bi for all lines
@kersulis
kersulis / isf.jl
Last active November 12, 2015 21:25
Injection shift factors for DC power flow. Accommodates both single slack and distributed slack (droop response).
"""
Calculate injection shift factor matrix.
Each row corresponds to a line in the network.
Each column corresponds to a node.
Credit to Jonathon Martin for derivation.
Inputs:
* `Y`: full admittance matrix
* `lines`: vector of tuples; each tuple encodes a line as (i,j)
* `ref`: index of angle reference bus
@kersulis
kersulis / surfer.jl
Last active August 29, 2015 14:27
Translate a portion of the web into a directed graph. Similar to surfer.m. https://www.math.washington.edu/~greenbau/Math_498/surfer.m
using Requests, Graphs, ProgressMeter
function surfer(root,n=100)
# Crawling mechanics: hyperlink regex and
# filter used for skipping certain links
rexp = r"https?://[^\"|<|>|;|'|)| |…]+"
# Focus on links that contribute to the graph by skipping
# all links that contain the following strings:
skip = [".gif";".jpg";".jpeg";".pdf";".css";".js";".asp";".mwc";".ram";
".cgi";"lmscadsi";"cybernet";"w3.org";"google";"yahoo";
@kersulis
kersulis / NotebookNirvana.ipynb
Last active November 20, 2020 11:37
Python 2 and Python 3, Julia 0.3 (release) and 0.4 (dev), plus bash, all in Jupyter notebook on Ubuntu.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@kersulis
kersulis / MatpowerCases.jl.cov
Created July 23, 2015 21:59
Code coverage file for MatpowerCases.jl. Something about this file may be causing this: https://github.com/IainNZ/Coverage.jl/issues/75
- module MatpowerCases
- using MAT
-
- export loadcase,casenames
-
- """ Return a Dict containing power system data
- in MATPOWER's format.
- Return this list of cases by running casenames().
- """
- function loadcase(caseName::ASCIIString; describe=true)