
// Make newform 3744.2.a.y in Magma, downloaded from the LMFDB on 20 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_y();".
// 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_y();".
// 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
        poly := [-4, -1, 1];
        Kf := NumberField(Polynomial([elt : elt in poly]));
        AssignNames(~Kf, ["nu"]);
    end if;
    Rfbasis := [Kf.1^i : i in [0..Degree(Kf)-1]];
    inp_vec := Vector(Rfbasis)*ChangeRing(Transpose(Matrix([[elt : elt in row] : row in input])),Kf);
    return Eltseq(inp_vec);
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], [0, 0], [1, 1], [1, 1], [-2, 0], [-1, 0], [-1, -1], [6, 0], [0, 0], [2, -4], [4, -2], [-5, 3], [-4, 2], [-1, 5], [3, 3], [8, 2], [6, 0], [4, -6], [6, 0], [-3, -3], [10, 0], [12, 0], [8, -6], [2, -4], [-6, 0], [4, -2], [0, 0], [12, 0], [3, 3], [-2, 4], [-4, -4], [-11, 3], [-8, -2], [3, -3], [2, -4], [15, 3], [2, 0], [0, -6], [0, -6], [-12, 6], [5, -9], [8, -6], [10, -6], [-8, -6], [-9, 3], [6, -6], [11, 5], [19, -5], [-12, 6], [9, -3], [-9, 3], [-17, 3], [2, 0], [16, 0], [-5, 7], [-8, 12], [12, -6], [-3, -3], [8, 6], [-14, 4], [-4, -4], [25, 1], [-6, 0], [6, -6], [3, -9], [26, -4], [12, -6], [-13, -9], [21, 3], [15, -9], [4, -14], [8, -6], [-18, 6], [-4, 6], [20, 2], [-3, -3], [-14, 4], [-6, 0], [-28, 2], [-16, -6], [-1, 9], [-29, 3], [-1, 3], [19, 3], [-26, 10], [3, -3], [-18, 0], [-14, 0], [29, 5], [8, -10], [4, 12], [-29, 3], [-16, 2], [-31, 3], [20, 2], [14, 6], [-2, -8], [-11, 1], [-28, 8], [-23, -3], [9, -9], [23, -13], [-39, 3], [21, -3], [-19, -1], [14, 12], [0, -6], [-14, 4], [-26, 6], [-7, 9], [10, -14], [10, 0], [8, -10], [0, -6], [27, -9], [2, 8], [-18, 0], [2, -18], [32, -10], [-12, 0], [-2, 12], [-7, 9], [-12, 6], [-4, -6], [-8, -2], [0, 6], [-2, -12], [6, -6], [-26, 10], [-11, 9], [-24, 18], [-9, 3], [-20, 4], [0, 6], [30, 0], [0, -6], [21, -3], [20, -10], [-6, -12], [29, -7], [4, -14], [9, 9], [-30, 6], [18, -12], [-14, 12], [-8, 18], [17, -3], [26, -4], [-36, 0], [11, -9], [1, 21], [-1, -13], [-19, -1], [-12, 0], [3, -15], [18, -6], [40, 4], [-20, 10], [22, -12], [-33, 3], [-2, 0], [29, 5], [15, 15], [11, -3], [-26, 16], [-17, 15], [-6, -6], [26, -12], [6, -12], [-2, -20], [-30, 0], [13, -15], [-44, 12], [-8, -6], [2, 14], [-11, 13], [-36, 6], [-22, 20], [12, 12], [8, 18], [0, 6], [12, -12], [-22, -12], [34, 4], [-20, 6], [-26, 16], [2, 0], [14, 8], [-4, -18], [8, -6], [1, 9], [-12, 6], [1, 7], [18, -12], [19, 9], [6, 0], [-4, -18], [-32, -6], [28, -14], [10, -6], [1, 1], [44, 8], [-31, -3], [-35, -3], [0, 6], [10, 16], [23, -1], [-12, 0], [2, -28], [57, 3], [46, -12], [-22, 8], [-12, 0], [-30, -12], [17, -15], [32, -6], [8, -4], [23, 11], [60, -6], [15, 3], [-4, -6], [7, -5], [28, -14], [-33, 15], [-41, -3], [22, 0], [-19, 5], [60, 0], [2, -10], [42, 12], [14, -12], [-36, 0], [56, 2], [1, 13], [-43, -13], [-17, 27], [-30, 24], [-5, 31], [8, -24], [-14, 18], [30, 12], [-9, 21], [-4, -16], [-7, 21], [-22, -4], [24, -24], [35, -1], [12, 18], [-2, -8], [18, 6], [-21, 3], [-6, -12], [-3, -15], [-13, 15], [-36, -6], [22, -24], [12, 6], [26, 20], [-24, 6], [-26, 12], [-36, -12], [-27, 3], [-19, 9], [-26, 0], [-8, -26], [27, -27], [-3, -3], [32, -10], [36, -18], [-8, 10], [-20, -6], [42, 12], [-26, -12], [-4, -28], [-66, 0], [0, 18], [-6, 24], [2, 0], [8, -6], [-9, -27], [-32, 12], [-29, -17], [6, 6], [-34, 12], [40, 10], [54, -6], [-19, -15], [-61, -1], [25, -23], [-2, 16], [-36, 6], [17, -9], [-8, -2], [-2, 24], [6, 12], [4, -14], [34, 10], [-52, 2], [15, 9], [54, -12], [19, -21], [29, -19], [26, 14], [-60, 0], [57, -9], [68, 6], [-31, 27], [28, 18], [-36, 30], [-40, -6], [20, 0], [27, -9], [29, 17], [-44, 16], [33, -27], [-7, 9], [18, 0], [48, -18], [28, -18], [10, -32], [-36, 12], [24, -30], [-4, -10], [-29, 31], [-8, -2], [-14, 0], [30, -12], [-4, -22], [8, -24], [4, 10], [34, -24], [8, 14], [-24, 12], [36, -6], [40, -26], [48, -6], [10, -24], [-12, -6], [20, 6], [18, 6], [-1, -9], [-79, 5], [-55, 17], [-14, -2], [27, -9], [-35, -9], [-22, 0], [36, -18], [57, -3], [34, 4], [34, 4], [-54, 24], [17, 17], [-24, 0], [2, 36], [62, -4], [5, 9], [-30, 0], [-71, 1], [-15, 33], [-10, 12], [34, 16], [-10, -18], [36, -18], [-36, -12], [-3, 21], [3, 15], [-48, 30], [77, -3], [31, -15], [-63, 3], [-33, 15], [2, -16], [-36, 0], [-58, 0], [66, 0], [22, -6], [48, -30], [-34, 32], [-10, 24], [30, -24], [14, 8], [-28, -22], [42, 12], [0, 18], [-21, 27], [37, 25], [12, 18], [10, -20], [52, 0], [2, -24], [-53, 7], [-22, 36], [-29, -11], [63, -9], [-27, -15], [67, -5], [-9, 15], [-12, -24], [37, -11], [3, -21], [30, -36], [3, 15], [-8, -26], [43, -17], [90, -6], [57, -27], [-8, 22], [-46, 32], [-48, 6], [-59, 9], [14, -28], [35, -3], [47, -19], [29, 9], [-52, -10], [-71, 9], [-24, 36], [-90, 0], [18, -42], [4, 22], [70, 12], [-16, -18], [-57, -15], [-12, 6], [2, -4], [-51, -9], [-8, 22], [-60, 6], [70, 6]];
    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_y();".
// 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_y(:prec:=2)
    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_y();".
// 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_y( : 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, -3, 1]>,<7,R![-2, -3, 1]>,<11,R![2, 1]>,<29,R![-68, 0, 1]>],Snew);
    return Vf;
end function;