
// Make newform 4050.2.a.u in Magma, downloaded from the LMFDB on 24 April 2024.
// 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_u();".
// 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_u();".
// 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+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 := [[1], [0], [0], [-2], [0], [4], [-6], [-7], [0], [6], [-10], [-2], [-9], [1], [6], [-12], [9], [-4], [13], [-6], [1], [2], [9], [-15], [-17], [-12], [-2], [9], [2], [3], [-20], [12], [-3], [-4], [6], [-4], [-14], [7], [12], [-12], [9], [-16], [-6], [-2], [18], [-16], [5], [16], [-15], [-10], [3], [0], [-19], [-15], [3], [-6], [30], [-28], [-2], [18], [19], [12], [-8], [30], [19], [30], [11], [-14], [-12], [-10], [15], [-12], [10], [4], [20], [24], [-6], [22], [-18], [-31], [-3], [-10], [-6], [-2], [-4], [36], [15], [19], [-6], [-26], [-15], [-36], [-2], [3], [11], [24], [36], [-6], [1], [32], [-5], [-12], [9], [42], [35], [-35], [36], [-9], [-30], [-10], [40], [-44], [-9], [23], [-16], [-33], [13], [-48], [-36], [-33], [2], [-2], [-12], [-3], [35], [48], [-28], [-18], [40], [22], [-25], [6], [-4], [10], [-21], [5], [6], [-20], [-36], [39], [5], [24], [-14], [-27], [32], [42], [10], [-39], [-31], [12], [10], [-30], [-20], [6], [7], [-12], [8], [54], [-23], [54], [-21], [-30], [-20], [21], [-15], [-30], [44], [28], [53], [6], [-3], [20], [-12], [-11], [-4], [45], [-52], [-42], [-2], [44], [-26], [39], [34], [15], [42], [-24], [34], [-35], [-22], [-18], [-2], [27], [-40], [0], [-12], [-57], [-1], [34], [-66], [-18], [24], [38], [-62], [50], [15], [12], [2], [-51], [-27], [5], [43], [30], [-8], [-45], [0], [-22], [-14], [-45], [36], [-6], [32], [-34], [6], [-38], [-39], [-28], [39], [-30], [-26], [-21], [-26], [-7], [50], [-15], [28], [-24], [-55], [0], [-60], [0], [21], [-7], [34], [-34], [-66], [-60], [-2], [51], [-19], [30], [-32], [-39], [-30], [-37], [0], [-75], [-64], [-53], [-12], [-38], [46], [39], [20], [-14], [-3], [44], [-66], [33], [4], [60], [-10], [-29], [-62], [32], [73], [-8], [-57], [-52], [-13], [60], [60], [-58], [0], [26], [4], [66], [34], [60], [-40], [-75], [-66], [-81], [-57], [-48], [22], [18], [-82], [0], [-36], [7], [46], [-78], [2], [-9], [-67], [-29], [-12], [56], [-12], [-68], [72], [42], [39], [76], [-30], [-25], [-36], [-78], [34], [9], [23], [-77], [60], [4], [-54], [-43], [44], [13], [54], [60], [62], [-6], [-10], [-60], [53], [12], [-46], [-45], [-13], [-20], [-44], [-3], [12], [-16], [-42], [9], [-82], [-35], [-66], [-18], [-19], [-41], [30], [-86], [32], [63], [0], [-3], [-30], [78], [46], [66], [72], [-21], [49], [70], [36], [-86], [-7], [-12], [-31], [-24], [-96], [62], [16], [-81], [42], [61], [6], [22], [0], [-30], [-2], [-57], [-55], [12], [-94], [22], [76], [-12], [11], [-66], [69], [28], [96], [7], [2], [-21], [32], [12], [14], [9], [-44], [-15], [12], [-34], [100], [-57], [-95], [27], [-47], [-54], [-12], [89], [73], [-72], [12], [82], [9], [84], [6], [-2], [54], [-12], [-101], [-42], [-39], [105], [71], [84]];
    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_u();".
// 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_u(: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_u();".
// 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_u( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_4050_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<7,R![2, 1]>,<11,R![0, 1]>,<13,R![-4, 1]>,<17,R![6, 1]>,<23,R![0, 1]>,<41,R![9, 1]>],Snew);
    return Vf;
end function;