
// Make newform 3762.2.a.y in Magma, downloaded from the LMFDB on 25 April 2024.
// To make the character of type GrpDrchElt, type "MakeCharacter_3762_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_3762_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_3762_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_3762_a();"
function MakeCharacter_3762_a()
    N := 3762;
    order := 1;
    char_gens := [2927, 343, 2377];
    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_3762_a_Hecke(Kf)
    return MakeCharacter_3762_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, 0], [-2, 0], [1, 1], [-1, 0], [-1, -1], [-1, -1], [-1, 0], [-3, -1], [1, -3], [2, 0], [-2, 2], [-2, 0], [0, 0], [-8, 0], [-5, 5], [3, -3], [0, -4], [-3, 3], [-6, 0], [-5, 3], [-4, 0], [-8, 4], [4, 2], [-4, 2], [10, 2], [-4, 6], [-1, -7], [-11, -3], [0, -2], [-4, -4], [2, 2], [-11, 1], [-2, -6], [10, 2], [4, 0], [-2, 8], [2, -6], [10, 6], [-18, 0], [-12, -4], [-14, -2], [13, 3], [4, -2], [-12, 4], [7, -3], [-5, -3], [6, -12], [-11, 11], [16, 2], [10, -12], [-13, 7], [-4, 10], [-14, 2], [22, 0], [-12, -2], [-6, -2], [21, -3], [-4, -8], [16, -6], [2, -6], [7, 7], [-16, 4], [-3, -5], [1, 1], [-7, -9], [-13, 1], [8, -2], [6, 10], [6, -10], [-5, 11], [5, -7], [24, -4], [5, 1], [5, -1], [2, 0], [-18, 0], [14, -16], [14, 4], [10, -4], [18, 2], [-19, 7], [-10, 14], [16, 6], [6, 10], [-18, 14], [-8, 10], [35, -1], [-8, 12], [16, 8], [-16, -4], [4, -6], [28, -2], [30, -6], [16, 4], [-17, 3], [22, -6], [4, -2], [17, -9], [-8, 20], [-24, 12], [-14, 2], [24, -12], [12, -6], [10, 10], [27, -9], [28, -8], [-34, 0], [-4, -14], [12, -6], [2, 10], [12, -4], [-26, 12], [-16, -4], [16, 8], [-16, 10], [-22, -6], [21, -17], [0, -10], [-9, -15], [-25, 1], [-24, 2], [11, 11], [-16, 8], [-18, -10], [16, -16], [-30, 8], [9, -1], [-1, -15], [-2, -18], [-20, 16], [36, -4], [10, -20], [-30, 8], [35, -5], [-9, 3], [-27, 7], [11, -3], [23, 5], [-19, -7], [13, 3], [-4, -16], [-3, 7], [-39, 7], [-5, -11], [4, -2], [-18, 22], [10, 0], [-6, -14], [6, 4], [-43, 1], [-2, 12], [-24, -12], [32, 0], [3, 21], [6, 16], [31, -1], [-1, 7], [-21, -13], [-23, 1], [12, -16], [4, 2], [32, -14], [4, 4], [-40, -6], [38, 4], [16, 2], [-38, -6], [-4, -6], [-4, -22], [11, 5], [-3, -9], [-32, 14], [-27, -7], [30, -8], [34, 8], [12, -4], [20, -14], [-22, 10], [-8, 16], [17, 7], [11, -3], [34, 2], [12, 2], [37, 3], [-8, 14], [41, 5], [12, 12], [-26, 12], [-17, -5], [-32, 10], [-8, 4], [-4, 20], [1, -1], [-4, -16], [-44, 10], [-5, -13], [24, -2], [-21, -1], [21, -19], [-38, 4], [4, 4], [13, 19], [42, -8], [-22, -6], [2, 24], [27, 13], [-23, 15], [-7, 17], [33, -21], [-1, -5], [32, -22], [0, -20], [9, 3], [26, -18], [-32, 14], [-59, -3], [-12, -10], [-2, -24], [-10, -4], [26, -16], [-28, 4], [-42, 4], [35, 1], [3, -19], [-18, -6], [0, -2], [-8, 18], [-2, -2], [-4, 0], [-14, 0], [-13, -19], [8, 8], [-32, 14], [-16, 20], [1, 9], [14, -32], [-6, 6], [-16, -12], [14, -16], [-36, 12], [-44, 4], [-31, 27], [25, 3], [-32, 22], [64, -2], [-16, 20], [20, -12], [-44, -8], [-33, -1], [16, 8], [-45, -9], [25, -13], [-6, -16], [10, 10], [-6, -18], [-8, 14], [-36, 8], [-18, -6], [-38, -4], [-10, -18], [4, 0], [-48, -4], [-61, -1], [38, 8], [9, -5], [-19, -9], [5, -19], [-63, -1], [-36, 0], [21, 15], [59, -7], [-27, 1], [-1, 15], [28, 2], [5, 11], [44, -8], [-53, -5], [54, -12], [-16, -12], [-34, -14], [15, -3], [15, -13], [-9, 3], [-8, 8], [-8, 8], [-15, -23], [-38, -4], [41, -13], [-12, 30], [-44, -10], [-14, 18], [12, -2], [52, -16], [-18, 0], [-38, -10], [36, 6], [4, 22], [-13, -11], [-8, 28], [-30, 12], [51, 7], [45, 1], [-25, 13], [24, -4], [2, 20], [-61, 5], [23, 1], [24, 16], [-20, -12], [-20, 6], [20, 2], [-4, 30], [12, -24], [40, -14], [38, -12], [-32, 20], [18, 14], [15, 7], [18, 4], [-7, 31], [-33, 11], [-19, -19], [22, -16], [-41, 7], [-60, -6], [-3, 19], [-61, 5], [4, -24], [-50, 10], [-20, 20], [49, -9], [-63, 13], [6, -18], [-10, 22], [-38, -6], [39, 9], [24, -30], [-13, 15], [-17, -13], [11, 5], [-47, -7], [37, 5], [36, -18], [-33, -7], [20, 0], [20, 26], [63, -3], [-36, 36], [24, 6], [12, 24], [22, -8], [-14, 8], [-30, 6], [22, -8], [-28, -10], [-35, 35], [-28, -12], [55, -9], [-2, 0], [-18, 18], [-26, -8], [-22, -20], [81, 7], [54, -2], [-58, 12], [30, -6], [-74, -6], [-54, -4], [-44, 8], [23, -31], [-57, 19], [-19, -7], [-32, -14], [-54, -4], [13, -5], [-8, -30], [66, -16], [33, -27], [0, -30], [-18, 30], [-6, 28], [-5, -13], [2, -28], [-45, -3], [-26, 4], [25, -29], [56, -10], [-10, 26], [46, -12], [34, -8], [21, -25], [54, -2], [-48, -20], [-46, 20], [-34, -12], [-28, 8], [-4, -2], [4, -28], [-10, 6], [-34, 28], [-34, -12], [-20, -16], [26, -24], [74, 0], [49, -11], [14, 16], [-48, 10], [-25, -3], [-28, 0], [-26, 12], [-12, 20], [-61, -7], [-52, 12], [27, 3], [72, 10], [34, 14], [70, -20], [-47, 33], [30, 18], [22, 18], [33, 1], [-53, -3], [-13, 21], [-10, -16], [41, -21], [59, 1], [-41, 19], [50, -30], [63, -21]];
    aps := ConvertToHeckeField(raw_aps);
    chi := MakeCharacter_3762_a_Hecke(Universe(aps));
    return ExtendMultiplicatively(weight, aps, chi);
end function;


// To make the newform (type ModFrm), type "MakeNewformModFrm_3762_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_3762_2_a_y(:prec:=2)
    chi := MakeCharacter_3762_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_3762_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_3762_2_a_y( : sign := -1)
    R<x> := PolynomialRing(Rationals());
    chi := MakeCharacter_3762_a();
    // SetVerbose("ModularSymbols", true);
    Snew := NewSubspace(CuspidalSubspace(ModularSymbols(chi,2,sign)));
    Vf := Kernel([<5,R![2, 1]>,<7,R![-2, -3, 1]>,<13,R![-2, 3, 1]>,<17,R![-2, 3, 1]>],Snew);
    return Vf;
end function;