Skip to content

Instantly share code, notes, and snippets.

View NoFishLikeIan's full-sized avatar
🦉
night owling

Andrea NoFishLikeIan

🦉
night owling
View GitHub Profile
@NoFishLikeIan
NoFishLikeIan / persistent_sequence.py
Created October 20, 2023 14:13
Get breaking index of state persistence for sequences of length more than tau
import numpy as np
import numpy.typing as npt
def simulate(P:npt.NDArray, T:int, s0:int = 0) -> np.ndarray:
'Simulates with transition matrix P for T steps, starting at s0 = 0.'
u = np.random.uniform(0., 1., T)
S = np.empty(T, dtype = int)
S[0] = s0
### A Pluto.jl notebook ###
# v0.19.22
using Markdown
using InteractiveUtils
# This Pluto notebook uses @bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of @bind gives bound variables a default value (instead of an error).
macro bind(def, element)
quote
local iv = try Base.loaded_modules[Base.PkgId(Base.UUID("6e696c72-6542-2067-7265-42206c756150"), "AbstractPlutoDingetjes")].Bonds.initial_value catch; b -> missing; end
@NoFishLikeIan
NoFishLikeIan / bifurcation.py
Last active December 9, 2022 09:33
A small routine to compute bifurcation diagrams
import numpy as np
import matplotlib.pyplot as plt
"""
Compute the trajectory of a function f: (R^n x R) -> R^n, where x_{t + 1} = f(x_t, beta).
"""
def trajectory(f, beta, x0: np.ndarray, T: int) -> np.ndarray:
n = x0.shape[0]
X = np.zeros((T + 1, n), dtype = np.float64)
import numpy as np
import matplotlib.pyplot as plt
# This function takes previous estimations of s(t) and generates a new RUNGE-KUTTA estimation based upon given timepoints
def NextApproximation(f, t, s, h):
k1 = f(t, s)
k2 = f(t + h/2, s + h*(k1/2))
"Pair"
function ↑(a::String, b::String)
a * '0' * b * a * '1' * b
end
"""
Unpair one step
"""
function ↓(p::String)
mid = length(p) ÷ 2
#!/bin/bash
filename=$1
echo "timeout --foreground 2h python3 $filename" | at 2300
@NoFishLikeIan
NoFishLikeIan / plotattractors.jl
Last active November 30, 2021 14:05
Plot attractors and associated basins, as returned by `DynamicalSystems.basins_of_attraction`
function plotattractors(basins, attractors, x_space; kwargs...)
lims = extrema(x_space)
attractors_keys = keys(attractors) |> collect |> sort
isdivergent = -1 ∈ basins # Check whether there is a divergent region
isunique = length(unique(basins)) == 1 # Check whether the basin is uniform
levels = isdivergent ? [-1, attractors_keys..., Inf] : [attractors_keys..., Inf]
### A Pluto.jl notebook ###
# v0.16.4
using Markdown
using InteractiveUtils
# This Pluto notebook uses @bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of @bind gives bound variables a default value (instead of an error).
macro bind(def, element)
quote
local el = $(esc(element))
@NoFishLikeIan
NoFishLikeIan / plotvectorfield.jl
Last active December 11, 2023 10:55
Small routine to plot vector fields
begin
function plotvectorfield(xs, ys, g::Function; plotkwargs...)
fig = plot()
plotvectorfield!(fig, xs, ys, g; plotkwargs...)
return fig
end
function plotvectorfield!(figure, xs, ys, g::Function; rescale = 1, plotkwargs...)
xlims = extrema(xs)
@NoFishLikeIan
NoFishLikeIan / longestitword.jl
Created August 19, 2021 13:46
Find "longest" Italian word!
using DelimitedFiles, Base.Iterators
alphabet = 'A':'Z'
n = length(alphabet)
alphabetdict = Dict(alphabet .=> 1:n)
function parsepair(str::String)
clean = strip(str, [' ', '|'])
l, r = only.(split(clean, ""))