
// Make newform 12.16.a.a in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_12_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_12_16_a_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_16_a_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
        Kf := Rationals();
    end if;
    return [Kf!elt[1] : elt in input];
end function;


// To make the character of type GrpDrchElt, type "MakeCharacter_12_a();"
function MakeCharacter_12_a()
    N := 12;
    order := 1;
    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;

function MakeCharacter_12_a_Hecke(Kf)
    return MakeCharacter_12_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 := 16;
    raw_aps := [[0], [-2187], [45702], [1217888], [-26895924], [-162581770], [-743272542], [-4003014700], [-30097540728], [19021888926], [-4621552936], [649297928654], [790230862890], [1388728387532], [-3933841180608], [-13472208095706], [-24672598493364], [23630686395542], [32385083278292], [-74451150070920], [176524276453946], [-137959485182488], [-458794939458348], [-32239404369270], [478308097627586], [1637643553179318], [-922136356924912], [1946108089082844], [1301994815794742], [3403473856364466], [-5693697023514712], [5989695440892372], [-2918111663554326], [16326097165364444], [-17295052812656778], [26593573098022640], [11022712609545686], [-8741561840314300], [-27811798434939096], [-96028824653186562], [-1527576097010364], [56226604403451950], [223413853954247424], [24019851200424578], [174867641537208822], [-40021466650688368], [-317744049126149020], [-518380637223537880], [-103080911958134460], [237740757543773534], [-595188492486029190], [352689275840825232], [575221648947961778], [1080965151558467388], [-2347816362809374206], [136050251448222792], [-2302557822590534226], [-1060469332160790376], [670093233845992526], [-4402742820188389974], [268593348555988076], [2334522574818577206], [802474800039188804], [6072792257431531224], [967198941756605258], [4519995312580219998], [-11144680010677541188], [-12481169061641977486], [-6606446109414261156], [22775326828155494774], [-9541054458123715134], [-3436246296004357224], [14734517019738092648], [18012435602799489182], [7343371576244652092], [-16018755185599847424], [30379548561979075974], [-23872610797794608506], [-54549532322769506814], [-40705281779348493046], [50687871148058517828], [9706727701172965310], [40711611452505920928], [-101839017149198776942], [99469218202067967392], [-16714488333700223172], [97823129623411067442], [31325873499491654810], [74212165020899101086], [-120899238238582338664], [-58549986735740473356], [-52642817901346431024], [-155160155573702475904], [-232475556525465300516], [103666730501112407876], [-260380413979412009112], [160376526209476425438], [124872063406119930762], [-204743574654749398324], [-591289129577369768122], [223972160006260885796], [-523942543415120660514], [-119268587491329012300], [658606465904927661594], [-596008071275814376660], [-782829843366864084094], [411165951310029459708], [-62254433398630810158], [1185806505215389002984], [846669631881393208298], [47422604444595466184], [-1170064776622947135634], [294503638592371200282], [1721573885208587450876], [-1588184913232713310768], [-1199842894023986869326], [-1827921521502380075452], [-713338166525046698376], [-2118869078388696362178], [-14777194185313523388], [81485734688871326558], [2437801525455093159650], [-799743107573575666362], [-1996065673490202187572], [1999704473504838042068], [1555560146663804026206], [2512594769672994076286], [1186873343363490848352], [1854363433835659061936], [-2504029246715429741338], [-4990892610968808551980], [888432832584180890568], [-1583717032874445604408], [-3792608524597943464498], [-1820129419412754212358], [372176441051044183298], [4883322929390808241014], [7746167626407777660596], [3934936548295709085486], [-8177510066059231230006], [9025216975475090698700], [-755316352541653978554], [1968076270316806611056], [-10656165956949679894164], [4827920413135179088358], [-5328104493558220793064], [13670578362614069925854], [-323695087389190460934], [-10187260734089411772580], [-5779684582939042596576], [9300658534179115991462], [19706843545931254197330], [23604810954243899826644], [10220085315658387543608], [-18726211032747259217716], [12939057707737904531568], [-17563343152792164142768], [-30437521940148156681678], [2832425361223397106074], [18604621391756693537790], [-456986386582383190716], [11488958105235854560218], [15525893212857683704880], [-14885426567594439149652], [-28745056685824606695294], [45735037023476844869976], [10596593066366443460408], [3014391913943231305262]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_12_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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