
// Make newform 324.4.e.i in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_324_e();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_324_e_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_324_4_e_i();".
// 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_324_4_e_i();".
// 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 := [16, 0, 12, 0, 5, 0, 3, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0, 0, 0, 0, 0], [16, 0, 15, 0, 5, 0, 3, 0], [0, -18, 0, 45, 0, -45, 0, -9], [0, -30, 0, -3, 0, 3, 0, 3], [-18, 0, -3, 0, -9, 0, -3, 0], [0, 132, 0, 69, 0, 27, 0, 9], [0, 72, 0, 9, 0, -9, 0, 9], [-252, 0, -15, 0, -45, 0, -51, 0]];
    Rf_basisdens := [1, 20, 20, 4, 1, 8, 8, 10];
    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_324_e();"
function MakeCharacter_324_e()
    N := 324;
    order := 3;
    char_gens := [163, 245];
    v := [3, 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_324_e_Hecke();"
function MakeCharacter_324_e_Hecke(Kf)
    N := 324;
    order := 3;
    char_gens := [163, 245];
    char_values := [[1, 0, 0, 0, 0, 0, 0, 0], [-1, -1, 0, 0, 0, 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, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, -1, 0, 0, 0, 0], [0, -4, 0, 0, 1, 0, 0, -1], [0, 0, -1, 0, 0, -1, 1, 0], [-29, -29, 0, 0, 0, 0, 0, -1], [0, 0, 0, 2, 0, 2, 5, 0], [56, 0, 0, 0, 1, 0, 0, 0], [0, 0, 5, 7, 0, 0, 0, 0], [0, 0, -12, 0, 0, 5, 12, 0], [-92, -92, 0, 0, 0, 0, 0, 6], [167, 0, 0, 0, -7, 0, 0, 0], [0, 0, 4, -16, 0, 0, 0, 0], [0, 164, 0, 0, 9, 0, 0, -9], [0, 0, -20, 0, 0, 0, 20, 0], [0, 0, 0, 22, 0, 22, 30, 0], [0, 0, 28, 4, 0, 0, 0, 0], [0, 251, 0, 0, 9, 0, 0, -9], [-80, -80, 0, 0, 0, 0, 0, -15], [0, 0, 0, 23, 0, 23, 41, 0], [305, 0, 0, 0, 0, 0, 0, 0], [0, -16, 0, 0, -25, 0, 0, 25], [0, 0, 10, 0, 0, 54, -10, 0], [0, 0, 0, -88, 0, -88, -43, 0], [0, 506, 0, 0, -4, 0, 0, 4], [0, 0, 14, 0, 0, 46, -14, 0], [892, 892, 0, 0, 0, 0, 0, 18], [0, 0, 0, 10, 0, 10, -30, 0], [533, 0, 0, 0, 15, 0, 0, 0], [0, 0, -95, 24, 0, 0, 0, 0], [-1744, 0, 0, 0, 9, 0, 0, 0], [0, 0, 69, 47, 0, 0, 0, 0], [0, 0, 55, 0, 0, 2, -55, 0], [364, 364, 0, 0, 0, 0, 0, -86], [0, 0, -150, -41, 0, 0, 0, 0], [0, -928, 0, 0, 6, 0, 0, -6], [-623, -623, 0, 0, 0, 0, 0, 3], [-832, 0, 0, 0, -88, 0, 0, 0], [0, 0, 65, -33, 0, 0, 0, 0], [0, 0, -30, 0, 0, 147, 30, 0], [0, 0, 0, -224, 0, -224, -12, 0], [-238, 0, 0, 0, -76, 0, 0, 0], [0, 0, 29, 0, 0, 37, -29, 0], [-1979, -1979, 0, 0, 0, 0, 0, -6], [0, 0, 0, 35, 0, 35, -80, 0], [-2644, 0, 0, 0, 88, 0, 0, 0], [-2108, -2108, 0, 0, 0, 0, 0, -61], [0, -4336, 0, 0, 39, 0, 0, -39], [0, 0, 65, 0, 0, 165, -65, 0], [-65, -65, 0, 0, 0, 0, 0, -111], [0, 0, 0, 172, 0, 172, 125, 0], [0, 0, 150, -162, 0, 0, 0, 0], [0, 1769, 0, 0, 130, 0, 0, -130], [0, 0, 0, 205, 0, 205, 275, 0], [0, 0, -25, 36, 0, 0, 0, 0], [0, 0, -400, 0, 0, 76, 400, 0], [0, 0, 0, -281, 0, -281, 142, 0], [5984, 0, 0, 0, 33, 0, 0, 0], [0, -502, 0, 0, 48, 0, 0, -48], [0, 0, -125, 0, 0, 308, 125, 0], [2428, 2428, 0, 0, 0, 0, 0, 12], [0, 0, -70, -131, 0, 0, 0, 0], [-2716, 0, 0, 0, -174, 0, 0, 0], [0, 0, 26, 422, 0, 0, 0, 0], [0, 7331, 0, 0, -74, 0, 0, 74], [0, 0, -50, 0, 0, 61, 50, 0], [0, 632, 0, 0, -217, 0, 0, 217], [6142, 6142, 0, 0, 0, 0, 0, -92], [0, 0, 285, 87, 0, 0, 0, 0], [0, -2182, 0, 0, -104, 0, 0, 104], [0, 0, -240, 0, 0, 104, 240, 0], [0, 0, 0, 363, 0, 363, -47, 0], [0, 2888, 0, 0, 258, 0, 0, -258], [-974, -974, 0, 0, 0, 0, 0, -256], [9644, 0, 0, 0, -58, 0, 0, 0], [0, 0, -695, -361, 0, 0, 0, 0], [0, 0, 250, 0, 0, 238, -250, 0], [-3049, 0, 0, 0, 219, 0, 0, 0], [0, 0, 125, -490, 0, 0, 0, 0], [-1985, -1985, 0, 0, 0, 0, 0, 154], [0, 0, -50, -486, 0, 0, 0, 0], [0, 4037, 0, 0, 91, 0, 0, -91], [0, 0, 0, -250, 0, -250, -594, 0], [-3553, 0, 0, 0, 18, 0, 0, 0], [0, -13444, 0, 0, 48, 0, 0, -48], [0, 0, 280, 0, 0, -756, -280, 0], [0, 0, 0, 636, 0, 636, -248, 0], [0, -2311, 0, 0, -546, 0, 0, 546], [0, 0, 926, 0, 0, 98, -926, 0], [6424, 6424, 0, 0, 0, 0, 0, -184], [0, 0, 0, -242, 0, -242, -990, 0], [0, 0, -463, 0, 0, 921, 463, 0], [6572, 0, 0, 0, 118, 0, 0, 0], [0, 0, 329, 79, 0, 0, 0, 0], [2716, 2716, 0, 0, 0, 0, 0, 351], [0, 0, 0, -378, 0, -378, -110, 0], [0, 0, 350, 614, 0, 0, 0, 0], [0, 0, 0, -988, 0, -988, -200, 0], [-316, 0, 0, 0, 801, 0, 0, 0], [1697, 0, 0, 0, -763, 0, 0, 0], [0, 1124, 0, 0, 324, 0, 0, -324], [0, 0, 0, 665, 0, 665, 350, 0], [0, 0, -920, -540, 0, 0, 0, 0], [0, 0, -83, 0, 0, -626, 83, 0], [-15440, -15440, 0, 0, 0, 0, 0, -44], [4787, 0, 0, 0, 348, 0, 0, 0], [0, 0, 715, 0, 0, -149, -715, 0], [0, 0, 0, -208, 0, -208, 865, 0], [0, 0, -498, 382, 0, 0, 0, 0], [0, 6743, 0, 0, -588, 0, 0, 588], [-1364, -1364, 0, 0, 0, 0, 0, 49], [-7426, 0, 0, 0, 276, 0, 0, 0], [0, 0, 1295, 536, 0, 0, 0, 0], [0, 25352, 0, 0, 138, 0, 0, -138], [-3568, 0, 0, 0, 242, 0, 0, 0], [0, 0, 771, 0, 0, 26, -771, 0], [-8300, -8300, 0, 0, 0, 0, 0, 270], [0, 0, 0, -147, 0, -147, 515, 0], [0, 0, -290, 766, 0, 0, 0, 0], [0, 0, 525, 0, 0, -215, -525, 0], [-3239, -3239, 0, 0, 0, 0, 0, 603], [0, -16045, 0, 0, -430, 0, 0, 430], [0, 0, -210, 0, 0, 1042, 210, 0], [0, 0, 0, -324, 0, -324, 760, 0], [0, 28352, 0, 0, -261, 0, 0, 261], [0, 0, 0, -197, 0, -197, 614, 0], [0, 11009, 0, 0, 293, 0, 0, -293], [0, 0, 0, -39, 0, -39, 467, 0], [0, 9092, 0, 0, 207, 0, 0, -207], [8206, 8206, 0, 0, 0, 0, 0, 444], [3692, 0, 0, 0, -480, 0, 0, 0], [0, 0, -1300, -960, 0, 0, 0, 0], [-12020, -12020, 0, 0, 0, 0, 0, 67], [962, 0, 0, 0, 588, 0, 0, 0], [0, 0, 701, -570, 0, 0, 0, 0], [5893, 5893, 0, 0, 0, 0, 0, -406], [0, 0, 0, 327, 0, 327, 440, 0], [-14732, -14732, 0, 0, 0, 0, 0, -303], [0, 0, 610, 2067, 0, 0, 0, 0], [0, 0, 0, -610, 0, -610, 203, 0], [10136, 0, 0, 0, 180, 0, 0, 0], [0, 0, 1366, 0, 0, -503, -1366, 0], [27844, 27844, 0, 0, 0, 0, 0, 56], [0, 0, 0, -169, 0, -169, -575, 0], [6626, 0, 0, 0, 676, 0, 0, 0], [0, 0, -43, 0, 0, -1407, 43, 0], [0, -18730, 0, 0, 240, 0, 0, -240], [0, 0, 755, 0, 0, -654, -755, 0], [2164, 2164, 0, 0, 0, 0, 0, -332], [0, 0, 0, 653, 0, 653, -65, 0], [3649, 3649, 0, 0, 0, 0, 0, 781], [0, 0, 0, -752, 0, -752, -2356, 0], [-22804, 0, 0, 0, -566, 0, 0, 0], [0, 0, -535, -2341, 0, 0, 0, 0], [0, -26152, 0, 0, 278, 0, 0, -278], [0, 0, 1751, 0, 0, 1179, -1751, 0], [-7792, 0, 0, 0, 1329, 0, 0, 0], [0, 0, 1113, 0, 0, -1206, -1113, 0], [-7897, 0, 0, 0, -1108, 0, 0, 0], [0, 0, -2050, -293, 0, 0, 0, 0], [0, 0, 2495, 0, 0, 435, -2495, 0], [0, 0, 0, 2778, 0, 2778, 575, 0], [29404, 29404, 0, 0, 0, 0, 0, 501], [0, 0, 0, -99, 0, -99, -1641, 0], [0, 0, 360, -692, 0, 0, 0, 0], [0, 0, -3090, 0, 0, -50, 3090, 0], [2492, 0, 0, 0, 1317, 0, 0, 0], [0, 3971, 0, 0, -159, 0, 0, 159]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_324_e_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_324_4_e_i();".
// 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_324_4_e_i(:prec:=8)
    chi := MakeCharacter_324_e();
    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_324_4_e_i();".
// 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_324_4_e_i( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_324_e();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![2562890625, 0, 28248750, 0, 260739, 0, 558, 0, 1]>,<7,R![547600, 5920, 804, -8, 1]>],Snew);
    return Vf;
end function;