Created
December 8, 2024 16:10
-
-
Save manncodes/90954cedc72ce3239046f02f8ae92148 to your computer and use it in GitHub Desktop.
serpenski triangle integral solution: https://x.com/Anthony_Bonato/status/1865455964591120562
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 | |
| from typing import Tuple | |
| def f0(a: float, b: float) -> float: | |
| """ | |
| Implements the formula: | |
| f0(a,b) = (((a^4-1)^2-64)^2 - 2^7(1-(1-b^2)^2)^2) / 2^15 | |
| """ | |
| a_term = (a**4 - 1)**2 - 64 | |
| a_term = a_term**2 | |
| b_term = 1 - (1 - b**2)**2 | |
| b_term = b_term**2 * 2**7 | |
| result = (a_term - b_term) / 2**15 | |
| return result | |
| def fn(n: int, a: float, b: float, debug: bool = False) -> float: | |
| """ | |
| Calculates fn(a,b) recursively using: | |
| fn(a,b) = f(n-1)(f(n-1)(a,0), f(n-1)(1,b)) | |
| """ | |
| if n == 0: | |
| return f0(a, b) | |
| prev_fn = lambda x, y: fn(n-1, x, y) | |
| inner_1 = prev_fn(a, 0) # Calculate f(n-1)(a,0) | |
| inner_2 = prev_fn(1, b) # Calculate f(n-1)(1,b) | |
| result = prev_fn(inner_1, inner_2) # Combine them | |
| if debug: | |
| print(f"n={n}, inner1={inner_1}, inner2={inner_2}, result={result}") | |
| return result | |
| def calculate_limit(max_iterations: int = 10, tolerance: float = 1e-15) -> Tuple[float, int]: | |
| """ | |
| Calculates the limit of fn(1,0) by iterating until convergence or max_iterations | |
| Returns: (limit value, number of iterations needed) | |
| """ | |
| previous = f0(1, 0) | |
| print(f"f0(1,0) = {previous}") | |
| for i in range(1, max_iterations + 1): | |
| current = fn(i, 1, 0) | |
| print(f"f{i}(1,0) = {current}") | |
| # check convergence | |
| if abs(current - previous) < tolerance: | |
| return current, i | |
| previous = current | |
| return previous, max_iterations | |
| def main(): | |
| print("Calculating limit of fn(1,0)...") | |
| limit, iterations = calculate_limit() | |
| print("\nResults:") | |
| print(f"Limit value = {limit}") | |
| print(f"Converged in {iterations} iterations") | |
| # Additional verification | |
| next_value = fn(iterations + 1, 1, 0) | |
| print(f"\nVerification - next iteration: {next_value}") | |
| print(f"Difference from limit: {abs(next_value - limit)}") | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment