
// Make newform 45.4.b.b in Magma, downloaded from the LMFDB on 17 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_45_b();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_45_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_45_4_b_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_45_4_b_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 := [100, 0, 21, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 1, 0, 1], [0, 13, 0, 1], [32, 0, 3, 0]];
    Rf_basisdens := [1, 10, 2, 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_45_b();"
function MakeCharacter_45_b()
    N := 45;
    order := 2;
    char_gens := [11, 37];
    v := [2, 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_45_b_Hecke();"
function MakeCharacter_45_b_Hecke(Kf)
    N := 45;
    order := 2;
    char_gens := [11, 37];
    char_values := [[1, 0, 0, 0], [-1, 0, 0, 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 := 4;
    raw_aps := [[0, -1, 0, 0], [0, 0, 0, 0], [-2, 0, -1, 1], [0, -4, 2, 0], [22, 0, 0, -2], [0, -2, -8, 0], [0, -16, -6, 0], [-24, 0, 0, -8], [0, 2, -6, 0], [-152, 0, 0, -14], [24, 0, 0, 4], [0, 66, 12, 0], [206, 0, 0, -4], [0, -82, 14, 0], [0, 54, -38, 0], [0, -28, 54, 0], [-94, 0, 0, 2], [186, 0, 0, -32], [0, 106, 46, 0], [-12, 0, 0, 60], [0, -192, -84, 0], [-240, 0, 0, 100], [0, -238, -30, 0], [534, 0, 0, 48], [0, -244, -4, 0], [592, 0, 0, -62], [0, 208, 274, 0], [0, 114, 202, 0], [366, 0, 0, -68], [0, 432, -110, 0], [0, -84, -174, 0], [1514, 0, 0, -118], [0, 344, 30, 0], [44, 0, 0, -12], [-28, 0, 0, -166], [152, 0, 0, 60], [0, 406, -140, 0], [0, 378, 198, 0], [0, -46, 18, 0], [0, -388, 450, 0], [-2910, 0, 0, -30], [3354, 0, 0, 4], [1972, 0, 0, 148], [0, 148, -200, 0], [0, -616, -354, 0], [-3472, 0, 0, 36], [-876, 0, 0, -356], [0, 608, 362, 0], [0, 74, 138, 0], [-126, 0, 0, 268], [0, 1032, 526, 0], [-556, 0, 0, -172], [-1962, 0, 0, 232], [138, 0, 0, 210], [0, -136, -18, 0], [0, 162, -38, 0], [1944, 0, 0, 558], [2568, 0, 0, 532], [0, 806, 1064, 0], [-2750, 0, 0, -56], [0, 178, -818, 0], [0, 32, 762, 0], [0, 1326, -762, 0], [-748, 0, 0, -652], [0, -172, 248, 0], [0, 1184, -1038, 0], [-208, 0, 0, -840], [0, -1064, -1196, 0], [0, -126, -1046, 0], [3102, 0, 0, 184], [0, -1008, -1802, 0], [-384, 0, 0, 552], [0, 1196, -526, 0], [0, -2942, 724, 0], [-540, 0, 0, -1100], [0, -1378, 522, 0], [10156, 0, 0, -398], [0, -894, 1068, 0], [-5686, 0, 0, 692], [3594, 0, 0, 28], [-6706, 0, 0, -922], [-5250, 0, 0, -284], [-3744, 0, 0, 936], [0, 3348, -72, 0], [-6480, 0, 0, 1060], [0, 1802, 1218, 0], [-498, 0, 0, -876], [0, 596, -1108, 0], [-5732, 0, 0, -50], [0, -932, -1046, 0], [0, -1006, -1158, 0], [-15500, 0, 0, 460], [0, -1224, 1386, 0], [10634, 0, 0, -358], [-12504, 0, 0, 112], [0, 2822, -102, 0], [6536, 0, 0, 962], [-6774, 0, 0, -1284], [0, -3822, 174, 0], [-3166, 0, 0, 1044], [0, 1166, -178, 0], [0, -4476, -3686, 0], [0, 162, 970, 0], [10054, 0, 0, 148], [-5712, 0, 0, 712], [0, -2544, -2544, 0], [0, 1754, 162, 0], [0, 152, -1494, 0], [-12928, 0, 0, 344], [3618, 0, 0, -668], [0, 3976, 3826, 0], [0, 2598, -3180, 0], [0, 2024, 1170, 0], [6884, 0, 0, -1092], [-3888, 0, 0, -20], [11086, 0, 0, -1484], [0, 5338, -2786, 0], [0, -5866, 354, 0], [0, 3272, 2382, 0], [1030, 0, 0, 2230], [11402, 0, 0, -1560], [0, -1852, 656, 0], [0, 2304, -3134, 0], [0, 3242, 282, 0], [9408, 0, 0, 352], [16344, 0, 0, -18], [20186, 0, 0, -588], [-16968, 0, 0, -1056], [0, -4264, -1270, 0], [0, -302, 1024, 0], [1824, 0, 0, 1768], [0, 3942, -2054, 0], [30528, 0, 0, 412], [0, 5886, 2160, 0], [-1390, 0, 0, 3044], [-17682, 0, 0, -1484], [0, -2608, -4218, 0], [0, -14, -2882, 0], [0, -5596, 1902, 0], [6210, 0, 0, 900], [-17908, 0, 0, -900], [-1760, 0, 0, -2306], [0, 1708, 3610, 0], [0, 674, 2754, 0], [4790, 0, 0, 900], [-17720, 0, 0, 400], [0, 3158, -3172, 0], [0, 9344, -798, 0], [-34620, 0, 0, 100], [0, 2, 4386, 0], [0, 9646, 3772, 0], [17250, 0, 0, -1056], [0, -5502, 5334, 0], [0, 5114, 1002, 0], [0, -514, 5558, 0], [18448, 0, 0, -800], [1280, 0, 0, -1260], [-4946, 0, 0, -2252], [0, -4584, -1776, 0], [8516, 0, 0, 2006], [0, 10274, 2490, 0], [0, -2208, 3658, 0], [0, -8324, -3254, 0], [-10134, 0, 0, 306], [0, 864, 5074, 0], [0, -9058, -5286, 0], [-14808, 0, 0, -2660], [0, -754, 3140, 0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_45_b_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_45_4_b_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_45_4_b_b(:prec:=4)
    chi := MakeCharacter_45_b();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 4));
    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_45_4_b_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_45_4_b_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_45_b();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![64, 0, 25, 0, 1]>],Snew);
    return Vf;
end function;