
// Make newform 450.6.c.d in Magma, downloaded from the LMFDB on 18 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_450_c();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_450_c_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_450_6_c_d();".
// 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_450_6_c_d();".
// 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;
    Rf_num := [[1, 0], [0, 2]];
    Rf_basisdens := [1, 1];
    Rf_basisnums := ChangeUniverse([[z : z in elt] : elt in Rf_num], Kf);
    Rfbasis := [Rf_basisnums[i]/Rf_basisdens[i] : i in [1..Degree(Kf)]];
    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_450_c();"
function MakeCharacter_450_c()
    N := 450;
    order := 2;
    char_gens := [101, 127];
    v := [2, 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_450_c_Hecke();"
function MakeCharacter_450_c_Hecke(Kf)
    N := 450;
    order := 2;
    char_gens := [101, 127];
    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, -2], [0, 0], [0, 0], [0, 49], [-354, 0], [0, -202], [0, -327], [-1796, 0], [0, -540], [-5754, 0], [10196, 0], [0, 2776], [12960, 0], [0, 4484], [0, 2700], [0, 4107], [3954, 0], [962, 0], [0, -8978], [56148, 0], [0, 42845], [26044, 0], [0, -46734], [73428, 0], [0, 64489], [154794, 0], [0, -13849], [0, -110586], [-123122, 0], [0, 110193], [0, 90649], [254514, 0], [0, -207753], [312340, 0], [300954, 0], [-37576, 0], [0, -189158], [0, -33490], [0, 41712], [0, 21159], [-167406, 0], [-483118, 0], [772572, 0], [0, 335285], [0, -425961], [561256, 0], [-578764, 0], [0, -431557], [0, 356400], [343666, 0], [0, 109407], [-1020948, 0], [1371830, 0], [932058, 0], [0, 632847], [0, -349968], [-488178, 0], [525968, 0], [0, 1214656], [-2290308, 0], [0, 1045778], [0, -53559], [0, 564688], [-3061452, 0], [0, 615125], [0, -339561], [98516, 0], [0, -998897], [0, 1325160], [2033782, 0], [0, 474315], [494616, 0], [0, -37523], [0, -266734], [199780, 0], [0, -434700], [-1051902, 0], [0, 2096308], [1902924, 0], [-1629098, 0], [-2330274, 0], [612974, 0], [-6693840, 0], [0, -1100971], [-1104368, 0], [0, -1911012], [3939852, 0], [0, -508763], [6808302, 0], [0, -3371407], [0, -1613094], [6212124, 0], [0, -756629], [3438354, 0], [-5273396, 0], [0, 4405860], [-10378818, 0], [4721772, 0], [0, 2121698], [-1306822, 0], [0, -4846940], [0, -4238331], [0, 575508], [-45396, 0], [6436964, 0], [0, -289379], [0, -480402], [0, 2851101], [15356904, 0], [12168950, 0], [0, -1473149], [0, 5309024], [0, -5708715], [1282420, 0], [7378940, 0], [1638024, 0], [0, -10192342], [0, -10515876], [0, -3927519], [9327270, 0], [15732482, 0], [0, 6328097], [0, 1325307], [0, -2872374], [-14374876, 0], [-4261638, 0], [-19708766, 0], [-10221096, 0], [0, -6605165], [0, 6529316], [15376660, 0], [0, 13126860], [-13615060, 0], [0, 4206040], [-414684, 0], [-6715214, 0], [0, 11861853], [0, -15889886], [0, 3631401], [-32276856, 0], [-10068892, 0], [-20867070, 0], [0, -7034143], [0, 12410508], [14095414, 0], [6472032, 0], [0, -8585296], [0, -1227471], [-1131260, 0], [0, -9203448], [0, 13076974], [-19764036, 0], [0, -17177542], [0, 10562676], [0, 1306540], [-32704560, 0], [-20646308, 0], [25431804, 0], [0, 12640021], [-18018042, 0], [0, 25524228], [0, 22680525], [0, -6235031], [21378594, 0], [0, -6591207], [0, -14941164], [-37653448, 0], [0, 7672138]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_450_c_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_450_6_c_d();".
// 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_450_6_c_d(:prec:=2)
    chi := MakeCharacter_450_c();
    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_450_6_c_d();".
// 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_450_6_c_d( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_450_c();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,6,sign)));
    Vf := Kernel([<7,R![9604, 0, 1]>,<11,R![354, 1]>],Snew);
    return Vf;
end function;