
// Make newform 4010.2.a.g in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_4010_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_4010_2_a_g();".
// 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_4010_2_a_g();".
// 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 := [-2, 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_4010_a();"
function MakeCharacter_4010_a()
    N := 4010;
    order := 1;
    char_gens := [2407, 3211];
    v := [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_4010_a_Hecke(Kf)
    return MakeCharacter_4010_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 := [[-1, 0], [0, 1], [1, 0], [0, 2], [-2, 0], [-2, -1], [0, -1], [-2, 0], [2, -1], [0, -6], [-4, 0], [6, -3], [0, 4], [0, -8], [-4, -2], [2, 3], [6, 4], [8, 0], [-4, -5], [-8, 2], [-2, -6], [-12, 4], [-4, 6], [-10, 0], [-12, -1], [4, -6], [0, 0], [8, 5], [-12, 2], [-6, 0], [2, 1], [10, 0], [16, 1], [14, 2], [2, 4], [-2, 6], [6, -1], [0, 11], [-2, 3], [-10, -6], [-10, -4], [-2, -12], [-4, 0], [12, -7], [2, 12], [16, 2], [-6, 4], [-12, 4], [-8, -1], [-2, 8], [12, 3], [-6, 6], [10, -4], [10, -8], [26, 2], [8, 0], [-4, -14], [0, 0], [-26, 3], [6, 14], [-8, 5], [-14, 3], [-20, 4], [-8, 8], [2, 6], [-2, -7], [-2, 0], [2, 14], [0, -1], [-24, 4], [8, -15], [4, -12], [-6, -3], [6, -10], [-10, -4], [-12, 2], [0, 10], [-6, 20], [1, 0], [8, -4], [-14, -12], [24, 2], [0, 4], [-30, -8], [-16, 12], [0, -7], [-14, 0], [10, 8], [20, -2], [10, -5], [8, 7], [-16, 0], [-16, 10], [-8, 8], [-12, -4], [12, -6], [-32, 0], [6, -4], [-4, -13], [-10, 0], [-4, 0], [10, -22], [0, -12], [-26, 0], [-6, -12], [30, -4], [12, 18], [-4, 29], [-18, 2], [-6, -16], [-20, 18], [30, 13], [20, 11], [-14, 0], [-20, 0], [26, -10], [-12, 4], [10, 13], [2, -16], [-2, -12], [4, 20], [8, 27], [38, 0], [-24, -11], [28, -12], [-12, -28], [-2, -12], [2, 6], [-10, -27], [2, 10], [-22, -8], [10, 25], [16, -16], [-22, 0], [32, 0], [-2, -10], [18, -12], [24, 17], [-6, -20], [-22, 20], [-16, -20], [-20, 0], [-22, 5], [8, 16], [-16, 10], [12, -2], [-26, 10], [-10, 2], [18, 4], [18, -9], [14, 13], [-26, 22], [-16, 4], [18, -17], [-20, -13], [8, 32], [0, 12], [-6, 0], [40, 7], [0, -26], [-20, 18], [26, 20], [-10, -35], [8, 0], [14, 12], [-4, -12], [24, 18], [30, 4], [-10, -14], [-18, 1], [-6, 14], [0, -18], [-10, -2], [16, 31], [0, -8], [4, -12], [18, -14], [-8, 30], [-4, -4], [12, 34], [-4, 30], [-18, 8], [-14, -2], [24, 15], [-20, -22], [-24, 26], [34, -14], [-32, 2], [30, -4], [28, -12], [-22, 18], [20, -22], [-30, 4], [-24, 26], [48, -8], [-26, 22], [-12, -12], [-34, 20], [-42, -6], [36, 2], [36, -20], [54, 6], [6, 23], [6, 18], [-46, 0], [-14, -39], [-12, 30], [12, -10], [-8, 20], [34, 8], [-30, 2], [40, -14], [-4, 18], [-56, 7], [-40, 0], [2, -18], [-34, 19], [-18, -8], [52, -2], [34, -7], [-8, -10], [-18, 18], [14, 0], [-36, -16], [-32, -4], [-34, 20], [24, 15], [8, 42], [-62, -1], [-34, -22], [-30, -8], [-24, 16], [24, -10], [62, -6], [-12, 4], [6, 1], [-18, -40], [-34, 4], [-18, -14], [-32, 8], [24, 14], [18, 24], [58, -5], [-24, 18], [34, 6], [40, -12], [58, 13], [22, 6], [-58, -8], [14, -7], [-42, 4], [-22, -4], [-2, -25], [-46, -16], [10, -44], [-26, -10], [40, 10], [36, 1], [58, -5], [-52, -13], [18, 19], [40, -8], [28, -12], [26, -34], [-6, -12], [14, -30], [-4, 48], [-18, 0], [32, -25], [18, 5], [-40, 14], [-48, 5], [6, -26], [8, -12], [38, 10], [-44, -4], [-8, 38], [-24, 16], [-10, -4], [-10, -44], [-2, -43], [28, -14], [-26, 23], [-46, 8], [-16, -44], [-4, 8], [-8, -7], [-6, -16], [6, -22], [-8, 40], [-36, -12], [-40, -8], [16, -15], [-38, 30], [-50, 6], [18, 36], [-24, -14], [42, 14], [-2, -44], [-56, -8], [-48, -5], [2, 16], [-32, 18], [16, 20], [-22, 28], [-28, 35], [36, -32], [28, 8], [24, -12], [-2, -9], [4, -50], [16, -6], [30, 0], [0, 12], [28, 12], [78, 6], [-28, -32], [-80, -2], [8, 15], [2, -20], [-8, -44], [12, -1], [-32, 4], [-20, 26], [-20, 49], [-60, 8], [42, -34], [8, -42], [38, 35], [-10, 37], [12, 24], [-38, -16], [-24, 24], [-20, -23], [-74, -6], [-24, -9], [44, 14], [12, 11], [44, 4], [-2, -21], [38, 22], [70, -8], [24, -22], [36, 28], [-46, -18], [-34, -10], [-56, 12], [16, 15], [40, -28], [10, 56], [-22, -36], [-26, 6], [46, -12], [10, 33], [-32, -40], [-64, -13], [-14, -22], [38, 32], [10, -36], [2, 23], [-30, -7], [14, 48], [-24, 24], [-6, -36], [-20, -5], [-40, -9], [42, 31], [34, 11], [-42, 34], [0, -16], [14, 6], [-18, -13], [32, 10], [30, 6], [-46, 22], [-24, 36], [8, -16], [32, -9], [14, -16], [32, 25], [-12, -30], [-16, 1], [-52, -16], [-20, -27], [22, 16], [-36, -22], [4, -22], [14, -7], [-28, -19], [-46, 25], [6, -32], [-10, -24], [38, 14], [40, 0], [72, 2], [82, 2], [-6, 38], [8, -8], [-6, -8], [60, 0], [-12, -10], [0, -55], [56, 26], [-24, 5], [18, -8], [-22, -42], [2, 18], [-34, 44], [-8, -2], [-26, -10], [28, -19], [6, 45], [-72, 6], [-38, 40], [-10, -14], [36, 26], [-72, 8], [40, 26], [-10, 0], [-34, -9], [-12, 10], [26, -54], [-10, -47], [10, 50], [-74, -2], [-2, -55], [16, 32], [-2, -24], [-8, -8], [28, 6]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_4010_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_4010_2_a_g();".
// 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_4010_2_a_g(:prec:=2)
    chi := MakeCharacter_4010_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_4010_2_a_g();".
// 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_4010_2_a_g( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_4010_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![-2, 0, 1]>,<7,R![-8, 0, 1]>,<11,R![2, 1]>],Snew);
    return Vf;
end function;