
// Make newform 42.4.e.b in Magma, downloaded from the LMFDB on 24 April 2024.
// 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_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_42_4_e_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_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)
    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 := 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 := [[2, -2], [0, 3], [15, -15], [14, 7], [0, 9], [-88, 0], [0, 84], [-104, 104], [84, -84], [51, 0], [0, -185], [-44, 44], [-168, 0], [326, 0], [138, -138], [0, -639], [0, -159], [-722, 722], [0, 166], [1086, 0], [0, -218], [583, -583], [-597, 0], [1038, -1038], [-169, 0], [0, -642], [-464, 464], [-393, 393], [0, -14], [-2184, 0], [-373, 0], [1173, -1173], [0, -30], [-82, 0], [1434, -1434], [0, 2671], [0, -2252], [-1676, 1676], [3030, 0], [-3438, 3438], [0, 1212], [3032, 0], [-2520, 2520], [0, -365], [-1590, 0], [0, 5380], [-5362, 0], [-1573, 0], [0, -921], [-4052, 4052], [468, -468], [4932, 0], [0, 1537], [5319, 0], [-5346, 5346], [0, -774], [0, -2415], [475, -475], [0, -3728], [1602, 0], [0, -686], [-1101, 0], [2780, 0], [0, -4296], [-5489, 5489], [-4491, 4491], [3964, -3964], [161, 0], [0, 5916], [-142, 0], [0, 4440], [-2286, 2286], [0, 2869], [3064, -3064], [-6040, 0], [-1842, 1842], [0, -7830], [14764, -14764], [-6264, 6264], [0, -4751], [-4704, 0], [-4474, 0], [0, 12804], [-5074, 0], [1267, -1267], [6933, -6933], [11688, 0], [-551, 551], [13386, 0], [-6376, 0], [-5700, 5700], [0, -19794], [0, -15935], [9963, 0], [-19142, 19142], [-12192, 0], [19809, -19809], [0, 1794], [6448, -6448], [-7262, 7262], [14204, 0], [0, -15825], [0, -1059], [-3960, 3960], [0, 2530], [0, -11831], [4809, 0], [-21804, 21804], [0, -14166], [5891, 0], [2737, -2737], [0, 26188], [-2358, 0], [0, -13766], [21287, 0], [0, -21426], [9962, 0], [0, 18174], [19167, -19167], [13080, 0], [0, 15190], [4397, 0], [-4029, 4029], [0, -15021], [13984, -13984], [-31053, 0], [-15086, 15086], [6378, -6378], [-7363, 0], [-32810, 32810], [0, 24034], [-8022, 0], [-29519, 29519], [-3742, 0], [10896, -10896], [17285, 0], [0, -11826], [0, -17714], [-24939, 0], [0, 29064], [-15370, 0], [-44031, 44031], [0, 4192], [33195, 0], [0, -16448], [16860, 0], [29054, 0], [0, -41958], [-5546, 5546], [32538, -32538], [-32096, 32096], [-8490, 0], [-48352, 0], [-15492, 15492], [0, 8116], [-4446, 0], [-26504, 26504], [5430, -5430], [33803, 0], [0, 48483], [-37296, 37296], [-38478, 0], [27257, 0], [-34341, 34341], [0, -27426], [0, -12324], [0, -47597], [0, 11242]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_42_e_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_42_4_e_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_42_4_e_b(: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_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_42_4_e_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_42_e();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,4,sign)));
    Vf := Kernel([<5,R![225, -15, 1]>],Snew);
    return Vf;
end function;