
// Make newform 324.3.f.o in Magma, downloaded from the LMFDB on 20 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_o();".
// 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_o();".
// 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], [0, -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, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, -1, 3, 1, 0, -1, 0], [-1, -1, 0, 0, 0, 0, 0, -2], [0, 0, 3, 3, -1, 0, -2, 0], [0, 1, 0, 0, 0, 2, 0, 4], [0, 0, 10, -2, -4, 0, 3, 0], [3, -6, 0, 0, 0, 0, 0, 0], [0, 0, -7, -3, 3, 0, -5, 0], [0, 0, -4, -4, -4, 0, 0, 0], [16, -8, 0, 0, 0, 8, 0, 0], [-7, 0, 0, 0, 0, -2, 0, 2], [0, 0, 16, 0, 0, 0, -8, 0], [20, 20, 0, 0, 0, 0, 0, 4], [0, 0, 3, -21, -1, 0, 10, 0], [0, 0, -12, 28, -8, 0, -10, 0], [0, 0, -19, 9, -9, 0, -5, 0], [-5, 5, 0, 0, 0, -4, 0, -2], [-38, 19, 0, 0, 0, -16, 0, 0], [0, 0, 24, -48, -12, 0, -6, 0], [29, 0, 0, 0, 0, 4, 0, -4], [-55, -55, 0, 0, 0, 0, 0, -14], [0, 0, -6, -6, 2, 0, 4, 0], [0, 0, 10, -2, -4, 0, 3, 0], [31, -31, 0, 0, 0, -32, 0, -16], [0, 0, -20, -4, -20, 0, 8, 0], [58, -29, 0, 0, 0, 2, 0, 0], [0, 0, 12, 24, 18, 0, -15, 0], [-70, 0, 0, 0, 0, 4, 0, -4], [0, 0, -31, 21, 7, 0, 5, 0], [-84, 168, 0, 0, 0, -12, 0, -12], [0, 0, 8, -24, 24, 0, -8, 0], [0, 0, 11, -13, 11, 0, -12, 0], [-194, 97, 0, 0, 0, -4, 0, 0], [0, 0, -70, 66, 22, 0, 2, 0], [-31, -31, 0, 0, 0, 0, 0, 34], [0, -62, 0, 0, 0, 20, 0, 40], [21, -42, 0, 0, 0, 24, 0, 24], [0, 0, 53, 9, -9, 0, 31, 0], [0, 0, 8, -40, 8, 0, -24, 0], [0, 0, -48, 24, -12, 0, 30, 0], [-169, 0, 0, 0, 0, 18, 0, -18], [0, 0, -15, -39, 5, 0, 22, 0], [0, 109, 0, 0, 0, 20, 0, 40], [0, 0, -110, -2, 56, 0, -27, 0], [3, -6, 0, 0, 0, 30, 0, 30], [-2, 1, 0, 0, 0, 68, 0, 0], [-112, -112, 0, 0, 0, 0, 0, -32], [0, 0, -150, 90, 50, 0, -20, 0], [0, -206, 0, 0, 0, 8, 0, 16], [0, 0, -104, 88, 8, 0, -48, 0], [0, 0, -22, 18, -18, 0, -2, 0], [67, -67, 0, 0, 0, 104, 0, 52], [0, 0, 24, 72, 48, 0, -36, 0], [0, 0, 62, -186, -62, 0, 62, 0], [0, 0, 66, -78, -22, 0, 28, 0], [0, 0, 46, -110, 32, 0, 39, 0], [-87, 174, 0, 0, 0, -66, 0, -66], [22, -22, 0, 0, 0, -8, 0, -4], [0, 0, -4, -4, -4, 0, 0, 0], [-248, 124, 0, 0, 0, 44, 0, 0], [0, 0, 65, 93, 31, 0, -79, 0], [-132, 264, 0, 0, 0, 84, 0, 84], [0, 0, -1, -21, 21, 0, -11, 0], [13, -13, 0, 0, 0, -88, 0, -44], [0, 0, 26, 122, 26, 0, 48, 0], [41, 41, 0, 0, 0, 0, 0, 4], [0, 55, 0, 0, 0, -56, 0, -112], [0, 0, -166, 18, -18, 0, -74, 0], [-221, 221, 0, 0, 0, -20, 0, -10], [0, 0, -50, -130, -50, 0, -40, 0], [0, 0, -72, -12, -42, 0, 57, 0], [-133, -133, 0, 0, 0, 0, 0, -14], [0, -449, 0, 0, 0, 22, 0, 44], [63, -126, 0, 0, 0, -36, 0, -36], [0, 0, 92, -132, 132, 0, -20, 0], [0, 0, 19, -157, 19, 0, -88, 0], [-322, 0, 0, 0, 0, 52, 0, -52], [0, 0, 170, -78, -26, 0, -46, 0], [0, 415, 0, 0, 0, -40, 0, -80], [0, 0, -139, -15, 15, 0, -77, 0], [193, -193, 0, 0, 0, -52, 0, -26], [0, 0, -48, -204, -126, 0, 87, 0], [470, 0, 0, 0, 0, -44, 0, 44], [152, 152, 0, 0, 0, 0, 0, -56], [0, 0, -114, -66, 38, 0, 52, 0], [0, 0, 222, 10, -116, 0, 53, 0], [-50, 50, 0, 0, 0, 56, 0, 28], [0, 0, 13, 77, 13, 0, 32, 0], [34, -17, 0, 0, 0, 134, 0, 0], [0, 0, 132, 72, 102, 0, -117, 0], [0, 0, 210, 18, -70, 0, -44, 0], [411, -822, 0, 0, 0, -42, 0, -42], [0, 0, 155, -33, 33, 0, 61, 0], [688, -344, 0, 0, 0, 8, 0, 0], [0, 0, 192, -252, -30, 0, -81, 0], [0, 0, 91, 63, 21, 0, -77, 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], [-247, -247, 0, 0, 0, 0, 0, 76], [0, 0, -122, 106, 8, 0, -57, 0], [0, 0, -136, -24, 24, 0, -80, 0], [0, 0, 53, 77, 53, 0, 12, 0], [226, -113, 0, 0, 0, -124, 0, 0], [155, 0, 0, 0, 0, 76, 0, -76], [0, 0, -267, 69, 89, 0, 10, 0], [0, 0, 196, -308, 56, 0, 126, 0], [0, 0, 68, -12, 12, 0, 28, 0], [-374, 374, 0, 0, 0, 56, 0, 28], [106, -53, 0, 0, 0, 2, 0, 0], [-115, 0, 0, 0, 0, -126, 0, 126], [0, 0, 203, -249, -83, 0, 23, 0], [83, 83, 0, 0, 0, 0, 0, -56], [249, -498, 0, 0, 0, -90, 0, -90], [0, 0, -142, 50, -142, 0, 96, 0], [712, -356, 0, 0, 0, 44, 0, 0], [0, 0, -264, 408, 72, 0, 96, 0], [0, 0, -122, -306, -102, 0, 214, 0], [0, 0, -114, 222, 38, 0, -92, 0], [0, -485, 0, 0, 0, -22, 0, -44], [697, -697, 0, 0, 0, -112, 0, -56], [0, 0, 31, -193, 31, 0, -112, 0], [0, 0, -312, 264, -24, 0, 168, 0], [-388, -388, 0, 0, 0, 0, 0, -116], [0, 0, -122, 106, 8, 0, -57, 0], [589, -589, 0, 0, 0, -28, 0, -14], [0, 0, -240, 216, -12, 0, 126, 0], [-64, -64, 0, 0, 0, 0, 0, 112], [0, 334, 0, 0, 0, 28, 0, 56], [-60, 120, 0, 0, 0, -276, 0, -276], [0, 0, 245, -135, 135, 0, 55, 0], [262, -131, 0, 0, 0, -298, 0, 0], [-637, 0, 0, 0, 0, -18, 0, 18], [0, 0, -499, 561, 187, 0, -31, 0], [0, -575, 0, 0, 0, -64, 0, -128], [0, 0, 124, -92, -16, 0, 54, 0], [-86, 43, 0, 0, 0, 104, 0, 0], [0, 0, 224, -240, -80, 0, 8, 0], [0, 0, 292, -452, 80, 0, 186, 0], [-108, 216, 0, 0, 0, 252, 0, 252], [0, 0, -217, 167, -217, 0, 192, 0], [646, -323, 0, 0, 0, -178, 0, 0], [0, 0, 36, 408, 222, 0, -129, 0], [47, 0, 0, 0, 0, 18, 0, -18], [0, 0, -510, 450, 170, 0, -140, 0], [211, -211, 0, 0, 0, -68, 0, -34], [0, 0, 110, 14, 110, 0, -48, 0], [-866, 433, 0, 0, 0, -148, 0, 0], [0, 0, 312, 156, 234, 0, -273, 0], [0, 847, 0, 0, 0, 14, 0, 28], [0, 0, -138, 370, -116, 0, -127, 0], [-15, 30, 0, 0, 0, 252, 0, 252], [0, 0, 146, -102, 102, 0, 22, 0], [539, 539, 0, 0, 0, 0, 0, 64], [0, 0, 372, -540, -124, 0, 208, 0], [-336, 672, 0, 0, 0, -96, 0, -96], [0, 0, 2, 50, 2, 0, 24, 0], [-205, 0, 0, 0, 0, -68, 0, 68], [0, 0, -7, 309, 103, 0, -151, 0], [0, 0, -519, 201, 173, 0, -14, 0], [0, 0, -254, 214, 20, 0, -117, 0], [-266, 133, 0, 0, 0, -106, 0, 0], [0, 0, 348, -312, 18, 0, -183, 0], [0, 0, -14, 666, 222, 0, -326, 0], [0, 0, -87, 321, 29, 0, -146, 0], [-21, 42, 0, 0, 0, 78, 0, 78], [562, -562, 0, 0, 0, 344, 0, 172]];
    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_o();".
// 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_o(: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_o();".
// 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_o( : 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;