
// Make newform 18.7.b.a in Magma, downloaded from the LMFDB on 19 April 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_7_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_7_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+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 := 7;
    raw_aps := [[0, 4], [0, 0], [0, 123], [-484, 0], [0, 948], [3368, 0], [0, 9], [5744, 0], [0, -2388], [0, 20757], [-39796, 0], [52526, 0], [0, -26193], [3800, 0], [0, 54300], [0, 168813], [0, -176664], [13250, 0], [168968, 0], [0, -375804], [236144, 0], [-35116, 0], [0, -7764], [0, -91449], [-321424, 0], [0, 472899], [1993412, 0], [0, -184320], [194456, 0], [0, -581169], [3057212, 0], [0, 2173560], [0, -3170751], [-1092328, 0], [0, -1570395], [-4078708, 0], [6155678, 0], [800696, 0], [0, 3397416], [0, -2524317], [0, -5258736], [-10381192, 0], [0, 9188184], [-3931954, 0], [0, 3804003], [-565900, 0], [-13516504, 0], [-5354836, 0], [0, -9621132], [4346408, 0], [0, -14307111], [0, -14421216], [-3120928, 0], [0, 3602844], [0, 10208769], [0, 22101816], [0, 177723], [29639900, 0], [13221272, 0], [0, -4330041], [-6743248, 0], [0, -7087971], [-5230600, 0], [0, 22077384], [22477826, 0], [0, -19531077], [-5761384, 0], [-40105168, 0], [0, 47950836], [-42063838, 0], [0, -12443361], [0, -9893844], [-26585476, 0], [17882930, 0], [72097760, 0], [0, 6142272], [0, -34960155], [15691106, 0], [0, -33553191], [-115511632, 0], [0, 103727724], [139238840, 0], [0, -70987572], [-40063138, 0], [-138591844, 0], [0, -78801828], [0, -43215993], [35666480, 0], [0, 107468013], [114978116, 0], [0, 62345988], [0, -63242772], [-75168844, 0], [0, 31877952], [91545824, 0], [0, -113874372], [0, 16970181], [0, -63703071], [-37769056, 0], [25479992, 0], [205215896, 0], [0, -170513523], [0, -119414172], [0, -172530591], [241501808, 0], [-49397902, 0], [0, -121659144], [0, 191373777], [0, -122541204], [-431090242, 0], [16699052, 0], [-192320782, 0], [0, -132245481], [254873216, 0], [92360180, 0], [0, -300283863], [375946328, 0], [0, 18649764], [0, 182968467], [0, -98531736], [-472544818, 0], [548833250, 0], [0, -71248245], [0, -221364], [-372812200, 0], [0, 439233525], [-246510232, 0], [0, -67909788], [391370852, 0], [34907816, 0], [-302999344, 0], [0, 173685480], [-82327012, 0], [-603578680, 0], [0, -164743329], [815795570, 0], [0, -259215939], [-402462160, 0], [0, -366945645], [0, -2149857], [225521120, 0], [0, 195893571], [-707335828, 0], [0, -188334792], [503826392, 0], [0, 539780124], [-18798514, 0], [0, -485377377], [551931608, 0], [0, 258564168], [-585387070, 0], [0, -303886863], [220084904, 0], [0, 828702744], [731614232, 0], [0, 649544808], [-215986660, 0], [0, 219290889], [-742448338, 0], [0, 128527629], [0, 607538568], [0, 485654193], [-1094109820, 0], [0, 313329276], [0, 841488423], [0, -835705176], [509602268, 0], [-990780046, 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_7_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_7_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, 7));
    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_7_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_7_b_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_18_b();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,7,sign)));
    Vf := Kernel([],Snew);
    return Vf;
end function;