|
import numpy as np |
|
import matplotlib.pyplot as plt |
|
|
|
class HeatLogicGates: |
|
def __init__(self, grid_size=50): |
|
self.grid_size = grid_size |
|
self.T = np.zeros((grid_size, grid_size)) # Initialize heat field |
|
|
|
def apply_vortex(self, x, y, strength=1.0): |
|
"""Create a vortex heat structure at a given location""" |
|
for i in range(self.grid_size): |
|
for j in range(self.grid_size): |
|
dx, dy = i - x, j - y |
|
self.T[i, j] += strength * np.exp(-0.1 * (dx**2 + dy**2)) # Gaussian decay |
|
|
|
def nand_gate(self, A, B): |
|
"""Implements a NAND gate using thermal diffusion""" |
|
self.apply_vortex(10, 10, A) |
|
self.apply_vortex(20, 20, B) |
|
|
|
# Compute heat diffusion (simulating logical transition) |
|
output = 1 - np.clip(self.T[15, 15] / max(A, B, 1), 0, 1) |
|
return round(output) |
|
|
|
def or_gate(self, A, B): |
|
"""Implements an OR gate using heat flow""" |
|
self.apply_vortex(10, 10, A) |
|
self.apply_vortex(20, 20, B) |
|
|
|
# OR logic: If either has heat, the output is 1 |
|
output = np.clip((self.T[15, 15] + self.T[25, 25]) / 2, 0, 1) |
|
return round(output) |
|
|
|
def and_gate(self, A, B): |
|
"""Implements an AND gate using vortex interactions""" |
|
self.apply_vortex(10, 10, A) |
|
self.apply_vortex(20, 20, B) |
|
|
|
# AND logic: If both have heat, the output is high |
|
output = np.clip((self.T[15, 15] * self.T[25, 25]) / 2, 0, 1) |
|
return round(output) |
|
|
|
def visualize(self): |
|
"""Plot the heatmap for logic gate processing""" |
|
plt.imshow(self.T, cmap='hot', interpolation='nearest') |
|
plt.colorbar() |
|
plt.show() |
|
class HeatCompiler: |
|
def __init__(self): |
|
self.heat_logic = HeatLogicGates() |
|
|
|
def parse_instruction(self, instruction): |
|
"""Parses a single instruction and executes it""" |
|
parts = instruction.strip().split() |
|
if parts[0] == "LOAD": |
|
self.heat_logic.apply_vortex(int(parts[1]), int(parts[2]), float(parts[3])) |
|
elif parts[0] == "NAND": |
|
return self.heat_logic.nand_gate(int(parts[1]), int(parts[2])) |
|
elif parts[0] == "OR": |
|
return self.heat_logic.or_gate(int(parts[1]), int(parts[2])) |
|
elif parts[0] == "AND": |
|
return self.heat_logic.and_gate(int(parts[1]), int(parts[2])) |
|
elif parts[0] == "READ": |
|
x, y = int(parts[1]), int(parts[2]) |
|
return self.heat_logic.T[x, y] |
|
else: |
|
raise ValueError(f"Unknown instruction: {instruction}") |
|
|
|
def run_program(self, program): |
|
"""Executes a list of instructions""" |
|
results = [] |
|
for instruction in program: |
|
result = self.parse_instruction(instruction) |
|
if result is not None: |
|
results.append(result) |
|
self.heat_logic.visualize() |
|
return results |
|
|
|
# Example program |
|
heat_program = [ |
|
"LOAD 10 10 1.0", |
|
"LOAD 20 20 1.0", |
|
"NAND 1 1", |
|
"OR 0 1", |
|
"AND 1 1" |
|
] |
|
|
|
compiler = HeatCompiler() |
|
results = compiler.run_program(heat_program) |
|
print("Program Results:", results) |
|
|
|
|
|
# Example NAND, OR, AND Gate Execution |
|
logic = HeatLogicGates() |
|
print(f"NAND(1,1): {logic.nand_gate(1, 1)}") |
|
print(f"OR(0,1): {logic.or_gate(0, 1)}") |
|
print(f"AND(1,1): {logic.and_gate(1, 1)}") |
|
logic.visualize() |
|
|