-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmathem.py
149 lines (116 loc) · 3.28 KB
/
mathem.py
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
import random
from math import log
import math
import sys
sys.setrecursionlimit(1000)
class PrimerGenerator:
def extract_nums(self, exp):
symbols = list(exp)
NUM = "1234567890."
for i in range(len(symbols)):
symbols[i] = "N" if symbols[i] in NUM else "T"
begins = []
ends = []
for i in range(len(symbols) - 1):
fn = symbols[i] + symbols[i + 1]
if fn == "TN":
begins.append(i)
elif fn == "NT":
ends.append(i)
if exp[-1] in NUM:
ends.append(len(exp) - 1)
if exp[0] in NUM:
begins = [-1] + begins
return [(x + 1, y + 1) for x, y in zip(begins, ends)]
def __init__(self):
self.funcs = []
def add_expander(self, func):
self.funcs.append(func)
def complexify(self, num):
return random.choice(self.funcs)(num)
def __rxp__(self, exp):
x, y = random.choice(self.extract_nums(exp))
exp = exp[:x] + "(" + self.complexify(float(exp[x:y])) + ")" + exp[y:]
return exp
def randexpr(self, ans, steps):
e = str(ans)
for i in range(steps):
e = self.__rxp__(e)
return e
def unmin(*args, acc=2):
r = []
for arg in args:
f = round(arg, acc)
if f > 0:
f = str(f)
else:
f = "(" + str(f) + ")"
r.append(f)
return r
def __c_sum(num):
a = round(random.random() * 100, 3)
b = num - a
a, b = unmin(a, b)
return a + " + " + b
def __c_mul(num):
a = num / (random.random() * 100 + 10)
if a == 0.0:
b = random.random()
else:
b = num / a
a, b = unmin(a, b, acc=5)
return a + " * " + b
def __c_sub(num):
a = num + 100 ** (random.random() * 2)
b = (a - num)
a, b = unmin(a, b)
return a + " - " + b
def __c_log(num):
fr = random.randint(300, 500)
a = math.e ** (num / fr)
a, fr = unmin(a, fr, acc=5)
return "log(" + a + ") * " + fr
def __l_sum(num):
a = 100 ** (random.random() * 2)
b = num - a
a, b = unmin(a, b)
return a + " + " + b
def __l_div(num):
a = num * (random.random() * 100 + 10)
if a == 0.0:
b = random.random()
else:
b = a / num
a, b = unmin(a, b)
return "\\frac{" + a + "}{" + b + "}"
def __l_pow(num):
if num == 0:
return str(random.randint(2, 7)) + "^{-\\infty}"
a = random.randint(0, 10) + 3
b = math.log(abs(num), a)
a, b = unmin(a, b)
return ("-" if num < 0 else "") + a + "^{" + b + "}"
def __l_sqrt(num):
a = num ** 0.5
a = unmin(a)[0]
return "\\sqrt{" + a + "}"
def __l_int(num):
patterns = [
("x^{2}", (3 * num) ** (1 / 3), "dx"),
("y^{3}", (4 * num) ** (1 / 4), "dy"),
("\sqrt{t}", (1.5 * num) ** (2 / 3), "dt")
]
p, b, f = random.choice(patterns)
b = str(round(b, 3))
return "\\int_{0}^{" + b + "} " + p + " " + f
def __l_sig(num):
a = random.randint(1, 10)
b = random.randint(1, 10) + a
s = sum([i for i in range(a, b + 1)])
c = num / s
a, b, c = unmin(a, b, c)
return "\\sum_{i=" + a + "}^{" + b + "} i*" + c
def generate_math_example(num: int):
gen = PrimerGenerator()
gen.add_expander(__c_sum)
return gen.complexify(num)