
// Make newform 245.4.a.h in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_245_a();"
// To make the coeffs of the qexp of the newform in the Hecke field type "qexpCoeffs();"
// To make the newform (type ModFrm), type "MakeNewformModFrm_245_4_a_h();".
// 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_245_4_a_h();".
// 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 := [-2, 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_245_a();"
function MakeCharacter_245_a()
    N := 245;
    order := 1;
    char_gens := [197, 101];
    v := [1, 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;

function MakeCharacter_245_a_Hecke(Kf)
    return MakeCharacter_245_a();
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 := [[-3, 1], [1, 3], [-5, 0], [0, 0], [14, 10], [18, -10], [38, -54], [-80, -26], [-11, -117], [-125, -60], [66, 100], [-208, 136], [53, -30], [-333, 5], [98, 64], [-476, 142], [-420, 266], [-49, -570], [-643, 69], [532, 350], [86, 118], [-620, -214], [953, 5], [-325, 324], [314, 500], [-709, -180], [1251, 387], [617, -239], [535, 174], [332, -650], [-194, 1240], [296, 960], [-868, 404], [-450, 150], [975, 1246], [-2206, -960], [-1102, -104], [-986, -692], [-991, 1365], [3336, -448], [-1270, -116], [-1297, -1000], [-1246, -1590], [814, -1728], [326, 260], [700, 2196], [292, -650], [-1082, 2180], [-382, 1396], [-210, 2924], [2314, -102], [690, 260], [1306, -2620], [-62, 2740], [68, -2516], [3169, 2797], [-1815, 1586], [-5544, -1870], [562, -3346], [1822, -380], [-954, -2712], [-3872, -2200], [3519, -1025], [1456, -2530], [-2564, 732], [4342, 566], [-6566, 560], [-1404, 650], [-6623, -831], [945, -200], [-4304, -1642], [-910, 7996], [-7947, 4031], [-2676, 2028], [-2600, -1390], [-3589, 1557], [3050, 1084], [8418, 384], [259, 8040], [3195, -5314], [-8220, 280], [6817, 90], [4714, -1030], [1428, 10910], [-10080, 1504], [-4241, -3867], [-6455, -3750], [10732, -5644], [3098, -40], [4737, 485], [-5397, 7739], [-1830, 496], [-1818, 4684], [-13838, 980], [-14210, 1706], [7303, -7215], [-10935, 1024], [17666, 1900], [-6474, -1948], [-471, -160], [-10359, 1645], [-4238, 12604], [-12539, -3307], [1250, -13924], [-8956, -1290], [14298, -944], [17794, -1140], [7056, -5518], [-3340, 5084], [-6702, 3460], [11373, -8421], [3674, 7822], [5136, 3160], [9010, -294], [6622, -15150], [6959, -11490], [3078, -860], [-6067, -16589], [9834, -3282], [-5560, -4370], [-7449, -950], [7032, 16500], [11118, 4454], [319, 6017], [-5554, -3590], [4877, 17850], [13415, -6364], [4510, -15306], [8469, -3675], [-14754, 7272], [390, -2026], [437, 6225], [-2556, 5380], [-14374, -16550], [12466, -14120], [-790, -16560], [-5114, -1702], [20603, -3169], [12424, 14000], [-11115, -9496], [-28542, 6900], [11014, 5000], [12969, -3913], [7261, 13595], [27670, -5864], [12410, -5490], [-28932, -2570], [-19702, 13836], [12440, -1456], [469, 17593], [-13788, -6174], [-407, 9070], [-29918, -11380], [32953, -3441], [-14703, -11141], [-9498, -12270], [-16580, -15214], [-10325, -18406], [2964, -28880], [-29094, -17700], [-21603, -11839], [-17048, 1380], [-12889, 14005], [-16664, -16160], [-18358, -14496], [-8149, 6123], [-7486, -20320], [29248, -5934]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_245_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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