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'")