
// Make newform 5776.2.a.br in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_5776_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_5776_2_a_br();".
// 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_5776_2_a_br();".
// 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, -3, 0, 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_5776_a();"
function MakeCharacter_5776_a()
    N := 5776;
    order := 1;
    char_gens := [5055, 1445, 2529];
    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_5776_a_Hecke(Kf)
    return MakeCharacter_5776_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 := [[0, 0, 0], [1, 1, 0], [-1, 1, 0], [0, -1, 1], [0, 2, -1], [0, 3, -2], [2, 0, -1], [0, 0, 0], [2, 2, -2], [-5, -1, 0], [3, 3, -1], [0, 1, 2], [-4, 1, 3], [0, 2, -5], [2, 2, 1], [-2, 2, -3], [7, -2, 2], [3, 0, 4], [-6, 4, -6], [10, -2, 2], [0, 4, -4], [3, -1, 7], [0, 3, 6], [-5, -2, -1], [5, 4, -2], [-5, 1, -4], [6, 3, -4], [0, -3, -3], [5, -2, -2], [-3, -7, -1], [1, -1, -7], [7, 1, -7], [10, -6, 1], [-6, 5, -5], [8, 7, -3], [10, -3, 0], [-3, 3, -8], [0, -2, -3], [11, -8, 0], [2, -2, -12], [9, -6, 9], [2, 10, -8], [-9, 4, -11], [9, -3, -2], [-6, 11, -1], [-12, 0, 8], [-3, 6, -7], [9, 3, -4], [6, -3, 9], [3, -6, -8], [-1, 14, -5], [3, -11, 10], [9, 5, -3], [2, -7, 10], [7, -5, 2], [1, 4, -13], [-4, 10, 3], [7, 14, -13], [5, -3, 12], [4, -3, 13], [7, -7, -1], [3, -8, 1], [-11, 8, 5], [-9, 2, -7], [-10, -17, 10], [28, 0, 1], [-15, -10, 4], [15, -5, 8], [1, 5, -3], [0, -11, 17], [-15, -3, -3], [-14, -1, 6], [9, -14, 6], [14, -6, 21], [-15, 3, 5], [16, -6, -5], [-8, -8, -1], [-12, -13, 3], [5, 3, -7], [-7, 16, -2], [-3, -16, 5], [2, -2, 4], [17, 4, 9], [-9, 9, -17], [18, 1, -9], [-7, 18, 2], [15, -5, -11], [0, -10, -3], [-25, -7, 1], [15, 10, 6], [-21, 0, -3], [-4, -19, 1], [1, -12, 9], [7, 15, 1], [3, -14, 12], [-1, -17, 0], [1, -21, 1], [-18, 8, 8], [0, -2, 21], [9, 4, -1], [-6, 2, 4], [13, 5, -6], [9, 5, -19], [-12, 9, -12], [0, -10, 4], [-7, 3, -6], [28, 1, -7], [-8, -2, 2], [17, -9, -13], [-27, 7, -13], [6, -13, -11], [14, 4, -2], [-23, 12, -23], [-18, 6, 2], [-18, 10, 0], [14, 9, -1], [-33, 9, -4], [3, -19, 14], [27, 0, 0], [-1, 6, -25], [-10, 13, -2], [0, -8, -1], [0, -4, -1], [-12, 5, -4], [-8, -24, 12], [11, -2, 21], [-21, -5, -7], [13, -8, 29], [-27, 4, -21], [-25, 6, -6], [-3, -15, 2], [-17, -4, 12], [3, 6, -13], [18, 11, -15], [-3, 5, -1], [21, 0, 1], [-2, -13, 0], [-12, 22, -9], [9, 22, -11], [27, 23, -19], [-2, -7, -1], [-8, -10, -3], [-26, -10, -1], [-37, -2, 0], [-21, -2, -7], [19, 20, -12], [-15, 18, -29], [-16, 3, -7], [3, 8, 1], [6, 17, -28], [-15, 7, 2], [33, 8, -1], [15, -1, -17], [23, -17, 21], [1, 2, 23], [36, -1, -10], [-3, -9, -13], [11, -7, 19], [-9, 7, -1], [-23, -14, 20], [4, 4, -13], [14, 17, -8], [-24, 0, 8], [22, -12, -8], [-3, 24, -18], [7, 38, -27], [0, 21, -28], [12, -11, 14], [-10, -20, 22], [-8, -12, 28], [0, -18, -3], [18, 10, 14], [7, 3, 19], [9, 17, -21], [12, -14, 30], [-23, -5, 17], [30, -3, 8], [-13, -15, 29], [24, -16, 28], [-3, -30, 7], [7, 14, -7], [21, -7, 32], [6, 9, -26], [-31, 11, 4], [30, 13, -14], [-9, 16, -8], [-28, -17, 13], [-27, -25, 19], [8, 0, -21], [27, -1, -13], [18, -14, -10], [-13, 9, -19], [-6, 4, -21], [-13, -1, -11], [-19, 9, -31], [-8, 5, 18], [26, 40, -26], [3, -9, 31], [12, -13, 5], [12, 28, -32], [-5, -16, 9], [-5, 14, -1], [3, -14, 35], [-15, -7, -15], [-23, -13, 0], [-11, 15, -41], [25, -7, -4], [-16, -21, 2], [5, 8, 4], [-24, 12, -4], [-6, -10, -12], [16, -24, 4], [-15, -36, 14], [-1, 9, -10], [15, 18, -6], [21, -19, -3], [28, -7, 17], [-33, -7, -10], [-3, 34, -26], [5, 14, 19], [-15, -26, 11], [-27, -8, 12], [35, 25, -12], [-27, 17, -2], [1, -26, 14], [-9, -25, 6], [24, -22, 26], [13, 32, -3], [10, -13, -13], [36, -11, 7], [-21, 19, -37], [0, 8, -29], [-29, 12, -27], [-27, 17, -13], [-2, -27, 3], [-41, -6, 16], [3, -32, 23], [-27, 4, -23], [46, 1, -10], [26, 3, 20], [-50, 3, -14], [-2, 3, 12], [-6, -15, 2], [-3, -17, 35], [14, -7, 7], [-6, 7, -2], [16, 5, 2], [11, 15, -22], [-3, 36, 5], [-4, -9, 20], [-12, -20, 29], [8, -14, 42], [9, 7, -8], [17, -29, 40], [-13, -22, 10], [-31, -6, 23], [-3, 23, 0], [-9, 18, 2], [-22, 21, -46], [-3, 4, 8], [6, 15, -1], [-5, -29, 41], [-27, -28, 21], [21, -10, 6], [-2, -30, 43], [-9, -26, 0], [-9, 37, -26], [21, 10, -14], [0, 1, -6], [-17, -43, 30], [48, 20, -12], [-2, 24, 15], [32, -2, -17], [1, 24, 9], [3, 44, -12], [0, 11, 13], [21, -6, 3], [27, -28, 15], [-42, -6, -11], [8, -4, 10], [9, 28, -8], [-5, 0, 27], [32, -11, 9], [-27, -7, 21], [31, 8, -10], [17, 4, -21], [33, -14, 44], [-5, 25, -4], [-30, 0, 2], [-3, -20, 22], [21, -11, -5], [-15, 20, 17], [25, 17, 6], [6, -13, 11], [-6, -23, 26], [-42, 12, 15], [22, -10, -1], [-25, -2, -6], [22, 53, -33], [12, -44, 15], [-16, 22, -26], [-7, 25, -3], [-33, 25, -12], [-18, 16, 28], [-27, 9, -4], [-15, -33, 37], [-4, 38, -20], [33, 13, -7], [11, -12, -28], [-3, 10, -7], [15, 32, -7], [2, 6, -40], [-8, -24, 31], [-3, 33, -28], [4, -24, -20], [-19, -39, 21], [-7, -37, 37], [19, -14, 2], [30, 1, 29], [-27, 12, -6], [15, -42, 35], [6, -11, 29], [25, 23, -30], [18, -5, -18], [-8, 42, 10], [-6, -27, 28], [-36, -34, 28], [-9, -13, 4], [31, -11, 23], [1, 7, 17], [-30, -16, -9], [35, 5, -5], [7, -16, 14], [51, 2, -4], [-17, -7, 17], [17, 19, 15], [-15, -16, -15], [33, -29, -8], [18, 23, -6], [12, -13, -8], [-4, -23, 31], [38, 24, -4], [19, -41, 8], [7, -39, 30], [43, -25, 6], [19, 25, -10], [14, -11, -5], [16, 5, -28], [31, -23, -1], [15, -19, 5], [39, 10, -24], [-21, -5, 8], [-26, -17, 20], [34, 21, 9], [17, -14, -35], [12, -23, 34], [4, 34, -7], [4, 7, 20], [1, 15, -38], [-10, -21, -13], [-28, 43, 1], [-11, 3, 16], [-34, -32, 12], [24, -33, 21], [-2, -4, -28], [-13, -5, -17], [27, 30, 0], [-15, -42, 8], [-33, 14, 18], [29, -31, 31], [57, 17, -17], [19, -9, 22], [35, 2, 16], [-24, -22, -2], [-24, -43, 33], [1, -5, -34], [39, 13, 22], [-34, 28, -23], [49, -18, 7], [3, 17, -42], [69, 1, -8], [6, -16, -37], [-26, -43, 23], [11, 19, -42], [51, -19, 28], [-32, 35, 21], [-24, -5, 21], [17, -14, 13], [67, -4, 23], [-18, -36, 33], [27, 11, 0], [41, 0, 20], [3, 41, -55], [-21, 37, 15], [62, 22, -12], [-19, 25, -29], [15, 24, 11], [42, -23, -17], [27, 27, -37], [14, -26, -15], [36, -24, -2], [47, -24, 41], [16, 3, 0], [29, -9, 2], [-8, -5, 50], [54, 48, -34], [27, 1, -7], [9, 32, -19], [40, -4, -19], [0, 9, -15], [69, 16, -19], [-26, 24, 22], [9, -35, 25], [3, 15, 32], [-64, -9, 18], [0, 16, 19], [-25, -51, 32], [7, 24, -24], [-35, 12, 7], [56, 32, -8], [17, 5, -8], [-42, 28, -49], [51, -21, 48], [2, 10, -24], [15, -12, 25], [-12, -26, 22], [-15, -24, 24], [-4, 7, -9], [18, 14, -23], [-32, -20, 2]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_5776_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_5776_2_a_br();".
// 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_5776_2_a_br(:prec:=3)
    chi := MakeCharacter_5776_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_5776_2_a_br();".
// 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_5776_2_a_br( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_5776_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![1, 0, -3, 1]>,<5,R![-3, 0, 3, 1]>,<7,R![-1, -3, 0, 1]>,<11,R![9, -9, 0, 1]>,<13,R![37, -21, 0, 1]>],Snew);
    return Vf;
end function;