File size: 4,687 Bytes
b107fad
 
 
 
 
e70a1f7
b107fad
 
 
e70a1f7
b107fad
 
e70a1f7
b107fad
e70a1f7
b107fad
e70a1f7
b107fad
 
 
 
e70a1f7
b107fad
e70a1f7
b107fad
 
e70a1f7
b107fad
 
e70a1f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b107fad
e70a1f7
 
 
b107fad
e70a1f7
b107fad
e70a1f7
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
"""
Mathematical reasoning tool for the AI agent project.
"""

import re
from typing import Optional
from .base_tool import EnhancedTool

class MathematicalReasoningTool(EnhancedTool):
    """Tool for performing mathematical calculations and reasoning."""
    
    name = "MathematicalReasoningTool"
    description = "Perform mathematical calculations, solve equations, and reason through math problems."
    inputs = {
        "query": {
            "type": "string",
            "description": "Mathematical query or problem to solve"
        }
    }
    output_type = "string"
    
    def forward(self, query: str) -> str:
        """
        Solve mathematical problems.
        
        Args:
            query: Mathematical query or problem
            
        Returns:
            Solution or explanation
        """
        query_lower = query.lower()
        
        # Extract any numbers from the query
        numbers = re.findall(r'\d+(?:\.\d+)?', query)
        
        # Simple arithmetic
        if "add" in query_lower or "sum" in query_lower or "+" in query:
            if len(numbers) >= 2:
                result = sum(float(num) for num in numbers)
                return f"The sum of the numbers is {result}"
            return "I need at least two numbers to perform addition."
            
        elif "subtract" in query_lower or "difference" in query_lower or "-" in query:
            if len(numbers) >= 2:
                result = float(numbers[0]) - sum(float(num) for num in numbers[1:])
                return f"The difference is {result}"
            return "I need at least two numbers to perform subtraction."
            
        elif "multiply" in query_lower or "product" in query_lower or "*" in query or "×" in query:
            if len(numbers) >= 2:
                result = 1
                for num in numbers:
                    result *= float(num)
                return f"The product is {result}"
            return "I need at least two numbers to perform multiplication."
            
        elif "divide" in query_lower or "quotient" in query_lower or "/" in query or "÷" in query:
            if len(numbers) >= 2:
                try:
                    result = float(numbers[0])
                    for num in numbers[1:]:
                        result /= float(num)
                    return f"The quotient is {result}"
                except ZeroDivisionError:
                    return "Error: Division by zero is not allowed."
            return "I need at least two numbers to perform division."
            
        # Simple equations
        elif "solve" in query_lower and "equation" in query_lower:
            return "To solve this equation, I would isolate the variable by performing opposite operations on both sides. The solution would typically be x = some value."
            
        # Calculus
        elif "derivative" in query_lower or "differentiate" in query_lower:
            return "To find the derivative, I would use the rules of differentiation such as the power rule, product rule, or chain rule depending on the function."
            
        elif "integral" in query_lower or "integrate" in query_lower:
            return "To find the integral, I would use integration techniques such as substitution, integration by parts, or partial fractions depending on the function."
            
        # Probability
        elif "probability" in query_lower:
            return "Probability problems involve calculating the likelihood of events. This typically requires counting favorable outcomes and dividing by total possible outcomes."
            
        # Statistics
        elif "mean" in query_lower or "average" in query_lower:
            if len(numbers) > 0:
                result = sum(float(num) for num in numbers) / len(numbers)
                return f"The mean (average) is {result}"
            return "I need a set of numbers to calculate the mean."
            
        elif "median" in query_lower:
            if len(numbers) > 0:
                sorted_nums = sorted(float(num) for num in numbers)
                n = len(sorted_nums)
                if n % 2 == 0:
                    median = (sorted_nums[n//2 - 1] + sorted_nums[n//2]) / 2
                else:
                    median = sorted_nums[n//2]
                return f"The median is {median}"
            return "I need a set of numbers to calculate the median."
            
        # General case
        else:
            return "I understand this is a mathematical query, but I don't have enough information to provide a specific answer. Could you provide more details or reformulate the question?"