
// Make newform 324.3.f.h in Magma, downloaded from the LMFDB on 17 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_h();".
// 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_h();".
// 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 := [1, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rfbasis := [Kf.1^i : i in [0..Degree(Kf)-1]];
    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], [-1, 1]];
    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 := [[2, -2], [0, 0], [7, -7], [10, -5], [10, -5], [-20, 20], [8, 0], [6, -12], [2, 2], [0, 10], [-31, -31], [-10, 0], [-50, 50], [-20, 10], [100, -50], [47, 0], [20, 20], [0, 64], [50, 50], [0, 0], [-55, 0], [-8, 4], [34, -17], [-10, 0], [0, 25], [0, -155], [80, 80], [75, -150], [134, 0], [-74, 74], [-15, 30], [95, 95], [0, -62], [-100, -100], [115, -115], [-50, 25], [-20, 20], [60, -120], [-142, -142], [0, 127], [-135, 270], [56, 0], [40, -20], [-65, 65], [-253, 0], [-75, 150], [86, 86], [40, -20], [-104, 52], [-146, 146], [-334, 0], [-10, -10], [0, -134], [120, -240], [268, -268], [-500, 250], [350, 0], [-21, 42], [0, 520], [0, -440], [-190, -190], [-218, 218], [-120, 240], [170, 170], [0, -485], [0, 217], [-500, 250], [310, -310], [125, 125], [0, -74], [0, 394], [330, -660], [-650, 325], [40, -40], [-396, 792], [-208, -208], [0, 475], [260, 0], [-740, 740], [-659, 659], [-340, -340], [0, 496], [330, -660], [-235, 0], [478, -239], [664, -332], [470, 0], [0, 325], [0, 655], [95, 95], [-33, 66], [-380, 190], [300, -600], [125, 125], [26, 26], [222, -444], [265, -265], [380, 0], [360, -720], [-532, 0], [-1040, 520], [89, 0], [-175, -175], [0, 100], [-196, -196], [-730, 0], [-926, 463], [-982, 0], [-130, -130], [0, -251], [-220, -220], [650, 0], [-758, 758], [-200, 100], [-69, 138], [0, 910], [20, 20], [528, -1056], [103, -103], [70, -35], [-578, 578], [0, -845], [0, -1154], [108, -216], [568, -284], [215, 0], [0, 532], [-660, 1320], [-1430, 715], [-950, 950], [-336, 672], [500, 500], [101, 101], [830, 0], [-560, 560], [331, -331], [-298, 0], [-640, -640], [1303, -1303], [170, 0], [-450, 900], [0, 1090], [545, 545], [48, -96], [542, 0], [-800, 400], [0, -290], [0, -368], [-616, -616], [-588, 1176], [1330, -1330], [-1060, 0], [870, -1740], [-412, -412], [1540, -770], [1780, -890], [525, -1050], [0, -1340], [-1225, 0], [115, -115], [-794, 397], [44, 0], [-145, -145], [195, -390], [346, -346], [916, -458], [-609, 1218], [0, -260]];
    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_h();".
// 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_h(:prec:=2)
    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_h();".
// 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_h( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_324_f();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,3,sign)));
    Vf := Kernel([<5,R![49, -7, 1]>,<7,R![75, -15, 1]>,<11,R![75, -15, 1]>],Snew);
    return Vf;
end function;