
// Make newform 4026.2.a.l in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_4026_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_4026_2_a_l();".
// 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_4026_2_a_l();".
// 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 := [-4, -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_4026_a();"
function MakeCharacter_4026_a()
    N := 4026;
    order := 1;
    char_gens := [1343, 1465, 3235];
    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_4026_a_Hecke(Kf)
    return MakeCharacter_4026_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], [-1, 0], [-1, -1], [-2, 2], [1, 0], [3, -1], [0, 2], [-4, 0], [0, 0], [3, -1], [3, -3], [-6, 0], [9, 1], [4, -4], [0, 4], [0, -2], [-1, 1], [1, 0], [4, 0], [-2, 2], [0, 2], [0, 0], [4, -4], [1, 5], [1, -3], [11, -1], [16, 0], [3, 1], [13, -3], [14, -4], [0, 8], [-1, 5], [-6, 0], [9, -5], [8, -2], [-16, 0], [11, 3], [-5, -3], [4, 4], [15, 3], [-2, -2], [11, -5], [10, -2], [-1, -5], [-6, 4], [2, 6], [11, 1], [4, 0], [-8, 4], [8, -2], [12, 6], [-20, 4], [0, -6], [23, 1], [-6, -8], [-10, 2], [15, -9], [-4, 4], [-2, 0], [0, -6], [-12, 0], [28, 2], [-15, 11], [-4, -4], [-4, -2], [3, 3], [16, -4], [23, 3], [-15, 3], [-12, -6], [-30, 0], [-9, 5], [-6, 6], [-8, 14], [19, 5], [2, -10], [4, -14], [9, 5], [15, -9], [-9, 11], [12, 0], [3, 3], [-30, -2], [-24, 2], [-9, 13], [-6, 2], [-24, -6], [-6, 12], [14, 8], [-10, -6], [27, -3], [-4, -4], [12, -12], [12, -12], [-2, -10], [10, -2], [-24, -2], [10, -16], [-25, -3], [12, -6], [-9, -3], [-9, 11], [-24, 0], [-1, 11], [-4, 0], [-4, 6], [-27, -5], [34, -8], [12, -12], [-30, 0], [-21, 9], [17, 1], [27, 3], [1, -9], [0, -4], [6, 12], [18, -6], [-2, -14], [0, 6], [31, 5], [-15, 5], [-13, 7], [14, -8], [22, -2], [25, -1], [11, 7], [-9, -1], [32, -4], [32, 0], [2, 12], [-11, 15], [-21, 1], [10, -10], [-24, 6], [-24, -6], [-3, 13], [-11, -7], [9, -5], [18, 0], [-21, 15], [20, 12], [-6, 12], [-19, -5], [20, -4], [0, 6], [-19, -5], [13, 5], [3, -9], [15, 9], [-51, 3], [-24, 6], [24, -6], [-40, 4], [-16, 16], [8, -12], [3, -3], [-27, 7], [-24, 18], [10, 0], [15, 3], [0, -20], [22, -4], [-45, 9], [-36, 0], [-12, 22], [-34, 2], [26, -18], [20, -14], [-41, 3], [36, -14], [-10, 14], [22, -16], [35, -7], [-10, 4], [8, -12], [-32, 10], [-36, 0], [-4, 2], [-8, 0], [-42, 0], [13, -21], [12, -24], [-8, 6], [-14, -4], [37, -13], [24, 6], [6, 8], [-13, -3], [10, 12], [37, 7], [16, 2], [17, -29], [13, 11], [-5, -5], [22, -2], [6, -12], [-8, 18], [-37, 3], [26, -4], [-22, -10], [46, 8], [46, -6], [-39, -3], [-39, 1], [-12, 8], [17, 5], [41, -17], [6, -18], [28, -2], [6, 6], [14, -20], [38, 4], [-22, 6], [-3, -21], [59, -7], [52, 6], [23, -27], [-18, -16], [-9, 1], [-38, -4], [33, 1], [35, -15], [-3, 17], [-30, 6], [6, -18], [-8, -18], [-39, 13], [24, 20], [-42, 18], [-20, -4], [-15, -3], [20, -24], [38, -6], [30, -12], [1, -9], [27, -15], [-22, 24], [-1, 11], [-13, 13], [-25, 25], [-13, 13], [-20, -12], [15, -15], [30, -12], [28, -26], [24, 0], [4, -12], [-6, -14], [-36, -12], [-8, -8], [32, -18], [21, -3], [36, 12], [-54, 0], [44, -22], [-36, 24], [-17, -9], [-44, 8], [-46, -4], [45, -11], [-35, 15], [3, 17], [-2, -24], [-11, 21], [-34, 20], [47, 9], [-1, 23], [22, -4], [-58, 6], [-16, 14], [18, 0], [17, 7], [25, -3], [48, 0], [-1, -21], [4, -12], [49, -17], [10, -28], [6, -16], [-37, 1], [35, -7], [12, 28], [48, 0], [-16, 6], [5, 15], [-9, -7], [-44, -10], [46, 8], [40, 0], [-10, 20], [-32, -2], [-44, 16], [45, -3], [18, -30], [-13, -17], [-16, 14], [4, -28], [34, -4], [-4, -20], [-12, 12], [-38, 0], [-6, 12], [64, -16], [-20, -8], [-38, 18], [-30, -16], [-32, 28], [-12, -14], [-24, 8], [-12, 34], [40, 8], [42, -24], [-14, -8], [29, -21], [38, -22], [52, -18], [-48, -12], [-52, 4], [9, 21], [6, 4], [24, -4], [32, -14], [14, 16], [-11, 3], [-24, 34], [24, -22], [56, 12], [44, 16], [-16, -8], [66, -12], [29, -3], [-62, 16], [-10, 10], [-3, -9], [13, -25], [-3, 11], [12, 10], [-23, -23], [-10, 36], [-18, -30], [-48, 22], [21, 1], [-24, 22], [-21, 45], [46, -28], [-44, 16], [-6, 24], [-70, 10], [-32, 16], [25, -11], [0, -8], [36, 6], [30, -16], [16, 0], [-10, 0], [54, 16], [29, -37], [48, -20], [12, 14], [-38, 14], [-30, -12], [-42, 24], [48, 0], [-59, -1], [-25, 9], [-50, -12], [-41, -5], [-4, 12], [-24, -6], [-57, 17], [-46, -6], [-36, 36], [30, 16], [-16, 0], [-8, -10], [67, -11], [20, -4], [-45, -9], [6, 24], [-78, 4], [-18, 24], [9, -3], [-12, -20], [15, 3], [-8, 20], [46, 2], [-18, -22], [-9, -1], [26, 18], [-57, 25], [49, -3], [-2, 4], [22, -34], [-6, 10], [-16, 44], [-67, -11], [70, -6], [38, -20], [83, -3], [-90, 0], [-11, -27], [9, -11], [-19, 31], [62, -4], [29, 5], [53, 7], [32, -42], [-34, -16], [-25, -15], [0, -12], [-60, 22], [14, 8], [69, 3], [10, 2], [2, 4], [-18, 8], [-54, -18], [8, -24], [9, -27], [-8, -8], [-10, 20], [-24, -10], [-45, -3], [-44, 16], [-29, 11], [-30, 12], [-43, -13], [-91, 5], [-42, 14], [33, 3]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_4026_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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