CoCalc Shared FilesLinear Algebra / Other Fractals.html
Author: Peter Francis
Views : 16
(File too big to render with math typesetting.)
Other Fractals
In [1]:
%load_ext sage
pretty_print_default(True)
latex.matrix_delimiters("[", "]")

In [2]:
Box = column_matrix(QQ, [ [0, 0, 1], [1, 0, 1], [1, 1, 1], [0, 1, 1], [0, 0, 1], [1/8, 1/8, 1], [1/8-1/16, 1/8+1/16, 1] ])

def PlotFigures(Figures, IncludeAxes, FigSize, Color):
Plot = []
for M in Figures:
P = M.columns()
Plot = Plot + [line([ [P[i][0], P[i][1]], [P[i+1][0], P[i+1][1]] ], color=Color) for i in [0..len(P)-2]]
show(sum(Plot), axes=IncludeAxes, aspect_ratio=1, figsize=FigSize)

def PlotFiguresT(Figures, IncludeAxes, FigSize, Color, ThicknessStart, ratioLTOne):
Plot = []
Thickness=ThicknessStart
for M in Figures:
P = M.columns()
Thickness = Thickness*ratioLTOne
Plot = Plot + [line([ [P[i][0], P[i][1]], [P[i+1][0], P[i+1][1]] ], color=Color,thickness=Thickness) for i in [0..len(P)-2]]
show(sum(Plot), axes=IncludeAxes, aspect_ratio=1, figsize=FigSize)

def Scale(s): return matrix(RR, [
[s, 0, 0],
[0, s, 0],
[0, 0, 1]
])

def Translate(a, b): return matrix(RR, [
[1, 0, a],
[0, 1, b],
[0, 0, 1]
])

def Transform(Figures, Transformations):
New_Figures = []

for M in Figures:
for T in Transformations:
New_Figures = New_Figures + [T*M]

return New_Figures

def Generate(n, Figures, Transformations):
Output_Figures = Figures
for i in [1..n]:
Output_Figures = Transform(Output_Figures, Transformations)
return Output_Figures

def PlotPointFigures(Point_Figures, IncludeAxes, PointSize, FigSize, Color):
Points = [[P.columns()[0][0], P.columns()[0][1]] for P in Point_Figures]
show(points(Points, pointsize=PointSize, color=Color), axes=IncludeAxes, aspect_ratio=1, figsize=FigSize)

# Don't worry at all about how the function OpNorm (below) works. It's used in part F.

def OpNorm(A):
G = matrix(RR, 2, [A[0,0], A[0,1], A[1,0], A[1,1]]);
return N(sqrt(max([x for x in (G * G.transpose()).eigenvalues()])))

# Rotation by angle 'theta' (counter-clockwise about the origin).

def Rotate(theta): return matrix(RR, [
[cos(theta), -sin(theta), 0],
[sin(theta), cos(theta), 0],
[0, 0, 1]
])

# Shearing in the x and y directions, each with shear factor 't'.

def ShearX(t):return matrix(RR,[[1, t, 0],
[0,1, 0],
[0, 0, 1]
])
def ShearY(t): return matrix(RR,[[1, 0, 0],
[t,1, 0],
[0, 0, 1]
])

# Scale by 's' in the x direction and by 't' in the y direction.

def ScaleXY(s, t): return matrix(RR,[[s, 0, 0],
[0,t, 0],
[0, 0, 1]
])

def GenerateRandom(n, Figure, Transformations):
Output_Figures = Figure
for i in [1..n]:
set_random_seed()
RandomTransIndex = floor(random()*len(Transformations))
CurrentNumTrans = len(Output_Figures)
Output_Figures = Output_Figures + [Transformations[RandomTransIndex]*Output_Figures[CurrentNumTrans - 1]]
return Output_Figures


Tree

In [3]:
Tree = column_matrix(RR, [ [0,0, 1], [0, 1, 1]])

T_1 = Translate(0,1)*Rotate(pi/12)*Scale(2/3)
T_2 = Translate(0,1)*Rotate(-pi/3)*Scale(2/3)
T_3 = Scale(1/2)
T_4 = Translate(0,0.5)*Scale(1/2)

T = [T_1, T_2, T_3, T_4]

PlotPointFigures(Generate(7, [Tree], T), False, 2, 10, 'blue')

Out[3]:

Cleft

In [3]:
Cleft = column_matrix(RR, [ [0,0,1],[1,1,1]])

C_1 = Translate(0,1)*Rotate(-pi/4)*Scale(1/sqrt(2))
C_2 = Rotate(pi/4)*Scale(1/sqrt(2))

C = [C_1, C_2]

PlotPointFigures(Generate(15, [Cleft], C), False, 1, 10, 'blue')

Out[3]:

Bolt

In [4]:
Bolt = column_matrix(RR, [ [0.5,1, 1], [-0.5, 0, 1], [0.5,0, 1], [-0.5, -1, 1]])

B_1 = Translate(0,0.5)*Rotate(-pi/10)*Scale(sqrt(2/5))
B_2 = Rotate(13*pi/20)*Scale(sqrt(1/5))
B_3 = Translate(0,-0.5)*Rotate(pi-pi/10)*Scale(sqrt(2/5))

B = [B_1, B_2, B_3]

PlotPointFigures(Generate(10, [Bolt], B), False, 1, 10, 'blue')

Out[4]:

Epsilon

In [5]:
Epsilon = column_matrix(RR, [ [0.5,1, 1], [-0.5, 0, 1], [0.5,0, 1], [-0.5, -1, 1]])

E_1 = Translate(0,0.5)*Rotate(-pi/3)*Scale(1/2)
E_2 = Rotate(pi/2)*Scale(1/2)
E_3 = Translate(0,-0.5)*Rotate(pi/3)*Scale(1/2)

E = [E_1, E_2, E_3]

PlotFigures(Generate(7, [Epsilon], E), False, 10, 'blue')

Out[5]: