File size: 5,083 Bytes
5877fd4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/env python3
"""
example.py — Usage demonstrations for hv_writhe_stories.

NumPy only.
"""

import numpy as np

from hv_writhe_stories import (
    Beat, Story, make_corpus,
    check_R1_flips_mod2, check_R2_preserves_mod2, check_R3_preserves_mod2,
)


def demo_basic():
    print("=" * 72)
    print("Demo 1 — encode and read a story")
    print("=" * 72)
    print()

    story = Story(
        name='My_Story',
        beats=[
            Beat(-1, 'ordinary life, restless'),
            Beat(-2, 'inciting rupture'),
            Beat(+1, 'training, awakening'),
            Beat(-1, 'betrayal'),
            Beat(-2, 'near defeat'),
            Beat(+2, 'resurrection'),
        ],
    )
    print(f"  Name:        {story.name}")
    print(f"  Beats:       {story.n_beats()}")
    print(f"  Writhe:      {story.writhe()}")
    print(f"  Mod-2:       {story.mod2()}")
    print(f"  Sign class:  {story.sign_class()}")
    print(f"  Trajectory:  {story.trajectory().tolist()}")
    print()
    print(story.ascii_plot(width=60, height=10))
    print()


def demo_reidemeister():
    print("=" * 72)
    print("Demo 2 — Reidemeister moves")
    print("=" * 72)
    print()

    base = Story(
        name='base',
        beats=[Beat(-1, 'loss'), Beat(+1, 'gain'), Beat(-1, 'loss')],
    )
    print(f"  Base story:   W = {base.writhe()}, mod 2 = {base.mod2()}")
    print()

    # R1: add a single beat, changes W
    r1_odd = base.apply_R1(position=1, direction=+1)
    r1_even = base.apply_R1(position=1, direction=+2)
    print(f"  R1 (+1): W = {r1_odd.writhe()}, mod 2 = {r1_odd.mod2()} "
          f"(flipped)")
    print(f"  R1 (+2): W = {r1_even.writhe()}, mod 2 = {r1_even.mod2()} "
          f"(preserved)")
    print()

    # R2: matched pair, preserves both
    r2 = base.apply_R2(position=1, magnitude=1)
    print(f"  R2 (pair):  W = {r2.writhe()}, mod 2 = {r2.mod2()} "
          f"(preserved)")
    print()

    # R3: swap, preserves both
    r3 = base.apply_R3(i=0, j=2)
    print(f"  R3 (swap):  W = {r3.writhe()}, mod 2 = {r3.mod2()} "
          f"(preserved)")
    print()


def demo_hamlet_edit():
    print("=" * 72)
    print("Demo 3 — edit Hamlet three ways")
    print("=" * 72)
    print()

    corpus = make_corpus()
    hamlet = corpus['hamlet']

    print(f"  Original: {hamlet.name}")
    print(f"    W = {hamlet.writhe()}, mod 2 = {hamlet.mod2()}")
    print()

    # Give Hamlet a happy ending
    hamlet_happy = hamlet.apply_R1(hamlet.n_beats(), direction=+2)
    print(f"  Add a +2 beat (Hamlet survives):")
    print(f"    W = {hamlet_happy.writhe()}, mod 2 = {hamlet_happy.mod2()}")
    print()

    # Add a false alarm
    hamlet_false = hamlet.apply_R2(2, magnitude=1)
    print(f"  Add a false alarm at position 2:")
    print(f"    W = {hamlet_false.writhe()}, mod 2 = {hamlet_false.mod2()}")
    print()

    # Reorder beats
    hamlet_reorder = hamlet.apply_R3(0, 3)
    print(f"  Swap beats 0 and 3 (non-chronological):")
    print(f"    W = {hamlet_reorder.writhe()}, "
          f"mod 2 = {hamlet_reorder.mod2()}")
    print()


def demo_corpus_summary():
    print("=" * 72)
    print("Demo 4 — corpus summary")
    print("=" * 72)
    print()

    corpus = make_corpus()
    print(f"  {'story':<24s}  {'genre':<10s}  {'W':>4s}  {'mod-2':>5s}  "
          f"{'class':<10s}")
    print("  " + "-" * 62)
    for name, story in sorted(corpus.items()):
        s = story.summary()
        print(f"  {s['name']:<24s}  {s['genre_hint']:<10s}  "
              f"{s['writhe']:>4d}  {s['mod2']:>5d}  {s['sign_class']:<10s}")
    print()


def demo_mod2_partition():
    print("=" * 72)
    print("Demo 5 — mod-2 partition of the corpus")
    print("=" * 72)
    print()

    corpus = make_corpus()
    partition = {}
    for story in corpus.values():
        key = (story.sign_class(), story.mod2())
        partition.setdefault(key, []).append(story.name)

    for key in sorted(partition.keys()):
        stories = ', '.join(sorted(partition[key]))
        print(f"  {key[0]:<10s}  mod-2 = {key[1]}  →  {stories}")
    print()
    print("  Note: 'tragedy' splits across two mod-2 classes.")
    print("  Oedipus Rex: W = -6 (even)")
    print("  Hamlet:      W = -7 (odd)")
    print()


def demo_moves_verification():
    print("=" * 72)
    print("Demo 6 — verification of the invariance property")
    print("=" * 72)
    print()

    corpus = make_corpus()
    print(f"  {'story':<24s}  {'R1 flips':>10s}  {'R2 holds':>10s}  "
          f"{'R3 holds':>10s}")
    print("  " + "-" * 60)
    for name, story in sorted(corpus.items()):
        r1 = check_R1_flips_mod2(story, n_trials=20)
        r2 = check_R2_preserves_mod2(story, n_trials=20)
        r3 = check_R3_preserves_mod2(story, n_trials=20)
        print(f"  {story.name:<24s}  {str(r1):>10s}  {str(r2):>10s}  "
              f"{str(r3):>10s}")
    print()


def main():
    demo_basic()
    demo_reidemeister()
    demo_hamlet_edit()
    demo_corpus_summary()
    demo_mod2_partition()
    demo_moves_verification()


if __name__ == "__main__":
    main()