File size: 12,252 Bytes
4d1746c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import math
from decimal import Decimal, InvalidOperation, getcontext
from typing import Dict, List, Optional, Union

import mpmath


class MathAPI:
    def __init__(self):
        self._api_description = "This tool belongs to the Math API, which provides various mathematical operations."
        
    def logarithm(
        self, value: float, base: float, precision: int
    ) -> Dict[str, float]:
        """
        Compute the logarithm of a number with adjustable precision using mpmath.

        Args:
            value (float): The number to compute the logarithm of.
            base (float): The base of the logarithm.
            precision (int): Desired precision for the result.

        Returns:
            result (float): The logarithm of the number with respect to the given base.
        """
        try:
            # Set precision for mpmath
            mpmath.mp.dps = precision

            # Use mpmath for high-precision logarithmic calculations
            result = mpmath.log(value) / mpmath.log(base)

            return {"result": result}
        except Exception as e:
            return {"error": str(e)}

    def mean(self, numbers: List[float]) -> Dict[str, float]:
        """
        Calculate the mean of a list of numbers.

        Args:
            numbers (List[float]): List of numbers to calculate the mean of.

        Returns:
            result (float): Mean of the numbers.
        """
        if not numbers:
            return {"error": "Cannot calculate mean of an empty list"}
        try:
            return {"result": sum(numbers) / len(numbers)}
        except TypeError:
            return {"error": "All elements in the list must be numbers"}

    def standard_deviation(self, numbers: List[float]) -> Dict[str, float]:
        """
        Calculate the standard deviation of a list of numbers.

        Args:
            numbers (List[float]): List of numbers to calculate the standard deviation of.

        Returns:
            result (float): Standard deviation of the numbers.
        """
        if not numbers:
            return {"error": "Cannot calculate standard deviation of an empty list"}
        try:
            mean = sum(numbers) / len(numbers)
            variance = sum((x - mean) ** 2 for x in numbers) / len(numbers)
            return {"result": math.sqrt(variance)}
        except TypeError:
            return {"error": "All elements in the list must be numbers"}

    def si_unit_conversion(
        self, value: float, unit_in: str, unit_out: str
    ) -> Dict[str, float]:
        """
        Convert a value from one SI unit to another.

        Args:
            value (float): Value to be converted.
            unit_in (str): Unit of the input value.
            unit_out (str): Unit to convert the value to.

        Returns:
            result (float): Converted value in the new unit.
        """
        to_meters = {"km": 1000, "m": 1, "cm": 0.01, "mm": 0.001, "um": 1e-6, "nm": 1e-9}
        from_meters = {unit: 1 / factor for unit, factor in to_meters.items()}

        if not isinstance(value, (int, float)):
            return {"error": "Value must be a number"}

        if unit_in not in to_meters or unit_out not in from_meters:
            return {
                "error": f"Conversion from '{unit_in}' to '{unit_out}' is not supported"
            }

        try:
            value_in_meters = value * to_meters[unit_in]
            result = value_in_meters * from_meters[unit_out]
            return {"result": result}
        except OverflowError:
            return {"error": "Conversion resulted in a value too large to represent"}

    def imperial_si_conversion(
        self, value: float, unit_in: str, unit_out: str
    ) -> Dict[str, float]:
        """
        Convert a value between imperial and SI units.

        Args:
            value (float): Value to be converted.
            unit_in (str): Unit of the input value.
            unit_out (str): Unit to convert the value to.

        Returns:
            result (float): Converted value in the new unit.
        """
        conversion = {
            "cm_to_in": 0.393701,
            "in_to_cm": 2.54,
            "m_to_ft": 3.28084,
            "ft_to_m": 0.3048,
            "m_to_yd": 1.09361,
            "yd_to_m": 0.9144,
            "km_to_miles": 0.621371,
            "miles_to_km": 1.60934,
            "kg_to_lb": 2.20462,
            "lb_to_kg": 0.453592,
            "celsius_to_fahrenheit": 1.8,
            "fahrenheit_to_celsius": 5 / 9,
        }

        if not isinstance(value, (int, float)):
            return {"error": "Value must be a number"}

        if unit_in == unit_out:
            return {"result": value}

        conversion_key = f"{unit_in}_to_{unit_out}"
        if conversion_key not in conversion:
            return {
                "error": f"Conversion from '{unit_in}' to '{unit_out}' is not supported"
            }

        try:
            if unit_in == "celsius" and unit_out == "fahrenheit":
                result = (value * conversion[conversion_key]) + 32
            elif unit_in == "fahrenheit" and unit_out == "celsius":
                result = (value - 32) * conversion[conversion_key]
            else:
                result = value * conversion[conversion_key]

            return {"result": result}
        except OverflowError:
            return {"error": "Conversion resulted in a value too large to represent"}

    def add(self, a: float, b: float) -> Dict[str, float]:
        """
        Add two numbers.

        Args:
            a (float): First number.
            b (float): Second number.

        Returns:
            result (float): Sum of the two numbers.
        """
        try:
            return {"result": a + b}
        except TypeError:
            return {"error": "Both inputs must be numbers"}

    def subtract(self, a: float, b: float) -> Dict[str, float]:
        """
        Subtract one number from another.

        Args:
            a (float): Number to subtract from.
            b (float): Number to subtract.

        Returns:
            result (float): Difference between the two numbers.
        """
        try:
            return {"result": a - b}
        except TypeError:
            return {"error": "Both inputs must be numbers"}

    def multiply(self, a: float, b: float) -> Dict[str, float]:
        """
        Multiply two numbers.

        Args:
            a (float): First number.
            b (float): Second number.

        Returns:
            result (float): Product of the two numbers.
        """
        if not isinstance(a, (int, float)) or not isinstance(b, (int, float)):
            return {"error": "Both inputs must be numbers"}

        try:
            return {"result": a * b}
        except TypeError:
            return {"error": "Both inputs must be numbers"}

    def divide(self, a: float, b: float) -> Dict[str, float]:
        """
        Divide one number by another.

        Args:
            a (float): Numerator.
            b (float): Denominator.

        Returns:
            result (float): Quotient of the division.
        """
        try:
            if b == 0:
                return {"error": "Cannot divide by zero"}
            return {"result": a / b}
        except TypeError:
            return {"error": "Both inputs must be numbers"}

    def power(self, base: float, exponent: float) -> Dict[str, float]:
        """
        Raise a number to a power.

        Args:
            base (float): The base number.
            exponent (float): The exponent.

        Returns:
            result (float): The base raised to the power of the exponent.
        """
        try:
            return {"result": base**exponent}
        except TypeError:
            return {"error": "Both inputs must be numbers"}

    def square_root(self, number: float, precision: int) -> Dict[str, float]:
        """
        Calculate the square root of a number with adjustable precision using the decimal module.

        Args:
            number (float): The number to calculate the square root of.
            precision (int): Desired precision for the result.

        Returns:
            result (float): The square root of the number, or an error message.
        """
        try:
            if number < 0:
                return {"error": "Cannot calculate square root of a negative number"}

            # Set the precision for the decimal context
            getcontext().prec = precision

            # Use Decimal for high-precision square root calculation
            decimal_number = Decimal(number)

            result = decimal_number.sqrt()
            return {"result": result}
        except (TypeError, InvalidOperation):
            return {
                "error": "Input must be a number or computation resulted in an invalid operation"
            }

    def absolute_value(self, number: float) -> Dict[str, float]:
        """
        Calculate the absolute value of a number.

        Args:
            number (float): The number to calculate the absolute value of.

        Returns:
            result (float): The absolute value of the number.
        """
        try:
            return {"result": abs(number)}
        except TypeError:
            return {"error": "Input must be a number"}

    def round_number(
        self, number: float, decimal_places: int = 0
    ) -> Dict[str, float]:
        """
        Round a number to a specified number of decimal places.

        Args:
            number (float): The number to round.
            decimal_places (int): [Optional] The number of decimal places to round to. Defaults to 0.

        Returns:
            result (float): The rounded number.
        """
        try:
            return {"result": round(number, decimal_places)}
        except TypeError:
            return {
                "error": "First input must be a number, second input must be an integer"
            }

    def percentage(self, part: float, whole: float) -> Dict[str, float]:
        """
        Calculate the percentage of a part relative to a whole.

        Args:
            part (float): The part value.
            whole (float): The whole value.

        Returns:
            result (float): The percentage of the part relative to the whole.
        """
        try:
            if whole == 0:
                return {"error": "Whole value cannot be zero"}
            return {"result": (part / whole) * 100}
        except TypeError:
            return {"error": "Both inputs must be numbers"}

    def min_value(self, numbers: List[float]) -> Dict[str, float]:
        """
        Find the minimum value in a list of numbers.

        Args:
            numbers (List[float]): List of numbers to find the minimum from.

        Returns:
            result (float): The minimum value in the list.
        """
        if not numbers:
            return {"error": "Cannot find minimum of an empty list"}
        try:
            return {"result": min(numbers)}
        except TypeError:
            return {"error": "All elements in the list must be numbers"}

    def max_value(self, numbers: List[float]) -> Dict[str, float]:
        """
        Find the maximum value in a list of numbers.

        Args:
            numbers (List[float]): List of numbers to find the maximum from.

        Returns:
            result (float): The maximum value in the list.
        """
        if not numbers:
            return {"error": "Cannot find maximum of an empty list"}
        try:
            return {"result": max(numbers)}
        except TypeError:
            return {"error": "All elements in the list must be numbers"}

    def sum_values(self, numbers: List[float]) -> Dict[str, float]:
        """
        Calculate the sum of a list of numbers.

        Args:
            numbers (List[float]): List of numbers to sum.

        Returns:
            result (float): The sum of all numbers in the list.
        """
        if not numbers:
            return {"error": "Cannot calculate sum of an empty list"}
        try:
            return {"result": sum(numbers)}
        except TypeError:
            return {"error": "All elements in the list must be numbers"}