'''
Created on 12-Jul-2021

@author: bthakur
'''
import random
import inspect
import traceback
import re
from sympy import (Eq, symbols, sqrt, latex,
                   Rational, solve, log, nsimplify, E, exp,  
                   sin, cos, I, series, pi)
from sympy.abc import theta
import sympy

from xv.km import CustomLogarithm as Log
from xv.km.util import latex_fraction, latex_exponent


class EulerNumberHelper:
    def __init__(self, **kwargs):
        self.verbose = kwargs.get('verbose', False)
        self.e = E
        self.implies = symbols(u'\U000021D2')
        pass

    def  get_definition(self):
        s = r"""
        ${\displaystyle e=\lim_{n\to \infty }\left(1+{\frac {1}{n}}\right)^{n}}$

        We can also write:
        
        ${\displaystyle e=\lim _{n\to 0}\left(1+n\right)^{\frac {1}{n}}}$

        As a series:
        
        ${\displaystyle e=\sum \limits _{n=0}^{\infty }{\frac {1}{n!}}=1+{\frac {1}{1}}+{\frac {1}{1\cdot 2}}+{\frac {1}{1\cdot 2\cdot 3}}+\cdots }$
        
        ${\displaystyle e=\sum \limits _{n=0}^{\infty }{\frac {1}{n!}}={\frac {1}{0!}}+{\frac {1}{1!}}+{\frac {1}{2!}}+{\frac {1}{3!}}+\cdots }$
        
        """
        return s
        pass
    
    def as_series(self):
        s = r"""
        ${\displaystyle e=\sum \limits _{n=0}^{\infty }{\frac {1}{n!}}=1+{\frac {1}{1}}+{\frac {1}{1\cdot 2}}+{\frac {1}{1\cdot 2\cdot 3}}+\cdots }$
        """
        return s
        pass

    def exp_x(self):
        s = r"""
        $e^{x}=1+{x \over 1!}+{x^{2} \over 2!}+{x^{3} \over 3!}+\cdots =\sum _{n=0}^{\infty }{\frac {x^{n}}{n!}}$
        """
        return s
        pass
    
    
    def expr_cos(self):
        expr = (exp(I * theta) + exp(-I * theta))/2
        return expr
        pass
    
    def expr_sin(self):
        expr = (exp(I * theta) - exp(-I * theta)) / 2 * I
        return expr
        pass
    
    def expr_cos_latex(self):
        s = r"""
        \cos x={\dfrac {e^{ix}+e^{-ix}}{2}}
        """
        return s 
        pass
    
    def expr_sin_latex(self):
        s = r"""
        \sin x={\dfrac {e^{ix}-e^{-ix}}{2i}}
        """
        return s 
        pass
    

    def get_detailed_content(self):
        s = r"""
         The number e , also known as Euler's number , is a mathematical constant approximately equal to 2.71828, and can be characterized in many ways. It is the base of the natural logarithm . [1] [2] [3] It is the limit of (1 + 1/n)n as n approaches infinity, an expression that arises in the study of compound interest . It can also be calculated as the sum of the infinite series [4] [5]
        
        
        
        ${\displaystyle e=\sum \limits _{n=0}^{\infty }{\frac {1}{n!}}=1+{\frac {1}{1}}+{\frac {1}{1\cdot 2}}+{\frac {1}{1\cdot 2\cdot 3}}+\cdots }$
        
        The discovery of the constant itself is credited to Jacob Bernoulli in 1683, [13] [14] who attempted to find the value of the following expression (which is equal to e ):
        
        
        
        $\lim _{n\to \infty }\left(1+{\frac {1}{n}}\right)^{n}.$
        
        This is an example of a Bernoulli trial process. Each time the gambler plays the slots, there is a one in n chance of winning. Playing n times is modeled by the binomial distribution , which is closely related to the binomial theorem and Pascal's triangle . The probability of winning k times out of n trials is:
        
        
        
        ${\displaystyle {\binom {n}{k}}\left({\frac {1}{n}}\right)^{k}\left(1-{\frac {1}{n}}\right)^{n-k}.}$
        
        In particular, the probability of winning zero times ( k = 0 ) is
        
        
        
        ${\displaystyle \left(1-{\frac {1}{n}}\right)^{n}.}$
        
        The normal distribution with zero mean and unit standard deviation is known as the standard normal distribution , given by the probability density function
        
        
        
        ${\displaystyle \phi (x)={\frac {1}{\sqrt {2\pi }}}e^{-{\frac {1}{2}}x^{2}}.}$
        
        Another application of e , also discovered in part by Jacob Bernoulli along with Pierre Remond de Montmort , is in the problem of derangements , also known as the hat check problem : [20] n guests are invited to a party, and at the door, the guests all check their hats with the butler, who in turn places the hats into n boxes, each labelled with the name of one guest. But the butler has not asked the identities of the guests, and so he puts the hats into boxes selected at random. The problem of de Montmort is to find the probability that none of the hats gets put into the right box. This probability, denoted by 𝑝𝑛
        , is:
        
        
        
        ${\displaystyle p_{n}=1-{\frac {1}{1!}}+{\frac {1}{2!}}-{\frac {1}{3!}}+\cdots +{\frac {(-1)^{n}}{n!}}=\sum _{k=0}^{n}{\frac {(-1)^{k}}{k!}}.}$
        
        A stick of length L is broken into n equal parts. The value of n that maximizes the product of the lengths is then either [22]
        
        
        
        ${\displaystyle n=\left\lfloor {\frac {L}{e}}\right\rfloor }$
        
        The number e occurs naturally in connection with many problems involving asymptotics . An example is Stirling's formula for the asymptotics of the factorial function , in which both the numbers e and π appear:
        
        
        
        ${\displaystyle n!\sim {\sqrt {2\pi n}}\left({\frac {n}{e}}\right)^{n}.}$
        
        As a consequence,
        
        
        
        ${\displaystyle e=\lim _{n\to \infty }{\frac {n}{\sqrt[{n}]{n!}}}.}$
        
        The principal motivation for introducing the number e , particularly in calculus , is to perform differential and integral calculus with exponential functions and logarithms . [23] A general exponential function y = ax has a derivative, given by a limit :
        
        
        
        ${\displaystyle {\begin{aligned}{\frac {d}{dx}}a^{x}&=\lim _{h\to 0}{\frac {a^{x+h}-a^{x}}{h}}=\lim _{h\to 0}{\frac {a^{x}a^{h}-a^{x}}{h}}\\&=a^{x}\cdot \left(\lim _{h\to 0}{\frac {a^{h}-1}{h}}\right).\end{aligned}}}$
        
        The parenthesized limit on the right is independent of the variable x. Its value turns out to be the logarithm of a to base e . Thus, when the value of a is set to e, this limit is equal to 1, and so one arrives at the following simple identity:
        
        
        
        ${\frac {d}{dx}}e^{x}=e^{x}.$
        
        Another motivation comes from considering the derivative of the base- a logarithm (i.e., loga x ), [24] for x > 0 :
        
        
        
        ${\displaystyle {\begin{aligned}{\frac {d}{dx}}\log _{a}x&=\lim _{h\to 0}{\frac {\log _{a}(x+h)-\log _{a}(x)}{h}}\\&=\lim _{h\to 0}{\frac {\log _{a}(1+h/x)}{x\cdot h/x}}\\&={\frac {1}{x}}\log _{a}\left(\lim _{u\to 0}(1+u)^{\frac {1}{u}}\right)\\&={\frac {1}{x}}\log _{a}e,\end{aligned}}}$
        
        where the substitution u = h/x was made. The base- a logarithm of e is 1, if a equals e . So symbolically,
        
        
        
        ${\displaystyle {\frac {d}{dx}}\log _{e}x={\frac {1}{x}}.}$
        
        No Parent Tag
        
        
        
        $e=\lim _{n\to \infty }\left(1+{\frac {1}{n}}\right)^{n}$
        
        Similarly:
        
        
        
        ${\displaystyle e=\lim _{t\to 0}\left(1+t\right)^{\frac {1}{t}}}$
        
        No Parent Tag
        
        
        
        $\int _{1}^{e}{\frac {1}{t}}\,dt=1.$
        
        As in the motivation, the exponential function ex is important in part because it is the unique nontrivial function that is its own derivative (up to multiplication by a constant):
        
        
        
        ${\frac  {d}{dx}}e^{x}=e^{x}$
        
        and therefore its own antiderivative as well:
        
        
        
        ${\displaystyle \int e^{x}\,dx=e^{x}+C.}$
        
        The number e is the unique real number such that
        
        
        
        ${\displaystyle \left(1+{\frac {1}{x}}\right)^{x}<e<\left(1+{\frac {1}{x}}\right)^{x+1}}$
        
        Also, we have the inequality
        
        
        
        ${\displaystyle e^{x}\geq x+1}$
        
        Steiner's problem asks to find the global maximum for the function
        
        
        
        ${\displaystyle f(x)=x^{\frac {1}{x}}.}$
        
        Similarly, x = 1/e is where the global minimum occurs for the function
        
        
        
        ${\displaystyle f(x)=x^{x}}$
        
        defined for positive x . More generally, for the function
        
        
        
        ${\displaystyle f(x)=x^{x^{n}}}$
        
        The infinite tetration
        
        
        
        $x^{x^{x^{\cdot ^{\cdot ^{\cdot }}}}}$
        
        The exponential function ex may be written as a Taylor series
        
        
        
        $e^{x}=1+{x \over 1!}+{x^{2} \over 2!}+{x^{3} \over 3!}+\cdots =\sum _{n=0}^{\infty }{\frac {x^{n}}{n!}}$
        
        Because this series is convergent for every complex value of x , it is commonly used to extend the definition of ex to the complex numbers. This, with the Taylor series for sin and cos x , allows one to derive Euler's formula :
        
        
        
        ${\displaystyle e^{ix}=\cos x+i\sin x,}$
        
        which holds for every complex x . The special case with x = π is Euler's identity :
        
        
        
        ${\displaystyle e^{i\pi }+1=0,}$
        
        from which it follows that, in the principal branch of the logarithm,
        
        
        
        ${\displaystyle \ln(-1)=i\pi .}$
        
        Furthermore, using the laws for exponentiation,
        
        
        
        ${\displaystyle (\cos x+i\sin x)^{n}=\left(e^{ix}\right)^{n}=e^{inx}=\cos(nx)+i\sin(nx),}$
        
        The expression
        
        
        
        ${\displaystyle \cos x+i\sin x}$
        
        The expressions of sin x and cos x in terms of the exponential function can be deduced:
        
        
        
        ${\displaystyle \sin x={\frac {e^{ix}-e^{-ix}}{2i}},\qquad \cos x={\frac {e^{ix}+e^{-ix}}{2}}.}$
        
        The family of functions
        
        
        
        ${\displaystyle y(x)=Ce^{x},}$
        
        where C is any real number, is the solution to the differential equation
        
        
        
        ${\displaystyle y'=y.}$
        
        The number e can be represented in a variety of ways: as an infinite series , an infinite product , a continued fraction , or a limit of a sequence . Two of these representations, often used in introductory calculus courses, are the limit
        
        
        
        ${\displaystyle e=\lim _{n\to \infty }\left(1+{\frac {1}{n}}\right)^{n},}$
        
        given above, and the series
        
        
        
        $e=\sum _{n=0}^{\infty }{\frac {1}{n!}}$
        
        Less common is the continued fraction
        
        
        
        ${\displaystyle e=[2;1,2,1,1,4,1,1,6,1,...,1,2n,1,...],}$
        
        which written out looks like
        
        
        
        ${\displaystyle e=2+{\cfrac {1}{1+{\cfrac {1}{2+{\cfrac {1}{1+{\cfrac {1}{1+{\cfrac {1}{4+{\cfrac {1}{1+{\cfrac {1}{1+\ddots }}}}}}}}}}}}}}.}$
        
        This continued fraction for e converges three times as quickly: [citation needed]
        
        
        
        ${\displaystyle e=1+{\cfrac {2}{1+{\cfrac {1}{6+{\cfrac {1}{10+{\cfrac {1}{14+{\cfrac {1}{18+{\cfrac {1}{22+{\cfrac {1}{26+\ddots }}}}}}}}}}}}}}.}$
        
        In addition to exact analytical expressions for representation of e , there are stochastic techniques for estimating e . One such approach begins with an infinite sequence of independent random variables X1 , X2 ..., drawn from the uniform distribution on [0, 1]. Let V be the least number n such that the sum of the first n observations exceeds 1:
        
        
        
        ${\displaystyle V=\min \left\{n\mid X_{1}+X_{2}+\cdots +X_{n}>1\right\}.}$
        
        
        """
        return s
        pass
    
    
    pass
#class EulerNumberHelper:
