
// Make newform 3536.2.a.e in Magma, downloaded from the LMFDB on 05 June 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_3536_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_3536_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_3536_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_3536_a();"
function MakeCharacter_3536_a()
    N := 3536;
    order := 1;
    char_gens := [1327, 885, 3265, 1873];
    v := [1, 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_3536_a_Hecke(Kf)
    return MakeCharacter_3536_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]];
    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], [0], [-4], [2], [2], [-1], [1], [0], [4], [2], [2], [0], [0], [-4], [8], [-6], [-8], [-2], [-16], [14], [-16], [-8], [12], [-18], [-4], [-2], [-16], [4], [4], [14], [8], [-8], [6], [-4], [6], [0], [2], [-2], [6], [-22], [12], [-18], [-16], [-16], [-12], [-8], [-20], [24], [-30], [-18], [-26], [-12], [12], [12], [-18], [24], [18], [20], [-6], [22], [8], [-26], [-20], [0], [14], [12], [4], [2], [-36], [10], [-14], [-12], [4], [-34], [-22], [36], [18], [32], [12], [10], [-12], [34], [-18], [34], [-20], [-12], [-20], [10], [18], [-40], [4], [30], [-2], [20], [14], [20], [22], [-18], [-12], [-28], [32], [-30], [-44], [22], [12], [6], [16], [-34], [-16], [-26], [24], [-34], [16], [-46], [44], [10], [34], [-16], [-6], [-12], [2], [-14], [-18], [26], [2], [30], [8], [-44], [8], [2], [36], [26], [-24], [-42], [-42], [-42], [38], [22], [-18], [6], [-26], [8], [48], [42], [-14], [-50], [-8], [-2], [-20], [20], [48], [-18], [44], [-28], [24], [36], [0], [12], [-6], [28], [-18], [2], [4], [-36], [30], [-38], [-24], [-34], [20], [-46], [24], [42], [8], [-2], [0], [-14], [34], [52], [-16], [-34], [12], [20], [-26], [38], [44], [-38], [-50], [24], [32], [-54], [22], [-66], [-4], [-34], [-28], [6], [-28], [22], [-52], [56], [24], [-20], [-10], [46], [56], [30], [32], [36], [-12], [60], [-10], [18], [28], [-28], [42], [-8], [56], [38], [2], [18], [34], [-2], [-42], [-46], [-12], [-22], [-70], [-24], [56], [-30], [-50], [8], [-24], [10], [-20], [28], [-32], [-20], [-60], [-56], [-30], [-52], [0], [38], [8], [-48], [-74], [38], [-56], [24], [38], [-16], [32], [-70], [34], [68], [-54], [-62], [78], [-38], [-64], [-36], [-52], [-26], [-12], [20], [-14], [-22], [30], [-6], [-38], [20], [6], [40], [14], [-36], [4], [42], [-2], [-36], [-56], [18], [40], [-22], [42], [-16], [-70], [68], [-32], [14], [-18], [52], [18], [-50], [34], [-42], [48], [-22], [8], [16], [86], [38], [36], [76], [20], [-52], [44], [54], [-88], [6], [-72], [-16], [6], [-4], [16], [50], [12], [-62], [68], [-10], [-20], [88], [30], [-56], [56], [-50], [62], [54], [-28], [-22], [-44], [-22], [-56], [60], [10], [58], [50], [-52], [-36], [-8], [42], [18], [64], [-16], [50], [20], [58], [-44], [-26], [6], [32], [16], [6], [-8], [-6], [66], [-48], [90], [-42], [-48], [-8], [90], [16], [70], [4], [-62], [24], [-64], [90], [-60], [8], [10], [54], [0], [-42], [-30], [0], [26], [-82], [-50], [-72], [86], [54], [88], [-46], [-66], [-12], [26], [-74], [-8], [12], [28], [72], [100], [84], [58], [-48], [-58], [0], [-6], [-56], [-34], [-52], [64], [-18], [-92], [22], [102], [-28], [18], [46], [54], [-20], [44], [38], [6], [90], [56], [48], [-48], [-58], [40], [-30], [-36], [-28], [-2], [64], [36], [-28], [20]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_3536_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_3536_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_3536_2_a_e(:prec:=1)
    chi := MakeCharacter_3536_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_3536_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_3536_2_a_e( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_3536_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![0, 1]>,<5,R![4, 1]>,<7,R![-2, 1]>,<11,R![-2, 1]>],Snew);
    return Vf;
end function;