
// Make newform 3920.2.a.by in Magma, downloaded from the LMFDB on 19 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_3920_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_3920_2_a_by();".
// 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_3920_2_a_by();".
// 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 := [-2, 0, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rfbasis := [Kf.1^i : i in [0..Degree(Kf)-1]];
    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_3920_a();"
function MakeCharacter_3920_a()
    N := 3920;
    order := 1;
    char_gens := [1471, 981, 3137, 3041];
    v := [1, 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_3920_a_Hecke(Kf)
    return MakeCharacter_3920_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], [1, 1], [1, 0], [0, 0], [1, 0], [-1, 1], [-1, -1], [2, 0], [-2, -3], [1, 0], [6, 3], [2, 7], [-6, 1], [6, 4], [9, -1], [8, -1], [6, -3], [4, 2], [0, 1], [-6, 6], [8, 2], [9, -4], [0, -8], [-4, -4], [-7, -5], [-2, -6], [11, -3], [6, 6], [-7, -8], [-2, -5], [-8, 3], [16, 3], [-8, -8], [0, 1], [-12, 2], [1, 4], [-2, -6], [-10, 5], [9, -1], [1, 9], [10, -10], [10, 11], [-11, -2], [12, -8], [-8, -3], [6, -13], [-3, -10], [1, -13], [3, -1], [14, -7], [12, -6], [-1, 4], [8, -13], [-8, 11], [-2, -6], [-2, -12], [-20, 1], [8, -4], [-6, -5], [7, 8], [1, 17], [17, 1], [-19, 5], [10, 0], [11, 9], [18, -4], [-18, 6], [-6, -5], [-2, -19], [18, -8], [-9, -15], [24, 4], [7, 3], [20, 2], [-6, -16], [20, 2], [-9, 14], [7, -1], [1, -18], [2, 22], [-30, 1], [-15, -8], [-17, -4], [10, -12], [-8, 3], [-2, 26], [-19, -6], [12, -5], [4, 20], [0, -18], [7, 1], [-8, -23], [-14, 9], [3, 6], [7, 14], [7, -17], [-12, -3], [-6, 0], [-24, 6], [-1, -8], [-4, -22], [-12, 10], [-6, 24], [-20, -10], [-2, 18], [35, 1], [-28, 10], [-25, 11], [25, 8], [-10, 20], [13, 5], [18, 8], [-10, -1], [-8, -5], [15, 16], [4, 20], [-9, -15], [4, 10], [6, -20], [-9, -6], [28, 2], [48, 2], [-29, -5], [16, -2], [-4, 10], [-23, -10], [-27, 4], [20, 21], [30, 8], [-3, 21], [-15, 14], [-4, 7], [-3, -16], [0, -26], [-20, 11], [10, 6], [3, 13], [1, -31], [-43, 5], [15, 24], [-24, -11], [13, 6], [12, 17], [30, 17], [-6, -1], [12, -3], [2, -8], [-46, -6], [-28, -18], [16, -27], [10, 12], [-12, -4], [-4, -22], [10, -20], [-22, 21], [-32, 8], [11, -28], [2, 23], [7, -15], [-24, 0], [0, -7], [40, 13], [-26, -2], [10, -27], [-6, 34], [15, 13], [-6, 8], [3, 7], [-1, -40], [-4, 6], [-26, -20], [-22, 26], [27, -10], [10, -13], [34, -8], [-12, 22], [-13, -28], [-35, 0], [42, 0], [-16, -10], [-36, -12], [-4, 14], [2, -34], [-2, -12], [0, -12], [4, -17], [12, 3], [-19, 3], [13, 2], [-12, -18], [10, 8], [26, 6], [17, -2], [-50, -13], [44, 6], [-39, 13], [-49, 6], [-16, -1], [-15, 33], [-21, 23], [-16, -30], [6, -14], [6, 20], [42, 6], [10, -10], [-9, 11], [16, -17], [8, 6], [-28, 12], [28, 8], [-4, 21], [-8, -19], [32, 3], [11, 15], [-18, -20], [28, -10], [-42, 1], [14, 3], [-38, -2], [-30, 7], [-26, -8], [6, 28], [1, 2], [30, 16], [11, 31], [34, -4], [3, 13], [-17, -18], [27, -17], [10, 28], [-4, -22], [-30, 28], [-10, -38], [1, -8], [-1, 5], [-5, 35], [-10, 7], [-4, -2], [-21, 12], [0, 21], [-4, -44], [-48, 4], [27, -33], [16, -14], [17, -25], [-2, -8], [-4, 20], [26, -2], [-5, 34], [14, 18], [-18, 32], [58, -12], [4, 1], [-12, -8], [6, 30], [-39, 14], [-72, 2], [50, -16], [33, -9], [-13, 11], [-2, -34], [-14, -5], [10, -45], [69, -5], [62, 4], [-16, -29], [-3, 34], [-26, -34], [0, -23], [-2, 20], [14, -33], [32, -3], [5, 17], [-2, 36], [-30, -28], [-23, -13], [48, -2], [52, 16], [7, 8], [-34, -10], [15, -37], [-7, -38], [-20, -12], [44, 19], [1, 31], [-5, -8], [-2, 42], [18, -4], [22, -7], [50, 9], [27, -10], [25, -13], [-28, -7], [6, 5], [-54, 4], [-18, -47], [56, 7], [5, 29], [0, -30], [-33, 21], [45, -31], [14, -7], [19, -12], [24, 20], [-1, 10], [18, -25], [34, -26], [8, 8], [-18, 24], [4, 45], [-28, 18], [29, 6], [0, 28], [-6, 10], [58, 8], [52, -12], [26, -28], [61, 20], [25, -23], [13, -10], [26, 32], [-32, -23], [16, -7], [0, 23], [-14, -34], [-30, -9], [-59, -6], [0, -12], [-20, 17], [0, 13], [-16, -2], [-36, -33], [24, 23], [-5, 5], [62, 6], [51, 25], [13, -22], [32, -10], [-10, 4], [9, -41], [-6, 1], [-16, -3], [40, 13], [21, -10], [-30, 33], [-20, 28], [56, -17], [52, 4], [-60, 9], [-21, -35], [30, 15], [28, -6], [19, 32], [55, 5], [1, 18], [-40, 10], [-74, 12], [12, -31], [34, 20], [-62, -19], [-8, -38], [-54, 7], [-8, -3], [-59, 2], [-4, -60], [30, -10], [-31, 21], [10, -38], [-33, -12], [-49, 6], [40, -22], [36, 9], [-25, -16], [38, 8], [22, 10], [-64, -14], [13, 12], [57, 21], [-36, -7], [3, -25], [-6, 40], [-26, 25], [-52, 11], [-22, -31], [38, -23], [-13, -23], [85, 2], [-23, -11], [44, -22], [15, -27], [-1, -18], [15, -37], [1, 38], [-60, 6], [1, -4], [-68, -4], [-12, -2], [-50, -13], [63, 12], [80, 12], [-96, 0], [-72, 15], [-30, 47], [19, 3], [24, -20], [6, 70], [38, 15], [25, -54], [56, -16], [24, -24], [-67, 19], [14, 50], [-58, -3], [22, -50], [26, 9], [28, 14], [21, -58], [-36, 22], [30, 14], [-53, 1], [-19, -40], [-30, -8], [50, -28], [32, -30], [-37, 31], [-79, 1], [-8, -25], [-52, -10], [-70, 7], [8, -40]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_3920_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_3920_2_a_by();".
// 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_3920_2_a_by(:prec:=2)
    chi := MakeCharacter_3920_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_3920_2_a_by();".
// 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_3920_2_a_by( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_3920_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![-1, -2, 1]>,<11,R![-1, 1]>,<13,R![-1, 2, 1]>,<17,R![-1, 2, 1]>],Snew);
    return Vf;
end function;