'''
Created on 20-Jan-2022

@author: bthakur
'''

import inspect
import traceback
import random
import numpy as np
from sympy import (latex, Symbol, symbols,
                   UnevaluatedExpr as ueval,
                   )

from xv.util import toIntOrFloat, listAttr
from xv.km.storeddata import BaseStoredQuestions
from xv.km import CustomLogarithm as Log
from xv.km.util import latex_log, latex_fraction, latex_exponent

class LogarithmStoredQuestions(BaseStoredQuestions):
    _QUESTION_KEYWORDS = None
    
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        pass
    
    
    def get_question_q1(self):
        a = random.choice([random.randint(2, 3), 
                           Symbol('a'),
                           Symbol('a') * 2,
                           Symbol('a') + 3
                           ])
        b = random.choice([random.randint(2, 5), 
                           Symbol('b'),
                           ])
        c = random.choice([random.randint(2, 3), 
                           Symbol('x'),
                           Symbol('x') * 2,
                           Symbol('x') * 3
                           ])
        
        
        keywords = [
            'log',
            '512',
            'log5',
            'log2',
            'log3',
            'root and log'
            ]
        
        prob_s = rf"""
        Find the value of 
        $\log _{{{latex(a)}}} \log _{{2}} \log _{{{latex(c)}}} \log _{{{b}}} {latex(b**(c**2))} $
        """
        
        soln_s = rf"""
            $\log _{{{latex(a)}}} \log _{{2}} \log _{{{latex(c)}}} \log _{{{b}}} {latex(b**(c**2))} $
            $$
            \begin{{aligned}}
            &=\log _{{{latex(a)}}} \log _{{2}} \log _{{{latex(c)}}} \log _{{{b}}}\left({b}^{{{latex(c**2)}}}\right) \\
            &=\log _{{{latex(a)}}} \log _{{2}} \log _{{{latex(c)}}} \left({latex(c**2)}\right) \\
            &=\log _{{{latex(a)}}} \log _{{2}} \log _{{{latex(c)}}} \left({latex(c)}\right)^{{2}} \\
            &=\log _{{{latex(a)}}} \log _{{2}} 2 \\
            &=\log _{{{latex(a)}}} 1 \\
            &=\log _{{{latex(a)}}} \left({{{latex(a)}}}\right)^0 \\
            &=0
            \end{{aligned}}
            $$
        """
        
        answer_s = rf"""
        0
        """

        result = {
            'keywords': keywords,
            'prob_s': prob_s.strip(),
            'soln_s': soln_s.strip(),
            'answer_s': answer_s.strip(),
            }
        return result
        pass
    

    
    def get_question_q3(self):
        keywords = [
            'log',
            'log2/5',
            'log10',
            ]
        
        prob_s = rf"""
        Prove that 
        $\frac{{2}}{{5}}<\log _{{10}} 3<\frac{{1}}{{2}}$
        """
        
        soln_s = rf"""
        $\log _{{10}} 3 \quad ? \quad \frac{{2}}{{5}}$
        $$
        \begin{{array}}{{ll}}
        \Rightarrow 3 \quad ? \quad 10^{{\frac{{2}}{{5}}}} \\
        \Rightarrow 3^{{5}} > 10^{{2}},  \\
        \\
        \text {{Now }} \quad \log _{{10}} 3 \quad ? \quad \frac{{1}}{{2}} \\
        \Rightarrow \quad 3 \quad ? \quad 10^{{\frac{{1}}{{2}}}} \\
        \Rightarrow \quad 3^{{2}} < 10, \text {{ which is true }} \\
        \\
        \text {{Hence }} \quad \frac{{2}}{{5}}<\log _{{10}} 3<\frac{{1}}{{2}}
        \end{{array}}
        $$

        """
        
        answer_s = rf"""
        $\frac{{2}}{{5}}<\log _{{10}} 3<\frac{{1}}{{2}}$
        """

        result = {
            'keywords': keywords,
            'prob_s': prob_s.strip(),
            'soln_s': soln_s.strip(),
            'answer_s': answer_s.strip(),
            }
        return result
        pass
    
    
    
    def get_question_q5(self):
        keywords = [
            'log',
            'iit',
            ]
        
        prob_s = rf"""
        Find value of {{x}} if
        
        $$
        (2 x)^{{\ln 2}}=(3 y)^{{\ln 3}}
        $$
        $\displaystyle 3^{{\ln x}}=2^{{\ln y}}$
        """
        
        
        
        soln_s = rf"""

        Given that $\displaystyle (2 x)^{{\ln 2}}=(3 y)^{{\ln 3}}$..(i)
        
        Taking log on both sides
        $\displaystyle \Rightarrow \log 2 \log (2 x)=\log 3 \log (3 y)$
        $\displaystyle \Rightarrow \log 2(\log 2+\log x)=\log 3(\log 3+\log y)$
        
        Let:
        $\log 2 = {{m}}  $
        $\log 3 = {{n}}  $
        $\log x = {{p}}  $
        $\log y = {{q}}  $
        
        $\displaystyle \Rightarrow {{m}}({{m}}+{{p}})={{n}}({{n}}+{{q}})$..(ii)
        
        
        Also $\displaystyle 3^{{\ln x}}=2^{{\ln y}}$..(iii)
        
        Taking log on both sides
        $\displaystyle \Rightarrow \log x \log 3=\log y \log 2$
        $\displaystyle \Rightarrow {{p}} {{n}}={{q}} {{m}} \ldots$ (iv)
        

        Now,
        $\displaystyle \Rightarrow {{m}}({{m}}+{{p}})={{n}}({{n}}+{{q}})$ from ..(ii)
        
        Multiply both sides with ${{m}}$,
        $\displaystyle \Rightarrow {{m}}^2({{m}}+{{p}})={{m}}{{n}}({{n}}+{{q}})$
        $\displaystyle \Rightarrow {{m}}^3 + {{m}}^2{{p}}={{m}}{{n}}^2 + {{m}}{{q}}{{n}}$
        
        From ..(iv)
        $\displaystyle \Rightarrow {{m}}^3 + {{m}}^2{{p}}={{m}}{{n}}^2 + {{p}}{{n}}{{n}}$
        $\displaystyle \Rightarrow {{m}}^3 + {{m}}^2{{p}}={{m}}{{n}}^2 + {{p}}{{n}}^2$
        $\displaystyle \Rightarrow  {{m}}^2{{p}} - {{p}}{{n}}^2 = {{m}}{{n}}^2 - {{m}}^3 $
        $\displaystyle \Rightarrow {{p}} \left( {{m}}^2 - {{n}}^2 \right) = {{m}} \left( {{n}}^2 - {{m}}^2 \right) $
        $\displaystyle \Rightarrow {{p}} = - {{m}} $
        
        Substituting back the values:
        $\displaystyle \Rightarrow \log x = - \log 2$
        $\displaystyle \Rightarrow \log x = \log 2^{{-1}}$
        $\displaystyle \Rightarrow x = 2^{{-1}}$
        $\displaystyle \Rightarrow x = 1/2$

        """
        
        answer_s = rf"""
        $1 / 2$
        """

        result = {
            'keywords': keywords,
            'prob_s': prob_s.strip(),
            'soln_s': soln_s.strip(),
            'answer_s': answer_s.strip(),
            }
        return result
        pass
    
    
    def get_question_q6(self):
        keywords = [
            'log',
            'iit',
            ]
        
        prob_s = rf"""
        Find $x=$ if 
        
        $3^{{x}}=4^{{x-1}}$
        """
        
        soln_s = rf"""
        Given that $3^{{x}}=4^{{x-1}}$
        
        Take log on both sides
        $$
        \begin{{aligned}}
        &\log 3^{{x}}=\log 4^{{x-1}} \\
        \\
        &\Rightarrow x \log 3 = (x-1) \log 4 \\
        \\
        &\Rightarrow x \log 3 = x \log 4-\log 4 \\
        \\
        &\Rightarrow x \log 3 - x \log 4 = -\log 4 \\
        \\
        &\Rightarrow x(\log 4-\log 3) = \log 4  \\
        \\
        &\Rightarrow x = \frac {{\log 4}} {{\log 4-\log 3}}  \\
        \\
        &\text {{Diving rhs numerator and denominator by $\log 4$ }} \\
        \\
        &\Rightarrow x =  \frac {{1}} {{ 1 -  \frac {{\log 3}} {{\log 4}}  }}\\
        \\
        &\Rightarrow x =  \frac {{1}} {{ 1 - \log _{{4}} 3  }}\\
        \end{{aligned}}
        $$
        
        """
        
        answer_s = rf"""
        $$\Rightarrow \frac {{1}} {{ 1 - \log _{{4}} 3  }}$$
        """

        result = {
            'keywords': keywords,
            'prob_s': prob_s.strip(),
            'soln_s': soln_s.strip(),
            'answer_s': answer_s.strip(),
            }
        return result
        pass
    
    
    def get_question_q7(self):
        keywords = [
            'log',
            'iit',
            ]
        
        prob_s = rf"""
        Find the number of positive integers satisfying the equation 
        
        $x+\log _{{10}}\left(2^{{x}}+1\right)=x$ $\log _{{10}} 5+\log _{{10}} 6$
        """
        
        soln_s = rf"""
        $$
        \begin{{aligned}}

        & x + \log _{{10}}\left(2^{{x}}+1\right)=x \log _{{10}} 5+\log _{{10}} 6 \\
        \\
        &\Rightarrow \log _{{10}}{{10}}^{{x}}+\log _{{10}}\left(2^{{x}}+1\right)= \log _{{10}} 5^x+\log _{{10}} 6 \\
        \\

        &\Rightarrow \log _{{10}} \left( {{10}}^{{x}} * \left( 2^{{x}}+1 \right) \right) = \log _{{10}} \left( 5^x * 6 \right) \\
        \\
        
        &\text {{Removing log on both sides}} \\
        \\
        &\Rightarrow  {{10}}^{{x}} * \left( 2^{{x}}+1 \right) = 5^x * 6 \\
        \\
        &\text {{Diving both sides by $5^x$ }} \\
        \\
        &\Rightarrow  {{2}}^{{x}} * \left( 2^{{x}}+1 \right) =  6 \\
        \\
        &\Rightarrow  \left(2^{{x}}\right)^{{2}}+2^{{x}}-6=0 \\
        \\
        &\text {{Solving quadratic equation for $2^x$ }} \\
        \\
        &\Rightarrow  2^{{x}}=2 \text {{ or }} -3 \\
        \\
        &\text {{ The second value does not satisfy the condition of positive integer. Hence, }} \\
        \\
        &\Rightarrow  2^{{x}} = 2 \\
        \\
        &\Rightarrow  {{x}} = 1 \\
         \end{{aligned}}
        $$
        So number of possible solutions of ${{x}}$ is 1.
        """
        
        answer_s = rf"""
        1
        """

        result = {
            'keywords': keywords,
            'prob_s': prob_s.strip(),
            'soln_s': soln_s.strip(),
            'answer_s': answer_s.strip(),
            }
        return result
        pass


    def get_question_q8(self):
        keywords = [
            'log',
            'summary',
            'log summary',
            ]
        
        prob_s = rf"""
            $\displaystyle \text {{For what range of values for x and b, }} \log _{{b}} x \text {{ is defined}}? $
            
            If $\displaystyle \log _{{b}} a=c$, what is the value of  $a$?
            
            Find value of $\displaystyle \log _{{b}} 1$.
            
            Find value of $\displaystyle \log _{{b}} b$.
            
            Find value of $\displaystyle \log _{{a}} b * \log _{{b}} a$.
            
            Expand the log in $\displaystyle \log _{{b}} x y$
            
            Expand the log in $\displaystyle \log _{{b}} \left( \frac x y \right)$
            
            Find value of $\displaystyle \log _{{b}} x^{{m}}$
            
            Remove ${{n}}$ from base in $\displaystyle \log _{{b^n}} x$
            
            Find value of $\displaystyle (b)^{{\log b x}}$
        """
        prob_s = self.convert_str_to_ordered_list(prob_s, list_style = 'lower-roman') 
        prob_s = f"""
        Answer the followings:
        {prob_s}
        """       
        
        
        soln_s = rf"""
            $\displaystyle \log _{{b}} x$ is defined for $x>0, b>0, b \neq 1$.
            
            If $\displaystyle \log _{{b}} a=c$ \Rightarrow $a=b^{{c}}$
            
            $\displaystyle \log _{{b}} 1 = \log _{{b}} b^0 = 0 $
            
            $\displaystyle \log _{{b}} b = \log _{{b}} b^1 = 1  $
            
            $\displaystyle \log _{{a}} b * \log _{{b}} a =  1$
            
            $\displaystyle \log _{{b}} \left( x y \right) = \log _{{b}} x+\log _{{b}} y$
            
            $\displaystyle \log _{{b}} \left( \frac x y \right) = \log _{{b}} x - \log _{{b}} y$
            
            $\displaystyle \log _{{b}} x^{{m}}=m \log _{{b}} x$
            
            $\displaystyle \log _{{b^n}} x =  \log _{{b}} {{ x ^ {{ \frac 1 n }} }} $
            
            $\displaystyle (b)^{{\log b x}} = x $
        """

        soln_s = self.convert_str_to_ordered_list(soln_s, list_style = 'lower-roman') 
        
        answer_s = soln_s

        result = {
            'keywords': keywords,
            'prob_s': prob_s.strip(),
            'soln_s': soln_s.strip(),
            'answer_s': answer_s.strip(),
            }
        return result
        pass
    
    
    
    pass
#class LogarithmStoredQuestions:
