
// Make newform 4012.2.a.d in Magma, downloaded from the LMFDB on 23 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_4012_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_4012_2_a_d();".
// 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_4012_2_a_d();".
// 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_4012_a();"
function MakeCharacter_4012_a()
    N := 4012;
    order := 1;
    char_gens := [2007, 3777, 3129];
    v := [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_4012_a_Hecke(Kf)
    return MakeCharacter_4012_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, -2], [2, -1], [-3, 0], [0, 1], [-2, -1], [-1, 0], [-1, 2], [4, -1], [1, 3], [-9, 0], [3, 0], [9, 0], [-3, 1], [-1, 3], [-5, 6], [1, 0], [1, 4], [-1, -6], [5, 5], [-7, 10], [-8, 5], [1, -4], [1, 7], [-13, 5], [5, -8], [-16, 4], [-12, 9], [4, -6], [7, 6], [-3, -4], [-12, -6], [13, -10], [11, 0], [5, 6], [-11, 1], [-4, -3], [5, 9], [0, 3], [15, -6], [10, -17], [0, 4], [16, -4], [7, -18], [4, -1], [-8, -8], [-11, 12], [-17, -3], [-13, 7], [-9, 7], [16, -18], [12, -19], [10, -19], [-16, 15], [13, -22], [19, -5], [-7, 1], [1, -14], [-19, 11], [-5, -3], [-1, -13], [8, -13], [5, 14], [-29, 0], [-6, -15], [18, 7], [11, -5], [14, -7], [-28, 2], [-4, 4], [0, -1], [10, -2], [4, -17], [1, -2], [2, -11], [-6, 12], [6, -10], [-10, 3], [11, -10], [-2, -18], [2, 22], [-6, 12], [7, 18], [-27, 3], [0, -1], [13, -24], [-11, -4], [-17, 18], [-7, 28], [5, 14], [-12, 29], [17, -20], [-9, 12], [-18, 24], [-8, -2], [9, 1], [-2, -4], [-26, 8], [2, -3], [13, -15], [-11, 10], [8, -33], [17, 0], [15, -22], [-6, -3], [-32, 0], [-9, 0], [-6, -20], [33, -8], [-3, 7], [-23, 13], [29, -7], [28, 0], [-7, 17], [-5, 25], [16, -7], [-9, -4], [25, 11], [-29, 24], [34, -14], [27, -34], [3, -4], [18, -25], [-11, 20], [-9, 28], [20, -22], [14, -13], [-12, -4], [20, -29], [-30, 13], [-18, 38], [-39, -2], [-16, 12], [14, 12], [-28, 20], [12, -37], [17, 9], [9, -27], [3, 4], [-26, 6], [19, -17], [25, -7], [23, -24], [-37, -12], [0, 0], [-22, -18], [45, -14], [25, 1], [23, 6], [10, -25], [1, 13], [-33, 21], [-26, 5], [-13, 40], [-20, 17], [5, -9], [-37, -3], [-4, 20], [14, -28], [10, -3], [6, -32], [-10, 7], [-9, 37], [-13, -15], [-23, 43], [-5, -9], [43, -6], [-31, -3], [34, -44], [-3, 2], [-17, 29], [-15, -2], [0, -8], [-5, -11], [12, -20], [32, -38], [-28, 51], [27, -42], [32, -1], [-39, 0], [38, -12], [0, 20], [-12, 45], [-43, -7], [5, -24], [-20, 10], [-21, 40], [-25, 23], [47, -4], [-23, 22], [-6, 11], [-40, 8], [10, -46], [-43, 14], [-35, -2], [21, -16], [27, 8], [4, -35], [-32, 22], [-4, 25], [3, 22], [43, 16], [5, -2], [-35, 28], [27, -23], [3, 9], [-3, 8], [7, -12], [8, -28], [-5, -28], [-22, 25], [-2, -9], [-24, -4], [0, 17], [-1, -30], [-18, 24], [-12, -35], [28, -47], [-31, 4], [-34, 20], [29, -53], [3, -12], [37, -6], [-14, 26], [11, 0], [28, -31], [3, 11], [45, -20], [-32, 8], [-35, -9], [-25, 5], [3, -46], [-9, 12], [-3, 42], [-9, -19], [9, 18], [37, -28], [35, -26], [-7, -22], [35, -25], [8, 22], [63, 1], [-32, -9], [-33, 25], [-14, -4], [12, 32], [43, -24], [-62, 24], [3, -18], [9, -25], [-24, 9], [-2, 6], [-40, 37], [60, 1], [21, -4], [-10, -7], [-15, -38], [49, 3], [-8, 33], [36, -35], [43, -47], [-40, 8], [-4, 5], [23, -42], [-53, 34], [36, 10], [-31, 59], [-36, -9], [-40, 26], [19, -20], [-4, -31], [-22, 25], [-3, -26], [0, -32], [7, 25], [14, 27], [-16, -7], [-28, -6], [-57, -14], [-33, -4], [-28, 2], [-67, 9], [-27, 28], [-11, 36], [32, 10], [28, -22], [16, 36], [8, -26], [19, -60], [-23, 63], [40, 24], [-15, 27], [-37, -9], [-6, -5], [-50, 36], [-10, -18], [-26, 58], [10, -11], [-44, 17], [43, -38], [-22, -9], [6, 22], [-59, 34], [-59, 22], [-67, 14], [-2, 46], [14, 15], [10, -10], [-24, 48], [-2, -39], [17, -21], [-21, 40], [-13, 12], [-34, 31], [-33, 73], [17, 41], [-28, 6], [-14, -6], [-12, -11], [2, 4], [-5, 15], [-42, 45], [19, -53], [-17, 40], [-24, 1], [-37, -4], [-45, -10], [-5, -44], [-4, 16], [17, -63], [-45, 6], [64, -29], [57, -18], [-56, -15], [-16, 63], [17, 17], [7, 46], [-15, 29], [-35, -2], [-36, 24], [26, -29], [-22, 5], [-14, -9], [57, 2], [-6, 36], [49, 9], [2, -9], [34, -60], [0, -16], [-21, 8], [-8, 5], [-7, -18], [-25, -9], [7, -2], [-37, 62], [-3, 16], [7, 27], [55, -26], [-59, 9], [63, 21], [34, -53], [10, -22], [2, -33], [18, -25], [-67, 34], [19, -34], [-17, 58], [-55, 38], [17, 6], [-2, 4], [-63, 27], [64, -32], [-36, 48], [34, -7], [49, 17], [-2, 2], [53, -27], [15, 31], [-11, -16], [-33, 10], [33, 30], [-47, 20], [-31, -38], [70, 6], [30, 8], [-5, 3], [-7, 8], [27, 29], [-59, 50], [-22, 71], [-8, -49], [66, -5], [52, -11], [-21, 52], [-56, -19], [21, -9], [62, -23], [-36, 55], [-18, -19], [-4, 39], [-4, 42], [-24, 46], [-40, 83], [19, 37], [60, 0], [-68, -3], [-6, 43], [24, -4], [74, 5], [26, -62], [8, 34], [49, -62], [-73, -10], [75, -21], [-15, -26], [19, -30], [57, -72], [59, -34], [-3, -57], [-6, -40], [7, 36], [9, 18], [27, -32], [21, -24], [-87, 0], [-69, 56], [3, -59], [-1, 29], [8, -55], [96, -15]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_4012_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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