
// Make newform 1620.4.i.r in Magma, downloaded from the LMFDB on 09 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1620_i();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_1620_i_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_1620_4_i_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_1620_4_i_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 := [4, -2, -1, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [-4, -1, 1, 1], [3, 9, 3, -3], [-42, 9, 3, 15]];
    Rf_basisdens := [1, 2, 1, 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_1620_i();"
function MakeCharacter_1620_i()
    N := 1620;
    order := 3;
    char_gens := [811, 1541, 1297];
    v := [3, 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_1620_i_Hecke();"
function MakeCharacter_1620_i_Hecke(Kf)
    N := 1620;
    order := 3;
    char_gens := [811, 1541, 1297];
    char_values := [[1, 0, 0, 0], [0, 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, 0, 0, 0], [0, 0, 0, 0], [0, -5, 0, 0], [1, 1, 0, 1], [-21, -21, 0, 1], [0, -7, -5, 5], [12, 0, 7, 0], [-70, 0, 1, 0], [0, 63, 8, -8], [63, 63, 0, 11], [0, -28, 17, -17], [-286, 0, 0, 0], [0, -33, -21, 21], [265, 265, 0, 7], [198, 198, 0, -14], [-252, 0, -25, 0], [0, 219, -7, 7], [301, 301, 0, -42], [0, -460, -42, 42], [399, 0, -15, 0], [-385, 0, -27, 0], [862, 862, 0, 21], [243, 243, 0, -28], [-147, 0, 63, 0], [-56, -56, 0, 80], [84, 84, 0, -56], [0, -700, -24, 24], [-420, 0, -68, 0], [113, 0, 56, 0], [0, 1344, 26, -26], [-232, 0, -42, 0], [0, 2058, 56, -56], [1764, 1764, 0, 39], [0, -700, 48, -48], [0, 357, 9, -9], [76, 76, 0, 0], [0, 455, -65, 65], [986, 0, -56, 0], [0, -93, -196, 196], [1890, 1890, 0, 49], [-630, 0, -230, 0], [2345, 0, -80, 0], [882, 882, 0, 188], [0, 2129, -105, 105], [-2562, 0, 131, 0], [-1078, 0, 134, 0], [0, 2000, -161, 161], [658, 658, 0, 200], [5061, 5061, 0, 42], [0, 2429, 182, -182], [546, 0, 96, 0], [0, 3780, 46, -46], [-5621, -5621, 0, 38], [2910, 0, 266, 0], [0, 714, 497, -497], [4326, 4326, 0, 214], [-5640, 0, 98, 0], [-3724, 0, -9, 0], [-4787, -4787, 0, -217], [-5019, -5019, 0, -95], [0, 917, -127, 127], [0, 912, -189, 189], [-4606, 0, 84, 0], [0, 8139, -91, 91], [-2030, -2030, 0, -370], [7938, 7938, 0, -125], [-218, -218, 0, 630], [0, 2129, 35, -35], [0, -672, -228, 228], [2023, 2023, 0, 444], [-180, -180, 0, -378], [2688, 0, 158, 0], [-1799, -1799, 0, 201], [0, 2162, 602, -602], [-2344, 0, -707, 0], [0, -999, 602, -602], [105, 105, 0, 815], [-3115, 0, -227, 0], [0, 10038, -288, 288], [0, -2191, -652, 652], [0, 1041, 413, -413], [61, 61, 0, -672], [9639, 0, 531, 0], [9527, 0, -157, 0], [4102, 4102, 0, -873], [7497, 7497, 0, -274], [-4935, 0, 413, 0], [394, 394, 0, 210], [-9060, -9060, 0, 462], [0, -10564, 126, -126], [-4389, 0, 294, 0], [1671, 1671, 0, 749], [-6505, 0, 413, 0], [0, 5376, 1138, -1138], [0, -8602, -21, 21], [2571, 0, 364, 0], [0, -12948, -168, 168], [6351, 0, -483, 0], [3605, 0, 153, 0], [-13294, 0, 392, 0], [3697, 3697, 0, 63], [-168, 0, 138, 0], [0, -2436, -1316, 1316], [945, 945, 0, -1223], [0, -190, 931, -931], [-2569, 0, -279, 0], [6069, 6069, 0, 1092], [-8790, 0, 539, 0], [0, -3339, -1185, 1185], [-17339, -17339, 0, -422], [0, -3913, -177, 177], [4160, 0, 1176, 0], [0, -2856, 837, -837], [-7322, -7322, 0, 306], [-13882, 0, -665, 0], [-28224, -28224, 0, 72], [0, 12845, 771, -771], [10269, 0, -1274, 0], [0, -1764, 749, -749], [-16359, -16359, 0, -1111], [0, -4858, 324, -324], [14443, 14443, 0, -777], [-12768, -12768, 0, -1246], [-3507, 0, 832, 0], [-20468, -20468, 0, 75], [-1218, 0, 2012, 0], [-1226, -1226, 0, -1540], [24054, 0, 420, 0], [-27650, -27650, 0, -702], [0, -2968, 936, -936], [5294, 0, 1575, 0], [0, -19068, 1332, -1332], [0, 4142, -2583, 2583], [-22756, 0, -378, 0], [0, -24561, 77, -77], [0, 7805, 1780, -1780], [6480, 0, -161, 0], [0, 4361, 1197, -1197], [0, -13692, -2093, 2093], [-31017, 0, 141, 0], [3752, 0, 520, 0], [-18858, -18858, 0, -416], [0, 30323, -413, 413], [22785, 0, 602, 0], [-1204, 0, -2154, 0], [6414, 6414, 0, 1554], [47278, 47278, 0, -42], [-25842, -25842, 0, 7], [0, 1190, -639, 639], [5481, 0, -1106, 0], [0, -26839, -959, 959], [-5985, 0, 1155, 0], [33419, 0, 595, 0], [0, 29265, 938, -938], [-14960, -14960, 0, -1764], [18795, 18795, 0, -2157], [5132, 0, 3276, 0], [-10911, -10911, 0, 1715], [-25354, 0, 2306, 0], [0, 6666, 3332, -3332], [3759, 3759, 0, 1660], [17850, 0, -1328, 0], [0, -8062, 3696, -3696], [-47598, 0, -392, 0], [0, -6300, 6, -6], [7635, 7635, 0, -2422], [-17590, 0, -2289, 0], [-1001, -1001, 0, 417], [6593, 0, 1302, 0], [0, 1482, -2527, 2527], [-31164, -31164, 0, -424], [0, -50155, 534, -534], [0, -17829, 2161, -2161], [27028, 27028, 0, 2632], [0, 686, 2629, -2629], [0, 5031, -2359, 2359], [-18566, -18566, 0, -1057], [-45633, 0, -749, 0], [13979, 0, -2935, 0], [50890, 50890, 0, -104], [1723, 1723, 0, -427], [47094, 47094, 0, 938], [0, -31750, -2520, 2520], [27792, 0, -1022, 0], [0, -4515, 2984, -2984], [22776, 22776, 0, 308], [-32293, 0, 987, 0], [49126, 49126, 0, -1030], [0, -18457, -2086, 2086], [3519, 0, 1463, 0], [10514, 0, -428, 0], [57225, 57225, 0, 908], [-37918, 0, 1211, 0], [27708, 27708, 0, 1428], [-1155, 0, 1668, 0], [0, 48060, -2345, 2345], [0, -1465, 1302, -1302], [51010, 51010, 0, 294], [-15876, -15876, 0, -1078], [23256, 0, -868, 0], [0, 3843, 1367, -1367], [-11522, -11522, 0, -4098], [0, -31570, 408, -408], [-54551, -54551, 0, -1452], [39765, 0, -3003, 0], [9606, 0, -2072, 0], [-49966, 0, 2026, 0], [0, 567, 1378, -1378], [-8988, -8988, 0, 1584], [0, -4396, 2240, -2240], [6593, 0, -4403, 0], [0, -20559, -2807, 2807], [55180, 55180, 0, -1428], [17148, 17148, 0, -1764], [0, 58704, -1148, 1148], [-59591, -59591, 0, 42], [0, -10093, -1841, 1841], [-26628, -26628, 0, 2492], [63861, 0, 1534, 0], [0, 73710, 1095, -1095], [0, 14582, 812, -812], [0, -7000, -1383, 1383], [0, 10983, 4357, -4357], [63533, 0, 2247, 0], [0, 100305, -252, 252], [29329, 29329, 0, -2660], [62484, 62484, 0, 819], [56007, 0, 681, 0], [-26096, -26096, 0, 2906], [58884, 58884, 0, 1352], [0, 13445, -2807, 2807], [13916, 0, -4689, 0], [0, 8240, 3472, -3472], [0, 57750, -2920, 2920], [8575, 8575, 0, 1251], [42525, 42525, 0, 2282], [0, 3395, -330, 330], [-27258, 0, 2607, 0], [0, 34062, -1476, 1476], [-26325, 0, 4081, 0], [5460, 5460, 0, 4252], [-42112, 0, 5031, 0], [0, 29372, -1050, 1050], [58553, 0, -1008, 0], [0, 34944, 5026, -5026], [-63264, -63264, 0, 3654], [-14434, 0, 5050, 0], [0, 6753, 763, -763], [0, 74780, -3640, 3640], [-6699, 0, -1528, 0], [0, -5956, 1890, -1890], [29316, 0, -826, 0], [0, -43785, 718, -718], [-14903, -14903, 0, 2214], [-71328, -71328, 0, -2275], [-16548, 0, -1100, 0], [23981, 0, 5460, 0], [-36029, -36029, 0, -4077], [69300, 0, 595, 0], [-3409, 0, 2475, 0], [68227, 68227, 0, 2513], [-43365, -43365, 0, -6162], [0, 6167, 3048, -3048], [41552, 0, -3700, 0], [0, -16758, -7231, 7231], [31948, 31948, 0, -3072], [-72324, 0, -572, 0], [-83622, -83622, 0, 1680], [0, -39166, 4116, -4116], [0, -8400, 5142, -5142], [0, -58429, -2972, 2972], [-14380, 0, -5670, 0], [-26075, -26075, 0, -1845], [0, -20656, 21, -21], [0, 88991, -517, 517], [-22099, 0, 8781, 0], [0, -5439, -2458, 2458], [-35225, -35225, 0, 1484], [953, 0, -5684, 0], [-10113, -10113, 0, -5159], [0, -107340, -2464, 2464], [0, 22952, -8379, 8379], [14943, 14943, 0, 2170], [31969, 31969, 0, 7212], [0, -90748, 3336, -3336], [17115, 0, 2467, 0], [38900, 0, 6902, 0], [0, -45570, 88, -88], [6370, 6370, 0, -6135], [-50724, 0, 1316, 0], [-26964, -26964, 0, -84], [-83907, 0, -3962, 0], [0, 31962, -3500, 3500], [0, -21912, 8330, -8330], [110419, 110419, 0, -1491], [0, -57990, 5222, -5222], [60424, 60424, 0, 4673], [-32370, -32370, 0, 4536], [-25356, 0, 8652, 0], [61747, 61747, 0, 3903], [0, -31633, -225, 225], [-36582, 0, 2309, 0], [-79672, 0, -1120, 0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1620_i_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1620_4_i_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_1620_4_i_r(:prec:=4)
    chi := MakeCharacter_1620_i();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 4));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(1999) - 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_1620_4_i_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_1620_4_i_r( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1620_i();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<7,R![35344, 376, 192, -2, 1]>,<11,R![63504, 10584, 1512, 42, 1]>],Snew);
    return Vf;
end function;