
// Make newform 3549.2.a.d in Magma, downloaded from the LMFDB on 23 May 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_3549_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_3549_2_a_d();".
// 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_3549_2_a_d();".
// 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_3549_a();"
function MakeCharacter_3549_a()
    N := 3549;
    order := 1;
    char_gens := [1184, 1522, 3382];
    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_3549_a_Hecke(Kf)
    return MakeCharacter_3549_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 := [[2], [-1], [1], [-1], [2], [0], [-4], [-3], [-9], [-1], [5], [8], [-6], [-9], [3], [3], [0], [10], [2], [-12], [-5], [-13], [11], [-1], [-1], [-6], [-12], [12], [2], [-15], [-4], [4], [0], [-18], [-18], [-4], [24], [-8], [-7], [12], [-5], [14], [-16], [6], [6], [-4], [19], [23], [-4], [-26], [19], [24], [-25], [30], [14], [-27], [-20], [0], [-7], [22], [24], [33], [7], [-18], [30], [-20], [10], [9], [24], [35], [14], [-12], [10], [-2], [18], [4], [-26], [19], [20], [11], [-2], [-20], [-30], [-8], [-34], [23], [-16], [-8], [2], [-34], [-18], [-39], [18], [-36], [-24], [-26], [17], [0], [-34], [-12], [25], [12], [0], [-29], [-1], [34], [-13], [17], [-25], [22], [-46], [-28], [-10], [-36], [34], [-19], [-8], [22], [-6], [37], [-9], [-23], [-14], [12], [-45], [-7], [38], [6], [14], [27], [22], [24], [-25], [1], [-35], [45], [-10], [5], [-38], [-27], [28], [34], [0], [8], [2], [0], [45], [38], [-26], [-40], [40], [10], [-20], [-48], [39], [5], [48], [9], [44], [35], [-18], [-3], [46], [-30], [-54], [-43], [4], [52], [-18], [12], [-30], [14], [5], [0], [60], [36], [-48], [30], [-14], [6], [16], [-30], [14], [-51], [-50], [-54], [7], [-11], [-26], [21], [-8], [-34], [-65], [25], [-15], [-48], [2], [13], [7], [-54], [60], [32], [-49], [-40], [-69], [-52], [-31], [19], [-44], [-2], [-13], [-12], [-64], [-28], [11], [-71], [-13], [-64], [-12], [24], [-19], [-16], [-28], [38], [-18], [55], [-12], [-55], [22], [46], [66], [-40], [16], [-18], [22], [3], [70], [32], [-19], [-2], [32], [46], [-16], [66], [-21], [-72], [47], [-3], [28], [33], [-8], [-50], [-42], [10], [6], [68], [-73], [4], [12], [-13], [3], [49], [19], [-2], [-18], [-38], [34], [-45], [-14], [-38], [26], [24], [26], [53], [0], [8], [-28], [-16], [8], [0], [-66], [-74], [-20], [-53], [-44], [-5], [-53], [-48], [-32], [20], [15], [0], [-66], [33], [-11], [-8], [42], [16], [20], [1], [-22], [32], [55], [-61], [-7], [42], [20], [76], [-30], [39], [-28], [54], [-50], [-71], [-32], [56], [69], [50], [-55], [33], [-64], [68], [-18], [-12], [0], [36], [-56], [-37], [-20], [6], [-50], [-69], [-38], [-77], [74], [5], [-40], [-86], [-14], [-62], [-24], [70], [34], [-64], [40], [48], [-14], [56], [85], [46], [-63], [-18], [-60], [50], [-10], [62], [-29], [-21], [66], [14], [-66], [78], [27], [72], [-38], [-17], [-75], [-58], [-57], [60], [-18], [32], [-33], [-60], [27], [36], [96], [38], [4], [6], [21], [-6], [64], [12], [-25], [59], [60], [-96], [4], [68], [70], [-65], [40], [-28], [100], [-23], [40], [78], [37], [-72], [-83], [-33], [-10], [63], [-35], [8], [68], [64], [-33], [-9], [-40], [53], [-51], [52], [-33], [-60], [4], [56], [42], [-28], [-5], [102], [-68], [-64], [-86], [-67], [15], [48], [-65], [42]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_3549_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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