<jupyter_start><jupyter_text>1st Subdivide<jupyter_code>import numpy as np

class Pt:
  def __init__(self):
    self.x = 0.0
    self.y = 0.0
  
  def __init__(self,x,y):
    self.x = x *1.0
    self.y = y *1.0

  def add(o1,o2):
    # print(o1,o2)
    new = Pt(o1.x + o2.x,o1.y + o2.y)
    return new

  def __add__(self, new):
    print(new)
    return Pt(self.x+new.x,self.y+new.y)

  def __truediv__(self,value):
    self.x /= value
    self.y /= value
  
  def __str__(self):
    return "("+str(self.x)+","+str(self.y)+")"

  def __repr__(self):
    return "("+str(self.x)+","+str(self.y)+")"

A = np.array([[Pt(-1,1),Pt(1,1)],[Pt(-1,-1),Pt(1,-1)]])
A
def square(mesh,x,y,step):
  half = step//2
  avg = mesh[x][y]+mesh[x+step][y]+mesh[x][y+step]+mesh[x+step][y+step]
  avg /= 4.0
  mesh[x+half][y+half] = avg
def diamond(mesh,x,y,step):
  half = step//2
  size = mesh.shape[0]-1
  avg = mesh[(x-half+size)%size][y]+ \
        mesh[(x+half)%size][y]+ \
        mesh[x][(y+half)%size]+ \
        mesh[x][(y-half+size)%size]
  avg /= 4.0
  mesh[x][y] = avg
  if x== 0:
    mesh[size][y]=avg
  if y== 0:
    mesh[x][size]=avg
def subdivide(mesh):
  new_mesh = np.empty((mesh.shape[0]*2-1,mesh.shape[1]*2-1),dtype=Pt)
  new_mesh.fill(Pt(0.0,0.0))
  new_mesh[0][0] = mesh[0][0]
  new_mesh[0][new_mesh.shape[1]-1]= mesh[0][mesh.shape[1]-1] 
  new_mesh[new_mesh.shape[0]-1][0]= mesh[mesh.shape[0]-1][0]
  new_mesh[new_mesh.shape[0]-1][new_mesh.shape[1]-1]= mesh[mesh.shape[0]-1][mesh.shape[1]-1]
  # print(new_mesh) 
  step = new_mesh.shape[0]-1
  while step>1: 
    for i in range(new_mesh.shape[0]-1,step):
      for j in range(new_mesh.shape[1]-1,step):
        diamond(new_mesh,i,j,step)
    for i in range(new_mesh.shape[0]-1,step//2):
      for j in range((x+step//2)%step,new_mesh.shape[1]-1,step):
        square(new_mesh,i,j,step)
    step //= 2
  return new_mesh
def subdivision(A,times):
  SA = A
  for i in range(times):
    SA = subdivide(SA)
  return SA

SA = subdivision(A,3)
# print(A)
# print(SA)<jupyter_output><empty_output><jupyter_text>2nd subdivide<jupyter_code>import random
 def squareStep(A,x,z,reach):
   count = 0
   avg = 0.0
   sx = int(A.shape[0])
   sz = int(A.shape[1])

   if (x - reach) >= 0 and (z - reach) >= 0:
     avg += A[x - reach][z - reach]
     count+=1
   if (x - reach) >= 0 and (z + reach) < sz:
     avg += A[x - reach][z + reach]
     count+=1
   if (x + reach) < sx and (z - reach) >= 0:
     avg += A[x + reach][z - reach]
     count+=1
   if (x + reach) < sx and (z + reach) < sz:
     avg += A[x + reach][z + reach]
     count+=1
   avg += random.random()*reach
   avg /= count
   A[x][z] = avg
def diamondStep(A,x,z,reach):
   count = 0
   avg = 0.0
   sx = int(A.shape[0])
   sz = int(A.shape[1])

   if (x - reach) >= 0 :
     avg += A[x - reach][z]
     count+=1
   if (x + reach) < sx:
     avg += A[x + reach][z]
     count+=1
   if (z - reach) >= 0:
     avg += A[x][z - reach]
     count+=1
   if (z + reach) < sz:
     avg += A[x][z + reach]
     count+=1
   avg += random.random()*reach
   avg /= count
   A[x][z] = avg
def diamondSquare(A,size):
  half = int(size/2)
  print(half)
  if half < 1:
    return
  sx = int(A.shape[0])
  sz = int(A.shape[1])
  
  # Square
  for z in range(half,sz,size):
    for x in range(half,sx,size):
      squareStep(A,x % sx, z % sz,half)

  # Diamond
  col = 0 
  for x in range(0,sx,half):
    col+=1
    if col % 2 == 1:
      for z in range(half,sz,size):
        diamondStep(A,x % sx, z % sz,half)
    else:
      for z in range(0,sz,size):
        diamondStep(A,x % sx, z % sz,half)
  diamondSquare(A,int(size / 2))
  return A
size = 50
A = np.array([[0.0 for i in range(size)] for j in range(size)])
SA = diamondSquare(A,size)
SA
import matplotlib.pyplot as plt
plt.imshow(SA)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.20392670157068063, 'fraction_numerical': 0.032460732984293195, 'mean_word_length': 2.805776892430279, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '290487', 'license': 'MIT', 'n_tokens_mistral': 1777, 'n_tokens_neox': 1693, 'n_words': 340, 'path': 'ipynb/Subdivision_test.ipynb', 'repo_name': 'Dooomino/python-thoughts', 'size': '3820'}