
// Make newform 294.4.e.o in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_294_e();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_294_e_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_294_4_e_o();".
// 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_294_4_e_o();".
// 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 := [4, 0, 2, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 7, 0, 0], [0, 0, 1, 0], [0, 0, 0, 7]];
    Rf_basisdens := [1, 1, 2, 2];
    Rf_basisnums := ChangeUniverse([[z : z in elt] : elt in Rf_num], Kf);
    Rfbasis := [Rf_basisnums[i]/Rf_basisdens[i] : i in [1..Degree(Kf)]];
    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_294_e();"
function MakeCharacter_294_e()
    N := 294;
    order := 3;
    char_gens := [197, 199];
    v := [3, 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_294_e_Hecke();"
function MakeCharacter_294_e_Hecke(Kf)
    N := 294;
    order := 3;
    char_gens := [197, 199];
    char_values := [[1, 0, 0, 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 := 4;
    raw_aps := [[2, 0, 2, 0], [0, 0, -3, 0], [-6, -1, -6, 0], [0, 0, 0, 0], [0, 6, -2, 6], [24, 0, 0, 3], [0, 1, 42, 1], [36, -2, 36, 0], [154, 6, 154, 0], [-40, 0, 0, 18], [0, 6, 192, 6], [-268, -12, -268, 0], [378, 0, 0, 5], [200, 0, 0, 24], [-156, -10, -156, 0], [0, 24, -26, 24], [0, -2, 432, -2], [-708, -13, -708, 0], [0, -24, 72, -24], [-762, 0, 0, -30], [0, -83, 372, -83], [-488, 84, -488, 0], [-156, 0, 0, 136], [-54, -29, -54, 0], [-372, 0, 0, -125], [0, 87, -618, 87], [96, -106, 96, 0], [454, 90, 454, 0], [0, 96, 784, 96], [966, 0, 0, -48], [-716, 0, 0, -192], [60, 24, 60, 0], [0, 30, 324, 30], [-12, 0, 0, -92], [1154, 192, 1154, 0], [0, -108, 1004, -108], [0, -157, 12, -157], [1208, 132, 1208, 0], [-1380, 0, 0, -86], [-1002, 111, -1002, 0], [0, -114, 594, -114], [-216, 0, 0, 189], [-1190, -222, -1190, 0], [0, -96, -958, -96], [314, 0, 0, 240], [0, -204, -1152, -204], [756, 0, 0, -60], [-1608, 0, 0, -300], [0, 18, -2040, 18], [-3072, -231, -3072, 0], [-36, -546, -36, 0], [1278, 0, 0, 246], [0, -153, 5484, -153], [-1128, 0, 0, 490], [1710, 297, 1710, 0], [0, 66, 3042, 66], [0, 23, 6930, 23], [-456, 454, -456, 0], [0, 48, -3230, 48], [4276, 0, 0, 510], [0, 622, 588, 622], [-1794, 0, 0, 13], [-3660, 0, 0, -166], [0, -418, -3012, -418], [8448, 15, 8448, 0], [3566, -72, 3566, 0], [44, -168, 44, 0], [-784, 0, 0, 252], [0, -594, 3030, -594], [5052, 0, 0, 33], [0, -125, -5490, -125], [1214, 618, 1214, 0], [0, 64, -3408, 64], [2462, -504, 2462, 0], [-4724, 0, 0, -792], [2904, -140, 2904, 0], [0, -450, 1208, -450], [-8196, 643, -8196, 0], [3564, -18, 3564, 0], [0, 645, 2868, 645], [-2016, 0, 0, -562], [1434, 0, 0, -648], [0, -942, 4878, -942], [-1560, 0, 0, 549], [-4200, 360, -4200, 0], [-2594, 714, -2594, 0], [-6666, 0, 0, -432], [-4590, 912, -4590, 0], [-1062, 0, 0, 1315], [2668, 0, 0, -204], [-5616, -286, -5616, 0], [0, -782, 3372, -782], [0, -540, -9132, -540], [-138, 0, 0, -954], [3328, 900, 3328, 0], [-4296, 0, 0, -992], [-3354, 1357, -3354, 0], [0, 571, 54, 571], [11964, -132, 11964, 0], [13514, -960, 13514, 0], [-6572, 0, 0, 1116], [0, -600, -4398, -600], [0, -1874, -5472, -1874], [884, 1242, 884, 0], [0, 276, 5044, 276], [0, -249, 22632, -249], [5712, 0, 0, 1422], [15198, 1255, 15198, 0], [0, 1350, -12826, 1350], [-2040, 0, 0, -909], [9576, 780, 9576, 0], [0, 708, -7848, 708], [-4220, 0, 0, 1194], [0, -1420, 8628, -1420], [-6916, 0, 0, -516], [0, -870, 2708, -870], [12468, 0, 0, 442], [0, 630, 13956, 630], [25992, -486, 25992, 0], [19086, 0, 0, -942], [0, -69, 17388, -69], [8772, 0, 0, 1800], [-12618, 2067, -12618, 0], [0, 1830, 7650, 1830], [-18972, -576, -18972, 0], [7104, 0, 0, -2250], [-28456, -216, -28456, 0], [-12480, 1064, -12480, 0], [-16008, 0, 0, -26], [7608, 681, 7608, 0], [0, -1452, -3384, -1452], [-25802, 0, 0, 342], [2968, -1692, 2968, 0], [18896, 0, 0, -1824], [5226, 2383, 5226, 0], [-20088, 0, 0, 525], [0, 1769, 906, 1769], [0, 400, 7116, 400], [-4338, 0, 0, -927], [0, -240, -29134, -240], [-22692, 0, 0, 684], [7902, -1152, 7902, 0], [0, 1980, 20392, 1980], [9402, 0, 0, 2034], [0, -443, -10152, -443], [18540, 0, 0, 1130], [732, 0, 0, -1875], [0, 1289, -26922, 1289], [-3012, -526, -3012, 0], [-17246, 1482, -17246, 0], [-29912, -108, -29912, 0], [-28566, 0, 0, 1335], [-16600, 0, 0, 2292], [-2724, -314, -2724, 0], [0, 2508, 17560, 2508], [-3130, 0, 0, -546], [21068, 1032, 21068, 0], [-15318, -1705, -15318, 0], [17136, 0, 0, -521], [0, -2849, -3606, -2849], [-4074, -1506, -4074, 0], [4978, 0, 0, -432], [28860, 0, 0, 1368], [-18948, -352, -18948, 0], [0, 1710, -17556, 1710], [0, 1904, 26184, 1904], [0, -1104, -21052, -1104], [0, -2237, 34524, -2237]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_294_e_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_294_4_e_o();".
// 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_294_4_e_o(:prec:=4)
    chi := MakeCharacter_294_e();
    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_294_4_e_o();".
// 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_294_4_e_o( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_294_e();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![3844, -744, 206, 12, 1]>,<11,R![12418576, 14096, 3540, -4, 1]>],Snew);
    return Vf;
end function;