
// Make newform 405.4.e.r in Magma, downloaded from the LMFDB on 24 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_r();".
// 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_r();".
// 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 := [16, -68, 85, -35, 20, -3, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0, 0, 0], [24, -13, 14, -2, 1, 0], [12, 7, -8, 2, -1, 0], [-64, 149, -52, 38, -5, 2], [-356, 786, -287, 206, -28, 11], [220, -514, 183, -132, 18, -7]];
    Rf_basisdens := [1, 4, 2, 4, 4, 2];
    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 := [[0, 0, 0, -1, 0, 0], [1, 0, 0, 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, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 5, 0, 0], [-14, 0, 0, 14, 2, 0], [12, 0, 4, -12, 2, 4], [0, 10, 0, -14, 10, -4], [3, 2, 8, 0, 0, 0], [59, 14, -4, 0, 0, 0], [0, 32, 0, -39, 32, -4], [42, 0, -8, -42, -42, -8], [0, 30, 0, -83, 30, 8], [50, -64, -8, 0, 0, 0], [0, 42, 0, -132, 42, -16], [16, 0, 8, -16, 78, 8], [-168, 0, 4, 168, -28, 4], [-165, -14, -12, 0, 0, 0], [0, -82, 0, 78, -82, 12], [-173, 0, -16, 173, 60, -16], [0, 52, 0, -302, 52, 24], [192, -34, 60, 0, 0, 0], [404, 22, 60, 0, 0, 0], [-221, 0, 12, 221, -22, 12], [-489, 0, 60, 489, -120, 60], [-756, 66, 48, 0, 0, 0], [-440, 0, -8, 440, 64, -8], [-180, 0, 64, 180, 208, 64], [0, -272, 0, -140, -272, -56], [120, 200, -20, 0, 0, 0], [953, -224, -28, 0, 0, 0], [0, -116, 0, -342, -116, 20], [-322, -108, 16, 0, 0, 0], [0, -144, 0, 1470, -144, -72], [-1845, 0, -68, 1845, -2, -68], [0, 288, 0, 844, 288, 144], [0, -210, 0, 1380, -210, 80], [700, 0, -28, -700, 0, -28], [0, 34, 0, -1112, 34, -140], [-2350, -176, -112, 0, 0, 0], [0, 56, 0, 1257, 56, 128], [-909, 0, -80, 909, 34, -80], [2436, -308, -72, 0, 0, 0], [41, -592, 196, 0, 0, 0], [1182, 0, -24, -1182, 56, -24], [0, -430, 0, 262, -430, 332], [1905, 154, -4, 0, 0, 0], [-880, 276, 136, 0, 0, 0], [0, 242, 0, 3295, 242, -40], [970, 0, 0, -970, 720, 0], [495, 0, 56, -495, -812, 56], [0, 228, 0, 3049, 228, 20], [-30, 64, 8, 0, 0, 0], [0, -188, 0, 234, -188, -160], [-1103, 0, 220, 1103, -1108, 220], [-792, 492, 216, 0, 0, 0], [0, -690, 0, -1689, -690, -252], [-1188, 0, -268, 1188, 268, -268], [-294, -580, -16, 0, 0, 0], [-85, -46, 152, 0, 0, 0], [340, 0, -524, -340, -498, -524], [1254, 0, -184, -1254, 290, -184], [0, -770, 0, 1084, -770, 144], [0, 698, 0, 4227, 698, -28], [-1066, 56, 128, 0, 0, 0], [0, 662, 0, 1914, 662, -340], [-4256, 0, 256, 4256, -740, 256], [-7707, 0, -56, 7707, -90, -56], [3772, 0, 104, -3772, -404, 104], [0, 1370, 0, -5246, 1370, -156], [0, 168, 0, -4164, 168, 760], [-4301, 0, -312, 4301, -152, -312], [-1134, 0, 256, 1134, -116, 256], [5358, -1660, -128, 0, 0, 0], [5506, 0, -200, -5506, -558, -200], [0, 364, 0, 2488, 364, -400], [-2491, 822, 232, 0, 0, 0], [0, -1156, 0, 465, -1156, -232], [264, 0, -16, -264, 1886, -16], [1586, 1254, 412, 0, 0, 0], [0, -1120, 0, 3930, -1120, 320], [0, 248, 0, -7037, 248, 644], [0, -2490, 0, -6900, -2490, -580], [2797, 0, -112, -2797, -48, -112], [-6774, 986, -44, 0, 0, 0], [7658, -38, 756, 0, 0, 0], [-515, 0, 1064, 515, -1746, 1064], [8823, 0, 164, -8823, -1444, 164], [-3306, -2138, -240, 0, 0, 0], [-4148, 0, 768, 4148, 2500, 768], [-4518, 0, -368, 4518, -1892, -368], [0, 1668, 0, 4642, 1668, 56], [8865, 180, -592, 0, 0, 0], [3636, 0, -380, -3636, -1958, -380], [-3868, 390, -1032, 0, 0, 0], [0, 764, 0, 5154, 764, 376], [0, 1754, 0, -6155, 1754, -240], [10983, 1112, -540, 0, 0, 0], [0, -2928, 0, 6636, -2928, 1072], [-708, 902, -48, 0, 0, 0], [5054, -2002, 32, 0, 0, 0], [5210, -464, 604, 0, 0, 0], [1504, 0, -888, -1504, -1538, -888], [66, -3700, -776, 0, 0, 0], [0, 2152, 0, -1440, 2152, -44], [4746, 0, 392, -4746, 146, 392], [0, -2550, 0, 4849, -2550, -552], [11552, -82, -596, 0, 0, 0], [5913, 0, 700, -5913, 520, 700], [741, 3098, -1476, 0, 0, 0], [0, -2334, 0, -1878, -2334, 1100], [13087, 0, 268, -13087, 532, 268], [0, -2334, 0, 664, -2334, 712], [-22792, -1072, -232, 0, 0, 0], [0, 166, 0, -531, 166, 364], [2584, 0, -248, -2584, 4420, -248], [1949, 1202, 1484, 0, 0, 0], [360, 0, 1432, -360, -2096, 1432], [0, 1690, 0, 1750, 1690, 0], [9651, 164, 1760, 0, 0, 0], [0, -442, 0, -11343, -442, 1312], [13698, 0, -828, -13698, 2770, -828], [0, 1880, 0, 7966, 1880, -636], [-4430, 0, -908, 4430, 2574, -908], [-11406, 0, -156, 11406, -2364, -156], [-9459, -1632, -1824, 0, 0, 0], [4711, 0, 1192, -4711, 4358, 1192], [-7542, 1992, 776, 0, 0, 0], [18130, 0, 292, -18130, 2936, 292], [11034, 1368, 192, 0, 0, 0], [3904, 0, 1800, -3904, 3588, 1800], [0, 4176, 0, 8080, 4176, 1728], [2597, 1634, 1808, 0, 0, 0], [0, 5144, 0, -3216, 5144, -1464], [0, -498, 0, 3499, -498, -1004], [-17422, -3660, -248, 0, 0, 0], [0, -698, 0, -17442, -698, -176], [0, -584, 0, 1687, -584, -2492], [-8913, 4834, 1400, 0, 0, 0], [0, 3878, 0, -620, 3878, -1416], [0, -4230, 0, -7809, -4230, 404], [-26412, -730, -648, 0, 0, 0], [12896, -7472, -452, 0, 0, 0], [-19050, 0, -880, 19050, 4160, -880], [0, 1786, 0, -6368, 1786, -80], [3195, 7212, 256, 0, 0, 0], [-8350, 3540, -2044, 0, 0, 0], [-4224, 0, 2104, 4224, -4380, 2104], [7492, 0, -1776, -7492, 6572, -1776], [8901, 0, 864, -8901, -2786, 864], [0, 1614, 0, -14933, 1614, -1088], [7239, 4820, 2292, 0, 0, 0], [0, -2690, 0, 20344, -2690, 1404], [-6222, -7718, 1896, 0, 0, 0], [-18898, 8442, 1272, 0, 0, 0], [0, -804, 0, 7689, -804, -396], [124, 0, 2248, -124, -1416, 2248], [25710, 0, -4, -25710, -858, -4], [11864, -2424, 664, 0, 0, 0], [2292, 0, 16, -2292, 486, 16], [1832, 5084, -24, 0, 0, 0], [0, 7416, 0, 4602, 7416, -880], [10443, 0, -2428, -10443, -3976, -2428], [-14742, 1344, 1600, 0, 0, 0], [0, -9120, 0, 1150, -9120, 1680], [12090, 368, -1664, 0, 0, 0], [0, 8116, 0, -5598, 8116, 440], [26925, 0, -180, -26925, -2652, -180], [14153, 3938, 772, 0, 0, 0], [17128, 0, 836, -17128, -542, 836]];
    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_r();".
// 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_r(:prec:=6)
    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_r();".
// 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_r( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_405_e();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![36, 138, 535, -11, 24, 1, 1]>,<7,R![3240000, 993600, 225504, 20688, 1384, 44, 1]>],Snew);
    return Vf;
end function;