
// Make newform 4050.2.a.k in Magma, downloaded from the LMFDB on 05 February 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_k();".
// 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_k();".
// 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)
    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], [-5], [-6], [5], [3], [0], [8], [-2], [-3], [4], [9], [-9], [-15], [-4], [4], [-6], [-14], [14], [-6], [-18], [16], [0], [-17], [6], [8], [-6], [7], [15], [-6], [-13], [6], [-22], [7], [16], [-12], [-9], [-21], [2], [12], [-20], [9], [-16], [5], [16], [12], [-28], [-24], [-6], [-10], [-21], [-6], [-3], [6], [-22], [-11], [15], [-8], [9], [16], [-18], [-8], [-33], [32], [22], [6], [26], [-6], [18], [-8], [10], [-25], [15], [-36], [-14], [15], [5], [-24], [-10], [24], [-8], [-10], [30], [27], [-8], [0], [-29], [18], [30], [-29], [-3], [-13], [-24], [6], [-9], [10], [-16], [-20], [-3], [36], [15], [-4], [-8], [-30], [18], [30], [5], [-32], [-23], [42], [-31], [-16], [-30], [40], [24], [-42], [15], [-28], [-14], [-15], [18], [-1], [-6], [-34], [-6], [19], [22], [20], [24], [14], [37], [27], [14], [42], [34], [-6], [-27], [32], [30], [4], [12], [8], [36], [-26], [-6], [-4], [45], [31], [-42], [-14], [-33], [-50], [6], [-16], [-42], [52], [-54], [48], [0], [-8], [45], [6], [-24], [8], [55], [-1], [3], [57], [-4], [42], [-8], [50], [15], [29], [-30], [16], [14], [-8], [45], [-59], [-48], [-45], [18], [43], [10], [53], [30], [-50], [18], [20], [-66], [0], [0], [65], [-41], [-30], [-3], [54], [-40], [7], [-7], [36], [-51], [50], [-6], [-6], [-4], [-38], [12], [19], [60], [-48], [11], [16], [18], [-27], [6], [26], [-22], [21], [25], [-60], [8], [18], [30], [-17], [-3], [-5], [-25], [-16], [51], [28], [-63], [-43], [66], [-9], [60], [60], [29], [-11], [38], [-30], [30], [-5], [-69], [-55], [36], [22], [-15], [-72], [35], [15], [15], [26], [-44], [-57], [52], [-29], [-18], [-28], [61], [60], [17], [-54], [-27], [40], [-27], [50], [16], [46], [-40], [82], [-44], [-24], [74], [-31], [81], [-81], [32], [3], [-64], [-32], [60], [-74], [-3], [-52], [-30], [30], [12], [-78], [-84], [37], [-18], [38], [-66], [-15], [-50], [-56], [33], [-28], [18], [23], [10], [18], [-34], [18], [-26], [-36], [42], [-33], [-44], [-48], [17], [-9], [0], [16], [-78], [-7], [4], [36], [40], [30], [47], [20], [64], [27], [39], [-10], [-27], [26], [66], [65], [12], [26], [-36], [-43], [-83], [7], [-12], [-48], [26], [15], [-60], [38], [-14], [-78], [45], [23], [-62], [36], [1], [32], [6], [0], [39], [18], [-51], [34], [69], [-9], [3], [10], [-8], [-69], [-44], [-61], [-57], [29], [81], [-48], [74], [49], [3], [6], [52], [54], [4], [-18], [-36], [79], [-6], [-16], [99], [-28], [13], [-62], [-93], [-34], [-3], [-60], [-62], [-42], [-2], [-34], [69], [65], [-48], [32], [54], [1], [-48], [-66], [-70], [-11], [3], [94], [12], [70], [18], [-60], [35], [-68], [-12], [48], [16], [-78], [0], [72], [-89], [33], [63], [-56], [-27], [48], [-99], [-52], [0]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_4050_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_4050_2_a_k();".
// 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_k(: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_k();".
// 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_k( : 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![5, 1]>,<17,R![6, 1]>,<23,R![-3, 1]>,<41,R![3, 1]>],Snew);
    return Vf;
end function;