
// Make newform 336.4.q.b in Magma, downloaded from the LMFDB on 05 September 2025.
// To make the character of type GrpDrchElt, type "MakeCharacter_336_q();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_336_q_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_q_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_336_4_q_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_336_q();"
function MakeCharacter_336_q()
    N := 336;
    order := 3;
    char_gens := [127, 85, 113, 241];
    v := [3, 3, 3, 2];
    // 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_q_Hecke();"
function MakeCharacter_336_q_Hecke( : Kf := false)
    N := 336;
    order := 3;
    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], [0, -2], [-14, -7], [-18, 18], [33, 0], [68, -68], [0, 25], [0, 92], [92, 0], [25, -25], [0, 213], [94, 0], [67, 0], [0, 278], [400, -400], [744, -744], [0, 734], [555, -555], [642, 0], [-973, 973], [0, -785], [822, 0], [0, -424], [-734, 0], [270, -270], [0, 417], [0, -1644], [551, -551], [-402, 0], [-1889, 0], [0, 554], [2956, -2956], [795, 0], [0, 908], [-3272, 3272], [-234, 234], [0, -836], [-6, 0], [0, 532], [4070, -4070], [-4747, 0], [0, -2402], [2925, -2925], [60, 0], [-3184, 3184], [1612, 0], [5736, 0], [4970, -4970], [0, 763], [0, 5374], [-878, 0], [-670, 670], [4700, 0], [0, -1734], [3036, -3036], [-7110, 7110], [0, 4808], [7091, -7091], [-1992, 0], [1517, -1517], [8016, 0], [4169, 0], [-9102, 9102], [0, 7993], [0, 6288], [0, 8015], [2093, 0], [-996, 996], [5522, 0], [-510, 510], [0, 2364], [2423, -2423], [0, -8375], [-9651, 0], [0, 7080], [-5158, 5158], [0, 13385], [0, -14328], [-5349, 5349], [-12734, 0], [-3287, 0], [11946, -11946], [-4433, 0], [0, -12360], [0, 10376], [-16382, 0], [0, 9659], [7524, 0], [-13151, 0], [0, -9150], [-11816, 11816], [13087, -13087], [-3612, 0], [0, 3493], [2354, 0], [0, 13146], [-13380, 13380], [0, -5353], [0, -21685], [-3724, 0], [-24166, 24166], [-25118, 25118], [0, 6162], [-12619, 12619], [8937, -8937], [-12784, 0], [0, -13746], [6076, -6076], [-3977, 0], [0, -5873], [11438, -11438], [25866, 0], [-12069, 12069], [-392, 0], [27884, -27884], [-4069, 0], [23586, -23586], [0, 7822], [4096, 0], [24601, -24601], [16231, 0], [0, -1404], [22248, -22248], [0, 32483], [16180, 0], [0, -24358], [0, -26658], [20701, 0], [0, 12839], [-21815, 21815], [24286, 0], [0, 15173], [-13742, 0], [0, -34764], [-31241, 0], [-27622, 27622], [30544, -30544], [31668, 0], [-11562, 11562], [3144, 0], [0, -10926], [-35224, 35224], [38322, 0], [-18089, 18089], [2412, 0], [8611, 0], [-10560, 10560], [0, -7808], [0, -18910], [0, -9290], [-39428, 0], [-37051, 0], [0, -21450], [1523, -1523], [-1324, 0], [0, -22457], [0, -50146], [-32841, 0], [13176, -13176], [0, -20594], [-992, 0], [-21459, 0], [0, -27344], [50226, -50226], [-4152, 4152], [-53727, 53727], [29741, -29741]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_336_q_Hecke( : Kf := Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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