File size: 6,740 Bytes
30e9073
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import os
import gradio as gr
from solution import solutions
from sympy.core.numbers import Rational

EN_US = os.getenv("LANG") != "zh_CN.UTF-8"
ZH2EN = {
    "# “注意到”证明法比较大小": "# Compare sizes by 'Note that' proof",
    "比较 e^(m/n) 与 u/v 大小": "Compare e^(m/n) and u/v",
    "分母不能为 0": "The denominator cannot be 0",
    "比较 π^n 与 p/q 大小": "Compare π^n and p/q",
    "比较 e 与 p/q 大小": "Compare e and p/q",
    "比较 π 与 p/q 大小": "Compare π and p/q",
    "#### 证明结果": "#### Proof result",
    "注意到": "Note that",
    "状态栏": "Status",
    "证毕!": "QED",
}


def _L(zh_txt: str):
    return ZH2EN[zh_txt] if EN_US else zh_txt


def generate_md(args_dict, name):
    ans_latex = solutions[name](**args_dict).get_latex_ans()
    return f"{_L('注意到')} $${ans_latex}$$ {_L('证毕!')}"


def float_to_fraction(x):
    x_str = "{0:.10f}".format(x).rstrip("0").rstrip(".")  # 移除小数点后的无效零

    # 检查是否为整数
    if "." not in x_str:
        return Rational(x_str), Rational(1)

    # 分割整数部分和小数部分
    integer_part, decimal_part = x_str.split(".")
    decimal_digits = len(decimal_part)

    # 构造分子和分母
    numerator = int(integer_part + decimal_part)
    denominator = 10**decimal_digits

    # 简化分数
    gcd_value = 1
    a = numerator
    b = denominator
    while b != 0:
        a, b = b, a % b
        gcd_value = a

    p = Rational(numerator // gcd_value)
    q = Rational(denominator // gcd_value)
    return p, q


def infer_pi(p, q):
    status = "Success"
    proof = None
    try:
        if q == 0:
            raise ValueError(_L("分母不能为 0"))

        p, q = float_to_fraction(p / q)
        args_dict = {"p": p, "q": q}
        proof = generate_md(args_dict, "π")

    except Exception as e:
        status = f"{e}"

    return status, proof


def infer_e(p, q):
    status = "Success"
    proof = None
    try:
        if q == 0:
            raise ValueError(_L("分母不能为 0"))

        p, q = float_to_fraction(p / q)
        args_dict = {"p": p, "q": q}
        proof = generate_md(args_dict, "e")

    except Exception as e:
        status = f"{e}"

    return status, proof


def infer_eq(q1, q2, u, v):
    status = "Success"
    proof = None
    try:
        if q2 == 0 or v == 0:
            raise ValueError(_L("分母不能为 0"))

        q1, q2 = float_to_fraction(q1 / q2)
        u, v = float_to_fraction(u / v)
        args_dict = {"q1": q1, "q2": q2, "u": u, "v": v}
        proof = generate_md(args_dict, "e^q")

    except Exception as e:
        status = f"{e}"

    return status, proof


def infer_pin(n: int, p, q):
    status = "Success"
    proof = None
    try:
        if q == 0:
            raise ValueError(_L("分母不能为 0"))

        p, q = float_to_fraction(p / q)
        args_dict = {"n": n, "p": p, "q": q}
        proof = generate_md(args_dict, "π^n")

    except Exception as e:
        status = f"{e}"

    return status, proof


if __name__ == "__main__":
    os.chdir(os.path.dirname(__file__))
    for file_name in os.listdir("solutions"):
        if not file_name.endswith(".py"):
            continue

        __import__(f"solutions.{file_name[:-3]}")

    with gr.Blocks() as demo:
        gr.Markdown(_L("# “注意到”证明法比较大小"))
        with gr.Tabs():
            with gr.TabItem("π"):
                gr.Interface(
                    fn=infer_pi,
                    inputs=[
                        gr.Number(label="p", value=314),
                        gr.Number(label="q", value=100),
                    ],
                    outputs=[
                        gr.Textbox(label=_L("状态栏"), show_copy_button=True),
                        gr.Markdown(
                            value=_L("#### 证明结果"),
                            show_copy_button=True,
                            container=True,
                            min_height=122,
                        ),
                    ],
                    title=_L("比较 π 与 p/q 大小"),
                    flagging_mode="never",
                )

            with gr.TabItem("e"):
                gr.Interface(
                    fn=infer_e,
                    inputs=[
                        gr.Number(label="p", value=2718),
                        gr.Number(label="q", value=1000),
                    ],
                    outputs=[
                        gr.Textbox(label=_L("状态栏"), show_copy_button=True),
                        gr.Markdown(
                            value=_L("#### 证明结果"),
                            show_copy_button=True,
                            container=True,
                            min_height=122,
                        ),
                    ],
                    title=_L("比较 e 与 p/q 大小"),
                    flagging_mode="never",
                )

            with gr.TabItem("e^q"):
                gr.Interface(
                    fn=infer_eq,
                    inputs=[
                        gr.Number(label="m", value=3),
                        gr.Number(label="n", value=4),
                        gr.Number(label="u", value=2117),
                        gr.Number(label="v", value=1000),
                    ],
                    outputs=[
                        gr.Textbox(label=_L("状态栏"), show_copy_button=True),
                        gr.Markdown(
                            value=_L("#### 证明结果"),
                            show_copy_button=True,
                            container=True,
                            min_height=122,
                        ),
                    ],
                    title=_L("比较 e^(m/n) 与 u/v 大小"),
                    flagging_mode="never",
                )

            with gr.TabItem("π^n"):
                gr.Interface(
                    fn=infer_pin,
                    inputs=[
                        gr.Number(label="n", value=3, step=1),
                        gr.Number(label="p", value=31),
                        gr.Number(label="q", value=1),
                    ],
                    outputs=[
                        gr.Textbox(label=_L("状态栏"), show_copy_button=True),
                        gr.Markdown(
                            value=_L("#### 证明结果"),
                            show_copy_button=True,
                            container=True,
                            min_height=122,
                        ),
                    ],
                    title=_L("比较 π^n 与 p/q 大小"),
                    flagging_mode="never",
                )

    demo.launch()