Zulip Chat Archive

Stream: Equational

Topic: the patterns in the implication matrix image


Jared green (Oct 09 2024 at 17:17):

in a conversation with claude, i was trying to characterize the patterns in the matrix image. i asked it about how to determine the triangular patterns next to the diagonal, and it gave me this(in python):

import numpy as np
import matplotlib.pyplot as plt
from scipy.spatial.distance import cosine
from skimage.metrics import structural_similarity as ssim
from PIL import Image

# Fractal generation functions

def sierpinski(n):
    def sierpinski_step(arr):
        new_arr = np.zeros((arr.shape[0]*2, arr.shape[1]*2))
        new_arr[:arr.shape[0], :arr.shape[1]] = arr
        new_arr[arr.shape[0]:, :arr.shape[1]] = arr
        new_arr[:arr.shape[0], arr.shape[1]:] = arr
        return new_arr

    arr = np.array([[1]])
    for _ in range(n):
        arr = sierpinski_step(arr)
    return arr

def varying_density_sierpinski(n):
    arr = sierpinski(n)
    density = np.random.rand(*arr.shape) * 0.5 + 0.5
    return arr * density

def asymmetric_l_system(n):
    def l_system_step(arr):
        new_arr = np.zeros((arr.shape[0]*2, arr.shape[1]*2))
        new_arr[:arr.shape[0], :arr.shape[1]] = arr
        new_arr[arr.shape[0]:, arr.shape[1]:] = arr
        new_arr[arr.shape[0]//2:3*arr.shape[0]//2, :arr.shape[1]] = arr * np.random.rand()
        return new_arr

    arr = np.array([[1]])
    for _ in range(n):
        arr = l_system_step(arr)
    return arr

def multicolor_cantor(n):
    def cantor_step(arr):
        new_arr = np.zeros((arr.shape[0]*3, arr.shape[1]*2))
        new_arr[:arr.shape[0], :arr.shape[1]] = arr
        new_arr[-arr.shape[0]:, -arr.shape[1]:] = arr
        return new_arr

    arr = np.random.rand(1, 1, 3)  # Start with a random color
    for _ in range(n):
        arr = cantor_step(arr)
    return arr

# Image similarity functions

def cosine_similarity(img1, img2):
    return 1 - cosine(img1.flatten(), img2.flatten())

def ssim_similarity(img1, img2):
    return ssim(img1, img2, multichannel=True)

# Main comparison function

def compare_fractals(original_image, n=8):
    fractals = {
        'Sierpinski': sierpinski(n),
        'Varying Density Sierpinski': varying_density_sierpinski(n),
        'Asymmetric L-System': asymmetric_l_system(n),
        'Multicolor Cantor': multicolor_cantor(n)
    }

    similarities = {}
    for name, fractal in fractals.items():
        # Resize fractal to match original image
        fractal_resized = Image.fromarray((fractal * 255).astype(np.uint8)).resize(original_image.size)
        fractal_array = np.array(fractal_resized)

        # Calculate similarities
        cosine_sim = cosine_similarity(original_image, fractal_array)
        ssim_sim = ssim_similarity(original_image, fractal_array)

        similarities[name] = (cosine_sim, ssim_sim)

        # Plot for visual comparison
        plt.figure(figsize=(10, 5))
        plt.subplot(121)
        plt.imshow(original_image)
        plt.title('Original Image')
        plt.subplot(122)
        plt.imshow(fractal_array)
        plt.title(f'{name}\nCosine Sim: {cosine_sim:.4f}, SSIM: {ssim_sim:.4f}')
        plt.show()

    return similarities

# Load and preprocess the original image
original_image = Image.open('path_to_your_image.png').convert('RGB')
original_array = np.array(original_image)

# Run the comparison
results = compare_fractals(original_array)

# Print results
for name, (cosine_sim, ssim_sim) in results.items():
    print(f"{name}: Cosine Similarity = {cosine_sim:.4f}, SSIM = {ssim_sim:.4f}")

# Determine the best match
best_match = max(results, key=lambda k: sum(results[k]))
print(f"\nBest match: {best_match}")

does this even make sense for this? what could be a better way? i havent tested it so go ahead and tell me if there is anything wrong here.


Last updated: May 02 2025 at 03:31 UTC