
// Make newform 4026.2.a.n in Magma, downloaded from the LMFDB on 26 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_n();".
// 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_n();".
// 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, -3, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [0, 1, 0], [-2, 0, 1]];
    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_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, 0], [1, 0, 0], [-1, -1, 0], [0, -1, 1], [1, 0, 0], [-1, 0, -1], [1, 2, 0], [-2, 2, -3], [-2, 0, -1], [2, 1, 3], [-5, 3, 1], [-2, -2, 5], [-5, 2, 0], [-2, -1, -4], [3, 7, -2], [2, 5, -6], [-3, -5, 1], [-1, 0, 0], [-9, 3, -7], [5, 1, -5], [2, -1, 7], [-1, 0, 1], [2, -7, 1], [1, 3, 4], [3, 5, -3], [3, -5, -2], [-9, 3, -4], [3, -6, 9], [1, -4, -7], [-8, -7, 8], [-5, -2, 8], [-6, -7, 6], [-3, -6, 4], [-9, 0, -5], [9, 0, -6], [2, -4, 4], [-6, -9, 5], [-6, -5, 5], [4, 9, 2], [0, 1, -5], [-1, -11, 6], [-9, 4, 4], [-7, -4, -3], [5, 1, 3], [-8, -3, 5], [-8, -14, 6], [0, 2, 2], [-2, -11, 10], [2, 1, 1], [2, -14, 9], [-6, 6, -8], [3, 7, -8], [-8, -4, 5], [0, -3, 13], [-7, -13, 5], [-4, 1, -9], [-13, 0, 0], [-2, -2, 1], [5, 6, -13], [-2, -7, 13], [0, -1, 8], [18, 11, -6], [6, -16, 1], [-4, 8, -11], [6, 4, -3], [16, 3, -8], [-5, 11, 7], [-6, -8, -8], [7, 5, -2], [4, 18, -18], [-3, -2, -11], [3, -12, 2], [-15, 14, 8], [7, 13, -9], [-5, 20, -2], [5, -2, -12], [0, -9, 1], [-7, 7, 11], [0, -10, 2], [-3, 5, 9], [-13, -16, 7], [6, -8, -13], [1, -16, 22], [-5, -10, -4], [-11, 4, -7], [-3, 9, -3], [9, 22, -14], [-1, -7, -7], [-6, -14, 8], [1, 2, 11], [-4, 6, -11], [4, 3, -6], [-2, 13, 13], [12, 15, -16], [-13, 6, 3], [19, 15, -15], [-22, 6, -9], [-12, -15, 11], [-18, -1, -8], [10, 9, -8], [-1, -8, -13], [11, -5, 25], [-5, 18, -18], [-11, 7, 11], [-11, 5, 6], [30, 2, -3], [-9, 5, 5], [-6, -9, 0], [-9, -12, 3], [-9, 18, 2], [-5, -19, 24], [4, -14, 15], [-8, -11, 1], [-13, 5, -16], [7, 7, -17], [-4, -6, -16], [9, -13, -14], [6, 1, 4], [-1, 2, 8], [6, -2, 14], [8, 28, -23], [23, -5, 6], [5, 15, -10], [-14, -4, -2], [-13, 15, -35], [6, -4, 17], [22, 16, -6], [5, 18, -13], [-11, -19, 1], [-13, 3, -17], [-17, -18, 13], [6, 4, 18], [-11, -5, 10], [20, -17, 1], [13, 8, -21], [22, -8, -1], [-17, 22, -1], [-3, 13, -30], [11, 24, -25], [44, -2, 4], [-26, 5, -17], [1, -8, 16], [1, -11, -14], [13, 8, -19], [3, -13, -9], [-6, -4, 14], [22, -8, 17], [23, 3, -14], [9, 6, -23], [-9, 13, -6], [1, 3, 26], [-15, 3, 22], [6, -14, 27], [5, -12, 21], [21, 4, 11], [32, 19, -16], [22, -1, 1], [3, -20, 24], [-7, 8, -1], [3, 6, -8], [23, -14, 29], [-1, 13, -7], [22, 19, -7], [-41, 7, 3], [13, -5, -4], [19, 22, -25], [-13, -4, -1], [31, -5, 15], [33, -1, 10], [15, 10, -1], [8, 26, -7], [25, 22, -6], [-9, -6, 27], [1, 19, -18], [7, 3, 24], [39, 3, -3], [-5, -10, 5], [-21, -17, 16], [11, -2, -17], [-34, 0, 13], [18, 2, -6], [10, -16, -7], [-18, 1, 11], [-7, -6, 5], [-9, -25, -1], [12, -29, 9], [5, -8, -17], [-1, -18, 21], [-3, 6, -23], [-18, -18, 9], [23, -4, 29], [-11, 28, -31], [-13, 14, -14], [-29, 2, 10], [6, 35, -4], [-9, -12, -8], [-20, 19, -4], [23, -6, 1], [-16, -17, -2], [-17, 11, 17], [14, -23, -6], [8, -12, 12], [38, -9, 6], [39, 19, -9], [10, 10, -12], [-18, -31, 17], [-25, -15, -1], [-12, 25, 10], [-5, 13, 20], [-34, 5, 9], [35, -16, 12], [-41, 12, -8], [27, -9, 17], [4, -14, -15], [23, -10, 19], [8, 31, -2], [-23, 5, -16], [-3, -15, 10], [12, 34, -7], [-1, -22, -14], [16, 14, 11], [-28, -16, 12], [-9, 18, -22], [-27, 0, -9], [25, 29, -5], [63, -3, -1], [-15, -4, 11], [-6, 6, -36], [-39, -20, 12], [40, -8, -7], [-40, -11, 7], [1, -17, 25], [-12, -34, 31], [-15, 22, -33], [19, 1, -20], [-10, 4, -33], [-13, -29, 4], [34, 20, -16], [-8, 27, -14], [-37, 6, 14], [-35, -9, 16], [-48, 13, -7], [-26, 18, -23], [21, -20, -13], [-10, -11, 9], [-27, -1, 27], [11, -15, 8], [-34, 28, -6], [26, -8, 32], [-27, -18, 0], [19, 14, -19], [10, 26, -22], [6, 24, -4], [29, -17, 16], [-29, 24, -17], [-21, -36, 24], [-15, 9, -38], [-2, -12, 12], [6, -5, 4], [15, 19, 4], [16, 47, -29], [-26, -14, -9], [1, 7, -32], [53, 5, -13], [-37, 20, -8], [4, 0, 3], [18, 4, 27], [1, -3, -29], [-51, -1, -3], [26, -13, 31], [-36, -22, 19], [-22, 8, -45], [9, -9, -34], [40, 7, -9], [5, 11, 2], [-22, 18, -10], [-4, 24, -19], [11, -13, 31], [-63, -3, -6], [-43, -13, 7], [-4, 11, -21], [-40, 23, -19], [19, -9, 15], [-3, -19, -17], [-13, -4, -21], [-26, 13, -49], [47, 1, 20], [-35, -3, -20], [13, -7, 47], [-63, 4, -12], [28, 7, 13], [27, 1, -23], [6, 19, 3], [17, 17, 4], [-3, 6, 5], [-5, 16, -1], [-26, 20, 5], [7, -13, -6], [-71, -5, 0], [-26, 32, 9], [-30, 2, -14], [9, -9, -11], [0, -14, 2], [23, -20, 55], [-42, -29, 27], [-34, -5, 9], [35, -11, -7], [0, -9, -7], [53, 5, -6], [-18, 3, 29], [-31, 38, 3], [-1, -1, -22], [-46, -29, 17], [-32, 23, -13], [7, 11, -14], [-14, -35, 44], [-4, -27, 31], [3, 20, -1], [30, 25, -19], [26, 0, -10], [-36, 21, -43], [23, 37, -13], [-32, -9, 4], [2, 5, 9], [26, 7, 30], [3, -6, -23], [-6, 21, 24], [-14, -42, 2], [0, -14, 27], [-5, -3, 25], [25, -17, -18], [2, -34, 36], [39, 41, -17], [-17, -11, -22], [-43, -9, -6], [15, -45, 16], [12, 26, -38], [9, 12, 7], [0, -31, 26], [16, -22, 38], [3, -7, -21], [63, -15, 0], [-21, -5, 4], [20, 24, -34], [33, -23, -10], [-11, 21, -43], [24, 23, -15], [23, -15, -20], [25, -11, -6], [-28, 25, 28], [-8, 16, -15], [2, -1, 27], [16, -30, 41], [-35, -5, -21], [-12, 23, 5], [59, -18, 2], [-30, -46, 26], [-45, 15, -20], [4, -1, -36], [-53, 4, -1], [14, 23, -33], [25, 47, -43], [-37, 11, 19], [-13, -20, 42], [-26, 29, -10], [19, 25, -25], [14, -29, 24], [5, -11, -11], [-37, 40, 11], [10, -2, -19], [-3, 40, -31], [-29, 38, -3], [-18, 36, -42], [14, -3, -3], [25, 0, 29], [-2, -17, 35], [24, -11, 20], [-4, -18, 24], [-4, 22, -9], [65, -8, 16], [-48, -36, 9], [17, 20, 17], [7, -18, 8], [57, -1, -16], [4, 38, -21], [-16, 27, -38], [-13, -36, 38], [10, 8, -10], [-23, -31, 1], [1, -37, -7], [54, -20, -6], [21, -12, 10], [22, 10, 4], [-13, 24, -1], [27, -5, 34], [-11, -27, 29], [1, -22, -12], [16, 60, -47], [28, 10, 0], [4, 27, -14], [-32, -6, 18], [-10, -18, 3], [3, -19, -29], [-10, 1, -25], [-67, 7, -28], [-28, 32, -55], [55, 7, -14], [0, -12, 17], [12, -18, 18], [-45, -6, 23], [1, -46, 2], [42, -18, -14], [-18, 9, 21], [-17, 35, 30], [-13, -27, 0], [-20, 38, -34], [-50, -4, -5], [7, -37, -1], [24, 0, 10], [20, -37, 33], [-53, 3, -35], [-31, 40, 22], [-3, -17, 46], [-24, 10, -59], [42, -12, -17], [55, -9, 30], [-6, -9, -9], [-11, -26, -6], [22, 3, 13], [19, -2, -28]];
    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_n();".
// 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_n(:prec:=3)
    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_n();".
// 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_n( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_4026_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![-1, 0, 3, 1]>,<7,R![-1, -3, 0, 1]>,<13,R![-3, 0, 3, 1]>],Snew);
    return Vf;
end function;