
// Make newform 14.12.a.a in Magma, downloaded from the LMFDB on 03 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_14_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_14_12_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_14_12_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_14_a();"
function MakeCharacter_14_a()
    N := 14;
    order := 1;
    char_gens := [3];
    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_14_a_Hecke(Kf)
    return MakeCharacter_14_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 := 12;
    raw_aps := [[-32], [-396], [7350], [16807], [-108780], [-635842], [-9225918], [-7555372], [26489400], [-169827594], [-51362704], [-251605906], [-928817814], [-1818895756], [523343136], [4199520078], [9140129196], [-6639312802], [-2878139188], [-4345596360], [23450332826], [-28761853648], [-5577757548], [78002173386], [-26685859630], [17882145750], [113608451528], [284799380004], [-287039718682], [-268972406382], [7860543248], [-36466904892], [373779850746], [284074152236], [-103401922962], [-1546083497992], [-842896812178], [673322097692], [1751625121416], [3101625055614], [4337685674172], [3797364918710], [-4403912665056], [4102160346242], [-1816531306146], [-4433674659640], [7555177015676], [5395956291104], [-14171339041404], [5520258139814], [6988518195642], [-9353016518304], [7220420640770], [-7614696707748], [-15246926998014], [-6343753947720], [-10939784245218], [-8955005588656], [-18838117442290], [-46624707344886], [58191168090284], [25081453650102], [-69418567359916], [16051269528456], [17173924298330], [7682239127622], [-99049236813292], [-72543765939022], [-42245441432508], [51157654902158], [-116006537155326], [72525194223624], [-91095251360944], [250341771364622], [75322311646916], [-2458874098992], [74598446033742], [-182482940267170], [-223180958801838], [-290743143950854], [-96604688951436], [251913924475070], [-83517789606336], [-179810628240382], [-109961416587928], [382051956600084], [229942618368642], [68063023881050], [380318951473662], [-653295715595344], [290413960141428], [-815890486358544], [-652013860607512], [-653305197056412], [-89681571143236], [391571696544840], [395207188775214], [360801676239786], [175686460365116], [-377999454048442], [-907524495711364], [2136289928921622], [439189622522340], [-1405705084637046], [1597752721801412], [-2549002797953566], [-631842898159284], [-1121979466089198], [1203949510088424], [2852684031991946], [3416615064463904], [-235908536887474], [235155736852602], [701595306645404], [-3169190995173016], [856125057397602], [-1001448162081388], [-2192897818866744], [-2590401517654746], [4137774527325324], [-3997038256953850], [-2978882912609662], [-3519246516453114], [-3861921747651516], [-153785117599276], [4732535210955318], [-3577629452770354], [927429579409248], [-5153103398715592], [3484228001108030], [961034608715996], [-10364612062592568], [9389857591836800], [-11898598544461186], [8716856480606202], [9713861714391506], [-8468091709237098], [787090640872436], [-13712991348036594], [-12672345170596806], [9148351129267772], [4712608716582126], [-9436751389238680], [7744463975243988], [5292527075661854], [-1102478905081752], [23841996823131062], [-22629655972131846], [-20929516741878724], [6252903343456128], [-26122923394996474], [-28506354611366574], [-2170143399707620], [27892705415710488], [32525659479353828], [17445618960441888], [-7364628516681016], [9289061335069554], [7246010586222074], [-27473251524701394], [26331522218166828], [983092936365738], [-50593887949172536], [6011472089338524], [39857462722484178], [53723030152630872], [-6798919700980096], [-12558007843325194]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_14_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_14_12_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_14_12_a_a(:prec:=1)
    chi := MakeCharacter_14_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 12));
    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_14_12_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_14_12_a_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_14_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,12,sign)));
    Vf := Kernel([<3,R![396, 1]>],Snew);
    return Vf;
end function;