
// Make newform 49.6.c.b in Magma, downloaded from the LMFDB on 23 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_49_c();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_49_c_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_49_6_c_b();".
// 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_49_6_c_b();".
// 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_49_c();"
function MakeCharacter_49_c()
    N := 49;
    order := 3;
    char_gens := [3];
    v := [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_49_c_Hecke();"
function MakeCharacter_49_c_Hecke(Kf)
    N := 49;
    order := 3;
    char_gens := [3];
    char_values := [[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+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 := 6;
    raw_aps := [[0, 10], [-14, 14], [0, -56], [0, 0], [-232, 232], [140, 0], [-1722, 1722], [0, -98], [0, -1824], [3418, 0], [-7644, 7644], [0, 10398], [17962, 0], [10880, 0], [0, 9324], [-2262, 2262], [-2730, 2730], [0, 25648], [48404, -48404], [-58560, 0], [68082, -68082], [0, -31784], [20538, 0], [0, -50582], [58506, 0], [38696, -38696], [0, 53060], [0, 146324], [-92898, 92898], [-83354, 0], [60384, 0], [0, -61586], [204462, -204462], [35406, 0], [0, 20226], [-70904, 70904], [293524, -293524], [0, -13192], [-493612, 0], [0, 240716], [-294932, 294932], [336980, 0], [0, -358264], [989554, -989554], [-990050, 0], [-840756, 840756], [1150732, 0], [824264, 0], [74382, -74382], [0, 1131956], [0, 198726], [482904, 0], [805910, -805910], [-430738, 0], [0, -1176910], [-1290976, 1290976], [-1277556, 1277556], [0, 1650544], [1064090, -1064090], [-22342, 0], [-2495738, 2495738], [1931776, 0], [459074, 0], [667128, -667128], [0, -111034], [0, 68778], [0, 564448], [2077294, 0], [53248, -53248], [2272004, 0], [4006450, -4006450], [0, -73784], [1404312, -1404312], [0, 1603234], [-4770120, 0], [0, -2230788], [-4840242, 4840242], [0, 995820], [0, 3316050], [3072734, -3072734], [-2814378, 0], [3058022, 0], [-1937496, 1937496], [-3947902, 0], [0, -7417704], [0, -1402692], [-590574, 0], [0, 2904842], [922684, 0], [7182352, 0], [0, -612570], [2603300, -2603300], [-5463088, 5463088], [1640900, 0], [0, -2997964], [6894048, 0], [0, 2304764], [-12096042, 12096042], [0, 5484430], [0, 6717994], [-5002352, 0], [-9019606, 9019606], [12405106, -12405106], [0, -6488038], [10228504, -10228504], [2653378, -2653378], [14304374, 0], [0, -10026534], [7522920, -7522920], [-3386250, 0], [0, -6908608], [9688958, -9688958], [-7847418, 0], [-10197194, 10197194], [-8362576, 0], [-1102830, 1102830], [-17135426, 0], [-54964, 54964], [0, 485166], [-2721360, 0], [-2145248, 2145248], [2927962, 0], [0, -13499220], [5429724, -5429724], [0, 20827982], [23514066, 0], [0, 19574670], [0, -26115180], [-15412628, 0], [0, -16986816], [-2015112, 2015112], [-15138072, 0], [0, -7214008], [-10969726, 0], [0, 19244190], [-8211854, 0], [18618656, -18618656], [26250070, -26250070], [10037300, 0], [-14088390, 14088390], [-18143342, 0], [0, 21366922], [-17801736, 17801736], [16292124, 0], [-2084992, 2084992], [22784972, 0], [22697500, 0], [25289950, -25289950], [0, -10394734], [0, -43339944], [0, -37165930], [-9047850, 0], [32967860, 0], [0, -16109884], [44728564, -44728564], [-6605184, 0], [0, 30892984], [0, -4872154], [32500398, 0], [-2640400, 2640400], [0, 40817920], [-6719830, 0], [-2789792, 0], [0, 33359382], [7600326, -7600326], [-5797596, 5797596], [-12682496, 12682496], [14440048, -14440048]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_49_c_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_49_6_c_b();".
// 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_49_6_c_b(:prec:=2)
    chi := MakeCharacter_49_c();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 6));
    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_49_6_c_b();".
// 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_49_6_c_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_49_c();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,6,sign)));
    Vf := Kernel([<2,R![100, -10, 1]>,<3,R![196, 14, 1]>],Snew);
    return Vf;
end function;