
// Make newform 4.7.b.a in Magma, downloaded from the LMFDB on 23 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_4_b();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_4_b_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_4_7_b_a();".
// 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_4_7_b_a();".
// 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 := [4, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0], [-2, 4]];
    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_4_b();"
function MakeCharacter_4_b()
    N := 4;
    order := 2;
    char_gens := [3];
    v := [1];
    // 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_4_b_Hecke();"
function MakeCharacter_4_b_Hecke(Kf)
    N := 4;
    order := 2;
    char_gens := [3];
    char_values := [[-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 := [[2, 1], [0, -4], [10, 0], [0, 40], [0, -124], [1466, 0], [-4766, 0], [0, 972], [0, -1352], [25498, 0], [0, 5408], [1994, 0], [29362, 0], [0, -2780], [0, -976], [-192854, 0], [0, -10124], [-10918, 0], [0, -50884], [0, 68712], [288626, 0], [0, -40112], [0, -26356], [310738, 0], [-1457086, 0], [-639158, 0], [0, 179336], [0, -148380], [1535738, 0], [-601694, 0], [0, 216192], [0, 367228], [3810034, 0], [0, 17924], [-3274262, 0], [0, -722120], [816794, 0], [0, -238308], [0, 1028296], [-5126534, 0], [0, -301332], [9691562, 0], [0, -1473856], [-2430334, 0], [-2230646, 0], [0, 632040], [0, 504716], [0, 430048], [0, -1743268], [1595978, 0], [8043826, 0], [0, 1452784], [5051042, 0], [0, 608820], [23455234, 0], [0, -2801368], [-29427782, 0], [0, -1099248], [27622634, 0], [8640082, 0], [0, -1644076], [-44541494, 0], [0, -6313620], [0, 6457816], [1127186, 0], [34488154, 0], [0, 5198788], [-34253086, 0], [0, 7042324], [47000858, 0], [-12723134, 0], [0, -2609784], [0, -1433744], [687146, 0], [0, 1917108], [0, -4336256], [-101121782, 0], [-34826566, 0], [-68839838, 0], [45995858, 0], [0, 3500572], [-94207798, 0], [0, -6702672], [84021026, 0], [0, -19121576], [0, 10376180], [-88092542, 0], [-37542286, 0], [115259962, 0], [0, -1335088], [0, 21201036], [0, 10021088], [0, -1395192], [0, -23891996], [0, -8826580], [0, 1698456], [134186458, 0], [-198564878, 0], [0, 27822468], [14418842, 0], [0, 5475484], [89084794, 0], [0, -31187348], [-25614062, 0], [0, 14301812], [107271554, 0], [0, 2793668], [200340706, 0], [0, -17763080], [-190305838, 0], [0, 16196960], [-99111094, 0], [37082674, 0], [0, -52320668], [0, -3261672], [-421292798, 0], [0, 10556540], [0, -23790936], [64384186, 0], [0, 69468236], [283897322, 0], [277692866, 0], [-92302646, 0], [0, -34968156], [0, -2486036], [352233562, 0], [462732938, 0], [0, 59435472], [0, 62248312], [-508270054, 0], [0, 16494684], [0, -36575480], [0, -27796240], [-303985366, 0], [863611282, 0], [-19789822, 0], [-564048374, 0], [0, 52181708], [-21599846, 0], [651310258, 0], [0, 71377956], [-894865238, 0], [0, 109022168], [0, -4016268], [-40544422, 0], [0, -53940376], [-428994454, 0], [220597714, 0], [0, -148164844], [0, 8326752], [515252474, 0], [-118518878, 0], [0, -16611156], [0, 65834008], [0, -70128380], [0, -77406064], [0, -124772808], [722626498, 0], [-412016206, 0], [787666042, 0], [0, -68358676], [1183226386, 0], [0, 61798376], [0, -90745788], [-921322526, 0], [0, 31553144], [0, 230034144], [1367900234, 0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_4_b_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_4_7_b_a();".
// 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_4_7_b_a(:prec:=2)
    chi := MakeCharacter_4_b();
    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_4_7_b_a();".
// 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_4_7_b_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_4_b();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,7,sign)));
    Vf := Kernel([],Snew);
    return Vf;
end function;