
// Make newform 405.4.e.n in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_405_e();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_405_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_405_4_e_n();".
// 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_405_4_e_n();".
// 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_405_e();"
function MakeCharacter_405_e()
    N := 405;
    order := 3;
    char_gens := [326, 82];
    v := [2, 3];
    // 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_405_e_Hecke();"
function MakeCharacter_405_e_Hecke(Kf)
    N := 405;
    order := 3;
    char_gens := [326, 82];
    char_values := [[0, -1], [1, 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 := [[5, -5], [0, 0], [0, -5], [30, -30], [50, -50], [0, 20], [10, 0], [-44, 0], [0, 120], [-50, 50], [0, -108], [-40, 0], [0, 400], [-280, 280], [-280, 280], [610, 0], [0, 50], [518, -518], [0, 180], [-700, 0], [-410, 0], [516, -516], [660, -660], [1500, 0], [1630, -1630], [-450, 450], [0, -770], [-660, 0], [1754, 0], [0, -310], [-1070, 0], [0, 1950], [1050, -1050], [0, -1676], [0, 2050], [-448, 448], [0, 100], [-1900, 0], [0, 1920], [2550, -2550], [-3650, 0], [-4342, 0], [-3500, 3500], [0, -3350], [-90, 0], [3664, 0], [0, 268], [3670, -3670], [3760, -3760], [0, 1434], [3450, 0], [0, -4900], [-4822, 4822], [4650, 0], [0, 5130], [-1280, 1280], [-3350, 0], [5512, 0], [-4920, 4920], [4500, -4500], [0, 6900], [0, 1530], [3040, 0], [0, -5700], [-3110, 3110], [-950, 950], [-2292, 2292], [0, 7730], [0, -1120], [-1186, 1186], [3630, -3630], [1800, 0], [-8490, 8490], [0, -100], [-8084, 0], [0, -9480], [-10950, 10950], [13840, 0], [0, 9300], [0, 2854], [0, 1150], [11162, -11162], [1200, 0], [1510, 0], [-424, 424], [12360, -12360], [1300, 0], [7190, -7190], [-150, 150], [0, -2670], [-1180, 0], [-14100, 14100], [-9850, 0], [0, -2450], [0, 17036], [20600, 0], [0, 5750], [15500, 0], [-13940, 0], [-20478, 0], [-12040, 12040], [-23550, 0], [0, -6120], [-11700, 11700], [0, 8188], [11690, 0], [21060, -21060], [22910, 0], [0, -1400], [11002, -11002], [0, -4630], [24040, 0], [0, -1890], [-19244, 19244], [15892, 0], [-12600, 12600], [0, -7260], [-7400, 0], [0, -4790], [-1450, 1450], [0, -11818], [-5550, 5550], [12930, -12930], [-32580, 0], [-10228, 10228], [-8350, 0], [14954, -14954], [-29400, 0], [16330, -16330], [0, -30800], [-9524, 0], [0, -28600], [0, 8252], [-24920, 0], [0, 27900], [0, 11506], [12510, 0], [0, 1100], [0, -4490], [-28600, 0], [10068, 0], [-14250, 14250], [0, -6830], [8920, 0], [-3534, 0], [-8000, 8000], [-5160, 5160], [7670, -7670], [0, -25804], [-400, 0], [0, 35100], [18700, 0], [-2980, 0], [0, -35880], [-45240, 45240], [33200, -33200], [35356, 0], [-25700, 25700], [52890, 0], [0, 38050], [-29640, 29640], [-15170, 0], [0, 5470], [-15150, 0], [0, 31190], [-7560, 7560], [32672, 0], [4740, -4740]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_405_e_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_405_4_e_n();".
// 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_405_4_e_n(:prec:=2)
    chi := MakeCharacter_405_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_405_4_e_n();".
// 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_405_4_e_n( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_405_e();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![25, -5, 1]>,<7,R![900, -30, 1]>],Snew);
    return Vf;
end function;