Created
February 9, 2024 19:12
-
-
Save CoffeeVampir3/017e288c5da6277452a363aa93962d7f to your computer and use it in GitHub Desktop.
stuff
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sympy as sp | |
import random | |
from functools import partial | |
# Define the symbol | |
x = sp.symbols('x') | |
# Rules dictionary mapping rule names to (operation, inverse operation) tuples | |
rules = { | |
'addition': (lambda f, n: f + n, lambda f, n: f - n), | |
'subtraction': (lambda f, n: f - n, lambda f, n: f + n), | |
'multiplication': (lambda f, n: f * n, lambda f, n: f / n), | |
'division': (lambda f, n: f / n, lambda f, n: f * n), | |
'exponentiation': (lambda f, n: f ** n, lambda f, n: f ** (1/n)), | |
'logarithm': (lambda f, n: sp.log(f, n), lambda f, n: n ** f), | |
} | |
def explain_step(rule_name, n): | |
if rule_name == 'addition': | |
return f"Subtract {n} from the equation to simplify it." | |
elif rule_name == 'subtraction': | |
return f"Add {n} to the equation to simplify it." | |
elif rule_name == 'multiplication': | |
return f"Divide the equation by {n} to simplify it." | |
elif rule_name == 'division': | |
return f"Multiply the equation by {n} to simplify it." | |
elif rule_name == 'exponentiation': | |
return f"Take the {n}th root of the equation to simplify it." | |
elif rule_name == 'logarithm': | |
return f"Raise {n} to the power of the equation to simplify it." | |
else: | |
return "The given rule is not recognized. Please enter a valid rule name." | |
def get_inverse_operation_name(rule_name): | |
inverse_operations = { | |
'addition': 'Subtract', | |
'subtraction': 'Add', | |
'multiplication': 'Divide by', | |
'division': 'Multiply by', | |
'exponentiation': 'Take the root of', | |
'logarithm': 'Raise to power', | |
} | |
# Return the inverse operation name, or a default message if not found | |
return inverse_operations.get(rule_name, "Unknown operation") | |
def validate_input(rule_name, n): | |
""" | |
Validate the input based on the rule's domain restrictions. | |
Generates a new n if the input is invalid for the given rule. | |
""" | |
if n == 0: return False | |
if rule_name == 'division' and (n == 1): | |
return False | |
elif rule_name == 'exponentiation' and (n == 1): | |
return False | |
elif rule_name == 'logarithm' and (n == 1): | |
return False | |
# If none of the above conditions are met, the inputs are considered valid. | |
return True | |
def generate_rule_value(rule_name): | |
""" | |
Generate a valid n for the given rule and function. | |
""" | |
valid = False | |
while not valid: | |
n = random.randint(-5, 5) | |
valid = validate_input(rule_name, n) | |
return n | |
def count_terms(expression): | |
""" | |
Count the number of distinct terms in a sympy expression. | |
""" | |
# Simplify the expression to combine like terms | |
simplified_expr = sp.simplify(expression) | |
# If the simplified expression is a sum, its arguments are the terms | |
if simplified_expr.func is sp.Add: | |
return len(simplified_expr.args) | |
# If the expression is not a sum, it's considered a single term | |
return 1 | |
# Function to apply a rule and its inverse | |
def apply_rule_and_inverse(function, rule_name, n): | |
if rule_name in rules: | |
forward, inverse = rules[rule_name] | |
# Apply the forward operation | |
transformed = forward(function, n) | |
print(f"After applying {rule_name} with n = {n}: {transformed}") | |
terms = count_terms(transformed) | |
print(f"Num terms: {terms}") | |
return transformed, partial(inverse, n=n) | |
else: | |
print("Rule not found") | |
return function, function | |
def apply_random_rules(function, steps): | |
rule_list = [] | |
step_list = [] | |
value_list = [] | |
for _ in range(steps): | |
rule_name, operation = random.choice(list(rules.items())) | |
v = generate_rule_value(rule_name) | |
function, inverse = apply_rule_and_inverse(function, rule_name, v) | |
rule_list.append(rule_name) | |
step_list.append(function) | |
value_list.append(v) | |
return function, rule_list, step_list, value_list | |
f_x = x | |
transformed_f_x, rule_list, step_list, value_list = apply_random_rules(f_x, 9) | |
# Print the transformed and reverted function | |
print(f"Transformed function: f(x) = {transformed_f_x}") | |
rule_list.reverse() | |
step_list.reverse() | |
value_list.reverse() | |
transformed_f_x | |
print("\n\n") | |
for rule, func, val in zip(rule_list, step_list, value_list): | |
inv_name = get_inverse_operation_name(rule) | |
print(f"Function: {func}") | |
print(f"Next step: {inv_name} {val}") | |
print(f_x) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment