
// Make newform 4400.2.a.j in Magma, downloaded from the LMFDB on 20 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_j();".
// 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_j();".
// 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
        Kf := Rationals();
    end if;
    return [Kf!elt[1] : elt in input];
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], [-1], [0], [1], [1], [0], [-1], [-1], [0], [-1], [1], [-1], [0], [6], [8], [-9], [-4], [-7], [-4], [-5], [-14], [-4], [16], [-7], [16], [-10], [8], [8], [10], [-2], [-16], [-21], [-12], [-12], [17], [-10], [-13], [19], [-3], [22], [10], [-22], [-24], [-13], [-18], [-17], [-17], [-10], [12], [12], [-1], [16], [0], [-6], [-24], [-21], [8], [6], [8], [-22], [-20], [-16], [-16], [13], [18], [1], [34], [13], [-18], [10], [14], [-20], [0], [-4], [-2], [18], [6], [14], [-29], [-32], [-8], [24], [0], [-26], [-26], [20], [-2], [1], [9], [-36], [-5], [34], [18], [27], [-28], [-24], [8], [-2], [-28], [3], [-12], [-8], [-12], [24], [-7], [-4], [33], [18], [5], [14], [3], [46], [8], [0], [-21], [-1], [-1], [48], [-27], [33], [28], [35], [-36], [-15], [-40], [33], [18], [41], [-18], [6], [16], [21], [29], [-10], [-20], [-22], [-45], [30], [54], [34], [17], [42], [2], [-14], [4], [-24], [16], [-53], [-2], [52], [38], [-10], [41], [48], [7], [27], [26], [15], [-10], [27], [-35], [-39], [-25], [48], [0], [42], [-24], [-10], [0], [-9], [43], [16], [62], [3], [-12], [-5], [44], [-30], [11], [1], [14], [-37], [-63], [11], [58], [-24], [-20], [-52], [0], [40], [10], [-50], [-52], [-2], [38], [-48], [-12], [-34], [-42], [-33], [11], [40], [-35], [-70], [-54], [-25], [8], [44], [-36], [58], [17], [42], [-16], [49], [-34], [15], [23], [42], [-8], [-31], [58], [-66], [33], [52], [-24], [-15], [-6], [3], [-24], [40], [58], [37], [46], [-10], [-4], [35], [-29], [14], [8], [-51], [-15], [-52], [62], [20], [-30], [30], [24], [-4], [-45], [-33], [62], [60], [-48], [-5], [36], [51], [-54], [-10], [-26], [-7], [-44], [6], [17], [-44], [18], [-50], [-36], [5], [28], [-54], [-30], [-83], [-46], [-76], [83], [8], [-12], [62], [-32], [-63], [-67], [-76], [44], [-63], [-8], [-39], [50], [64], [35], [-48], [-24], [60], [75], [85], [62], [-30], [-32], [69], [76], [-50], [-55], [40], [52], [-20], [-78], [-8], [-12], [-64], [21], [4], [8], [36], [-72], [-21], [57], [-56], [-72], [-6], [52], [20], [5], [-56], [-42], [50], [49], [-65], [12], [-28], [41], [86], [31], [-30], [-76], [-36], [-9], [-41], [28], [7], [-19], [25], [54], [48], [-9], [-53], [-45], [-11], [-46], [41], [82], [1], [63], [4], [24], [19], [-47], [-51], [-15], [-27], [-14], [-64], [88], [30], [-72], [29], [67], [12], [12], [33], [-26], [-46], [-28], [56], [-75], [36], [-46], [-66], [42], [-77], [48], [89], [-26], [-14], [-88], [57], [-100], [38], [64], [-3], [74], [14], [-11], [-39], [-36], [52], [-60], [-6], [-74], [51], [-2], [-63], [-57], [50], [-24], [38], [-22], [-66], [-43], [68], [21], [30], [6], [-48], [-8], [8], [-94], [-42], [18], [70], [0], [69], [-86], [-58], [42], [59], [-16], [-31], [-79], [70], [14], [-18]];
    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_j();".
// 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_j(:prec:=1)
    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_j();".
// 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_j( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_4400_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![1, 1]>,<7,R![-1, 1]>,<13,R![0, 1]>],Snew);
    return Vf;
end function;