
// Make newform 900.4.d.d in Magma, downloaded from the LMFDB on 19 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_4_d_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_900_4_d_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_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 := 4;
    raw_aps := [[0, 0], [0, 0], [0, 0], [0, 1], [-30, 0], [0, 2], [0, -45], [28, 0], [0, 60], [210, 0], [-4, 0], [0, 100], [-240, 0], [0, 68], [0, 60], [0, -15], [-450, 0], [-166, 0], [0, 454], [1020, 0], [0, 125], [916, 0], [0, -570], [-420, 0], [0, 769], [-450, 0], [0, 575], [0, 810], [1702, 0], [0, 675], [0, 1225], [-690, 0], [0, -1035], [1924, 0], [2910, 0], [176, 0], [0, 1174], [0, 998], [0, -1560], [0, 885], [-2130, 0], [-1654, 0], [-1740, 0], [0, -43], [0, 1245], [832, 0], [2084, 0], [0, 587], [0, 1560], [58, 0], [0, -2955], [3300, 0], [-2986, 0], [6630, 0], [0, 765], [0, 1320], [-7350, 0], [3512, 0], [0, -2684], [3060, 0], [0, 2522], [0, -1005], [0, -1376], [-9540, 0], [0, -4627], [0, -75], [1892, 0], [0, -3689], [0, -3360], [-5186, 0], [0, 1665], [9000, 0], [0, -4379], [0, -2362], [-7292, 0], [0, 7260], [7110, 0], [0, -5744], [-780, 0], [-5402, 0], [-2190, 0], [-7162, 0], [-9360, 0], [0, -6403], [-11288, 0], [0, -4260], [-1260, 0], [0, -6875], [3210, 0], [0, 6425], [0, 4110], [-7020, 0], [0, -4061], [13470, 0], [-2468, 0], [0, -2220], [-11190, 0], [4020, 0], [0, 4538], [-7486, 0], [0, 3700], [0, -5745], [0, -9660], [8340, 0], [21044, 0], [0, 709], [0, 11010], [0, 12615], [8280, 0], [-18874, 0], [0, 5275], [0, -5500], [0, 5655], [17572, 0], [1604, 0], [-31320, 0], [0, 15650], [0, -5460], [0, -1605], [-11910, 0], [-3382, 0], [0, -7975], [0, -16095], [0, 11070], [-6172, 0], [-19170, 0], [21898, 0], [16680, 0], [0, 3259], [0, 11600], [16324, 0], [0, -60], [30548, 0], [0, 8476], [20220, 0], [20722, 0], [0, 2175], [0, 20986], [0, 19755], [16680, 0], [-15484, 0], [-4170, 0], [0, 15113], [0, 7380], [9934, 0], [-23520, 0], [0, -14908], [0, 17715], [36196, 0], [0, 240], [0, 14266], [20340, 0], [0, 5378], [0, -300], [0, 12700], [36240, 0], [-6572, 0], [2340, 0], [0, 1261], [52770, 0], [0, -14100], [0, -7785], [0, -4175], [-43650, 0], [0, -9405], [0, -12660], [-6736, 0], [0, -10250]];
    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_4_d_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_900_4_d_d(:prec:=2)
    chi := MakeCharacter_900_d();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 4));
    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_900_4_d_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_900_4_d_d( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_900_d();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<7,R![4, 0, 1]>,<11,R![30, 1]>],Snew);
    return Vf;
end function;