
// Make newform 3549.2.a.r in Magma, downloaded from the LMFDB on 25 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_r();".
// 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_r();".
// 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, -1, 0], [-1, 0, 0], [0, 0, 0], [-1, 0, 0], [0, 1, 0], [0, 0, 0], [0, 0, -2], [-4, 2, -4], [2, 0, 5], [-5, 3, -3], [-2, -2, 2], [0, 4, -3], [6, 2, 0], [3, 1, -3], [4, -6, 6], [-6, 6, -7], [4, -4, 4], [0, 6, 2], [-8, 2, -3], [2, 0, -1], [8, -2, 8], [-2, -5, -2], [-2, 6, -2], [2, 0, 6], [-6, 4, -6], [-6, 0, 0], [0, -2, 8], [-16, 3, 0], [0, -1, 8], [2, -11, 6], [-13, -3, 1], [-10, 2, 4], [6, -7, 2], [12, -6, 4], [-9, -1, -7], [5, 3, 3], [6, -10, -2], [-11, 3, 3], [-6, 16, -6], [-6, -4, -6], [-4, 3, 4], [10, -16, 8], [9, -7, 5], [1, -13, 9], [-10, 0, -12], [-12, 10, -8], [-12, 6, -9], [6, 2, -6], [16, -4, -6], [20, 2, -2], [15, -9, 1], [-3, -7, 1], [2, -16, -2], [-18, -2, 4], [0, 4, 4], [-8, -11, 0], [-8, -6, -6], [12, -2, 10], [20, -5, 8], [2, -16, 12], [-24, 14, -18], [-6, 10, 2], [10, -4, -4], [-8, -12, 12], [-4, 6, 14], [18, -1, 2], [-8, 10, -5], [25, -13, 21], [-22, 4, 9], [10, -14, -6], [-10, 4, 4], [-6, 8, -15], [4, -2, -14], [12, 8, -3], [9, -5, -5], [-2, 10, -16], [-22, 7, -6], [22, -10, 0], [9, -7, -1], [6, -6, 22], [0, -10, -4], [-14, 8, 8], [0, -23, 8], [8, 0, -12], [-10, 22, -16], [-31, 5, -3], [-14, -10, -5], [0, -11, 8], [24, -2, -6], [-2, -17, 6], [-4, -2, -18], [6, -10, 2], [-30, 11, -10], [-10, -24, 9], [-9, 13, -11], [-8, 0, -2], [-20, -12, 2], [0, 4, 18], [4, 12, 14], [0, 24, -13], [10, 19, -2], [14, 14, -13], [-12, -10, 10], [2, 7, 6], [-19, -5, -9], [-6, -2, -14], [-10, 8, 18], [-12, 20, -28], [-1, 23, -9], [-6, 26, -14], [-20, 0, 0], [-1, -11, -13], [2, 4, 28], [2, -20, 4], [-14, -9, -10], [14, -6, 23], [28, -14, 4], [0, 6, 28], [-5, 27, -15], [25, -27, 33], [-24, 20, -28], [12, 5, -16], [10, -12, 16], [-24, 12, -16], [20, -26, 24], [6, -30, 3], [3, 17, -13], [-28, 4, -12], [8, 0, -18], [-16, 22, 6], [-16, -12, 4], [-13, 19, -9], [10, -1, -18], [13, 11, -15], [16, 16, 0], [4, 8, 10], [10, 2, 22], [18, -4, -10], [-4, -26, 2], [-6, 6, 5], [-4, 0, 4], [-18, 6, 19], [-12, -6, 3], [-6, -32, 17], [2, -18, -4], [4, 26, -10], [0, -2, -22], [6, -6, 6], [2, -10, 0], [-20, -5, -4], [16, -29, 16], [-10, -8, -28], [-14, 7, 22], [-12, -4, 8], [32, -26, 35], [-6, 28, -11], [16, -6, 19], [-28, -8, -4], [-6, -12, 8], [-8, 16, 24], [26, -8, 29], [26, 2, 13], [-36, 14, -11], [-22, 12, -18], [-2, 29, -6], [-14, -14, -14], [-15, -1, -13], [2, -8, 18], [1, -17, 17], [-30, 14, -28], [-24, 28, -8], [50, -6, 8], [-3, -19, -11], [-4, 8, 29], [-16, 2, 10], [-12, 32, -28], [3, -3, -19], [-6, 13, -6], [28, 14, 8], [-24, 34, -10], [-2, 11, 18], [-24, 2, -10], [1, -1, -23], [34, -16, 8], [-22, 24, -1], [-22, 14, 10], [0, 23, -12], [32, 0, -4], [16, -41, 24], [-28, 6, 2], [-16, 8, -10], [2, 8, 31], [-20, -2, 5], [-34, 10, -10], [16, 20, 0], [-18, 10, -16], [28, -4, 25], [20, -27, -16], [-16, 36, 10], [-28, 12, 2], [-5, 7, 1], [14, -12, 6], [48, 0, 10], [30, 14, -2], [50, -6, -10], [-50, 28, -42], [-36, 28, -42], [-28, 4, -16], [-10, 3, -14], [-38, 24, -16], [28, -28, 7], [-30, 18, 18], [-14, 17, -38], [-32, -2, -2], [-26, 16, -46], [-26, 24, -32], [20, -28, 16], [-28, 24, -46], [7, -21, 31], [-11, 33, 19], [24, -23, 28], [34, 6, -10], [-25, 19, 5], [25, 7, -9], [32, -20, 32], [0, 16, 19], [24, -6, 42], [-12, 36, -12], [-4, 12, 8], [25, -31, 17], [-12, 16, -29], [-48, -18, 8], [-2, -19, 2], [-3, -11, 7], [4, 18, -2], [0, -18, 32], [-20, 4, 2], [6, -33, 34], [4, 27, -16], [6, -46, 28], [8, 4, -28], [-22, 6, 30], [26, -24, 40], [18, 0, -16], [4, -8, 8], [18, -8, -6], [10, -42, 8], [-18, 24, 2], [17, -9, -21], [-20, 4, -24], [-24, 1, -12], [-2, 18, -4], [-3, -15, -3], [14, -4, -16], [-4, -4, -10], [-18, 8, -39], [-12, 32, -13], [12, 0, -8], [18, -12, -4], [14, -36, -22], [12, 34, -17], [-36, -12, 16], [-2, -28, 12], [6, -24, 8], [14, -4, -34], [-22, 28, 4], [31, -41, 13], [20, -2, 32], [-46, 23, -10], [3, 19, -23], [28, -48, 34], [10, -49, 26], [0, 6, -12], [4, 4, -16], [-20, 38, -34], [14, -32, 26], [12, -37, -8], [-40, -11, -4], [-55, 19, -19], [-48, 4, -30], [50, -2, -2], [12, 2, 49], [-4, 22, -34], [2, 8, 24], [-10, -6, -34], [36, -11, 24], [16, -37, -20], [-10, -17, 22], [8, 42, -10], [16, 14, 0], [14, 5, 22], [-60, 38, -66], [-50, 22, -27], [-58, 6, -22], [-8, 16, 5], [-10, 24, 40], [-4, -10, 12], [2, 18, -20], [12, -46, 12], [22, 0, 20], [2, 22, 10], [-7, 33, -17], [-35, -25, 15], [22, 16, -19], [10, -31, 18], [8, 8, 5], [-43, 9, -43], [18, -36, 8], [11, -1, -5], [44, -5, 8], [46, 2, -16], [18, 14, -23], [6, -37, 26], [36, -36, -8], [-12, 15, -36], [20, -16, -6], [-48, 10, -26], [-25, 59, -41], [26, -16, 6], [34, -41, 22], [-28, -14, -26], [-32, 8, -3], [22, -8, 0], [12, -6, 41], [6, 47, -6], [46, -52, 62], [2, 7, 22], [6, -2, -12], [40, -44, 28], [14, -29, 46], [2, -20, -20], [42, -42, 59], [-32, 34, -54], [-26, 6, -20], [-20, 52, -32], [48, -14, 26], [16, -8, -11], [-50, -4, 8], [-52, 14, -38], [-54, 0, 10], [-7, 15, -19], [15, 23, -19], [60, -16, 36], [-12, 46, -7], [-10, -10, 13], [-8, 0, 12], [40, 20, -4], [-2, 41, -2], [18, -26, -22], [16, 22, -6], [-44, 28, -20], [-36, 33, 0], [-42, 54, -41], [70, -18, 44], [-28, -9, -4], [0, 2, -4], [8, 16, 14], [-78, 24, -32], [-6, 42, 9], [34, -12, -9], [-22, 4, -4], [22, -48, -6], [61, -15, 33], [11, 7, -5], [-8, -2, -8], [0, 48, -37], [28, -6, -6], [-14, 9, 46], [32, 12, 25], [-6, 4, -13], [8, 32, -22], [-54, 56, -57], [10, -3, 34], [-12, -32, 24], [55, -23, 7], [16, -2, 46], [12, -35, 28], [-36, 42, -38], [-18, 38, -18], [82, -38, 68], [34, -54, 24], [31, 7, 13], [48, -42, 53], [23, -61, 9], [32, 24, 22], [28, -62, 38], [18, -31, 50], [-4, 6, -16], [28, -18, -15], [74, -40, 72], [8, -33, 44], [-38, 48, -48], [24, -52, 36], [38, 18, -4], [-32, 44, -16], [16, 25, -4], [30, -8, 30], [-54, 38, -34], [-24, 14, 27], [-10, 37, -22], [-8, 30, 16], [33, -15, -9], [44, -28, 8], [-46, 12, 14], [38, -40, -18], [-40, -8, 0], [-56, -3, 16], [-78, 15, 2], [50, -34, 60], [-20, 0, -28], [-2, 30, -44], [-37, 63, -51], [30, 4, 35], [-22, 51, 2], [16, 12, 31], [20, -8, 54], [20, -12, 8], [10, 34, 26], [12, -26, 8], [36, -50, 34], [-21, 15, -11], [-14, -42, 30], [53, -3, 5], [4, 46, 2], [-44, 50, -20], [-46, -22, 4], [32, -31, -8], [50, -4, -32], [48, 0, 24], [12, -34, -28]];
    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_r();".
// 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_r(: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_r();".
// 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_r( : 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;