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


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