Skip to content

Instantly share code, notes, and snippets.

View bencbartlett's full-sized avatar

Ben Bartlett bencbartlett

View GitHub Profile
# Electromagnetic simulation at varying timescales - simulation code to make this animation:
# https://twitter.com/bencbartlett/status/1369396941730312193
#
# Animation idea was inspired by u/cenit997's (Twitter: @__cenit) Reddit post:
# https://www.reddit.com/r/Python/comments/hxmhai/a_simulation_of_how_an_incoherent_light_source/
#---
using LinearAlgebra, Base.Threads, BenchmarkTools, LoopVectorization, CSV, DataFrames, Printf, Plots
SetDirectory[NotebookDirectory[]];
\[Sigma] = 10;
\[Rho] = 28;
\[Beta] = 8/3;
Tmax = 10;
eqn = {
x'[t] == \[Sigma] (-x[t] + y[t]),
y'[t] == \[Rho]* x[t] - y[t] - x[t]*z[t],
@bencbartlett
bencbartlett / pulse.nb
Created October 6, 2019 21:34
Mathematica code for photon pulse animation
<< MaTeX`
SetOptions[MaTeX, "Preamble" -> {"\\usepackage{color,txfonts}"}];
SetDirectory[NotebookDirectory[]];
c = 1;
\[Alpha] = .7;
\[Beta] = .05;
k0 = 7;
\[Omega]0 = 2 c;
vg = c;
@bencbartlett
bencbartlett / hydrogen_orbitals.nb
Last active November 27, 2021 03:10
Mathematica code for this animation of transitions in hydrogen wavefunctions: https://twitter.com/bencbartlett/status/1287802625602117632
<< MaTeX`
SetOptions[MaTeX, "Preamble" -> {"\\usepackage{color,txfonts}"}];
SetDirectory[NotebookDirectory[]];
Clear[drawLadder];
drawLadder[n_, l_, m_, imsize_: 500] := Module[{maxrungs = 5, mag = 4},
Graphics[{
White, Opacity[1], Thickness[.02], Dashing[None],
Table[Line[{{0, k}, {1, k}}], {k, maxrungs}], (*draw n lines*)
SetDirectory[NotebookDirectory[]];
\[Theta]0 = -.1 \[Pi]; \[Phi]0 = .35*\[Pi];
\[Psi]0 = {Cos[\[Theta]0] Cos[\[Phi]0], Sin[\[Theta]0] Cos[\[Phi]0],
Sin[\[Phi]0]};
Clear[BlochSphereVector];
BlochSphereVector[\[Psi]_, imSize_ : 1000, color_ : Gray,
opacity_ : 1, \[Phi]view_ : \[Pi]/4, viewdist_ : 100] := Module[{
tubeball, pointline,
class Foo {
constructor() {
this.bar = "baz";
}
}
console.log('' + Foo);
// > "class Foo {\n constructor()...z\";\n }\n\n}"
@bencbartlett
bencbartlett / Hatchery.ts
Last active December 20, 2018 20:54
Hatchery: build() vs refresh()
import {$} from '../caching/GlobalCache';
export class Hatchery extends HiveCluster {
// constructor gets run only on ticks with build() phase
constructor(colony: Colony, headSpawn: StructureSpawn) {
super(colony, headSpawn, 'hatchery');
this.memory = Mem.wrap(this.colony.memory, 'hatchery', HatcheryMemoryDefaults, true);
// Register physical structure components
this.spawns = colony.spawns;
@bencbartlett
bencbartlett / GlobalCache.ts
Last active December 20, 2018 20:42
GlobalCache.ts
export class $ { // $ = cash = cache... get it? :D
static set<T extends HasRef, K extends keyof T>(thing: T, key: K,
callback: () => (T[K] & (undefined | HasID | HasID[])),
timeout = CACHE_TIMEOUT): void {
const cacheKey = thing.ref + '$' + key;
if (!_cache.things[cacheKey] || Game.time > _cache.expiration[cacheKey]) {
// Recache if new entry or entry is expired
_cache.things[cacheKey] = callback();
_cache.expiration[cacheKey] = getCacheExpiration(timeout, Math.ceil(timeout / 10));
@bencbartlett
bencbartlett / MarkovText.py
Created June 20, 2016 18:09
Very simple second-order Markov text generator. Train on whatever sources you want.
import random
normalizeLengths = True # Set the length of the two input sources to be equal.
# Generate transition matrix
A = "ABCDEFGHIJKLMNOPQRSTUVWXYZ "
charnums = len(A)
# Read through text and index transition matrix
trainingText = " ".join(open("KingJamesBible.txt",'r').read().splitlines())
trainingText2 = " ".join(open("GriffithsQuantumMechanics.txt",'r').read().splitlines())
@bencbartlett
bencbartlett / LayerPlot.py
Created June 20, 2016 18:07
Code to make 2D evolving multiplot histogram with side plots, like this one: https://gfycat.com/PersonalGraciousAfricanharrierhawk
# Importations
import os, shutil
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
import pylab
import warnings
from mpl_toolkits.mplot3d import Axes3D, art3d
from matplotlib.offsetbox import TextArea, VPacker, AnnotationBbox