// PreProcessingTM_GivenDiscriminant.m

/*
INPUT:
    C:= [c_1, c_2, c_3, c_4], the coefficients of the Thue-Mahler form, F, where
        F:= c_1*x^3 + c_2*x^2*y + c_3*x*y^2 + c_4*y^3, having discriminant DF:= +/- 2^{alpha_0} * 3^{beta_0} * N_1
    
    p:= a set of rational primes [2, 3 , ..., p_s] such that
        F(u,v) = 2^{alpha_1} * 3^{beta_1} * p_3^{k_3} * p_4^{k_4} *...* p_s^{k_s}
    
OUTPUT:
    if Ord_2(Disc(F)) == 4:
        Bounda:= 1, where
            Bounda:= the bound on alpha_1
    else:
        Bounda:= 2^15, a placeholder value, where
            Bounda:= the bound on alpha_1
                NB. here, not enough information is known to bound alpha_1
    
    if Ord_3(Disc(F)) >= 2:
        Boundb:= 1, where
            Boundb:= the bound on beta_1
    else:
        Boundb:= 2^15, a placeholder value, where
            Bounda:= the bound on beta_1
                NB. here, not enough information is known to bound beta_1
                
COMMENTS:
    Given a Thue-Mahler form F, F:= c_1*x^3 + c_2*x^2*y + c_3*x*y^2 + c_4*y^3, having discriminant DF:= +/- 2^{alpha_0} * 3^{beta_0} * N_1, and primes p, one can compute solutions to 
        F(u,v) = 2^{alpha_1} * 3^{beta_1} * p_3^{k_3} * p_4^{k_4} *...* p_s^{k_s}
    leading to elliptic curves having good reduction outside p
    
    This algorithm determines a potential upper bound on alpha_1, beta_1 as per Theorem 1 of the reference
    
REFERENCE:
    M. Bennett, A. Gherga, A. Rechnitzer. Computing Elliptic Curves over Q. Submitted, University of British Columbia, 2017.

EXAMPLE:
    > p:= [2,3,23]; 
    > C:= [1,0,-18,-6];
    > Bounda, Boundb:= PreProcessingTM_GivenDiscriminant(C,p);
    > Bounda;
    32768
    > Boundb;
    1
*/


function PreProcessingTM_GivenDiscriminant(C,p)
    Sort(~p);
    R<x,y>:= PolynomialRing(Integers(),2); 
    F:= C[1]*x^3 + C[2]*x^2*y + C[3]*x*y^2 + C[4]*y^3;  // generates the relevant Thue-Mahler polynomial, F
    DF:= -27*C[1]^2*C[4]^2 + C[2]^2*C[3]^2 + 18*C[1]*C[2]*C[3]*C[4] - 4*C[1]*C[3]^3 - 4*C[2]^3*C[4];    // computes the discriminant of F
    DF_fac:= [f[1] : f in Factorization(DF)];           // computes the prime factors of the discriminant of F
    
    alpha_0:= Valuation(DF,2);
    beta_0:= Valuation(DF,3);
    
    if (alpha_0 eq 4) then
        // alpha_1:= [ [0,0], [1,0] ];   
        Bounda:= 1;     // the upper bound on alpha_1
    elif (alpha_0 in [2,3]) then
        // alpha_1:= [];   // no information is known if alpha_0 = 2 or alpha_0 = 3 (not enough information is known)
                            // NB. the only possibilities for alpha_0 are 2,3,4
        Bounda:= 2^15;  // placeholder, no upper bound exists, or not enough information is known
    end if;
    
    if (beta_0 ge 2) then
        // beta_1:= [ [0,0], [1,0] ];
        Boundb:= 1;     // the upper bound on beta_1
    elif (beta_0 in [0,1]) then
        // beta_1:= [];    // no information is known if beta_0 = 0 or alpha_0 = 1 (not enough information is known)
                            // NB. the possibilties for beta_0 are all integers
        Boundb:= 2^15;  // placeholder, no upper bound exists, or not enough information is known
    end if;
    
    return Bounda, Boundb;

end function;