
// Make newform 208.4.w.b in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_208_w();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_208_w_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_208_4_w_b();".
// 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_208_4_w_b();".
// 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_208_w();"
function MakeCharacter_208_w()
    N := 208;
    order := 6;
    char_gens := [79, 53, 145];
    v := [6, 6, 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_208_w_Hecke();"
function MakeCharacter_208_w_Hecke(Kf)
    N := 208;
    order := 6;
    char_gens := [79, 53, 145];
    char_values := [[1, 0], [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, 0], [2, -2], [1, -2], [16, -8], [-8, -8], [39, 13], [0, -117], [132, -66], [-78, 78], [141, -141], [90, -180], [-83, -83], [157, 157], [0, 104], [174, -348], [93, 0], [328, -164], [0, -145], [454, 454], [-1220, 610], [-265, 530], [-1276, 0], [-456, 912], [-564, -564], [232, -116], [-429, 429], [-182, 0], [-1506, 1506], [-896, 1792], [0, 687], [286, -286], [1974, 0], [-978, 489], [0, 236], [-54, 27], [-1022, 2044], [1211, 0], [1160, -580], [-528, -528], [0, -2574], [3744, -3744], [-637, 0], [0, -2598], [645, 645], [1184, 1184], [0, -2522], [1042, -1042], [1390, 1390], [-2780, 1390], [-1448, 2896], [-5850, 0], [-3108, 6216], [5678, -2839], [0, -3978], [-2067, 2067], [-2052, 2052], [0, -3330], [-1620, -1620], [0, -377], [21, -42], [-7124, 7124], [-9610, 4805], [-1282, 2564], [-4914, 0], [-518, 0], [-2261, 4522], [8608, -4304], [-3575, 0], [0, -6966], [-3840, -3840], [3251, 3251], [-4116, 8232], [2, -2], [0, -3499], [-3186, -3186], [8504, -4252], [-1209, 0], [13504, -6752], [-1721, -1721], [50, -25], [-9462, 9462], [-4081, 8162], [5732, 5732], [0, 6617], [13988, -13988], [-2004, 0], [10488, -5244], [1457, 1457], [22618, -11309], [4984, -9968], [-5460, 0], [1474, 1474], [-12504, 6252], [11388, -11388], [-10206, 20412], [0, 3876], [-9853, -9853], [2121, 0], [-11464, 11464], [2751, -5502], [-6554, 0], [-10457, -10457], [0, -12168], [7722, -7722], [-11440, 0], [-8917, 17834], [8124, 8124], [-15553, 31106], [10554, 0], [14831, -14831], [0, -7954], [14561, 14561], [-20074, 10037], [-4740, 9480], [-14856, 7428], [0, -6201], [19424, -9712], [-13494, 13494], [11334, -11334], [0, 13236], [-6843, -6843], [-8021, 8021], [-21630, 0], [30644, -15322], [480, 480], [30186, 0], [-13718, 6859], [0, 18408], [-21112, 0], [13833, -27666], [-1828, -1828], [17380, 17380], [28496, -28496], [-17422, 17422], [47720, -23860], [-8124, -8124], [232, -116], [7972, -3986], [0, 31278], [-8049, 8049], [8098, -16196], [-4640, -4640], [0, -40300], [22820, -45640], [12311, -12311], [-24788, 12394], [-447, 894], [13923, 0], [22358, 0], [-1288, 2576], [-19346, 9673], [-17355, 17355], [46982, 0], [-8916, 8916], [-30836, 30836], [27480, 0], [0, -28442], [8058, -4029], [-38465, 0], [-2820, 5640], [-12566, -12566], [0, -6474], [4354, -8708], [0, 34998], [-14559, -14559], [-32586, 65172], [59282, -59282], [0, 37711]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_208_w_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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