
// Make newform 570.4.i.f in Magma, downloaded from the LMFDB on 06 June 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_570_i();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_570_i_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_570_4_i_f();".
// 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_570_4_i_f();".
// 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, -1, 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_570_i();"
function MakeCharacter_570_i()
    N := 570;
    order := 3;
    char_gens := [191, 457, 211];
    v := [3, 3, 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_570_i_Hecke();"
function MakeCharacter_570_i_Hecke(Kf)
    N := 570;
    order := 3;
    char_gens := [191, 457, 211];
    char_values := [[1, 0], [1, 0], [0, -1]];
    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]];
    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 := [[2, -2], [3, -3], [5, -5], [-19, 0], [15, 0], [0, -50], [42, -42], [-95, 57], [0, 45], [0, 108], [-196, 0], [-43, 0], [-213, 213], [-338, 338], [0, -240], [0, 453], [180, -180], [0, -98], [0, -110], [-204, 204], [760, -760], [214, -214], [-198, 0], [0, -615], [52, -52], [0, -318], [-1003, 0], [-1122, 0], [-1046, 1046], [1590, 0], [0, 799], [-219, 219], [0, -2508], [0, 364], [-1302, 1302], [-682, 0], [2533, -2533], [3470, 0], [0, -1929], [1437, -1437], [909, 0], [0, -3836], [-2334, 0], [118, -118], [-4299, 0], [0, -1046], [763, -763], [-1901, 1901], [-1176, 0], [518, 0], [1794, -1794], [-3780, 0], [0, -2558], [0, -5292], [0, -6186], [6195, -6195], [5850, -5850], [1762, -1762], [-5722, 0], [0, -2967], [538, -538], [-1215, 0], [1708, -1708], [-1272, 0], [0, 7144], [0, -879], [5951, 0], [8422, -8422], [6546, -6546], [-11194, 0], [-114, 0], [-6072, 6072], [0, 8896], [-529, 0], [-1888, 0], [-7128, 7128], [0, 9564], [-7373, 7373], [1938, -1938], [0, -749], [-10641, 0], [-8030, 8030], [0, -3630], [0, 13468], [1354, -1354], [0, 5154], [12099, 0], [-1126, 0], [6642, -6642], [7985, 0], [10860, 0], [0, 14064], [-6415, 0], [-15993, 15993], [-9581, 9581], [0, 18867], [0, -12630], [-882, 0], [0, 4852], [0, -16388], [0, 23218], [0, -13593], [-16284, 0], [10755, 0], [-3052, 0], [9110, 0], [-21180, 21180], [0, 20424], [0, 13206], [1985, 0], [-20875, 0], [15847, -15847], [0, -672], [-8713, 0], [0, -8420], [-21762, 21762], [7570, -7570], [-21261, 0], [-11535, 0], [0, 19545], [0, -18884], [14792, 0], [14529, 0], [-2778, 0], [-16315, 0], [-3030, 3030], [0, -26378], [-1812, 1812], [25648, -25648], [32393, 0], [10099, -10099], [25845, -25845], [0, 1888], [25615, -25615], [-20505, 0], [0, -19346], [0, 12765], [28484, 0], [33399, 0], [21954, 0], [0, -2279], [0, 6354], [0, -19739], [0, 29940], [1976, 0], [-12750, 12750], [-4430, 4430], [10464, -10464], [0, -37595], [14331, 0], [48133, -48133], [1263, 0], [0, 4798], [0, 588], [25414, -25414], [3138, 0], [-35692, 0], [41037, -41037], [0, -12680], [0, -16212], [24174, -24174], [-45720, 45720], [-14540, 14540], [-29580, 29580], [23820, 0], [17373, -17373], [-47672, 47672], [0, 57529]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_570_i_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_570_4_i_f();".
// 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_570_4_i_f(:prec:=2)
    chi := MakeCharacter_570_i();
    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_570_4_i_f();".
// 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_570_4_i_f( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_570_i();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<7,R![19, 1]>],Snew);
    return Vf;
end function;