
// Make newform 51.3.g.a in Magma, downloaded from the LMFDB on 07 September 2025.
// To make the character of type GrpDrchElt, type "MakeCharacter_51_g();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_51_g_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_51_3_g_a();".
// 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_51_3_g_a();".
// 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, 0, 0, 0, 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_51_g();"
function MakeCharacter_51_g()
    N := 51;
    order := 8;
    char_gens := [35, 37];
    v := [4, 7];
    // 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_51_g_Hecke();"
function MakeCharacter_51_g_Hecke( : Kf := false)
    N := 51;
    order := 8;
    char_gens := [35, 37];
    char_values := [[-1, 0, 0, 0], [0, 1, 0, 0]];
    assert UnitGenerators(DirichletGroup(N)) eq char_gens;
    values := ConvertToHeckeField(char_values : pass_field := ISA(Type(Kf), Fld), 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]];
    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 := [[-1, -1, -1, 0], [3, 0, 0, 0], [2, 2, 2, -2], [-1, -5, -5, -1], [-4, -3, -3, -4], [0, 2, 12, 2], [-6, 12, 10, -3], [-10, 0, 10, 4], [-7, 11, 11, -7], [0, 0, -26, 26], [3, 3, -21, 21], [6, 6, -2, 2], [4, -15, 15, -4], [7, 22, 7, 0], [-14, -52, 0, 52], [-18, 50, -18, 0], [75, 0, -75, 6], [-54, -40, -40, -54], [58, -7, 0, 7], [-33, 33, -37, -37], [-22, 22, -15, -15], [11, 51, -51, -11], [-9, -110, -9, 0], [-44, 31, 0, -31], [51, -51, 50, 50], [0, 6, 104, 6], [-56, -72, 0, 72], [22, 22, 17, -17], [-46, 20, 20, -46], [-10, 23, 23, -10], [52, -42, 52, 0], [79, 79, -22, 22], [0, 70, 114, 70], [-71, -71, 4, -4], [18, -24, 0, 24], [4, 0, -4, -2], [0, 26, -218, 26], [76, 71, 71, 76], [109, -109, 15, 15], [70, -70, -78, -78], [-24, 0, 24, -6], [-86, 42, -42, 86], [78, -62, 0, 62], [138, -105, 105, -138], [-2, 8, -8, 2], [-133, 133, 63, 63], [-109, 140, 140, -109], [-178, 0, 178, -76], [59, -88, -88, 59], [-142, -220, -142, 0], [-51, -51, 144, -144], [0, -102, 34, -102], [275, 275, -68, 68], [-234, 87, 0, -87], [101, -52, 101, 0], [68, 0, -68, -54], [100, -100, 180, 180], [-34, -130, 0, 130], [-248, 248, 88, 88], [-80, 0, 80, -418], [4, 169, -169, -4], [-262, 6, 0, -6], [258, 88, 0, -88], [281, 281, -99, 99], [95, -64, -64, 95], [-62, -18, -18, -62], [-95, -142, -95, 0], [-2, -2, 209, -209], [-145, 192, -192, 145], [162, 102, 162, 0], [-144, 85, 0, -85], [76, -440, 76, 0], [-45, 193, 193, -45], [164, 124, 0, -124], [107, -107, -136, -136], [26, 0, -26, 306], [152, -28, 152, 0], [76, 180, -180, -76], [215, 362, -362, -215], [20, -235, 0, 235], [-281, -310, -310, -281], [0, 118, -380, 118], [69, 169, 169, 69], [14, -168, 14, 0], [279, 279, -35, 35], [0, -5, 242, -5], [-291, -130, 130, 291], [-242, 0, 242, -464], [-228, 218, -228, 0], [0, 64, 44, 64], [-148, 0, 148, 562], [-183, 183, -279, -279], [219, 377, -377, -219], [24, 190, 24, 0], [-272, 191, -191, 272], [-29, -501, 501, 29], [0, 482, 138, 482], [-157, 48, 48, -157], [0, -349, 286, -349], [-48, -48, -208, 208], [-41, -41, -560, 560], [852, -72, 0, 72], [-425, 362, -425, 0], [303, 0, -303, -172], [163, 306, 306, 163], [658, 314, 0, -314], [-118, 0, 118, 524], [-1, -776, -1, 0], [172, -18, 0, 18], [-68, 123, -123, 68], [127, -127, -227, -227], [-978, -152, 0, 152], [396, 396, 323, -323], [6, -7, -7, 6], [280, 0, -280, 780], [444, 444, -203, 203], [-238, -238, 15, -15], [0, -156, -154, -156], [390, -176, 176, -390], [-72, -498, 0, 498], [-312, 0, 312, -350], [212, -31, -31, 212], [-34, 34, -276, -276], [-180, 180, -283, -283], [-402, -535, 535, 402], [204, 520, 0, -520], [216, -216, 238, 238], [321, 321, 169, -169], [0, 148, -234, 148], [502, 0, -502, -42], [20, 1194, 20, 0], [-31, -31, 227, -227], [57, 57, 117, -117], [516, 16, 516, 0], [-54, -274, 0, 274], [0, -174, -24, -174], [172, 0, -172, -644], [806, -806, -269, -269], [-268, -270, -268, 0], [-779, 84, -84, 779], [-452, 452, 239, 239], [200, 200, 482, -482], [-459, -525, -525, -459], [-173, 616, 616, -173], [0, -336, -10, -336], [51, 655, 655, 51], [64, 64, -256, 256], [-734, -67, 67, 734], [-117, 162, -117, 0], [-1024, 318, 0, -318], [460, 498, 498, 460], [201, -201, 204, 204], [-790, -171, 0, 171], [-771, 771, 607, 607], [-276, 103, -103, 276], [-235, -681, 681, 235], [-454, 502, 0, -502], [898, -81, -81, 898], [920, 0, -920, 102], [492, -1154, -1154, 492], [-774, -774, 337, -337], [0, 529, -804, 529], [-334, 0, 334, 1108], [-493, -794, -493, 0], [-31, 0, 31, -1432], [579, -579, 465, 465], [235, -235, -505, -505], [-212, -622, 622, 212]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_51_g_Hecke( : Kf := Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_51_3_g_a();".
// 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_51_3_g_a(:prec:=4)
    chi := MakeCharacter_51_g();
    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_51_3_g_a();".
// 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_51_3_g_a( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_51_g();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,3,sign)));
    Vf := Kernel([<2,R![1, 4, 8, 4, 1]>],Snew);
    return Vf;
end function;