
// Make newform 50.6.b.c in Magma, downloaded from the LMFDB on 24 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_50_b();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_50_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_50_6_b_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_50_6_b_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 := [1, 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_50_b();"
function MakeCharacter_50_b()
    N := 50;
    order := 2;
    char_gens := [27];
    v := [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_50_b_Hecke();"
function MakeCharacter_50_b_Hecke(Kf)
    N := 50;
    order := 2;
    char_gens := [27];
    char_values := [[-1, 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 := 6;
    raw_aps := [[0, 4], [0, -11], [0, 0], [0, 142], [777, 0], [0, 884], [0, 27], [-1145, 0], [0, 1854], [4920, 0], [1802, 0], [0, -13178], [-15123, 0], [0, 7844], [0, 6732], [0, 3414], [-33960, 0], [47402, 0], [0, 13177], [-7548, 0], [0, -59821], [-75830, 0], [0, 46299], [30585, 0], [0, -104018], [-23898, 0], [0, -22636], [0, -60633], [7090, 0], [0, -128841], [0, -141338], [80052, 0], [0, -32253], [-394865, 0], [491400, 0], [200402, 0], [0, 22942], [0, -336241], [0, -59748], [0, -60696], [7995, 0], [-454798, 0], [-428298, 0], [0, -835531], [0, -678318], [31900, 0], [-423673, 0], [0, 398204], [0, 1257612], [-203780, 0], [0, 823974], [555960, 0], [523577, 0], [113127, 0], [0, -872958], [0, -1646466], [-1788720, 0], [1121402, 0], [0, 598312], [-1530498, 0], [0, -1797001], [0, 754494], [0, 1966267], [-599298, 0], [0, -721366], [0, -102348], [1310477, 0], [0, 804397], [0, 2883207], [-1273550, 0], [0, 2830614], [981090, 0], [0, 4191052], [0, 3234554], [-1390355, 0], [0, 1141974], [-3462990, 0], [0, -5940068], [-2274123, 0], [4295515, 0], [-1797045, 0], [-257548, 0], [2229102, 0], [0, -4205851], [-352640, 0], [0, 1283619], [2103975, 0], [0, -825233], [4501452, 0], [0, -1442116], [0, -393348], [9176970, 0], [0, -6605978], [38052, 0], [-6856700, 0], [0, 8200164], [-4065810, 0], [5284077, 0], [0, 2533829], [498752, 0], [0, -3002693], [0, 12737292], [0, -5970816], [9269055, 0], [-3895348, 0], [0, -7294163], [0, 8728197], [0, -13096281], [13016850, 0], [-9939973, 0], [0, -15643808], [0, 9337934], [0, 5066802], [-13767020, 0], [2070602, 0], [-17911398, 0], [0, -17141356], [0, 8487732], [0, -2454786], [5915565, 0], [4330952, 0], [0, -9139846], [0, -4572288], [0, 15322149], [7025477, 0], [7911252, 0], [15448540, 0], [-23054370, 0], [0, -16290488], [0, 12827924], [12453460, 0], [0, -26324706], [-17499448, 0], [0, -3463808], [-12617523, 0], [-5708045, 0], [0, 12082704], [0, 13763572], [0, 8777382], [10204590, 0], [-1173748, 0], [-19806198, 0], [0, -30672196], [0, -25551963], [9194020, 0], [15690960, 0], [0, 1601114], [0, 16461267], [-19673585, 0], [0, 36806784], [0, 26959612], [34733502, 0], [0, 21618749], [0, -4481628], [0, 33663892], [-1031748, 0], [-4101470, 0], [-7716030, 0], [0, -43845803], [-10021548, 0], [0, -27952968], [0, -23181111], [0, 1584352], [34455177, 0], [0, 29359887], [0, 8939574], [-17889898, 0], [0, -33051668]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_50_b_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_50_6_b_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_50_6_b_c(:prec:=2)
    chi := MakeCharacter_50_b();
    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_50_6_b_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_50_6_b_c( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_50_b();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,6,sign)));
    Vf := Kernel([<3,R![121, 0, 1]>],Snew);
    return Vf;
end function;