
// Make newform 294.6.e.d in Magma, downloaded from the LMFDB on 01 May 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_6_e_d();".
// 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_6_e_d();".
// 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_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, -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, -4], [9, -9], [0, -76], [0, 0], [-650, 650], [762, 0], [556, -556], [0, 2452], [0, 2950], [-674, 0], [3024, -3024], [0, -7730], [-17016, 0], [21836, 0], [0, 23940], [-15594, 15594], [-5608, 5608], [0, -150], [43784, -43784], [-39178, 0], [23570, -23570], [0, 17892], [38972, 0], [0, -6024], [108430, 0], [70424, -70424], [0, 31552], [0, -108282], [72146, -72146], [220906, 0], [-239652, 0], [0, 274172], [391154, -391154], [339364, 0], [0, 29334], [-71608, 71608], [-296318, 296318], [0, 480400], [160180, 0], [0, 8984], [-182886, 182886], [138330, 0], [0, -327222], [-786902, 786902], [423098, 0], [-1023920, 1023920], [461516, 0], [995048, 0], [95568, -95568], [0, 1044086], [0, 1169410], [-27342, 0], [907714, -907714], [44088, 0], [0, 829200], [-1319466, 1319466], [783788, -783788], [0, -955080], [-1912730, 1912730], [-1026202, 0], [-1746676, 1746676], [2232120, 0], [1853236, 0], [450956, -450956], [0, -1602634], [0, 20862], [0, -2076212], [1205082, 0], [876642, -876642], [-1295930, 0], [3990396, -3990396], [0, -4064518], [1672432, -1672432], [0, -3167686], [-4203884, 0], [0, 342616], [3839594, -3839594], [0, -3438942], [0, 3894210], [1646794, -1646794], [-1676592, 0], [-566742, 0], [-6684678, 6684678], [6913370, 0], [0, 4562808], [0, -4597598], [1706578, 0], [0, 6939158], [-2618048, 0], [7136020, 0], [0, 2173976], [4632940, -4632940], [4566448, -4566448], [-5314290, 0], [0, 2463140], [2799240, 0], [0, -1999140], [-3521604, 3521604], [0, -2606852], [0, 1374414], [-8783976, 0], [-6292618, 6292618], [-4865816, 4865816], [0, 4463826], [-8170536, 8170536], [5503430, -5503430], [8142512, 0], [0, 2731356], [-1237326, 1237326], [-15975598, 0], [0, 1882752], [9828042, -9828042], [-8212618, 0], [-6984652, 6984652], [12678884, 0], [14032378, -14032378], [1303676, 0], [-1571100, 1571100], [0, 8341146], [6183338, 0], [-928966, 928966], [17913086, 0], [0, 4963968], [-89526, 89526], [0, 142396], [10393486, 0], [0, -4655030], [0, 6721344], [-12407648, 0], [0, -13595802], [-2568192, 2568192], [-20213274, 0], [0, -7048128], [-20411994, 0], [0, 5079736], [23389786, 0], [1112528, -1112528], [-2008116, 2008116], [30089688, 0], [1882070, -1882070], [4882204, 0], [0, -8370962], [20209028, -20209028], [-13105926, 0], [-31866706, 31866706], [9947420, 0], [-6526114, 0], [8762376, -8762376], [0, -6479420], [0, 18341654], [0, -26906494], [-15217380, 0], [-26152036, 0], [0, 10802076], [9841668, -9841668], [27250902, 0], [0, -28643152], [0, -6784920], [30030818, 0], [-23072452, 23072452], [0, -27143314], [-16155190, 0], [-38032252, 0], [0, 22310364], [-30693006, 30693006], [15270576, -15270576], [-31627928, 31627928], [35584234, -35584234]];
    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_6_e_d();".
// 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_6_e_d(:prec:=2)
    chi := MakeCharacter_294_e();
    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_294_6_e_d();".
// 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_6_e_d( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_294_e();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,6,sign)));
    Vf := Kernel([<5,R![5776, 76, 1]>,<11,R![422500, 650, 1]>],Snew);
    return Vf;
end function;