
// Make newform 1568.3.d.e in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1568_d();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_1568_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_1568_3_d_e();".
// 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_1568_3_d_e();".
// 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_1568_d();"
function MakeCharacter_1568_d()
    N := 1568;
    order := 2;
    char_gens := [1471, 197, 1473];
    v := [1, 2, 2];
    // 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_1568_d_Hecke();"
function MakeCharacter_1568_d_Hecke(Kf)
    N := 1568;
    order := 2;
    char_gens := [1471, 197, 1473];
    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 := 3;
    raw_aps := [[0, 0], [0, 5], [9, 0], [0, 0], [0, 3], [16, 0], [-7, 0], [0, -11], [0, 19], [-32, 0], [0, 11], [-1, 0], [-40, 0], [0, 40], [0, 85], [7, 0], [0, 53], [79, 0], [0, -11], [0, 48], [143, 0], [0, 35], [0, 8], [-97, 0], [-88, 0], [-15, 0], [0, -93], [0, -11], [113, 0], [56, 0], [0, -96], [0, -141], [9, 0], [0, 136], [57, 0], [0, 27], [167, 0], [0, 19], [0, -190], [-145, 0], [0, -317], [-186, 0], [0, -283], [-295, 0], [128, 0], [0, 283], [0, 40], [0, -176], [0, 387], [-183, 0], [361, 0], [0, -50], [-129, 0], [0, -394], [89, 0], [0, 45], [-457, 0], [0, -397], [231, 0], [104, 0], [0, -397], [154, 0], [0, -376], [0, -323], [191, 0], [335, 0], [0, 35], [184, 0], [0, -187], [-208, 0], [-231, 0], [0, 37], [0, -483], [431, 0], [0, 714], [0, -651], [465, 0], [185, 0], [105, 0], [-7, 0], [0, 392], [64, 0], [0, 731], [376, 0], [0, -635], [0, -491], [-344, 0], [527, 0], [-704, 0], [0, 160], [0, -541], [0, 477], [0, -307], [0, -22], [0, 915], [0, 192], [-71, 0], [703, 0], [0, 325], [265, 0], [0, 134], [-575, 0], [0, 181], [319, 0], [0, 163], [719, 0], [0, -296], [361, 0], [0, 125], [1000, 0], [0, -411], [-1039, 0], [-248, 0], [0, -603], [0, -816], [639, 0], [0, 664], [0, 1261], [-593, 0], [0, -1192], [-473, 0], [824, 0], [-263, 0], [0, 643], [0, -125], [-54, 0], [1151, 0], [0, 403], [0, 496], [-161, 0], [0, 547], [0, -256], [0, -1131], [-336, 0], [345, 0], [568, 0], [-559, 0], [0, 661], [-736, 0], [287, 0], [0, -1272], [-743, 0], [0, -227], [0, -120], [241, 0], [0, 880], [176, 0], [255, 0], [0, 645], [0, 835], [-1143, 0], [-930, 0], [0, 1320], [0, 1061], [0, -891], [0, -464], [0, 1445], [-1601, 0], [1554, 0], [-985, 0], [0, 323], [-168, 0], [0, -112], [0, -1885], [25, 0], [0, -85], [0, 157], [-297, 0], [-1560, 0], [-753, 0], [0, 789], [560, 0], [0, -93], [1503, 0], [0, -1637], [440, 0], [0, -586], [-41, 0], [0, -1152], [809, 0], [0, -1357], [0, -712], [-730, 0], [239, 0], [0, 1325], [1607, 0], [-1999, 0], [0, 867], [1065, 0], [0, -277], [-1121, 0], [0, -1592], [0, -2011], [1721, 0], [0, -1565], [-1927, 0], [1033, 0], [-1271, 0], [-1352, 0], [0, -13], [1025, 0], [0, 992], [-2049, 0], [1193, 0], [0, -1032], [727, 0], [0, 1747], [0, 1349], [-1070, 0], [0, -523], [-1735, 0], [-224, 0], [0, -1696], [0, 691], [0, -2405], [-1639, 0], [0, 1627], [-1383, 0], [0, 101], [-886, 0], [729, 0], [0, -1118], [2031, 0], [0, 1813], [0, -776], [1312, 0], [1769, 0], [0, -371], [0, -1099], [0, 1555], [-2569, 0], [0, -2411], [0, 848], [-935, 0], [0, 2680], [0, 2541], [-1431, 0], [-55, 0], [0, 2408], [0, -450], [0, 213], [0, 1637], [0, -1027], [1663, 0], [1442, 0], [0, -829], [0, -1056], [0, 1859], [0, -77], [0, -1614], [-368, 0], [95, 0], [0, -3061], [-1576, 0], [-329, 0], [0, 2981], [-745, 0], [0, 1059], [-1920, 0], [-1703, 0], [0, 2061], [0, -616], [-31, 0], [-1824, 0], [-2071, 0], [0, -139], [-960, 0], [1592, 0], [0, -2392], [-1327, 0], [1689, 0], [0, 1027], [-1527, 0], [0, 1765], [-424, 0], [0, -941], [0, -989], [2775, 0], [-209, 0], [0, -2011], [0, 2315], [0, -1157], [0, -16], [2192, 0], [0, -1179], [0, -139], [-305, 0], [320, 0], [0, -1635], [2088, 0], [129, 0], [0, -2237], [1071, 0], [0, -1366], [-1168, 0], [-2671, 0], [0, -1099], [2032, 0], [0, -1645], [0, 454], [2943, 0], [2761, 0], [0, 1723]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1568_d_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1568_3_d_e();".
// 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_1568_3_d_e(:prec:=2)
    chi := MakeCharacter_1568_d();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 3));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(1999) - 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_1568_3_d_e();".
// 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_1568_3_d_e( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1568_d();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,3,sign)));
    Vf := Kernel([<3,R![25, 0, 1]>,<5,R![-9, 1]>],Snew);
    return Vf;
end function;