-
-
Save MPrometheus69/896b315f97955de369c88d4372300200 to your computer and use it in GitHub Desktop.
MBM Logic Block
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import numpy as np | |
| class MBMLogicSystem: | |
| """ | |
| A system to evaluate the validity and stability of a Membrane-Based Model (MBM) | |
| by checking various physical and mathematical conditions. | |
| """ | |
| def __init__(self, membrane_tension_threshold: float = 1e-5, entropy_max: float = 1e3, collapse_threshold: float = 0.01): | |
| """ | |
| Initializes the MBMLogicSystem with configurable thresholds. | |
| Args: | |
| membrane_tension_threshold (float): The maximum allowable absolute difference for recursive validity | |
| and the maximum allowable absolute tension for membrane stability. | |
| entropy_max (float): The maximum allowed entropy value. | |
| collapse_threshold (float): The maximum allowed probability of collapse. | |
| """ | |
| if not isinstance(membrane_tension_threshold, (int, float)) or membrane_tension_threshold <= 0: | |
| raise ValueError("membrane_tension_threshold must be a positive number.") | |
| if not isinstance(entropy_max, (int, float)) or entropy_max <= 0: | |
| raise ValueError("entropy_max must be a positive number.") | |
| if not isinstance(collapse_threshold, (int, float)) or not (0 <= collapse_threshold <= 1): | |
| raise ValueError("collapse_threshold must be a number between 0 and 1.") | |
| self.membrane_tension_threshold = membrane_tension_threshold | |
| self.entropy_max = entropy_max | |
| self.collapse_threshold = collapse_threshold | |
| def recursive_validity(self, previous_layer_data: np.ndarray, current_layer_data: np.ndarray) -> bool: | |
| """ | |
| Checks if the difference between current and previous layer data is within | |
| the membrane tension threshold, indicating recursive validity. | |
| Args: | |
| previous_layer_data (np.ndarray): Data from the previous layer. | |
| current_layer_data (np.ndarray): Data from the current layer. | |
| Returns: | |
| bool: True if the recursive validity condition is met, False otherwise. | |
| """ | |
| if not isinstance(previous_layer_data, np.ndarray) or not isinstance(current_layer_data, np.ndarray): | |
| raise TypeError("Layer data must be NumPy arrays.") | |
| if previous_layer_data.shape != current_layer_data.shape: | |
| raise ValueError("Previous and current layer data must have the same shape.") | |
| delta = np.abs(current_layer_data - previous_layer_data) | |
| return np.all(delta < self.membrane_tension_threshold) | |
| def membrane_stability(self, tension_values: list[float] | np.ndarray) -> bool: | |
| """ | |
| Determines if the membrane is stable based on individual tension values. | |
| Args: | |
| tension_values (list[float] | np.ndarray): A list or NumPy array of tension values. | |
| Returns: | |
| bool: True if all tension values are within the membrane tension threshold, False otherwise. | |
| """ | |
| if not isinstance(tension_values, (list, np.ndarray)): | |
| raise TypeError("Tension values must be a list or NumPy array.") | |
| if not all(isinstance(t, (int, float)) for t in tension_values): | |
| raise ValueError("All tension values must be numeric.") | |
| return all(abs(tau) < self.membrane_tension_threshold for tau in tension_values) | |
| def entropy_within_bounds(self, entropy: float) -> bool: | |
| """ | |
| Checks if the given entropy value is within the acceptable bounds (0 to entropy_max). | |
| Args: | |
| entropy (float): The entropy value to check. | |
| Returns: | |
| bool: True if entropy is within bounds, False otherwise. | |
| """ | |
| if not isinstance(entropy, (int, float)): | |
| raise TypeError("Entropy must be a numeric value.") | |
| return 0 <= entropy <= self.entropy_max | |
| def energy_symmetry_check(self, energy_tensor: np.ndarray) -> bool: | |
| """ | |
| Verifies if the energy tensor is symmetric, which is often a requirement | |
| in physical systems to ensure conservation laws. | |
| Args: | |
| energy_tensor (np.ndarray): The energy tensor to check for symmetry. | |
| Returns: | |
| bool: True if the tensor is symmetric within a small tolerance, False otherwise. | |
| """ | |
| if not isinstance(energy_tensor, np.ndarray): | |
| raise TypeError("Energy tensor must be a NumPy array.") | |
| if energy_tensor.ndim != 2 or energy_tensor.shape[0] != energy_tensor.shape[1]: | |
| raise ValueError("Energy tensor must be a square 2D NumPy array.") | |
| return np.allclose(energy_tensor, energy_tensor.T, atol=1e-10) | |
| def collapse_probability_check(self, P_collapse: float) -> bool: | |
| """ | |
| Checks if the probability of collapse is within the defined acceptable range. | |
| Args: | |
| P_collapse (float): The probability of collapse. | |
| Returns: | |
| bool: True if the collapse probability is within the threshold, False otherwise. | |
| """ | |
| if not isinstance(P_collapse, (int, float)): | |
| raise TypeError("Collapse probability must be a numeric value.") | |
| return 0.0 <= P_collapse <= self.collapse_threshold | |
| def mbm_check_all(self, | |
| previous_layer_data: np.ndarray, | |
| current_layer_data: np.ndarray, | |
| tension_values: list[float] | np.ndarray, | |
| entropy: float, | |
| energy_tensor: np.ndarray, | |
| P_collapse: float) -> tuple[bool, dict[str, bool]]: | |
| """ | |
| Performs all defined checks for the Membrane-Based Model. | |
| Args: | |
| previous_layer_data (np.ndarray): Data from the previous layer. | |
| current_layer_data (np.ndarray): Data from the current layer. | |
| tension_values (list[float] | np.ndarray): Tension values for membrane stability. | |
| entropy (float): The entropy value. | |
| energy_tensor (np.ndarray): The energy tensor. | |
| P_collapse (float): The probability of collapse. | |
| Returns: | |
| tuple[bool, dict[str, bool]]: A tuple containing: | |
| - bool: True if all checks pass, False otherwise. | |
| - dict[str, bool]: A dictionary with the results of each individual check. | |
| """ | |
| checks = { | |
| "Recursive Validity": self.recursive_validity(previous_layer_data, current_layer_data), | |
| "Membrane Stability": self.membrane_stability(tension_values), | |
| "Entropy Bounds": self.entropy_within_bounds(entropy), | |
| "Energy Symmetry": self.energy_symmetry_check(energy_tensor), | |
| "Collapse Probability Valid": self.collapse_probability_check(P_collapse), | |
| } | |
| passed = all(checks.values()) | |
| return passed, checks | |
| ### πΉ Example Usage | |
| if __name__ == "__main__": | |
| print("--- MBM Logic System Example ---") | |
| # Initialize the system | |
| mbm = MBMLogicSystem(membrane_tension_threshold=1e-5, entropy_max=1e3, collapse_threshold=0.01) | |
| # --- Example 1: All checks should pass --- | |
| print("\n--- Test Case 1: All checks expected to PASSED ---") | |
| prev_layer_good = np.array([0.01, 0.015, 0.02]) | |
| curr_layer_good = np.array([0.010001, 0.015002, 0.020003]) # Small differences | |
| tension_good = [1e-6, 5e-6, 2e-6] # Within threshold | |
| entropy_val_good = 0.75 # Within bounds | |
| energy_tensor_good = np.array([[1.0, 0.01, 0.0], | |
| [0.01, 1.0, 0.01], | |
| [0.0, 0.01, 1.0]]) # Symmetric | |
| P_collapse_good = 0.005 # Within threshold | |
| passed_good, results_good = mbm.mbm_check_all( | |
| prev_layer_good, curr_layer_good, tension_good, entropy_val_good, energy_tensor_good, P_collapse_good | |
| ) | |
| print("MBM Logic Check (Good Case):", "β PASSED" if passed_good else "β FAILED") | |
| for key, value in results_good.items(): | |
| print(f" {key}: {'β' if value else 'β'}") | |
| # --- Example 2: Some checks should fail --- | |
| print("\n--- Test Case 2: Some checks expected to FAILED ---") | |
| prev_layer_bad = np.array([0.01, 0.015, 0.02]) | |
| curr_layer_bad = np.array([0.012, 0.014, 0.023]) # Larger difference, will fail recursive_validity | |
| tension_bad = [1e-6, 5e-5, 2e-6] # One value exceeds threshold | |
| entropy_val_bad = 1500.0 # Exceeds max entropy | |
| energy_tensor_bad = np.array([[1.0, 0.05, 0.0], | |
| [0.01, 1.0, 0.01], | |
| [0.0, 0.01, 1.0]]) # Not symmetric (0.05 vs 0.01) | |
| P_collapse_bad = 0.02 # Exceeds threshold | |
| passed_bad, results_bad = mbm.mbm_check_all( | |
| prev_layer_bad, curr_layer_bad, tension_bad, entropy_val_bad, energy_tensor_bad, P_collapse_bad | |
| ) | |
| print("MBM Logic Check (Bad Case):", "β PASSED" if passed_bad else "β FAILED") | |
| for key, value in results_bad.items(): | |
| print(f" {key}: {'β' if value else 'β'}") | |
| # --- Example 3: Edge cases / Invalid Inputs --- | |
| print("\n--- Test Case 3: Edge cases / Invalid Inputs (demonstrating error handling) ---") | |
| try: | |
| MBMLogicSystem(membrane_tension_threshold=-1e-5) | |
| except ValueError as e: | |
| print(f" Caught expected error for invalid membrane_tension_threshold: {e}") | |
| try: | |
| mbm.recursive_validity(np.array([1,2]), "not an array") | |
| except TypeError as e: | |
| print(f" Caught expected error for invalid current_layer_data type: {e}") | |
| try: | |
| mbm.energy_symmetry_check(np.array([[1,2,3]])) # Not square | |
| except ValueError as e: | |
| print(f" Caught expected error for non-square energy tensor: {e}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment