
// Make newform 3800.2.a.l in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_3800_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_3800_2_a_l();".
// 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_3800_2_a_l();".
// 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_3800_a();"
function MakeCharacter_3800_a()
    N := 3800;
    order := 1;
    char_gens := [951, 1901, 1977, 401];
    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_3800_a_Hecke(Kf)
    return MakeCharacter_3800_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], [0, 0], [1, -1], [0, -1], [1, 2], [-1, 0], [-1, 0], [-1, 3], [1, -2], [2, -1], [-8, -2], [0, 5], [2, 3], [-8, 0], [-1, -2], [13, -1], [2, -1], [-1, -5], [-4, -5], [-11, 2], [2, 2], [-6, -6], [-8, -3], [-2, 4], [6, -7], [0, -7], [1, -1], [-5, 0], [6, -5], [-14, -2], [16, -4], [-5, 8], [-4, 0], [-12, 4], [14, 2], [-6, -4], [-10, -5], [-2, 15], [-16, 2], [-6, 12], [-8, -6], [-7, 3], [-2, 4], [-8, 9], [9, -5], [-15, 7], [-6, -2], [13, -3], [-10, 12], [8, 4], [15, 9], [0, -4], [-12, 11], [-16, -9], [10, 4], [-10, 0], [-3, 13], [-8, 8], [-8, 3], [-18, 6], [9, -14], [-4, -20], [19, 3], [-1, -4], [13, -14], [1, 13], [0, -9], [-18, 2], [6, 16], [-5, -22], [1, -5], [8, 18], [11, 12], [15, -13], [8, 9], [-2, 7], [0, -16], [-16, 7], [4, -1], [-6, -15], [-1, 10], [-4, -1], [2, -5], [-18, -9], [0, -11], [-2, -24], [-5, -8], [-20, 9], [-8, -10], [12, 7], [8, 18], [-10, 2], [22, -3], [26, -5], [-23, -7], [4, 4], [-28, 0], [-1, 15], [6, 17], [-10, 24], [32, 8], [28, 12], [8, -12], [18, 21], [25, 0], [14, 13], [42, 0], [-18, -11], [16, -11], [-22, -6], [-10, 9], [12, 14], [-12, 13], [-22, 12], [-18, 15], [-34, -2], [-5, 5], [4, -20], [1, 7], [35, -6], [-28, 0], [27, 4], [-12, 8], [-18, -18], [16, -12], [22, 8], [29, -1], [23, 15], [-12, -16], [6, -1], [-10, 3], [34, 3], [22, 4], [-21, -12], [-11, 14], [-9, 0], [27, 3], [-21, 10], [-17, -6], [-1, 29], [-12, 28], [15, -23], [7, -19], [-11, -26], [-16, 24], [-14, -24], [-16, 0], [-16, -15], [28, -5], [21, -6], [-16, -20], [10, 18], [-8, -5], [7, 11], [-4, 4], [3, -31], [17, -6], [-11, 8], [43, -10], [-4, -32], [2, 15], [-36, 10], [4, 28], [20, 13], [34, -14], [-22, -23], [-12, 11], [-12, 32], [12, 21], [25, 21], [-3, 36], [34, 12], [-43, 4], [-26, 3], [4, -2], [2, 11], [-6, 32], [14, -7], [4, 3], [-3, -9], [11, 5], [-32, 18], [36, -8], [21, -1], [-2, 4], [39, -4], [-28, -18], [34, 11], [19, -17], [-6, 31], [12, 16], [4, -42], [35, 22], [12, 8], [24, 21], [-33, -6], [-40, -4], [39, -20], [9, -11], [-4, 8], [0, -8], [-9, -2], [24, -7], [-14, -22], [20, -24], [-17, -13], [39, 9], [-27, 6], [1, -25], [-17, -30], [12, -16], [-56, 2], [-1, -33], [2, 3], [6, -35], [19, -21], [-18, 4], [18, -10], [42, 12], [60, 0], [6, -30], [-2, 5], [-55, -11], [3, -7], [42, 0], [46, 12], [48, 7], [-24, 27], [26, -22], [6, -23], [-23, 23], [-12, 20], [-42, -8], [-6, -38], [-29, -5], [52, 16], [-44, -1], [-22, -30], [2, 7], [20, -20], [8, 35], [-7, -17], [11, -10], [-20, -17], [-40, -18], [14, -20], [-16, 22], [16, -6], [47, 13], [2, -27], [-61, -6], [-7, 7], [-10, 43], [0, -27], [-24, -17], [-10, 20], [12, 24], [-32, 30], [-36, 2], [-30, -15], [-20, 32], [20, -37], [-35, -26], [12, -38], [-11, -29], [53, 6], [3, 8], [-37, -15], [-38, 31], [27, -12], [-73, -7], [13, -44], [-5, 11], [-18, 27], [-11, -29], [-32, 33], [-5, -42], [24, 8], [20, 4], [10, -18], [-21, 33], [-1, -37], [39, 6], [-20, 28], [44, -2], [-53, 12], [10, -48], [-29, -5], [-34, 8], [22, -15], [4, 12], [-38, 1], [12, -40], [76, -4], [24, -9], [-30, -30], [-66, 0], [-17, 9], [-76, 3], [-20, 18], [-29, -18], [21, 23], [-41, 15], [26, -27], [38, 0], [-3, 9], [49, -24], [14, -36], [-48, 24], [16, 16], [42, 1], [-54, -9], [28, 8], [12, -38], [16, -23], [22, -2], [-32, 11], [17, -32], [-26, 0], [-29, -37], [-51, -10], [15, 10], [24, 27], [27, 36], [-6, 35], [-19, -25], [-17, -15], [22, -7], [60, 17], [64, 8], [-13, 44], [5, 31], [10, 26], [-30, -2], [-6, -33], [-39, 10], [64, 0], [15, -16], [-3, -17], [3, -28], [-69, -14], [23, 28], [38, 30], [65, -4], [-32, -38], [66, 16], [-37, 9], [-20, -37], [44, -24], [-60, 2], [-58, 17], [-36, -24], [4, 28], [-40, -32], [12, -32], [73, -3], [12, -32], [-19, -12], [-22, -54], [40, 23], [-24, 8], [-16, -37], [5, -1], [56, -17], [-60, -9], [-46, -12], [0, 41], [-18, -8], [10, 22], [13, 1], [-69, 11], [55, 14], [-32, 2], [-12, -32], [-51, 17], [-8, 24], [-4, -10], [19, -28], [60, -23], [-48, 2], [-44, -6], [35, -33], [-34, 24], [-31, 31], [34, -46], [43, -23], [20, 7], [14, 6], [28, -16], [34, 28], [35, -22], [30, 30], [-12, 11], [-22, -55], [62, 12], [62, 22], [-16, 24], [-2, 16], [-16, -31], [6, 48], [-42, -40], [22, 22], [28, 37], [-20, 28], [7, 45], [-10, 63], [36, 28], [-37, 7], [2, -13], [-4, -13], [6, -20], [25, 37], [-16, 19], [-7, -48], [-36, -13], [-28, -17], [50, 17], [15, 26], [4, 32], [-24, -16], [-23, -2], [17, 59], [11, 3], [14, 55], [-3, 14], [-41, -33], [-45, -26], [12, -32], [3, -13]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_3800_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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