Skip to content

Instantly share code, notes, and snippets.

@dmishin
dmishin / coprime_fft.py
Created October 23, 2014 13:57
Generate Fourier transform of the coprime integers map
#Requires: numpy, PIL
from fractions import gcd
from numpy import array, meshgrid, frompyfunc, clip, histogram
from numpy.fft import fft2
import numpy as np
from PIL import Image
def value_diapason(x, percent=0.95, nbins=100):
"""Use histogram to determine area, covering 95% of values"""
counts, bins = histogram(x.ravel(),nbins)
@dmishin
dmishin / sumsin_mpmath.py
Created October 31, 2015 11:50
Experiments with S(x) = sin(x) + sin(2x)/2 + sin(4x)/4 + ...
"""Experiments with the 'sumsin' function: maximization and plotting
S(x) = sin(x) + sin(2x)/2 + sin(4x)/4 + ...
it appears that S(x) reaches maximum at
xmax = 0.8905302010175791857059461558*7027*
whereas
36pi/127=0.8905302010175791857059461558*9025*
Plots of the function:
#!/usr/bin/env python
#This is Python3 executable
import os.path
from numpy import array, hstack, vstack, clip
from PIL import Image
import numpy as np
import subprocess
import re
#SOX_EXEC = r"C:\Program Files\sox-14-4-2\sox.exe"
@dmishin
dmishin / hilbert_image2audio.py
Created April 29, 2016 18:45
Unwrap imgage pixels into a 1-dimensional array along the Hilbert's curve, then convert it to sound using SOX
#!/usr/bin/env python
from __future__ import print_function, division
import os.path
from numpy import array, hstack, vstack, clip
from PIL import Image
import numpy as np
import subprocess # need the subprocess module
from tempfile import mkdtemp
import shutil
@dmishin
dmishin / approx_fractional_iterate.py
Last active November 22, 2018 12:37
Numerically calculate fractional iterate of x^2-2
def approxfi(x, n):
#Apply x -> F(x) mapping until x is sufficiently large
k = 0
while (x < 1e14) and (k<100):
x = x**2-2
k += 1
#Calculate approximate fractional iterate for large argument
x = x**(2**n)
#Apply reverse mapping x -> F[-1](x)
for _ in range(k):
@dmishin
dmishin / schreiber.py
Created November 18, 2019 11:22
Detect optimal step for minimal order Schreiber test signal
import numpy as np
import scipy as sp
import scipy.linalg
from math import floor, pi
#Detect optimal step for minimal order Schreiber test signal
#Set to False to disable printing
_verbose = True
@dmishin
dmishin / check_invfib_sum.py
Created April 22, 2020 09:35
Numerically checking surprising identities regarding the sums involving Fibonacci numbers
#%metadata%
#url: https://twitter.com/diegorattaggi/status/1252654580720119810/photo/1
from sympy import *
mfib = Matrix([[0,1],[1,1]])
fib0 = Matrix([0,1])
luc0 = Matrix([2,1])
eye = Matrix([[1,0],[0,1]])
def fibo(n):
$("#run").click(() => tryCatch(run));
async function run() {
Excel.run(async function (context) {
var sctivesheet = context.workbook.worksheets.getActiveWorksheet();
var table = sctivesheet.tables.add(sctivesheet.getRangeByIndexes(0, 0, 1, 2), true);
var data = [[1, "A"], [2, "B"], [3, "C"]];
table.rows.add(0, data);
@dmishin
dmishin / thue_morse_hilbert_fft.py
Created October 27, 2022 11:43
Visualize Fourier transform of the Thue-Morse sequence arranged along the Hilbert curve
import numpy as np
from matplotlib import pyplot as plt
import scipy
def ht(n:int)->np.array:
"""Generates Thue-Morse sequence, arranged along the Hilbert curve of order N. Result is (2**n, 2**n) matrix"""
if n < 0: raise ValueError("Bad order")
if n == 0:
return np.array([[1]], dtype=np.int8)
else:
@dmishin
dmishin / grotsen_iterates.py
Created June 28, 2023 21:05
Calculating and plotting smooth fractional iterates of 1-sqrt(1-x^2)
"""Calculate smooth iterates of the function
f:[0,1]->[0,1]
f(x) = 1-sqrt(1-x^2)
https://mathoverflow.net/questions/449748/what-are-the-iterates-of-x-mapsto-1-sqrt1-x2
https://twitter.com/gro_tsen/status/1674132770203881477