
// Make newform 51.3.c.c in Magma, downloaded from the LMFDB on 03 June 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_51_c();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_51_c_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_c_c();".
// 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_c_c();".
// 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 := [225, 0, 294, 0, 142, 0, 14, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0], [537, 0, -1, 0, -2, 0, -1, 0], [1581, 0, 1415, 0, 139, 0, 11, 0], [-645, 0, -1181, 0, -139, 0, -11, 0], [0, 1464, 0, 1532, 0, 139, 0, 11], [0, 12687, 0, 4001, 0, 397, 0, 23], [0, -14349, 0, -15097, 0, -1529, 0, -121]];
    Rf_basisdens := [1, 1, 117, 234, 234, 1755, 1170, 3510];
    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_51_c();"
function MakeCharacter_51_c()
    N := 51;
    order := 2;
    char_gens := [35, 37];
    v := [1, 1];
    // 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_c_Hecke();"
function MakeCharacter_51_c_Hecke(Kf)
    N := 51;
    order := 2;
    char_gens := [35, 37];
    char_values := [[-1, 0, 0, 0, 0, 0, 0, 0], [-1, 0, 0, 0, 0, 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]];
    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, 0, -1, 0, 0, 0, 0], [0, -1, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 1, 0, 1], [0, 2, 0, 0, 0, 1, 1, 0], [0, 0, 0, 0, 0, -5, 0, -4], [2, 0, -2, 0, 0, 0, 0, 0], [-1, 0, -1, -1, 2, -3, 0, 4], [11, 0, 3, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, -6, 0, -7], [0, 0, 0, 0, 0, 11, 0, -7], [0, 14, 0, 0, 0, 7, -1, 0], [0, -4, 0, 0, 0, -2, 5, 0], [0, 0, 0, 0, 0, 20, 0, 6], [8, 0, 2, 0, 0, 0, 0, 0], [-3, 0, -3, 8, 6, 0, 0, 0], [-1, 0, -1, -18, 2, 0, 0, 0], [-2, 0, -2, 6, 4, 0, 0, 0], [0, 16, 0, 0, 0, 8, -9, 0], [51, 0, -1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 8, 0, -1], [0, -48, 0, 0, 0, -24, -4, 0], [0, -10, 0, 0, 0, -5, 15, 0], [0, 0, 0, -18, 0, 0, 0, 0], [11, 0, 11, -8, -22, 0, 0, 0], [0, 4, 0, 0, 0, 2, -4, 0], [-3, 0, -3, 24, 6, 0, 0, 0], [-21, 0, 21, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, -51, 0, -12], [0, 8, 0, 0, 0, 4, -17, 0], [0, 0, 0, 0, 0, 48, 0, -44], [-31, 0, 11, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, -35, 0, 8], [-1, 0, -1, 22, 2, 0, 0, 0], [0, 22, 0, 0, 0, 11, 2, 0], [-11, 0, -11, 18, 22, 0, 0, 0], [171, 0, -7, 0, 0, 0, 0, 0], [40, 0, 50, 0, 0, 0, 0, 0], [0, 2, 0, 0, 0, 1, -4, 0], [0, 0, 0, 0, 0, 40, 0, -5], [0, 0, 0, 0, 0, 37, 0, 69], [27, 0, 27, 44, -54, 0, 0, 0], [0, 16, 0, 0, 0, 8, 31, 0], [-11, 0, -11, -52, 22, 0, 0, 0], [0, -72, 0, 0, 0, -36, -10, 0], [0, 0, 0, 0, 0, -3, 0, 79], [0, 58, 0, 0, 0, 29, 3, 0], [0, -86, 0, 0, 0, -43, 4, 0], [-194, 0, -36, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, -15, 0, 90], [-80, 0, 24, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 72, 0, 104], [15, 0, 15, 20, -30, 0, 0, 0], [0, 176, 0, 0, 0, 88, 16, 0], [23, 0, 23, 96, -46, 0, 0, 0], [7, 0, 7, -34, -14, 0, 0, 0], [26, 0, 26, -24, -52, 0, 0, 0], [0, 0, 0, 0, 0, -95, 0, -149], [240, 0, -18, 0, 0, 0, 0, 0], [0, 16, 0, 0, 0, 8, -7, 0], [-22, 0, -22, -94, 44, 0, 0, 0], [0, -74, 0, 0, 0, -37, -30, 0], [-5, 0, -5, 94, 10, 0, 0, 0], [-48, 0, 18, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 8, 0, -103], [0, -52, 0, 0, 0, -26, 42, 0], [0, 0, 0, 0, 0, -215, 0, -145], [94, 0, 40, 0, 0, 0, 0, 0], [0, -124, 0, 0, 0, -62, -10, 0], [0, 0, 0, 0, 0, -51, 0, -122], [262, 0, -8, 0, 0, 0, 0, 0], [8, 0, 8, 30, -16, 0, 0, 0], [-33, 0, -33, 64, 66, 0, 0, 0], [0, -6, 0, 0, 0, -3, 31, 0], [278, 0, 22, 0, 0, 0, 0, 0], [0, -46, 0, 0, 0, -23, 54, 0], [-40, 0, -40, -116, 80, 0, 0, 0], [7, 0, 7, 84, -14, 0, 0, 0], [0, -116, 0, 0, 0, -58, -27, 0], [0, 0, 0, 0, 0, 100, 0, -14], [-14, 0, -122, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 35, 0, -144], [-232, 0, 36, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, -44, 0, -277], [-377, 0, -63, 0, 0, 0, 0, 0], [0, 78, 0, 0, 0, 39, -37, 0], [7, 0, 7, 116, -14, 0, 0, 0], [0, 0, 0, 0, 0, -12, 0, 224], [227, 0, -7, 0, 0, 0, 0, 0], [3, 0, 3, 106, -6, 0, 0, 0], [371, 0, -71, 0, 0, 0, 0, 0], [21, 0, 21, -184, -42, 0, 0, 0], [0, 0, 0, 0, 0, -288, 0, -63], [0, -226, 0, 0, 0, -113, -69, 0], [-17, 0, -17, 236, 34, 0, 0, 0], [0, 186, 0, 0, 0, 93, 76, 0], [0, 0, 0, 0, 0, 128, 0, -109], [39, 0, 39, -222, -78, 0, 0, 0], [0, 0, 0, 0, 0, 126, 0, 258], [225, 0, -115, 0, 0, 0, 0, 0], [0, 40, 0, 0, 0, 20, -35, 0], [0, -282, 0, 0, 0, -141, -18, 0], [5, 0, 5, 132, -10, 0, 0, 0], [10, 0, 10, -266, -20, 0, 0, 0], [-2, 0, -2, -224, 4, 0, 0, 0], [0, 98, 0, 0, 0, 49, 48, 0], [543, 0, 67, 0, 0, 0, 0, 0], [55, 0, 55, 144, -110, 0, 0, 0], [-2, 0, -2, -140, 4, 0, 0, 0], [-66, 0, -66, -52, 132, 0, 0, 0], [0, -56, 0, 0, 0, -28, 14, 0], [0, 134, 0, 0, 0, 67, 23, 0], [72, 0, 18, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, -212, 0, -104], [0, 138, 0, 0, 0, 69, 18, 0], [-446, 0, -100, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 38, 0, 22], [0, 166, 0, 0, 0, 83, -90, 0], [21, 0, 21, 244, -42, 0, 0, 0], [0, 0, 0, 0, 0, 227, 0, 159], [-14, 0, -14, 82, 28, 0, 0, 0], [162, 0, 52, 0, 0, 0, 0, 0], [0, 188, 0, 0, 0, 94, -130, 0], [0, 0, 0, 0, 0, -167, 0, 61], [0, 0, 0, 0, 0, -393, 0, -456], [0, -214, 0, 0, 0, -107, -84, 0], [53, 0, 53, -64, -106, 0, 0, 0], [0, -436, 0, 0, 0, -218, -71, 0], [0, 0, 0, 0, 0, 378, 0, 147], [-604, 0, -6, 0, 0, 0, 0, 0], [-742, 0, -88, 0, 0, 0, 0, 0], [623, 0, -141, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 302, 0, -1], [0, -362, 0, 0, 0, -181, 53, 0], [-452, 0, 72, 0, 0, 0, 0, 0], [45, 0, 45, 150, -90, 0, 0, 0], [755, 0, 59, 0, 0, 0, 0, 0], [83, 0, 83, 0, -166, 0, 0, 0], [0, 114, 0, 0, 0, 57, -92, 0], [-67, 0, -67, -150, 134, 0, 0, 0], [0, 0, 0, 0, 0, -116, 0, 456], [0, 154, 0, 0, 0, 77, -36, 0], [0, 0, 0, 0, 0, 175, 0, 123], [0, -382, 0, 0, 0, -191, 27, 0], [0, 0, 0, 0, 0, -297, 0, 276], [352, 0, -158, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 304, 0, 579], [0, 84, 0, 0, 0, 42, 1, 0], [0, 0, 0, 0, 0, -174, 0, -88], [-520, 0, 154, 0, 0, 0, 0, 0], [-104, 0, -104, -24, 208, 0, 0, 0], [0, 104, 0, 0, 0, 52, 93, 0], [0, 0, 0, 0, 0, -312, 0, -528], [19, 0, -169, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 458, 0, -359], [0, -22, 0, 0, 0, -11, -128, 0], [0, 0, 0, 0, 0, -478, 0, -555], [390, 0, -156, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 596, 0, 278], [-700, 0, 140, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 123, 0, 339], [0, 0, 0, 0, 0, 207, 0, 354], [53, 0, 53, 360, -106, 0, 0, 0], [42, 0, -252, 0, 0, 0, 0, 0], [32, 0, 32, 54, -64, 0, 0, 0], [-52, 0, -52, 508, 104, 0, 0, 0], [0, 0, 0, 0, 0, 440, 0, 15], [0, 498, 0, 0, 0, 249, -77, 0], [0, 296, 0, 0, 0, 148, 45, 0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_51_c_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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