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