
// Make newform 3542.2.a.o in Magma, downloaded from the LMFDB on 10 June 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_3542_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_3542_2_a_o();".
// 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_3542_2_a_o();".
// 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_3542_a();"
function MakeCharacter_3542_a()
    N := 3542;
    order := 1;
    char_gens := [507, 1289, 925];
    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_3542_a_Hecke(Kf)
    return MakeCharacter_3542_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 := [[1], [-1], [1], [1], [1], [-1], [-2], [-5], [1], [-10], [-8], [-2], [7], [-6], [3], [-6], [0], [12], [13], [-8], [-1], [0], [-11], [-15], [-7], [2], [4], [-12], [-5], [4], [8], [-8], [-12], [20], [5], [-23], [18], [-16], [18], [9], [0], [7], [7], [4], [8], [-10], [-3], [-11], [3], [-5], [24], [-15], [-18], [-18], [8], [-26], [0], [12], [-32], [17], [9], [-26], [-2], [7], [29], [23], [-8], [3], [3], [10], [4], [-30], [-2], [29], [-25], [14], [20], [-32], [22], [-5], [-30], [-18], [-18], [29], [20], [4], [15], [-17], [2], [-26], [28], [-5], [-12], [7], [30], [-1], [20], [42], [24], [22], [28], [3], [24], [45], [-28], [18], [-12], [9], [0], [2], [18], [-21], [18], [20], [-8], [-18], [4], [-37], [29], [10], [2], [-26], [28], [4], [-13], [2], [-40], [0], [8], [-46], [-20], [-6], [32], [-22], [27], [0], [-26], [13], [33], [20], [-8], [2], [-56], [8], [40], [-20], [39], [13], [-20], [-36], [-22], [42], [-16], [-12], [13], [-23], [20], [50], [-52], [42], [-42], [-41], [8], [-8], [-42], [-26], [42], [-27], [-10], [-46], [-10], [52], [42], [-26], [-55], [-50], [-48], [17], [19], [20], [-62], [-33], [-31], [-42], [9], [-5], [8], [-46], [40], [2], [-66], [-1], [-3], [-28], [-2], [-11], [-43], [44], [8], [-66], [35], [-18], [38], [-30], [-20], [-22], [40], [69], [-50], [-53], [-12], [-48], [-11], [28], [40], [-3], [-47], [27], [-32], [9], [32], [-35], [-65], [-16], [28], [-10], [19], [40], [8], [17], [-31], [-20], [22], [-68], [54], [63], [-25], [39], [20], [-3], [29], [22], [-41], [25], [-66], [-50], [-32], [72], [-20], [64], [58], [2], [-52], [-65], [9], [60], [-28], [-32], [-32], [13], [54], [78], [-25], [4], [48], [-20], [-40], [47], [-46], [-36], [52], [-17], [-41], [0], [-42], [-16], [-2], [-20], [-13], [27], [4], [72], [-7], [22], [-2], [-28], [64], [-2], [5], [-35], [72], [28], [54], [17], [24], [70], [32], [-26], [0], [8], [-76], [78], [10], [-6], [12], [78], [-62], [30], [-15], [74], [14], [60], [37], [-6], [-52], [80], [-65], [-23], [-41], [0], [7], [-12], [17], [-16], [-51], [-38], [45], [-41], [-72], [-21], [-28], [3], [0], [-66], [-68], [-37], [70], [-36], [-58], [-47], [-86], [-72], [-45], [-48], [-16], [-25], [-88], [-62], [-33], [3], [-88], [-47], [22], [-61], [-10], [-11], [-60], [-43], [38], [-96], [-7], [42], [-12], [80], [-92], [-26], [18], [-36], [37], [-78], [75], [44], [-30], [7], [-42], [-60], [62], [-36], [40], [-2], [-43], [-6], [53], [48], [60], [-56], [82], [68], [84], [18], [0], [-46], [-10], [-52], [37], [14], [-25], [-45], [-3], [52], [-50], [-81], [-72], [-42], [-25], [-38], [68], [-58], [4], [105], [29], [-42], [-36], [-48], [63], [-48], [40], [-67], [-37], [99], [60], [-22], [48], [0], [-46], [-12], [84], [60], [87], [-30]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_3542_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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