File size: 4,795 Bytes
511dd67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import gradio as gr
from sympy import symbols, Eq, solve, sympify, binomial, factorial
import numpy as np
import matplotlib.pyplot as plt

# Function: Solve User Equation
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}"

# Function: Calculate Expression
def calculate_expression(expression_str):
    expression = sympify(expression_str)
    result = expression.evalf()
    return f"計算結果: {result}"

# Function: Calculate Custom Expression
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}"

# Function: Factorize Equation
def factor_equation(equation):
    factors = sympy.factor(equation)
    return f"因数分解結果: {factors}"

# Function: Expand Equation
def expand_equation(equation):
    expanded_equation = sympy.expand(equation)
    return f"展開結果: {expanded_equation}"

# Function: Calculate Probability
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}"

# Function: Evaluate Target
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

# Function: Calculate Intersection
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()

# Function: Calculate Days Remaining
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."

# Function: Solve Equations
def solve_equations(equation1, equation2):
    x = symbols('x')
    y = symbols('y')
    solutions = solve([equation1, equation2])
    return f"解: {solutions}"

# Function: Factorize Number
def factorize_number(number):
    return f"素因数分解: {素因数分解(number)}"

# Function: Combination Calculation
def calculate_combination(n, m):
    result = binomial(n, m)
    return f"{n}C{m} is {result}"

# Function: Permutation Calculation
def calculate_permutation(n, m):
    result = factorial(n) / factorial(n - m)
    return f"{n}P{m} is {result}"

# Gradio Interface
iface = gr.Interface(
    fn=calculate_combination,
    inputs=["number", "number"],
    outputs="text",
    title="Combination Calculator",
    description="Calculate combinations (n choose m)."
)

iface.launch()