
// Make newform 8015.2.a.e in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_8015_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_8015_2_a_e();".
// 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_8015_2_a_e();".
// 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_8015_a();"
function MakeCharacter_8015_a()
    N := 8015;
    order := 1;
    char_gens := [3207, 4581, 4586];
    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_8015_a_Hecke(Kf)
    return MakeCharacter_8015_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], [2], [-1], [-1], [-1], [-5], [2], [4], [-4], [-4], [9], [7], [1], [-3], [3], [-5], [4], [2], [12], [9], [-14], [12], [6], [9], [-10], [-14], [12], [12], [-2], [14], [20], [-9], [-12], [19], [7], [-9], [-1], [24], [-12], [12], [-26], [12], [-14], [-3], [-6], [12], [8], [11], [7], [1], [23], [6], [20], [-3], [18], [6], [-6], [-32], [19], [-20], [27], [-22], [-2], [-20], [1], [4], [-32], [1], [33], [-33], [2], [21], [10], [35], [28], [2], [-20], [-22], [38], [-4], [1], [6], [-4], [-18], [30], [32], [-25], [-34], [-6], [-23], [-24], [17], [20], [-24], [-10], [-36], [-38], [27], [21], [-19], [2], [18], [20], [11], [0], [3], [12], [0], [46], [-10], [-34], [-6], [41], [42], [7], [42], [42], [45], [6], [-24], [-20], [22], [-30], [-19], [50], [-17], [34], [-41], [-16], [-28], [-30], [8], [29], [-3], [-47], [14], [1], [22], [15], [0], [4], [-10], [-8], [24], [-42], [-5], [-47], [39], [50], [24], [2], [53], [-33], [3], [-12], [-7], [-55], [30], [-43], [24], [38], [-2], [-1], [2], [59], [-20], [-25], [40], [22], [-54], [-27], [18], [-54], [-14], [35], [-21], [8], [-36], [-2], [22], [-17], [36], [-26], [-2], [0], [-8], [-52], [47], [-35], [0], [5], [-58], [-15], [-28], [23], [6], [29], [-10], [-5], [36], [-16], [25], [58], [38], [60], [-22], [50], [-3], [-21], [40], [10], [45], [29], [-43], [-14], [36], [70], [-35], [6], [71], [-40], [-4], [-4], [-1], [38], [45], [-74], [6], [-16], [68], [-22], [-26], [-19], [70], [52], [-37], [-17], [-52], [23], [25], [-12], [-43], [10], [-34], [61], [60], [42], [29], [-14], [-77], [50], [30], [72], [14], [-13], [-20], [-42], [13], [-21], [69], [-21], [16], [5], [-13], [46], [13], [-44], [22], [22], [-58], [-20], [-60], [35], [74], [-20], [38], [-69], [25], [-52], [35], [-10], [23], [58], [-41], [-17], [6], [46], [27], [13], [-22], [-30], [-54], [27], [-6], [12], [-65], [-33], [35], [72], [-44], [-44], [3], [31], [20], [58], [3], [-60], [-7], [24], [-44], [-60], [-10], [-5], [-32], [24], [-70], [-40], [18], [25], [-37], [-4], [53], [-68], [-33], [20], [50], [-76], [68], [-44], [24], [80], [31], [20], [17], [2], [-51], [32], [54], [-26], [16], [-6], [72], [-36], [32], [-39], [73], [-26], [7], [38], [62], [30], [-28], [-62], [31], [-51], [34], [-85], [70], [-50], [-54], [-49], [-84], [-16], [71], [45], [-89], [-56], [56], [-86], [-79], [-4], [-96], [-74], [7], [50], [-96], [-44], [29], [45], [-57], [-75], [-72], [-46], [78], [-76], [-18], [-40], [24], [70], [62], [-10], [24], [-21], [52], [-43], [12], [20], [4], [15], [72], [28], [14], [57], [9], [-59], [60], [-83], [-83], [-4], [-28], [14], [10], [-33], [52], [68], [-93], [-30], [39], [28], [0], [71], [94], [50], [-12], [-81], [-28], [95], [40], [31], [8]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_8015_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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