
// Make newform 4400.2.a.bs in Magma, downloaded from the LMFDB on 16 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_4400_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_4400_2_a_bs();".
// 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_4400_2_a_bs();".
// 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 := [-3, -1, 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_4400_a();"
function MakeCharacter_4400_a()
    N := 4400;
    order := 1;
    char_gens := [2751, 3301, 177, 1201];
    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_4400_a_Hecke(Kf)
    return MakeCharacter_4400_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], [0, 0], [3, -1], [1, 0], [-5, 0], [0, -3], [1, 0], [-5, -1], [-6, 3], [-1, -4], [-7, 2], [-1, -2], [-2, 4], [-3, 0], [-1, 1], [5, 4], [-1, -3], [-4, 0], [-2, 2], [1, 3], [4, 3], [8, -5], [4, -1], [-13, -1], [11, -5], [-4, 3], [-3, 0], [0, 5], [-3, 6], [-8, 7], [3, 3], [6, 3], [-3, 4], [8, 4], [13, -6], [-10, -4], [7, 1], [-5, 8], [-9, 6], [-1, -5], [-13, -3], [-8, -1], [-4, -4], [-11, -1], [18, -5], [-7, 14], [5, -12], [4, -1], [2, -9], [-9, -3], [-5, -7], [-8, 13], [0, -3], [18, 0], [-9, -6], [-12, 9], [-9, 4], [-19, 2], [-15, 6], [4, 1], [-10, 4], [-10, -3], [9, -6], [-14, 6], [-6, 3], [17, 2], [-6, 16], [-1, -5], [-19, 6], [9, -12], [-10, 4], [-23, 1], [-9, -8], [-17, 6], [-6, 12], [12, 0], [-24, 1], [-22, 4], [3, -4], [-11, 2], [3, -1], [-33, 0], [-5, 0], [-22, -1], [-4, -2], [35, -5], [23, -1], [23, 4], [21, -4], [9, -12], [21, 6], [-5, 4], [19, -4], [15, -5], [14, -2], [-25, 1], [0, -18], [-14, -8], [5, 9], [9, -7], [-1, -8], [15, -3], [-7, -17], [3, -11], [4, -21], [-18, 15], [11, -2], [18, 3], [5, -3], [-2, 4], [32, -7], [-8, 2], [-7, 8], [23, -13], [37, -4], [-22, 0], [-1, -14], [10, -7], [24, 3], [5, -6], [-7, -10], [8, -14], [11, 16], [7, -15], [-32, -4], [-4, -12], [-21, 6], [-14, -11], [-15, -2], [-19, -7], [43, -1], [20, 11], [-3, -20], [-32, 8], [-19, 6], [13, -7], [1, 4], [8, -5], [12, 12], [21, -26], [12, 0], [0, 8], [0, 6], [-14, 19], [-7, 7], [-8, 9], [3, 0], [-46, 2], [-17, 2], [-20, 0], [-15, 15], [11, 18], [29, -14], [-4, 0], [34, -4], [-36, 4], [49, 2], [0, 4], [55, 2], [-21, 12], [18, -24], [8, 3], [3, -21], [29, 10], [-28, 16], [10, 3], [-26, -9], [-19, -12], [12, -6], [23, -17], [29, -21], [-4, 19], [7, 9], [37, -6], [-14, -4], [12, 4], [-21, 0], [26, -6], [-4, 24], [-12, 14], [5, -5], [-31, 14], [42, 0], [-45, 12], [41, -8], [6, -2], [9, -7], [13, -26], [8, 4], [-18, 13], [24, 0], [-13, 8], [0, 15], [-29, 23], [-49, 10], [-4, 18], [43, -18], [-19, 13], [16, -1], [-19, 25], [13, 9], [-50, 3], [-38, 1], [-6, -12], [-19, 31], [28, 0], [43, 2], [30, -24], [-25, 23], [3, -8], [-13, -2], [-27, 11], [20, -26], [3, 24], [18, 2], [-44, 4], [-8, -1], [-15, -3], [27, -33], [18, -19], [47, -10], [-4, -8], [-38, 1], [41, 4], [-7, -12], [11, -5], [-12, 30], [56, -12], [-54, 9], [-5, 15], [-17, 6], [31, 6], [57, 0], [-10, 3], [45, -18], [-2, 10], [39, -3], [28, -31], [-9, 21], [31, -34], [2, -10], [38, -18], [-9, -4], [-54, -6], [-33, -3], [-25, -15], [-27, 9], [-20, -9], [0, 6], [16, 6], [-29, 2], [54, 6], [7, 16], [-15, 21], [-30, -6], [-23, -2], [-15, -10], [29, -5], [-14, 12], [-64, 12], [30, 6], [-17, 22], [-46, -13], [35, -14], [38, 2], [9, 12], [-29, -13], [-53, -8], [-9, 36], [-4, 30], [7, -2], [52, -6], [17, 8], [-68, 9], [1, 16], [8, -8], [46, 1], [63, -13], [31, -22], [18, -15], [-28, 8], [1, 17], [1, 4], [65, 3], [-36, 12], [18, 13], [13, -25], [-71, -3], [-12, 21], [-39, -12], [35, -20], [-24, 12], [6, -15], [-25, 32], [25, -25], [-69, 4], [-61, 10], [-14, 8], [-19, -6], [23, 14], [30, -12], [25, 15], [67, -4], [-42, -11], [-45, 22], [-40, -2], [37, -11], [32, 1], [49, -12], [14, 1], [-48, 3], [11, -35], [17, 0], [27, 24], [30, -34], [42, -24], [14, -26], [-20, -18], [35, 7], [5, 5], [61, -21], [3, -18], [-27, -22], [-12, -9], [-26, -8], [28, -27], [25, 19], [-4, -14], [-18, 24], [-2, -38], [-37, -20], [4, -21], [-51, 12], [-34, 38], [-26, -28], [12, -34], [-46, 10], [21, -31], [-49, -3], [-2, -18], [-37, 4], [5, -38], [-15, -23], [3, 12], [-10, -29], [-12, 35], [15, -22], [-6, -21], [17, 4], [-1, 5], [-37, 23], [-15, 6], [-32, -11], [32, 0], [15, 24], [30, 0], [65, -17], [23, 10], [28, 8], [-5, 9], [-21, 42], [-41, 2], [64, -1], [-23, -17], [19, -30], [-12, 30], [-21, 8], [-60, -16], [28, -31], [28, 0], [25, -7], [4, 23], [6, -5], [-15, 28], [63, -9], [-85, -5], [58, -24], [75, -15], [-41, -9], [40, -22], [-51, -6], [22, -2], [12, -27], [1, -30], [-20, 35], [5, 11], [-24, -20], [-89, -2], [26, 28], [-3, 5], [0, -24], [15, 0], [-24, -10], [-3, 6], [55, -18], [0, -5], [-14, 38], [-15, 16], [21, 3], [52, -32], [-21, 45], [25, -35], [21, -12], [-9, 18], [43, 18], [26, -10], [-45, 12], [52, -32], [16, 26], [28, 21], [3, 24], [-61, 10], [5, -37], [-26, 0], [-47, 14], [-51, -6], [32, -9], [-3, -18], [64, -19], [-31, 46], [61, -18], [-23, 38], [9, -18], [-38, -18], [-28, 28], [-30, 48], [-84, 9], [-30, -20], [-24, 3]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_4400_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


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