Skip to content

Instantly share code, notes, and snippets.

View somacdivad's full-sized avatar

David Amos somacdivad

  • RelationalAI
  • Houston, TX
View GitHub Profile
@somacdivad
somacdivad / continued_fractions.py
Created December 11, 2023 02:16
Infinite periodic continued fractions
def periodic(L: list):
"""Return an infinite periodic continued fraction representation."""
# Create a copy of the list and append it to itself.
# This creates an "infinite" list of the form [a0, ..., an, [a0, ..., an, [a0, ..., an, ...]]]
# The list is not actually infinite, it only has length n+1! It has infinite depth and finite length 🤯
S = list(L)
S.append(S)
return S
# EXAMPLES:
import sys
import termios
import time
import tty
import random
def read_char():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
@somacdivad
somacdivad / permutations.ipynb
Created September 2, 2022 04:16
permutations.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
using Base.Iterators
RIGHT = (0, 1)
DOWN = (1, 0)
LEFT = (0, -1)
UP = (-1, 0)
function involute(n::Int)::Matrix{Int}
directions = cycle((RIGHT, DOWN, LEFT, UP))
repeats = pushfirst!(collect(flatten(zip(n-1:-1:1, n-1:-1:1))), n-1)
@somacdivad
somacdivad / lilac_chaser.jl
Created February 14, 2022 16:14
Julia code that reproduces Jeremy Hinton's "Lilac Chaser" illusion. See https://michaelbach.de/ot/col-lilacChaser/ for more information on how the illusion works.
using Javis
video = Video(500, 500)
N = 12
frames = 4(N+1)
Background(1:frames, (args...) -> background("grey70"))
# Draw crosshairs
Object(@JShape begin
using Animations
using ColorSchemes
using Javis
N = 6 # number of circles
radius = 20 # Radius of each circles
speed = 5 # degrees per frame
colors = ColorSchemes.rainbow1
video = Video(500, 500)
using ColorSchemes
using Javis
N = 6 # Number of dots
radius = 20 # Radius of each dot
colors = ColorSchemes.rainbow1 # Color palette
speed = 10 # Degrees per frame
function draw_circle(frame, i, radius, speed)
# Angle of the direction vector for the dot
@somacdivad
somacdivad / circles.jl
Created February 8, 2022 09:58
Makes an animation of four circle growing and shrinking while changing color.
using Javis
using ColorSchemes
# Set the color scheme
# More options:
# https://juliagraphics.github.io/ColorSchemes.jl/stable/catalogue/
colors = ColorSchemes.cool
function circ(radius, color="white")
sethue(color) # Set the color of the circle
@somacdivad
somacdivad / musical_graphs.jl
Last active February 2, 2022 17:52
Six graphs on four vertices and their musical chords
import LinearAlgebra as LinAlg
import SparseArrays
import Graphs
import GraphRecipes: graphplot
import Plots: savefig
import WAV
function normalized_laplacian_matrix(g::Graphs.SimpleGraph)
@somacdivad
somacdivad / graph_music.jl
Created January 31, 2022 23:34
Create music from graphs by converting the eigenvalues of the normalized Laplacian matrix to frequencies
import LinearAlgebra as LinAlg
import SparseArrays
import Graphs
import WAV
function normalized_laplacian_matrix(g::Graphs.SimpleGraph)
A = Graphs.CombinatorialAdjacency(Graphs.adjacency_matrix(g).+ 0.0)
 = Graphs.NormalizedAdjacency(A)