
// Make newform 38.7.b.a in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_38_b();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_38_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_38_7_b_a();".
// 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_38_7_b_a();".
// 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 := [2900002611200, 0, 232626987584, 0, 2475755792, 0, 7354489, 0, 5050, 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, 0], [-102497988859192320, 0, -1845042240099528, 0, -6581721953521, 0, -4834130050, 0, -1003849, 0], [325693833756348416, 0, -1011447157780712, 0, -16755790470373, 0, -14052901482, 0, -3036701, 0], [-5761655028472529920, 0, -143692636451786888, 0, -584839838800809, 0, -413005498610, 0, -80563009, 0], [2932743674976963584, 0, 196466494243753720, 0, 700898949271623, 0, 484279228654, 0, 94415663, 0], [0, 48500090563401536, 0, -1289835989850272, 0, -1584249446409, 0, -758922410, 0, -128881], [0, 208340494858277824, 0, 1289835989850272, 0, 1584249446409, 0, 758922410, 0, 128881], [0, 1269965551955694627328, 0, 6337401708404491000, 0, 19467457749725199, 0, 13702415612990, 0, 2748818487], [0, -6054794255099484799232, 0, -143486961555925440376, 0, -565091498731173747, 0, -412231451711590, 0, -83926954043], [0, -14636876727188374780928, 0, -213332869018261197448, 0, -699063050265817545, 0, -489205973118514, 0, -97505079905]];
    Rf_basisdens := [1, 486640529977344, 1155771258696192, 27738510208708608, 27738510208708608, 256840585421679360, 128420292710839680, 5798889661965027328, 65237508697106557440, 52190006957685245952];
    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_38_b();"
function MakeCharacter_38_b()
    N := 38;
    order := 2;
    char_gens := [21];
    v := [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_38_b_Hecke();"
function MakeCharacter_38_b_Hecke(Kf)
    N := 38;
    order := 2;
    char_gens := [21];
    char_values := [[-1, 0, 0, 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 := 7;
    raw_aps := [[0, 0, 0, 0, 0, 0, -1, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1, 0, 0, 0], [-11, -1, 0, 0, 0, 0, 0, 0, 0, 0], [-23, 2, 1, 1, 0, 0, 0, 0, 0, 0], [364, 6, 6, -4, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 7, -19, -1, 3, 3], [-1048, 18, 1, 12, -15, 0, 0, 0, 0, 0], [-1719, -20, 15, 0, -4, -7, 72, -3, -7, 4], [3785, -98, 7, 29, 13, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 216, 1497, 15, -36, 9], [0, 0, 0, 0, 0, 315, 370, 10, 55, -64], [0, 0, 0, 0, 0, 1247, -320, 16, -39, 120], [0, 0, 0, 0, 0, 843, -998, -14, -77, -112], [550, -12, -40, 416, 55, 0, 0, 0, 0, 0], [32100, 464, 146, 146, 169, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 24, -3061, -97, 74, -83], [0, 0, 0, 0, 0, 1458, 11195, -64, 11, 358], [42425, -463, 20, 1490, 140, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 6373, 7785, -54, 274, -478], [0, 0, 0, 0, 0, 66, 1214, -10, -754, 94], [-78458, 1134, -1129, -1882, 595, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 395, -62530, 128, 325, -226], [-9862, 1798, -974, -3238, -752, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 5667, 39092, -316, 11, -470], [0, 0, 0, 0, 0, -11734, -66262, 356, -488, -376], [-611590, 3136, 700, 2000, -560, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 29812, 10512, 342, 394, -1318], [0, 0, 0, 0, 0, -20976, 79619, 248, 1865, 130], [0, 0, 0, 0, 0, -12306, -111323, -113, 1630, -331], [0, 0, 0, 0, 0, -16863, 87810, -558, -2163, 648], [0, 0, 0, 0, 0, -13173, 108824, 554, 1325, -584], [-1188474, -13984, 4004, 3524, 2023, 0, 0, 0, 0, 0], [951810, -3742, 2819, -3778, -689, 0, 0, 0, 0, 0], [-2442768, -7930, -4326, 11184, 3637, 0, 0, 0, 0, 0], [-1668161, -6747, -954, -7684, 2326, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 36170, 140910, 1992, 2540, -2024], [1698510, -14950, -9240, -13686, -1700, 0, 0, 0, 0, 0], [1175340, -19336, -6672, 5970, 14902, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 60585, 195070, -2264, 7927, -4042], [0, 0, 0, 0, 0, 97332, -514244, -2882, -3692, 3368], [0, 0, 0, 0, 0, -41349, 358548, 1272, -11475, 3696], [0, 0, 0, 0, 0, 93044, -900736, 734, -3188, -5572], [1075787, 5426, -3989, -3941, -16354, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, -595, 82706, -2884, 5383, -1606], [3365714, 24034, 28024, 41126, 12088, 0, 0, 0, 0, 0], [373561, 19584, 24569, -20749, 7720, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, -6328, -1234779, 3666, -2155, 14272], [0, 0, 0, 0, 0, -136401, -1234720, -340, -5065, -17774], [0, 0, 0, 0, 0, -81840, 982145, -3508, -14377, -5006], [2785357, -103063, 17212, 9934, 22704, 0, 0, 0, 0, 0], [-5315825, 69171, -21942, -32170, -578, 0, 0, 0, 0, 0], [3443411, -33988, -32417, 48217, 10334, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, -138185, 2145162, 4134, 10927, -17392], [1748172, 17676, -57562, -57002, 26249, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 301314, -1952976, -2106, -20316, 3690], [12939516, 84660, 8918, 4438, 14945, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, -547347, -492912, 7812, -7041, 7428], [-10624357, 11010, 19857, 17715, 44547, 0, 0, 0, 0, 0], [-5948189, -301, 119686, -45662, -25158, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 426270, 1637500, -8552, 12070, -6856], [3152022, 9348, -7052, -24512, -85177, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 281667, -1506541, -3517, -5155, 11587], [0, 0, 0, 0, 0, -338721, -35116, -18364, -7435, 5956], [-12935303, 34088, -116871, -62997, -16368, 0, 0, 0, 0, 0], [6005505, 142079, 33801, -37308, -46091, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 266397, 4955129, -7699, 29111, 12757], [0, 0, 0, 0, 0, 157195, 300091, 13606, -56494, 12526], [0, 0, 0, 0, 0, 377356, -1186940, -18782, -70090, 47242], [-28993982, -36838, 39588, 123750, 35571, 0, 0, 0, 0, 0], [-32042261, -45171, 51002, -62560, 30346, 0, 0, 0, 0, 0], [-9280813, 197167, -20129, -658, 27115, 0, 0, 0, 0, 0], [777625, -495634, -48627, 164517, 56472, 0, 0, 0, 0, 0], [21715892, 73686, 32304, 243060, -69258, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 575369, 4956783, 1467, 55943, -27209], [0, 0, 0, 0, 0, 277352, 6572475, -3822, 9719, 69412], [0, 0, 0, 0, 0, 924933, 4633184, 17180, -2827, 17710], [1524879, 106935, 168290, -35558, -63634, 0, 0, 0, 0, 0], [14010719, 255903, 210678, 282738, 58494, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, -854619, -6010524, -366, -13437, 20328], [0, 0, 0, 0, 0, 644783, -2923158, -68316, -15187, -5270], [31010162, 304282, 153322, 248894, -65192, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, -1156382, 985031, 21263, -13248, -35967], [0, 0, 0, 0, 0, 2147472, 5015320, 17308, 45844, -95644], [0, 0, 0, 0, 0, -347697, 3336756, -8676, -59205, 19014], [0, 0, 0, 0, 0, -133761, -2925716, 44890, 51097, 91028], [-17719232, 173354, 318750, -366180, 192429, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, -1304700, -6528454, -30874, 66536, 81406], [33528470, 627414, -244173, -94482, -210597, 0, 0, 0, 0, 0], [-6843139, 126849, -94664, -3114, 199296, 0, 0, 0, 0, 0], [16770672, 86004, 244138, 257578, 294533, 0, 0, 0, 0, 0], [-22677760, -394908, -150530, -478638, -113955, 0, 0, 0, 0, 0], [46125512, -324362, -86924, -342664, -96590, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, -2010934, -1582192, 20486, -117512, -59242], [27834482, 329728, 84778, 677288, 96904, 0, 0, 0, 0, 0], [-18599386, -374356, -267016, 282392, 175943, 0, 0, 0, 0, 0], [-15947815, 366568, -183857, 691139, 35677, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, -1828542, -8237460, 31182, 58626, -213036], [0, 0, 0, 0, 0, -378363, 143690, -29854, -233851, 253084], [0, 0, 0, 0, 0, -1207148, 19656099, 88014, 35095, -105040], [34223165, -1938609, -158340, 196776, 179400, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, -1881896, -7045944, -57870, 221656, -1468], [-101292079, 738635, 294808, 463696, 106100, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, -1479381, -10843052, -55916, 48673, 237404], [0, 0, 0, 0, 0, 4532874, 870558, 33372, -217824, -112692], [140119460, -72834, -241296, 520404, -83154, 0, 0, 0, 0, 0], [-15016184, -92878, -237395, 710092, -157767, 0, 0, 0, 0, 0], [-70023702, -526146, -414796, 597226, 273875, 0, 0, 0, 0, 0], [110293826, 64468, -170156, -771796, 144544, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 7236369, 22934060, -103894, 395843, 121480], [0, 0, 0, 0, 0, 5524074, -15422766, 25362, -94338, -219174], [0, 0, 0, 0, 0, -353854, -14547614, 19822, -406194, 123594], [-148856119, -265693, -331708, 138656, -178324, 0, 0, 0, 0, 0], [-41486257, 202427, 551626, -469490, -181162, 0, 0, 0, 0, 0], [-117932526, 812948, 522642, -934476, 49960, 0, 0, 0, 0, 0], [69492212, 1871636, 506486, 6446, -120415, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, -3371370, 17972136, 98670, 503256, -205590], [-62952044, 683016, -602326, 521810, 46609, 0, 0, 0, 0, 0], [221662765, -1261224, -437127, 155999, 337012, 0, 0, 0, 0, 0], [-222891361, -2075581, 352018, -903122, 38174, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, -561060, -23067907, -38638, -153007, 326536], [0, 0, 0, 0, 0, 2623693, 12822727, 42655, -588097, 607411], [0, 0, 0, 0, 0, 321010, 34842670, -132908, 261908, 28216], [0, 0, 0, 0, 0, 5032176, -45650393, 84025, 5836, 184775], [0, 0, 0, 0, 0, -2093322, -19229256, -121110, 56802, -197676], [-11540974, 186954, -262416, 299958, -46221, 0, 0, 0, 0, 0], [31446440, -2372162, 1024942, 1112984, 308218, 0, 0, 0, 0, 0], [11581184, 2309934, 1282038, 42672, 275250, 0, 0, 0, 0, 0], [130790247, -486066, -759037, -2325653, 180818, 0, 0, 0, 0, 0], [-296002327, -1951068, -570099, -694593, -329712, 0, 0, 0, 0, 0], [11744012, 1437624, 54954, -2047662, 441702, 0, 0, 0, 0, 0], [30428670, 844130, 602280, 2054298, -371849, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, -569025, 25299190, -109526, 42187, 682340], [0, 0, 0, 0, 0, 5248658, 40671310, 46498, 128526, 362694], [108991473, 1347785, 44736, 178146, -431012, 0, 0, 0, 0, 0], [-81950670, -410334, 506591, 2184238, -373921, 0, 0, 0, 0, 0], [169727272, -3351724, -408047, -1298678, 289113, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, -3418359, -18157977, 205683, -156657, 230391], [0, 0, 0, 0, 0, 5418874, 20004005, -121612, 510525, -200538], [0, 0, 0, 0, 0, 2396304, -52525135, 266831, 577100, 376441], [-53556762, -2220336, -640705, -2587580, 80435, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 8637702, -35131913, 82042, 721561, 108224], [121831667, -4117561, -450758, -2196386, -319822, 0, 0, 0, 0, 0], [299098149, -1116910, -234651, -3289443, -843572, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, -1460049, -7873969, 69956, -144700, -619364], [0, 0, 0, 0, 0, -3391111, 13428637, -16031, 153075, -458799], [0, 0, 0, 0, 0, 6212142, 20884824, 148482, -124632, -3414], [315968060, -272472, 762378, 2469186, 479622, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 6727116, -13251736, -369136, 928928, -232172], [-351804874, -370300, 226556, 233504, -1189369, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 554853, -65107920, 86838, 1009083, -590952], [0, 0, 0, 0, 0, -3843665, -26246487, -120321, -951785, 326291], [-277893789, -2320165, 868238, 3358658, 926770, 0, 0, 0, 0, 0], [-45132026, -235366, 1869796, -1535798, -1330929, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 7249656, 64520328, -193188, -1167192, -614496], [0, 0, 0, 0, 0, -11534783, -39842485, 305432, 36942, 1510812], [0, 0, 0, 0, 0, -1701996, 27366136, -382706, 107494, 653486], [-14027067, -7655176, 821187, 2647215, 488641, 0, 0, 0, 0, 0], [-124341613, -5992379, -2762513, 788600, 106315, 0, 0, 0, 0, 0], [-60819562, 1031832, -1693713, -3847956, 78267, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, -20423013, 14478485, -459781, -962941, 1026199], [282136358, -3648098, -1985126, 2461406, -397580, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, -6778353, -12980408, 290164, 214843, 836018], [-12752604, 7068578, 720516, -1434360, -63818, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 28257948, -60812744, -15878, -886928, 221024], [0, 0, 0, 0, 0, -27858765, -56536010, 192892, -1062767, -816730], [0, 0, 0, 0, 0, -1801470, -86579164, -454936, -1193686, 420352], [0, 0, 0, 0, 0, 12446315, -108381728, 92152, 2028567, 695238], [143714331, -2198509, -3501282, 2729562, 1046050, 0, 0, 0, 0, 0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_38_b_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_38_7_b_a();".
// 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_38_7_b_a(:prec:=10)
    chi := MakeCharacter_38_b();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 7));
    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_38_7_b_a();".
// 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_38_7_b_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_38_b();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,7,sign)));
    Vf := Kernel([],Snew);
    return Vf;
end function;