| import gradio as gr |
| from sympy import symbols, Eq, solve, sympify, binomial, factorial |
| import numpy as np |
| import matplotlib.pyplot as plt |
|
|
| |
| def solve_user_equation(equation_str): |
| x, y = symbols('x y') |
| equation = Eq(sympify(equation_str), 0) |
| solution = solve(equation, x) |
| return f"x の値: {solution}" |
|
|
| |
| def calculate_expression(expression_str): |
| expression = sympify(expression_str) |
| result = expression.evalf() |
| return f"計算結果: {result}" |
|
|
| |
| def calculate_custom_expression(custom_expression_str): |
| x, y = symbols('x y') |
| custom_expression = sympify(custom_expression_str) |
| result = custom_expression.evalf(subs={x: 3, y: 5}) |
| return f"計算結果: {result}" |
|
|
| |
| def factor_equation(equation): |
| factors = sympy.factor(equation) |
| return f"因数分解結果: {factors}" |
|
|
| |
| def expand_equation(equation): |
| expanded_equation = sympy.expand(equation) |
| return f"展開結果: {expanded_equation}" |
|
|
| |
| def calculate_probability(n, min_value, values, target_option, target_value, as_fraction=False): |
| dice_values = symbols(','.join(values)) |
| combinations = np.array(np.meshgrid(*[range(min_value, n + min_value) for _ in range(len(values))])).T.reshape(-1, len(values)) |
|
|
| count = 0 |
| for combo in combinations: |
| subs_dict = dict(zip(dice_values, combo)) |
| result = evaluate_target(subs_dict, target_option, target_value) |
| if result: |
| count += 1 |
|
|
| probability = count / len(combinations) |
|
|
| if as_fraction: |
| return f"{Fraction(probability).limit_denominator()}" |
| else: |
| return f"{probability:.4f}" |
|
|
| |
| def evaluate_target(subs_dict, target_option, target_value): |
| if target_option == "目標の結果(Comma Separated)": |
| results = [int(val.strip()) for val in target_value.split(',')] |
| return any(subs_dict[val] in results for val in subs_dict) |
| elif target_option == "目標の結果(倍数)": |
| return all(subs_dict[val] % int(target_value) == 0 for val in subs_dict) |
| elif target_option == "目標の結果(約数)": |
| return all(int(target_value) % subs_dict[val] == 0 for val in subs_dict) |
| elif target_option == "目標の結果(関数)": |
| try: |
| result = eval(target_value, subs_dict) |
| return bool(result) |
| except Exception as e: |
| print(f"目標関数の評価エラー: {e}") |
| return False |
|
|
| |
| def calculate_intersection(x1, y1, x2, y2, quadratic_expression): |
| x, y = symbols('x y') |
| m = (y2 - y1) / (x2 - x1) |
| b = y1 - m * x1 |
| line_equation = Eq(y, m*x + b) |
| quadratic_equation = Eq(y, eval(quadratic_expression)) |
| intersection = solve((line_equation, quadratic_equation), (x, y)) |
|
|
| x_vals = np.linspace(min(x1, x2) - 1, max(x1, x2) + 1, 100) |
| y_line = m * x_vals + b |
|
|
| quad_func = lambdify(x, quadratic_expression, 'numpy') |
| y_quad = quad_func(x_vals) |
|
|
| plt.figure() |
| plt.plot(x_vals, y_line, label='Line through points') |
| plt.plot(x_vals, y_quad, label='Quadratic function') |
|
|
| intersection_x = float(intersection[x]) |
| intersection_y = float(intersection[y]) |
| plt.scatter([intersection_x], [intersection_y], color='red', label='Intersection') |
|
|
| plt.xlabel('x') |
| plt.ylabel('y') |
| plt.legend() |
| plt.title('Intersection of Line and Quadratic Function') |
| plt.show() |
|
|
| |
| def calculate_days_remaining(target_date_str): |
| try: |
| target_date = datetime.strptime(target_date_str, "%Y-%m-%d") |
| current_date = datetime.now() |
| days_remaining = (target_date - current_date).days |
| return f"Days remaining: {days_remaining} days" |
| except ValueError: |
| return "Invalid date format. Use YYYY-MM-DD." |
|
|
| |
| def solve_equations(equation1, equation2): |
| x = symbols('x') |
| y = symbols('y') |
| solutions = solve([equation1, equation2]) |
| return f"解: {solutions}" |
|
|
| |
| def factorize_number(number): |
| return f"素因数分解: {素因数分解(number)}" |
|
|
| |
| def calculate_combination(n, m): |
| result = binomial(n, m) |
| return f"{n}C{m} is {result}" |
|
|
| |
| def calculate_permutation(n, m): |
| result = factorial(n) / factorial(n - m) |
| return f"{n}P{m} is {result}" |
|
|
| |
| iface = gr.Interface( |
| fn=calculate_combination, |
| inputs=["number", "number"], |
| outputs="text", |
| title="Combination Calculator", |
| description="Calculate combinations (n choose m)." |
| ) |
|
|
| iface.launch() |
|
|