
// Make newform 550.6.b.g in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_550_b();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_550_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_550_6_b_g();".
// 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_550_6_b_g();".
// 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, 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_550_b();"
function MakeCharacter_550_b()
    N := 550;
    order := 2;
    char_gens := [177, 101];
    v := [1, 2];
    // 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_550_b_Hecke();"
function MakeCharacter_550_b_Hecke(Kf)
    N := 550;
    order := 2;
    char_gens := [177, 101];
    char_values := [[-1, 0], [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 := 6;
    raw_aps := [[0, -4], [0, 21], [0, 0], [0, 98], [121, 0], [0, -824], [0, 978], [2140, 0], [0, -3699], [-3480, 0], [-7813, 0], [0, -13597], [6492, 0], [0, -14234], [0, -20352], [0, 366], [-9825, 0], [26132, 0], [0, 17093], [-23583, 0], [0, 35176], [42490, 0], [0, -22674], [17145, 0], [0, -30727], [138102, 0], [0, -11864], [0, 16998], [221830, 0], [0, 196671], [0, 120548], [68442, 0], [0, -373647], [60610, 0], [438030, 0], [-239398, 0], [0, 62153], [0, -298724], [0, 82728], [0, -135834], [-112725, 0], [593807, 0], [652557, 0], [0, -402164], [0, -268482], [581200, 0], [-183988, 0], [0, -631679], [0, 1398], [-206135, 0], [0, -640704], [-128250, 0], [-1697488, 0], [-325323, 0], [0, 1649478], [0, 1376526], [-75450, 0], [-360568, 0], [0, -418522], [-794298, 0], [0, -1807964], [0, 875436], [0, 137468], [1226292, 0], [0, 3136921], [0, -1691187], [-2139013, 0], [0, 553598], [0, 167208], [-469490, 0], [0, -2821539], [-1956960, 0], [0, -2285917], [0, -583274], [-2836295, 0], [0, 3522021], [1813575, 0], [0, 3420758], [398442, 0], [-1409570, 0], [1866180, 0], [2077742, 0], [-6284358, 0], [0, -3265589], [2645680, 0], [0, 5635311], [370005, 0], [0, 3318908], [8770212, 0], [0, -224249], [0, -2555727], [-5366640, 0], [0, -8175277], [3780072, 0], [-3981860, 0], [0, -10456854], [6055065, 0], [10505277, 0], [0, -6494924], [5663492, 0], [0, -13360912], [0, -10094742], [0, -5586924], [-2208840, 0], [-10532428, 0], [0, 1843193], [0, 11695908], [0, -5812524], [-7856040, 0], [10942862, 0], [0, 185018], [0, -17744864], [0, 15391218], [17550205, 0], [-5646133, 0], [-4463073, 0], [0, 3240991], [0, 10188453], [0, 4667601], [-1291770, 0], [-11565763, 0], [0, -14770574], [0, -3090222], [0, -14739384], [2362757, 0], [-17888778, 0], [-12273455, 0], [7352325, 0], [0, -3167617], [0, -857924], [19355110, 0], [0, 28030476], [25701437, 0], [0, -11722342], [5777832, 0], [20498920, 0], [0, -15029754], [0, 20532488], [0, -1762467], [-20028900, 0], [-25540918, 0], [-6731658, 0], [0, -14488019], [0, -4158792], [-10252505, 0], [-30770535, 0], [0, -41923214], [0, -18768552], [39551995, 0], [0, -15056904], [0, 15859088], [-22914243, 0], [0, -10607324], [0, 22852698], [0, -32194732], [20225412, 0], [23001910, 0], [16491270, 0], [0, -49940212], [4210002, 0], [0, 27606633], [0, -16929654], [0, 1087928], [7153347, 0], [0, -7885347], [0, -7588419], [8809352, 0], [0, 35383298]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_550_b_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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