
// Make newform 405.4.b.c in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_405_b();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_405_b_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_b_c();".
// 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_b_c();".
// 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 := [1900, 0, 2024, 0, 567, 0, 44, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0], [11, 0, 1, 0, 0, 0, 0, 0], [-5080, -2112, -2700, -1206, -225, -117, -5, -3], [-5080, 2112, -2700, 1206, -225, 117, -5, 3], [0, -3506, 0, -1953, 0, -171, 0, -4], [0, 5126, 0, 2043, 0, 171, 0, 4], [548, 0, 333, 0, 36, 0, 1, 0]];
    Rf_basisdens := [1, 1, 1, 180, 180, 90, 90, 9];
    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_b();"
function MakeCharacter_405_b()
    N := 405;
    order := 2;
    char_gens := [326, 82];
    v := [2, 1];
    // 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_b_Hecke();"
function MakeCharacter_405_b_Hecke(Kf)
    N := 405;
    order := 2;
    char_gens := [326, 82];
    char_values := [[1, 0, 0, 0, 0, 0, 0, 0], [-1, 0, 0, 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, 0, 0], [2, 0, 0, -1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, -1, 0], [0, 0, 1, 0, 0, 0, 0, 1], [0, 0, 0, -2, 2, 1, -1, 0], [0, -8, 0, 0, 0, 3, 0, 0], [15, 0, -4, -1, -1, 0, 0, -2], [0, 4, 0, 1, -1, 1, -5, 0], [40, 0, -13, -1, -1, 0, 0, 5], [-54, 0, -1, 8, 8, 0, 0, -5], [0, -12, 0, -2, 2, 13, 10, 0], [63, 0, -16, -9, -9, 0, 0, -4], [0, -12, 0, 15, -15, 9, -4, 0], [0, -12, 0, 14, -14, 0, 9, 0], [0, 64, 0, 14, -14, -4, 17, 0], [-139, 0, 76, -17, -17, 0, 0, -2], [-42, 0, 14, -31, -31, 0, 0, -2], [0, -60, 0, -26, 26, -8, 8, 0], [-216, 0, -3, 0, 0, 0, 0, 21], [0, -204, 0, 2, -2, -13, -10, 0], [332, 0, -32, -30, -30, 0, 0, 12], [0, 64, 0, -19, 19, 41, 2, 0], [134, 0, 5, 7, 7, 0, 0, 35], [0, -48, 0, 29, -29, 29, 34, 0], [598, 0, 71, -10, -10, 0, 0, -19], [0, 360, 0, -3, 3, 15, 37, 0], [0, 108, 0, -6, 6, -62, -8, 0], [244, 0, -27, 25, 25, 0, 0, 11], [0, 156, 0, -28, 28, 51, 78, 0], [0, 252, 0, -66, 66, 6, -27, 0], [-435, 0, -46, 57, 57, 0, 0, -28], [0, 16, 0, -83, 83, -32, -2, 0], [561, 0, 52, -85, -85, 0, 0, -50], [392, 0, -104, 55, 55, 0, 0, -32], [-960, 0, -47, -22, -22, 0, 0, -59], [0, -492, 0, 53, -53, 20, -21, 0], [0, -696, 0, -103, 103, -31, -58, 0], [0, -164, 0, -81, 81, 21, -78, 0], [0, 592, 0, -84, 84, -81, -27, 0], [-153, 0, 102, 81, 81, 0, 0, -24], [-1520, 0, -75, 52, 52, 0, 0, -25], [2174, 0, 111, 154, 154, 0, 0, 39], [0, 396, 0, -4, 4, 137, -24, 0], [0, -44, 0, -68, 68, 73, 37, 0], [86, 0, 210, 78, 78, 0, 0, -78], [-1899, 0, 20, -91, -91, 0, 0, 22], [0, -204, 0, 64, -64, 46, -36, 0], [0, -188, 0, -59, 59, 79, 172, 0], [136, 0, 252, 37, 37, 0, 0, 68], [0, 252, 0, -126, 126, -11, 88, 0], [1226, 0, -278, 166, 166, 0, 0, 34], [-986, 0, -429, -11, -11, 0, 0, 125], [-2603, 0, -209, -85, -85, 0, 0, 37], [0, 412, 0, 9, -9, -24, 12, 0], [0, -672, 0, 119, -119, 117, 9, 0], [-364, 0, 41, 181, 181, 0, 0, -1], [-3498, 0, -118, 200, 200, 0, 0, 130], [0, 696, 0, 218, -218, -4, 175, 0], [4649, 0, 291, 10, 10, 0, 0, -63], [0, 576, 0, 30, -30, -222, -62, 0], [0, -200, 0, -181, 181, -172, -175, 0], [0, 48, 0, -241, 241, -13, -232, 0], [-806, 0, 105, -172, -172, 0, 0, 105], [0, -360, 0, 213, -213, -12, -160, 0], [0, 1300, 0, 89, -89, 38, -199, 0], [-2376, 0, -143, -316, -316, 0, 0, 1], [0, -348, 0, -270, 270, -174, 148, 0], [0, 1944, 0, -73, 73, -99, -36, 0], [2478, 0, 47, -202, -202, 0, 0, 133], [0, -1092, 0, -139, 139, -27, 306, 0], [862, 0, 157, -490, -490, 0, 0, -107], [0, 912, 0, 190, -190, 262, 132, 0], [0, -912, 0, -379, 379, -19, 84, 0], [3329, 0, -461, 237, 237, 0, 0, 45], [0, -1304, 0, 357, -357, 141, 375, 0], [-426, 0, -584, 84, 84, 0, 0, 124], [0, -480, 0, 249, -249, -162, -348, 0], [-2935, 0, -461, 376, 376, 0, 0, -23], [2819, 0, -215, 264, 264, 0, 0, -21], [744, 0, 1004, -312, -312, 0, 0, 20], [-4572, 0, 737, -91, -91, 0, 0, -41], [-2678, 0, -11, 44, 44, 0, 0, -179], [0, 2352, 0, 203, -203, 170, 98, 0], [5416, 0, -165, -14, -14, 0, 0, -25], [0, -272, 0, 103, -103, -413, -608, 0], [-3677, 0, -8, 107, 107, 0, 0, -248], [0, 2256, 0, -102, 102, -279, -574, 0], [680, 0, 189, 604, 604, 0, 0, 39], [0, -1044, 0, 423, -423, -207, 1, 0], [0, 1708, 0, 633, -633, 189, 312, 0], [3560, 0, -477, -476, -476, 0, 0, 51], [0, -648, 0, -275, 275, 187, 295, 0], [2067, 0, -496, -141, -141, 0, 0, 158], [3357, 0, -154, -667, -667, 0, 0, -140], [0, -932, 0, 160, -160, -224, 280, 0], [1548, 0, 134, 630, 630, 0, 0, -106], [3945, 0, -195, -201, -201, 0, 0, 99], [0, 2076, 0, -248, 248, 46, 160, 0], [788, 0, -81, -417, -417, 0, 0, -159], [0, 4020, 0, -492, 492, -480, -418, 0], [0, 540, 0, -288, 288, -353, -227, 0], [0, -1932, 0, -205, 205, -261, -558, 0], [-917, 0, 206, 686, 686, 0, 0, -250], [2360, 0, 55, -762, -762, 0, 0, 411], [0, 408, 0, 802, -802, -107, -56, 0], [0, 3532, 0, 261, -261, 555, -102, 0], [0, 508, 0, -481, 481, -664, 158, 0], [10112, 0, -959, -386, -386, 0, 0, -47], [-2699, 0, 348, 676, 676, 0, 0, 176], [0, -3984, 0, 401, -401, 755, 42, 0], [0, -324, 0, 947, -947, -271, -349, 0], [0, 4012, 0, -549, 549, 12, 642, 0], [-4353, 0, 1045, 671, 671, 0, 0, 67], [4568, 0, -1871, 438, 438, 0, 0, 177], [-3738, 0, -1433, -123, -123, 0, 0, -131], [0, 708, 0, 865, -865, 457, 884, 0], [0, 3496, 0, 174, -174, -312, -396, 0], [0, 52, 0, -370, 370, 422, -310, 0], [5131, 0, 1397, -475, -475, 0, 0, 143], [6330, 0, -329, 59, 59, 0, 0, -419], [0, -1068, 0, -657, 657, -12, -784, 0], [0, 120, 0, -343, 343, -93, 231, 0], [0, -488, 0, 1284, -1284, 372, 1314, 0], [-301, 0, 445, 261, 261, 0, 0, -549], [12246, 0, 99, 507, 507, 0, 0, -363], [7452, 0, 1096, -55, -55, 0, 0, -80], [-10492, 0, -607, -572, -572, 0, 0, 269], [0, -1176, 0, -21, 21, 1263, 455, 0], [0, 5532, 0, -724, 724, -508, -318, 0], [2260, 0, 2001, 70, 70, 0, 0, -79], [0, 1356, 0, 1343, -1343, -57, 690, 0], [-902, 0, 1350, -104, -104, 0, 0, -466], [0, 2436, 0, 687, -687, 393, -543, 0], [-7403, 0, 1446, -442, -442, 0, 0, -450], [-8950, 0, 49, 711, 711, 0, 0, -1053], [0, 3388, 0, -24, 24, 327, -1134, 0], [0, -2736, 0, 765, -765, -873, 10, 0], [0, -1292, 0, -71, 71, 28, -896, 0], [-19067, 0, -290, 98, 98, 0, 0, -206], [-2896, 0, 1833, 18, 18, 0, 0, -819], [-15178, 0, -465, -305, -305, 0, 0, 189], [0, 2508, 0, -640, 640, 1520, 1356, 0], [0, -7052, 0, 452, -452, 530, -574, 0], [5132, 0, -2061, -1224, -1224, 0, 0, 189], [7912, 0, 1115, -664, -664, 0, 0, -25], [0, 384, 0, -105, 105, 1023, 56, 0], [0, 7528, 0, -119, 119, 358, 424, 0], [15680, 0, 43, -546, -546, 0, 0, -165], [0, 2920, 0, 168, -168, -624, -1293, 0], [0, -4788, 0, -1792, 1792, 77, -1419, 0], [14170, 0, 3313, 350, 350, 0, 0, 679], [0, -708, 0, -419, 419, -1319, 376, 0], [0, 4792, 0, 1600, -1600, 448, 271, 0], [0, 1932, 0, -4, 4, -1300, -1412, 0], [-23386, 0, 117, -1070, -1070, 0, 0, -111], [6524, 0, 141, 1020, 1020, 0, 0, 69], [14654, 0, -1149, -563, -563, 0, 0, -471], [0, 2916, 0, 1001, -1001, 458, 1658, 0], [934, 0, -1530, 23, 23, 0, 0, 546], [0, -4436, 0, -658, 658, 608, 542, 0], [0, -3348, 0, -339, 339, 1414, 172, 0], [0, 864, 0, 929, -929, -1261, 1002, 0], [25187, 0, -850, 199, 199, 0, 0, 512], [0, -6228, 0, -508, 508, -120, 330, 0], [0, 4168, 0, 1295, -1295, 1103, -610, 0], [-1136, 0, 1473, 1072, 1072, 0, 0, 737], [0, 6828, 0, -865, 865, -1036, 19, 0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_405_b_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_405_4_b_c();".
// 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_b_c(:prec:=8)
    chi := MakeCharacter_405_b();
    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_b_c();".
// 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_b_c( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_405_b();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<2,R![1900, 0, 2024, 0, 567, 0, 44, 0, 1]>,<11,R![502920, 7038, -1755, 0, 1]>],Snew);
    return Vf;
end function;