Skip to content

Instantly share code, notes, and snippets.

View jaems33's full-sized avatar
🤔

James Teow jaems33

🤔
View GitHub Profile
@jaems33
jaems33 / catenary.c
Last active August 18, 2021 21:11
Catenary Equation in C
#include <stdio.h>
#include <math.h>
double catenary(const double x, const double a) {
return a * cosh(x / a);
}
double catenary_with_eulers(const double x, const double a) {
return (a / 2.0) * (exp(x / a) + exp(-x / a));
}
@jaems33
jaems33 / fit_one_cycle.py
Last active November 2, 2018 17:19
From train.py via fast.ai
def fit_one_cycle(learn:Learner,
cyc_len:int,
max_lr:Union[Floats,slice]=default_lr,
moms:Tuple[float,float]=(0.95,0.85),
div_factor:float=25.,
pct_start:float=0.3,
wd:float=None,
callbacks:Optional[CallbackList]=None, **kwargs) -> None:
"""Fit a model following the 1cycle policy."""
max_lr = learn.lr_range(max_lr)
import random
import numpy as np
import matplotlib.pyplot as plt
# Udacity provided code
class Robot(object):
def __init__(self, length=20.0):
"""
Creates robot and initializes location/orientation to 0, 0, 0.
@jaems33
jaems33 / path_smoothing.py
Last active August 26, 2023 07:00
Path smoothing
from copy import deepcopy
def printpaths(path, newpath):
for old, new in zip(path, newpath):
print('[' + ', '.join('%.3f' % x for x in old) +
'] -> [' + ', '.join('%.3f' % x for x in new) + ']')
path = [[0, 0], [0, 1], [0, 2], [1, 2], [2, 2], [3, 2], [4, 2], [4, 3], [4, 4]]
import numpy as np
from numpy.linalg import inv
x_observations = np.array([4000, 4260, 4550, 4860, 5110])
v_observations = np.array([280, 282, 285, 286, 290])
z = np.c_[x_observations, v_observations]
# Initial Conditions
a = 2 # Acceleration
import numpy as np
# Initial Values
x = 50
x_dot = 5
# Standard Deviations
x_std = 0.5
x_dot_std = 0.2
import numpy as np
A = np.array([[90, 80, 40],
[90, 60, 80],
[60, 50, 70],
[30, 40, 70],
[30, 20, 90]])
ones = np.ones([5, 5])
deviation = A - (ones.dot(A) / len(A))
def predict(mean1, var1, mean2, var2):
new_mean = mean1 + mean2
new_var = var1 + var2
return [new_mean, new_var]
def updated_mean(mean1, var1, mean2, var2):
new_mean = (mean1 * var2 + mean2 * var1) / (var1 + var2)
return new_mean
def updated_var(var1, var2):
new_var = 1 / ((1 / var1) + (1 / var2))
return new_var
def expectedReturn(state, action, stateValue):
# Initiate and populate returns with cost associated with moving cars
returns = 0.0
returns -= COST_OF_MOVING * np.absolute(action)
# Number of cars to start the day
carsLoc1 = int(min(state[0] - action, MAX_CARS))
carsLoc2 = int(min(state[1] + action, MAX_CARS))
# Iterate over Rental Rates
for rentalsLoc1 in range(0, POISSON_UPPER_BOUND):
for rentalsLoc2 in range(0, POISSON_UPPER_BOUND):