File size: 3,644 Bytes
a68a733
 
34a73d6
 
 
f74d5d2
462f856
 
 
 
 
34a73d6
462f856
 
 
 
 
 
 
 
 
 
 
 
 
34a73d6
462f856
34a73d6
 
 
 
462f856
 
 
34a73d6
 
462f856
 
34a73d6
 
462f856
 
 
 
 
 
34a73d6
 
 
462f856
34a73d6
462f856
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34a73d6
 
462f856
 
9aba116
34a73d6
f74d5d2
 
 
9aba116
f74d5d2
 
 
34a73d6
9aba116
f74d5d2
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
import gradio as gr

import pandas as pd
import math

def add_variables(M, L1, L2, LS, A, z, n):
    
    r_m = []
    r_l1 = []
    r_l2 = []
    r_ls = []

    if L2 == 0:
        if LS == 0:
            raise Exception('Error! L2 and LS cannot be both 0, please input again!')
        else:
            for m in range(math.ceil(2*A / M) + 1):
                for l1 in range(math.ceil(2*A / L1) + 1):
                    for ls in range(math.ceil(2*A / LS) + 1):
                        total_mass = M * m + L1 * l1 + LS * ls
                        if (z * A - n <= total_mass <= z * A + n) and ls < m:
                            r_m.append(m)
                            r_l1.append(l1)
                            r_l2.append(None)
                            r_ls.append(ls)

            results = {
            'M': r_m,
            'L1': r_l1,
            'L2': r_l2,
            'LS': r_ls,
            }
            df = pd.DataFrame(results)
            print("符合 {} =< {}*m+{}*l1+{}*ls <= {} 并 LS < M 的所有自然数解有{}组,分别为:"
              .format(A - n, M, L1, LS,  A + n, len(r_m)))

            print(df.to_markdown(tablefmt="grid"))
            df.to_csv("result.csv", index=False)

    else:
        if LS == 0:
            for m in range(math.ceil(2*A / M) + 1):
                for l1 in range(math.ceil(2*A / L1) + 1):
                    for l2 in range(math.ceil(2*A / L2) + 1):
                        total_mass = M * m + L1 * l1 + L2 * l2
                        if z * A - n <= total_mass <= z * A + n:
                            r_m.append(m)
                            r_l1.append(l1)
                            r_l2.append(l2)
                            r_ls.append(None)

            results = {
                'M': r_m,
                'L1': r_l1,
                'L2': r_l2,
                'LS': r_ls,
            }
            df = pd.DataFrame(results)
            print("符合 {} =< {}*m+{}*l1+{}*l2 <= {} 的所有自然数解有{}组,分别为:"
              .format(A - n, M, L1, L2, A + n, len(r_m)))

            print(df.to_markdown(tablefmt="grid"))
            df.to_csv("result.csv", index=False)

        else:
            for m in range(math.ceil(2*A / M) + 1):
                for l1 in range(math.ceil(2*A / L1) + 1):
                    for l2 in range(math.ceil(2*A / L2) + 1):
                        for ls in range(math.ceil(2*A / LS) + 1):
                            total_mass = M * m + L1 * l1 + L2 * l2 + LS * ls
                            if (z * A - n <= total_mass <= z * A + n) and ls < m:
                                r_m.append(m)
                                r_l1.append(l1)
                                r_l2.append(l2)
                                r_ls.append(ls)

            results = {
                'M': r_m,
                'L1': r_l1,
                'L2': r_l2,
                'LS': r_ls,
            }
            df = pd.DataFrame(results)
            print("符合 {} =< {}*m+{}*l1+{}*l2+{}*ls <= {} 并 LS < M 的所有自然数解有{}组,分别为:"
              .format(A - n, M, L1, L2, LS, A + n, len(r_m)))

            print(df.to_markdown(tablefmt="grid"))
            df.to_csv("result.csv", index=False)
    return df, "result.csv"

iface = gr.Interface(
    fn=add_variables,
    inputs=["number", "number", "number", "number", "number", "number", "number"],
    outputs=["dataframe",'file'],
    title="MS",
    description="输入:|-M: 金属| -L1: 配体1 | -L2: 配体2 | -LS: 含S配体的相对分子质量 | -A: 质谱上对应的分子量 | -z: 电荷 |"
)

iface.launch(share=True)