
// Make newform 405.4.e.u in Magma, downloaded from the LMFDB on 17 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_u();".
// 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_u();".
// 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 := [243, 0, 223, 0, 29, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0, 0, 0], [9, 13, 1, 1, 0, 0], [-36, 79, 0, 20, 0, 1], [9, 0, 1, 0, 0, 0], [-207, 29, -144, -20, -9, -1], [207, 29, 144, -20, 9, -1]];
    Rf_basisdens := [1, 4, 72, 2, 72, 72];
    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, 1, 0, 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, -1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 5, 0, 0, 0], [10, 5, 10, 0, 0, -3], [17, -7, 17, 0, 0, 5], [0, -13, -20, 13, -13, 0], [-14, 0, 0, -8, 8, -8], [-5, 0, 0, -10, -14, 14], [0, 15, 22, -15, 19, 0], [95, -65, 95, 0, 0, -13], [0, -5, 211, 5, -1, 0], [-156, 0, 0, -54, 2, -2], [0, 2, -59, -2, -62, 0], [288, 28, 288, 0, 0, 8], [56, -67, 56, 0, 0, -31], [-186, 0, 0, -53, 21, -21], [0, 58, -159, -58, 90, 0], [-6, -122, -6, 0, 0, -58], [0, 154, 38, -154, -18, 0], [-177, 0, 0, -79, -69, 69], [-226, 0, 0, 32, 12, -12], [184, -82, 184, 0, 0, 54], [648, 210, 648, 0, 0, -30], [297, 0, 0, 39, -3, 3], [-112, -336, -112, 0, 0, -32], [329, 393, 329, 0, 0, 5], [0, 153, -402, -153, -143, 0], [-1870, 0, 0, 12, 16, -16], [631, 0, 0, 15, 133, -133], [0, 412, 646, -412, 136, 0], [-954, 0, 0, -319, 227, -227], [0, -252, -723, 252, 60, 0], [-1108, -378, -1108, 0, 0, 38], [0, 522, -43, -522, -78, 0], [0, 32, -254, -32, -152, 0], [-1131, 391, -1131, 0, 0, 59], [0, 351, -1932, -351, 139, 0], [-170, 0, 0, -666, -206, 206], [0, 570, -74, -570, 46, 0], [794, -507, 794, 0, 0, -211], [477, 0, 0, -344, 360, -360], [-981, 0, 0, 561, 83, -83], [669, 97, 669, 0, 0, 213], [0, 708, 40, -708, 488, 0], [-392, 0, 0, 243, -175, 175], [144, 0, 0, -160, 200, -200], [0, -702, -1697, 702, -574, 0], [-1004, 60, -1004, 0, 0, -360], [-1694, 622, -1694, 0, 0, 538], [0, -878, 502, 878, -718, 0], [688, 0, 0, 24, 260, -260], [0, 322, -1808, -322, 682, 0], [-2713, 349, -2713, 0, 0, 457], [-2628, 0, 0, -465, -291, 291], [0, -24, 1026, 24, 876, 0], [1498, 1147, 1498, 0, 0, -581], [1963, 0, 0, 1561, -301, 301], [-1976, 0, 0, -1162, 682, -682], [3860, 11, 3860, 0, 0, -5], [2686, 855, 2686, 0, 0, -701], [0, -986, -946, 986, 102, 0], [0, -429, -2960, 429, 715, 0], [-2012, 0, 0, 2132, -452, 452], [0, -441, -815, 441, 283, 0], [-1696, 168, -1696, 0, 0, -272], [-6340, 1351, -6340, 0, 0, 203], [549, 557, 549, 0, 0, -463], [0, -1558, 2612, 1558, 270, 0], [0, -1124, -5806, 1124, -604, 0], [787, 1621, 787, 0, 0, -951], [7952, 474, 7952, 0, 0, 566], [2075, 0, 0, -27, 871, -871], [1706, 458, 1706, 0, 0, 178], [0, 956, 6442, -956, -604, 0], [9888, 0, 0, 113, -517, 517], [0, -843, -3464, 843, 37, 0], [3886, -702, 3886, 0, 0, 82], [-5910, 0, 0, 1364, 236, -236], [0, -1155, -8090, 1155, 169, 0], [0, -3519, 2230, 3519, -1363, 0], [0, 1580, 7276, -1580, 916, 0], [-4341, 793, -4341, 0, 0, -1291], [-1339, 0, 0, 861, -1121, 1121], [-5998, 0, 0, -2230, -618, 618], [-1587, -1877, -1587, 0, 0, 1151], [4552, -1434, 4552, 0, 0, 958], [3687, 0, 0, -590, -1218, 1218], [-5120, -344, -5120, 0, 0, -1752], [-475, -1275, -475, 0, 0, -1399], [0, 1681, -5054, -1681, -595, 0], [106, 0, 0, -1384, 2084, -2084], [-5107, -873, -5107, 0, 0, -1933], [2864, 0, 0, 1047, 1737, -1737], [0, -358, 4445, 358, -622, 0], [0, -2668, -2689, 2668, 108, 0], [13308, 0, 0, 1772, -1656, 1656], [0, 3112, -1030, -3112, -1240, 0], [7926, 0, 0, 2549, -153, 153], [5596, 0, 0, 182, 1318, -1318], [5775, 0, 0, 1903, 485, -485], [-6608, -188, -6608, 0, 0, 0], [-1144, 0, 0, -1953, 2797, -2797], [0, -2328, -6148, 2328, 1940, 0], [-7739, 342, -7739, 0, 0, -1970], [0, 4437, 2873, -4437, 1689, 0], [738, 0, 0, -1110, -2914, 2914], [7364, 4422, 7364, 0, 0, -478], [5694, 0, 0, -4372, 768, -768], [0, 335, 5353, -335, -1781, 0], [-6793, -1788, -6793, 0, 0, 2476], [0, 3032, -8304, -3032, 1696, 0], [1720, 0, 0, 4637, 127, -127], [0, -1362, 1400, 1362, 1874, 0], [11708, -5331, 11708, 0, 0, -287], [14107, 0, 0, -2629, 889, -889], [-10081, 4989, -10081, 0, 0, 1361], [0, 6712, -10384, -6712, 684, 0], [9970, 0, 0, 1330, 1502, -1502], [0, 698, -3064, -698, -2338, 0], [6024, 2777, 6024, 0, 0, 1581], [0, -991, 101, 991, -3411, 0], [8474, 200, 8474, 0, 0, 820], [-3666, -6117, -3666, 0, 0, 1251], [4932, 0, 0, -4926, -426, 426], [5612, 2779, 5612, 0, 0, -2657], [-2447, 0, 0, -1837, 1625, -1625], [9758, -1436, 9758, 0, 0, -1196], [-4575, 0, 0, -7053, -231, 231], [2380, -3963, 2380, 0, 0, -2235], [0, 3492, 2870, -3492, 4056, 0], [9157, 0, 0, -7447, 2995, -2995], [0, -3554, -22434, 3554, -1158, 0], [0, -5470, -2232, 5470, 1090, 0], [6228, 0, 0, -4627, -385, 385], [0, -1422, 17519, 1422, 1682, 0], [0, 315, 11145, -315, 1351, 0], [15940, 0, 0, 1986, -4918, 4918], [0, 2804, 20114, -2804, -2532, 0], [0, 5102, 10000, -5102, -1262, 0], [-16647, 0, 0, 2054, -1158, 1158], [7395, 0, 0, 4699, -271, 271], [8995, 3213, 8995, 0, 0, -239], [0, 1182, -4110, -1182, -3662, 0], [-15868, 0, 0, 3400, -3356, 3356], [-7421, 0, 0, 2299, -1439, 1439], [-9991, -107, -9991, 0, 0, 161], [5266, 5204, 5266, 0, 0, -996], [-7392, -5734, -7392, 0, 0, 3978], [0, 6191, 7779, -6191, -581, 0], [4830, 0, 0, -3523, 4659, -4659], [0, 5815, 28772, -5815, -69, 0], [-5919, 0, 0, -521, 5229, -5229], [9902, 0, 0, -4128, -1452, 1452], [0, 6477, -6264, -6477, -3771, 0], [11234, 5972, 11234, 0, 0, 3472], [-7589, 7325, -7589, 0, 0, 313], [-1323, 0, 0, 4229, -3337, 3337], [-20503, 2707, -20503, 0, 0, -265], [-538, 0, 0, -38, 3450, -3450], [0, -6988, 20962, 6988, -1316, 0], [29902, 4734, 29902, 0, 0, -470], [14336, 0, 0, 9952, 2212, -2212], [0, 7398, -1738, -7398, -3162, 0], [-21121, 0, 0, -3252, -3956, 3956], [0, -4286, 5272, 4286, -746, 0], [2766, 1998, 2766, 0, 0, 5622], [5595, 0, 0, 8259, -5431, 5431], [-23418, -3253, -23418, 0, 0, -5569]];
    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_u();".
// 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_u(: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_u();".
// 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_u( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_405_e();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![324, -252, 214, -22, 15, -1, 1]>,<7,R![260499600, -10458720, 823404, -16080, 1273, -25, 1]>],Snew);
    return Vf;
end function;