Exemplo n.º 1
0
def tanh(argument: float) -> float:
    """Returns an approximation of the value of tanh('argument').

    Args:
        argument (float): Input to hyperbolic tan function

    Returns:
        float: Approximation of the value of tanh('argument')
    """
    e = exp.pow_e(argument)
    return (e - common.inverse(e)) / (e + common.inverse(e))
Exemplo n.º 2
0
 def test_exp_input_big(self):
     # Tests euler number function's capacity for handling big numbers
     self.assertAlmostEqual(
         exp.pow_e(32), pow(math.e, 32)
     )  # Accuracy needs work for big numbers, decimal places incorrect
     self.assertAlmostEqual(exp.pow_e(18), pow(math.e, 18))
Exemplo n.º 3
0
 def test_exp_input_transcendental(self):
     # Tests euler number function's capacity for handling other transcendental numbers such as pi as input
     self.assertAlmostEqual(exp.pow_e(trig.generate_pi()),
                            pow(math.e, math.pi))
     self.assertAlmostEqual(exp.pow_e(exp.pow_e()), pow(math.e, math.e))
Exemplo n.º 4
0
 def test_exp_input_rational(self):
     # Tests euler number function's capacity for handling non-integer inputs.
     self.assertAlmostEqual(exp.pow_e(0.5), math.pow(math.e, 0.5), 12)
     self.assertAlmostEqual(exp.pow_e(5 / 8), math.pow(math.e, 5 / 8), 12)
Exemplo n.º 5
0
 def test_exp_input_neg(self):
     # Tests euler number function's capacity for handling negative inputs.
     self.assertAlmostEqual(exp.pow_e(-10), math.pow(math.e, -10), 12)
     self.assertAlmostEqual(exp.pow_e(-3), math.pow(math.e, -3), 12)
Exemplo n.º 6
0
 def test_exp_input_0(self):
     # Tests euler number function's capacity for handling input of 0.
     self.assertAlmostEqual(exp.pow_e(0), math.pow(math.e, 0), 12)
Exemplo n.º 7
0
 def test_e(self):
     # Tests euler number function's capacity for generating euler number.
     self.assertAlmostEqual(exp.pow_e(), math.e, 15)
Exemplo n.º 8
0
    def __init__(self: object, is_rad: bool, is_binary: bool) -> None:
        """Define grammar to be used by parser and parse actions to be used in constructing the symbol stack.

        Args:
            is_rad (bool): Angle mode
            is_binary (bool): Binary input option
        """

        # Settings
        self._is_rad = is_rad
        self._is_binary = is_binary

        # Expressions
        expr = Forward()

        # Operations
        plus, minus, multiply, divide, mod = map(Literal, "+-*/%")
        left_bracket, right_bracket = map(Suppress, "()")
        addition_operation = plus | minus
        multiplication_operation = multiply | divide | mod
        power_operation = Literal("^")
        factorial_operation = Literal("!")

        # Functions
        def add_arg_count_to_tokens(tokens):
            function_id = tokens.pop(0)
            arg_count = len(tokens[0])
            tokens.insert(0, (function_id, arg_count))

        function_id = Word(alphas)
        # Expressions must be in groups so that we can count them separately.
        argument_list = delimitedList(Group(expr))
        function = (function_id + left_bracket + Group(argument_list) + right_bracket
                    ).setParseAction(add_arg_count_to_tokens)

        # Values
        e = CaselessKeyword("E")
        pi = CaselessKeyword("PI")
        number = Regex(r"[+-]?\d+(?:\.\d*)?(?:[eE][+-]?\d+)?")
        value = (e | pi | number)

        # Expression grammars. Order of operations is determined here.
        factor = Forward()
        atom = (addition_operation[...] + (
                (function | value).setParseAction(self.push_first) |
                Group(left_bracket + expr + right_bracket)
        )).setParseAction(self.push_unary_operator)
        factor << atom + (
                (power_operation + factor) |
                factorial_operation
        ).setParseAction(self.push_first)[...]
        term = factor + (
                multiplication_operation + factor
        ).setParseAction(self.push_first)[...]
        expr << term + (
                addition_operation + term
        ).setParseAction(self.push_first)[...]
        self.bnf = expr

        # Mapping between constant names and providers
        self.constant_map = {"PI": trigonometry.generate_pi(),
                             "E": exponents_and_logs.pow_e(1),
                             }
        # Mapping between operators and appropriate function calls
        self.operation_map = {"+": lambda a, b: a + b,
                              "-": lambda a, b: a - b,
                              "*": lambda a, b: a * b,
                              "/": lambda a, b: a / b,
                              "%": lambda a, b: a % b,
                              "^": exponents_and_logs.pow,
                              "!": common.factorial
                              }
        # Mapping between function ids and appropriate function calls.
        # Remember that function names must only contain letters.
        self.function_map = {
            # Exponential and logarithmic functions
            "sqrt": lambda a: exponents_and_logs.radical(a, 2),
            "radical": exponents_and_logs.radical,
            "root": exponents_and_logs.radical,
            "pow": exponents_and_logs.pow,
            "powTen": exponents_and_logs.pow_10,
            "powPi": exponents_and_logs.pow_pi,
            "powE": exponents_and_logs.pow_e,
            "exp": exponents_and_logs.pow_e,
            "ln": exponents_and_logs.ln,
            "log": exponents_and_logs.log,
            # Statistics functions
            "mean": statistic.mean,
            "mad": statistic.mad,
            "std": statistic.std
        }

        # Mapping between trig function ids and appropriate function calls.
        # Remember that function names must only contain letters.
        self.trig_map = {
            # Basic trigonometry functions
            "sin": trigonometry.sin,
            "cos": trigonometry.cos,
            "tan": trigonometry.tan,
            # Hyperbolic trigonometry functions
            "sinh": trigonometry.sinh,
            "cosh": trigonometry.cosh,
            "tanh": trigonometry.tanh,
        }