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