import numpy as np

def dynamic_paralax(m_A, m_B, alpha, P):
    """
    Urci hmotnosti zloziek a paralaxu.
    
    Args:
        m_A: Zdanliva hviezdna velkost zlozky A.
        m_B: Zdanliva hviezdna velkost zlozky B.
        alpha: Vzdialenost zloziek v uhlovych sekundach.
        P: Perioda v rokoch.
        
    Returns:
        mass_A: Hmotnost zlozky A.
        mass_B: Hmotnost zlozky B.
        pi: Paralaxa v uhlovych sekundach.
    """
    mass_A = 1   # hmotnost v hmotnostiach Slnka
    mass_B = 1
    pi = 1   # paralaxa v uhlovych sekundach
    delta_pi = 1
    M_S = 4.75   # absolutna hviezdna velkost Slnka
    while delta_pi > pi/1000:
        a = (P**2*(mass_A + mass_B))**(1/3)
        pi_old = pi
        pi = alpha/a
        delta_pi = np.abs(pi_old - pi)
        M_A = m_A + 5*np.log10(pi) + 5   # absolutna hviezdna velkost
        M_B = m_B + 5*np.log10(pi) + 5
        L_A = 10**(0.4*(M_S - M_A))   # ziarivy vykon zlozky A
        L_B = 10**(0.4*(M_S - M_B))
        mass_A = get_mass(L_A, mass_A)
        mass_B = get_mass(L_B, mass_B)
    return mass_A, mass_B, pi

def get_mass(L, M_0):
    """
    Vypocita hmotnost z Mass-Luminosity vztahu.
    
    Args:
        L: Ziarivy vykon hviezdy.
        M_0: Pociatocny odhad hmotnosti hviezdy.
    
    Returns:
        M: Hmotnost hviezdy.
    """
    if M_0 < 0.43:
        return (L/0.23)**(1/2.3)
    elif M_0 < 2:
        return L**(1/3.5)
    elif M_0 < 20:
        return (L/1.5)**0.25
    else:
        return L/3200

mass_A, mass_B, pi = dynamic_paralax(0, 1.2, 17.58, 79.92)
print("Hmotnosti zloziek alpha Cen su {} a {} hmotnosti Slnka a paralaxa {}."
      .format(np.round(mass_A, 3), np.round(mass_B, 3), np.round(pi, 3)))
