
// Make newform 18.9.b.a in Magma, downloaded from the LMFDB on 07 June 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_9_b_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_18_9_b_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 := [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]];
    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 := 9;
    raw_aps := [[0, 8], [0, 0], [0, 165], [-3532, 0], [0, -14268], [-41824, 0], [0, 67023], [-36304, 0], [0, 292476], [0, 190347], [-471196, 0], [-3007402, 0], [0, -1212927], [3623720, 0], [0, -4252980], [0, 7266699], [0, 1900776], [-5440630, 0], [-6121576, 0], [0, -14986476], [-49031152, 0], [8357756, 0], [0, -36339492], [0, 75898881], [20431328, 0], [0, -119913339], [-29825236, 0], [0, -86849280], [-48884416, 0], [0, 133816593], [339908276, 0], [0, 54393240], [0, 90409983], [9089288, 0], [0, 130958715], [-335601148, 0], [-201630826, 0], [-527660728, 0], [0, -128421528], [0, -49954251], [0, -55887696], [548167808, 0], [0, 1588142616], [655575182, 0], [0, -316947339], [734930300, 0], [-3268001224, 0], [-3808405372, 0], [0, 3073446276], [-2863969408, 0], [0, 393930927], [0, 925114944], [-4841822128, 0], [0, -5499247284], [0, 4017767583], [0, -739149672], [0, 3483803493], [-6592236460, 0], [-121589824, 0], [0, -3964311759], [1625595344, 0], [0, -2978681973], [5884748840, 0], [0, 6929688936], [3761621042, 0], [0, 11974751781], [3189982856, 0], [16637753456, 0], [0, 758424612], [-2323022422, 0], [0, -14036520783], [0, 7331245596], [-20287976908, 0], [22211095850, 0], [12378955520, 0], [0, 20532105696], [0, -20770403925], [25815844298, 0], [0, -4631150769], [44181328832, 0], [0, -31998384996], [21919269920, 0], [0, 6802525692], [-46480518466, 0], [-37227366796, 0], [0, 37745484396], [0, -2943591183], [-29197541920, 0], [0, -11721524853], [-36684739828, 0], [0, -12753910764], [0, 2016093948], [-101307203332, 0], [0, -8306306592], [114666616736, 0], [0, -11568561876], [0, 27768810891], [0, -26304072849], [30330132608, 0], [75426247712, 0], [-20422080472, 0], [0, 17942860539], [0, -73145637996], [0, -67232587521], [-103046498992, 0], [-82886920846, 0], [0, -7955181768], [0, 137858511951], [0, -21490400004], [-119014816882, 0], [-113079939004, 0], [193018275146, 0], [0, 161333719473], [121474399424, 0], [303109105340, 0], [0, 20084481663], [-338883217624, 0], [0, -135065220012], [0, -33694624779], [0, 57711330024], [58885669142, 0], [165633890, 0], [0, -182984533275], [0, -87774558372], [119733702440, 0], [0, 101578663035], [414241119392, 0], [0, 217615720212], [-15939589684, 0], [16783072352, 0], [-367552074256, 0], [0, -236413192920], [-179372448172, 0], [-195682837120, 0], [0, -232844414991], [-86720075470, 0], [0, -497939877], [-404090146000, 0], [0, -280836539835], [0, -423527321727], [-162270954880, 0], [0, -166519605531], [364579434404, 0], [0, 529059044376], [491505860288, 0], [0, -48471880116], [-731087123818, 0], [0, 588211975041], [-264366341848, 0], [0, -516864097656], [663180327530, 0], [0, 316512437823], [-227693255272, 0], [0, 135826391928], [650952813416, 0], [0, -208452738648], [-825356656780, 0], [0, 762112252239], [1442142760286, 0], [0, 330558224331], [0, -756810293304], [0, -401662265841], [558774930860, 0], [0, 502995475404], [0, 193344971601], [0, 209120052552], [-1290477523372, 0], [-221939198518, 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_9_b_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_18_9_b_a(:prec:=2)
    chi := MakeCharacter_18_b();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 9));
    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_9_b_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_18_9_b_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_18_b();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,9,sign)));
    Vf := Kernel([<5,R![54450, 0, 1]>],Snew);
    return Vf;
end function;