Weierstrass

There’s something wonderfully unsettling about a function that has no tangent anywhere, no matter how far you zoom in.

#!/usr/bin/env python3
import numpy as n,matplotlib.pyplot as p
a=.5;b=11;n_terms=50
x=n.linspace(-2,2,20000)
f=sum(a**k*n.cos(b**k*n.pi*x) for k in range(n_terms))
p.plot(x,f,'b',lw=.1)
p.title('Weierstrass Function')
p.grid()
p.show()

Posted in Uncategorized | Leave a comment

Vidir

Vidir (part of Debian’s moreutils) lets me edit the names of files in a directory in vim as though the directory was just a file, which it really is.

Posted in Uncategorized | Leave a comment

Colrm

Remove columns 1 through 9, and from column 48 to the end of the line:

cat starplot6.txt | colrm 1 9 | colrm 48

Posted in Uncategorized | Leave a comment

Hexes




Hexes

seq 0 255 | awk '{printf "%02x ", $1}' | xargs -n16




Posted in Uncategorized | Leave a comment

Morse

Morse Code with sed

#!/bin/sed -rf
s/.*/\U&/
s/$/\nA.-B-…C-.-.D-..E.F..-.G–.H….I..J.—K-.-L.-..M–N-.O—P.–.Q–.-R.-.S…T-U..-V…-W.–X-..-Y-.–Z–../
:a
s/([A-Z])([^\n]*\n.*\1([-.]+))/\3 \2/
ta
s/\n.*//

Posted in Uncategorized | Leave a comment

Fourier

Additive synthesis with Python

#!/usr/bin/env python3
import numpy as n,matplotlib.pyplot as p
x=n.linspace(0,2*n.pi,1000)
def s(x,t):
    k=n.arange(1,2*t,2)
    return 4/n.pi*(n.sin(k[:,None]*x)/k[:,None]).sum(0)
N=12
colors=p.cm.rainbow(n.linspace(0,1,N))
for i,t in enumerate(range(1,N+1)):
    p.plot(x,s(x,t),color=colors[i],label=f'{2*t-1} harmonics')
p.title('Fourier Series Approximation of a Square Wave')
p.grid()
p.legend()
p.show()

Posted in Uncategorized | Leave a comment

Prep

Remove ASCII art boxes from text

#!/usr/bin/env python3
import sys
import re
def prep(lines):
    cleaned = []
    for raw in lines:
        line = raw.rstrip("\n")
        if re.fullmatch(r"\+[-+]+\s*", line):
            continue
        if line.startswith("|"):
            line = line[1:]
        if line.endswith("|"):
            line = line[:-1]
        line = line.rstrip()
        if not line.strip():
            if cleaned and cleaned[-1] != "":
                cleaned.append("")
            continue
        line = line.lstrip()
        if cleaned and cleaned[-1] != "":
            cleaned[-1] += " " + line
        else:
            cleaned.append(line)
    return "\n".join(cleaned) + "\n"
if __name__ == "__main__":
    sys.stdout.write(prep(sys.stdin))

Posted in Uncategorized | Leave a comment

dfc

dfc (Disk Free Color)

Posted in Uncategorized | Leave a comment

Units

teresita:~$ units -1 -o "%.0f" "299792458 m/s" "furlongs per fortnight" 
	* 1802617499785
teresita:~$ 

Posted in Uncategorized | Leave a comment

Ellipse

#! /usr/bin/python3
import math
import sys
a = float(sys.argv[1])
b = float(sys.argv[2])
h = ((a - b) / (a + b))**2
eps = sys.float_info.epsilon
def binom_half(n):
    coef = 1.0
    for k in range(n):
        coef *= (0.5 - k) / (k + 1)
    return coef
perimeter = 0.0
n = 0
term = 1.0  
while abs(term) > eps:
    coef = binom_half(n)
    term = (coef**2) * h**(2*n)
    perimeter += term
    n += 1
perimeter *= math.pi * (a + b)
print(f"Ellipse perimeter: {perimeter:.17f}")
print(f"Series converged after {n} terms")

$ ellipse .387 .379
Ellipse perimeter: 2.40645997980731741
Series converged after 3 terms

Posted in Uncategorized | Leave a comment