
// Make newform 4140.2.a.o in Magma, downloaded from the LMFDB on 23 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_o();".
// 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_o();".
// 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 := [-3, 0, 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], [1, 2], [1, 3], [-5, -1], [6, -1], [-1, -1], [1, 0], [-2, 3], [-1, 0], [-1, 6], [6, -1], [0, -2], [1, 1], [2, 5], [2, -3], [-9, 1], [5, 0], [2, -7], [-13, -1], [4, 0], [-2, -5], [6, -6], [4, 0], [8, 3], [10, -4], [4, 3], [-5, -5], [10, -3], [-1, -7], [8, -8], [12, 0], [7, 0], [-11, -3], [8, 6], [-5, -10], [10, 8], [11, 3], [-4, 8], [-8, 2], [-2, 6], [-11, -3], [14, -6], [4, -2], [-8, -2], [-11, 4], [14, 0], [16, -4], [14, -2], [0, 12], [10, 7], [1, 9], [-10, 8], [3, 7], [20, 5], [0, -1], [13, -6], [-18, -8], [7, 13], [17, -8], [16, -5], [-5, -5], [-16, 4], [5, -10], [-9, -13], [-3, 14], [22, 4], [-18, -6], [-3, -2], [-17, -9], [7, -1], [-27, 2], [-6, 2], [-4, -8], [-12, 11], [-16, -2], [32, 0], [-20, 6], [-1, 18], [-9, -7], [-9, -13], [-14, -6], [-19, 6], [-2, -12], [7, 3], [-6, -1], [-23, 2], [12, -10], [5, 9], [0, -17], [9, -15], [-1, 1], [0, 11], [-25, 2], [-12, 3], [12, -12], [-3, 11], [16, -12], [-8, 4], [4, 4], [-10, -15], [0, 9], [-4, -2], [-9, 9], [-4, -12], [18, -4], [33, 1], [-14, -6], [-17, -6], [-3, 15], [-14, 16], [24, 7], [4, -4], [-5, -23], [9, 11], [-5, -2], [25, -1], [-3, -21], [21, -17], [32, 2], [5, 19], [26, 3], [9, 1], [-4, -2], [3, -9], [19, -7], [-16, -1], [29, 0], [-19, -20], [-19, -10], [4, -8], [-37, 7], [37, 8], [0, 3], [-1, 5], [0, 26], [35, -6], [-6, -1], [18, -3], [-27, 4], [-12, -8], [0, -12], [18, -13], [7, -28], [-42, 0], [-28, 2], [-6, 16], [-19, 0], [0, 30], [-4, 2], [-27, -11], [-37, 5], [0, -10], [13, -4], [24, 0], [-16, 4], [12, 13], [12, 4], [-17, 3], [26, 10], [-2, -16], [-29, -7], [30, -10], [-22, -7], [-6, 3], [1, -16], [-50, 2], [12, -16], [27, 11], [-32, -12], [-19, -20], [-10, -8], [-25, 0], [28, -12], [-27, 13], [-30, 16], [-16, 9], [23, 16], [13, -7], [31, -13], [15, 7], [8, 10], [-33, -7], [-16, -7], [11, 9], [15, 19], [7, -24], [3, 4], [-30, 13], [-13, 27], [18, 8], [13, -3], [-32, -11], [0, -2], [-14, -17], [5, -7], [0, -28], [-10, -16], [42, 4], [9, -35], [-28, -10], [5, -15], [-49, -9], [9, -27], [16, -18], [5, 15], [37, 3], [26, -21], [-16, -22], [-36, -2], [-36, -10], [-6, -16], [-4, 18], [-24, -17], [7, -19], [28, -2], [10, 23], [24, -11], [7, -11], [11, 0], [36, 14], [12, -15], [-33, -12], [1, 11], [9, 10], [-4, 30], [18, -1], [16, -4], [-4, 31], [-5, -33], [37, -19], [-8, 6], [-50, 3], [-1, -10], [0, 34], [-35, 3], [-42, 8], [0, -21], [52, -3], [20, -19], [-13, 24], [-20, 10], [-28, 16], [12, -20], [-10, 22], [-65, -7], [-33, 25], [28, 6], [12, -19], [-24, -24], [-2, -12], [-16, -25], [4, -10], [15, -7], [50, 9], [3, -15], [-8, 8], [23, -11], [10, -24], [17, 10], [-14, -21], [21, 0], [-31, -20], [42, -6], [-37, 21], [3, -13], [42, -4], [65, 4], [12, -32], [-3, -2], [15, -14], [15, 38], [30, 24], [23, 5], [-41, -17], [-29, 7], [13, 18], [49, -1], [27, 27], [-10, 16], [61, 3], [62, 6], [-44, 20], [-27, -3], [-26, -13], [-1, -16], [-30, 0], [-38, 0], [36, 2], [-12, 30], [-6, 9], [-50, -14], [-65, -9], [-29, 33], [28, 4], [-7, -31], [29, 15], [40, -7], [22, -4], [-55, -16], [10, -13], [18, -14], [17, 29], [28, 24], [5, 35], [-27, -13], [-26, 0], [-33, -17], [35, -7], [12, 16], [59, 5], [53, -11], [16, -24], [54, 13], [-37, 9], [52, 3], [34, -29], [-3, -2], [40, 28], [-31, 19], [41, -22], [-30, -32], [38, 16], [30, -15], [-11, -21], [-15, -25], [7, 27], [-48, 4], [-12, -33], [-7, -10], [-67, 11], [-7, -16], [-3, -25], [80, 8], [44, 12], [8, -2], [-4, -2], [20, -20], [-61, 18], [-64, -10], [-26, -16], [-16, 2], [-17, -3], [36, 17], [42, 13], [19, 0], [11, 35], [-30, -16], [-22, 25], [-31, 12], [-39, -17], [-26, -3], [59, 12], [71, 5], [30, -20], [5, -13], [-6, -32], [-21, 5], [20, -38], [17, 0], [18, -29], [-28, 34], [-26, 18], [48, 10], [-20, 14], [-3, -3], [23, -34], [8, -28], [28, 5], [-47, 22], [-6, -24], [-46, 10], [44, 8], [-55, 9], [-28, -5], [-12, 50], [13, -10], [6, 34], [-56, 2], [68, 10], [-64, -22], [-32, -12], [23, 29], [-2, 30], [18, -16], [47, -30], [-47, 23], [27, -28], [0, -7], [25, 13], [48, 2], [6, 25], [23, 17], [-27, -33], [-11, 30], [68, -10], [2, -2], [-64, -4], [10, 7], [-13, -16], [46, 8], [-1, -18], [54, 5], [34, -3], [25, 36], [-13, -12], [-8, -6], [40, 2], [16, 36], [-59, 19], [-1, -39], [-48, 4], [-50, 26], [-5, 10], [-58, -16], [-78, -6], [-39, 31], [24, -15], [-26, 38], [-25, 17], [4, 36], [43, -3], [24, -8], [1, -33], [60, 0], [18, 6], [-68, -9], [-2, 12], [-38, 7]];
    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_o();".
// 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_o(: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_o();".
// 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_o( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_4140_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<7,R![-11, -2, 1]>,<11,R![-26, -2, 1]>,<13,R![22, 10, 1]>,<17,R![33, -12, 1]>],Snew);
    return Vf;
end function;