
// Make newform 600.8.f.a in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_600_f();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_600_f_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_600_8_f_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_600_8_f_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;
    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_600_f();"
function MakeCharacter_600_f()
    N := 600;
    order := 2;
    char_gens := [151, 301, 401, 577];
    v := [2, 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_600_f_Hecke();"
function MakeCharacter_600_f_Hecke(Kf)
    N := 600;
    order := 2;
    char_gens := [151, 301, 401, 577];
    char_values := [[1, 0], [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 := 8;
    raw_aps := [[0, 0], [0, -27], [0, 0], [0, 120], [-7196, 0], [0, 9626], [0, 18674], [-7004, 0], [0, 63704], [-29334, 0], [87968, 0], [0, 227982], [-160806, 0], [0, -136132], [0, -1206960], [0, 398786], [-1152436, 0], [-2070602, 0], [0, -4073428], [-383752, 0], [0, -3006010], [4948112, 0], [0, 9163492], [-7304106, 0], [0, -690526], [13266702, 0], [0, -4868248], [0, 7918596], [4930298, 0], [0, -2317074], [0, -22511936], [-13589524, 0], [0, 714618], [-17881572, 0], [20039074, 0], [-40763416, 0], [0, 55470166], [0, 1803444], [0, 5992728], [0, -125630598], [-48649788, 0], [-49754754, 0], [111324480, 0], [0, 134786494], [0, 39755694], [103320776, 0], [-179474660, 0], [0, -285310816], [0, -27479796], [85656242, 0], [0, -104906650], [27718384, 0], [-295271022, 0], [-302885260, 0], [0, 41463554], [0, -420007096], [-156477350, 0], [-271490512, 0], [0, -98031842], [-404838934, 0], [0, -241438676], [0, 195051954], [0, -102082116], [304912776, 0], [0, -647440842], [0, -148934538], [968290340, 0], [0, 165423666], [0, -1124551404], [255382890, 0], [0, -345150626], [811306536, 0], [0, -776624816], [0, -1667904830], [1250065420, 0], [0, -1433738880], [-731612782, 0], [0, 1190591078], [-2446377870, 0], [-1836634986, 0], [2778538932, 0], [1166249870, 0], [-290479408, 0], [0, -2206505938], [-99756360, 0], [0, 818947660], [2462406206, 0], [0, -2761354054], [1483079014, 0], [0, 1425912976], [0, 1995789468], [-566385056, 0], [0, -1236092264], [-749612412, 0], [3656423172, 0], [0, 2193895608], [-4248654774, 0], [-370617094, 0], [0, -6336450532], [-4007922858, 0], [0, -3660460852], [0, 5256297478], [0, 5019854276], [-2804950090, 0], [6104541876, 0], [0, -302864446], [0, -2423700188], [0, 6855380942], [-147202536, 0], [7158843562, 0], [0, 4083334880], [0, 6810805426], [0, 3951609114], [10783545596, 0], [11044348936, 0], [-5937968894, 0], [0, 8249448084], [0, -6491445064], [0, 1044060762], [-11112564828, 0], [-1701049570, 0], [0, 7459384478], [0, -5804728754], [0, 7269028028], [11729799356, 0], [-9887431434, 0], [14328441490, 0], [17360709968, 0], [0, -12773966424], [0, -6124190102], [-10533521804, 0], [0, -7553916376], [-13127324976, 0], [0, -5721255874], [16377144458, 0], [6596342782, 0], [0, 6241259154], [0, 16932756316], [0, 12557859094], [2518596646, 0], [27076006084, 0], [27008869950, 0], [0, -12388298312], [0, -22856233036], [8687419850, 0], [-11679172344, 0], [0, -2254521950], [0, -4966046166], [-15403923732, 0], [0, -4612253344], [0, 35750464134], [-24830856430, 0], [0, -9379955708], [0, 5989397576], [0, 1765682788], [-41823154064, 0], [37165770776, 0], [-25657994210, 0], [0, -5753525286], [-46059907194, 0], [0, -93470916], [0, -11728910538], [0, -16478577352], [-18843582684, 0], [0, -42666068430], [0, -4345270248], [13623873760, 0], [0, -43278776882]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_600_f_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_600_8_f_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_600_8_f_a(:prec:=2)
    chi := MakeCharacter_600_f();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 8));
    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_600_8_f_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_600_8_f_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_600_f();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,8,sign)));
    Vf := Kernel([<7,R![14400, 0, 1]>],Snew);
    return Vf;
end function;