
// Make newform 4012.2.a.f in Magma, downloaded from the LMFDB on 25 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_f();".
// 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_f();".
// 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, -4, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [-1, 2, 0], [-5, -2, 2]];
    Rf_basisdens := [1, 1, 1];
    Rf_basisnums := ChangeUniverse([[z : z in elt] : elt in Rf_num], Kf);
    Rfbasis := [Rf_basisnums[i]/Rf_basisdens[i] : i in [1..Degree(Kf)]];
    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], [-1, 0, 0], [0, -1, 0], [1, 0, 0], [-2, 0, 0], [1, 1, 0], [1, 0, 0], [-2, 1, 0], [0, -1, -1], [-2, -1, 0], [2, 1, -1], [3, 2, -1], [-2, 1, 0], [-2, 1, -1], [-4, 1, 1], [5, 0, 2], [-1, 0, 0], [-3, -1, 0], [-2, 2, 2], [-2, 0, 2], [2, 0, 0], [-5, 0, 0], [-1, 2, -1], [4, 1, -1], [-3, 1, -2], [-7, -2, -1], [-7, 3, 2], [1, -2, 0], [6, -2, -2], [-1, -3, -2], [6, -3, 0], [-12, -2, 2], [5, -2, 0], [-2, 0, 2], [-5, 1, 0], [-11, 0, 1], [-1, 1, -2], [2, -4, -2], [-3, 2, 2], [9, 1, 2], [-9, -1, 0], [-2, -1, 4], [-10, -3, 1], [-2, -3, 0], [0, 2, 4], [-9, 0, -2], [0, 3, 1], [-16, -2, 2], [17, -2, -1], [-7, 0, -3], [-9, -4, 1], [-4, 1, 0], [-14, 3, 0], [4, 3, -2], [9, 2, -2], [0, 1, 0], [7, 2, 3], [-10, -3, -4], [10, 1, 0], [5, 0, 6], [-1, -1, -4], [-11, 6, 2], [18, 1, -2], [-15, 0, 0], [11, 1, 0], [4, -4, -2], [-22, 1, 0], [-2, -4, 2], [-9, 0, 5], [1, 2, 1], [5, 4, -1], [-4, 1, 2], [-11, 0, -5], [-4, 0, -2], [-11, 4, 0], [-6, -2, -4], [-6, -2, 2], [13, -1, -4], [-9, 1, 2], [13, -1, -2], [-23, -1, 2], [4, -1, -5], [24, 2, 2], [-17, -2, 2], [-6, 0, -2], [18, 5, -1], [8, 1, 4], [3, -8, 1], [26, 2, -2], [0, 9, 1], [8, -2, 0], [-16, -6, 2], [-3, 2, 6], [2, -7, 2], [3, -2, -2], [-29, -4, 3], [3, -5, 0], [10, 2, -2], [-16, 5, 4], [4, 1, -7], [4, 0, 0], [17, 6, 0], [-4, -2, 2], [9, 0, -5], [15, 2, 9], [25, 0, 0], [12, -5, -3], [-1, 4, 6], [-2, 1, 4], [6, -10, -4], [13, -8, -4], [-12, 5, -1], [-14, -1, -4], [11, -4, -2], [-8, -2, -6], [20, 2, -4], [-17, -6, 6], [-16, 7, 2], [-2, 9, 0], [9, -8, -3], [3, 6, -2], [0, -5, -7], [-16, 2, 4], [-11, 1, -6], [0, -3, 5], [10, 1, -5], [26, 1, 2], [-1, -11, 0], [-2, -2, -4], [-8, 0, 6], [-3, -9, -6], [-16, 4, -4], [-30, -2, 2], [-33, 4, 2], [-7, -2, -6], [9, -1, 0], [-13, -2, -7], [-18, -2, -4], [-3, -7, 2], [7, -2, -5], [3, 6, -5], [5, -2, 3], [29, 1, -2], [-10, 4, 2], [7, -4, 6], [-21, -4, -1], [18, 9, -2], [18, 1, -3], [2, -7, -5], [24, 2, 4], [16, 7, 2], [-11, 8, 3], [-18, 1, -6], [8, 8, 2], [27, 4, 0], [3, -12, -6], [27, 0, 5], [-24, 4, -2], [-4, -12, 0], [3, -7, 6], [-7, 6, 0], [-22, 2, 6], [15, -4, -3], [16, 1, 4], [-24, 9, 3], [4, -8, -2], [19, -4, -1], [-10, -13, 0], [21, 0, 1], [14, 5, 5], [14, -2, 8], [-16, 3, 3], [-9, -2, -12], [-20, -2, -6], [-2, -1, -2], [6, 1, -6], [11, -2, -4], [36, 3, -3], [-42, -4, 2], [5, 2, -8], [6, -1, -2], [-3, -10, 0], [13, 4, 5], [27, -4, -4], [20, 4, 8], [34, 5, -3], [18, 3, -7], [-10, -2, 8], [-18, 5, 7], [-2, 3, 7], [-30, -5, -3], [7, -5, -6], [-5, -3, 4], [-34, -6, 2], [-9, 2, -6], [-8, 6, 8], [-24, -4, -6], [-8, -3, -9], [-36, 1, 3], [1, -7, 6], [16, 8, 2], [23, -2, -2], [10, 6, -10], [16, 1, -7], [-18, -7, -10], [8, 8, 8], [8, 7, -5], [2, -3, 11], [-11, 14, 5], [47, 1, 2], [10, -8, -4], [13, -6, 4], [-53, 2, 2], [-24, -7, -4], [-35, 6, 6], [18, 7, 11], [32, 4, 4], [-32, 2, -4], [11, 0, 7], [-26, 6, 6], [26, -7, -5], [44, 2, 2], [-31, 9, 2], [-15, 0, -12], [7, -3, -10], [25, 1, 2], [-4, -5, 4], [24, 6, 6], [-28, 1, 7], [7, -6, -10], [24, 5, 9], [9, -6, -3], [-15, -3, 6], [-26, -6, -2], [-13, -9, 10], [-24, 15, 6], [-13, 0, 3], [16, -12, 0], [-19, -9, 8], [-10, 10, -4], [-19, -6, 0], [-8, 0, -12], [24, 3, -10], [7, -10, -8], [-38, -3, -10], [22, -2, 4], [-21, 3, -4], [23, -13, -4], [-4, -9, -6], [-38, -1, 6], [25, 10, -6], [-34, -3, 7], [-37, -1, -10], [40, -3, 0], [-41, -4, -10], [-28, -8, 8], [6, 3, 8], [-17, 2, -5], [-16, 4, -10], [8, -4, 10], [9, -5, 6], [8, -7, 2], [-26, 3, 10], [0, -6, -4], [-10, -15, 2], [44, -5, 1], [-15, -2, -6], [-9, -9, 0], [15, 6, -2], [-45, 0, 6], [-9, 5, -10], [36, 2, 2], [29, 10, -5], [46, 5, -2], [-37, 0, 4], [-44, 0, 6], [-12, 6, -2], [-17, -14, -2], [0, -6, 6], [-16, -7, 0], [0, -6, -10], [41, 9, 0], [-1, 6, -13], [-12, -8, 10], [22, 0, 0], [20, -3, 7], [-11, -9, -8], [46, -5, -2], [-1, 9, 0], [3, 12, 0], [-20, 4, 8], [-18, -6, 4], [6, 6, 6], [5, -1, 8], [24, -3, -4], [-22, 3, 1], [20, 0, 8], [-17, -12, -6], [-31, 0, 5], [19, -11, 2], [-13, -6, -4], [-19, -7, -14], [-16, 6, 6], [-57, -8, 5], [11, 12, -4], [28, -1, -1], [10, -3, -6], [29, 2, 15], [13, -10, -11], [29, 9, -4], [-15, -14, 0], [40, 9, -2], [8, -9, -10], [5, -14, -9], [-10, 15, 12], [5, 9, 6], [-26, -5, -5], [-33, -12, -2], [-30, 0, 4], [40, 10, -8], [-22, -8, -2], [-26, -7, 1], [7, 12, -4], [-58, -1, 2], [-20, -15, -4], [-32, 13, 3], [-25, 3, -12], [-26, 4, 2], [10, -5, 7], [-17, 6, 5], [-7, 11, 6], [-11, 2, -11], [33, 13, -10], [8, 11, -10], [12, -10, -10], [-1, -12, -6], [47, 0, -6], [45, 2, 7], [-52, 2, -6], [30, -7, 8], [-16, -6, -12], [32, 7, -9], [-38, 1, -5], [-37, 12, 5], [-3, -7, 2], [-37, 14, 5], [-3, 3, -2], [8, -7, 8], [29, 9, 8], [-8, -11, 7], [3, -4, -1], [12, 11, -2], [-9, 2, 0], [-34, -1, 7], [14, 10, -6], [-17, 9, -8], [15, 11, 6], [-5, 10, 10], [18, 9, 18], [4, 13, -4], [31, 12, 9], [2, 5, 8], [-30, 9, 0], [9, 6, 15], [26, -18, -4], [-29, 8, 5], [-31, 8, -1], [-18, -11, 10], [-36, 2, 4], [26, 0, -2], [8, 3, 3], [35, 6, -5], [34, -14, -2], [-38, -3, 5], [-10, 3, 10], [0, -23, -3], [-46, 7, -3], [45, 14, -5], [47, 6, -9], [-37, -2, 8], [-8, -3, -4], [14, -3, -5], [-60, 8, 4], [-25, 10, 9], [-7, 2, 10], [-33, -15, 0], [16, -11, -4], [26, 6, -6], [50, 12, -2], [-40, 7, 9], [-59, -2, -9], [6, -3, 3], [71, 5, -4], [55, 2, 11], [20, 12, 10], [-8, 4, 0], [44, 7, 6], [27, -2, -2], [35, 4, -2], [21, 4, -4], [-22, -8, 16], [-1, -2, 3], [0, -13, -16], [-42, -8, 4], [31, 4, 10], [-2, 4, -16], [9, 5, -12], [23, 20, 0], [-28, 8, -6], [-32, 6, -10], [14, 11, -2], [-20, -6, 4], [-46, -3, 3], [-51, -8, 4], [-51, -6, -8], [-22, -5, -4], [16, -10, -8], [6, 5, -5], [40, -2, 10], [-2, -3, -8], [68, -5, -1], [-10, 6, 10], [-44, -4, -16], [-74, -1, 6], [-12, 1, -10], [3, -8, -10], [-56, -14, 6], [20, 6, 20], [-30, -3, -6], [12, -6, -6]];
    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_f();".
// 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_f(:prec:=3)
    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_f();".
// 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_f( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_4012_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![1, 1]>,<5,R![9, -17, -1, 1]>],Snew);
    return Vf;
end function;