'''
Created on 20-Jan-2022

@author: bthakur
'''

import inspect
import traceback
import random
import numpy as np
import sympy as sp


from sympy import (simplify, nsimplify, Rational, latex, Symbol, 
                   Eq)

from sympy.core.expr import UnevaluatedExpr as ueval
import matplotlib.pyplot as plt

from ...util import MathUtil
from xv.util import toIntOrFloat, list_to_str

from ._arithmetic_helper import ArithmeticHelper

# _irrational_number_helper
# IrrationalNumberHelper

class IrrationalNumberHelper(ArithmeticHelper):
    
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        pass


    def get_problem_sqrt_of_prime_number_is_irrational(self, **kwargs):
        pnhelper = self.get_prime_number_helper()
        prime_numbers = pnhelper.getPrimeNumbers(size = 25)

        num = random.choice(prime_numbers)
        num_sqrt = sp.sqrt(num)
        
        p, q, m = sp.symbols('p, q, m')
        
        
        
        
        prob_s = f"""
        Prove that ${latex(num_sqrt)}$ is an irrational number.
        """
        prob_s = prob_s.replace("\n", " ").strip()

        soln_stmts = []
        
        soln_stmt = f"""
        Let us assume that ${latex(num_sqrt)}$ is a rational number. 
        """
        soln_stmts.append(soln_stmt.strip())
        
        soln_stmt = f"""
        $\\displaystyle 
        \\begin{{aligned}}

        \\text{{Let }} 
        & {latex(num_sqrt)}
        = \\frac{{ {latex(p)} }}{{ {latex(q)} }}
        \\quad \\text {{ where p and q are coprime integers. }}
        \\\\ \\\\
        
        \\implies
        & {latex(num)}
        = \\frac{{ {latex(p ** 2)} }}{{ {latex(q ** 2)} }}
        \\\\ \\\\
        
        \\implies
        & {latex(num)} {latex(q ** 2)}
        = {latex(p ** 2)}
        \\qquad \\cdots \\text {{ (i) }}
        
        \\end{{aligned}}
        $
        """
        soln_stmts.append(soln_stmt.strip())
        
        soln_stmt = f"""
        As ${latex(p)}$ and ${latex(q)}
        $ are co-prime, so (i) can be true only if ${latex(num)}
        $ is a factor of ${latex(p)}
        \\qquad \\cdots \\text {{ (ii) }}
        $. 
        """
        soln_stmts.append(soln_stmt.strip())
        
        
        soln_stmt = f"""
        Let ${latex(p)} = {latex(num)} {latex(m)}
        \\quad \\text{{ where }} {latex(m)}$ is an integer.
        
        $\\displaystyle 
        \\begin{{aligned}}

        \\implies
        & {latex(num)} {latex(q ** 2)}
        = {{ \\left( {latex(num)} {latex(m)} \\right) }}^2
        \\qquad \\text {{ from (i) }}
        \\\\ \\\\
        
        \\implies
        & {latex(num)} {latex(q ** 2)}
        = {{ {latex(num)}}}^2 \\;  {latex(m ** 2)} 
        \\\\ \\\\
        
        \\implies
        & {latex(q ** 2)}
        = {latex(num)}  {latex(m ** 2)} 
        \\qquad \\cdots \\text {{ (iii) }}
        
        \\end{{aligned}}
        $
        """
        soln_stmts.append(soln_stmt.strip())
        
        soln_stmt = f"""
        As ${latex(p)}$ and ${latex(q)}
        $ are co-prime, so (ii) can be true only if ${latex(num)}
        $ is a factor of ${latex(q)}
        \\qquad \\cdots \\text {{ (iv) }}
        $. 
        
        So from (ii) and (iv), ${latex(num)}
        $ if factor of both ${latex(p)}$ and ${latex(q)}
        $ both but they are co-prime which are contradictory. So our assumption that $
        {latex(num_sqrt)}
        $ is a rational number must be wrong.
        
        Therefore, ${latex(num_sqrt)}
        $ is an irrational number.
        """
        soln_stmts.append(soln_stmt.strip())
        
        
        answer_s = f"""
        NA
        """
        answer_s = answer_s.replace("\n", " ").strip()
        
        soln_s = "\n\n".join(soln_stmts)
        
        result = {
            'prob_s': prob_s.strip(), 
            'soln_s': soln_s.strip(), 
            'answer_s': answer_s.strip(), 
            }
        
        return result
    
    


    
    pass
#class IrrationalNumberHelper:
