
// Make newform 12.9.c.b in Magma, downloaded from the LMFDB on 16 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_12_c();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_12_c_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_9_c_b();".
// 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_9_c_b();".
// 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 := [110, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0], [0, 6]];
    Rf_basisdens := [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_c();"
function MakeCharacter_12_c()
    N := 12;
    order := 2;
    char_gens := [7, 5];
    v := [2, 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_c_Hecke();"
function MakeCharacter_12_c_Hecke(Kf)
    N := 12;
    order := 2;
    char_gens := [7, 5];
    char_values := [[1, 0], [-1, 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 := 9;
    raw_aps := [[0, 0], [-51, 1], [0, -18], [-3094, 0], [0, -18], [-7294, 0], [0, 936], [-80326, 0], [0, -1548], [0, -13734], [435914, 0], [1159298, 0], [0, -43164], [990266, 0], [0, -106488], [0, 160038], [0, 25398], [19369154, 0], [-28024294, 0], [0, -534852], [-25230142, 0], [-63401398, 0], [0, 755514], [0, -1244196], [19550306, 0], [0, 697374], [113497898, 0], [0, -873954], [1743554, 0], [0, -3090312], [-177120502, 0], [0, 2083842], [0, -2802348], [-232284838, 0], [0, -9301482], [-417368086, 0], [818571266, 0], [318428666, 0], [0, 18440460], [0, 22509954], [0, -24921558], [-619722046, 0], [0, 7750368], [1345770626, 0], [0, -25434738], [-1418596246, 0], [2704590554, 0], [1691150666, 0], [0, 10091250], [79063874, 0], [0, 90904932], [0, -74853720], [1118570114, 0], [0, 31291254], [0, 29757888], [0, -79913124], [0, 24975090], [4273738442, 0], [-2436193534, 0], [0, 93488508], [5677320986, 0], [0, -57041154], [-14398779814, 0], [0, 8137188], [-9705276574, 0], [0, -67771926], [-5352454438, 0], [-5996980894, 0], [0, -381008826], [28040768642, 0], [0, -119298384], [0, -273283092], [16639713866, 0], [-10003569214, 0], [37800970874, 0], [0, -329285376], [0, 213166638], [8795997506, 0], [0, -572546520], [9781407554, 0], [0, 449408466], [-19409647678, 0], [0, -94462776], [-23220552094, 0], [30986199914, 0], [0, 60368598], [0, 421946496], [-69372204862, 0], [0, 326734866], [5345822858, 0], [0, -1097946054], [0, 938472912], [-56305194454, 0], [0, 1641523230], [-64565600806, 0], [0, -1333747836], [0, -1237812534], [0, -222510060], [-29506163014, 0], [70005567554, 0], [-92512251142, 0], [0, 1461057282], [0, 1503154314], [0, -504903060], [114810505754, 0], [-55420042654, 0], [0, -2551584978], [0, 2343476520], [0, -2805076908], [-203533626046, 0], [-124166527222, 0], [34977924098, 0], [0, 493604964], [165856577114, 0], [-114663090646, 0], [0, 2365923744], [237606652538, 0], [0, -2229981732], [0, 634781682], [0, -4345678278], [201420140162, 0], [-56303485342, 0], [0, 3660908094], [0, 996744798], [205761798842, 0], [0, -4097992086], [-109816956286, 0], [0, -4544355528], [-323635545814, 0], [462635195138, 0], [479772356762, 0], [0, -9000021780], [177970842122, 0], [-217313259454, 0], [0, -461656692], [-147893538046, 0], [0, -5617832274], [-210775956934, 0], [0, -5448565926], [0, 5886163044], [464058105914, 0], [0, -4925524122], [65653610666, 0], [0, 3739066614], [729416394242, 0], [0, 10898260668], [-580498117054, 0], [0, 6240562812], [-1021634282758, 0], [0, -9559976112], [143690703746, 0], [0, -16319251080], [-1101809178694, 0], [0, 337795524], [479910002618, 0], [0, 17554988952], [592307330474, 0], [0, 21565609776], [1136358298466, 0], [0, -11838085182], [0, 6674392170], [0, -855603540], [-52629935062, 0], [0, -10030357170], [0, -9549558648], [0, -1555739820], [27346015754, 0], [297835251458, 0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_12_c_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_12_9_c_b();".
// 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_9_c_b(:prec:=2)
    chi := MakeCharacter_12_c();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 9));
    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_9_c_b();".
// 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_9_c_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_12_c();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,9,sign)));
    Vf := Kernel([<5,R![1283040, 0, 1]>],Snew);
    return Vf;
end function;