File size: 4,307 Bytes
a5a6e6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import random
import math
import csv

# Define the operations and functions
operations = ['+', '-', '*', '/']
functions = ['sqrt', 'log10', 'log2', 'ln', 'exp',
             'sin', 'cos', 'tan', 'asin', 'acos', 'atan']

# Function to generate a random number (integer or float, positive or negative) with logarithmic distribution


def generate_number():
    sign = random.choice([-1, 1])
    # Generates a number in the range 0.0001 to 1000000000 with 4 decimal places
    number = round(math.exp(random.uniform(
        math.log(0.0001), math.log(1000000000))), 4)
    number *= sign
    if random.random() < 0.5:
        return int(number)  # Randomly convert to integer
    return number

# Function to generate an expression with varying depth and parentheses


def generate_expression(depth=0, max_depth=2):
    if depth > max_depth:  # Control the depth of the recursion
        return str(generate_number())

    if random.random() < 0.3:  # Randomly decide whether to add a math function
        func = random.choice(functions)
        return f"{func}({generate_expression(depth + 1, max_depth)})"

    if random.random() < 0.5:  # Randomly decide whether to add parentheses
        return f"({generate_expression(depth + 1, max_depth)} {random.choice(operations)} {generate_expression(depth + 1, max_depth)})"
    else:
        return f"{generate_expression(depth + 1, max_depth)} {random.choice(operations)} {generate_expression(depth + 1, max_depth)}"

# Function to validate an expression


def is_valid_expression(expr):
    try:
        eval(expr, {"sqrt": math.sqrt, "log10": math.log10, "log2": math.log2, "ln": math.log,
                    "exp": math.exp, "sin": math.sin, "cos": math.cos, "tan": math.tan,
                    "asin": math.asin, "acos": math.acos, "atan": math.atan})
        return True
    except:
        return False

# Function to evaluate an expression and return the result


def evaluate_expression(expr):
    try:
        result = eval(expr, {"sqrt": math.sqrt, "log10": math.log10, "log2": math.log2, "ln": math.log,
                             "exp": math.exp, "sin": math.sin, "cos": math.cos, "tan": math.tan,
                             "asin": math.asin, "acos": math.acos, "atan": math.atan})
        return result
    except:
        return "INVALID"

# Function to generate multiple valid expressions


def generate_valid_expressions(count, max_depth):
    expressions = []
    while len(expressions) < count:
        expr = generate_expression(depth=0, max_depth=max_depth)
        if is_valid_expression(expr):
            result = evaluate_expression(expr)
            expressions.append((expr, result))
    return expressions

# Function to generate multiple invalid expressions


def generate_invalid_expressions(count, max_depth):
    expressions = []
    while len(expressions) < count:
        invalid_expr = f"{generate_expression(depth=0, max_depth=max_depth)} {random.choice(
            operations)} {random.choice(functions)}({generate_expression(depth=0, max_depth=max_depth)})"
        if not is_valid_expression(invalid_expr):
            expressions.append((invalid_expr, "INVALID"))
    return expressions

# Function to generate multiple expressions with a mix of valid and invalid


def generate_mixed_expressions(count, max_depth):
    valid_count = int(count * 0.95)
    invalid_count = count - valid_count

    valid_expressions = generate_valid_expressions(valid_count, max_depth)
    invalid_expressions = generate_invalid_expressions(
        invalid_count, max_depth)

    all_expressions = valid_expressions + invalid_expressions
    random.shuffle(all_expressions)  # Shuffle the list of expressions

    return all_expressions


# Generate 1,000,000 expressions (you can change this number to any count you need)
expression_count = 1_000_000
max_depth = 3  # Adjust the max depth as needed

expressions = generate_mixed_expressions(expression_count, max_depth)

# Save the expressions to a CSV file
with open('data.csv', 'w', newline='') as file:
    writer = csv.writer(file)
    writer.writerow(['expression', 'result'])  # Write the header
    for expression, result in expressions:
        writer.writerow([expression, result])

print(f"Generated {
      expression_count} math expressions and saved to 'data.csv'")