Skip to content

Instantly share code, notes, and snippets.

View cshjin's full-sized avatar

Hongwei Jin cshjin

View GitHub Profile
@cshjin
cshjin / power_method.py
Created September 22, 2018 23:28
Power method
import numpy as np
import sys
# reference: https://en.wikipedia.org/wiki/Power_iteration
def power_method(M):
b = np.random.rand(M.shape[1])
diff = np.linalg.norm(b)
while diff > 10 ** (-6):
b_new = np.dot(M, b)
b_norm = np.linalg.norm(b_new)
@cshjin
cshjin / subl.reg
Created September 17, 2017 06:26
Create the right click for sublime 3 on Windows, adjust your subl path accordingly.
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\*\shell\Edit with Sublime Text]
@="Edit with &Sublime Text"
"Icon"="C:\\Program Files\\Sublime Text 3\\sublime_text.exe,0"
"MuiVerb"="Edit with Sublime Text"
[HKEY_CLASSES_ROOT\*\shell\Edit with Sublime Text\command]
@="C:\\Program Files\\Sublime Text 3\\sublime_text.exe \"%1\""
@cshjin
cshjin / saddle_point.m
Created September 9, 2017 21:32
Saddle point example
v = -5:0.5:5;
[x, y] = meshgrid(v);
z = x .* exp(x.^2 - y.^2) + y.*exp(-x.^2 + y.^2);
[px, py] = gradient(z);
figure
surf(x, y, z)
figure
contour(x, y, z);
hold on
quiver(x, y, px, py);
@cshjin
cshjin / gradient_descent_convergence.m
Last active September 10, 2017 01:49
Gradient descent example
%% demonstrate the linear convergence rate of gradient descent
stepsize = 0.05;
x = rand(2, 1);
f_diff = 1;
f = @(x) 1/2*(x(1)^2 + 10 * x(2)^2);
first_grad = @(x) [x(1); 10*x(2)];
iter = 0;
fprintf('ITER \t F_VAL \t F_VAL_U \t F_DIFF \n');
% fprintf('ITER \t\t F_VAL \t F_VAL_U \t F_DIFF \t F_GRAD \t F_GRAD_U \n');
clear;
has_quadprog = exist( 'quadprog' ) == 2 | exist( 'quadprog' ) == 3;
has_linprog = exist( 'linprog' ) == 2 | exist( 'linprog' ) == 3;
rnstate = randn( 'state' ); randn( 'state', 1 );
s_quiet = cvx_quiet(true);
s_pause = cvx_pause(false);
cvx_clear; echo on
b = [2; 0; 2; 0];
cvx_begin
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 28 15:28:15 2015
"""
from sklearn.cluster import KMeans
import numpy as np
import pandas
dataset = np.array([1, 4, 5, 6, 9])
km = KMeans(n_clusters=3)
@cshjin
cshjin / farmer_lp.py
Last active August 29, 2015 14:24
Pyomo solving Farmer's problem step by step improvement
# -*- coding: utf-8
"""
A deterministic model of farmer's problem:
"""
try:
from pyomo.core import *
from pyomo import *
from pyomo.opt import *
from pyomo.core.base import *
@cshjin
cshjin / farmer_concrete.py
Created June 26, 2015 01:31
An example of using Coopr (Pymo) and PySP with ConcreteModel
# _________________________________________________________________________
#
# Pyomo: Python Optimization Modeling Objects
# Copyright (c) 2014 Sandia Corporation.
# Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
# the U.S. Government retains certain rights in this software.
# This software is distributed under the BSD License.
# _________________________________________________________________________
#
# Farmer: rent out version has a scalar root node var
@cshjin
cshjin / Makefile
Created March 18, 2015 22:26
C Based Server TCP demo
all: webserver
webserver: webserver.o tcp.o request.o
gcc webserver.o tcp.o request.o -o webserver -g -lpthread
webserver.o: webserver.c
g cc -Wall -g -c webserver.c -o webserver.o
tcp.o: tcp.c tcp.h
gcc -Wall -g -c tcp.c -o tcp.o
@cshjin
cshjin / minimum_scalar_product.py
Created February 23, 2015 21:07
Code Jam Practice - Round 1A 2008
N = int(raw_input())
with open('outfile.txt', 'w') as outfile:
for test in range(N):
size = int(raw_input())
v1 = sorted(map(int, raw_input().split(" ")), reverse=True)
v2 = sorted(map(int, raw_input().split(" ")))
pro = 0
for i in range(size):
pro += v1[i] * v2[i]
outfile.write("Case #{}: ".format(test + 1) + str(pro) + "\n")