
// Make newform 294.4.e.e in Magma, downloaded from the LMFDB on 14 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_4_e_e();".
// 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_e();".
// 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 := 4;
    raw_aps := [[0, 2], [-3, 3], [0, -15], [0, 0], [9, -9], [88, 0], [-84, 84], [0, 104], [0, 84], [51, 0], [185, -185], [0, -44], [168, 0], [326, 0], [0, -138], [-639, 639], [159, -159], [0, 722], [166, -166], [1086, 0], [218, -218], [0, 583], [597, 0], [0, -1038], [169, 0], [642, -642], [0, 464], [0, -393], [-14, 14], [-2184, 0], [-373, 0], [0, -1173], [-30, 30], [82, 0], [0, 1434], [2671, -2671], [2252, -2252], [0, -1676], [-3030, 0], [0, 3438], [1212, -1212], [-3032, 0], [0, -2520], [-365, 365], [-1590, 0], [-5380, 5380], [-5362, 0], [1573, 0], [921, -921], [0, 4052], [0, 468], [4932, 0], [-1537, 1537], [-5319, 0], [0, 5346], [-774, 774], [2415, -2415], [0, -475], [-3728, 3728], [1602, 0], [686, -686], [1101, 0], [-2780, 0], [4296, -4296], [0, 5489], [0, -4491], [0, 3964], [161, 0], [5916, -5916], [142, 0], [-4440, 4440], [0, -2286], [-2869, 2869], [0, 3064], [-6040, 0], [0, 1842], [-7830, 7830], [0, -14764], [0, -6264], [4751, -4751], [4704, 0], [-4474, 0], [12804, -12804], [5074, 0], [0, -1267], [0, 6933], [11688, 0], [0, -551], [-13386, 0], [-6376, 0], [0, 5700], [19794, -19794], [-15935, 15935], [9963, 0], [0, -19142], [12192, 0], [0, -19809], [-1794, 1794], [0, -6448], [0, -7262], [14204, 0], [-15825, 15825], [1059, -1059], [0, -3960], [2530, -2530], [11831, -11831], [-4809, 0], [0, 21804], [-14166, 14166], [-5891, 0], [0, -2737], [26188, -26188], [-2358, 0], [13766, -13766], [21287, 0], [-21426, 21426], [-9962, 0], [-18174, 18174], [0, 19167], [13080, 0], [-15190, 15190], [4397, 0], [0, 4029], [-15021, 15021], [0, -13984], [-31053, 0], [0, -15086], [0, -6378], [7363, 0], [0, 32810], [24034, -24034], [-8022, 0], [0, -29519], [-3742, 0], [0, -10896], [-17285, 0], [11826, -11826], [17714, -17714], [24939, 0], [29064, -29064], [15370, 0], [0, -44031], [4192, -4192], [33195, 0], [16448, -16448], [-16860, 0], [-29054, 0], [41958, -41958], [0, 5546], [0, 32538], [0, -32096], [8490, 0], [-48352, 0], [0, 15492], [8116, -8116], [-4446, 0], [0, -26504], [0, -5430], [-33803, 0], [-48483, 48483], [0, -37296], [-38478, 0], [27257, 0], [0, 34341], [-27426, 27426], [12324, -12324], [-47597, 47597], [-11242, 11242]];
    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_e();".
// 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_e(:prec:=2)
    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_e();".
// 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_e( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_294_e();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![225, 15, 1]>,<11,R![81, -9, 1]>],Snew);
    return Vf;
end function;