
// Make newform 1476.2.a.g in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1476_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_1476_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_1476_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 := [9, -6, -10, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rf_num := [[1, 0, 0, 0], [0, 1, 0, 0], [-3, -7, 0, 1], [-12, 7, 3, -1]];
    Rf_basisdens := [1, 1, 3, 3];
    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_1476_a();"
function MakeCharacter_1476_a()
    N := 1476;
    order := 1;
    char_gens := [739, 821, 1441];
    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_1476_a_Hecke(Kf)
    return MakeCharacter_1476_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, 0], [0, 0, 0, 0], [-2, 0, 1, -1], [-1, 1, 1, -1], [-1, -1, 1, 1], [0, 2, 0, 0], [0, 0, 0, -2], [2, -1, -1, 0], [2, 0, 2, 0], [2, 2, -2, 0], [0, 0, -2, 2], [4, 0, -1, -1], [1, 0, 0, 0], [0, 2, 0, -2], [2, 3, -1, 0], [4, -2, 2, 2], [-2, 0, -2, 0], [6, 2, 0, 0], [7, -1, 1, 1], [4, -1, -3, 4], [4, 0, -1, 3], [-6, -1, 3, 0], [2, 4, 0, -2], [0, 4, -2, 0], [2, 0, 0, -4], [2, -2, 0, -2], [-6, -2, 2, 2], [2, 0, -4, -2], [2, -2, 4, -2], [0, 0, 3, -1], [0, 4, 2, -2], [-4, 2, -2, 0], [4, -4, 0, 2], [0, 0, -2, -2], [2, 2, -2, 0], [-8, -3, -1, 0], [-2, 2, 4, -2], [6, 4, -4, 2], [-11, 1, -1, -1], [6, -4, -4, 4], [5, -5, 3, 1], [4, -6, 2, 2], [-9, -1, 5, 1], [2, 0, -6, 2], [6, 6, 0, -4], [5, -3, -9, 3], [7, 1, -3, 7], [-4, -8, 4, 4], [0, -5, 1, -4], [14, 2, -4, 2], [20, 0, -4, 6], [-18, 1, 1, -4], [6, 2, 0, 0], [-4, -2, -6, 4], [-8, 0, -2, 0], [-8, 1, -1, -4], [6, 6, 0, 0], [-14, 0, 2, -4], [-2, 4, -3, 3], [14, -4, -8, 4], [0, 0, -2, -2], [14, -6, 2, 4], [-10, 2, 2, 2], [-9, -3, -3, -1], [-2, 8, -2, -6], [4, -6, 4, 8], [1, 3, -1, 1], [14, 4, -11, 7], [-18, 5, 5, 0], [-2, 0, -1, 5], [-10, -4, 1, -1], [16, 4, -4, 0], [-16, -2, 4, 2], [-2, -8, 0, 4], [2, -4, 2, -4], [11, -3, -7, 7], [-14, 4, -4, 0], [-8, -6, 8, -4], [-6, -4, -1, -3], [4, 0, 5, -3], [-4, -2, 0, -2], [0, -14, 2, 2], [-2, 2, 0, -4], [-6, 0, 4, -8], [11, 7, -11, 5], [10, -10, 6, 2], [2, -2, -12, 4], [12, -4, -12, 2], [8, 0, 5, 1], [-4, 1, 7, 0], [10, -4, 6, 4], [17, -1, -5, -3], [-8, -6, 2, -4], [-14, -12, -2, 8], [16, -1, 1, 0], [-2, 5, -3, -4], [-4, 6, -4, 4], [6, 0, 6, 2], [12, -4, -12, 8], [22, 0, -1, 5], [6, -1, -9, 4], [28, 2, -6, 2], [-17, 7, -7, -7], [-6, 12, -9, -3], [-11, 1, 3, -9], [-20, -4, 4, -2], [3, 5, 5, -9], [-12, -4, 14, -8], [0, 0, 6, 6], [0, 8, -12, -6], [-2, 6, -4, 0], [18, -8, -1, 1], [18, 0, -12, 4], [12, -8, 2, 2], [6, 8, 0, 6], [-6, 0, 6, -6], [9, -5, -1, 1], [-16, 2, -2, 4], [14, -14, 0, -2], [14, 7, -9, 4], [0, 12, -11, 1], [16, -4, -2, 4], [4, -4, 3, 11], [2, 1, 9, -4], [10, 11, -5, -8], [4, 16, -7, -7], [30, -6, -8, 2], [2, -1, -5, 8], [10, -9, -1, 12], [6, 2, 0, 8], [6, 8, 0, 6], [2, -10, -2, -2], [-1, 11, -7, -7], [-2, -18, 8, -2], [4, -8, -1, -1], [14, 16, 4, -12], [-8, 2, 6, 2], [28, -2, -6, 0], [-18, 6, 12, -4], [8, 8, -8, -2], [-4, -6, 6, -4], [-12, 8, -7, 1], [18, -9, 7, 4], [-17, -3, 1, -1], [22, 0, -13, 1], [-1, 1, -3, -5], [14, -4, 8, 8], [-2, -4, 9, -1], [10, -2, -6, 6], [28, 6, 4, -6], [-10, 2, -4, 8], [-22, -8, 12, -8], [4, 13, -9, -8], [19, -9, 13, 5], [-18, 2, 6, -14], [28, 10, -4, -2], [9, -1, 3, -3], [-20, -16, 6, 4], [-2, -12, 2, 14], [-2, -4, 0, 4], [-6, -12, 6, 0], [16, 0, 7, 3], [-27, -3, 7, -5], [32, 1, 3, 4], [0, 20, -10, 4], [-22, -16, -2, 12], [-28, -3, 3, 0], [-24, 2, -6, 6], [14, -10, -4, -4], [-10, -2, -6, 8], [12, 7, 1, -12], [20, -8, -5, 3], [27, 1, 1, 3], [-6, 2, 0, 4], [-3, 3, -17, 1], [16, 4, 8, -6], [0, -3, 19, -8], [-14, -4, 0, 8], [16, -3, 11, -4], [-26, -10, 10, 0], [12, 6, -14, -4], [-6, -8, 10, 8], [10, -2, 6, 4], [2, 0, -16, 4], [-12, 4, 16, -12], [18, -18, 0, 0], [2, 2, -16, 0], [-20, 0, 2, -14], [22, -12, 2, 6], [22, 13, -7, 0], [-18, -8, 8, -8], [17, -7, -5, -5], [18, -4, 12, 6], [-6, 8, -7, -5], [-34, -2, 12, -20], [-10, -4, 1, 15], [10, 16, -6, -6], [12, -6, -8, 8], [-12, 12, -12, -2], [-3, 13, 7, -13], [-24, 0, 9, -3], [-6, -20, 20, -2], [-12, 6, -14, -2], [-38, -4, 4, 4], [-17, -11, 5, 3], [-24, 10, 16, -12], [-6, -10, 6, -10], [12, -11, -17, 8], [-6, 2, -4, 4], [-10, -2, 4, -8], [0, -12, 10, 8], [-4, -2, 6, -14], [-36, 2, 6, -8], [-12, 6, -6, -8], [-7, -15, 11, 7], [2, 2, -4, 0], [-23, 1, 3, -17], [2, -4, 7, 21], [12, -15, 15, 0], [18, -8, 1, 11], [14, 22, -2, -8], [6, -10, -12, 8], [6, 0, 24, -8], [36, 5, -9, 12], [-26, -10, 0, -4], [-6, 2, 18, -8], [-48, 8, 5, -3], [-18, -4, -4, 6], [-5, -3, 5, -5], [-4, 20, -8, 0], [-36, -8, 11, -13], [-4, 13, -5, -4], [-44, -2, 6, 4], [10, -4, 3, -7], [21, 5, 15, -9], [-28, -1, 13, -12], [14, -20, 22, 6], [-18, 2, 20, -6], [-8, 16, 2, -6], [-33, -3, 21, -17], [25, 7, -1, -3], [-25, 3, 21, -15], [0, 7, -19, 8], [6, 10, -16, 8], [12, 0, -27, 9], [-24, -16, 14, -6], [-20, 18, 8, -6], [34, -9, -5, 12], [0, -2, 2, 8], [4, 16, -4, -12], [-14, -12, 20, -4], [14, 8, -20, 0], [16, 6, -2, -12], [18, 20, 3, -19], [-4, 2, 10, -10], [-20, -14, 8, 6], [28, -6, 2, 18], [4, -17, 9, 12], [-12, 2, 8, 0], [16, 0, -16, 2], [22, 4, -12, 10], [-15, 11, -1, 9], [-2, 10, -6, -20], [10, -10, 16, 6], [-38, -14, 8, -8], [-2, 24, -16, -6], [10, 10, 2, 0], [-30, -6, 12, -12], [26, 0, -6, 12], [4, -2, 2, 10], [0, 2, 0, 16], [-12, -16, 6, 2], [-2, -12, 11, -7], [-6, -12, 10, -8], [-12, 0, 4, -14], [-44, -6, 20, -10], [6, 9, 9, 0], [8, 2, 14, 2], [50, 4, -14, 14], [20, 1, 3, -8], [0, -19, -1, 24], [-20, 19, -3, -8], [40, 2, -12, 14], [2, 4, 1, -5], [3, -9, -23, 13], [39, 5, -7, -5], [20, -12, 6, 12], [-10, -8, 9, 7], [34, -5, -9, 16], [4, -16, 6, 20], [20, -6, -22, 10], [-6, 14, -16, 4], [4, -4, -6, -4], [-14, -8, -16, 6], [-22, -2, 10, -4], [34, -6, -2, -4], [-10, 9, -3, -16], [-42, 6, 0, -4], [-29, -7, -3, 11], [-1, 17, -7, -13], [26, 20, -1, -7], [-20, -6, 22, 2], [-14, -8, 24, -2], [33, 13, -5, -1], [44, 10, 4, -6], [-14, 18, 8, -12], [36, -4, 2, -2], [-16, 12, -15, -3], [-30, -7, 17, 0], [8, 10, 4, -16], [-19, 13, -9, -5], [-52, 2, -8, 0], [-26, -6, 16, -4], [14, -18, 0, 0], [0, -12, 6, 18], [-16, -16, 23, -9], [26, -10, -2, 10], [20, 18, -10, 0], [8, -8, -2, 4], [8, 28, -6, 4], [-10, -20, -8, 10], [-2, -14, 24, 0], [22, -2, -16, 20], [-17, 15, -7, -19], [-6, -8, 4, 20], [-36, 8, 24, -6], [27, 7, 5, -7], [-27, -19, 15, -9], [13, -23, -1, -1], [24, 8, -19, 9], [8, 26, -10, -18], [-12, 8, -7, 1], [16, 16, -18, -2], [27, -1, -7, -11], [34, -16, -2, -8], [18, 15, 3, -12], [30, 22, -4, -14], [42, -14, 4, -4], [56, -8, 4, -2], [-38, 14, 10, -2], [24, 10, -28, 16], [2, -22, 4, 12], [6, -18, 0, 6], [6, -19, -3, 12], [-38, 8, 0, -4], [18, -14, 10, 2], [4, -24, 11, 19], [6, -8, 26, 4], [28, 5, -9, -4], [-76, -4, 7, -9], [16, 11, -11, 16], [4, -8, 9, 1], [4, -6, 22, -6], [-2, 2, 10, 22], [28, -2, -6, -6], [32, 8, -2, 0], [-2, -14, 14, -6], [6, -6, -18, 6], [-10, 4, 15, 1], [-32, -4, 0, -4], [-6, 8, 12, 8], [-26, 16, -10, -6], [17, 7, -9, 17], [14, -4, -2, 16], [1, 9, -1, -1], [10, 12, -10, -10], [40, 2, -6, 14], [34, 18, -16, 8], [14, 10, 16, -8], [-25, -17, -15, 17], [-3, -1, -9, 9], [12, 4, -8, -4], [26, 6, -16, -14], [4, -22, 16, 6], [24, 2, -6, -6], [10, -8, 20, -6], [-60, 6, 6, -12], [-26, -10, 4, 12], [32, 8, -20, 18], [42, -4, -6, 2], [38, -14, -18, 16], [42, 8, -4, -8], [2, 16, -8, -26], [-26, -4, 3, 13], [-9, 21, -23, -17], [-26, 18, 10, -6], [-11, -3, -25, 11], [8, -2, 4, -8], [34, -20, 12, 6], [-36, -15, 3, 4], [52, 0, -4, 6], [44, 6, -10, -2], [14, 15, -1, -20], [-42, 8, -6, 0], [32, -10, -2, 12], [-6, 0, 10, 14], [-8, 21, -1, 8], [54, -4, 5, -5], [10, 8, -14, 4], [22, -6, -8, 2], [2, 8, -1, 9], [10, 4, -4, -16], [-4, 30, -6, -12], [6, 8, -10, -2], [14, 0, -4, -12], [-26, -1, 7, -8], [4, -12, 17, -7], [42, 8, -10, 6], [-14, -21, -1, 4], [-18, 0, 18, 12], [-2, -18, -4, 20], [2, 8, 1, 3], [-58, -19, 25, -20], [-12, -11, -1, 16], [-4, -12, 0, 18], [-42, 14, 8, 12], [16, -10, 0, -10], [0, -9, 13, -8], [-64, 12, -4, -2], [22, 2, 6, -10], [2, -4, -8, -8], [-34, -14, 28, -10], [-42, -4, 20, -2], [-1, -7, 25, -5], [-8, 8, 25, -11], [-50, -12, 16, -12], [15, 37, -23, -1], [-26, 4, 2, -2], [-41, 5, -11, -5], [24, 33, -21, 0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1476_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1476_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_1476_2_a_g(:prec:=4)
    chi := MakeCharacter_1476_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_1476_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_1476_2_a_g( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1476_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![-36, -44, -8, 4, 1]>],Snew);
    return Vf;
end function;