
// Make newform 21.42.g.a in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_21_g();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_21_g_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_21_42_g_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_21_42_g_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
        poly := [1, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rfbasis := [Kf.1^i : i in [0..Degree(Kf)-1]];
    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_21_g();"
function MakeCharacter_21_g()
    N := 21;
    order := 6;
    char_gens := [8, 10];
    v := [3, 5];
    // 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_21_g_Hecke();"
function MakeCharacter_21_g_Hecke(Kf)
    N := 21;
    order := 6;
    char_gens := [8, 10];
    char_values := [[-1, 0], [0, 1]];
    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 := 42;
    raw_aps := [[0, 0], [3486784401, 3486784401], [0, 0], [203578679277844478, 14333144549023023], [0, 0], [-39338535167225678926429, 78677070334451357852858], [0, 0], [327892480542512998547656854, -163946240271256499273828427], [0, 0], [0, 0], [3972104657603020535799159829295, 3972104657603020535799159829295], [0, 64784776974663170778578317212011], [0, 0], [1978790716644901667795378814766045, 0], [0, 0], [0, 0], [0, 0], [78549836622620336471400199098016568, -39274918311310168235700099549008284], [50826626565024479057127571253517786091, -50826626565024479057127571253517786091], [0, 0], [4710896449426576845570173703317689599, 4710896449426576845570173703317689599], [0, 1275403016012072140452019839110723403583], [0, 0], [0, 0], [33583226752533476288894527675921565030728, -67166453505066952577789055351843130061456], [0, 0], [124586986746556916188957029271572385588018, -62293493373278458094478514635786192794009], [0, 0], [1128483700445313805830532603986645951801677, -1128483700445313805830532603986645951801677], [0, 0], [7284759449548809036854976317796213132872399, 0], [0, 0], [0, 0], [-96550587816253762366199726246036666820047597, 193101175632507524732399452492073333640095194], [0, 0], [-380337578946459333552170484110490459607777444, 380337578946459333552170484110490459607777444], [-809160449030413003103028520495986114640269612, -809160449030413003103028520495986114640269612], [0, -2142364695965127952541789904425857307623233992], [0, 0], [0, 0], [0, 0], [-4507562162584254107990146236170936983497766635, 9015124325168508215980292472341873966995533270], [0, 0], [-134894316296770686754952352356669126173831626025, 134894316296770686754952352356669126173831626025], [0, 0], [-145951528809663537659743024047544572308857752062, -145951528809663537659743024047544572308857752062], [-834499637213587924239459431220682695473269461104, 0], [-1446989389374606224133427603935791422149000991686, 2893978778749212448266855207871582844298001983372], [0, 0], [-5439631669324555525325322268362348158350454777674, 2719815834662277762662661134181174079175227388837], [0, 0], [0, 0], [7409507315339607435330149080677321340541360852624, 7409507315339607435330149080677321340541360852624], [0, 0], [0, 0], [0, 0], [0, 0], [-114666715389846890927606436355780795294327784771180, 57333357694923445463803218177890397647163892385590], [-214735395649086086416383712884855142045527980510501, 214735395649086086416383712884855142045527980510501], [0, 0], [142052958968407965340600892233918928106944814073981, 142052958968407965340600892233918928106944814073981], [0, 0], [1004348128522061759386164721155881432858776060852001, -2008696257044123518772329442311762865717552121704002], [0, 0], [-792327752546546267228186036939686091730273798494694, 396163876273273133614093018469843045865136899247347], [0, 0], [0, -8189125131790828797057498408159351502618931334733231], [8263354306146580569490962826177591425040392923785795, 0], [0, 0], [15490255075536370466156872141356072636444893877959180, -30980510151072740932313744282712145272889787755918360], [0, 0], [0, 0], [-43248907102587632128531787327885346845554420315361091, -43248907102587632128531787327885346845554420315361091], [0, 104756969854447899765081033459395650085287521733811737], [133136186902247875459662016984163470170046329308677267, 0], [0, 0], [0, 0], [-435143630369783250016540700362554193541107696412026322, 217571815184891625008270350181277096770553848206013161], [0, 0], [264941275527682560590925520085725561195038709928828017, 264941275527682560590925520085725561195038709928828017], [0, 0], [-973208152765986817622679154145631897424233343332950359, 0], [0, 0], [1279857124173402382361840662875315110490280386679172437, -2559714248346804764723681325750630220980560773358344874], [-535471977383396912226404145113460149016840998748864804, 267735988691698456113202072556730074508420499374432402], [0, 0], [0, 0], [0, 4808370959515156228353736992152426338968965806734259169], [0, 0], [-7965068687680039443930555585360115537951389143937658843, 0], [0, 0], [0, 0], [10673880219289592631562971235612970348709861336428697725, -10673880219289592631562971235612970348709861336428697725], [0, 0], [0, -11010420762760692735953146231561167370458703015165609093], [0, 0], [0, 0], [0, 0], [-48389457370057170259271062967174056695590947952568441166, 24194728685028585129635531483587028347795473976284220583], [0, -134539878120594431165277790477662613322582409375614104529], [252425233346320980451526854719978759549688450176033902360, 0], [0, 0], [0, 0], [0, 0], [-399854576511738398607162599718790815984991347086183165759, 399854576511738398607162599718790815984991347086183165759], [212918633614367386405618647023558548442091618172597167931, 212918633614367386405618647023558548442091618172597167931], [0, 0], [0, 0], [0, 0], [1062059668645847789782786921647800575594267152196862735399, -2124119337291695579565573843295601151188534304393725470798], [-2435519543637389676887608507827560980913911567184036903154, 1217759771818694838443804253913780490456955783592018451577], [2461262643926373207844324859014744190238939453950982458410, -2461262643926373207844324859014744190238939453950982458410], [0, 0], [-1521477363180333890845875774541757559723574693538587533205, -1521477363180333890845875774541757559723574693538587533205], [-1152045689888779937994319292392623605005924653162824965876, 0], [0, 0], [3512681687347152471581434678135832835839285497024125553189, -7025363374694304943162869356271665671678570994048251106378], [0, 0], [0, 0], [0, 0], [465828359357392266765165098792285113296391108081321968771, 465828359357392266765165098792285113296391108081321968771], [17094914244536764534165775337535070702399023348562966541613, 0], [0, 0], [0, 0], [-18045504185555019459111006200928229480201528856608737741638, 9022752092777509729555503100464114740100764428304368870819], [0, 0], [0, 53957340724644740519869439642563013019262274152261655181578], [0, 0], [49143868086139029330148757925816869990927670775883934755407, -98287736172278058660297515851633739981855341551767869510814], [71066312102634690033161814386635833637385339102609899147162, -35533156051317345016580907193317916818692669551304949573581], [125638075520363298583410564512548220714474209959878490301237, -125638075520363298583410564512548220714474209959878490301237], [0, 0], [0, -16523956529849481738365352781598597880604710785971079883589], [-203932606271969340651354478992209290632087380780036983501754, 0], [0, 0], [152118950173630907700226477680125846199792181498283092190865, -304237900347261815400452955360251692399584362996566184381730], [0, 0], [153863692899548723875832927993623387094854387344184097156002, 153863692899548723875832927993623387094854387344184097156002], [0, 0], [0, 0], [-471163360350052503372346609750226868835577215495399677993894, 942326720700105006744693219500453737671154430990799355987788], [0, 0], [669274400168706298202354275757490533974766245778359305747028, -669274400168706298202354275757490533974766245778359305747028], [0, 0], [-218874887286542493696353396761800955708051556066682694247187, -218874887286542493696353396761800955708051556066682694247187], [0, 0], [-496137920057266788611514585657594027812620079271931624316577, 992275840114533577223029171315188055625240158543863248633154], [0, 0], [-857352769419247009012262572560129256872004678038640765403180, 428676384709623504506131286280064628436002339019320382701590], [0, 0], [0, -272693889973228485586365464140269342671922336137363432532734], [0, 0], [1457505563248519584458056962707680963722812881410995045928967, 0], [0, 0], [-3763168439495844539249395426959532357895437092915807947281219, 3763168439495844539249395426959532357895437092915807947281219], [0, 0], [0, -7006339462647666864475354510269750129340338658735298405321851], [0, 0], [9461704626726946190876915560331007816595997996120007991674979, -18923409253453892381753831120662015633191995992240015983349958], [0, 0], [0, 0], [0, 0], [6014105704836412195549435092201300165124973387718971050825241, 0], [0, 0], [0, 0], [0, 0], [-51347133010030542550946848941504641356089836951536051130925133, 51347133010030542550946848941504641356089836951536051130925133], [9716572824485995767527247553409384869492802640970825569228013, 9716572824485995767527247553409384869492802640970825569228013]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_21_g_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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