
// Make newform 405.4.e.h 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_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_405_4_e_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_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 := [[1, -1], [0, 0], [0, 5], [6, -6], [-47, 47], [0, 5], [131, 0], [-56, 0], [0, 3], [-157, 157], [0, -225], [-70, 0], [0, 140], [-397, 397], [-347, 347], [-4, 0], [0, 748], [338, -338], [0, -492], [-32, 0], [970, 0], [1257, -1257], [-102, 102], [1488, 0], [-974, 974], [-1335, 1335], [0, -686], [1098, 0], [-700, 0], [0, -1055], [-1646, 0], [0, -1833], [1098, -1098], [0, 1042], [0, -2941], [-511, 511], [0, 571], [713, 0], [0, 1596], [4134, -4134], [-1828, 0], [-520, 0], [4826, -4826], [0, -1670], [1380, 0], [4357, 0], [0, 4162], [5956, -5956], [4940, -4940], [0, -4344], [5202, 0], [0, 1546], [3659, -3659], [-1221, 0], [0, -6255], [836, -836], [2231, 0], [-4832, 0], [-6450, 6450], [-1050, 1050], [0, 1584], [0, 6594], [-4343, 0], [0, -2124], [7516, -7516], [-6880, 6880], [4986, -4986], [0, -904], [0, 8860], [4454, -4454], [-8781, 8781], [-2928, 0], [-9102, 9102], [0, 8183], [6136, 0], [0, 5643], [8991, -8991], [-12449, 0], [0, 8076], [0, 2833], [0, -4777], [6464, -6464], [-10680, 0], [11566, 0], [1448, -1448], [-2376, 2376], [14894, 0], [-16204, 16204], [-5082, 5082], [0, 10326], [-4184, 0], [-15576, 15576], [10220, 0], [0, -2692], [0, -5764], [2437, 0], [0, -5849], [17032, 0], [4147, 0], [-3942, 0], [13751, -13751], [-7944, 0], [0, -6702], [-2760, 2760], [0, -8930], [6944, 0], [-4206, 4206], [6571, 0], [0, -9490], [-11861, 11861], [0, 518], [15163, 0], [0, 19011], [7906, -7906], [-3416, 0], [4830, -4830], [0, -12549], [-8164, 0], [0, 23768], [21472, -21472], [0, -12982], [6006, -6006], [1164, -1164], [-26496, 0], [-17110, 17110], [-30251, 0], [-18820, 18820], [-31890, 0], [11452, -11452], [0, -7094], [-3200, 0], [0, -20831], [0, 15605], [21349, 0], [0, 3702], [0, 1393], [6906, 0], [0, -30493], [0, 33488], [15304, 0], [-40122, 0], [25098, -25098], [0, 43492], [-11206, 0], [-23964, 0], [-34606, 34606], [18477, -18477], [-41342, 41342], [0, -21898], [18487, 0], [0, -7593], [3038, 0], [-16732, 0], [0, -8031], [38487, -38487], [-5120, 5120], [28075, 0], [-12856, 12856], [-1374, 0], [0, 8543], [13506, -13506], [-21775, 0], [0, -3854], [-12933, 0], [0, 17521], [-12573, 12573], [8945, 0], [58179, -58179]];
    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_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_405_4_e_h(: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_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_405_4_e_h( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_405_e();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![1, -1, 1]>,<7,R![36, -6, 1]>],Snew);
    return Vf;
end function;