
// Make newform 38.10.a.d in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_38_a();"
// 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_10_a_d();".
// 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_10_a_d();".
// 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 := [-48270488, -2723303, -34433, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [-5880008, -252737, -736, 9], [30780440, 698613, 244, -17], [-18350856, -413779, -490, 13]];
    Rf_basisdens := [1, 20632, 20632, 10316];
    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_a();"
function MakeCharacter_38_a()
    N := 38;
    order := 1;
    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;

function MakeCharacter_38_a_Hecke(Kf)
    return MakeCharacter_38_a();
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 := 10;
    raw_aps := [[-16, 0, 0, 0], [21, 1, 0, 0], [-350, -5, -1, -4], [3075, 27, 2, -9], [-26056, -3, 25, 0], [30071, 766, -33, -169], [-103123, -276, -587, 234], [130321, 0, 0, 0], [752981, -678, 1531, 93], [1537183, -1436, -1027, -3481], [3192456, 9482, -2050, -2150], [5128462, -40262, -6770, 14570], [2893200, -11750, -19020, -28480], [1937238, -59233, 24965, 25660], [-7894898, 45347, -31111, 32602], [18108791, 259234, -35357, -78901], [-37327107, -25343, -66072, -8236], [32233368, -7739, -115309, 44408], [33192661, -873165, 156322, 19056], [-11884254, 50978, 8844, -407378], [-9921749, -1030550, -184019, -170182], [-76710310, -1674604, 293190, -123590], [-186639194, -980280, -92806, 104262], [72054508, -224494, 201740, 1072810], [197503898, -3887418, -963354, -2541012], [491365346, 3423196, 922096, 1059368], [301262866, 900876, -1594406, 273372], [227298043, 3265539, -629356, 313752], [-551394775, -5567322, 515073, -1081671], [-1694311634, -6228400, 1800740, 2531560], [-1415706382, -3370762, 3818232, -664634], [-2051926390, 15697493, -2038265, 3277840], [163598345, -3992284, -3495887, -2252186], [1311466170, -1428269, 3949613, 9599264], [-1498856998, 36814303, -25001, -5558338], [-868922830, -109600, -2343690, 10801840], [3713146638, -27733258, -2536030, 1975930], [2070893992, -22669030, -3440126, 3672322], [2587248312, -64871718, 1999050, 11517000], [-215897998, -41011762, 9714142, 8008606], [-8387782292, 58085426, -825214, -9615452], [9146257862, -25026746, 3638250, 32493950], [-7022312089, -5965425, -164894, 16196523], [16950065182, -50803804, -2995124, -11000162], [42966280, -29961646, 15717440, 11675560], [-8436684505, -57660261, -30590562, -27553881], [-3439396375, 15482789, -28902720, 34648720], [19212545166, 169502528, 54602090, 39914860], [-13809654279, -102000109, 8588502, 19466836], [10656110848, 49319179, 10613425, 5533700], [-9849701816, -101212589, -43503055, -69107770], [-18198952889, 161264803, 27464294, -9189493], [28702041624, -34217526, -49969108, -93061284], [7558951636, -203062751, -65615755, 34163630], [30355130572, -113194058, -59713960, 60467210], [-681596746, 199719227, 92520985, 65967790], [90548450530, 140003136, -45225448, -76864614], [-62786388691, 376867554, -6360189, -133459827], [5581653206, -11747455, -119713479, -56055272], [-22513495936, -264986046, 72992788, 45271524], [47440123640, -299199115, -62450991, 142558792], [-56560249207, -242745506, -154009607, 121217189], [159497240828, -113240204, -63714924, 109610588], [-119646666781, -177630735, 82805920, -166455885], [-62619903547, 467390005, 133006314, 215854592], [96817374689, 78152456, 126825691, 196037233], [-102796378201, -263216747, 80340450, -263594740], [32851432404, 393172258, 8667956, -83641402], [-47457758112, -1042808363, 205467165, 458509310], [181688146084, 1303885651, 76868413, -154485316], [-77546925735, 471411729, 287338050, -40212660], [172545685933, -820126649, -117537616, 393884297], [25324387956, 242988196, -144876832, 491616944], [110396724797, -1961715114, -9526347, -116316351], [142877099877, -3132098147, -50903572, 579595964], [185877070218, 274663922, 272052600, 369399400], [-155725361600, 273407655, 152224409, -168953058], [-420099288294, -978178519, -11509723, -920897984], [-510803504956, 2335240388, 115486498, 76434544], [-140154169696, -2180292470, -68000856, -993489298], [166085418736, -4384533314, 202127126, 618894998], [-221830534477, 84969842, 45326007, 292272211], [-475085462326, -3189821756, -28320074, -184698382], [-606966846114, 766100750, 444935798, -310723586], [-99492637312, 4239287066, 617683122, 1248072376], [-86592442352, 351977011, -1001816473, -1637320744], [360467958902, -2313065008, -82009760, -104663090], [113849039623, 497424140, 323574307, 1813808986], [708322293870, -5720733, -452979081, -292250718], [-494233953910, -2272792697, 941345193, 1265302694], [-21738861288, 1240984505, 371417433, 1656795874], [333341740620, -8146248528, -197090172, 1679862624], [-14334933994, -3238001376, -930587742, -1056700416], [807861920908, 3105983464, -726638152, -2070255256], [1028293972550, -6076121079, -2990697, -848292036], [1835397455525, -2245116714, -448985033, -1529513259], [-923324337898, -4972113116, -280032536, 379432802], [-438422801296, 2626086456, 594143530, 3464452620], [814975978935, -8839742541, -58207792, 264285204], [-1399498927976, -2642005453, -1607358371, 1263526642], [-641230727868, 10215905188, 338794796, 885012668], [278715181630, 15200261477, 1284252917, -2944353164], [1317703512256, -1375422722, 3115774826, 168955628], [478250955028, 5009895544, -3209196706, 3012729512], [463174211044, 14549667332, -2017425644, -6129851372], [-3059248638775, -3625903450, -278242917, -4392525386], [-809244624166, -5984559993, -845884163, -2339551374], [201309798874, 20086938524, -1189107292, -5588364536], [2761661431656, -12834645856, -972703800, 3261118180], [-1275096297072, 5976527444, -1617551758, -5480550434], [2530538099430, 9688655596, 5448792074, -320286538], [1022342248892, -10003936491, -2175431445, -948303450], [2833290594468, 5607554601, -2100090597, -3146466666], [-2861248617018, -9531188586, 2536513964, 3087743112], [-2845386912854, -3773865223, -1819040981, -5811368438], [-2537374193202, 2596428712, 1732263744, -9373467778], [1830670545044, 23207649035, -5573641593, -5884702874], [2037049357543, -16971067517, -4195241098, -3309472369], [-984262749650, -2598063971, 4500747797, 8484438296], [1190811057847, -28359125905, 3032397284, -6126284228], [2975839878161, 24060532406, -2403402447, -3854264471], [-1971611268724, 23560492686, -5376660756, 2758018332], [357192587609, -29631952096, -869032301, 4057514137], [-179171644996, 19647963380, -1877211044, -10079278412], [-4985100817252, -1119380683, -3699841719, 2097096598], [-484140996796, -33752004090, 5487649624, 4761738612], [2347265680034, 11701385382, 3209298918, 2793953994], [-3980142145461, -13144063013, 3520706130, -579723025], [-893794040005, -21336802443, -10214244156, 3497798007], [-2912709294172, 52694977928, -4547127978, -3932869634], [241410499498, -18620858267, 4832141803, -7201465306], [720144918984, 29693347986, 10091577750, 9469690230], [-110656286264, 20548782304, 4943459512, 11815469096], [5686855391988, -64979207831, 1873695107, 6188832896], [-2101320057651, 8685048968, -838759815, 16162017130], [1429855814675, 7398348556, -14164850625, 2857121810], [3701666208493, -46723887860, -7796263261, -20857151443], [12483618184473, -54396752565, 2201380238, -2001127236], [1833927400027, -29627389806, 6652463087, -10071236529], [-6588462723257, 17594143240, 9718983983, 1424096594], [10416865445651, -50013253831, 5744039010, -1635904520], [1739727982974, 10425685233, 4653146553, -3663390996], [-817155405823, -28581896557, -8402492328, -12048174119], [4166587793297, 16767036479, 17320406458, 8034440104], [10859462920409, 82035347022, 23508255561, 2507601753], [2527553667320, 5455428020, -6405037184, 28661594578], [9960305488282, 34276785192, 3328636036, 765319248], [-376090838044, 19323377798, -3267094244, -33734829392], [4632914378812, 1893524257, -9564599759, 14705569388], [-277507460018, 56989261074, -11111394520, -23155964130], [-1799716568111, 16961807312, 263324827, -4998466379], [-1048076207752, 76178918285, -2911973, 843848386], [8186045754488, -65739786545, 7229501919, 23775900962], [-2534693381194, -43286613694, -8210647064, -21123673682], [-15914123086549, -29689128595, -31689604554, -2807073092], [-4147595286720, -46746519984, 4186916244, -28281177498], [17481067244975, -6355521442, -9894672471, -16943293613], [-7829699400671, -32896737731, 31113456662, -21086269564], [-6327519136133, -57189920206, 29518985653, 25576252354], [1109737666171, 117381389984, -27525362755, -22297825505], [-4423144614164, 182367166706, 9275865038, -14291059106], [-2300132318394, 103174813560, -8431028688, 35794053486], [21740159878288, 22529609568, -11829844568, -24204645144], [-2414728624948, -99416190496, -23644531520, 19829495920], [7088313327796, 155019075022, 34169868536, 38935574618], [11546508861238, 30185008600, 17478926606, -21384727282], [-26447099066628, -52698067512, -8706966016, -1025574618], [-4913287305394, -122282968899, 4806118941, 7187530368]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_38_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_38_10_a_d();".
// 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_10_a_d(:prec:=4)
    chi := MakeCharacter_38_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 10));
    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_10_a_d();".
// 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_10_a_d( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_38_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,10,sign)));
    Vf := Kernel([<3,R![62650008, -1329018, -44107, -84, 1]>],Snew);
    return Vf;
end function;