
// Make newform 324.3.f.p in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_324_f();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_324_f_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_3_f_p();".
// 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_3_f_p();".
// 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, 24, 0, 32, 0, 6, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0, 0, 0, 0, 0], [-8, 0, -96, 0, -16, 0, -3, 0], [0, 24, 0, -24, 0, -4, 0, -1], [0, 184, 0, 160, 0, 32, 0, 5], [0, 264, 0, -64, 0, -16, 0, -5], [-312, 0, -224, 0, -48, 0, -5, 0], [0, 24, 0, -80, 0, -16, 0, -3], [120, 0, -224, 0, -48, 0, -11, 0]];
    Rf_basisdens := [1, 64, 16, 64, 64, 64, 16, 64];
    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_f();"
function MakeCharacter_324_f()
    N := 324;
    order := 6;
    char_gens := [163, 245];
    v := [3, 4];
    // 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_f_Hecke();"
function MakeCharacter_324_f_Hecke(Kf)
    N := 324;
    order := 6;
    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 := 3;
    raw_aps := [[0, 0, -1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 1, 1, 0, 0, 0], [2, -1, 0, 0, 0, -2, 0, 0], [0, 0, -1, 3, -3, 0, 1, 0], [1, -1, 0, 0, 0, -4, 0, -2], [0, 0, -10, 2, 4, 0, -3, 0], [3, -6, 0, 0, 0, 0, 0, 0], [0, 0, -15, 9, 5, 0, -2, 0], [0, 0, 4, -12, -4, 0, 4, 0], [-8, -8, 0, 0, 0, 0, 0, 8], [-7, 0, 0, 0, 0, -2, 0, 2], [0, 0, 8, -8, 8, 0, -8, 0], [-40, 20, 0, 0, 0, 4, 0, 0], [0, 0, 11, -9, 9, 0, 1, 0], [0, 0, 12, -28, 8, 0, 10, 0], [0, 0, -15, 33, 5, 0, -14, 0], [0, -5, 0, 0, 0, 2, 0, 4], [19, 19, 0, 0, 0, 0, 0, -16], [0, 0, -24, 48, 12, 0, 6, 0], [29, 0, 0, 0, 0, 4, 0, -4], [110, -55, 0, 0, 0, -14, 0, 0], [0, 0, 2, -6, 6, 0, -2, 0], [0, 0, -10, 2, 4, 0, -3, 0], [0, 31, 0, 0, 0, 16, 0, 32], [0, 0, -4, -36, -12, 0, 20, 0], [-29, -29, 0, 0, 0, 0, 0, 2], [0, 0, -12, -24, -18, 0, 15, 0], [-70, 0, 0, 0, 0, 4, 0, -4], [0, 0, -5, 19, -5, 0, 12, 0], [-84, 168, 0, 0, 0, -12, 0, -12], [0, 0, -24, -24, 8, 0, 16, 0], [0, 0, 25, -3, -1, 0, -11, 0], [97, 97, 0, 0, 0, 0, 0, -4], [0, 0, -2, 46, -2, 0, 24, 0], [62, -31, 0, 0, 0, 34, 0, 0], [-62, 62, 0, 0, 0, -40, 0, -20], [21, -42, 0, 0, 0, 24, 0, 24], [0, 0, 93, -75, -31, 0, 22, 0], [0, 0, 64, -48, -16, 0, -8, 0], [0, 0, 48, -24, 12, 0, -30, 0], [-169, 0, 0, 0, 0, 18, 0, -18], [0, 0, 17, -27, 27, 0, -5, 0], [109, -109, 0, 0, 0, -40, 0, -20], [0, 0, 110, 2, -56, 0, 27, 0], [3, -6, 0, 0, 0, 30, 0, 30], [1, 1, 0, 0, 0, 0, 0, 68], [224, -112, 0, 0, 0, -32, 0, 0], [0, 0, -70, -30, 30, 0, -50, 0], [-206, 206, 0, 0, 0, -16, 0, -8], [0, 0, 104, -88, -8, 0, 48, 0], [0, 0, -6, 42, 2, 0, -20, 0], [0, 67, 0, 0, 0, -52, 0, -104], [0, 0, -24, -72, -48, 0, 36, 0], [0, 0, -62, -62, -62, 0, 0, 0], [0, 0, 50, -6, 6, 0, 22, 0], [0, 0, -46, 110, -32, 0, -39, 0], [-87, 174, 0, 0, 0, -66, 0, -66], [0, 22, 0, 0, 0, 4, 0, 8], [0, 0, 4, -12, -4, 0, 4, 0], [124, 124, 0, 0, 0, 0, 0, 44], [0, 0, 79, -17, 79, 0, -48, 0], [-132, 264, 0, 0, 0, 84, 0, 84], [0, 0, -33, -9, 11, 0, 10, 0], [0, 13, 0, 0, 0, 44, 0, 88], [0, 0, -170, 222, 74, 0, -26, 0], [-82, 41, 0, 0, 0, 4, 0, 0], [55, -55, 0, 0, 0, 112, 0, 56], [0, 0, -222, 258, 74, 0, -92, 0], [0, -221, 0, 0, 0, 10, 0, 20], [0, 0, 170, -270, -90, 0, 50, 0], [0, 0, 72, 12, 42, 0, -57, 0], [266, -133, 0, 0, 0, -14, 0, 0], [-449, 449, 0, 0, 0, -44, 0, -22], [63, -126, 0, 0, 0, -36, 0, -36], [0, 0, -60, -204, 20, 0, 112, 0], [0, 0, 245, -207, -69, 0, -19, 0], [-322, 0, 0, 0, 0, 52, 0, -52], [0, 0, 46, -98, 46, 0, -72, 0], [415, -415, 0, 0, 0, 80, 0, 40], [0, 0, -231, 201, 77, 0, -62, 0], [0, 193, 0, 0, 0, 26, 0, 52], [0, 0, 48, 204, 126, 0, -87, 0], [470, 0, 0, 0, 0, -44, 0, 44], [-304, 152, 0, 0, 0, -56, 0, 0], [0, 0, 14, -90, 90, 0, -38, 0], [0, 0, -222, -10, 116, 0, -53, 0], [0, -50, 0, 0, 0, -28, 0, -56], [0, 0, -109, 135, 45, 0, -13, 0], [-17, -17, 0, 0, 0, 0, 0, 134], [0, 0, -132, -72, -102, 0, 117, 0], [0, 0, 26, 114, -114, 0, 70, 0], [411, -822, 0, 0, 0, -42, 0, -42], [0, 0, 183, -249, -61, 0, 94, 0], [-344, -344, 0, 0, 0, 0, 0, 8], [0, 0, -192, 252, 30, 0, 81, 0], [0, 0, 77, -35, 77, 0, -56, 0], [0, 0, 66, -154, 44, 0, 55, 0], [27, -54, 0, 0, 0, 96, 0, 96], [-493, 0, 0, 0, 0, 70, 0, -70], [494, -247, 0, 0, 0, 76, 0, 0], [0, 0, 122, -106, -8, 0, 57, 0], [0, 0, -240, 192, 80, 0, -56, 0], [0, 0, -89, 195, 65, 0, -53, 0], [-113, -113, 0, 0, 0, 0, 0, -124], [155, 0, 0, 0, 0, 76, 0, -76], [0, 0, -79, -99, 99, 0, -89, 0], [0, 0, -196, 308, -56, 0, -126, 0], [0, 0, 84, -108, -28, 0, 40, 0], [0, -374, 0, 0, 0, -28, 0, -56], [-53, -53, 0, 0, 0, 0, 0, 2], [-115, 0, 0, 0, 0, -126, 0, 126], [0, 0, -23, -143, -23, 0, -60, 0], [-166, 83, 0, 0, 0, -56, 0, 0], [249, -498, 0, 0, 0, -90, 0, -90], [0, 0, -146, -138, -46, 0, 142, 0], [-356, -356, 0, 0, 0, 0, 0, 44], [0, 0, 264, -408, -72, 0, -96, 0], [0, 0, -214, 10, -214, 0, 112, 0], [0, 0, -130, 54, -54, 0, -38, 0], [-485, 485, 0, 0, 0, 44, 0, 22], [0, 697, 0, 0, 0, 56, 0, 112], [0, 0, 305, -243, -81, 0, -31, 0], [0, 0, 312, -264, 24, 0, -168, 0], [776, -388, 0, 0, 0, -116, 0, 0], [0, 0, 122, -106, -8, 0, 57, 0], [0, 589, 0, 0, 0, 14, 0, 28], [0, 0, 240, -216, 12, 0, -126, 0], [128, -64, 0, 0, 0, 112, 0, 0], [334, -334, 0, 0, 0, -56, 0, -28], [-60, 120, 0, 0, 0, -276, 0, -276], [0, 0, 165, -435, -55, 0, 190, 0], [-131, -131, 0, 0, 0, 0, 0, -298], [-637, 0, 0, 0, 0, -18, 0, 18], [0, 0, 31, 343, 31, 0, 156, 0], [-575, 575, 0, 0, 0, 128, 0, 64], [0, 0, -124, 92, 16, 0, -54, 0], [43, 43, 0, 0, 0, 0, 0, 104], [0, 0, -8, -152, -8, 0, -72, 0], [0, 0, -292, 452, -80, 0, -186, 0], [-108, 216, 0, 0, 0, 252, 0, 252], [0, 0, -359, -75, -25, 0, 217, 0], [-323, -323, 0, 0, 0, 0, 0, -178], [0, 0, -36, -408, -222, 0, 129, 0], [47, 0, 0, 0, 0, 18, 0, -18], [0, 0, -310, -30, 30, 0, -170, 0], [0, 211, 0, 0, 0, 34, 0, 68], [0, 0, 34, 186, 62, 0, -110, 0], [433, 433, 0, 0, 0, 0, 0, -148], [0, 0, -312, -156, -234, 0, 273, 0], [847, -847, 0, 0, 0, -28, 0, -14], [0, 0, 138, -370, 116, 0, 127, 0], [-15, 30, 0, 0, 0, 252, 0, 252], [0, 0, 66, -270, -22, 0, 124, 0], [-1078, 539, 0, 0, 0, 64, 0, 0], [0, 0, 332, -84, 84, 0, 124, 0], [-336, 672, 0, 0, 0, -96, 0, -96], [0, 0, -74, 78, 26, 0, -2, 0], [-205, 0, 0, 0, 0, -68, 0, 68], [0, 0, 151, 55, 151, 0, -48, 0], [0, 0, -187, -159, 159, 0, -173, 0], [0, 0, 254, -214, -20, 0, 117, 0], [133, 133, 0, 0, 0, 0, 0, -106], [0, 0, -348, 312, -18, 0, 183, 0], [0, 0, 326, 118, 326, 0, -104, 0], [0, 0, -175, 117, -117, 0, -29, 0], [-21, 42, 0, 0, 0, 78, 0, 78], [0, 562, 0, 0, 0, -172, 0, -344]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_324_f_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_324_3_f_p();".
// 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_3_f_p(:prec:=8)
    chi := MakeCharacter_324_f();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 3));
    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_3_f_p();".
// 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_3_f_p( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_324_f();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,3,sign)));
    Vf := Kernel([<5,R![4096, 0, 3584, 0, 3072, 0, 56, 0, 1]>,<7,R![3249, 342, -45, -6, 1]>,<11,R![207360000, 0, -5184000, 0, 115200, 0, -360, 0, 1]>],Snew);
    return Vf;
end function;