
// Make newform 49.6.a.f in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_49_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_49_6_a_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_49_6_a_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 := [-14, -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_a();"
function MakeCharacter_49_a()
    N := 49;
    order := 1;
    char_gens := [3];
    v := [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_49_a_Hecke(Kf)
    return MakeCharacter_49_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 := 6;
    raw_aps := [[5, -1], [6, -6], [4, 10], [0, 0], [136, 124], [112, 126], [-862, -76], [1642, -18], [1328, -568], [3474, -252], [-260, 540], [3386, -540], [3570, -1092], [-3904, 4788], [-7724, 3748], [4630, 208], [22994, -2050], [34780, -4806], [11420, 1944], [46608, 4200], [-6098, -5256], [33080, -14904], [-66654, 15750], [-31034, -22208], [-14798, 8820], [64196, 27434], [-19988, -10476], [-54532, -21904], [-96982, -3348], [59166, -23940], [-96976, -8064], [249946, 23278], [166498, 51448], [-214970, 46242], [31022, -33496], [41576, -2592], [-176240, -81234], [-463192, 5508], [-72156, -48468], [-10328, -20906], [24580, 71440], [61432, 103950], [-279736, 50504], [-207946, 158436], [-90354, 83832], [98692, 118476], [570236, -54936], [-747320, -65016], [451226, 12398], [-97856, 284742], [266810, 117872], [-218328, 10920], [-633326, 237924], [1629894, -13902], [-754610, -8432], [73552, 91432], [-1889560, -25738], [270736, -440640], [360230, -571104], [520410, -351456], [829954, 132462], [-585060, -462462], [-654038, 359982], [519224, -542800], [1526914, -579492], [2986310, 13208], [-405568, -418860], [-970282, -337932], [-270848, -191228], [-1132040, -388458], [-3069298, 125840], [1465448, -87760], [920296, -259272], [-3134770, 313704], [1906424, 393372], [3813604, -91004], [2620138, -610892], [-3418040, 505494], [-219466, 385076], [3896794, -194508], [44814, 47586], [1787270, 976752], [-1531352, -796280], [-1626758, -591444], [-3115304, 782064], [1437812, -800584], [-2811534, 1268400], [-761602, -780732], [2334528, 67662], [-1144288, -734832], [1602850, -606554], [249500, -661828], [-1546528, 1113768], [-358188, -1755096], [-2453572, 257256], [4357584, -1921752], [6153592, 1041526], [1167410, 1874300], [-5296070, -879498], [5549990, 1550520], [418448, -2204244], [-1871258, 799048], [6376838, 1286090], [-7086130, 1296644], [-3522040, -817812], [3598846, 415584], [-5965554, 1878954], [-2158250, 1784296], [13030216, -476240], [-1977626, -3251808], [5221024, -2468736], [-2537350, -1492668], [9635502, 84], [-2528366, -2256498], [-9411904, -859320], [7992022, -1972700], [10577686, -1494486], [-17583196, -280468], [-8214502, 1566620], [5747856, 2978556], [-1606340, 2807442], [1389866, -2907576], [11259448, -707522], [1432084, 4306888], [-3003206, 1256958], [-12474054, -750708], [-3409366, 1659636], [3059116, 2943580], [-18731972, 33012], [-9245204, -3672918], [-24635608, 79812], [-379272, 1063104], [-6446680, -4022712], [18842714, -896364], [13288762, -3792044], [1412362, -3671892], [-12573604, 5491298], [12299986, 3121686], [35784, -1816794], [3000238, -5095148], [-906374, 2319030], [-4796458, -1048720], [-5467816, -1254528], [-3198900, -4052496], [9362380, 1136682], [10621884, -309372], [-14815472, 4184838], [-9363334, -5899564], [1383526, -4137102], [32942264, -1695016], [19641410, 3126276], [-1608474, -154056], [13146212, 1551312], [8503660, -1980668], [9376412, 1603152], [11539152, -8821008], [14135672, -2356992], [-7652606, 925732], [30900142, -2751336], [-5996068, -1426774], [-24146656, 1064300], [925962, 9684528], [2260400, -203112], [-20742158, -5484434], [-20071958, -8415080], [11531372, 9011780], [28009712, -2830032], [-46077620, 813618]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_49_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_49_6_a_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_49_6_a_f(:prec:=2)
    chi := MakeCharacter_49_a();
    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_a_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_49_6_a_f( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_49_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,6,sign)));
    Vf := Kernel([<2,R![6, -9, 1]>,<3,R![-504, -6, 1]>],Snew);
    return Vf;
end function;