
// Make newform 336.4.bl.d in Magma, downloaded from the LMFDB on 23 September 2025.
// To make the character of type GrpDrchElt, type "MakeCharacter_336_bl();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_336_bl_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_336_4_bl_d();".
// 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_336_4_bl_d();".
// 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_336_bl();"
function MakeCharacter_336_bl()
    N := 336;
    order := 6;
    char_gens := [127, 85, 113, 241];
    v := [3, 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_336_bl_Hecke();"
function MakeCharacter_336_bl_Hecke( : Kf := false)
    N := 336;
    order := 6;
    char_gens := [127, 85, 113, 241];
    char_values := [[-1, 0], [1, 0], [1, 0], [0, 1]];
    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 := 4;
    raw_aps := [[0, 0], [3, -3], [-2, 1], [-7, 21], [-23, -23], [-20, 40], [-54, -54], [0, -94], [104, -52], [-225, 0], [-11, 11], [0, -358], [-82, 164], [-230, 460], [0, 192], [-417, 417], [561, -561], [-616, 308], [56, 56], [484, -968], [-12, -12], [-22, 11], [-693, 0], [-1180, 590], [-475, 950], [96, 96], [0, -164], [2034, -1017], [1420, -1420], [1434, 0], [-1507, 3014], [0, -1911], [564, -564], [-550, 0], [0, -930], [509, 509], [1668, 1668], [632, -316], [516, 0], [3872, -1936], [462, 462], [1854, -3708], [2336, -1168], [-4411, 4411], [-66, 0], [344, -344], [-1352, 2704], [-6395, 0], [5517, -5517], [412, -206], [0, 438], [-1968, 3936], [-1403, -1403], [-6783, 0], [-8180, 4090], [1118, 1118], [-1999, -1999], [0, -1277], [2468, -2468], [-7848, 0], [-4186, 4186], [-267, 534], [-6536, 0], [-4734, 4734], [-5046, 2523], [0, -573], [7008, -3504], [4991, 0], [-2170, -2170], [-3700, 7400], [-3124, -3124], [7248, -3624], [12299, -12299], [0, -2570], [-4130, 8260], [0, 10902], [-678, 678], [11416, -5708], [0, 15324], [851, 851], [-15324, 0], [328, 0], [9590, 9590], [3820, -7640], [0, -1213], [-12434, 6217], [12144, 0], [0, -10793], [-10132, 20264], [4422, -8844], [0, -10884], [-10194, 10194], [-2643, -2643], [-4519, 9038], [13708, -6854], [-10950, 0], [-10710, 5355], [-6722, -6722], [0, 1024], [0, 19526], [-13630, 27260], [10245, -10245], [-3075, 3075], [0, -14730], [-96, -96], [8071, 8071], [-2121, 0], [-13640, 6820], [3848, 3848], [11351, -22702], [0, -17773], [-30008, 30008], [4536, 0], [25216, -25216], [3637, -7274], [8760, -8760], [7000, 0], [-10236, 10236], [0, 20109], [13246, -26492], [-11658, -11658], [-4187, 0], [-21458, 10729], [-15791, -15791], [0, 6778], [-26979, 0], [0, -11558], [0, -9498], [35837, 0], [-19492, 9746], [-9262, -9262], [6040, -12080], [-16758, 8379], [-19792, 0], [-25248, 12624], [-19077, 38154], [-9656, -9656], [-20240, 20240], [3349, -6698], [5976, -5976], [-32656, 0], [0, -13077], [7054, 7054], [17303, -34606], [16146, 16146], [20130, 0], [1308, -2616], [-23236, -23236], [0, 25310], [51996, -25998], [0, -14264], [-19912, 39824], [6342, -12684], [0, -36396], [12664, 12664], [27732, -55464], [-62788, 31394], [732, -366], [29989, -59978], [9103, 9103], [-65164, 32582], [-8922, 0], [20855, -41710], [0, -21327], [55122, -55122], [12174, -12174], [-17383, -17383], [616, 616]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_336_bl_Hecke( : Kf := Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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