
// Make newform 405.4.e.p 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_p();".
// 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_p();".
// 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, 0, -1, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 1], [0, 2, 0, -1]];
    Rf_basisdens := [1, 1, 1, 1];
    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_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 := [[-1, 1, 0, 0], [1, 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 := 4;
    raw_aps := [[0, 1, -1, 0], [0, 0, 0, 0], [5, -5, 0, 0], [0, 0, 4, 0], [0, -11, 28, 0], [32, -32, -4, 4], [-16, 0, 0, 56], [-5, 0, 0, 68], [-42, 42, -32, 32], [0, -85, 24, 0], [129, -129, -12, 12], [38, 0, 0, -148], [-289, 289, 48, -48], [0, -190, -156, 0], [0, -242, 16, 0], [272, 0, 0, 100], [-353, 353, 28, -28], [0, -334, 192, 0], [726, -726, -52, 52], [487, 0, 0, -196], [592, 0, 0, -92], [0, -204, -104, 0], [0, -222, -600, 0], [513, 0, 0, 0], [0, 334, 440, 0], [0, 141, 608, 0], [-230, 230, -28, 28], [-1062, 0, 0, 260], [1271, 0, 0, -416], [16, -16, 284, -284], [100, 0, 0, -1008], [-1089, 1089, -852, 852], [0, -1740, -712, 0], [-2285, 2285, 36, -36], [446, -446, -552, 552], [0, 1133, 1044, 0], [538, -538, 1784, -1784], [116, 0, 0, 136], [1524, -1524, 892, -892], [0, 1206, 1916, 0], [1001, 0, 0, 1516], [503, 0, 0, 1760], [0, -2263, 1468, 0], [-1922, 1922, -956, 956], [-4260, 0, 0, -272], [2464, 0, 0, 240], [-2555, 2555, 100, -100], [0, 2116, 900, 0], [0, -5476, -172, 0], [-1950, 1950, 1656, -1656], [2994, 0, 0, -1676], [-2480, 2480, 1736, -1736], [0, 1403, -2552, 0], [252, 0, 0, -1728], [630, -630, -1092, 1092], [0, 3206, 740, 0], [6647, 0, 0, 1088], [736, 0, 0, -496], [0, 2754, 2196, 0], [0, 246, -4232, 0], [1890, -1890, 2288, -2288], [-5334, 5334, 1036, -1036], [-3314, 0, 0, -1744], [-1401, 1401, 3460, -3460], [0, -3404, 2504, 0], [0, -5326, -1968, 0], [0, -3483, 2756, 0], [9260, -9260, -428, 428], [-8048, 8048, -912, 912], [0, -1789, -5416, 0], [0, -1602, -124, 0], [3627, 0, 0, -1564], [0, 918, -4776, 0], [6644, -6644, 3488, -3488], [100, 0, 0, 7632], [5136, -5136, -1964, 1964], [0, 1638, 6352, 0], [-1094, 0, 0, 3792], [-6450, 6450, 3808, -3808], [-854, 854, -4640, 4640], [-2776, 2776, 3432, -3432], [0, 971, -4344, 0], [3645, 0, 0, 908], [3142, 0, 0, 136], [0, -2035, -60, 0], [0, -4134, -2528, 0], [4367, 0, 0, -5144], [0, 2714, 8792, 0], [0, 345, 2792, 0], [-4050, 4050, 5856, -5856], [-8540, 0, 0, -1932], [0, 11601, -4204, 0], [-12028, 0, 0, 3228], [1907, -1907, -572, 572], [-13759, 13759, 748, -748], [7816, 0, 0, 4484], [1042, -1042, 2952, -2952], [-16658, 0, 0, -1120], [-10196, 0, 0, -1084], [10587, 0, 0, -5072], [0, -2800, -2188, 0], [-8256, 0, 0, -7432], [-6810, 6810, -3868, 3868], [0, 2229, -5288, 0], [-15341, 15341, 3420, -3420], [-412, 0, 0, 1928], [0, 15522, 3596, 0], [-2054, 0, 0, -2068], [8327, -8327, -5772, 5772], [0, -7445, 5744, 0], [-10774, 10774, 3864, -3864], [28, 0, 0, -1312], [-1452, 1452, 2516, -2516], [0, -8156, 5600, 0], [-8129, 0, 0, 9308], [0, 12315, 7976, 0], [2826, -2826, -14980, 14980], [8930, 0, 0, -1024], [16772, -16772, 772, -772], [0, -15788, 5384, 0], [8423, -8423, 5872, -5872], [0, 594, 2820, 0], [0, 11466, -8940, 0], [4488, 0, 0, -8304], [0, -12772, -5240, 0], [-8315, 0, 0, -4152], [0, 10034, -9488, 0], [-4827, 0, 0, 1764], [0, -14792, 3012, 0], [28042, -28042, 4764, -4764], [-20291, 0, 0, 4148], [10540, -10540, 15940, -15940], [-16168, 16168, -6960, 6960], [26140, 0, 0, -4992], [3189, -3189, -1168, 1168], [24835, -24835, 872, -872], [-2706, 0, 0, 13372], [-520, 520, 4960, -4960], [-2488, 2488, -6012, 6012], [-30311, 0, 0, -1960], [-8829, 0, 0, 11116], [0, -18453, 16120, 0], [3322, -3322, 2588, -2588], [25232, 0, 0, 732], [-1965, 0, 0, 1512], [0, -34099, -4140, 0], [0, 972, -3488, 0], [0, 32152, 6632, 0], [16577, -16577, 2124, -2124], [12628, 0, 0, -2116], [4728, -4728, -19048, 19048], [-16303, 0, 0, 4816], [-700, 0, 0, -21204], [-20514, 20514, 3660, -3660], [0, -31578, 2556, 0], [0, -7049, -4764, 0], [-12587, 0, 0, -7932], [0, -15055, -11808, 0], [40350, 0, 0, -3028], [-20410, 20410, -9720, 9720], [0, 18918, 8164, 0], [27764, 0, 0, -16812], [-35084, 35084, 12144, -12144], [-16749, 0, 0, -9220], [18952, -18952, 16052, -16052], [0, -18852, -8808, 0], [-33649, 0, 0, -10684], [0, -12060, 5744, 0]];
    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_p();".
// 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_p(:prec:=4)
    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_p();".
// 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_p( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_405_e();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![4, 4, 6, -2, 1]>,<7,R![2304, 0, 48, 0, 1]>],Snew);
    return Vf;
end function;