
// Make newform 6.22.a.b in Magma, downloaded from the LMFDB on 18 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_6_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_6_22_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_6_22_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_6_a();"
function MakeCharacter_6_a()
    N := 6;
    order := 1;
    char_gens := [5];
    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_6_a_Hecke(Kf)
    return MakeCharacter_6_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 := 22;
    raw_aps := [[1024], [-59049], [12954174], [-479513104], [115657781700], [295658246702], [6626983431906], [28576184164796], [335385196791000], [-699224214482106], [-3484957262657992], [-35181531093012298], [6132056240639994], [233260850096910596], [-580205712121346400], [-1394471665941750306], [2352476807159705700], [9920628300330384590], [26068981808996843996], [-13336955952504341400], [9037529597968684202], [-77283864571811027992], [-155698418876868248388], [253837312813381912218], [-1030722092535365121598], [-765512937290535332082], [-897523743732066414304], [1717739980735440755988], [2998475215252850465390], [-5117109968957302667118], [4051025628579033462920], [-19286740168000300193700], [-29353605098731251876294], [42406781134264755350996], [50449188236194467441294], [83078909500562698128608], [-166284912959038859757298], [-113920763652858463071604], [352378464819560720079000], [-54370917211349474040282], [557843266009054775730924], [-529020623061951632686186], [-81926212869210079146624], [660173809644950548680578], [-879420276631515864067506], [882376051258445135770208], [-2862603888296932263299380], [-1048922279959895989620280], [-4225614488021020663223412], [-9400000044853291279966810], [-5595279014471894733687318], [-4796607628778362547725776], [1302395449469646017926802], [-12548931193766945489812524], [31247778284915427471470082], [-20434497719364343192705800], [-6026506414107250970122506], [-11301102664423726320437416], [75173087620117599385619702], [-22465876783827781470053382], [-60245993932778778689018428], [4650329370859849085610894], [-68909774995400835049064980], [-27525409738059325338010200], [-151255283266104985946197798], [-54595777259104615230348282], [457957823548632141595889972], [177476691565518411934227026], [-331317257435621945247024588], [-182866387650109005371973010], [-558760567313975539341116094], [-865985621863362703253968824], [-131813415096197568135344728], [-480047662826678413671242074], [361040795878149739922688596], [-445742697334730425298227200], [2636157352065095529260568894], [-1912764909696852395927354722], [2316213832094513219270497794], [1775001379158091807407753626], [-2225802770490730625183910900], [2262739155442248760253605190], [-1807507854061144300362844800], [7124674062886662639592795826], [1588746425863639052821643408], [352671881915152889798992212], [7747243893273538039427237394], [-14197730335024872696065336374], [-2103210149810778352670670330], [16634777210529449722133300696], [-19255182564457474743453465636], [19841658132709746717394199376], [-191035714941054591846033328], [8060028185385412999388379924], [31263065909788889136875679596], [-41471744113244580459758138376], [25426316637938758122125071494], [-32474228640042200990259857382], [14560469846857808119446750020], [15981981439759860982462789790], [103424910679138818032566782572], [-98134951843772964034664503482], [71014215660303120540770816988], [147836614248845593304912959818], [-130988178011621522814453377980], [-113007310705585626568650777598], [-105611914346383316417762121612], [122381033366206524823288185330], [137213271105827569801914565176], [189597933643873572981927243578], [-107831718570969991912447694104], [44989958893070551722310746902], [283972573364715438827944637130], [117452482538980094657149787444], [111858109311016855877381311232], [502292138837093267543378170770], [427106397633716070914542414796], [-432869371376299933545060356376], [-273969463308167944821768854682], [30774292497768829505538267948], [-306325493303110629021885253786], [509843014407425385278138613026], [-738046875917502110730592021506], [-468059179903946354258470489212], [-477474753886969351995997997956], [-976925940014301879769869530106], [-806956375219866570406831610362], [220302610113502876128256310400], [1521283370019011830129836963872], [336650773106104010116787073278], [-259622479515250382975372574052], [-1317173997682701985648189153224], [146729952611463169624658420360], [-316010259106827275514315890698], [-2026388602487662636896022895382], [1555099251267480721872700550402], [-3905272564662792578376330923058], [-691303328804076932427064514404], [3057148757751151739687234745270], [5623019003460037800152197443546], [-3357516641278651801212377105404], [1664941294983426468791189443518], [-4793926285690142404224018294880], [6927370987349351261609952142212], [-2557450599821838368593823615362], [-416859922163997371415247769976], [7407710354977321227034376385254], [-3644477359242619983454505616918], [-3704158592698023076184293859404], [686611927923057961132395491424], [14188600188599117744055088655102], [9830897872982398083175252108146], [1060307527387328856596881017596], [-8968940243707312627497904855800], [3045990468445518850013279544644], [9547961095613954803992792287952], [15636980862752453105548981409984], [-4040442560812209382984703380206], [-20983816638064628733787838869174], [21774437693871719273183311271718], [12146790671920069836878840665836], [6952123322837741329356621389706], [-8857946323324955690257851205504], [-30538219808047969188760674707676], [-23801887002071050191622646178942], [-34076116212649810464794109552024], [-35354604499683225631671603591016], [-42357710256097418841286021071274]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_6_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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