
// Make newform 4140.2.a.m in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_4140_a();"
// To make the coeffs of the qexp of the newform in the Hecke field type "qexpCoeffs();"
// To make the newform (type ModFrm), type "MakeNewformModFrm_4140_2_a_m();".
// 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_4140_2_a_m();".
// 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 := [-4, -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_4140_a();"
function MakeCharacter_4140_a()
    N := 4140;
    order := 1;
    char_gens := [2071, 461, 1657, 3961];
    v := [1, 1, 1, 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;

function MakeCharacter_4140_a_Hecke(Kf)
    return MakeCharacter_4140_a();
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 := 2;
    raw_aps := [[0, 0], [0, 0], [-1, 0], [0, 1], [-2, 0], [-1, -1], [0, -1], [6, 0], [-1, 0], [-1, -2], [-3, 4], [-2, -1], [1, -2], [0, 0], [3, -3], [2, 1], [-4, 3], [-2, -2], [-6, -1], [-5, -2], [-7, 1], [10, -2], [-4, 7], [-4, -4], [-8, -2], [10, 3], [-16, 0], [-6, 1], [0, 6], [0, -5], [-5, 5], [-13, 5], [-12, 2], [-1, -4], [-6, 6], [9, -9], [-12, 3], [-1, 7], [8, 0], [10, -6], [-11, 7], [10, -6], [-2, -6], [5, -1], [11, 3], [-10, 0], [8, -3], [8, -10], [-16, -4], [4, -12], [-7, 3], [-13, 4], [6, 0], [-8, 6], [-19, -5], [-18, 7], [-13, -4], [4, 7], [1, -3], [12, -2], [2, 5], [4, 5], [-16, -2], [-1, -3], [12, -5], [-10, -2], [3, 10], [-12, 10], [4, -8], [-11, -6], [-21, 9], [10, -10], [-20, -1], [-10, -4], [-16, -8], [-16, 5], [16, -2], [-9, 17], [-2, 0], [5, -16], [24, -2], [20, 2], [0, 4], [22, 7], [7, 5], [-1, 5], [14, 3], [12, -1], [23, 7], [-16, -6], [30, -3], [-8, 20], [-5, 5], [9, -8], [-3, 10], [-14, 7], [-1, 11], [-12, 10], [16, -12], [5, -3], [7, -11], [8, 3], [16, -19], [14, -12], [36, 0], [-27, -1], [-25, 7], [10, -10], [-8, 16], [21, 6], [-12, 8], [20, -10], [2, -1], [-8, 8], [20, 4], [-12, 2], [-18, 15], [-13, 3], [9, -19], [0, -6], [14, -8], [-9, 17], [-14, -5], [-19, -9], [-4, 8], [12, -4], [40, -4], [4, -9], [-34, 5], [8, -3], [-25, 10], [-24, 8], [-8, -2], [10, 1], [3, 16], [-26, 6], [36, 2], [-32, -3], [14, 15], [26, 11], [37, 4], [14, 8], [-15, -9], [-12, -3], [14, -17], [48, -2], [-10, -4], [41, -3], [-35, 8], [5, 1], [22, -16], [20, -24], [-20, 12], [11, -21], [14, 3], [-2, 2], [16, 12], [-15, 28], [-6, -4], [48, -4], [-49, -3], [18, 8], [-49, -3], [38, -6], [-6, -21], [14, 9], [20, 13], [30, 10], [-30, -10], [9, 11], [-34, -10], [-43, 4], [24, -2], [44, -7], [7, -11], [16, -12], [27, -11], [21, 12], [30, -7], [0, 10], [43, -9], [28, -12], [-23, -7], [-34, 20], [-26, 15], [-12, 2], [14, -20], [-2, 9], [-35, 4], [4, -3], [-25, 11], [-36, 10], [6, -12], [-22, 1], [-22, 6], [24, 13], [34, -4], [48, 2], [48, 6], [21, 1], [-4, -12], [-16, 20], [-3, 21], [-6, 8], [-12, -12], [-37, 3], [30, 10], [11, 3], [9, -26], [7, -15], [-1, -21], [21, -23], [-16, 0], [30, 6], [20, 11], [24, -20], [-15, 1], [3, -20], [-10, 15], [-3, 25], [31, 2], [62, 4], [-21, 2], [32, -5], [-53, 7], [-1, -20], [-38, -12], [-31, 14], [10, 18], [24, -15], [-46, 16], [4, 6], [-22, -8], [-22, -9], [-24, 1], [0, -12], [-8, 24], [-22, 0], [55, 0], [5, 20], [-28, 7], [25, -8], [-63, 1], [-37, 15], [10, -14], [7, 17], [29, 7], [22, 12], [-14, -16], [2, -11], [12, -26], [2, 0], [-10, -1], [-40, 2], [-50, 8], [-63, -2], [42, -14], [-30, 6], [-17, 11], [5, -11], [-40, 19], [-32, -7], [-2, -11], [-16, 11], [-6, -14], [14, -10], [-26, -14], [-52, 14], [-22, 13], [11, -23], [-27, -16], [-6, -17], [36, 17], [-44, -6], [-11, 3], [13, 5], [-12, -4], [-2, 31], [-22, 22], [16, -28], [9, -11], [-6, -6], [42, -2], [46, 14], [16, 14], [36, 1], [-10, -17], [-46, 0], [-32, 0], [18, -20], [-18, 12], [0, -19], [22, -6], [-46, -10], [53, -5], [-26, 6], [4, -22], [1, -15], [-27, -20], [-21, -13], [20, 23], [42, 1], [12, 12], [-55, 7], [-20, 24], [-53, -11], [-4, -10], [12, 14], [-58, 10], [10, -14], [-25, 13], [-14, -4], [-8, 28], [71, 1], [-50, 3], [-44, 16], [17, -22], [3, 4], [-36, 9], [-11, 17], [-24, 0], [28, -13], [-31, -7], [-24, -10], [-42, 5], [-16, 12], [26, 2], [23, -25], [-38, 22], [-20, -3], [37, 18], [-3, -3], [-13, -4], [1, 13], [-10, 14], [25, -5], [-54, -4], [-4, 30], [-5, 19], [80, 1], [27, 15], [-56, -6], [-22, -4], [-42, -6], [70, -1], [-24, 21], [19, -26], [60, 6], [-54, 8], [-14, -13], [-39, 10], [1, 3], [38, -21], [70, -7], [-56, 12], [-50, 12], [44, -6], [42, -24], [47, 3], [-28, -18], [-38, 35], [-13, -16], [-59, 21], [82, -6], [47, 15], [-11, 13], [69, -5], [38, -1], [-14, 10], [-51, 16], [56, 5], [-1, -33], [-36, 4], [56, -8], [-25, -5], [37, -14], [-2, 0], [74, -1], [28, 28], [27, -25], [-34, 28], [-54, -12], [-60, -12], [-3, -15], [20, -16], [25, 5], [51, -30], [-58, 6], [22, 3], [68, 11], [4, -18], [26, -4], [7, 30], [20, -20], [-16, 18], [-14, -1], [60, 2], [36, -20], [-4, 0], [14, 25], [-29, 16], [-1, -33], [-28, 15], [32, -7], [33, -4], [-71, 2], [-30, 9], [-73, 19], [-36, 8], [12, -8], [11, -19], [-39, -7], [34, -38], [-58, -14], [-26, -3], [27, -25], [-16, -8], [44, -28], [4, -21], [20, -48], [24, -6], [-16, -22], [0, -30], [44, 0], [-23, -1], [5, -1], [-42, -10], [-90, -5], [-23, -1], [39, -10]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_4140_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_4140_2_a_m();".
// 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_4140_2_a_m(:prec:=2)
    chi := MakeCharacter_4140_a();
    f_vec := qexpCoeffs();
    Kf := Universe(f_vec);
    // SetVerbose("ModularForms", true);
    // SetVerbose("ModularSymbols", true);
    S := CuspidalSubspace(ModularForms(chi, 2));
    S := BaseChange(S, Kf);
    maxprec := NextPrime(2999) - 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_4140_2_a_m();".
// 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_4140_2_a_m( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_4140_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<7,R![-4, -1, 1]>,<11,R![2, 1]>,<13,R![-2, 3, 1]>,<17,R![-4, 1, 1]>],Snew);
    return Vf;
end function;