
// Make newform 1936.2.a.g in Magma, downloaded from the LMFDB on 02 January 2025.
// To make the character of type GrpDrchElt, type "MakeCharacter_1936_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_1936_2_a_g();".
// 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_1936_2_a_g();".
// 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_1936_a();"
function MakeCharacter_1936_a()
    N := 1936;
    order := 1;
    char_gens := [1695, 485, 849];
    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_1936_a_Hecke(Kf)
    return MakeCharacter_1936_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 := [[0], [0], [3], [4], [0], [-3], [-3], [4], [8], [5], [4], [11], [-7], [-12], [8], [-1], [4], [2], [-4], [-12], [-10], [8], [4], [3], [-13], [10], [16], [8], [-7], [-1], [4], [-8], [-6], [-4], [-3], [-12], [-2], [4], [-8], [18], [20], [7], [-12], [5], [-3], [-20], [-28], [4], [-12], [3], [-11], [-8], [-18], [16], [-1], [-4], [-25], [12], [17], [6], [16], [-3], [8], [-12], [-9], [-18], [8], [-23], [16], [-7], [-9], [12], [-24], [10], [-16], [-16], [31], [-5], [27], [-7], [40], [-9], [-8], [-5], [28], [0], [-21], [-39], [37], [-12], [24], [-16], [12], [-16], [-4], [24], [14], [-38], [32], [-30], [36], [18], [-36], [6], [-40], [-41], [-24], [-47], [0], [-35], [12], [-23], [3], [-12], [24], [27], [-28], [-36], [14], [-8], [-25], [-2], [-27], [4], [-40], [13], [-26], [20], [-28], [41], [16], [24], [-44], [7], [33], [29], [6], [-36], [-2], [-42], [-8], [10], [4], [20], [19], [12], [-43], [-42], [-20], [28], [-11], [23], [-20], [20], [44], [0], [40], [-37], [53], [53], [-20], [25], [-8], [28], [19], [-4], [20], [-59], [21], [54], [48], [-34], [52], [-19], [12], [43], [-4], [43], [-20], [-35], [-16], [-24], [6], [13], [40], [54], [-35], [56], [57], [0], [-14], [-52], [-20], [11], [-24], [-29], [-15], [14], [33], [56], [-46], [-4], [11], [1], [-36], [-18], [-4], [60], [21], [8], [-43], [51], [32], [52], [48], [-33], [-28], [46], [-60], [-53], [61], [32], [51], [16], [-68], [69], [51], [36], [24], [60], [-21], [-4], [56], [-35], [4], [52], [-14], [9], [12], [-16], [-72], [0], [-44], [31], [-34], [-8], [48], [36], [0], [-76], [-62], [9], [-72], [-5], [-11], [-60], [51], [12], [75], [-10], [24], [8], [-43], [34], [51], [16], [-34], [-38], [-40], [-6], [15], [-8], [11], [32], [-34], [56], [-12], [33], [22], [36], [-32], [-40], [-12], [73], [56], [-72], [-46], [10], [32], [25], [-69], [-36], [29], [-72], [66], [-46], [-8], [-45], [64], [-20], [-26], [-79], [-32], [-48], [-68], [-62], [-32], [-17], [64], [-31], [16], [51], [21], [-36], [60], [53], [40], [60], [2], [65], [44], [-25], [-7], [-20], [33], [-14], [-24], [-32], [-48], [57], [-31], [-66], [88], [56], [-48], [-84], [-5], [30], [35], [-28], [51], [-54], [5], [68], [46], [-48], [23], [-8], [-48], [7], [-40], [-70], [47], [24], [25], [-27], [-36], [60], [-27], [12], [26], [54], [-48], [24], [20], [51], [77], [20], [-23], [-60], [88], [20], [-15], [84], [-25], [24], [0], [-95], [-19], [22], [30], [-1], [64], [-34], [-56], [0], [72], [31], [48], [48], [50], [-58], [72], [80], [88], [22], [28], [-9], [80], [-47], [85], [11], [40], [42], [-6], [-60], [-1], [77], [48], [-40], [57], [-15], [-24], [20], [45], [-33], [72], [-52], [50], [-24], [19], [58], [-52], [40], [103], [35], [-92], [-10], [80], [52]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_1936_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_1936_2_a_g();".
// 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_1936_2_a_g(:prec:=1)
    chi := MakeCharacter_1936_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_1936_2_a_g();".
// 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_1936_2_a_g( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_1936_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<3,R![0, 1]>,<5,R![-3, 1]>,<7,R![-4, 1]>],Snew);
    return Vf;
end function;