
// Make newform 3549.2.a.b in Magma, downloaded from the LMFDB on 24 April 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_b();".
// 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_b();".
// 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], [0], [-1], [3], [-5], [-9], [0], [-2], [-1], [-3], [-9], [0], [-2], [-10], [12], [-15], [11], [-3], [17], [-3], [14], [-4], [4], [-18], [-11], [12], [-8], [0], [-6], [6], [-20], [4], [20], [-25], [-4], [-1], [-14], [16], [10], [-10], [0], [-5], [29], [-12], [-14], [-9], [12], [13], [-14], [2], [-23], [-24], [8], [-23], [10], [-8], [31], [5], [6], [-22], [24], [-14], [1], [32], [-31], [-22], [16], [18], [38], [18], [-36], [-18], [-15], [0], [-23], [-30], [-8], [-30], [-20], [-6], [27], [12], [-28], [-2], [-22], [6], [-17], [2], [-28], [-28], [-6], [-41], [24], [-14], [4], [1], [44], [4], [-41], [-25], [14], [-19], [-33], [27], [30], [38], [-16], [-18], [-4], [34], [-15], [16], [38], [18], [-39], [5], [49], [42], [12], [25], [21], [30], [30], [38], [-15], [6], [12], [-41], [41], [-21], [7], [34], [47], [-38], [-15], [4], [46], [24], [12], [26], [32], [-17], [-38], [-58], [-48], [-4], [42], [44], [20], [-25], [9], [-40], [-41], [36], [13], [58], [41], [-34], [-42], [-30], [-45], [20], [-20], [14], [-28], [6], [-6], [9], [-8], [-16], [-16], [8], [26], [-54], [46], [-20], [6], [-34], [19], [-30], [14], [7], [23], [50], [11], [64], [18], [-1], [15], [37], [24], [22], [-51], [9], [42], [-56], [32], [-27], [4], [-11], [-4], [51], [39], [0], [22], [-45], [-36], [16], [-52], [37], [-13], [35], [64], [44], [-36], [29], [-16], [28], [-38], [-46], [-1], [-8], [5], [38], [-6], [-14], [4], [28], [46], [-54], [21], [-70], [52], [-15], [-46], [60], [30], [12], [-58], [-3], [-20], [29], [-53], [-52], [-51], [28], [18], [-34], [-2], [10], [-32], [-49], [-60], [-48], [73], [11], [29], [25], [2], [-14], [-10], [18], [29], [-22], [10], [22], [40], [-10], [5], [52], [64], [-40], [12], [20], [-4], [-58], [-18], [-4], [41], [-20], [63], [67], [0], [-56], [60], [67], [-56], [42], [-33], [37], [-80], [-50], [-16], [-40], [-77], [-66], [0], [-41], [-65], [33], [-18], [36], [-60], [-46], [79], [-36], [22], [6], [81], [28], [-40], [3], [54], [-13], [21], [-64], [-4], [30], [4], [-24], [52], [20], [25], [-76], [6], [-10], [-33], [-58], [-27], [38], [-61], [4], [-18], [-2], [-62], [32], [-10], [78], [40], [76], [-56], [-38], [-28], [3], [38], [-85], [18], [-76], [66], [-2], [62], [-3], [45], [2], [66], [42], [10], [63], [40], [2], [-17], [91], [14], [-33], [-60], [-18], [-36], [27], [-88], [43], [-52], [64], [-38], [-84], [42], [-53], [102], [68], [28], [-27], [13], [-20], [-56], [-80], [48], [98], [33], [12], [44], [4], [-23], [-40], [30], [-99], [48], [7], [83], [-70], [-15], [-45], [28], [96], [-32], [-83], [-15], [8], [-31], [41], [28], [-33], [0], [60], [-16], [78], [36], [15], [26], [16], [20], [-26], [-93], [-29], [-108], [69], [54]];
    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_b();".
// 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_b(: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_b();".
// 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_b( : 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;