
// Make newform 16.7.c.b in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_16_c();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_16_c_Hecke();"
// To make the coeffs of the qexp of the newform in the Hecke field type "qexpCoeffs();"
// To make the newform (type ModFrm), type "MakeNewformModFrm_16_7_c_b();".
// This may take a long time!  To see verbose output, uncomment the SetVerbose lines below.
// The precision argument determines an initial guess on how many Fourier coefficients to use.
// This guess is increased enough to uniquely determine the newform.
// To make the Hecke irreducible modular symbols subspace (type ModSym)
// containing the newform, type "MakeNewformModSym_16_7_c_b();".
// This may take a long time!  To see verbose output, uncomment the SetVerbose line below.
// The default sign is -1.  You can change this with the optional parameter "sign".
function ConvertToHeckeField(input: pass_field := false, Kf := [])
    if not pass_field then
        poly := [1, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0], [-16, 32]];
    Rf_basisdens := [1, 1];
    Rf_basisnums := ChangeUniverse([[z : z in elt] : elt in Rf_num], Kf);
    Rfbasis := [Rf_basisnums[i]/Rf_basisdens[i] : i in [1..Degree(Kf)]];
    inp_vec := Vector(Rfbasis)*ChangeRing(Transpose(Matrix([[elt : elt in row] : row in input])),Kf);
    return Eltseq(inp_vec);
end function;


// To make the character of type GrpDrchElt, type "MakeCharacter_16_c();"
function MakeCharacter_16_c()
    N := 16;
    order := 2;
    char_gens := [15, 5];
    v := [1, 2];
    // chi(gens[i]) = zeta^v[i]
    assert UnitGenerators(DirichletGroup(N)) eq char_gens;
    F := CyclotomicField(order);
    chi := DirichletCharacterFromValuesOnUnitGenerators(DirichletGroup(N,F),[F|F.1^e:e in v]);
    return MinimalBaseRingCharacter(chi);
end function;

// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_16_c_Hecke();"
function MakeCharacter_16_c_Hecke(Kf)
    N := 16;
    order := 2;
    char_gens := [15, 5];
    char_values := [[-1, 0], [1, 0]];
    assert UnitGenerators(DirichletGroup(N)) eq char_gens;
    values := ConvertToHeckeField(char_values : pass_field := true, Kf := Kf); // the value of chi on the gens as elements in the Hecke field
    F := Universe(values);// the Hecke field
    chi := DirichletCharacterFromValuesOnUnitGenerators(DirichletGroup(N,F),values);
    return chi;
end function;


function ExtendMultiplicatively(weight, aps, character)
    prec := NextPrime(NthPrime(#aps)) - 1; // we will able to figure out a_0 ... a_prec
    primes := PrimesUpTo(prec);
    prime_powers := primes;
    assert #primes eq #aps;
    log_prec := Floor(Log(prec)/Log(2)); // prec < 2^(log_prec+1)
    F := Universe(aps);
    FXY<X, Y> := PolynomialRing(F, 2);
    // 1/(1 - a_p T + p^(weight - 1) * char(p) T^2) = 1 + a_p T + a_{p^2} T^2 + ...
    R<T> := PowerSeriesRing(FXY : Precision := log_prec + 1);
    recursion := Coefficients(1/(1 - X*T + Y*T^2));
    coeffs := [F!0: i in [1..(prec+1)]];
    coeffs[1] := 1; //a_1
    for i := 1 to #primes do
        p := primes[i];
        coeffs[p] := aps[i];
        b := p^(weight - 1) * F!character(p);
        r := 2;
        p_power := p * p;
        //deals with powers of p
        while p_power le prec do
            Append(~prime_powers, p_power);
            coeffs[p_power] := Evaluate(recursion[r + 1], [aps[i], b]);
            p_power *:= p;
            r +:= 1;
        end while;    
    end for;
    Sort(~prime_powers);
    for pp in prime_powers do
        for k := 1 to Floor(prec/pp) do
            if GCD(k, pp) eq 1 then
                coeffs[pp*k] := coeffs[pp]*coeffs[k];
            end if;
        end for;
    end for;
    return coeffs;
end function;


function qexpCoeffs()
    // To make the coeffs of the qexp of the newform in the Hecke field type "qexpCoeffs();"
    weight := 7;
    raw_aps := [[0, 0], [0, -1], [-150, 0], [0, -22], [0, 33], [154, 0], [7458, 0], [0, -77], [0, -306], [-10758, 0], [0, 72], [-11350, 0], [67122, 0], [0, -2871], [0, 2508], [109962, 0], [0, 11037], [306746, 0], [0, 7951], [0, 13434], [165682, 0], [0, -27500], [0, -17853], [471954, 0], [910594, 0], [-955350, 0], [0, 26114], [0, 48345], [-61094, 0], [-2816094, 0], [0, 72864], [0, -101409], [-1385358, 0], [0, -57343], [3117642, 0], [0, 32494], [5337722, 0], [0, -31801], [0, 61842], [-1021350, 0], [0, -309957], [-2697974, 0], [0, 346416], [5358914, 0], [8005866, 0], [0, 331034], [0, -444829], [0, -637704], [0, 698775], [-16053590, 0], [-16890126, 0], [0, -401412], [16970338, 0], [0, -23667], [-3345918, 0], [0, 71082], [27265818, 0], [0, 1165252], [10705354, 0], [-28192494, 0], [0, 469557], [-29577174, 0], [0, 175131], [0, 691350], [17604242, 0], [-4723782, 0], [0, 2051249], [-26051102, 0], [0, -2490939], [20600954, 0], [-9921726, 0], [0, 811074], [0, -1245028], [70457354, 0], [0, 1743277], [0, -303264], [-85791510, 0], [59425562, 0], [32442402, 0], [10148050, 0], [0, 3196743], [-109865942, 0], [0, 2620332], [-64922398, 0], [0, 692406], [0, -312003], [-55937214, 0], [-3240974, 0], [-69135462, 0], [0, -3971596], [0, 400227], [0, 5222712], [0, 7624418], [0, 4873209], [0, -3016629], [0, -2327226], [-49917510, 0], [145291122, 0], [0, 4643617], [-40737158, 0], [0, -4001305], [-57842598, 0], [0, -8144037], [-64016238, 0], [0, 5279197], [268202114, 0], [0, 11796081], [-317791518, 0], [0, -10288674], [-183806062, 0], [0, 4504280], [413231786, 0], [280809714, 0], [0, -14235431], [0, 587110], [59898306, 0], [0, -10269729], [0, -11387382], [-54153702, 0], [0, 11883795], [-327505078, 0], [390277250, 0], [53217450, 0], [0, 3804009], [0, -9887301], [151827258, 0], [-605603222, 0], [0, 1376052], [0, -1139202], [241121914, 0], [0, 17125911], [0, 11479842], [0, -13769348], [-736714358, 0], [330915090, 0], [6689474, 0], [41844714, 0], [0, -22442189], [672511866, 0], [687717426, 0], [0, -6909111], [-547706742, 0], [0, -1320170], [0, 15333021], [-132771014, 0], [0, -24566598], [-29556406, 0], [-51304110, 0], [0, 7071557], [0, -27569256], [-483880870, 0], [311966370, 0], [0, 11298155], [0, -48927450], [0, 7770433], [0, -389532], [0, 29373102], [-258385278, 0], [-419100814, 0], [959807706, 0], [0, 11218491], [-613110894, 0], [0, 3989722], [0, -5656047], [-1217223774, 0], [0, -24417474], [0, -61234888], [620676650, 0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_16_c_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_16_7_c_b();".
// This may take a long time!  To see verbose output, uncomment the SetVerbose lines below.
// The precision argument determines an initial guess on how many Fourier coefficients to use.
// This guess is increased enough to uniquely determine the newform.
function MakeNewformModFrm_16_7_c_b(:prec:=2)
    chi := MakeCharacter_16_c();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 7));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(997) - 1;
    while true do
        trunc_vec := Vector(Kf, [0] cat [f_vec[i]: i in [1..prec]]);
        B := Basis(S, prec + 1);
        S_basismat := Matrix([AbsEltseq(g): g in B]);
        if Rank(S_basismat) eq Min(NumberOfRows(S_basismat), NumberOfColumns(S_basismat)) then
            S_basismat := ChangeRing(S_basismat,Kf);
            f_lincom := Solution(S_basismat,trunc_vec);
            f := &+[f_lincom[i]*Basis(S)[i] : i in [1..#Basis(S)]];
            return f;
        end if;
        error if prec eq maxprec, "Unable to distinguish newform within newspace";
        prec := Min(Ceiling(1.25 * prec), maxprec);
    end while;
end function;


// To make the Hecke irreducible modular symbols subspace (type ModSym)
// containing the newform, type "MakeNewformModSym_16_7_c_b();".
// This may take a long time!  To see verbose output, uncomment the SetVerbose line below.
// The default sign is -1.  You can change this with the optional parameter "sign".
function MakeNewformModSym_16_7_c_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_16_c();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,7,sign)));
    Vf := Kernel([<3,R![768, 0, 1]>],Snew);
    return Vf;
end function;