
// Make newform 3744.2.a.c in Magma, downloaded from the LMFDB on 23 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_3744_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_3744_2_a_c();".
// 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_3744_2_a_c();".
// 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_3744_a();"
function MakeCharacter_3744_a()
    N := 3744;
    order := 1;
    char_gens := [703, 2341, 2081, 2017];
    v := [1, 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_3744_a_Hecke(Kf)
    return MakeCharacter_3744_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 := [[0], [0], [-2], [0], [2], [1], [0], [-4], [4], [8], [-8], [2], [-6], [4], [6], [4], [6], [2], [-4], [6], [-2], [16], [2], [-10], [-2], [-8], [4], [12], [14], [12], [-8], [12], [2], [4], [-6], [8], [14], [20], [22], [-8], [8], [2], [0], [-6], [6], [8], [24], [0], [6], [6], [12], [6], [-2], [4], [20], [12], [-16], [-24], [10], [-6], [-16], [-22], [-20], [20], [-10], [6], [20], [-22], [-16], [2], [-6], [-2], [28], [-6], [-20], [2], [-12], [26], [30], [6], [28], [6], [-30], [-34], [-20], [4], [14], [14], [30], [-16], [-12], [18], [-8], [28], [-4], [-44], [30], [-28], [20], [22], [44], [-38], [-24], [-12], [-16], [-38], [-22], [-10], [-12], [6], [28], [10], [-22], [12], [40], [40], [-20], [-8], [-16], [-40], [-14], [-14], [44], [-46], [28], [20], [-42], [-12], [16], [22], [-20], [-42], [20], [-10], [26], [2], [42], [-4], [-12], [44], [-28], [-30], [20], [-30], [14], [-22], [-30], [12], [40], [-54], [2], [-20], [40], [56], [-32], [32], [0], [-54], [-2], [-22], [38], [-12], [8], [-48], [-26], [-30], [-4], [-34], [-42], [8], [-34], [-30], [20], [46], [-20], [24], [-20], [26], [-12], [2], [-8], [40], [-26], [30], [22], [-4], [42], [-4], [46], [-18], [30], [-14], [-60], [-6], [-16], [52], [-54], [30], [14], [-32], [-66], [-36], [-18], [14], [38], [12], [56], [-44], [30], [8], [-66], [-28], [16], [-14], [42], [50], [-8], [72], [-18], [2], [-30], [-40], [46], [56], [68], [-58], [-24], [-68], [-48], [62], [10], [20], [40], [0], [-36], [-42], [6], [18], [48], [48], [-46], [64], [8], [18], [54], [36], [-48], [78], [-4], [36], [-50], [-54], [-14], [50], [24], [10], [-58], [12], [-28], [34], [-32], [72], [22], [54], [-42], [32], [-50], [-54], [-36], [40], [-14], [68], [54], [4], [2], [64], [70], [-6], [34], [52], [64], [72], [48], [-62], [-68], [80], [-54], [2], [-32], [60], [36], [-48], [-18], [22], [-82], [-12], [-20], [-20], [8], [28], [-54], [-42], [-36], [0], [12], [-10], [72], [58], [90], [-38], [-52], [14], [4], [-64], [-18], [58], [38], [26], [70], [-80], [-60], [-30], [68], [-56], [-82], [-38], [68], [-44], [80], [-48], [-74], [24], [-72], [-46], [28], [-22], [34], [-38], [54], [76], [82], [-12], [34], [92], [6], [40], [-10], [-36], [54], [84], [-76], [22], [50], [80], [58], [92], [-42], [-34], [12], [-54], [-38], [0], [12], [-50], [-68], [10], [62], [-64], [10], [-36], [-76], [-42], [-68], [-56], [26], [-30], [24], [74], [-64], [-42], [-94], [10], [64], [102], [-36], [-30], [-72], [22], [12], [-20], [30], [-86], [18], [40], [30], [2], [8], [-64], [48], [-42], [-46], [-8], [64], [30], [6], [-40], [-42], [78], [-4], [2], [-14], [-4], [-96], [52], [-74], [36], [14], [-24], [102], [-84], [96], [98], [-10], [-44], [-22], [2], [28], [-74], [-20], [-88]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_3744_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_3744_2_a_c();".
// 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_3744_2_a_c(:prec:=1)
    chi := MakeCharacter_3744_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_3744_2_a_c();".
// 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_3744_2_a_c( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_3744_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![2, 1]>,<7,R![0, 1]>,<11,R![-2, 1]>,<29,R![-8, 1]>],Snew);
    return Vf;
end function;