Gusarich commited on
Commit
a5a6e6d
1 Parent(s): 2f509fd

Upload generate.py

Browse files
Files changed (1) hide show
  1. generate.py +118 -0
generate.py ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import random
2
+ import math
3
+ import csv
4
+
5
+ # Define the operations and functions
6
+ operations = ['+', '-', '*', '/']
7
+ functions = ['sqrt', 'log10', 'log2', 'ln', 'exp',
8
+ 'sin', 'cos', 'tan', 'asin', 'acos', 'atan']
9
+
10
+ # Function to generate a random number (integer or float, positive or negative) with logarithmic distribution
11
+
12
+
13
+ def generate_number():
14
+ sign = random.choice([-1, 1])
15
+ # Generates a number in the range 0.0001 to 1000000000 with 4 decimal places
16
+ number = round(math.exp(random.uniform(
17
+ math.log(0.0001), math.log(1000000000))), 4)
18
+ number *= sign
19
+ if random.random() < 0.5:
20
+ return int(number) # Randomly convert to integer
21
+ return number
22
+
23
+ # Function to generate an expression with varying depth and parentheses
24
+
25
+
26
+ def generate_expression(depth=0, max_depth=2):
27
+ if depth > max_depth: # Control the depth of the recursion
28
+ return str(generate_number())
29
+
30
+ if random.random() < 0.3: # Randomly decide whether to add a math function
31
+ func = random.choice(functions)
32
+ return f"{func}({generate_expression(depth + 1, max_depth)})"
33
+
34
+ if random.random() < 0.5: # Randomly decide whether to add parentheses
35
+ return f"({generate_expression(depth + 1, max_depth)} {random.choice(operations)} {generate_expression(depth + 1, max_depth)})"
36
+ else:
37
+ return f"{generate_expression(depth + 1, max_depth)} {random.choice(operations)} {generate_expression(depth + 1, max_depth)}"
38
+
39
+ # Function to validate an expression
40
+
41
+
42
+ def is_valid_expression(expr):
43
+ try:
44
+ eval(expr, {"sqrt": math.sqrt, "log10": math.log10, "log2": math.log2, "ln": math.log,
45
+ "exp": math.exp, "sin": math.sin, "cos": math.cos, "tan": math.tan,
46
+ "asin": math.asin, "acos": math.acos, "atan": math.atan})
47
+ return True
48
+ except:
49
+ return False
50
+
51
+ # Function to evaluate an expression and return the result
52
+
53
+
54
+ def evaluate_expression(expr):
55
+ try:
56
+ result = eval(expr, {"sqrt": math.sqrt, "log10": math.log10, "log2": math.log2, "ln": math.log,
57
+ "exp": math.exp, "sin": math.sin, "cos": math.cos, "tan": math.tan,
58
+ "asin": math.asin, "acos": math.acos, "atan": math.atan})
59
+ return result
60
+ except:
61
+ return "INVALID"
62
+
63
+ # Function to generate multiple valid expressions
64
+
65
+
66
+ def generate_valid_expressions(count, max_depth):
67
+ expressions = []
68
+ while len(expressions) < count:
69
+ expr = generate_expression(depth=0, max_depth=max_depth)
70
+ if is_valid_expression(expr):
71
+ result = evaluate_expression(expr)
72
+ expressions.append((expr, result))
73
+ return expressions
74
+
75
+ # Function to generate multiple invalid expressions
76
+
77
+
78
+ def generate_invalid_expressions(count, max_depth):
79
+ expressions = []
80
+ while len(expressions) < count:
81
+ invalid_expr = f"{generate_expression(depth=0, max_depth=max_depth)} {random.choice(
82
+ operations)} {random.choice(functions)}({generate_expression(depth=0, max_depth=max_depth)})"
83
+ if not is_valid_expression(invalid_expr):
84
+ expressions.append((invalid_expr, "INVALID"))
85
+ return expressions
86
+
87
+ # Function to generate multiple expressions with a mix of valid and invalid
88
+
89
+
90
+ def generate_mixed_expressions(count, max_depth):
91
+ valid_count = int(count * 0.95)
92
+ invalid_count = count - valid_count
93
+
94
+ valid_expressions = generate_valid_expressions(valid_count, max_depth)
95
+ invalid_expressions = generate_invalid_expressions(
96
+ invalid_count, max_depth)
97
+
98
+ all_expressions = valid_expressions + invalid_expressions
99
+ random.shuffle(all_expressions) # Shuffle the list of expressions
100
+
101
+ return all_expressions
102
+
103
+
104
+ # Generate 1,000,000 expressions (you can change this number to any count you need)
105
+ expression_count = 1_000_000
106
+ max_depth = 3 # Adjust the max depth as needed
107
+
108
+ expressions = generate_mixed_expressions(expression_count, max_depth)
109
+
110
+ # Save the expressions to a CSV file
111
+ with open('data.csv', 'w', newline='') as file:
112
+ writer = csv.writer(file)
113
+ writer.writerow(['expression', 'result']) # Write the header
114
+ for expression, result in expressions:
115
+ writer.writerow([expression, result])
116
+
117
+ print(f"Generated {
118
+ expression_count} math expressions and saved to 'data.csv'")