
// Make newform 12.8.b.a in Magma, downloaded from the LMFDB on 26 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_12_b();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_12_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_12_8_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_12_8_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 := [4, 0, 1, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, -6, 0, -2], [3, 3, 6, 1], [0, 6, 0, -2]];
    Rf_basisdens := [1, 1, 1, 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_b();"
function MakeCharacter_12_b()
    N := 12;
    order := 2;
    char_gens := [7, 5];
    v := [1, 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_12_b_Hecke();"
function MakeCharacter_12_b_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 := 8;
    raw_aps := [[0, 0, 0, 1], [0, 0, -3, 3], [0, 23, 0, 0], [0, 43, 86, 0], [0, -95, 0, -190], [-12730, 0, 0, 0], [0, 2108, 0, 0], [0, 1029, 2058, 0], [0, 3482, 0, 6964], [0, -11863, 0, 0], [0, -3889, -7778, 0], [43310, 0, 0, 0], [0, 87854, 0, 0], [0, -3047, -6094, 0], [0, 27124, 0, 54248], [0, -39013, 0, 0], [0, -36475, 0, -72950], [314198, 0, 0, 0], [0, -34915, -69830, 0], [0, 12750, 0, 25500], [-259270, 0, 0, 0], [0, 225271, 450542, 0], [0, -486017, 0, -972034], [0, 434306, 0, 0], [7243010, 0, 0, 0], [0, -1253953, 0, 0], [0, 498395, 996790, 0], [0, 10521, 0, 21042], [9807014, 0, 0, 0], [0, -605452, 0, 0], [0, 712191, 1424382, 0], [0, 122435, 0, 244870], [0, 2846870, 0, 0], [0, 1714553, 3429106, 0], [0, 276899, 0, 0], [0, -2440733, -4881466, 0], [-25423690, 0, 0, 0], [0, -1739619, -3479238, 0], [0, -2553514, 0, -5107028], [0, -872875, 0, 0], [0, 3619095, 0, 7238190], [36308702, 0, 0, 0], [0, -6232400, 0, -12464800], [-119988670, 0, 0, 0], [0, 4259207, 0, 0], [0, -4342773, -8685546, 0], [0, 8703029, 17406058, 0], [0, 1993903, 3987806, 0], [0, 7339099, 0, 14678198], [-43912786, 0, 0, 0], [0, 27121070, 0, 0], [0, 6457220, 0, 12914440], [419220242, 0, 0, 0], [0, 21388485, 0, 42776970], [0, -3633376, 0, 0], [0, -20512802, 0, -41025604], [0, 33581405, 0, 0], [0, 7256151, 14512302, 0], [-420256450, 0, 0, 0], [0, -57014542, 0, 0], [0, -665455, -1330910, 0], [0, 88680911, 0, 0], [0, 21846597, 43693194, 0], [0, -13432270, 0, -26864540], [-69283990, 0, 0, 0], [0, -100339615, 0, 0], [0, -51268007, -102536014, 0], [1155949970, 0, 0, 0], [0, -63351379, 0, -126702758], [-279114634, 0, 0, 0], [0, 71510984, 0, 0], [0, 33978630, 0, 67957260], [0, 65497063, 130994126, 0], [-419199970, 0, 0, 0], [0, -79200495, -158400990, 0], [0, 9037568, 0, 18075136], [0, -36045577, 0, 0], [-1346031610, 0, 0, 0], [0, -97619332, 0, 0], [1619129066, 0, 0, 0], [0, 88120715, 0, 176241430], [1299729518, 0, 0, 0], [0, 108399060, 0, 216798120], [-338793550, 0, 0, 0], [0, -74273645, -148547290, 0], [0, -83514443, 0, -167028886], [0, -107031040, 0, 0], [-2227509190, 0, 0, 0], [0, 276732557, 0, 0], [0, -106738921, -213477842, 0], [0, 51747519, 0, 103495038], [0, -14871800, 0, -29743600], [0, -90613989, -181227978, 0], [0, -28598695, 0, -57197390], [0, 146278949, 292557898, 0], [0, -193605438, 0, -387210876], [0, 132101105, 0, 0], [0, -396250954, 0, 0], [0, -124357143, -248714286, 0], [-459048202, 0, 0, 0], [0, 2957533, 5915066, 0], [0, 191380565, 0, 0], [0, 62187047, 0, 124374094], [0, 617493338, 0, 0], [0, 266510465, 533020930, 0], [-2630977630, 0, 0, 0], [0, 32807329, 0, 65614658], [0, 99563708, 0, 0], [0, 418090250, 0, 836180500], [-6487688278, 0, 0, 0], [0, 330087695, 660175390, 0], [10198563950, 0, 0, 0], [0, -857122258, 0, 0], [0, -332326007, -664652014, 0], [0, -166327725, -332655450, 0], [0, 351068336, 0, 0], [0, -332691091, -665382182, 0], [0, -105498594, 0, -210997188], [0, -721683427, 0, 0], [0, 357822175, 0, 715644350], [2660916542, 0, 0, 0], [9424989890, 0, 0, 0], [0, 1212762671, 0, 0], [0, -571341543, 0, -1142683086], [0, 40882549, 81765098, 0], [0, -856550815, 0, 0], [-6075761266, 0, 0, 0], [0, -723733140, 0, -1447466280], [0, 404309539, 808619078, 0], [7718559350, 0, 0, 0], [0, -17057523, -34115046, 0], [0, 234046598, 0, 468093196], [0, -375007417, -750014834, 0], [16797190430, 0, 0, 0], [0, -54597622, 0, 0], [9733302914, 0, 0, 0], [0, -2348260873, 0, 0], [0, -980630011, -1961260022, 0], [0, 1370449193, 0, 0], [0, 435963566, 0, 0], [0, 423400473, 846800946, 0], [0, 1968415163, 0, 0], [0, 687192755, 1374385510, 0], [0, 667277349, 0, 1334554698], [-30308324074, 0, 0, 0], [0, 847530190, 0, 1695060380], [-7774515010, 0, 0, 0], [0, 381186674, 0, 0], [0, -43826335, -87652670, 0], [0, -1076096952, 0, -2152193904], [-7428602650, 0, 0, 0], [0, -3166900108, 0, 0], [0, 930988821, 1861977642, 0], [0, 119771234, 0, 239542468], [0, 25957417, 51914834, 0], [0, -1293895460, 0, -2587790920], [0, 619724547, 1239449094, 0], [0, -931684024, 0, 0], [22445795450, 0, 0, 0], [0, -2960925835, 0, 0], [0, -1089267209, 0, -2178534418], [0, 5229563834, 0, 0], [0, -717476821, -1434953642, 0], [0, -74788335, 0, -149576670], [0, 862075148, 0, 0], [0, -2521269878, 0, -5042539756], [0, 259153743, 518307486, 0], [9885655790, 0, 0, 0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_12_b_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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