
// Make newform 4050.2.a.f in Magma, downloaded from the LMFDB on 09 September 2025.
// To make the character of type GrpDrchElt, type "MakeCharacter_4050_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_4050_2_a_f();".
// 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_4050_2_a_f();".
// 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_4050_a();"
function MakeCharacter_4050_a()
    N := 4050;
    order := 1;
    char_gens := [2351, 3727];
    v := [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_4050_a_Hecke( : Kf := Kf)
    return MakeCharacter_4050_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]];
    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 := [[-1], [0], [0], [-1], [0], [-4], [-3], [-4], [0], [6], [-4], [-4], [9], [2], [3], [-6], [0], [14], [14], [12], [-1], [-1], [18], [3], [-7], [6], [-16], [-18], [-4], [15], [11], [12], [-6], [-10], [6], [23], [8], [2], [-3], [12], [18], [20], [3], [-7], [-18], [11], [-22], [17], [-12], [-22], [6], [-27], [17], [-6], [15], [-3], [-6], [-1], [-10], [27], [14], [24], [-10], [21], [17], [6], [-10], [23], [12], [-16], [-33], [15], [17], [-10], [2], [3], [-24], [20], [-27], [5], [24], [2], [-33], [14], [32], [18], [33], [26], [12], [32], [6], [27], [29], [30], [20], [-15], [0], [-6], [26], [-16], [-4], [12], [-18], [-30], [14], [-10], [36], [18], [-39], [-25], [35], [-28], [27], [-22], [-7], [-15], [14], [21], [18], [-42], [38], [-46], [-6], [-24], [-28], [-24], [2], [0], [23], [-4], [20], [-33], [35], [-28], [-30], [-22], [-42], [-52], [54], [-51], [-16], [-30], [41], [-18], [-16], [51], [-10], [21], [8], [24], [-40], [15], [14], [-51], [26], [-39], [-1], [-27], [47], [-18], [48], [-51], [-16], [-24], [-3], [48], [8], [-28], [-19], [12], [24], [38], [-48], [-37], [23], [-9], [8], [12], [-52], [-16], [8], [-42], [38], [39], [-33], [-24], [-34], [-10], [-19], [45], [-13], [54], [-52], [54], [-42], [39], [59], [-46], [30], [27], [60], [-1], [-40], [14], [60], [60], [-4], [-66], [27], [-40], [-34], [-24], [8], [18], [9], [41], [8], [-27], [72], [24], [-10], [11], [33], [56], [-42], [26], [6], [33], [8], [24], [-76], [38], [-55], [-42], [2], [-57], [50], [36], [-6], [63], [6], [-34], [20], [-34], [-33], [48], [32], [-12], [50], [15], [32], [51], [48], [-37], [-54], [-30], [-10], [-64], [12], [-79], [56], [42], [-16], [-52], [-51], [62], [6], [78], [-40], [66], [-76], [-40], [-1], [-49], [-13], [80], [-78], [-10], [11], [42], [-51], [-13], [-36], [26], [50], [39], [-7], [-24], [32], [6], [60], [72], [75], [6], [14], [0], [29], [-36], [18], [-64], [-22], [-12], [23], [-18], [-64], [-22], [30], [-16], [24], [74], [81], [24], [30], [8], [3], [59], [72], [57], [71], [-45], [50], [-31], [24], [29], [-27], [5], [26], [14], [-72], [84], [8], [-12], [-55], [42], [80], [-12], [-10], [-9], [-82], [-7], [-34], [-51], [84], [71], [-12], [90], [-10], [2], [-39], [-18], [-76], [74], [-60], [32], [32], [-54], [-9], [-12], [57], [21], [38], [-33], [45], [-48], [-52], [-82], [-18], [56], [-7], [78], [56], [-21], [30], [5], [-94], [54], [-3], [-61], [-3], [-67], [36], [57], [23], [75], [50], [-3], [-13], [-82], [-16], [21], [71], [66], [6], [8], [-57], [74], [-16], [69], [62], [12], [-76], [9], [32], [-30], [-6], [-16], [2], [-3], [-94], [-54], [-37], [-18], [-6], [8], [86], [-18], [-105], [-7], [9], [-93], [42], [-58], [-36], [-66], [65], [6], [66], [15], [-64], [84]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_4050_a_Hecke( : Kf := Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_4050_2_a_f();".
// 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_4050_2_a_f(:prec:=1)
    chi := MakeCharacter_4050_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_4050_2_a_f();".
// 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_4050_2_a_f( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_4050_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<7,R![1, 1]>,<11,R![0, 1]>,<13,R![4, 1]>,<17,R![3, 1]>,<23,R![0, 1]>,<41,R![-9, 1]>],Snew);
    return Vf;
end function;