
// Make newform 900.6.d.a in Magma, downloaded from the LMFDB on 17 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_900_d();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_900_d_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_900_6_d_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_900_6_d_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 := [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_900_d();"
function MakeCharacter_900_d()
    N := 900;
    order := 2;
    char_gens := [451, 101, 577];
    v := [2, 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_900_d_Hecke();"
function MakeCharacter_900_d_Hecke(Kf)
    N := 900;
    order := 2;
    char_gens := [451, 101, 577];
    char_values := [[1, 0], [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, 0], [0, 0], [0, 0], [0, 44], [-540, 0], [0, -209], [0, 297], [-836, 0], [0, 2052], [-594, 0], [4256, 0], [0, 149], [-17226, 0], [0, -6050], [0, -648], [0, -9747], [-7668, 0], [-34738, 0], [0, -10906], [46872, 0], [0, 33781], [76912, 0], [0, -33858], [29754, 0], [0, 61199], [-11286, 0], [0, -13628], [0, 61182], [-99902, 0], [0, 14823], [0, -168256], [-100980, 0], [0, -158571], [148324, 0], [196614, 0], [74360, 0], [0, -60439], [0, -55670], [0, -245916], [0, -353727], [493668, 0], [-559450, 0], [724032, 0], [0, 3553], [0, -265221], [-56168, 0], [-339196, 0], [0, 389680], [0, -372438], [272746, 0], [0, 76923], [1154736, 0], [657074, 0], [-1341900, 0], [0, 66177], [0, -471636], [967518, 0], [-518320, 0], [0, -1111363], [196614, 0], [0, -776138], [0, 536085], [0, -792946], [730728, 0], [0, 292429], [0, -1241433], [377948, 0], [0, -319561], [0, -1452330], [3991570, 0], [0, -714609], [1161864, 0], [0, 544616], [0, 1752883], [-4043852, 0], [0, -2593728], [-950346, 0], [0, 260369], [-764370, 0], [-2640506, 0], [-4980204, 0], [-237994, 0], [3882384, 0], [0, -33479], [6501352, 0], [0, 2303802], [3776706, 0], [0, 1590347], [-6685470, 0], [0, -2175608], [0, 3539970], [3221856, 0], [0, -1148548], [-2821500, 0], [4136284, 0], [0, -4166316], [4341006, 0], [6741846, 0], [0, -3860978], [-682066, 0], [0, -1078858], [0, -1337985], [0, 1776654], [-12922470, 0], [-6083572, 0], [0, 7912031], [0, 2301102], [0, -4305609], [-7982280, 0], [10174010, 0], [0, 4979216], [0, 2097931], [0, 4562757], [-6457340, 0], [-14051368, 0], [-8471682, 0], [0, 244282], [0, 1240596], [0, 2645649], [4724676, 0], [-6174202, 0], [0, -4704623], [0, 7504299], [0, 6485346], [22655588, 0], [-19016910, 0], [-15131126, 0], [-15032304, 0], [0, 3706148], [0, -1389641], [12104620, 0], [0, -2234628], [28846256, 0], [0, -4804339], [-4545882, 0], [21592318, 0], [0, 7420005], [0, 12439262], [0, 15803991], [-3100086, 0], [1871804, 0], [20018394, 0], [0, 7655884], [0, 4796550], [-25220942, 0], [-17762328, 0], [0, -243485], [0, -963171], [-22353836, 0], [0, -9291888], [0, 14552417], [3146958, 0], [0, 7999762], [0, -17293716], [0, -8719822], [2595888, 0], [17641096, 0], [39678498, 0], [0, -19670629], [-46250622, 0], [0, -18951246], [0, 13323123], [0, -20489524], [27203364, 0], [0, 12677769], [0, -5956956], [29193056, 0], [0, 8650037], [12462670, 0], [0, 14476941], [-32851764, 0], [-21039346, 0], [-17290152, 0], [0, -12727979], [-8922704, 0], [19246842, 0], [48992108, 0], [-11072214, 0], [0, -28737500], [-46249214, 0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_900_d_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_900_6_d_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_900_6_d_a(:prec:=2)
    chi := MakeCharacter_900_d();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 6));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(1069) - 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_900_6_d_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_900_6_d_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_900_d();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,6,sign)));
    Vf := Kernel([<7,R![7744, 0, 1]>,<11,R![540, 1]>],Snew);
    return Vf;
end function;