
// Make newform 4012.2.a.b in Magma, downloaded from the LMFDB on 19 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_b();".
// 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_b();".
// 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
        Kf := Rationals();
    end if;
    return [Kf!elt[1] : elt in input];
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], [-3], [-1], [-5], [-2], [4], [1], [-3], [-4], [-3], [8], [0], [3], [4], [8], [7], [-1], [10], [10], [-8], [-10], [13], [-4], [14], [2], [2], [-16], [-3], [2], [-12], [-7], [4], [3], [-4], [-18], [-14], [-20], [16], [-17], [-6], [20], [1], [18], [3], [-18], [-5], [6], [0], [18], [-14], [4], [11], [-11], [-21], [-27], [-21], [12], [29], [11], [27], [20], [-11], [-25], [-13], [-2], [6], [17], [8], [0], [-24], [-12], [17], [20], [26], [-5], [-36], [10], [-2], [-32], [16], [0], [32], [0], [-33], [12], [-12], [33], [8], [-18], [-4], [-6], [-16], [-13], [27], [-19], [6], [-8], [-26], [11], [8], [-12], [-1], [-44], [28], [-22], [-23], [-38], [1], [33], [-28], [31], [26], [-7], [11], [-32], [-30], [-23], [-9], [11], [-14], [-13], [-2], [22], [-6], [44], [36], [25], [-26], [48], [42], [-8], [-40], [-38], [9], [-15], [-42], [-14], [44], [54], [16], [-28], [46], [-46], [20], [45], [30], [25], [18], [48], [-6], [-35], [24], [51], [-18], [17], [-37], [40], [-38], [12], [8], [45], [-54], [-12], [-57], [-38], [-50], [2], [1], [-10], [10], [-12], [4], [-25], [20], [37], [49], [-41], [38], [-8], [11], [-7], [-19], [-50], [-57], [-24], [-18], [64], [36], [-24], [50], [-64], [-46], [-22], [-54], [-23], [-42], [-26], [30], [42], [42], [-62], [-59], [10], [-40], [-69], [-24], [-20], [-8], [-32], [4], [-58], [-65], [57], [-69], [-33], [18], [-16], [-6], [2], [-50], [-20], [-12], [6], [67], [-28], [6], [55], [-32], [22], [13], [-32], [24], [26], [62], [26], [-25], [34], [-48], [-50], [28], [9], [60], [-29], [21], [-63], [-16], [36], [40], [59], [-31], [-7], [-4], [22], [-75], [9], [-44], [13], [32], [-70], [14], [24], [9], [67], [-66], [21], [50], [45], [-8], [5], [-19], [-20], [-20], [-58], [-41], [-57], [66], [16], [7], [28], [-69], [-64], [52], [20], [42], [30], [-26], [38], [31], [18], [-33], [12], [-52], [54], [-50], [61], [32], [-48], [49], [-54], [-64], [21], [58], [36], [-72], [61], [-52], [75], [-2], [-12], [-52], [69], [-69], [9], [58], [-21], [30], [26], [23], [-14], [30], [-60], [54], [35], [-55], [-7], [-46], [16], [-68], [82], [-78], [80], [12], [26], [-69], [-44], [-1], [-15], [38], [-32], [-43], [-86], [32], [-76], [12], [70], [-34], [80], [-17], [20], [22], [-10], [-15], [-27], [-66], [54], [6], [42], [41], [-11], [-81], [-32], [15], [-63], [34], [20], [64], [-12], [-61], [22], [44], [56], [24], [-2], [26], [7], [-44], [92], [-2], [-72], [73], [-59], [-60], [24], [6], [-81], [-46], [-93], [10], [-100], [78], [-46], [-44], [30], [-28], [6], [24], [-77], [-33], [-45], [-51], [-58], [-84], [-11], [18], [-39], [-78], [40], [19], [-26], [24], [-37], [18], [-100], [-1], [59], [-11], [34], [88], [36], [45], [-24], [74], [-24], [-59], [71], [21], [-4], [-50], [-17], [60]];
    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_b();".
// 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_b(:prec:=1)
    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_b();".
// 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_b( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_4012_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![3, 1]>,<5,R![1, 1]>],Snew);
    return Vf;
end function;