Category Archives: Uncategorized

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

KMPlots

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

Linalg

Python for all your linear algebra homework: #!/usr/bin/env python3 import sys,ast,numpy as np a=np.array(ast.literal_eval(sys.argv[1])) print(“Matrix:”) print(np.matrix(a)) print(“\nTranspose:”) print(np.matrix(a.T)) print(“\nTrace:”,a.trace()) print(“\nRank:”,np.linalg.matrix_rank(a)) if a.shape[0]==a.shape[1]: print(“\nDeterminant:”,round(np.linalg.det(a),6)) d,e=np.linalg.eig(a) print(“\nEigenvalues:”,np.round(d,6)) print(“\nEigenvectors:”) print(np.round(e,6)) print(“\nPseudo-inverse:”) print(np.matrix(np.linalg.pinv(a)))

Posted in Uncategorized | Leave a comment

Italics

Convert text to italics with Python #!/usr/bin/env python3 import sys a=”ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz” b=”𝘈𝘉𝘊𝘋𝘌𝘍𝘎𝘏𝘐𝘑𝘒𝘓𝘔𝘕𝘖𝘗𝘘𝘙𝘚𝘛𝘜𝘝𝘞𝘟𝘠𝘡𝘢𝘣𝘤𝘥𝘦𝘧𝘨𝘩𝘪𝘫𝘬𝘭𝘮𝘯𝘰𝘱𝘲𝘳𝘴𝘵𝘶𝘷𝘸𝘹𝘺𝘻” print(” “.join(sys.argv[1:]).translate(str.maketrans(a,b))) #!/usr/bin/env python3 import sys c=”ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789″ b=”𝗔𝗕𝗖𝗗𝗘𝗙𝗚𝗛𝗜𝗝𝗞𝗟𝗠𝗡𝗢𝗣𝗤𝗥𝗦𝗧𝗨𝗩𝗪𝗫𝗬𝗭𝗮𝗯𝗰𝗱𝗲𝗳𝗴𝗵𝗶𝗷𝗸𝗹𝗺𝗻𝗼𝗽𝗾𝗿𝘀𝘁𝘂𝘃𝘄𝘅𝘆𝘇𝟬𝟭𝟮𝟯𝟰𝟱𝟲𝟳𝟴𝟵” print(“”.join(b[c.find(x)] if x in c else x for x in ” “.join(sys.argv[1:])))

Posted in Uncategorized | Leave a comment