Skip to content

Instantly share code, notes, and snippets.

Avatar

Ben Bartlett bencbartlett

View GitHub Profile
@bencbartlett
bencbartlett / pulse.nb
Created Oct 6, 2019
Mathematica code for photon pulse animation
View pulse.nb
<< 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 Nov 27, 2021
Mathematica code for this animation of transitions in hydrogen wavefunctions: https://twitter.com/bencbartlett/status/1287802625602117632
View hydrogen_orbitals.nb
<< 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*)
View lorenz_many_particles.nb
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],
View coherent_incoherent_light.jl
# 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
View ring_computer_animation.nb
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,
View ClassStringify.js
class Foo {
constructor() {
this.bar = "baz";
}
}
console.log('' + Foo);
// > "class Foo {\n constructor()...z\";\n }\n\n}"
@bencbartlett
bencbartlett / Hatchery.ts
Last active Dec 20, 2018
Hatchery: build() vs refresh()
View Hatchery.ts
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;
View 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 Jun 20, 2016
Very simple second-order Markov text generator. Train on whatever sources you want.
View MarkovText.py
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 Jun 20, 2016
Code to make 2D evolving multiplot histogram with side plots, like this one: https://gfycat.com/PersonalGraciousAfricanharrierhawk
View LayerPlot.py
# 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