Posts
Author Archives: Linuxgal
Cmdlog
List bash commands by frequency #!/usr/bin/env python3 import os history_file = os.path.expanduser(“~/.bash_history”) counts = {} with open(history_file, “r”, errors=”ignore”) as f: for line in f: line = line.strip() if not line or line.startswith(“#”): continue cmdline = line.split(“|”, 1)[0].strip() parts = … Continue reading
Posted in Uncategorized
Leave a comment
Commands
Top 20 bash commands in Python #!/usr/bin/env python3 from collections import Counter import matplotlib.pyplot as plt import os history_file = os.path.expanduser(“~/.bash_history”) counter = Counter() with open(history_file, “r”, errors=”ignore”) as f: for line in f: line = line.strip() if not line … Continue reading
Posted in Uncategorized
Leave a comment
Phi
Calculate phi with bc and bash #!/usr/bin/env bash a=0 b=1 for((i=2;i<=23;i++));{ c=$((a+b)) printf "Fibonacci(%d) / Fibonacci(%d) = %.20f\n" $i $((i-1)) "$(bc -l<<<"$c/$b")" a=$b b=$c }
Posted in Uncategorized
Leave a comment
Weierstrass Monster
Generating the Weierstrass Monster with #Python This is pathology of calculus that is continuous everywhere but differentiable nowhere. Henri Poincaré condemned it as “an outrage against common sense.” Charles Hermite called it a “deplorable evil.” Muahahahaha! #!/usr/bin/env python3 import numpy … Continue reading
Posted in Uncategorized
Leave a comment
Invsum
Calculate parallel resistance (or series capacitance) with Python #!/usr/bin/env python3 import sys,numpy as n print(sys.argv[1:],’->’,1/(1/n.array(sys.argv[1:],float)).sum())
Posted in Uncategorized
Leave a comment
Intersect
Solve for the intersection of two lines from four points with Python #!/usr/bin/env python3 import sys,numpy as n p,q,r,s=map(n.array,((float(sys.argv[i]),float(sys.argv[i+1])) for i in (1,3,5,7))) M=n.c_[q-p,s-r] d=n.linalg.det(M) if abs(d)<1e-10: print("Coincident" if abs(n.cross(q-p,r-p))<1e-10 else "Parallel") else: t,_=n.linalg.solve(M,r-p) print(p+t*(q-p))
Posted in Uncategorized
Leave a comment
Square
Closing in on a square wave with the Fourier series under 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 … Continue reading
Posted in Uncategorized
Leave a comment
Deriv
Plotting a trig function and its derivative with Python #!/usr/bin/env python3 import numpy as n,matplotlib.pyplot as p x=n.linspace(-4,4,400) f=n.arctan(x**2);df=2*x/(x**4+1) p.plot(x,f,’b’,label=’f(x)=arctan(x²)’) p.plot(x,df,’r’,label=”f'(x)=2x/(x⁴+1)”) p.axhline(0,c=’k’,lw=.5);p.axvline(0,c=’k’,lw=.5) p.ylim(-2,2);p.xlabel(‘x’);p.ylabel(‘y’) p.title(“f(x) and f'(x)”) p.grid(ls=’:’,alpha=.7);p.legend();p.show()
Posted in Uncategorized
Leave a comment
Gamma
The gamma function with Python #!/usr/bin/env python3 from scipy.special import gamma a,b=.1,2 n=25 p=(5**.5-1)/2 c=b-p*(b-a) d=a+p*(b-a) fc,fd=gamma(c),gamma(d) print(f'{“Iter”:>4} {“x”:>12} {“gamma(x)”:>12}’) for i in range(1,n+1): if fc<fd: b,d,fd=d,c,fc c=b-p*(b-a) fc=gamma(c) x,f=c,fc else: a,c,fc=c,d,fd d=a+p*(b-a) fd=gamma(d) x,f=d,fd print(f"{i:4d} {x:12.8f} {f:12.8f}") print(f"\nLocal minimum … Continue reading
Posted in Uncategorized
Leave a comment