
// Make newform 12.5.d.a in Magma, downloaded from the LMFDB on 20 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_12_d();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_12_d_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_12_5_d_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_12_5_d_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 := [9, 3, 4, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 2, 0, 0], [3, -4, 4, -1], [5, 2, 0, 1]];
    Rf_basisdens := [1, 1, 2, 1];
    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_12_d();"
function MakeCharacter_12_d()
    N := 12;
    order := 2;
    char_gens := [7, 5];
    v := [1, 2];
    // 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_12_d_Hecke();"
function MakeCharacter_12_d_Hecke(Kf)
    N := 12;
    order := 2;
    char_gens := [7, 5];
    char_values := [[-1, 0, 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 := 5;
    raw_aps := [[2, -1, 0, 0], [0, 0, -1, 0], [6, 4, 0, -4], [-8, 12, -4, 4], [16, -24, 12, -8], [74, -24, 0, 24], [-150, -8, 0, 8], [-48, 72, 36, 24], [-16, 24, -96, 8], [222, 28, 0, -28], [56, -84, -140, -28], [-1102, -48, 0, 48], [138, -56, 0, 56], [304, -456, 332, -152], [16, -24, -216, -8], [1278, -68, 0, 68], [-448, 672, -108, 224], [1058, 480, 0, -480], [-448, 672, 316, 224], [432, -648, -240, -216], [2210, -1056, 0, 1056], [-296, 444, 980, 148], [1552, -2328, 660, -776], [-6270, 784, 0, -784], [5762, 1104, 0, -1104], [6942, 1084, 0, -1084], [392, -588, 244, -196], [-672, 1008, -3084, 336], [-8806, 600, 0, -600], [-3342, -2480, 0, 2480], [1704, -2556, 3516, -852], [-3712, 5568, 60, 1856], [-25446, 856, 0, -856], [896, -1344, -3260, -448], [37734, 868, 0, -868], [-3272, 4908, -4756, 1636], [5042, 336, 0, -336], [-3216, 4824, 4164, 1608], [-3904, 5856, -5976, 1952], [-14394, 1828, 0, -1828], [1728, -2592, 6636, -864], [8858, 1368, 0, -1368], [3520, -5280, -1728, -1760], [2594, -3552, 0, 3552], [-1842, -5588, 0, 5588], [5928, -8892, -4092, -2964], [704, -1056, 8332, -352], [-3128, 4692, -6580, 1564], [-2384, 3576, 5748, 1192], [-27862, 6312, 0, -6312], [8994, 5968, 0, -5968], [-1504, 2256, 8064, 752], [-37966, -2544, 0, 2544], [912, -1368, -900, -456], [32514, -6656, 0, 6656], [4096, -6144, 3144, -2048], [-55266, -3716, 0, 3716], [-3000, 4500, 9852, 1500], [17786, -1128, 0, 1128], [-20334, -9728, 0, 9728], [-9472, 14208, -5900, 4736], [-50178, 11164, 0, -11164], [-14016, 21024, -14676, 7008], [16352, -24528, 11208, -8176], [101138, 8544, 0, -8544], [-78258, 1900, 0, -1900], [-1184, 1776, 404, 592], [-101710, -14880, 0, 14880], [26576, -39864, 9756, -13288], [-4030, 12768, 0, -12768], [196338, -1328, 0, 1328], [-11856, 17784, 25392, 5928], [26296, -39444, 19844, -13148], [51650, 10752, 0, -10752], [24336, -36504, 7308, -12168], [2816, -4224, -45984, -1408], [127686, -17852, 0, 17852], [62402, 16512, 0, -16512], [77658, 9256, 0, -9256], [-143854, 20208, 0, -20208], [3440, -5160, -16428, -1720], [-30694, -6504, 0, 6504], [20976, -31464, 2040, -10488], [45266, -12000, 0, 12000], [24328, -36492, 7556, -12164], [-176, 264, 48060, 88], [105930, -19112, 0, 19112], [88178, -35376, 0, 35376], [-143034, 1732, 0, -1732], [-47608, 71412, -20516, 23804], [10992, -16488, 6084, -5496], [-14864, 22296, -53688, 7432], [-32712, 49068, 10236, 16356], [9440, -14160, 25620, -4720], [25088, -37632, -14612, -12544], [-10128, 15192, -39456, 5064], [-91986, -33716, 0, 33716], [-250038, -9752, 0, 9752], [8208, -12312, 51612, -4104], [-43078, 33144, 0, -33144], [-18032, 27048, -41356, 9016], [18102, 18676, 0, -18676], [-19696, 29544, -5484, 9848], [-287670, -9080, 0, 9080], [-10144, 15216, -13724, 5072], [-185710, 20832, 0, -20832], [63520, -95280, 21204, -31760], [-92814, -14768, 0, 14768], [-57712, 86568, 26256, 28856], [328226, -21696, 0, 21696], [-7576, 11364, -50948, 3788], [-41470, 15840, 0, -15840], [-118542, 70240, 0, -70240], [24640, -36960, -92380, -12320], [-67272, 100908, -29556, 33636], [-255414, 65656, 0, -65656], [-10288, 15432, 47668, 5144], [9456, -14184, 46608, -4728], [-22506, -91820, 0, 91820], [16672, -25008, -86148, -8336], [93746, 32016, 0, -32016], [443570, -55104, 0, 55104], [54390, -21164, 0, 21164], [55728, -83592, -1956, -27864], [35056, -52584, 90452, -17528], [16878, 67564, 0, -67564], [-413302, 35112, 0, -35112], [-29424, 44136, 131688, 14712], [-31688, 47532, -114004, 15844], [31274, -47352, 0, 47352], [-122016, 183024, -79188, 61008], [-51808, 77712, -62280, 25904], [54296, -81444, 72148, -27148], [-72214, -792, 0, 792], [329082, -12200, 0, 12200], [-544894, -36096, 0, 36096], [560622, -17972, 0, 17972], [96368, -144552, 80980, -48184], [59766, -72140, 0, 72140], [-341286, -38888, 0, 38888], [6192, -9288, 14508, -3096], [-192306, -100628, 0, 100628], [40088, -60132, -175076, -20044], [-81888, 122832, -76764, 40944], [-429190, -3816, 0, 3816], [66256, -99384, 25152, -33128], [-24334, 98160, 0, -98160], [157002, -37880, 0, 37880], [-25648, 38472, -72020, 12824], [-27840, 41760, -168096, 13920], [997346, 50304, 0, -50304], [644706, 46048, 0, -46048], [-120144, 180216, 64116, 60072], [126848, -190272, -75912, -63424], [-34928, 52392, 18236, 17464], [111712, -167568, 183552, -55856], [-91752, 137628, 18876, 45876], [671802, 76168, 0, -76168], [128498, -50880, 0, 50880], [-1160922, -2588, 0, 2588], [-113216, 169824, -3156, 56608], [732234, 137512, 0, -137512], [30920, -46380, 173044, -15460], [124656, -186984, 114396, -62328], [-169926, -175736, 0, 175736], [-268736, 403104, -96264, 134368], [140376, -210564, 78180, -70188], [-1125598, -960, 0, 960]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_12_d_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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