
// Make newform 18.12.a.c in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_18_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_18_12_a_c();".
// 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_18_12_a_c();".
// 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_18_a();"
function MakeCharacter_18_a()
    N := 18;
    order := 1;
    char_gens := [11];
    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_18_a_Hecke(Kf)
    return MakeCharacter_18_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], [0], [-5766], [72464], [408948], [1367558], [-5422914], [15166100], [52194072], [-118581150], [-57652408], [-375985186], [-856316202], [-1245189172], [1306762656], [-409556358], [2882866260], [5731767302], [3893272244], [9075890088], [-15571822822], [-30196762600], [-23135252628], [25614819990], [-61937553406], [94964150058], [54371908448], [-5713477884], [-49672374010], [-177802571058], [-473707536136], [-168573812532], [801479261526], [524839237820], [-1257020226870], [-492117459808], [-997091613946], [-484141975612], [-2315373820584], [-3371566157598], [-1645977026340], [2742173271902], [-4084089808512], [-4472389991422], [7370248975146], [2693084564000], [6634577708612], [-5995589013832], [7749301902396], [3758040333710], [-9026758358298], [4022692975920], [-14999667337678], [14681681636868], [16447633968126], [19681818745272], [11617411525650], [7467876506792], [15956443695614], [-39611493918282], [15000063411788], [-38825493139158], [75819366414884], [79990659222888], [-68561413056022], [-6378911616414], [89020993584092], [142600806741554], [133725973385316], [-63047442118810], [28974898162302], [-43310836423800], [-112470912775336], [129947513379758], [-94538171865220], [-54511744524288], [51137944162170], [-48723411062986], [-376407484129122], [312467212549610], [-281729092206660], [-301063838733778], [-346694170280832], [482345467463378], [118934088249200], [199914264990852], [506701914104430], [-362768435523526], [-65646585500862], [-206748343519192], [379400882843916], [-367939370639280], [631644906594224], [-278183150638332], [459050093742500], [915702867160632], [-1085622825947550], [163924983100278], [46912694264588], [-1281958508682538], [1105639461244004], [-2121608950717374], [2448882915166092], [738094760695590], [-21010268758708], [1132487353279874], [2912545182972516], [-1529013546440658], [-2715166624101000], [-2591038342922998], [1687610876689304], [-535763782489282], [2843668860910086], [814487455941980], [-1458684356311168], [1162971435762798], [4734612318417668], [-5475272623688664], [887970661792482], [-3144543068000100], [1329822392592302], [-6844380693513502], [-888819607540614], [-2224018428599628], [4597206126295892], [2587014691966818], [2520806909453870], [-3037163189811840], [6520856018525024], [-1482084159648202], [5393426983129460], [-8961919068060168], [-7787200900914568], [-2419224924478786], [5025522575565318], [12105571636854530], [7456300773991722], [-11257989560649676], [10466895892615986], [12817970979489270], [9792950640539852], [-7084100698669542], [19856913386391008], [20553656994225396], [-14592049502444170], [-6549633589083960], [19924415074625198], [-15914882431310394], [-1190195095374820], [1733431366649952], [11660223223786454], [16609123048628718], [-12820490726271532], [221358390467016], [-24836086882256116], [-8102951455624752], [-33381374096334400], [-35671257106709970], [39624761192998394], [-5371931506709022], [-22224103394043684], [-7668673573745178], [28047373147337984], [24342728765789268], [21914442147874206], [7045855376252712], [40487293131612392], [17960734932482174]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_18_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_18_12_a_c();".
// 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_18_12_a_c(:prec:=1)
    chi := MakeCharacter_18_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_18_12_a_c();".
// 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_18_12_a_c( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_18_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,12,sign)));
    Vf := Kernel([<5,R![5766, 1]>],Snew);
    return Vf;
end function;