
// Make newform 1050.2.a.r in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_1050_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_1050_2_a_r();".
// 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_1050_2_a_r();".
// 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_1050_a();"
function MakeCharacter_1050_a()
    N := 1050;
    order := 1;
    char_gens := [701, 127, 451];
    v := [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_1050_a_Hecke(Kf)
    return MakeCharacter_1050_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], [1], [0], [1], [2], [-1], [3], [0], [-1], [-5], [7], [-2], [7], [-11], [8], [-1], [-5], [-3], [-12], [12], [-6], [10], [-11], [-10], [-2], [12], [-11], [-2], [0], [-6], [-22], [-8], [-12], [20], [15], [-18], [-2], [-1], [18], [24], [-10], [-18], [-3], [14], [-17], [0], [-13], [9], [-17], [-10], [24], [0], [22], [7], [23], [-21], [-10], [-28], [-2], [32], [-16], [24], [-2], [-18], [-6], [3], [17], [-27], [-2], [15], [-6], [25], [23], [24], [-25], [4], [10], [13], [-8], [-10], [25], [22], [-3], [14], [35], [-16], [20], [33], [12], [4], [33], [-20], [-2], [2], [25], [-6], [20], [-3], [4], [32], [-37], [18], [-31], [30], [7], [38], [-27], [14], [5], [-8], [28], [14], [38], [-10], [-38], [42], [34], [-22], [14], [-30], [42], [29], [-32], [-46], [42], [27], [20], [-10], [13], [-11], [-25], [29], [32], [-2], [-18], [-20], [24], [-22], [-42], [0], [-18], [-18], [54], [-2], [-5], [-40], [29], [-22], [0], [-16], [-32], [7], [39], [58], [-17], [-53], [0], [-45], [-42], [-48], [-52], [-16], [8], [12], [18], [-16], [-8], [58], [-25], [24], [-30], [-53], [-43], [-31], [20], [35], [-28], [-23], [-11], [-35], [-42], [47], [-6], [-47], [24], [-30], [8], [-6], [25], [-48], [-36], [24], [32], [-38], [-32], [49], [-43], [-46], [-62], [-16], [50], [17], [-47], [70], [-65], [-12], [-5], [34], [30], [62], [-22], [22], [34], [-27], [50], [-8], [8], [42], [33], [-1], [32], [35], [10], [4], [-12], [10], [-26], [-40], [28], [62], [4], [-20], [-68], [22], [-56], [8], [-10], [54], [-10], [62], [24], [62], [-1], [-10], [39], [0], [53], [-28], [60], [24], [68], [27], [33], [10], [24], [-10], [-58], [38], [-62], [-2], [24], [48], [35], [-66], [23], [10], [75], [7], [49], [59], [7], [3], [-56], [0], [-12], [-36], [48], [50], [-73], [-33], [44], [-28], [-12], [37], [-32], [-3], [-11], [78], [25], [-50], [-38], [-12], [-36], [32], [-46], [0], [-23], [-66], [75], [-42], [44], [-57], [-10], [24], [52], [43], [-22], [-25], [-45], [-26], [-6], [-25], [42], [-16], [3], [50], [-20], [-63], [44], [80], [-78], [-62], [-78], [-16], [-66], [-28], [-45], [74], [48], [-21], [-78], [-37], [-15], [-21], [-33], [28], [-20], [-21], [-38], [-7], [34], [-42], [-60], [-8], [49], [0], [22], [-17], [12], [-2], [52], [13], [-63], [79], [-30], [-51], [30], [-3], [78], [19], [28], [2], [48], [-40], [-42], [-1], [-22], [24], [57], [72], [-60], [-81], [-15], [47], [28], [15], [-8], [84], [85], [-32], [42], [-96], [-12], [48], [40], [24], [-58], [-42], [4], [-22], [-65], [84], [-90], [38], [-43], [14], [-35], [45], [12], [102], [85], [54], [48], [33], [-60], [57], [-22], [42], [54], [0], [84], [58], [24], [-13], [-27], [-8], [85], [3], [23], [-106], [85], [43], [-27], [105], [44], [-52], [-86], [90], [-68], [90]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1050_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1050_2_a_r();".
// 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_1050_2_a_r(:prec:=1)
    chi := MakeCharacter_1050_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_1050_2_a_r();".
// 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_1050_2_a_r( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1050_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<11,R![-2, 1]>,<13,R![1, 1]>,<17,R![-3, 1]>,<19,R![0, 1]>],Snew);
    return Vf;
end function;