import numpy as np
import matplotlib.pyplot as plt
from numpy import linalg as LA
import pandas as pd
def alea(low=0.0,high=1.0):
return np.random.uniform(low,high)
def puntoAleatorio():
return np.array([
alea(),alea()
]
)
# semejanzas
A = np.array([
[1/2,0],[0,1/2]
])
B1 = np.array([0,0])
B2 = np.array([1/2,0])
B3 = 1/2*np.array([np.cos(np.pi/3),np.sin(np.pi/3)])
B = [B1,B2,B3]
listaPuntos = []
x = puntoAleatorio()
listaPuntos.append(x)
iteraciones = 5000
for _ in np.arange(iteraciones):
x = A.dot(x)
al = alea()
if al<=1/3:
x +=B[0]
elif al<=2/3:
x +=B[1]
else:
x +=B[2]
listaPuntos.append(x)
coordX = [l[0] for l in listaPuntos]
coordY = [l[1] for l in listaPuntos]
f,ax = plt.subplots(nrows=1,ncols=1,figsize=(8,8))
ax.plot(coordX,coordY,'.',markersize=0.5)
[<matplotlib.lines.Line2D at 0x7fd5c08703a0>]
Otra manera
listaPuntos = []
x = puntoAleatorio()
listaPuntos.append(x)
iteraciones = 5000
for _ in np.arange(iteraciones):
x = A.dot(x)+B[np.random.choice(3)]
listaPuntos.append(x)
coordX = [l[0] for l in listaPuntos]
coordY = [l[1] for l in listaPuntos]
f,ax = plt.subplots(nrows=1,ncols=1,figsize=(8,8))
ax.plot(coordX,coordY,'.',markersize=0.5)
[<matplotlib.lines.Line2D at 0x7fd5c088f760>]
puntos = pd.DataFrame(columns=['X','Y'])
puntos.loc[:,'X'] = coordX
puntos.loc[:,'Y'] = coordY
puntos.to_csv('listaPuntosSierpinski.csv',sep=';')
puntos.head()
X | Y | |
---|---|---|
0 | 0.207282 | 0.559435 |
1 | 0.603641 | 0.279718 |
2 | 0.801820 | 0.139859 |
3 | 0.650910 | 0.502942 |
4 | 0.825455 | 0.251471 |
Usamos simetrías del tipo
$$ f_i(x,y) = \begin{pmatrix}A_i&B_i\\C_i&D_i\end{pmatrix} \begin{pmatrix}x\\y\end{pmatrix}+ \begin{pmatrix}E_i\\F_i\end{pmatrix} $$Las simetrías vienen definidas en la tabla:
f | $A$ | $B$ | $C$ | $D$ | $E$ | $F$ | Prob |
---|---|---|---|---|---|---|---|
1 | 0 | 0 | 0 | 0.25 | 0.5 | 0 | 0.03 |
2 | 0.21 | 0.35 | -0.21 | 0.35 | 0.39 | 0.23 | 0.18 |
3 | 0.15 | -0.34 | 0.26 | 0.2 | 0.425 | -0.005 | 0.14 |
4 | 0.75 | 0 | 0 | 0.75 | 0.125 | 0.25 | 0.65 |
# semejanzas
A1 = np.array([
[0,0],[0,1/4]
])
A2 = np.array([
[0.21,0.35],[-0.21,0.35]
])
A3 = np.array([
[0.15,-0.34],[0.26,0.2]
])
A4 = np.array([
[3/4,0],[0,3/4]
])
A = [A1,A2,A3,A4]
B1 = np.array([0.5,0])
B2 = np.array([0.39,0.23])
B3 = np.array([0.425,-0.005])
B4 = np.array([1/8,1/4])
B = [B1,B2,B3,B4]
listaPuntos = []
x = puntoAleatorio()
listaPuntos.append(x)
iteraciones = 10000
for _ in np.arange(iteraciones):
choice = np.random.choice(4,p=[0.03,0.18,0.14,0.65])
x = A[choice].dot(x)+B[choice]
listaPuntos.append(x)
coordX = [l[0] for l in listaPuntos]
coordY = [l[1] for l in listaPuntos]
f,ax = plt.subplots(nrows=1,ncols=1,figsize=(8,8))
ax.plot(coordX,coordY,'.',markersize=0.5)
[<matplotlib.lines.Line2D at 0x7fd5c06c2970>]