
// Make newform 1620.4.i.e in Magma, downloaded from the LMFDB on 16 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1620_i();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_1620_i_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_1620_4_i_e();".
// 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_1620_4_i_e();".
// 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;
    Rfbasis := [Kf.1^i : i in [0..Degree(Kf)-1]];
    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_1620_i();"
function MakeCharacter_1620_i()
    N := 1620;
    order := 3;
    char_gens := [811, 1541, 1297];
    v := [3, 2, 3];
    // 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_1620_i_Hecke();"
function MakeCharacter_1620_i_Hecke(Kf)
    N := 1620;
    order := 3;
    char_gens := [811, 1541, 1297];
    char_values := [[1, 0], [0, -1], [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 := 4;
    raw_aps := [[0, 0], [0, 0], [0, -5], [22, -22], [-9, 9], [0, -17], [75, 0], [-4, 0], [0, 183], [129, -129], [0, 187], [-34, 0], [0, 264], [-443, 443], [609, -609], [228, 0], [0, 60], [454, -454], [0, 244], [-444, 0], [398, 0], [349, -349], [1038, -1038], [-852, 0], [-914, 914], [1155, -1155], [0, 574], [-294, 0], [-340, 0], [0, 681], [578, 0], [0, -783], [234, -234], [0, -3170], [0, -1263], [-851, 851], [0, -2279], [-1297, 0], [0, 324], [-174, 174], [4596, 0], [548, 0], [4638, -4638], [0, 3442], [3312, 0], [1745, 0], [0, 3358], [-3512, 3512], [2112, -2112], [0, -2900], [5178, 0], [0, 5010], [5203, -5203], [549, 0], [0, -7095], [4284, -4284], [6669, 0], [-5560, 0], [-6734, 6734], [-7710, 7710], [0, -4736], [0, -5550], [5663, 0], [0, -1920], [-2816, 2816], [5784, -5784], [4354, -4354], [0, -3896], [0, 7608], [-4286, 4286], [2907, -2907], [-5988, 0], [3214, -3214], [0, -9419], [4544, 0], [0, 11487], [7845, -7845], [4133, 0], [0, 3324], [0, -1871], [0, 5409], [13084, -13084], [7848, 0], [-10222, 0], [-5672, 5672], [444, -444], [-10434, 0], [-11180, 11180], [15018, -15018], [0, -16166], [-2964, 0], [-5412, 5412], [12356, 0], [0, 16740], [0, 14092], [2313, 0], [0, 14925], [-5820, 0], [-20875, 0], [22442, 0], [-8615, 8615], [-19524, 0], [0, 13014], [-18336, 18336], [0, -3182], [7088, 0], [6342, -6342], [-26253, 0], [0, -5382], [28411, -28411], [0, -15374], [-23983, 0], [0, -13773], [11146, -11146], [-2080, 0], [-31050, 31050], [0, -9083], [4740, 0], [0, -8196], [6912, -6912], [0, 28438], [-1946, 1946], [-25956, 25956], [8184, 0], [-30422, 30422], [-27273, 0], [-28940, 28940], [606, 0], [13084, -13084], [0, -27074], [-3580, 0], [0, -13947], [0, 31273], [-31729, 0], [0, -3138], [0, -20807], [-33078, 0], [0, 12541], [0, 31980], [3792, 0], [24086, 0], [9774, -9774], [0, -38636], [16458, 0], [20252, 0], [-40722, 40722], [-12113, 12113], [-1926, 1926], [0, -4394], [37491, 0], [0, 21325], [-17982, 0], [-25636, 0], [0, 4221], [28393, -28393], [-10056, 10056], [-37249, 0], [-1668, 1668], [2786, 0], [0, 8613], [-55602, 55602], [10785, 0], [0, 35062], [49701, 0], [0, -7911], [37719, -37719], [54725, 0], [6505, -6505], [24305, 0], [0, 50634], [25965, -25965], [0, -14618], [0, 31686], [4396, -4396], [0, -4373], [0, -13920], [-52220, 52220], [-23238, 0], [-29512, 0], [63070, -63070], [33544, -33544], [45456, -45456], [0, 5281], [-51681, 0], [0, 23568], [7674, -7674], [23813, 0], [6625, -6625], [0, -35942], [-11778, 0], [-66226, 0], [-33462, 33462], [63500, 0], [66387, -66387], [15132, 0], [0, -73398], [0, 68002], [29275, -29275], [31710, -31710], [9840, 0], [0, -9393], [-77060, 77060], [0, 73474], [-12350, 12350], [87279, 0], [23268, 0], [6305, 0], [0, -44526], [46518, -46518], [0, -46628], [-42544, 0], [0, -43305], [-59660, 59660], [77514, -77514], [0, 78618], [27025, -27025], [0, 77794], [-16362, 16362], [80127, 0], [0, 78246], [0, -60332], [0, 102031], [0, -31860], [29972, 0], [0, 52218], [-40994, 40994], [6861, -6861], [105534, 0], [69544, -69544], [27159, -27159], [0, -78494], [12962, 0], [0, 49921], [0, 24570], [-47612, 47612], [-46140, 46140], [0, -44147], [9024, 0], [0, -76800], [79512, 0], [-7992, 7992], [79676, 0], [0, -87674], [24254, 0], [0, -97422], [-13704, 13704], [-76774, 0], [0, -84231], [0, 65374], [18933, 0], [0, 41245], [79956, 0], [0, -89976], [-47474, 47474], [-76926, 76926], [-8439, 0], [31820, 0], [-11261, 11261], [-128154, 0], [90830, 0], [724, -724], [56766, -56766], [0, 61270], [-70447, 0], [0, 13503], [8512, -8512], [-43050, 0], [50880, -50880], [0, -21539], [0, -66684], [0, 11164], [-16351, 0], [95902, -95902], [0, 23095], [0, 51334], [-68512, 0], [0, -33336], [-40556, 40556], [-57931, 0], [34149, -34149], [0, 63609], [0, -67061], [-113073, 113073], [-3032, 3032], [0, 29269], [13554, 0], [54440, 0], [0, -11130], [-49565, 49565], [126534, 0], [-97137, 97137], [-66468, 0], [0, 108171], [0, -22836], [21130, -21130], [0, 73134], [85720, -85720], [-70914, 70914], [-147579, 0], [-19964, 19964], [0, 78208], [68760, 0], [-115639, 0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1620_i_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1620_4_i_e();".
// 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_1620_4_i_e(:prec:=2)
    chi := MakeCharacter_1620_i();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 4));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(1999) - 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_1620_4_i_e();".
// 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_1620_4_i_e( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1620_i();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<7,R![484, -22, 1]>,<11,R![81, 9, 1]>],Snew);
    return Vf;
end function;