
// Make newform 294.3.b.i in Magma, downloaded from the LMFDB on 16 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_294_b();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_294_b_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_3_b_i();".
// 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_3_b_i();".
// 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 := [3, -10, 11, -2, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [3, 8, 0, 1], [9, -19, 3, -2], [-9, 22, -3, 2]];
    Rf_basisdens := [1, 3, 3, 3];
    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_b();"
function MakeCharacter_294_b()
    N := 294;
    order := 2;
    char_gens := [197, 199];
    v := [1, 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_b_Hecke();"
function MakeCharacter_294_b_Hecke(Kf)
    N := 294;
    order := 2;
    char_gens := [197, 199];
    char_values := [[-1, 0, 0, 0], [1, 0, 0, 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 := 3;
    raw_aps := [[0, 0, 1, 0], [0, 1, 1, 1], [1, 0, 2, -2], [0, 0, 0, 0], [5, 0, 1, -10], [-8, 4, 2, 0], [-1, 0, -2, 2], [-1, -6, -3, 0], [5, 0, 7, -10], [10, 0, -10, -20], [-5, 10, 5, 0], [-1, 0, 0, 0], [-6, 0, 6, 12], [26, 12, 6, 0], [3, 0, -45, -6], [-3, 0, 36, 6], [7, 0, -55, -14], [-53, -8, -4, 0], [-39, 26, 13, 0], [-4, 0, -68, 8], [-33, 8, 4, 0], [13, 34, 17, 0], [8, 0, -44, -16], [-33, 0, -36, 66], [72, 20, 10, 0], [19, 0, -22, -38], [89, 42, 21, 0], [-9, 0, 3, 18], [57, -4, -2, 0], [-26, 0, -10, 52], [-50, 4, 2, 0], [25, 0, -13, -50], [-29, 0, 26, 58], [6, -28, -14, 0], [-3, 0, 6, 6], [111, 26, 13, 0], [-1, 0, 0, 0], [-67, -78, -39, 0], [-26, 0, 80, 52], [19, 0, 20, -38], [69, 0, 9, -138], [-170, 64, 32, 0], [-21, 0, -21, 42], [-19, -156, -78, 0], [2, 0, 70, -4], [-1, 18, 9, 0], [278, -12, -6, 0], [86, 36, 18, 0], [-51, 0, -123, 102], [21, -124, -62, 0], [29, 0, 154, -58], [18, 0, 0, -36], [-253, -24, -12, 0], [-30, 0, 120, 60], [-39, 0, 42, 78], [51, 0, -27, -102], [25, 0, 188, -50], [-59, 58, 29, 0], [-49, 24, 12, 0], [-46, 0, 226, 92], [99, -94, -47, 0], [144, 0, 0, -288], [-114, -4, -2, 0], [31, 0, 209, -62], [-297, 104, 52, 0], [-9, 0, -240, 18], [-235, 42, 21, 0], [148, 4, 2, 0], [-105, 0, 57, 210], [-12, -76, -38, 0], [27, 0, -138, -54], [-5, 0, 287, 10], [269, 42, 21, 0], [267, -16, -8, 0], [-72, 188, 94, 0], [-157, 0, -65, 314], [-29, 0, -118, 58], [-275, -140, -70, 0], [-147, 0, -138, 294], [305, 132, 66, 0], [-60, 0, -408, 120], [-452, 124, 62, 0], [-15, 0, -303, 30], [212, -132, -66, 0], [247, -206, -103, 0], [167, 0, -65, -334], [58, 0, -346, -116], [-213, -64, -32, 0], [-110, 0, -178, 220], [474, -52, -26, 0], [157, 0, -1, -314], [71, 0, -335, -142], [53, -150, -75, 0], [102, 0, 96, -204], [249, 98, 49, 0], [168, 0, 228, -336], [45, 0, -402, -90], [-87, 0, 228, 174], [-37, -198, -99, 0], [301, 268, 134, 0], [-296, -260, -130, 0], [91, 0, 86, -182], [-37, 0, -371, 74], [-105, 0, -216, 210], [-545, -14, -7, 0], [-501, -184, -92, 0], [44, 0, -224, -88], [105, 0, -138, -210], [-129, 0, -207, 258], [368, -204, -102, 0], [391, -14, -7, 0], [37, 52, 26, 0], [-94, 0, -50, 188], [513, 218, 109, 0], [290, -156, -78, 0], [-139, 0, 352, 278], [542, -276, -138, 0], [75, 0, 489, -150], [303, 0, 96, -606], [-124, 0, 340, 248], [339, -376, -188, 0], [-448, 12, 6, 0], [-219, 0, -30, 438], [-75, 0, -27, 150], [917, -6, -3, 0], [-32, 0, -400, 64], [287, 192, 96, 0], [-267, 0, -189, 534], [-486, 164, 82, 0], [-1289, -56, -28, 0], [-557, -38, -19, 0], [-204, 0, -12, 408], [739, 250, 125, 0], [652, 148, 74, 0], [377, 0, 58, -754], [-1032, -148, -74, 0], [-17, 0, 1022, 34], [-371, 178, 89, 0], [222, 0, -222, -444], [-143, 0, -544, 286], [-6, 476, 238, 0], [-19, 0, -878, 38], [-699, -142, -71, 0], [-48, 0, -672, 96], [-159, -244, -122, 0], [308, 0, -272, -616], [536, 36, 18, 0], [357, 0, 36, -714], [455, -270, -135, 0], [121, 0, -361, -242], [-495, 212, 106, 0], [-216, 0, 252, 432], [362, -108, -54, 0], [135, 0, 435, -270], [1213, -158, -79, 0], [144, 0, 72, -288], [-489, -286, -143, 0], [375, 0, -264, -750], [-214, -480, -240, 0], [-23, 0, -304, 46], [-119, 0, -229, 238], [290, 0, 466, -580], [-10, -300, -150, 0], [-63, 0, 795, 126], [111, 0, 138, -222], [57, 0, -627, -114], [-127, 18, 9, 0], [-1249, 144, 72, 0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_294_b_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_294_3_b_i();".
// 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_3_b_i(:prec:=4)
    chi := MakeCharacter_294_b();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 3));
    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_3_b_i();".
// 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_3_b_i( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_294_b();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,3,sign)));
    Vf := Kernel([<5,R![9, 0, 38, 0, 1]>,<13,R![-24, 16, 1]>],Snew);
    return Vf;
end function;