
// Make newform 3549.2.a.g 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_g();".
// 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_g();".
// 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
        poly := [1, -2, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0], [0, 1, 0], [-2, 0, 1]];
    Rf_basisdens := [1, 1, 1];
    Rf_basisnums := ChangeUniverse([[z : z in elt] : elt in Rf_num], Kf);
    Rfbasis := [Rf_basisnums[i]/Rf_basisdens[i] : i in [1..Degree(Kf)]];
    inp_vec := Vector(Rfbasis)*ChangeRing(Transpose(Matrix([[elt : elt in row] : row in input])),Kf);
    return Eltseq(inp_vec);
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 := [[-1, 0, -1], [-1, 0, 0], [0, 0, 0], [1, 0, 0], [0, 0, 1], [0, 0, 0], [2, -2, 2], [0, 4, -2], [-3, 5, -5], [-2, -3, 0], [4, -2, 0], [-3, 3, 1], [-6, 0, 2], [6, -3, 2], [2, -6, 0], [1, -7, 1], [0, -4, 0], [-2, 2, -8], [5, 3, -1], [-3, 1, -1], [0, -8, 6], [0, -2, 7], [0, 2, 4], [4, -6, 6], [0, 6, -2], [-6, 0, 0], [-8, 8, -6], [-16, 0, -3], [8, -8, 7], [-4, 6, 5], [-14, 1, 2], [-14, 4, -6], [-4, -2, -5], [8, 4, 2], [2, 7, -8], [-2, -3, 6], [8, -2, 12], [14, -3, 6], [0, 6, 10], [0, -6, 10], [-8, 4, -7], [2, 8, 8], [4, 5, 2], [8, -9, -4], [-2, 12, -12], [-4, -8, -2], [-3, -9, 3], [-12, 6, -4], [-22, 6, -10], [-22, 2, 0], [14, 1, 8], [4, -1, -6], [-4, 2, -18], [-22, 4, -2], [-4, 4, -8], [-8, 0, 11], [-2, -6, 12], [-2, -10, 8], [12, 8, -3], [10, -12, -4], [-6, -18, 4], [8, -2, 12], [-14, 4, -8], [-20, 12, 0], [-18, 14, -20], [-16, -2, 1], [3, 5, 5], [4, 21, -8], [-31, 9, -13], [-16, 6, -20], [14, -4, 8], [-9, 15, -7], [18, -14, 16], [15, -3, -5], [-14, 5, -10], [-14, 16, -6], [-16, -6, -1], [-22, 0, -10], [-10, 1, -8], [16, -22, 16], [4, -4, 14], [22, -8, 16], [8, -8, -15], [20, -12, 12], [6, -16, -6], [-28, -3, -2], [9, 5, -15], [8, -8, -3], [-30, 6, -8], [8, -6, -11], [14, -18, 20], [-4, -2, -8], [20, 10, 1], [-19, 9, 15], [-2, 11, 2], [-6, -2, 2], [22, -2, -10], [-18, 18, -22], [-10, 14, -26], [-13, 13, 11], [12, -2, -17], [-27, 13, 1], [-22, 10, 0], [-4, 6, -13], [-10, -9, 14], [-8, 14, -16], [28, -18, 26], [-16, 28, -8], [8, -9, -14], [8, -14, -12], [-20, 0, 0], [-12, 13, -24], [26, -28, 32], [2, -4, -16], [4, 10, -19], [-9, 23, -17], [-24, -4, -10], [-28, 28, -34], [10, -15, -12], [-8, 33, -6], [-4, 28, -8], [28, -16, 11], [-6, 16, -4], [8, 16, -4], [4, -24, -2], [3, 3, 27], [-16, 13, 4], [-16, -12, 8], [26, -18, 18], [22, -6, 28], [20, -4, -8], [4, 9, 10], [28, -18, 19], [28, -15, 4], [-16, 0, 16], [6, -10, 18], [12, -22, 24], [-28, 10, -14], [-6, 2, 24], [-11, 5, -11], [8, -4, 4], [37, -19, 25], [-15, 3, 3], [23, -17, -15], [6, -4, 22], [-14, 10, 16], [-22, 22, -24], [0, 6, 0], [2, 0, 10], [16, 4, -9], [0, -16, -13], [18, -28, 36], [-36, 22, -29], [-20, 8, -4], [-3, 35, -9], [5, -11, -17], [-3, 19, -13], [24, 4, -12], [-14, 8, 4], [32, -24, 40], [3, -29, 21], [13, 13, -15], [25, 11, 3], [-4, -18, 6], [-4, 6, 23], [0, 14, -28], [-2, -13, 14], [-16, 18, -10], [16, -17, 0], [-2, -28, 14], [16, 8, 20], [-42, -8, 2], [8, -11, 30], [33, -29, 37], [-26, 10, -12], [16, -28, -4], [-22, 19, -22], [0, 6, 7], [20, 8, -22], [-14, -10, -24], [20, -18, 29], [-14, -10, 8], [24, -23, 24], [-26, -8, -8], [21, 1, 23], [-32, 10, -24], [12, -12, -11], [-36, 4, -4], [8, -24, -17], [30, -2, 8], [-6, -10, 2], [29, -31, 39], [-25, 5, -3], [24, 10, 0], [16, 0, -20], [-2, -16, 6], [-3, -25, 21], [36, -16, 43], [26, -10, 46], [-30, 2, -14], [6, -1, 8], [8, 6, 6], [-38, -10, 10], [32, -2, -12], [-60, 10, -16], [-8, -42, 14], [-6, 42, -14], [-12, -16, 12], [-4, 14, -11], [-22, -16, -8], [21, 7, 21], [-48, 18, -36], [24, -38, 21], [30, 2, -4], [-20, 46, -30], [-6, 32, -8], [4, 16, 12], [18, -46, 22], [24, -31, 10], [30, -19, 52], [-4, 28, -5], [-44, 10, -4], [30, -5, 24], [-34, 9, -2], [0, 32, -12], [-19, 19, -35], [-18, 42, -36], [0, -12, -24], [-12, 8, -20], [-8, -17, -14], [17, -29, 13], [-56, 8, 10], [4, -2, -17], [-10, 7, 4], [6, -2, -16], [32, -32, 14], [22, -2, 6], [28, -34, 1], [20, -16, -11], [-22, 28, 18], [-36, 28, -24], [-52, 30, -36], [-14, 40, -16], [-34, 16, -16], [4, -8, 0], [24, -6, 14], [-2, -8, -34], [20, -2, 26], [-38, 21, -30], [4, -24, 20], [12, 12, -11], [-2, 4, 14], [0, 3, -18], [30, -16, 20], [6, -10, 14], [-21, 39, -31], [1, -13, -19], [-20, 8, -8], [22, -4, 16], [-36, 22, -58], [29, -17, -17], [-52, 16, -4], [14, -12, -16], [-2, 8, 16], [-48, 34, -38], [-26, 4, -32], [-18, -13, -28], [12, -32, 30], [36, 10, 13], [26, -23, 4], [-6, 34, 14], [16, -26, -23], [-12, 12, -6], [20, -16, 12], [14, -34, -4], [12, -26, -6], [-20, 8, -45], [36, 4, -15], [36, 19, 0], [-18, -30, 26], [52, -2, 4], [37, -49, 51], [30, -34, 12], [22, -24, 32], [-24, 34, -40], [12, 24, -13], [36, -20, 57], [32, -22, 5], [-18, 10, 32], [16, 0, -14], [-8, 22, -27], [6, -66, 28], [23, 27, -5], [36, 22, -16], [-13, 5, -21], [-50, 40, -64], [-16, 12, -2], [22, -20, 2], [0, 12, 34], [-2, -20, 20], [-8, 10, -32], [-10, 17, 16], [-50, 15, 10], [41, -19, 3], [-8, 18, 13], [-3, -5, 13], [0, -43, 34], [10, 8, 28], [-16, 5, -6], [36, 8, -3], [62, -16, 14], [-41, 23, -9], [-20, 26, 11], [44, -8, 44], [-24, 36, -21], [26, -6, 22], [22, 26, -16], [-16, 41, 18], [-20, -6, -10], [12, 22, 19], [-2, -26, 40], [29, 3, 5], [22, 0, 8], [29, -41, 35], [-12, 6, 41], [-16, 62, -10], [20, -22, 29], [-18, 12, -14], [12, 28, 16], [-32, 46, -17], [-22, 20, -40], [-17, 59, -17], [22, -54, 20], [6, 20, -14], [-12, 32, 20], [-22, -26, 12], [-27, 11, -19], [58, -8, 4], [14, 38, -24], [-64, 10, -10], [-12, 19, -4], [34, -19, -4], [-24, -36, 20], [-5, -7, -39], [23, -13, 3], [-20, 12, -12], [44, -4, -16], [0, 2, 39], [-40, 22, -48], [22, -6, -16], [24, 20, 8], [36, 0, 33], [1, 41, 13], [26, 44, -26], [-24, -4, 13], [4, -4, 2], [6, -14, 30], [46, 32, -8], [-15, 9, -51], [-43, 9, -21], [18, 4, 0], [28, -6, 54], [28, 33, -18], [-16, 5, 2], [0, -8, 10], [37, -37, -11], [-34, 6, -12], [60, -46, 55], [7, 25, -37], [7, -13, 9], [30, -22, -10], [-3, 57, -1], [-24, 34, -31], [-36, 24, 8], [48, 7, 16], [30, -46, 44], [-16, 28, 7], [-2, 38, 4], [0, -18, -20], [14, 68, -30], [-10, -24, -30], [-18, -13, 20], [5, -53, 11], [-14, -9, -52], [-10, -22, 46], [10, -38, -24], [32, -50, 19], [12, -16, 10], [-43, 15, -33], [2, 72, -32], [36, -44, 11], [-10, 48, 0], [12, -36, -16], [42, -4, -14], [16, 16, 28], [20, -4, -21], [0, -30, 22], [-20, -34, -4], [-51, 27, -41], [-12, 22, 15], [24, -16, 46], [42, -9, 24], [-36, -8, -20], [60, -14, 26], [-56, 18, -58], [-40, 0, 8], [72, -16, 13], [80, -2, 17], [10, -60, 26], [-8, 28, -28], [42, -44, 14], [14, -51, -12], [-5, 35, -39], [-24, 2, -53], [-15, 31, -43], [-34, 54, -46], [-12, -8, -4], [-16, 26, -60], [-4, -8, -18], [2, 34, 16], [-10, -11, -4], [44, -30, -12], [-48, -5, 2], [2, 2, -48], [24, 20, 30], [50, -4, -18], [40, -8, 39], [-82, 32, -36], [-24, -24, 24], [40, -28, 62]];
    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_g();".
// 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_g(:prec:=3)
    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_g();".
// 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_g( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_3549_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<2,R![-1, -1, 2, 1]>,<5,R![0, 1]>],Snew);
    return Vf;
end function;