
// Make newform 42.4.e.a in Magma, downloaded from the LMFDB on 12 October 2025.
// To make the character of type GrpDrchElt, type "MakeCharacter_42_e();"
// To make the character of type GrpDrchElt with Codomain the HeckeField, type "MakeCharacter_42_e_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_42_4_e_a();".
// 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_42_4_e_a();".
// 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_42_e();"
function MakeCharacter_42_e()
    N := 42;
    order := 3;
    char_gens := [29, 31];
    v := [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_42_e_Hecke();"
function MakeCharacter_42_e_Hecke( : Kf := false)
    N := 42;
    order := 3;
    char_gens := [29, 31];
    char_values := [[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 := [[2, -2], [0, -3], [6, -6], [7, -21], [0, 30], [53, 0], [0, 84], [97, -97], [-84, 84], [-180, 0], [0, -179], [145, -145], [126, 0], [-325, 0], [366, -366], [0, 768], [0, 264], [-818, 818], [0, 523], [-342, 0], [0, 43], [1171, -1171], [-810, 0], [600, -600], [386, 0], [0, -618], [-1475, 1475], [-1884, 1884], [0, -413], [-882, 0], [2483, 0], [-2118, 2118], [0, -3012], [-37, 0], [1644, -1644], [0, -1088], [0, -506], [-1844, 1844], [162, 0], [2724, -2724], [0, 1254], [-1807, 0], [-714, 714], [0, 3709], [-1044, 0], [0, 136], [1484, 0], [-2032, 0], [0, -6198], [4591, -4591], [-4530, 4530], [1530, 0], [0, -5534], [-468, 0], [2490, -2490], [0, -1572], [0, -1806], [6112, -6112], [0, 4231], [-3816, 0], [0, 3997], [4608, 0], [-631, 0], [0, -3894], [2185, -2185], [-3504, 3504], [-2945, 2945], [4277, 0], [0, -7188], [-9406, 0], [0, -3390], [4812, -4812], [0, 7099], [-2963, 2963], [-11899, 0], [-2568, 2568], [0, 10146], [6229, -6229], [2472, -2472], [0, 7075], [-4158, 0], [-6595, 0], [0, -1518], [8567, 0], [-10640, 10640], [-7032, 7032], [-14814, 0], [11251, -11251], [-3852, 0], [-475, 0], [-5934, 5934], [0, 13368], [0, -6653], [15444, 0], [-683, 683], [9882, 0], [-4206, 4206], [0, -9060], [15679, -15679], [7711, -7711], [4292, 0], [0, 9858], [0, 13890], [-19038, 19038], [0, 8053], [0, 17137], [18144, 0], [24702, -24702], [0, 2172], [4175, 0], [-2261, 2261], [0, 16318], [-26550, 0], [0, -19925], [-6832, 0], [0, -10212], [3779, 0], [0, -16998], [21750, -21750], [-10944, 0], [0, -10955], [25103, 0], [5604, -5604], [0, -10968], [-8405, 8405], [468, 0], [25066, -25066], [-11082, 11082], [13481, 0], [-24317, 24317], [0, 18217], [19782, 0], [4921, -4921], [18098, 0], [24468, -24468], [21719, 0], [0, 30306], [0, -27296], [-35100, 0], [0, -44394], [-8584, 0], [9834, -9834], [0, -43856], [13266, 0], [0, -17453], [-35172, 0], [3503, 0], [0, -22848], [13456, -13456], [-40710, 40710], [-2906, 2906], [-19188, 0], [-17251, 0], [2094, -2094], [0, 40267], [17604, 0], [-3509, 3509], [34638, -34638], [-17353, 0], [0, 46920], [-18354, 18354], [35568, 0], [-27343, 0], [-51024, 51024], [0, 2226], [0, -35304], [0, 2341], [0, -29015]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_42_e_Hecke( : Kf := Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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