
// Make newform 18.13.b.b in Magma, downloaded from the LMFDB on 05 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_18_b();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_18_b_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_18_13_b_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_18_13_b_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
        poly := [2, 0, 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_18_b();"
function MakeCharacter_18_b()
    N := 18;
    order := 2;
    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;

// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_18_b_Hecke();"
function MakeCharacter_18_b_Hecke(Kf)
    N := 18;
    order := 2;
    char_gens := [11];
    char_values := [[-1, 0]];
    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 := 13;
    raw_aps := [[0, 32], [0, 0], [0, 14565], [33572, 0], [0, 1211892], [-5196976, 0], [0, -22150593], [-66057424, 0], [0, -121799796], [0, 286119627], [-1730932396, 0], [4291562342, 0], [0, 5956699233], [2713386440, 0], [0, 5663026140], [0, 8205930171], [0, -7634221944], [-37496538790, 0], [74662225976, 0], [0, 50984072964], [-72295502128, 0], [317431332236, 0], [0, -86508745428], [0, 167489026641], [-134693309248, 0], [0, -344132859819], [-1976963805124, 0], [0, 768537826560], [-2368218143536, 0], [0, 1929584384337], [776185554404, 0], [0, 4253266365240], [0, 1799244181887], [9775350242408, 0], [0, 7046216872635], [-10182430762828, 0], [-6134092265914, 0], [20633549236328, 0], [0, -18242021494392], [0, 10810416735141], [0, 27154823934384], [32380287529808, 0], [0, -62514210366984], [-66837747714802, 0], [0, 35255329249509], [55584261245900, 0], [2245227000536, 0], [-1471564264588, 0], [0, -161957069648076], [-29508278539408, 0], [0, 152121377828223], [0, -9273004158336], [310821007140272, 0], [0, -150380590969764], [0, 193386280492767], [0, 229534486519992], [0, -162080362434267], [-116385488729980, 0], [-113006938597936, 0], [0, -491681106931359], [-896719522622704, 0], [0, 47122338567963], [541155525353480, 0], [0, 220385750594376], [443247467352338, 0], [0, 663218833865829], [-1063318732848664, 0], [1093339225699184, 0], [0, -1952090224180332], [1558745993464058, 0], [0, -61471777312047], [0, -478838664335124], [-1871856543139612, 0], [-4502902019038630, 0], [5401378296044480, 0], [0, -1775625693465696], [0, 944844997359435], [4709948736156122, 0], [0, -1353768235606689], [1126072795876832, 0], [0, -3026779706684436], [-3029263630979920, 0], [0, -4645137112207668], [-9269026641612034, 0], [-7675758501762076, 0], [0, -4012963319292996], [0, 2573913340187937], [8750690321061440, 0], [0, -1222023167009733], [8677172818550108, 0], [0, -8583863822279676], [0, 1396751456424588], [25091816887851212, 0], [0, -7050504082853472], [8177104120084256, 0], [0, 4677910060325436], [0, 12700559535450411], [0, 17219693548788111], [16578098845395392, 0], [-27047078192682448, 0], [12597183172812872, 0], [0, 41817196829262411], [0, 6992991737024196], [0, 22453839831670719], [-51743821342327792, 0], [40145605716244466, 0], [0, 171898225524888], [0, 17197259052520719], [0, 54351267190003596], [-1607758837284562, 0], [1188903176194484, 0], [25951946948889914, 0], [0, -30226218595275903], [49236878753136704, 0], [-103808994470503540, 0], [0, -87726705290410737], [74576819944318664, 0], [0, 58255600531065732], [0, -7385114930545851], [0, 49674321725105544], [-101133673928669338, 0], [12962242383618530, 0], [0, 57120584527311045], [0, -108908166379792788], [91800471651909320, 0], [0, -28093009463191845], [112448211807263312, 0], [0, 151420029963545412], [-127583578596451876, 0], [120030986773351568, 0], [-85679587211739856, 0], [0, 31715069810631240], [2794864842421508, 0], [-246670237364013520, 0], [0, -79684897225595631], [-201621753008814670, 0], [0, -13601836200085173], [56113904908472240, 0], [0, 108880989697075125], [0, -37089400045883967], [175187662353427520, 0], [0, 188910014741740149], [-20247745629109804, 0], [0, -101412124131337416], [51046855031722448, 0], [0, -222471293014565796], [-325530696300540442, 0], [0, -213814508641586751], [179679034012592072, 0], [0, 96768251164933416], [31993460017565690, 0], [0, 138242563904688543], [-237633780981288328, 0], [0, 217459998190641432], [-694836846182690296, 0], [0, 384501457521859272], [-455640025996165660, 0], [0, -164677986242960961], [-1199539586389279426, 0], [0, 70340862301672251], [0, 155161596057045864], [0, -456199738891954929], [-749191843765615300, 0], [0, 1128429017468423964], [0, 382487876096336769], [0, -1208133698318530392], [-1618441992785219452, 0], [-57772424853004102, 0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_18_b_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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