
// Make newform 810.4.e.h in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_810_e();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_810_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_810_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_810_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_810_e();"
function MakeCharacter_810_e()
    N := 810;
    order := 3;
    char_gens := [731, 487];
    v := [1, 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_810_e_Hecke();"
function MakeCharacter_810_e_Hecke(Kf)
    N := 810;
    order := 3;
    char_gens := [731, 487];
    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 := [[-2, 2], [0, 0], [0, 5], [4, -4], [-42, 42], [0, -20], [93, 0], [59, 0], [0, -9], [-120, 120], [0, -47], [-262, 0], [0, -126], [178, -178], [-144, 144], [741, 0], [0, 444], [-221, 221], [0, 538], [690, 0], [-1126, 0], [-665, 665], [-75, 75], [-1086, 0], [-1544, 1544], [132, -132], [0, 892], [-1140, 0], [-1735, 0], [0, 1434], [686, 0], [0, 114], [-159, 159], [0, -2276], [0, 1398], [-2624, 2624], [0, 394], [-3346, 0], [0, 1491], [-2403, 2403], [-2640, 0], [1073, 0], [-1470, 1470], [0, 4720], [-765, 0], [668, 0], [0, -4601], [2158, -2158], [-3123, 3123], [0, -2027], [-438, 0], [0, -6414], [-3431, 3431], [7308, 0], [0, 3729], [1956, -1956], [990, 0], [8495, 0], [1366, -1366], [-5520, 5520], [0, -5438], [0, 8253], [9290, 0], [0, 8112], [7900, -7900], [4419, -4419], [8200, -8200], [0, 9556], [0, 10116], [6751, -6751], [4062, -4062], [-8778, 0], [-956, 956], [0, -2300], [29, 0], [0, 8127], [-7938, 7938], [272, 0], [0, 4554], [0, -1001], [0, -1794], [16129, -16129], [13356, 0], [-11500, 0], [11149, -11149], [3849, -3849], [-18048, 0], [4264, -4264], [-10242, 10242], [0, -3302], [1923, 0], [15246, -15246], [-8206, 0], [0, -16806], [0, 5425], [19665, 0], [0, -14724], [-2058, 0], [11912, 0], [-5170, 0], [4186, -4186], [-13026, 0], [0, -10668], [-15372, 15372], [0, 14989], [-1066, 0], [-621, 621], [20187, 0], [0, -18228], [11743, -11743], [0, 24418], [2672, 0], [0, -8601], [-21308, 21308], [-19015, 0], [-4416, 4416], [0, -7580], [14901, 0], [0, 12915], [28128, -28128], [0, 8362], [-29708, 29708], [6762, -6762], [19155, 0], [22975, -22975], [6450, 0], [-34538, 34538], [-27114, 0], [-236, 236], [0, -27128], [5249, 0], [0, 13896], [0, -27665], [-8122, 0], [0, 10584], [0, 18619], [-22251, 0], [0, -24854], [0, 3681], [5142, 0], [-18484, 0], [25014, -25014], [0, 32146], [10977, 0], [36602, 0], [11076, -11076], [-36848, 36848], [-26961, 26961], [0, 415], [45501, 0], [0, -35042], [-1080, 0], [-20164, 0], [0, 20067], [26524, -26524], [35568, -35568], [-23704, 0], [-40590, 40590], [-12964, 0], [0, 29922], [5241, -5241], [-26214, 0], [0, -18278], [24942, 0], [0, -11226], [-23073, 23073], [22037, 0], [-19082, 19082]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_810_e_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_810_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_810_4_e_h(:prec:=2)
    chi := MakeCharacter_810_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_810_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_810_4_e_h( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_810_e();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<7,R![16, -4, 1]>,<11,R![1764, 42, 1]>],Snew);
    return Vf;
end function;