
// Make newform 162.7.d.a in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_162_d();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_162_d_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_162_7_d_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_162_7_d_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, 0, -2, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 2, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]];
    Rf_basisdens := [1, 1, 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_162_d();"
function MakeCharacter_162_d()
    N := 162;
    order := 6;
    char_gens := [83];
    v := [5];
    // 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_162_d_Hecke();"
function MakeCharacter_162_d_Hecke(Kf)
    N := 162;
    order := 6;
    char_gens := [83];
    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, 2, 0, -2], [0, 0, 0, 0], [0, 15, 0, 0], [0, 0, -389, 0], [0, 735, 0, -735], [-1415, 0, 1415, 0], [0, 0, 0, 837], [-3067, 0, 0, 0], [0, -7401, 0, 0], [0, 4578, 0, -4578], [11338, 0, -11338, 0], [47135, 0, 0, 0], [0, -2190, 0, 0], [0, 0, -145118, 0], [0, -63705, 0, 63705], [0, 0, 0, -93978], [0, -127893, 0, 0], [0, 0, 350305, 0], [-120341, 0, 120341, 0], [0, 0, 0, -118476], [175151, 0, 0, 0], [0, 0, 252259, 0], [0, 119886, 0, -119886], [0, 0, 0, 279297], [0, 0, 1297105, 0], [0, -266160, 0, 266160], [1573315, 0, -1573315, 0], [0, 0, 0, -259299], [-485998, 0, 0, 0], [0, 438315, 0, 0], [2530766, 0, 0, 0], [0, 1072014, 0, 0], [0, -238989, 0, 238989], [-4010357, 0, 4010357, 0], [0, 993372, 0, 0], [0, 0, -1217045, 0], [2814910, 0, -2814910, 0], [6925493, 0, 0, 0], [0, 47247, 0, 0], [0, 2998374, 0, -2998374], [0, 0, 0, 936324], [-2072257, 0, 0, 0], [0, -874263, 0, 874263], [987577, 0, -987577, 0], [0, 0, 0, -3488391], [9556877, 0, 0, 0], [4246819, 0, -4246819, 0], [0, 0, -11418374, 0], [0, -3124848, 0, 3124848], [-3772010, 0, 3772010, 0], [0, 0, 0, -3938148], [0, 1505076, 0, 0], [0, 0, 19802329, 0], [0, 0, 0, -1930986], [0, -9532506, 0, 0], [0, 1269492, 0, -1269492], [0, 0, 0, 1571841], [-1613491, 0, 0, 0], [0, 0, -24497714, 0], [0, -6407958, 0, 6407958], [15958834, 0, -15958834, 0], [0, -6379509, 0, 0], [-15402418, 0, 0, 0], [0, 3261057, 0, 0], [0, 0, 32781121, 0], [0, -433182, 0, 433182], [0, 0, -13608677, 0], [-60111983, 0, 60111983, 0], [0, 25080396, 0, 0], [0, 0, -32311343, 0], [0, 21132780, 0, -21132780], [0, 0, 0, 13777227], [0, 0, 33888571, 0], [-80664239, 0, 80664239, 0], [76388933, 0, 0, 0], [0, 3315696, 0, 0], [0, -882561, 0, 882561], [-31229710, 0, 0, 0], [0, 16884888, 0, 0], [41703289, 0, -41703289, 0], [0, -35514633, 0, 0], [0, 0, -74714303, 0], [0, 0, 0, 3066543], [-17435302, 0, 0, 0], [0, 0, 88762858, 0], [0, 44963394, 0, -44963394], [0, 0, 0, -55424511], [0, 0, 63096118, 0], [0, -41013201, 0, 41013201], [133440739, 0, -133440739, 0], [0, 0, 0, -23249727], [0, -62190462, 0, 62190462], [57220733, 0, 0, 0], [0, 4529013, 0, 0], [69988426, 0, -69988426, 0], [0, 0, 0, -64118889], [0, 13169121, 0, 0], [0, 0, 0, -42434217], [202394621, 0, 0, 0], [168172463, 0, 0, 0], [0, 0, 97089091, 0], [0, 0, 0, -76373631], [0, -42966246, 0, 0], [0, 84992703, 0, -84992703], [134707435, 0, -134707435, 0], [-336318073, 0, 0, 0], [0, -74297001, 0, 74297001], [0, 0, 0, 105777702], [0, -88251546, 0, 0], [0, 0, 146561542, 0], [103418971, 0, -103418971, 0], [144725159, 0, 0, 0], [0, 99829347, 0, 0], [0, 0, -240607901, 0], [367661045, 0, 0, 0], [0, -42973068, 0, 42973068], [-135204446, 0, 135204446, 0], [0, 0, 0, 33213402], [0, 79883718, 0, 0], [0, -76384038, 0, 76384038], [10377529, 0, -10377529, 0], [0, 0, 61177921, 0], [0, 105564975, 0, -105564975], [0, 0, 0, 169863444], [0, 0, -264962846, 0], [0, 0, 0, -126012447], [0, 0, 219800761, 0], [0, 0, 0, -34370370], [0, 0, 150070570, 0], [-287021234, 0, 287021234, 0], [419240990, 0, 0, 0], [0, -248891475, 0, 0], [-621902549, 0, 621902549, 0], [46204967, 0, 0, 0], [0, -54622461, 0, 0], [817868113, 0, -817868113, 0], [0, 0, 0, -48601314], [-485178149, 0, 485178149, 0], [0, -350590404, 0, 0], [0, 0, 0, 259404732], [635498894, 0, 0, 0], [0, 16499337, 0, -16499337], [362586931, 0, -362586931, 0], [0, 0, 0, -137541699], [496962551, 0, 0, 0], [0, -350328450, 0, 350328450], [0, 0, -263394119, 0], [0, -374053152, 0, 374053152], [401087707, 0, -401087707, 0], [0, 0, 0, -99449541], [-556535495, 0, 556535495, 0], [0, 0, 0, -291989025], [-538395379, 0, 0, 0], [0, -257686806, 0, 0], [0, 0, 434990275, 0], [0, 15620730, 0, -15620730], [428965958, 0, 0, 0], [0, 225212856, 0, -225212856], [11123615, 0, 0, 0], [0, 278395053, 0, 0], [0, -74379891, 0, 74379891], [0, 0, 0, -444820851], [812668867, 0, -812668867, 0], [0, 0, 0, 469166067], [0, -450295626, 0, 0], [0, 47621805, 0, -47621805], [-629069515, 0, 0, 0], [0, 0, 993161230, 0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_162_d_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_162_7_d_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_162_7_d_a(:prec:=4)
    chi := MakeCharacter_162_d();
    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_162_7_d_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_162_7_d_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_162_d();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,7,sign)));
    Vf := Kernel([<5,R![3240000, 0, -1800, 0, 1]>],Snew);
    return Vf;
end function;