Skip to content

Instantly share code, notes, and snippets.

View thomasnield's full-sized avatar

Thomas Nield thomasnield

  • Frisco, Texas (United States)
View GitHub Profile
@thomasnield
thomasnield / log_hill_climbing.py
Created October 19, 2019 20:29
Log function with hill climbing
import numpy as np
def log(base: float, n: float):
x = 1.0
best_loss = 1_000_000.0
for _ in range(100_000):
random_adj = np.random.normal()
x += random_adj
new_loss = ((base ** x) - n) ** 2
@thomasnield
thomasnield / binomial_distribution_from_scratch.py
Created October 20, 2019 01:40
Binomial Distribution from Scratch
import plotly.express as px
# Factorials multiply consecutive descending integers down to 1
# EXAMPLE: 5! = 5 * 4 * 3 * 2 * 1
def factorial(n: int):
f = 1
for i in range(n):
f *= (i + 1)
return f
@thomasnield
thomasnield / integral_sympy.py
Created October 20, 2019 20:03
Integrating with SymPy
from sympy import integrate, Symbol
x = Symbol('x')
# Calculate the integral function
integral = integrate(2 * x, x)
print(integral) # prints x**2
# Calculate the area under 2x between 0 and 2
area = integrate(2 * x, (x, 0, 2))
@thomasnield
thomasnield / approximate_integration_from_scratch.py
Created October 21, 2019 16:50
Approximating Integrals from Scratch
# Got this from https://www.kkhaydarov.com/approximate-integration-with-python/
def approximate_integral(a, b, n, f, rule="midpoint"):
delta_x = (b - a) / n
total_sum = 0
if rule == "midpoint":
for i in range(1, n + 1):
midpoint = 0.5 * (2 * a + delta_x * (2 * i - 1))
total_sum += f(midpoint)
@thomasnield
thomasnield / crude_quantile_approximation_from_scratch.py
Created October 21, 2019 18:56
Quantile approximation from scratch
import random
import plotly.express as px
# Got this from https://www.kkhaydarov.com/approximate-integration-with-python/
def approximate_integral(a, b, n, f, rule="midpoint"):
delta_x = (b - a) / n
total_sum = 0
if rule == "midpoint":
@thomasnield
thomasnield / simple_dji_tello_control.py
Last active February 11, 2022 03:56
Simple DJI Tello Drone Control
#
# Tello Python3 Control Demo
#
# http://www.ryzerobotics.com/
#
# Commands: https://dl-cdn.ryzerobotics.com/downloads/tello/0228/Tello+SDK+Readme.pdf
# 1/1/2018
import threading
import socket
@thomasnield
thomasnield / SnakeToCamelCase.kt
Created October 24, 2019 15:15
Snake Case to Camel Case
fun toCamelCase(s: String): String {
val parts = s.split("_".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
var camelCaseString = ""
for ((i,part) in parts.withIndex()) {
camelCaseString += part.substring(0, 1).let { if (i == 0) it.toLowerCase() else it.toUpperCase() } + part.substring(1).toLowerCase()
}
return camelCaseString
}
@thomasnield
thomasnield / SmartDateTimeParse.kt
Created October 24, 2019 16:09
Kotlin Smart DateTime Parse
fun String.smartLocalDateTimeParse(): LocalDateTime {
val dtmStr = replace("/", "-")
val (dt,tm) = dtmStr.split(" ")
return dt.smartLocalDateParse().atTime(tm.smartLocalTimeParse())
}
fun String.smartLocalDateParse(): LocalDate {
@thomasnield
thomasnield / CSVHelper.kt
Last active October 25, 2019 14:49
Kotlin CSV Assistant
package com.swa.np.common.util
import org.apache.commons.csv.CSVFormat
import org.apache.commons.csv.CSVRecord
import java.io.FileReader
object CSVHelper {
// Helpful docs: https://commons.apache.org/proper/commons-csv/user-guide.html
@thomasnield
thomasnield / plotly_demo.html
Created November 15, 2019 21:40
Plotly HTML
<head>
<!-- Plotly.js -->
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
</head>
<body>
<div id="myDiv"><!-- Plotly chart will be drawn inside this DIV --></div>