math-pathlib 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,5 @@
1
+ Metadata-Version: 2.4
2
+ Name: math_pathlib
3
+ Version: 0.1.0
4
+ Summary: Educational and practical Python learning resources
5
+ Requires-Python: >=3.13
@@ -0,0 +1,18 @@
1
+ matplotlib_pyplot/__init__.py,sha256=mgkpry1sJj_QfyJFN_MJTZFn8S7viF1DFuZ1NcnPFCY,46
2
+ matplotlib_pyplot/menu.py,sha256=JSJLiTxYQHRuM8qeSYLBQMv_OZL9Oet9VSmjjiBY5QM,1531
3
+ matplotlib_pyplot/logic1/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
+ matplotlib_pyplot/logic1/category_menu.py,sha256=prhnlL-HGNCKGxcW--h7xy-6vqgZ8Oa96RH8-_pJM_c,1524
5
+ matplotlib_pyplot/logic1/menu.py,sha256=S5u70zAptibRa0nbG0vLa9rGpDQoG3n4gIc7sZmg3N4,1572
6
+ matplotlib_pyplot/logic1/1_RFL/P_1_Model_MDP_Components.txt,sha256=g-4N00C8xJqfpP4MVVosXnY7Yco8aZWlyFiO0tMCVXA,361
7
+ matplotlib_pyplot/logic1/1_RFL/P_2_Compare_Bandit_Exploration.txt,sha256=Y7Pv6rDPqs4Hm46VStKHvJNmw2pi86zRubgX8qkZ25c,642
8
+ matplotlib_pyplot/logic1/1_RFL/P_3_Implement_TD0_Learning.txt,sha256=n2DtHX2RaKjQodPZp0lXn4QmW5jpbtc8e1oxMMOd41A,465
9
+ matplotlib_pyplot/logic1/1_RFL/P_4_Plot_Policy_Iteration_Convergence.txt,sha256=HA6mY5B6FKbarhuMIbI0ng7zwN6WhzXwrqnimh4oFTs,572
10
+ matplotlib_pyplot/logic1/1_RFL/P_5_Compare_Policy_Value_Iteration.txt,sha256=secXHf-yREa5UP99qVnjaH1L1mMbJyl6-m-8bjrjr4U,689
11
+ matplotlib_pyplot/logic1/1_RFL/P_6_Implement_SARSA_Control.txt,sha256=NkvGnvRNwqmDyTx2XAJBssk-JMxvIRn8IqVT3iGbdVs,642
12
+ matplotlib_pyplot/logic1/1_RFL/P_7_Compare_On_vs_Off_Policy.txt,sha256=KUHKqYIu95XgnYN5ObvZVuYi9uYirov52W4w-DGCM_w,735
13
+ matplotlib_pyplot/logic1/1_RFL/P_8_Implement_First_Visit_Monte_Carlo.txt,sha256=wtlJb-aJj-gbnWl75Gc0nRS_t2zvtJtzhQtPH9g_JZI,669
14
+ matplotlib_pyplot/logic1/1_RFL/menu.py,sha256=tHh1gMBJc7B3qC3jsO9xVFch6mFgWdYNpE9KBA2BaK4,1526
15
+ math_pathlib-0.1.0.dist-info/METADATA,sha256=3yIuzsqjAzfVh1zcNxqZZOvtqgRjUtqYt-qElITszo4,146
16
+ math_pathlib-0.1.0.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
17
+ math_pathlib-0.1.0.dist-info/top_level.txt,sha256=dS1gi1dQvVAiL2Kqyc8AuRcIpYh2GxbAyhHJ1d52E18,18
18
+ math_pathlib-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (84.0.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ matplotlib_pyplot
@@ -0,0 +1,4 @@
1
+ from .menu import run
2
+
3
+
4
+ __all__ = ["run"]
@@ -0,0 +1,11 @@
1
+ import numpy as np, pandas as pd, matplotlib.pyplot as plt
2
+
3
+ P = np.array([[0.7, 0.3], [0.4, 0.6]])
4
+ V = np.array([1.0, 0.0])
5
+ traj = [V @ np.linalg.matrix_power(P, t) for t in range(20)]
6
+ df = pd.DataFrame(traj, columns=["State A", "State B"])
7
+
8
+ df.plot(title="MDP State Transition Trajectory")
9
+ plt.xlabel("Time Step")
10
+ plt.ylabel("Probability")
11
+ plt.show()
@@ -0,0 +1,16 @@
1
+ import numpy as np
2
+ import pandas as pd
3
+ import matplotlib.pyplot as plt
4
+
5
+ def bandit(mode, K=5, N=1000):
6
+ q_true, Q, N_a = np.random.randn(K), np.zeros(K), np.zeros(K)
7
+ for t in range(1, N + 1):
8
+ bonus = np.sqrt(2 * np.log(t) / np.maximum(N_a, 1e-9))
9
+ a = np.argmax(Q + bonus) if mode == "ucb" else (np.random.randint(K) if np.random.rand() < 0.1 else np.argmax(Q))
10
+ r = q_true[a] + np.random.randn()
11
+ N_a[a] += 1
12
+ Q[a] += (r - Q[a]) / N_a[a]
13
+ yield r
14
+
15
+ pd.DataFrame({"UCB": bandit("ucb"), "Eps-Greedy": bandit("eps")}).expanding().mean().plot(title="Bandit Exploration")
16
+ plt.show()
@@ -0,0 +1,17 @@
1
+ import numpy as np
2
+ import pandas as pd
3
+ import matplotlib.pyplot as plt
4
+
5
+ V, g, a, V_true = np.zeros(5), 0.9, 0.1, 0.9 ** np.arange(3, -1, -1)
6
+ errs = []
7
+
8
+ for _ in range(300):
9
+ s = 0
10
+ while s < 4:
11
+ sn, r = s + 1, float(s == 3)
12
+ V[s] += a * (r + g * V[sn] - V[s])
13
+ s = sn
14
+ errs.append(np.sqrt(np.mean((V[:4] - V_true) ** 2)))
15
+
16
+ pd.Series(errs).plot(title="TD(0) RMSE Convergence", xlabel="Episode", ylabel="RMSE")
17
+ plt.show()
@@ -0,0 +1,16 @@
1
+ import numpy as np
2
+ import pandas as pd
3
+ import matplotlib.pyplot as plt
4
+
5
+ S, A, g = 5, 2, 0.9
6
+ P, R = np.random.dirichlet(np.ones(S), size=(S, A)), np.random.randn(S, A)
7
+ pi, history = np.zeros(S, dtype=int), []
8
+
9
+ for _ in range(10):
10
+ V = np.linalg.solve(np.eye(S) - g * P[np.arange(S), pi], R[np.arange(S), pi])
11
+ Q = np.column_stack([R[:, a] + g * P[:, a] @ V for a in range(A)])
12
+ pi = np.argmax(Q, axis=1)
13
+ history.append(V.mean())
14
+
15
+ pd.Series(history).plot(title="Policy Iteration Value Convergence", xlabel="Iteration", ylabel="Mean V")
16
+ plt.show()
@@ -0,0 +1,23 @@
1
+ import numpy as np
2
+ import pandas as pd
3
+ import matplotlib.pyplot as plt
4
+
5
+ S, A, g = 5, 2, 0.9
6
+ P, R = np.random.dirichlet(np.ones(S), size=(S, A)), np.random.randn(S, A)
7
+
8
+ def solve(pi):
9
+ V, pol = np.zeros(S), np.arange(S) % A
10
+ for _ in range(12):
11
+ Q = np.column_stack([R[:, a] + g * P[:, a] @ V for a in range(A)])
12
+ pol = np.argmax(Q, axis=1)
13
+ V = np.linalg.solve(
14
+ np.eye(S) - g * P[np.arange(S), pol],
15
+ R[np.arange(S), pol]
16
+ ) if pi else Q.max(axis=1)
17
+ yield V.mean()
18
+
19
+ pd.DataFrame({
20
+ "Policy Iteration": solve(True),
21
+ "Value Iteration": solve(False)
22
+ }).plot(title="PI vs VI Convergence")
23
+ plt.show()
@@ -0,0 +1,17 @@
1
+ import numpy as np
2
+ import pandas as pd
3
+ import matplotlib.pyplot as plt
4
+
5
+ def sarsa(episodes=300, Q=np.zeros((5, 2))):
6
+ pi = lambda s: np.random.randint(2) if np.random.rand() < 0.1 else np.argmax(Q[s])
7
+ for _ in range(episodes):
8
+ s, ep_r, a = 0, 0, pi(0)
9
+ while s < 4:
10
+ sn, r = min(4, max(0, s + (1 if a == 1 else -1))), (1.0 if s == 3 and a == 1 else -0.1)
11
+ an = pi(sn)
12
+ Q[s, a] += 0.1 * (r + 0.9 * Q[sn, an] - Q[s, a])
13
+ s, a, ep_r = sn, an, ep_r + r
14
+ yield ep_r
15
+
16
+ pd.Series(sarsa()).rolling(20).mean().plot(title="SARSA Control Learning Curve")
17
+ plt.show()
@@ -0,0 +1,22 @@
1
+ import numpy as np
2
+ import pandas as pd
3
+ import matplotlib.pyplot as plt
4
+
5
+ def run(off_policy, Q=np.zeros((5, 2))):
6
+ pi = lambda s: np.random.randint(2) if np.random.rand() < 0.2 else np.argmax(Q[s])
7
+ for _ in range(300):
8
+ s, ep_r = 0, 0
9
+ while s < 4:
10
+ a = pi(s)
11
+ sn = min(4, max(0, s + (1 if a == 1 else -1)))
12
+ r = 1.0 if s == 3 and a == 1 else -0.1
13
+ target = Q[sn].max() if off_policy else Q[sn, pi(sn)]
14
+ Q[s, a] += 0.1 * (r + 0.9 * target - Q[s, a])
15
+ s, ep_r = sn, ep_r + r
16
+ yield ep_r
17
+
18
+ pd.DataFrame({
19
+ "Q-Learning": run(True),
20
+ "SARSA": run(False)
21
+ }).rolling(20).mean().plot(title="SARSA vs Q-Learning")
22
+ plt.show()
@@ -0,0 +1,24 @@
1
+ import numpy as np
2
+ import pandas as pd
3
+ import matplotlib.pyplot as plt
4
+
5
+ V, returns = np.zeros(5), [[] for _ in range(5)]
6
+
7
+ for _ in range(300):
8
+ s, episode = 0, []
9
+ while s < 4:
10
+ sn, r = s + 1, float(s == 3)
11
+ episode.append((s, r))
12
+ s = sn
13
+
14
+ G = 0
15
+ for state, reward in reversed(episode):
16
+ G = reward + 0.9 * G
17
+ if not any(x[0] == state for x in episode[:episode.index((state, reward))]):
18
+ returns[state].append(G)
19
+ V[state] = np.mean(returns[state])
20
+
21
+ pd.Series(V).plot(kind="bar", title="First-Visit Monte Carlo State Values")
22
+ plt.xlabel("State")
23
+ plt.ylabel("Value")
24
+ plt.show()
@@ -0,0 +1,60 @@
1
+ from pathlib import Path
2
+
3
+
4
+ def discover_files():
5
+ """Discover all TXT files inside the RFL directory."""
6
+ rfl_path = Path(__file__).resolve().parent
7
+
8
+ return sorted(
9
+ rfl_path.glob("*.txt"),
10
+ key=lambda path: path.name
11
+ )
12
+
13
+
14
+ def display_file(file_path):
15
+ """Display the complete contents of a selected TXT file."""
16
+
17
+ print("\n========================================")
18
+ print(file_path.name)
19
+ print("========================================\n")
20
+
21
+ content = file_path.read_text(encoding="utf-8")
22
+
23
+ print(content)
24
+
25
+
26
+ def run():
27
+ """Display and handle the RFL file menu."""
28
+
29
+ while True:
30
+ files = discover_files()
31
+
32
+ print("\n========================================")
33
+ print("1_RFL")
34
+ print()
35
+
36
+ for index, file_path in enumerate(files, start=1):
37
+ print(f"{index}. {file_path.stem}")
38
+
39
+ print(f"{len(files) + 1}. Back")
40
+ print(" E. Exit")
41
+
42
+ choice = input("\nEnter option: ").strip().lower()
43
+
44
+ if choice in {"e", "exit"}:
45
+ print("Exiting matplotlib_pyplot.")
46
+ return
47
+
48
+ if choice.isdigit():
49
+ option = int(choice)
50
+
51
+ if 1 <= option <= len(files):
52
+ display_file(files[option - 1])
53
+
54
+ input("\nPress Enter to return to 1_RFL...")
55
+ continue
56
+
57
+ if option == len(files) + 1:
58
+ return
59
+
60
+ print("Invalid option. Please try again.")
File without changes
@@ -0,0 +1,55 @@
1
+ from pathlib import Path
2
+
3
+
4
+ def run_category(category_path):
5
+ """Display TXT files inside a category directory."""
6
+
7
+ category_path = Path(category_path)
8
+
9
+ while True:
10
+ files = sorted(
11
+ category_path.glob("*.txt"),
12
+ key=lambda path: path.name
13
+ )
14
+
15
+ print("\n========================================")
16
+ print(category_path.name)
17
+ print()
18
+
19
+ if files:
20
+ for index, file_path in enumerate(files, start=1):
21
+ print(f"{index}. {file_path.stem}")
22
+ else:
23
+ print("No TXT files found.")
24
+
25
+ print(f"{len(files) + 1}. Back")
26
+ print(" E. Exit")
27
+
28
+ choice = input("\nEnter option: ").strip().lower()
29
+
30
+ if choice in {"e", "exit"}:
31
+ return "exit"
32
+
33
+ if choice.isdigit():
34
+ option = int(choice)
35
+
36
+ if 1 <= option <= len(files):
37
+ selected_file = files[option - 1]
38
+
39
+ print("\n========================================")
40
+ print(selected_file.name)
41
+ print("========================================\n")
42
+
43
+ print(
44
+ selected_file.read_text(
45
+ encoding="utf-8"
46
+ )
47
+ )
48
+
49
+ input("\nPress Enter to return...")
50
+ continue
51
+
52
+ if option == len(files) + 1:
53
+ return "back"
54
+
55
+ print("Invalid option. Please try again.")
@@ -0,0 +1,63 @@
1
+ from pathlib import Path
2
+
3
+ from .category_menu import run_category
4
+
5
+
6
+ def discover_categories():
7
+ """Discover numbered category directories inside logic1."""
8
+
9
+ logic_path = Path(__file__).resolve().parent
10
+
11
+ categories = []
12
+
13
+ for path in logic_path.iterdir():
14
+ if (
15
+ path.is_dir()
16
+ and "_" in path.name
17
+ and path.name.split("_", 1)[0].isdigit()
18
+ ):
19
+ categories.append(path)
20
+
21
+ return sorted(
22
+ categories,
23
+ key=lambda path: int(path.name.split("_", 1)[0])
24
+ )
25
+
26
+
27
+ def run():
28
+ """Display and handle the logic1 category menu."""
29
+
30
+ while True:
31
+ categories = discover_categories()
32
+
33
+ print("\n========================================")
34
+ print("LOGIC1")
35
+ print()
36
+
37
+ for index, category in enumerate(categories, start=1):
38
+ print(f"{index}. {category.name}")
39
+
40
+ print(f"{len(categories) + 1}. Back")
41
+ print(" E. Exit")
42
+
43
+ choice = input("\nEnter option: ").strip().lower()
44
+
45
+ if choice in {"e", "exit"}:
46
+ print("Exiting matplotlib_pyplot.")
47
+ return
48
+
49
+ if choice.isdigit():
50
+ option = int(choice)
51
+
52
+ if 1 <= option <= len(categories):
53
+ result = run_category(categories[option - 1])
54
+
55
+ if result == "exit":
56
+ return
57
+
58
+ continue
59
+
60
+ if option == len(categories) + 1:
61
+ return
62
+
63
+ print("Invalid option. Please try again.")
@@ -0,0 +1,59 @@
1
+ from pathlib import Path
2
+ import importlib
3
+
4
+
5
+ def discover_logic_modules():
6
+ """Discover logic directories inside the main package."""
7
+ package_path = Path(__file__).resolve().parent
8
+
9
+ logic_modules = []
10
+
11
+ for path in package_path.iterdir():
12
+ if (
13
+ path.is_dir()
14
+ and path.name.startswith("logic")
15
+ and path.name[5:].isdigit()
16
+ ):
17
+ logic_modules.append(path.name)
18
+
19
+ return sorted(
20
+ logic_modules,
21
+ key=lambda name: int(name[5:])
22
+ )
23
+
24
+
25
+ def run():
26
+ """Display and handle the main logic menu."""
27
+
28
+ while True:
29
+ logic_modules = discover_logic_modules()
30
+
31
+ print("\n========================================")
32
+ print("LOGIC MENU")
33
+ print()
34
+
35
+ for index, logic_name in enumerate(logic_modules, start=1):
36
+ print(f"{index}. {logic_name}")
37
+
38
+ print(" E. Exit")
39
+
40
+ choice = input("\nEnter option: ").strip().lower()
41
+
42
+ if choice in {"e", "exit"}:
43
+ print("Exiting matplotlib_pyplot.")
44
+ return
45
+
46
+ if choice.isdigit():
47
+ option = int(choice)
48
+
49
+ if 1 <= option <= len(logic_modules):
50
+ selected_logic = logic_modules[option - 1]
51
+
52
+ module = importlib.import_module(
53
+ f"matplotlib_pyplot.{selected_logic}.menu"
54
+ )
55
+
56
+ module.run()
57
+ continue
58
+
59
+ print("Invalid option. Please try again.")