
// Make newform 12.18.a.b in Magma, downloaded from the LMFDB on 20 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_18_a_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_18_a_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
        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 := 18;
    raw_aps := [[0], [6561], [130950], [-14846776], [-845469684], [1751414990], [-47147886], [-56973573100], [-371395374696], [-3681168479586], [-5479889229856], [-5446958938138], [29773337634090], [98485895466284], [107861800207536], [626472886328118], [-1260971066668356], [-956343149707138], [-5519389511567164], [9303053873586120], [3692590926453962], [-2597720120860912], [26266742515599444], [63717157489864410], [-78558896505972382], [59353408767538278], [-226969656910043224], [-207210985680398868], [335896743538773038], [474893418758734962], [631697344618764224], [-1544557963022724348], [-1248214424352439158], [-1634753103116551924], [-4132946713620008682], [980466529302015320], [5269593381306749918], [1418175476760235940], [6000566345845354152], [6002157507301278126], [7599552243063387444], [-27070409541599688970], [12824449784456658624], [-27366986272111953214], [-23932893864230122554], [-16312011703851974392], [10944946090335892820], [51019419831635167520], [-175870786392427000860], [2644479268672674086], [35532254324855281770], [-282402747015658881552], [-149584703423113779982], [-297808563045925822212], [-120531643390634366718], [-272843277209395967736], [580932428693478777486], [-17598178803943869616], [249562228825296536918], [867045717579470700186], [188209773386911130012], [516362723377387903782], [-655739830440794537548], [1081998679070812807224], [-1513200728743236405574], [2240061530771258031294], [1480736943957865952972], [-137545453597579635118], [-4847278140089343407268], [2561566603748836870046], [-3298740053942539989918], [-5255347358190781431576], [-4803233677335560168656], [-4003248899966816231626], [5827545670139179197308], [-9152439872150751055488], [14792488808475832608006], [18391749880885487579342], [24425246943805432589586], [-20560618673129515883494], [2767810322407633796772], [17348560370999260961510], [-15478853497875293295312], [-47804515513389022871374], [-36179060358389609479432], [5104483362874060703676], [3955255816096720902978], [-33349823701087491066550], [-36668806467042587425074], [49870909263808991827472], [51477726577167398571732], [75813321993734154632544], [16208195787000171131048], [-34185333734082392465556], [55296710388442686302324], [-37268870022582540075144], [61604054189139500925822], [-246036108349349754658038], [920379916325582182412], [243070597028833736439518], [-327777736154883166762972], [384052271493901579730478], [267025477996849305548340], [124797081298451597017146], [-257524006494889963861060], [267657091422785063578178], [-18794192763349300427316], [-326639455251222412315566], [182888026574805580907736], [-702452739506127756598822], [-64889154592541823081568], [-350458707584813587689178], [-128219544083962891150614], [390943676967734685432044], [464947495279080893120312], [137899606828222912394754], [214587907583763511484036], [-657998926643548411145208], [-1030390664351732296856946], [1313847806427013781966868], [80792487160358156128598], [-2067275611352367541376350], [-2036226152231429659998426], [-1812545071313021205388884], [-2200697770170589505312332], [-640942814414107833237954], [-2662126876774995547378426], [2971338898353463679442768], [-788566440662391434605672], [-2069263178454239162663266], [4013518564499663991232100], [-3896461589295218111892504], [-3101563343637670291616848], [1774927168012688308693046], [1470443246034020872539642], [6081296367131220543317762], [-927737151841067970121722], [-2037595738848121165051372], [-452287798528993014531042], [-1947377905253182621729494], [5225013911014548825639980], [-6817070488236563658371274], [10020120186076006699575032], [72190958078155054021308], [10766627875312748593306622], [5677262418929152304506824], [-9605198093858594796790762], [1750751353950078109563738], [1046399290255492659579740], [372630084780374129171808], [-3013513468850383057558354], [-9481974850500498105206670], [10042420145803190822679668], [-6126673033895885417415816], [-19669408967596285128822388], [3111869542516460567530128], [7878097730017417784984408], [3603635211730949396724258], [34909611537220113729405482], [3587235436842791406268590], [13708310379270342313871412], [-8409685358661532667531334], [2136914959995231156955400], [-8220255071636693784293172], [-41190724219991214399825582], [-25093159636359929060853288], [-10803462273885395101939552], [49982983613176350092907686]];
    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_18_a_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_18_a_b(:prec:=1)
    chi := MakeCharacter_12_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 18));
    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_18_a_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_18_a_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_12_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,18,sign)));
    Vf := Kernel([<5,R![-130950, 1]>],Snew);
    return Vf;
end function;