
// Make newform 324.3.d.i in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_324_d();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_324_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_324_3_d_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_3_d_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 := [256, -192, 32, 48, -36, 12, 2, -3, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0, 0, 0, 0, 0], [-1, 2, 0, 0, 0, 0, 0, 0], [64, -96, 32, -12, -20, 10, -1, -1], [0, -160, 48, 4, -12, 6, -5, -1], [128, -32, 16, 36, -12, -2, 3, -1], [80, -16, -4, 12, -2, 1, 1, 0], [192, -32, -48, 36, -12, -2, 3, -1], [80, -32, -12, 16, -6, 1, 2, -1]];
    Rf_basisdens := [1, 1, 64, 64, 64, 16, 64, 16];
    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_d();"
function MakeCharacter_324_d()
    N := 324;
    order := 2;
    char_gens := [163, 245];
    v := [1, 2];
    // 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_d_Hecke();"
function MakeCharacter_324_d_Hecke(Kf)
    N := 324;
    order := 2;
    char_gens := [163, 245];
    char_values := [[-1, 0, 0, 0, 0, 0, 0, 0], [1, 0, 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, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0], [-1, 0, 0, -1, 0, 0, 0, -1], [0, 0, 0, -1, 0, -1, 0, 1], [-1, -1, 1, 1, 0, -1, 2, -1], [6, 1, -1, 0, -2, 0, 0, 0], [2, 2, -3, 0, -6, 0, -2, 0], [1, 1, 2, 1, 0, -2, -2, -1], [-3, -3, 1, -2, 0, -1, 6, 2], [-4, 3, -3, 2, -6, 0, 0, 2], [5, 5, 1, 0, 0, 3, -10, 0], [1, 5, 1, 3, 2, 0, 12, 3], [-10, 5, -3, 3, -6, 0, 4, 3], [6, 6, 0, 2, 0, 5, -12, -2], [-2, -2, 6, -3, 0, 3, 4, 3], [-7, 1, 3, 1, 6, 0, 8, 1], [2, 2, 6, 6, 0, 3, -4, -6], [6, 13, -7, 12, -14, 0, 12, 12], [3, 3, -9, 1, 0, -11, -6, -1], [1, 1, -5, -5, 0, -22, -2, 5], [4, 8, -5, -12, -10, 0, 6, -12], [11, 11, -5, 4, 0, -11, -22, -4], [5, 5, -26, 1, 0, -1, -10, -1], [-25, -13, 9, -3, 18, 0, -8, -3], [16, -1, 1, -9, 2, 0, 0, -9], [41, -10, 6, -3, 12, 0, -8, -3], [-4, -4, 28, -3, 0, 9, 8, 3], [13, 13, -8, 7, 0, 26, -26, -7], [25, -7, 7, 9, 14, 0, 0, 9], [-46, -9, 12, -1, 24, 0, 6, -1], [-18, -18, 0, 6, 0, -12, 36, -6], [-11, -11, 14, -13, 0, -23, 22, 13], [-94, -3, 3, 17, 6, 0, 0, 17], [12, 12, -36, -2, 0, -5, -24, 2], [8, 15, 9, 2, 18, 0, 48, 2], [-13, -13, -23, -8, 0, -35, 26, 8], [-3, 10, -22, 21, -44, 0, -24, 21], [2, 2, 4, 8, 0, -16, -4, -8], [-10, -10, -12, -9, 0, 21, 20, 9], [113, 18, 0, 5, 0, 0, 36, 5], [-2, -2, 34, -14, 0, 20, 4, 14], [-49, 3, -27, -3, -54, 0, -48, -3], [-26, -26, 24, 15, 0, 39, 52, -15], [0, -41, 29, 3, 58, 0, -24, 3], [38, -4, -24, 6, -48, 0, -56, 6], [-3, -3, -27, 3, 0, 42, 6, -3], [-38, -38, -19, 2, 0, -13, 76, -2], [-38, -38, 44, 15, 0, -9, 76, -15], [9, 9, -25, -25, 0, -29, -18, 25], [-123, -2, 8, -15, 16, 0, 12, -15], [-97, 5, 18, -21, 36, 0, 46, -21], [-37, -37, 9, 30, 0, -9, 74, -30], [81, -38, 8, 30, 16, 0, -60, 30], [-57, -57, 0, -3, 0, -18, 114, 3], [-109, 48, -18, 14, -36, 0, 60, 14], [-1, -1, 19, -14, 0, 17, 2, 14], [137, 5, 21, -21, 42, 0, 52, -21], [8, 8, -38, -22, 0, -28, -16, 22], [55, 20, -38, -45, -76, 0, -36, -45], [-85, 54, -15, -4, -30, 0, 78, -4], [-29, -29, 44, -19, 0, -43, 58, 19], [89, -42, 18, 23, 36, 0, -48, 23], [3, 3, -12, -3, 0, -66, -6, 3], [45, 45, 19, -8, 0, 35, -90, 8], [51, 4, 8, 6, 16, 0, 24, 6], [140, -9, 15, 32, 30, 0, 12, 32], [-38, -38, -13, -18, 0, -75, 76, 18], [79, 68, -38, 78, -76, 0, 60, 78], [-31, -31, -27, -21, 0, 81, 62, 21], [-101, 8, 34, 21, 68, 0, 84, 21], [-166, -91, 51, 15, 102, 0, -80, 15], [-26, -26, -62, -32, 0, 8, 52, 32], [39, 39, 21, -46, 0, 65, -78, 46], [28, 35, -23, -66, -46, 0, 24, -66], [72, 72, -39, 48, 0, 96, -144, -48], [28, 28, -40, -13, 0, -59, -56, 13], [203, 56, -6, 39, -12, 0, 100, 39], [-119, 65, 19, 33, 38, 0, 168, 33], [-496, -45, 27, -19, 54, 0, -36, -19], [-93, 28, 20, 6, 40, 0, 96, 6], [46, 46, -37, -4, 0, -89, -92, 4], [-132, 19, 5, -24, 10, 0, 48, -24], [2, 2, 98, -4, 0, 64, -4, 4], [82, 14, -5, 54, -10, 0, 18, 54], [33, 33, -51, 50, 0, 41, -66, -50], [118, 118, 42, 36, 0, 57, -236, -36], [-394, 34, 3, -50, 6, 0, 74, -50], [90, 25, 29, -45, 58, 0, 108, -45], [320, -25, -51, -42, -102, 0, -152, -42], [41, 41, -113, -30, 0, -57, -82, 30], [-81, -81, 78, 51, 0, -6, 162, -51], [9, 9, -133, -22, 0, 25, -18, 22], [10, 10, 50, -44, 0, -32, -20, 44], [-56, -56, 50, 44, 0, 103, 112, -44], [16, 16, 14, -70, 0, 35, -32, 70], [39, 39, -87, -27, 0, 6, -78, 27], [224, -1, -51, 108, -102, 0, -104, 108], [-277, -47, -6, 13, -12, 0, -106, 13], [19, 19, -37, -53, 0, 82, -38, 53], [238, -28, -32, 6, -64, 0, -120, 6], [67, 67, 29, -33, 0, -21, -134, 33], [227, 131, -69, 123, -138, 0, 124, 123], [7, 7, -109, 65, 0, 37, -14, -65], [167, 0, -84, 50, -168, 0, -168, 50], [-101, -101, 167, -1, 0, -31, 202, 1], [76, -28, 61, -78, 122, 0, 66, -78], [-86, -86, 168, 18, 0, 21, 172, -18], [-313, -1, 63, 39, 126, 0, 124, 39], [-42, -42, 184, 19, 0, 77, 84, -19], [-204, -89, 11, -21, 22, 0, -156, -21], [36, 36, -102, 85, 0, 25, -72, -85], [197, -75, 15, -39, 30, 0, -120, -39], [-136, -9, -39, -91, -78, 0, -96, -91], [-11, -11, -79, 27, 0, -117, 22, -27], [-76, -76, 118, 50, 0, 116, 152, -50], [-571, 78, -72, 26, -144, 0, 12, 26], [-15, -15, -33, -17, 0, 121, 30, 17], [50, 50, -10, 56, 0, -8, -100, -56], [179, -76, 120, -15, 240, 0, 88, -15], [64, 64, 15, 54, 0, 93, -128, -54], [-93, -80, -10, 45, -20, 0, -180, 45], [-255, 40, -37, -30, -74, 0, 6, -30], [-247, -82, 66, -75, 132, 0, -32, -75], [-19, -19, -136, 11, 0, -62, 38, -11], [-71, -71, 8, 27, 0, 99, 142, -27], [443, 47, -99, -99, -198, 0, -104, -99], [-27, -74, 152, 39, 304, 0, 156, 39], [60, 60, 24, 12, 0, 120, -120, -12], [-85, -85, -125, -38, 0, 1, 170, 38], [-38, 113, -107, 102, -214, 0, 12, 102], [-30, -30, -45, -6, 0, -84, 60, 6], [104, 104, -96, 9, 0, 33, -208, -9], [0, 0, -144, -77, 0, 85, 0, 77], [-22, 48, 0, 60, 0, 0, 96, 60], [215, -6, 21, -4, 42, 0, 30, -4], [6, -89, 2, -147, 4, 0, -174, -147], [227, -145, 129, 21, 258, 0, -32, 21], [-51, -51, 54, 79, 0, 49, 102, -79], [80, 63, 21, -64, 42, 0, 168, -64], [110, 50, 15, 114, 30, 0, 130, 114], [-213, -213, 162, 63, 0, 114, 426, -63], [-166, 81, -39, 122, -78, 0, 84, 122], [-45, -45, 45, 28, 0, -125, 90, -28], [26, 26, -10, -58, 0, -116, -52, 58], [371, -117, 39, -93, 78, 0, -156, -93], [6, 6, 62, 95, 0, 199, -12, -95], [-218, -19, -23, 78, -46, 0, -84, 78], [347, -102, 123, -118, 246, 0, 42, -118], [7, 7, 17, 29, 0, -67, -14, -29], [80, 80, -58, -70, 0, 4, -160, 70], [336, -65, 125, -132, 250, 0, 120, -132], [-421, 7, 9, -131, 18, 0, 32, -131], [151, 151, -118, -5, 0, -38, -302, 5], [15, 15, 115, 46, 0, 263, -30, -46], [16, 16, 32, 48, 0, 9, -32, -48], [-117, -117, 101, -58, 0, 25, 234, 58], [34, 34, 158, -44, 0, 160, -68, 44], [-175, 24, 3, -118, 6, 0, 54, -118], [43, -61, 31, -33, 62, 0, -60, -33], [953, -30, -24, 47, -48, 0, -108, 47], [35, 35, -161, 97, 0, 143, -70, -97], [290, -34, 3, -18, 6, 0, -62, -18], [161, 161, 103, -24, 0, 99, -322, 24], [-31, -31, -19, 29, 0, 46, 62, -29], [-379, 170, -186, 108, -372, 0, -32, 108], [97, 97, -51, -30, 0, -39, -194, 30], [-48, -48, -246, -18, 0, -84, 96, 18], [-3, 274, -190, 69, -380, 0, 168, 69]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_324_d_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_324_3_d_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_3_d_i(:prec:=8)
    chi := MakeCharacter_324_d();
    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_d_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_3_d_i( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_324_d();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,3,sign)));
    Vf := Kernel([<5,R![-296, -255, -53, 3, 1]>],Snew);
    return Vf;
end function;