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