Created
October 2, 2023 10:53
-
-
Save takato3000/7c01448c7d8db41a80b82320e9d1976d to your computer and use it in GitHub Desktop.
Chaotic Mapping
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 sympy | |
| import matplotlib.pyplot as plt | |
| import seaborn as sns | |
| import numpy as np | |
| import random | |
| import sys | |
| sys.set_int_max_str_digits(100000) | |
| def tent_map(beta, iterations) -> list: | |
| from sympy.abc import k | |
| z = sympy.Function("z") | |
| init_value = sympy.Float(random.random()) | |
| ans_list = [] | |
| ans_list.append(init_value) | |
| ans = init_value | |
| beta = sympy.Float(beta) | |
| if beta.epsilon_eq(0.5): | |
| choice = random.choice([0, 1]) | |
| # shift the beta a little bit if meets the critical value | |
| if choice == 0: | |
| beta = beta - 0.0001 | |
| elif choice == 1: | |
| beta = beta + 0.0001 | |
| expr_a = z(k+1) - z(k) / beta | |
| expr_b = z(k+1) - (1 - z(k)) / (1 - beta) | |
| for i in range(iterations): | |
| if ans < beta: | |
| ans = sympy.Float(sympy.solve(expr_a.subs(k, 0).subs(z(0), ans), z(1))[0]) | |
| else: | |
| ans = sympy.Float(sympy.solve(expr_b.subs(k, 0).subs(z(0), ans), z(1))[0]) | |
| ans_list.append(ans) | |
| return ans_list | |
| def dyadic_transformation(iterations) -> list: | |
| from sympy.abc import k | |
| z = sympy.Function("z") | |
| init_value = sympy.Float(random.random()) | |
| ans_list = [] | |
| ans_list.append(init_value) | |
| ans = init_value | |
| expr = z(k+1) - (2 * z(k)) % 1 | |
| for i in range(iterations): | |
| ans = sympy.Float(sympy.solve(expr.subs(k, 0).subs(z(0), ans), z(1))[0]) | |
| ans_list.append(ans) | |
| return ans_list | |
| # alpha from 0(not inclusive) to 4(inclusive) | |
| def sine_map(alpha: Union[float, sympy.Rational, sympy.Float], iterations: int) -> list: | |
| from sympy.abc import k | |
| z = sympy.Function("z") | |
| init_value = sympy.Float(random.random()) | |
| ans_list = [] | |
| ans_list.append(init_value) | |
| ans = init_value | |
| alpha = sympy.Float(alpha) | |
| # define the formula of sine map | |
| expr = z(k+1) - alpha / 4 * sympy.sin(sympy.pi * z(k)) | |
| for i in range(iterations): | |
| ans = sympy.Float(sympy.solve(expr.subs(k, 0).subs(z(0), ans), z(1))[0]) | |
| ans_list.append(ans) | |
| return ans_list | |
| # logistic map | |
| def logistic_map(alpha, iterations) -> list: | |
| from sympy.abc import k | |
| z = sympy.Function("z") | |
| init_value = sympy.Float(random.random()) | |
| if init_value in [0, 0.25, 0.5, 0.75, 1]: | |
| init_value = init_value + 0.0001 # shift the initial value a little bit if meets the critical value | |
| ans_list = [] | |
| ans_list.append(init_value) | |
| ans = init_value | |
| alpha = sympy.Float(alpha) | |
| # define the formula of logistic map | |
| expr = z(k+1) - alpha * z(k) * (1 - z(k)) | |
| for i in range(iterations): | |
| ans = sympy.Float(sympy.solve(expr.subs(k, 0).subs(z(0), ans), z(1))[0]) | |
| ans_list.append(ans) | |
| return ans_list | |
| def singer_map(mu, iterations) -> list: | |
| from sympy.abc import k | |
| z = sympy.Function("z") | |
| ans_list = [] | |
| init_value = sympy.Float(random.random()) | |
| ans_list.append(init_value) | |
| ans = init_value | |
| mu = sympy.Rational(mu) | |
| # define the formula of singer map | |
| expr = z(k+1) - mu * (7.86*z(k) - 23.31*z(k)**2 + 28.75*z(k)**3 - 13.302875*z(k)**4) | |
| for i in range(iterations): | |
| ans = sympy.Rational(sympy.solve(expr.subs(k, 0).subs(z(0), ans), z(1))[0]) | |
| ans_list.append(ans) | |
| return ans_list | |
| ans_list = tent_map(0.4, 1000) | |
| # plot the histogram of ans_list | |
| # plot a bigger figure | |
| plt.figure(figsize=(20, 10)) | |
| sns.histplot(np.array(ans_list, dtype=np.float64), bins=100, color='blue') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment